blob: 64053725468b66950cc874718e75b1b8e94442c6 [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 Moolenaar7c0a2f32016-07-10 22:11:16 +02001038 enter_buffer(buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001039# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001040 if (old_tw != curbuf->b_p_tw)
1041 check_colorcolumn(curwin);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001042# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001045
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001046# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001047 /* Restore the error/interrupt/exception state if not discarded by a
1048 * new aborting error, interrupt, or uncaught exception. */
1049 leave_cleanup(&cs);
1050# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 }
1052 else if (swap_exists_action == SEA_RECOVER)
1053 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001054# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001055 /* Reset the error/interrupt/exception state here so that
1056 * aborting() returns FALSE when closing a buffer. */
1057 enter_cleanup(&cs);
1058# endif
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 /* User selected Recover at ATTENTION prompt. */
1061 msg_scroll = TRUE;
1062 ml_recover();
1063 MSG_PUTS("\n"); /* don't overwrite the last message */
1064 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001065 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001066
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001067# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001068 /* Restore the error/interrupt/exception state if not discarded by a
1069 * new aborting error, interrupt, or uncaught exception. */
1070 leave_cleanup(&cs);
1071# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 }
1073 swap_exists_action = SEA_NONE;
1074}
1075#endif
1076
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077/*
1078 * do_bufdel() - delete or unload buffer(s)
1079 *
1080 * addr_count == 0: ":bdel" - delete current buffer
1081 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1082 * buffer "end_bnr", then any other arguments.
1083 * addr_count == 2: ":N,N bdel" - delete buffers in range
1084 *
1085 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1086 * DOBUF_DEL (":bdel")
1087 *
1088 * Returns error message or NULL
1089 */
1090 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001091do_bufdel(
1092 int command,
1093 char_u *arg, /* pointer to extra arguments */
1094 int addr_count,
1095 int start_bnr, /* first buffer number in a range */
1096 int end_bnr, /* buffer nr or last buffer nr in a range */
1097 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098{
1099 int do_current = 0; /* delete current buffer? */
1100 int deleted = 0; /* number of buffers deleted */
1101 char_u *errormsg = NULL; /* return value */
1102 int bnr; /* buffer number */
1103 char_u *p;
1104
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (addr_count == 0)
1106 {
1107 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1108 }
1109 else
1110 {
1111 if (addr_count == 2)
1112 {
1113 if (*arg) /* both range and argument is not allowed */
1114 return (char_u *)_(e_trailing);
1115 bnr = start_bnr;
1116 }
1117 else /* addr_count == 1 */
1118 bnr = end_bnr;
1119
1120 for ( ;!got_int; ui_breakcheck())
1121 {
1122 /*
1123 * delete the current buffer last, otherwise when the
1124 * current buffer is deleted, the next buffer becomes
1125 * the current one and will be loaded, which may then
1126 * also be deleted, etc.
1127 */
1128 if (bnr == curbuf->b_fnum)
1129 do_current = bnr;
1130 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1131 forceit) == OK)
1132 ++deleted;
1133
1134 /*
1135 * find next buffer number to delete/unload
1136 */
1137 if (addr_count == 2)
1138 {
1139 if (++bnr > end_bnr)
1140 break;
1141 }
1142 else /* addr_count == 1 */
1143 {
1144 arg = skipwhite(arg);
1145 if (*arg == NUL)
1146 break;
1147 if (!VIM_ISDIGIT(*arg))
1148 {
1149 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001150 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1151 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 if (bnr < 0) /* failed */
1153 break;
1154 arg = p;
1155 }
1156 else
1157 bnr = getdigits(&arg);
1158 }
1159 }
1160 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1161 FORWARD, do_current, forceit) == OK)
1162 ++deleted;
1163
1164 if (deleted == 0)
1165 {
1166 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001167 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001169 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001171 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 errormsg = IObuff;
1173 }
1174 else if (deleted >= p_report)
1175 {
1176 if (command == DOBUF_UNLOAD)
1177 {
1178 if (deleted == 1)
1179 MSG(_("1 buffer unloaded"));
1180 else
1181 smsg((char_u *)_("%d buffers unloaded"), deleted);
1182 }
1183 else if (command == DOBUF_DEL)
1184 {
1185 if (deleted == 1)
1186 MSG(_("1 buffer deleted"));
1187 else
1188 smsg((char_u *)_("%d buffers deleted"), deleted);
1189 }
1190 else
1191 {
1192 if (deleted == 1)
1193 MSG(_("1 buffer wiped out"));
1194 else
1195 smsg((char_u *)_("%d buffers wiped out"), deleted);
1196 }
1197 }
1198 }
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201 return errormsg;
1202}
1203
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001204static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001205
1206/*
1207 * Make the current buffer empty.
1208 * Used when it is wiped out and it's the last buffer.
1209 */
1210 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001211empty_curbuf(
1212 int close_others,
1213 int forceit,
1214 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001215{
1216 int retval;
1217 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001218 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001219
1220 if (action == DOBUF_UNLOAD)
1221 {
1222 EMSG(_("E90: Cannot unload last buffer"));
1223 return FAIL;
1224 }
1225
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001226 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001227 if (close_others)
1228 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001229 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001230
1231 setpcmark();
1232 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1233 forceit ? ECMD_FORCEIT : 0, curwin);
1234
1235 /*
1236 * do_ecmd() may create a new buffer, then we have to delete
1237 * the old one. But do_ecmd() may have done that already, check
1238 * if the buffer still exists.
1239 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001240 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001241 close_buffer(NULL, buf, action, FALSE);
1242 if (!close_others)
1243 need_fileinfo = FALSE;
1244 return retval;
1245}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246/*
1247 * Implementation of the commands for the buffer list.
1248 *
1249 * action == DOBUF_GOTO go to specified buffer
1250 * action == DOBUF_SPLIT split window and go to specified buffer
1251 * action == DOBUF_UNLOAD unload specified buffer(s)
1252 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1253 * action == DOBUF_WIPE delete specified buffer(s) really
1254 *
1255 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1256 * start == DOBUF_FIRST go to "count" buffer from first buffer
1257 * start == DOBUF_LAST go to "count" buffer from last buffer
1258 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1259 *
1260 * Return FAIL or OK.
1261 */
1262 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001263do_buffer(
1264 int action,
1265 int start,
1266 int dir, /* FORWARD or BACKWARD */
1267 int count, /* buffer number or number of buffers */
1268 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269{
1270 buf_T *buf;
1271 buf_T *bp;
1272 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1273 || action == DOBUF_WIPE);
1274
1275 switch (start)
1276 {
1277 case DOBUF_FIRST: buf = firstbuf; break;
1278 case DOBUF_LAST: buf = lastbuf; break;
1279 default: buf = curbuf; break;
1280 }
1281 if (start == DOBUF_MOD) /* find next modified buffer */
1282 {
1283 while (count-- > 0)
1284 {
1285 do
1286 {
1287 buf = buf->b_next;
1288 if (buf == NULL)
1289 buf = firstbuf;
1290 }
1291 while (buf != curbuf && !bufIsChanged(buf));
1292 }
1293 if (!bufIsChanged(buf))
1294 {
1295 EMSG(_("E84: No modified buffer found"));
1296 return FAIL;
1297 }
1298 }
1299 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1300 {
1301 while (buf != NULL && buf->b_fnum != count)
1302 buf = buf->b_next;
1303 }
1304 else
1305 {
1306 bp = NULL;
1307 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1308 {
1309 /* remember the buffer where we start, we come back there when all
1310 * buffers are unlisted. */
1311 if (bp == NULL)
1312 bp = buf;
1313 if (dir == FORWARD)
1314 {
1315 buf = buf->b_next;
1316 if (buf == NULL)
1317 buf = firstbuf;
1318 }
1319 else
1320 {
1321 buf = buf->b_prev;
1322 if (buf == NULL)
1323 buf = lastbuf;
1324 }
1325 /* don't count unlisted buffers */
1326 if (unload || buf->b_p_bl)
1327 {
1328 --count;
1329 bp = NULL; /* use this buffer as new starting point */
1330 }
1331 if (bp == buf)
1332 {
1333 /* back where we started, didn't find anything. */
1334 EMSG(_("E85: There is no listed buffer"));
1335 return FAIL;
1336 }
1337 }
1338 }
1339
1340 if (buf == NULL) /* could not find it */
1341 {
1342 if (start == DOBUF_FIRST)
1343 {
1344 /* don't warn when deleting */
1345 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001346 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 }
1348 else if (dir == FORWARD)
1349 EMSG(_("E87: Cannot go beyond last buffer"));
1350 else
1351 EMSG(_("E88: Cannot go before first buffer"));
1352 return FAIL;
1353 }
1354
1355#ifdef FEAT_GUI
1356 need_mouse_correct = TRUE;
1357#endif
1358
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 /*
1360 * delete buffer buf from memory and/or the list
1361 */
1362 if (unload)
1363 {
1364 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001365 bufref_T bufref;
1366
Bram Moolenaara997b452018-04-17 23:24:06 +02001367 if (buf->b_locked)
1368 {
1369 EMSG(_(e_buflocked));
1370 return FAIL;
1371 }
1372
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001373 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375 /* When unloading or deleting a buffer that's already unloaded and
1376 * unlisted: fail silently. */
1377 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1378 return FAIL;
1379
1380 if (!forceit && bufIsChanged(buf))
1381 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001382#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if ((p_confirm || cmdmod.confirm) && p_write)
1384 {
1385 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001386 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 /* Autocommand deleted buffer, oops! It's not changed
1388 * now. */
1389 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001390 /* If it's still changed fail silently, the dialog already
1391 * mentioned why it fails. */
1392 if (bufIsChanged(buf))
1393 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001395 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 {
1398 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1399 buf->b_fnum);
1400 return FAIL;
1401 }
1402 }
1403
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001404 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001405 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001406 end_visual_mode();
1407
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 /*
1409 * If deleting the last (listed) buffer, make it empty.
1410 * The last (listed) buffer cannot be unloaded.
1411 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001412 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (bp->b_p_bl && bp != buf)
1414 break;
1415 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001416 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 /*
1419 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001420 * (unless it's the only window). Repeat this so long as we end up in
1421 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001423 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001424 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001425 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001426 {
1427 if (win_close(curwin, FALSE) == FAIL)
1428 break;
1429 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430
1431 /*
1432 * If the buffer to be deleted is not the current one, delete it here.
1433 */
1434 if (buf != curbuf)
1435 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001436 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001437 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001438 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 return OK;
1440 }
1441
1442 /*
1443 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001444 * There should be another, otherwise it would have been handled
1445 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001446 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 * Then prefer the buffer we most recently visited.
1448 * Else try to find one that is loaded, after the current buffer,
1449 * then before the current buffer.
1450 * Finally use any buffer.
1451 */
1452 buf = NULL; /* selected buffer */
1453 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001454 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1455 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001457 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 {
1459 int jumpidx;
1460
1461 jumpidx = curwin->w_jumplistidx - 1;
1462 if (jumpidx < 0)
1463 jumpidx = curwin->w_jumplistlen - 1;
1464
1465 forward = jumpidx;
1466 while (jumpidx != curwin->w_jumplistidx)
1467 {
1468 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1469 if (buf != NULL)
1470 {
1471 if (buf == curbuf || !buf->b_p_bl)
1472 buf = NULL; /* skip current and unlisted bufs */
1473 else if (buf->b_ml.ml_mfp == NULL)
1474 {
1475 /* skip unloaded buf, but may keep it for later */
1476 if (bp == NULL)
1477 bp = buf;
1478 buf = NULL;
1479 }
1480 }
1481 if (buf != NULL) /* found a valid buffer: stop searching */
1482 break;
1483 /* advance to older entry in jump list */
1484 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1485 break;
1486 if (--jumpidx < 0)
1487 jumpidx = curwin->w_jumplistlen - 1;
1488 if (jumpidx == forward) /* List exhausted for sure */
1489 break;
1490 }
1491 }
1492#endif
1493
1494 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1495 {
1496 forward = TRUE;
1497 buf = curbuf->b_next;
1498 for (;;)
1499 {
1500 if (buf == NULL)
1501 {
1502 if (!forward) /* tried both directions */
1503 break;
1504 buf = curbuf->b_prev;
1505 forward = FALSE;
1506 continue;
1507 }
1508 /* in non-help buffer, try to skip help buffers, and vv */
1509 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1510 {
1511 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1512 break;
1513 if (bp == NULL) /* remember unloaded buf for later */
1514 bp = buf;
1515 }
1516 if (forward)
1517 buf = buf->b_next;
1518 else
1519 buf = buf->b_prev;
1520 }
1521 }
1522 if (buf == NULL) /* No loaded buffer, use unloaded one */
1523 buf = bp;
1524 if (buf == NULL) /* No loaded buffer, find listed one */
1525 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001526 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 if (buf->b_p_bl && buf != curbuf)
1528 break;
1529 }
1530 if (buf == NULL) /* Still no buffer, just take one */
1531 {
1532 if (curbuf->b_next != NULL)
1533 buf = curbuf->b_next;
1534 else
1535 buf = curbuf->b_prev;
1536 }
1537 }
1538
Bram Moolenaara02471e2014-01-10 16:43:14 +01001539 if (buf == NULL)
1540 {
1541 /* Autocommands must have wiped out all other buffers. Only option
1542 * now is to make the current buffer empty. */
1543 return empty_curbuf(FALSE, forceit, action);
1544 }
1545
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 /*
1547 * make buf current buffer
1548 */
1549 if (action == DOBUF_SPLIT) /* split window first */
1550 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001551 /* If 'switchbuf' contains "useopen": jump to first window containing
1552 * "buf" if one exists */
1553 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001554 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001555 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001556 * page containing "buf" if one exists */
1557 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 return OK;
1559 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 return FAIL;
1561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562
1563 /* go to current buffer - nothing to do */
1564 if (buf == curbuf)
1565 return OK;
1566
1567 /*
1568 * Check if the current buffer may be abandoned.
1569 */
1570 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1571 {
1572#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1573 if ((p_confirm || cmdmod.confirm) && p_write)
1574 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001575 bufref_T bufref;
1576
1577 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001579 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 /* Autocommand deleted buffer, oops! */
1581 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 }
1583 if (bufIsChanged(curbuf))
1584#endif
1585 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001586 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 return FAIL;
1588 }
1589 }
1590
1591 /* Go to the other buffer. */
1592 set_curbuf(buf, action);
1593
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001595 {
1596 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001599#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 if (aborting()) /* autocmds may abort script processing */
1601 return FAIL;
1602#endif
1603
1604 return OK;
1605}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606
1607/*
1608 * Set current buffer to "buf". Executes autocommands and closes current
1609 * buffer. "action" tells how to close the current buffer:
1610 * DOBUF_GOTO free or hide it
1611 * DOBUF_SPLIT nothing
1612 * DOBUF_UNLOAD unload it
1613 * DOBUF_DEL delete it
1614 * DOBUF_WIPE wipe it out
1615 */
1616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001617set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618{
1619 buf_T *prevbuf;
1620 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1621 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001622#ifdef FEAT_SYN_HL
1623 long old_tw = curbuf->b_p_tw;
1624#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001625 bufref_T newbufref;
1626 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001629 if (!cmdmod.keepalt)
1630 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001631 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 /* Don't restart Select mode after switching to another buffer. */
1634 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001636 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1637 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001639 set_bufref(&prevbufref, prevbuf);
1640 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001642 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1643 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001644 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001645 || (bufref_valid(&prevbufref)
1646 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001647#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001648 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001650 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001652#ifdef FEAT_SYN_HL
1653 if (prevbuf == curwin->w_buffer)
1654 reset_synblock(curwin);
1655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001657 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001658#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001659 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001660#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001661 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001663 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001664 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001665 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001666 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1668 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001669 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001670 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001671 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001672 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001673 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001676 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001677 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001678 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1679 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001680#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001681 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001683 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001684 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001686#ifdef FEAT_SYN_HL
1687 if (old_tw != curbuf->b_p_tw)
1688 check_colorcolumn(curwin);
1689#endif
1690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691}
1692
1693/*
1694 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001695 * Old curbuf must have been abandoned already! This also means "curbuf" may
1696 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 */
1698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700{
1701 /* Copy buffer and window local option values. Not for a help buffer. */
1702 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1703 if (!buf->b_help)
1704 get_winopts(buf);
1705#ifdef FEAT_FOLDING
1706 else
1707 /* Remove all folds in the window. */
1708 clearFolding(curwin);
1709 foldUpdateAll(curwin); /* update folds (later). */
1710#endif
1711
1712 /* Get the buffer in the current window. */
1713 curwin->w_buffer = buf;
1714 curbuf = buf;
1715 ++curbuf->b_nwindows;
1716
1717#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001718 if (curwin->w_p_diff)
1719 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720#endif
1721
Bram Moolenaara971b822011-09-14 14:43:25 +02001722#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001723 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001724#endif
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 /* Cursor on first line by default. */
1727 curwin->w_cursor.lnum = 1;
1728 curwin->w_cursor.col = 0;
1729#ifdef FEAT_VIRTUALEDIT
1730 curwin->w_cursor.coladd = 0;
1731#endif
1732 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001733 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001735 /* mark cursor position as being invalid */
1736 curwin->w_valid = 0;
1737
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 /* Make sure the buffer is loaded. */
1739 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001740 {
1741 /* If there is no filetype, allow for detecting one. Esp. useful for
1742 * ":ball" used in a autocommand. If there already is a filetype we
1743 * might prefer to keep it. */
1744 if (*curbuf->b_p_ft == NUL)
1745 did_filetype = FALSE;
1746
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001747 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 else
1750 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001751 if (!msg_silent)
1752 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001755#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001757#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1759 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761
1762 /* If autocommands did not change the cursor position, restore cursor lnum
1763 * and possibly cursor col. */
1764 if (curwin->w_cursor.lnum == 1 && inindent(0))
1765 buflist_getfpos();
1766
1767 check_arg_idx(curwin); /* check for valid arg_idx */
1768#ifdef FEAT_TITLE
1769 maketitle();
1770#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001771 /* when autocmds didn't change it */
1772 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1774
Bram Moolenaar009b2592004-10-24 19:18:58 +00001775#ifdef FEAT_NETBEANS_INTG
1776 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001777 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001778#endif
1779
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001780 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001781 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783#ifdef FEAT_KEYMAP
1784 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001785 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001787#ifdef FEAT_SPELL
1788 /* May need to set the spell language. Can only do this after the buffer
1789 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001790 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1791 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001792#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001793#ifdef FEAT_VIMINFO
1794 curbuf->b_last_used = vim_time();
1795#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001796
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 redraw_later(NOT_VALID);
1798}
1799
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001800#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1801/*
1802 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001803 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001804 */
1805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001806do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001807{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001808 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001809 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001810 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001811 shorten_fnames(TRUE);
1812}
1813#endif
1814
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001815 void
1816no_write_message(void)
1817{
1818#ifdef FEAT_TERMINAL
1819 if (term_job_running(curbuf->b_term))
1820 EMSG(_("E948: Job still running (add ! to end the job)"));
1821 else
1822#endif
1823 EMSG(_("E37: No write since last change (add ! to override)"));
1824}
1825
1826 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001827no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001828{
1829#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001830 if (term_job_running(buf->b_term))
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001831 EMSG(_("E948: Job still running"));
1832 else
1833#endif
1834 EMSG(_("E37: No write since last change"));
1835}
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837/*
1838 * functions for dealing with the buffer list
1839 */
1840
Bram Moolenaar480778b2016-07-14 22:09:39 +02001841static int top_file_num = 1; /* highest file number */
1842
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001844 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1845 * only one window. That means it can be re-used.
1846 */
1847 int
1848curbuf_reusable(void)
1849{
1850 return (curbuf != NULL
1851 && curbuf->b_ffname == NULL
1852 && curbuf->b_nwindows <= 1
1853 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
1854 && !curbufIsChanged());
1855}
1856
1857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 * Add a file name to the buffer list. Return a pointer to the buffer.
1859 * If the same file name already exists return a pointer to that buffer.
1860 * If it does not exist, or if fname == NULL, a new entry is created.
1861 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1862 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1863 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001864 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001865 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1866 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 * This is the ONLY way to create a new buffer.
1868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001870buflist_new(
1871 char_u *ffname, /* full path of fname or relative */
1872 char_u *sfname, /* short fname or NULL */
1873 linenr_T lnum, /* preferred cursor line */
1874 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875{
1876 buf_T *buf;
1877#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001878 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879#endif
1880
Bram Moolenaar480778b2016-07-14 22:09:39 +02001881 if (top_file_num == 1)
1882 hash_init(&buf_hashtab);
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1885
1886 /*
1887 * If file name already exists in the list, update the entry.
1888 */
1889#ifdef UNIX
1890 /* On Unix we can use inode numbers when the file exists. Works better
1891 * for hard links. */
1892 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1893 st.st_dev = (dev_T)-1;
1894#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001895 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896#ifdef UNIX
1897 buflist_findname_stat(ffname, &st)
1898#else
1899 buflist_findname(ffname)
1900#endif
1901 ) != NULL)
1902 {
1903 vim_free(ffname);
1904 if (lnum != 0)
1905 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001906
1907 if ((flags & BLN_NOOPT) == 0)
1908 /* copy the options now, if 'cpo' doesn't have 's' and not done
1909 * already */
1910 buf_copy_options(buf, 0);
1911
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1913 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001914 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001915
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001917 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001919 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001920 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001921 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001922 return NULL;
1923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 return buf;
1926 }
1927
1928 /*
1929 * If the current buffer has no name and no contents, use the current
1930 * buffer. Otherwise: Need to allocate a new buffer structure.
1931 *
1932 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001933 * (A spell file buffer is allocated in spell.c, but that's not a normal
1934 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 */
1936 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001937 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 {
1939 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 /* It's like this buffer is deleted. Watch out for autocommands that
1941 * change curbuf! If that happens, allocate a new buffer anyway. */
1942 if (curbuf->b_p_bl)
1943 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1944 if (buf == curbuf)
1945 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001946#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001947 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 {
1952 /* Make sure 'bufhidden' and 'buftype' are empty */
1953 clear_string_option(&buf->b_p_bh);
1954 clear_string_option(&buf->b_p_bt);
1955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 }
1957 if (buf != curbuf || curbuf == NULL)
1958 {
1959 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1960 if (buf == NULL)
1961 {
1962 vim_free(ffname);
1963 return NULL;
1964 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001965#ifdef FEAT_EVAL
1966 /* init b: variables */
1967 buf->b_vars = dict_alloc();
1968 if (buf->b_vars == NULL)
1969 {
1970 vim_free(ffname);
1971 vim_free(buf);
1972 return NULL;
1973 }
1974 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1975#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001976 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 }
1978
1979 if (ffname != NULL)
1980 {
1981 buf->b_ffname = ffname;
1982 buf->b_sfname = vim_strsave(sfname);
1983 }
1984
1985 clear_wininfo(buf);
1986 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1987
1988 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1989 || buf->b_wininfo == NULL)
1990 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001991 VIM_CLEAR(buf->b_ffname);
1992 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 if (buf != curbuf)
1994 free_buffer(buf);
1995 return NULL;
1996 }
1997
1998 if (buf == curbuf)
1999 {
2000 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002001 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 if (buf != curbuf) /* autocommands deleted the buffer! */
2003 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002004#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002005 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 return NULL;
2007#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002009
2010 /* Init the options. */
2011 buf->b_p_initialized = FALSE;
2012 buf_copy_options(buf, BCO_ENTER);
2013
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014#ifdef FEAT_KEYMAP
2015 /* need to reload lmaps and set b:keymap_name */
2016 curbuf->b_kmap_state |= KEYMAP_INIT;
2017#endif
2018 }
2019 else
2020 {
2021 /*
2022 * put new buffer at the end of the buffer list
2023 */
2024 buf->b_next = NULL;
2025 if (firstbuf == NULL) /* buffer list is empty */
2026 {
2027 buf->b_prev = NULL;
2028 firstbuf = buf;
2029 }
2030 else /* append new buffer at end of list */
2031 {
2032 lastbuf->b_next = buf;
2033 buf->b_prev = lastbuf;
2034 }
2035 lastbuf = buf;
2036
2037 buf->b_fnum = top_file_num++;
2038 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2039 {
2040 EMSG(_("W14: Warning: List of file names overflow"));
2041 if (emsg_silent == 0)
2042 {
2043 out_flush();
2044 ui_delay(3000L, TRUE); /* make sure it is noticed */
2045 }
2046 top_file_num = 1;
2047 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002048 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049
2050 /*
2051 * Always copy the options from the current buffer.
2052 */
2053 buf_copy_options(buf, BCO_ALWAYS);
2054 }
2055
2056 buf->b_wininfo->wi_fpos.lnum = lnum;
2057 buf->b_wininfo->wi_win = curwin;
2058
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002059#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002060 hash_init(&buf->b_s.b_keywtab);
2061 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062#endif
2063
2064 buf->b_fname = buf->b_sfname;
2065#ifdef UNIX
2066 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002067 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 else
2069 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002070 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 buf->b_dev = st.st_dev;
2072 buf->b_ino = st.st_ino;
2073 }
2074#endif
2075 buf->b_u_synced = TRUE;
2076 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002077 if (flags & BLN_DUMMY)
2078 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 buf_clear_file(buf);
2080 clrallmarks(buf); /* clear marks */
2081 fmarks_check_names(buf); /* check file marks for this file */
2082 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 if (!(flags & BLN_DUMMY))
2084 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002085 bufref_T bufref;
2086
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002087 /* Tricky: these autocommands may change the buffer list. They could
2088 * also split the window with re-using the one empty buffer. This may
2089 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002090 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002091 if (apply_autocmds(EVENT_BUFNEW, 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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002095 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002096 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002097 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002098 return NULL;
2099 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002100#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002101 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002104 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105
2106 return buf;
2107}
2108
2109/*
2110 * Free the memory for the options of a buffer.
2111 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2112 * 'fileencoding'.
2113 */
2114 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002115free_buf_options(
2116 buf_T *buf,
2117 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
2119 if (free_p_ff)
2120 {
2121#ifdef FEAT_MBYTE
2122 clear_string_option(&buf->b_p_fenc);
2123#endif
2124 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 clear_string_option(&buf->b_p_bh);
2126 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
2128#ifdef FEAT_FIND_ID
2129 clear_string_option(&buf->b_p_def);
2130 clear_string_option(&buf->b_p_inc);
2131# ifdef FEAT_EVAL
2132 clear_string_option(&buf->b_p_inex);
2133# endif
2134#endif
2135#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2136 clear_string_option(&buf->b_p_inde);
2137 clear_string_option(&buf->b_p_indk);
2138#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002139#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2140 clear_string_option(&buf->b_p_bexpr);
2141#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002142#if defined(FEAT_CRYPT)
2143 clear_string_option(&buf->b_p_cm);
2144#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002145 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002146#if defined(FEAT_EVAL)
2147 clear_string_option(&buf->b_p_fex);
2148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149#ifdef FEAT_CRYPT
2150 clear_string_option(&buf->b_p_key);
2151#endif
2152 clear_string_option(&buf->b_p_kp);
2153 clear_string_option(&buf->b_p_mps);
2154 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002155 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002157#ifdef FEAT_VARTABS
2158 clear_string_option(&buf->b_p_vsts);
2159 if (buf->b_p_vsts_nopaste)
2160 vim_free(buf->b_p_vsts_nopaste);
2161 buf->b_p_vsts_nopaste = NULL;
2162 if (buf->b_p_vsts_array)
2163 vim_free(buf->b_p_vsts_array);
2164 buf->b_p_vsts_array = NULL;
2165 clear_string_option(&buf->b_p_vts);
2166 if (buf->b_p_vts_array)
2167 vim_free(buf->b_p_vts_array);
2168 buf->b_p_vts_array = NULL;
2169#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170#ifdef FEAT_KEYMAP
2171 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002172 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 ga_clear(&buf->b_kmap_ga);
2174#endif
2175#ifdef FEAT_COMMENTS
2176 clear_string_option(&buf->b_p_com);
2177#endif
2178#ifdef FEAT_FOLDING
2179 clear_string_option(&buf->b_p_cms);
2180#endif
2181 clear_string_option(&buf->b_p_nf);
2182#ifdef FEAT_SYN_HL
2183 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002184 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002185#endif
2186#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002187 clear_string_option(&buf->b_s.b_p_spc);
2188 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002189 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002190 buf->b_s.b_cap_prog = NULL;
2191 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#endif
2193#ifdef FEAT_SEARCHPATH
2194 clear_string_option(&buf->b_p_sua);
2195#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197#ifdef FEAT_CINDENT
2198 clear_string_option(&buf->b_p_cink);
2199 clear_string_option(&buf->b_p_cino);
2200#endif
2201#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2202 clear_string_option(&buf->b_p_cinw);
2203#endif
2204#ifdef FEAT_INS_EXPAND
2205 clear_string_option(&buf->b_p_cpt);
2206#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002207#ifdef FEAT_COMPL_FUNC
2208 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002209 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211#ifdef FEAT_QUICKFIX
2212 clear_string_option(&buf->b_p_gp);
2213 clear_string_option(&buf->b_p_mp);
2214 clear_string_option(&buf->b_p_efm);
2215#endif
2216 clear_string_option(&buf->b_p_ep);
2217 clear_string_option(&buf->b_p_path);
2218 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002219 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#ifdef FEAT_INS_EXPAND
2221 clear_string_option(&buf->b_p_dict);
2222 clear_string_option(&buf->b_p_tsr);
2223#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002224#ifdef FEAT_TEXTOBJ
2225 clear_string_option(&buf->b_p_qe);
2226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002228 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002229#ifdef FEAT_LISP
2230 clear_string_option(&buf->b_p_lw);
2231#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002232 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002233#ifdef FEAT_MBYTE
2234 clear_string_option(&buf->b_p_menc);
2235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236}
2237
2238/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002239 * Get alternate file "n".
2240 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2241 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 * if (options & GETF_SETMARK) call setpcmark()
2243 * if (options & GETF_ALT) we are jumping to an alternate file.
2244 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2245 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002246 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 */
2248 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002249buflist_getfile(
2250 int n,
2251 linenr_T lnum,
2252 int options,
2253 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254{
2255 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 pos_T *fpos;
2258 colnr_T col;
2259
2260 buf = buflist_findnr(n);
2261 if (buf == NULL)
2262 {
2263 if ((options & GETF_ALT) && n == 0)
2264 EMSG(_(e_noalt));
2265 else
2266 EMSGN(_("E92: Buffer %ld not found"), n);
2267 return FAIL;
2268 }
2269
2270 /* if alternate file is the current buffer, nothing to do */
2271 if (buf == curbuf)
2272 return OK;
2273
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002274 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002275 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002276 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002278 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002279 if (curbuf_locked())
2280 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281
2282 /* altfpos may be changed by getfile(), get it now */
2283 if (lnum == 0)
2284 {
2285 fpos = buflist_findfpos(buf);
2286 lnum = fpos->lnum;
2287 col = fpos->col;
2288 }
2289 else
2290 col = 0;
2291
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 if (options & GETF_SWITCH)
2293 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002294 /* If 'switchbuf' contains "useopen": jump to first window containing
2295 * "buf" if one exists */
2296 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002298
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002299 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002300 * page containing "buf" if one exists */
2301 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002302 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002303
2304 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2305 * current buffer isn't empty: open new tab or window */
2306 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002307 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002309 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002310 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002311 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2312 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002314 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 }
2316 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317
2318 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002319 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2320 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
2322 --RedrawingDisabled;
2323
2324 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2325 if (!p_sol && col != 0)
2326 {
2327 curwin->w_cursor.col = col;
2328 check_cursor_col();
2329#ifdef FEAT_VIRTUALEDIT
2330 curwin->w_cursor.coladd = 0;
2331#endif
2332 curwin->w_set_curswant = TRUE;
2333 }
2334 return OK;
2335 }
2336 --RedrawingDisabled;
2337 return FAIL;
2338}
2339
2340/*
2341 * go to the last know line number for the current buffer
2342 */
2343 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002344buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345{
2346 pos_T *fpos;
2347
2348 fpos = buflist_findfpos(curbuf);
2349
2350 curwin->w_cursor.lnum = fpos->lnum;
2351 check_cursor_lnum();
2352
2353 if (p_sol)
2354 curwin->w_cursor.col = 0;
2355 else
2356 {
2357 curwin->w_cursor.col = fpos->col;
2358 check_cursor_col();
2359#ifdef FEAT_VIRTUALEDIT
2360 curwin->w_cursor.coladd = 0;
2361#endif
2362 curwin->w_set_curswant = TRUE;
2363 }
2364}
2365
Bram Moolenaar81695252004-12-29 20:58:21 +00002366#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2367/*
2368 * Find file in buffer list by name (it has to be for the current window).
2369 * Returns NULL if not found.
2370 */
2371 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002372buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002373{
2374 char_u *ffname;
2375 buf_T *buf = NULL;
2376
2377 /* First make the name into a full path name */
2378 ffname = FullName_save(fname,
2379#ifdef UNIX
2380 TRUE /* force expansion, get rid of symbolic links */
2381#else
2382 FALSE
2383#endif
2384 );
2385 if (ffname != NULL)
2386 {
2387 buf = buflist_findname(ffname);
2388 vim_free(ffname);
2389 }
2390 return buf;
2391}
2392#endif
2393
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394/*
2395 * Find file in buffer list by name (it has to be for the current window).
2396 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002397 * Skips dummy buffers.
2398 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
2400 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002401buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402{
2403#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002404 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 if (mch_stat((char *)ffname, &st) < 0)
2407 st.st_dev = (dev_T)-1;
2408 return buflist_findname_stat(ffname, &st);
2409}
2410
2411/*
2412 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2413 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002414 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 */
2416 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002417buflist_findname_stat(
2418 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002419 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420{
2421#endif
2422 buf_T *buf;
2423
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002424 /* Start at the last buffer, expect to find a match sooner. */
2425 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002426 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427#ifdef UNIX
2428 , stp
2429#endif
2430 ))
2431 return buf;
2432 return NULL;
2433}
2434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435/*
2436 * Find file in buffer list by a regexp pattern.
2437 * Return fnum of the found buffer.
2438 * Return < 0 for error.
2439 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002441buflist_findpat(
2442 char_u *pattern,
2443 char_u *pattern_end, /* pointer to first char after pattern */
2444 int unlisted, /* find unlisted buffers */
2445 int diffmode UNUSED, /* find diff-mode buffers only */
2446 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447{
2448 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 int match = -1;
2450 int find_listed;
2451 char_u *pat;
2452 char_u *patend;
2453 int attempt;
2454 char_u *p;
2455 int toggledollar;
2456
2457 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2458 {
2459 if (*pattern == '%')
2460 match = curbuf->b_fnum;
2461 else
2462 match = curwin->w_alt_fnum;
2463#ifdef FEAT_DIFF
2464 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2465 match = -1;
2466#endif
2467 }
2468
2469 /*
2470 * Try four ways of matching a listed buffer:
2471 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002472 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 * attempt == 2: with '$' at end (only match at end)
2474 * attempt == 3: with '^' at start and '$' at end (only full match)
2475 * Repeat this for finding an unlisted buffer if there was no matching
2476 * listed buffer.
2477 */
2478 else
2479 {
2480 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2481 if (pat == NULL)
2482 return -1;
2483 patend = pat + STRLEN(pat) - 1;
2484 toggledollar = (patend > pat && *patend == '$');
2485
2486 /* First try finding a listed buffer. If not found and "unlisted"
2487 * is TRUE, try finding an unlisted buffer. */
2488 find_listed = TRUE;
2489 for (;;)
2490 {
2491 for (attempt = 0; attempt <= 3; ++attempt)
2492 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002493 regmatch_T regmatch;
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 /* may add '^' and '$' */
2496 if (toggledollar)
2497 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2498 p = pat;
2499 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2500 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002501 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2502 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
2504 vim_free(pat);
2505 return -1;
2506 }
2507
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002508 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (buf->b_p_bl == find_listed
2510#ifdef FEAT_DIFF
2511 && (!diffmode || diff_mode_buf(buf))
2512#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002513 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002515 if (curtab_only)
2516 {
2517 /* Ignore the match if the buffer is not open in
2518 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002519 win_T *wp;
2520
Bram Moolenaar29323592016-07-24 22:04:11 +02002521 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002522 if (wp->w_buffer == buf)
2523 break;
2524 if (wp == NULL)
2525 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (match >= 0) /* already found a match */
2528 {
2529 match = -2;
2530 break;
2531 }
2532 match = buf->b_fnum; /* remember first match */
2533 }
2534
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002535 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (match >= 0) /* found one match */
2537 break;
2538 }
2539
2540 /* Only search for unlisted buffers if there was no match with
2541 * a listed buffer. */
2542 if (!unlisted || !find_listed || match != -1)
2543 break;
2544 find_listed = FALSE;
2545 }
2546
2547 vim_free(pat);
2548 }
2549
2550 if (match == -2)
2551 EMSG2(_("E93: More than one match for %s"), pattern);
2552 else if (match < 0)
2553 EMSG2(_("E94: No matching buffer for %s"), pattern);
2554 return match;
2555}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556
2557#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2558
2559/*
2560 * Find all buffer names that match.
2561 * For command line expansion of ":buf" and ":sbuf".
2562 * Return OK if matches found, FAIL otherwise.
2563 */
2564 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002565ExpandBufnames(
2566 char_u *pat,
2567 int *num_file,
2568 char_u ***file,
2569 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570{
2571 int count = 0;
2572 buf_T *buf;
2573 int round;
2574 char_u *p;
2575 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002576 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
2578 *num_file = 0; /* return values in case of FAIL */
2579 *file = NULL;
2580
Bram Moolenaar05159a02005-02-26 23:04:13 +00002581 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2582 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002584 patc = alloc((unsigned)STRLEN(pat) + 11);
2585 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002587 STRCPY(patc, "\\(^\\|[\\/]\\)");
2588 STRCPY(patc + 11, pat + 1);
2589 }
2590 else
2591 patc = pat;
2592
2593 /*
2594 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002595 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002596 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002597 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002598 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002599 regmatch_T regmatch;
2600
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002601 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002602 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002603 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2604 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002605 {
2606 if (patc != pat)
2607 vim_free(patc);
2608 return FAIL;
2609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
2611 /*
2612 * round == 1: Count the matches.
2613 * round == 2: Build the array to keep the matches.
2614 */
2615 for (round = 1; round <= 2; ++round)
2616 {
2617 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002618 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 {
2620 if (!buf->b_p_bl) /* skip unlisted buffers */
2621 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002622 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 if (p != NULL)
2624 {
2625 if (round == 1)
2626 ++count;
2627 else
2628 {
2629 if (options & WILD_HOME_REPLACE)
2630 p = home_replace_save(buf, p);
2631 else
2632 p = vim_strsave(p);
2633 (*file)[count++] = p;
2634 }
2635 }
2636 }
2637 if (count == 0) /* no match found, break here */
2638 break;
2639 if (round == 1)
2640 {
2641 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2642 if (*file == NULL)
2643 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002644 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002645 if (patc != pat)
2646 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002647 return FAIL;
2648 }
2649 }
2650 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002651 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 if (count) /* match(es) found, break here */
2653 break;
2654 }
2655
Bram Moolenaar05159a02005-02-26 23:04:13 +00002656 if (patc != pat)
2657 vim_free(patc);
2658
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 *num_file = count;
2660 return (count == 0 ? FAIL : OK);
2661}
2662
2663#endif /* FEAT_CMDL_COMPL */
2664
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665/*
2666 * Check for a match on the file name for buffer "buf" with regprog "prog".
2667 */
2668 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002669buflist_match(
2670 regmatch_T *rmp,
2671 buf_T *buf,
2672 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673{
2674 char_u *match;
2675
2676 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002677 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002679 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680
2681 return match;
2682}
2683
2684/*
2685 * Try matching the regexp in "prog" with file name "name".
2686 * Return "name" when there is a match, NULL when not.
2687 */
2688 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002689fname_match(
2690 regmatch_T *rmp,
2691 char_u *name,
2692 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
2694 char_u *match = NULL;
2695 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696
2697 if (name != NULL)
2698 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002699 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002700 rmp->rm_ic = p_fic || ignore_case;
2701 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 match = name;
2703 else
2704 {
2705 /* Replace $(HOME) with '~' and try matching again. */
2706 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002707 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 match = name;
2709 vim_free(p);
2710 }
2711 }
2712
2713 return match;
2714}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
2716/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002717 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718 */
2719 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002720buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002722 char_u key[VIM_SIZEOF_INT * 2 + 1];
2723 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724
2725 if (nr == 0)
2726 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002727 sprintf((char *)key, "%x", nr);
2728 hi = hash_find(&buf_hashtab, key);
2729
2730 if (!HASHITEM_EMPTY(hi))
2731 return (buf_T *)(hi->hi_key
2732 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 return NULL;
2734}
2735
2736/*
2737 * Get name of file 'n' in the buffer list.
2738 * When the file has no name an empty string is returned.
2739 * home_replace() is used to shorten the file name (used for marks).
2740 * Returns a pointer to allocated memory, of NULL when failed.
2741 */
2742 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002743buflist_nr2name(
2744 int n,
2745 int fullname,
2746 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747{
2748 buf_T *buf;
2749
2750 buf = buflist_findnr(n);
2751 if (buf == NULL)
2752 return NULL;
2753 return home_replace_save(helptail ? buf : NULL,
2754 fullname ? buf->b_ffname : buf->b_fname);
2755}
2756
2757/*
2758 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2759 * When "copy_options" is TRUE save the local window option values.
2760 * When "lnum" is 0 only do the options.
2761 */
2762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002763buflist_setfpos(
2764 buf_T *buf,
2765 win_T *win,
2766 linenr_T lnum,
2767 colnr_T col,
2768 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 wininfo_T *wip;
2771
2772 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2773 if (wip->wi_win == win)
2774 break;
2775 if (wip == NULL)
2776 {
2777 /* allocate a new entry */
2778 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2779 if (wip == NULL)
2780 return;
2781 wip->wi_win = win;
2782 if (lnum == 0) /* set lnum even when it's 0 */
2783 lnum = 1;
2784 }
2785 else
2786 {
2787 /* remove the entry from the list */
2788 if (wip->wi_prev)
2789 wip->wi_prev->wi_next = wip->wi_next;
2790 else
2791 buf->b_wininfo = wip->wi_next;
2792 if (wip->wi_next)
2793 wip->wi_next->wi_prev = wip->wi_prev;
2794 if (copy_options && wip->wi_optset)
2795 {
2796 clear_winopt(&wip->wi_opt);
2797#ifdef FEAT_FOLDING
2798 deleteFoldRecurse(&wip->wi_folds);
2799#endif
2800 }
2801 }
2802 if (lnum != 0)
2803 {
2804 wip->wi_fpos.lnum = lnum;
2805 wip->wi_fpos.col = col;
2806 }
2807 if (copy_options)
2808 {
2809 /* Save the window-specific option values. */
2810 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2811#ifdef FEAT_FOLDING
2812 wip->wi_fold_manual = win->w_fold_manual;
2813 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2814#endif
2815 wip->wi_optset = TRUE;
2816 }
2817
2818 /* insert the entry in front of the list */
2819 wip->wi_next = buf->b_wininfo;
2820 buf->b_wininfo = wip;
2821 wip->wi_prev = NULL;
2822 if (wip->wi_next)
2823 wip->wi_next->wi_prev = wip;
2824
2825 return;
2826}
2827
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002828#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002829static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002830
2831/*
2832 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2833 * page. That's because a diff is local to a tab page.
2834 */
2835 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002836wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002837{
2838 win_T *wp;
2839
2840 if (wip->wi_opt.wo_diff)
2841 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002842 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002843 /* return FALSE when it's a window in the current tab page, thus
2844 * the buffer was in diff mode here */
2845 if (wip->wi_win == wp)
2846 return FALSE;
2847 return TRUE;
2848 }
2849 return FALSE;
2850}
2851#endif
2852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853/*
2854 * Find info for the current window in buffer "buf".
2855 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002856 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2857 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 * Returns NULL when there isn't any info.
2859 */
2860 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002861find_wininfo(
2862 buf_T *buf,
2863 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864{
2865 wininfo_T *wip;
2866
2867 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002868 if (wip->wi_win == curwin
2869#ifdef FEAT_DIFF
2870 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2871#endif
2872 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002874
2875 /* If no wininfo for curwin, use the first in the list (that doesn't have
2876 * 'diff' set and is in another tab page). */
2877 if (wip == NULL)
2878 {
2879#ifdef FEAT_DIFF
2880 if (skip_diff_buffer)
2881 {
2882 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2883 if (!wininfo_other_tab_diff(wip))
2884 break;
2885 }
2886 else
2887#endif
2888 wip = buf->b_wininfo;
2889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890 return wip;
2891}
2892
2893/*
2894 * Reset the local window options to the values last used in this window.
2895 * If the buffer wasn't used in this window before, use the values from
2896 * the most recently used window. If the values were never set, use the
2897 * global values for the window.
2898 */
2899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002900get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901{
2902 wininfo_T *wip;
2903
2904 clear_winopt(&curwin->w_onebuf_opt);
2905#ifdef FEAT_FOLDING
2906 clearFolding(curwin);
2907#endif
2908
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002909 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002910 if (wip != NULL && wip->wi_win != NULL
2911 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002913 /* The buffer is currently displayed in the window: use the actual
2914 * option values instead of the saved (possibly outdated) values. */
2915 win_T *wp = wip->wi_win;
2916
2917 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2918#ifdef FEAT_FOLDING
2919 curwin->w_fold_manual = wp->w_fold_manual;
2920 curwin->w_foldinvalid = TRUE;
2921 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2922#endif
2923 }
2924 else if (wip != NULL && wip->wi_optset)
2925 {
2926 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2928#ifdef FEAT_FOLDING
2929 curwin->w_fold_manual = wip->wi_fold_manual;
2930 curwin->w_foldinvalid = TRUE;
2931 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2932#endif
2933 }
2934 else
2935 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2936
2937#ifdef FEAT_FOLDING
2938 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2939 if (p_fdls >= 0)
2940 curwin->w_p_fdl = p_fdls;
2941#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002942#ifdef FEAT_SYN_HL
2943 check_colorcolumn(curwin);
2944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945}
2946
2947/*
2948 * Find the position (lnum and col) for the buffer 'buf' for the current
2949 * window.
2950 * Returns a pointer to no_position if no position is found.
2951 */
2952 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002953buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954{
2955 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002956 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002958 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 if (wip != NULL)
2960 return &(wip->wi_fpos);
2961 else
2962 return &no_position;
2963}
2964
2965/*
2966 * Find the lnum for the buffer 'buf' for the current window.
2967 */
2968 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002969buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970{
2971 return buflist_findfpos(buf)->lnum;
2972}
2973
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002975 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002978buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979{
2980 buf_T *buf;
2981 int len;
2982 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002983 int ro_char;
2984 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002985#ifdef FEAT_TERMINAL
2986 int job_running;
2987 int job_none_open;
2988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989
2990 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2991 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02002992#ifdef FEAT_TERMINAL
2993 job_running = term_job_running(buf->b_term);
2994 job_none_open = job_running && term_none_open(buf->b_term);
2995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002997 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2998 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2999 || (vim_strchr(eap->arg, '+')
3000 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3001 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003002 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003003 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003004 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3005#ifdef FEAT_TERMINAL
3006 || (vim_strchr(eap->arg, 'R')
3007 && (!job_running || (job_running && job_none_open)))
3008 || (vim_strchr(eap->arg, '?')
3009 && (!job_running || (job_running && !job_none_open)))
3010 || (vim_strchr(eap->arg, 'F')
3011 && (job_running || buf->b_term == NULL))
3012#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003013 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3014 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3015 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3016 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3017 || (vim_strchr(eap->arg, '#')
3018 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003021 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 else
3023 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003024 if (message_filtered(NameBuff))
3025 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003027 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3028 : (bufIsChanged(buf) ? '+' : ' ');
3029#ifdef FEAT_TERMINAL
3030 if (term_job_running(buf->b_term))
3031 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003032 if (term_none_open(buf->b_term))
3033 ro_char = '?';
3034 else
3035 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003036 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3037 * closing, but it's not actually changed. */
3038 }
3039 else if (buf->b_term != NULL)
3040 ro_char = 'F';
3041 else
3042#endif
3043 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3044
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003045 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003046 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 buf->b_fnum,
3048 buf->b_p_bl ? ' ' : 'u',
3049 buf == curbuf ? '%' :
3050 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3051 buf->b_ml.ml_mfp == NULL ? ' ' :
3052 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003053 ro_char,
3054 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003055 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003056 if (len > IOSIZE - 20)
3057 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058
3059 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 i = 40 - vim_strsize(IObuff);
3061 do
3062 {
3063 IObuff[len++] = ' ';
3064 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003065 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3066 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003067 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 msg_outtrans(IObuff);
3069 out_flush(); /* output one line at a time */
3070 ui_breakcheck();
3071 }
3072}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073
3074/*
3075 * Get file name and line number for file 'fnum'.
3076 * Used by DoOneCmd() for translating '%' and '#'.
3077 * Used by insert_reg() and cmdline_paste() for '#' register.
3078 * Return FAIL if not found, OK for success.
3079 */
3080 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003081buflist_name_nr(
3082 int fnum,
3083 char_u **fname,
3084 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085{
3086 buf_T *buf;
3087
3088 buf = buflist_findnr(fnum);
3089 if (buf == NULL || buf->b_fname == NULL)
3090 return FAIL;
3091
3092 *fname = buf->b_fname;
3093 *lnum = buflist_findlnum(buf);
3094
3095 return OK;
3096}
3097
3098/*
3099 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3100 * The file name with the full path is also remembered, for when :cd is used.
3101 * Returns FAIL for failure (file name already in use by other buffer)
3102 * OK otherwise.
3103 */
3104 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003105setfname(
3106 buf_T *buf,
3107 char_u *ffname,
3108 char_u *sfname,
3109 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110{
Bram Moolenaar81695252004-12-29 20:58:21 +00003111 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003113 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114#endif
3115
3116 if (ffname == NULL || *ffname == NUL)
3117 {
3118 /* Removing the name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003119 VIM_CLEAR(buf->b_ffname);
3120 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121#ifdef UNIX
3122 st.st_dev = (dev_T)-1;
3123#endif
3124 }
3125 else
3126 {
3127 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3128 if (ffname == NULL) /* out of memory */
3129 return FAIL;
3130
3131 /*
3132 * if the file name is already used in another buffer:
3133 * - if the buffer is loaded, fail
3134 * - if the buffer is not loaded, delete it from the list
3135 */
3136#ifdef UNIX
3137 if (mch_stat((char *)ffname, &st) < 0)
3138 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003139#endif
3140 if (!(buf->b_flags & BF_DUMMY))
3141#ifdef UNIX
3142 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003144 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#endif
3146 if (obuf != NULL && obuf != buf)
3147 {
3148 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3149 {
3150 if (message)
3151 EMSG(_("E95: Buffer with this name already exists"));
3152 vim_free(ffname);
3153 return FAIL;
3154 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003155 /* delete from the list */
3156 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 }
3158 sfname = vim_strsave(sfname);
3159 if (ffname == NULL || sfname == NULL)
3160 {
3161 vim_free(sfname);
3162 vim_free(ffname);
3163 return FAIL;
3164 }
3165#ifdef USE_FNAME_CASE
3166# ifdef USE_LONG_FNAME
3167 if (USE_LONG_FNAME)
3168# endif
3169 fname_case(sfname, 0); /* set correct case for short file name */
3170#endif
3171 vim_free(buf->b_ffname);
3172 vim_free(buf->b_sfname);
3173 buf->b_ffname = ffname;
3174 buf->b_sfname = sfname;
3175 }
3176 buf->b_fname = buf->b_sfname;
3177#ifdef UNIX
3178 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003179 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 else
3181 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003182 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 buf->b_dev = st.st_dev;
3184 buf->b_ino = st.st_ino;
3185 }
3186#endif
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 buf_name_changed(buf);
3191 return OK;
3192}
3193
3194/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003195 * Crude way of changing the name of a buffer. Use with care!
3196 * The name should be relative to the current directory.
3197 */
3198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003199buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003200{
3201 buf_T *buf;
3202
3203 buf = buflist_findnr(fnum);
3204 if (buf != NULL)
3205 {
3206 vim_free(buf->b_sfname);
3207 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003208 buf->b_ffname = vim_strsave(name);
3209 buf->b_sfname = NULL;
3210 /* Allocate ffname and expand into full path. Also resolves .lnk
3211 * files on Win32. */
3212 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003213 buf->b_fname = buf->b_sfname;
3214 }
3215}
3216
3217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 * Take care of what needs to be done when the name of buffer "buf" has
3219 * changed.
3220 */
3221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 /*
3225 * If the file name changed, also change the name of the swapfile
3226 */
3227 if (buf->b_ml.ml_mfp != NULL)
3228 ml_setname(buf);
3229
3230 if (curwin->w_buffer == buf)
3231 check_arg_idx(curwin); /* check file name for arg list */
3232#ifdef FEAT_TITLE
3233 maketitle(); /* set window title */
3234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 fmarks_check_names(buf); /* check named file marks */
3237 ml_timestamp(buf); /* reset timestamp */
3238}
3239
3240/*
3241 * set alternate file name for current window
3242 *
3243 * Used by do_one_cmd(), do_write() and do_ecmd().
3244 * Return the buffer.
3245 */
3246 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003247setaltfname(
3248 char_u *ffname,
3249 char_u *sfname,
3250 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251{
3252 buf_T *buf;
3253
3254 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3255 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003256 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 curwin->w_alt_fnum = buf->b_fnum;
3258 return buf;
3259}
3260
3261/*
3262 * Get alternate file name for current window.
3263 * Return NULL if there isn't any, and give error message if requested.
3264 */
3265 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003266getaltfname(
3267 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268{
3269 char_u *fname;
3270 linenr_T dummy;
3271
3272 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3273 {
3274 if (errmsg)
3275 EMSG(_(e_noalt));
3276 return NULL;
3277 }
3278 return fname;
3279}
3280
3281/*
3282 * Add a file name to the buflist and return its number.
3283 * Uses same flags as buflist_new(), except BLN_DUMMY.
3284 *
3285 * used by qf_init(), main() and doarglist()
3286 */
3287 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003288buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289{
3290 buf_T *buf;
3291
3292 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3293 if (buf != NULL)
3294 return buf->b_fnum;
3295 return 0;
3296}
3297
3298#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3299/*
3300 * Adjust slashes in file names. Called after 'shellslash' was set.
3301 */
3302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003303buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304{
3305 buf_T *bp;
3306
Bram Moolenaar29323592016-07-24 22:04:11 +02003307 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 {
3309 if (bp->b_ffname != NULL)
3310 slash_adjust(bp->b_ffname);
3311 if (bp->b_sfname != NULL)
3312 slash_adjust(bp->b_sfname);
3313 }
3314}
3315#endif
3316
3317/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003318 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 * Also save the local window option values.
3320 */
3321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003322buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003324 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325}
3326
3327/*
3328 * Return TRUE if 'ffname' is not the same file as current file.
3329 * Fname must have a full path (expanded by mch_FullName()).
3330 */
3331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333{
3334 return otherfile_buf(curbuf, ffname
3335#ifdef UNIX
3336 , NULL
3337#endif
3338 );
3339}
3340
3341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342otherfile_buf(
3343 buf_T *buf,
3344 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003346 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003348 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349{
3350 /* no name is different */
3351 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3352 return TRUE;
3353 if (fnamecmp(ffname, buf->b_ffname) == 0)
3354 return FALSE;
3355#ifdef UNIX
3356 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003357 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358
Bram Moolenaar8767f522016-07-01 17:17:39 +02003359 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 if (stp == NULL)
3361 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003362 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 st.st_dev = (dev_T)-1;
3364 stp = &st;
3365 }
3366 /* Use dev/ino to check if the files are the same, even when the names
3367 * are different (possible with links). Still need to compare the
3368 * name above, for when the file doesn't exist yet.
3369 * Problem: The dev/ino changes when a file is deleted (and created
3370 * again) and remains the same when renamed/moved. We don't want to
3371 * mch_stat() each buffer each time, that would be too slow. Get the
3372 * dev/ino again when they appear to match, but not when they appear
3373 * to be different: Could skip a buffer when it's actually the same
3374 * file. */
3375 if (buf_same_ino(buf, stp))
3376 {
3377 buf_setino(buf);
3378 if (buf_same_ino(buf, stp))
3379 return FALSE;
3380 }
3381 }
3382#endif
3383 return TRUE;
3384}
3385
3386#if defined(UNIX) || defined(PROTO)
3387/*
3388 * Set inode and device number for a buffer.
3389 * Must always be called when b_fname is changed!.
3390 */
3391 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003392buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003394 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395
3396 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3397 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003398 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 buf->b_dev = st.st_dev;
3400 buf->b_ino = st.st_ino;
3401 }
3402 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003403 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404}
3405
3406/*
3407 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3408 */
3409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003410buf_same_ino(
3411 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003412 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003414 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && stp->st_dev == buf->b_dev
3416 && stp->st_ino == buf->b_ino);
3417}
3418#endif
3419
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003420/*
3421 * Print info about the current buffer.
3422 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003424fileinfo(
3425 int fullname, /* when non-zero print full path */
3426 int shorthelp,
3427 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428{
3429 char_u *name;
3430 int n;
3431 char_u *p;
3432 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003433 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434
3435 buffer = alloc(IOSIZE);
3436 if (buffer == NULL)
3437 return;
3438
3439 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3440 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003441 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 p = buffer + STRLEN(buffer);
3443 }
3444 else
3445 p = buffer;
3446
3447 *p++ = '"';
3448 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003449 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 else
3451 {
3452 if (!fullname && curbuf->b_fname != NULL)
3453 name = curbuf->b_fname;
3454 else
3455 name = curbuf->b_ffname;
3456 home_replace(shorthelp ? curbuf : NULL, name, p,
3457 (int)(IOSIZE - (p - buffer)), TRUE);
3458 }
3459
Bram Moolenaara800b422010-06-27 01:15:55 +02003460 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 curbufIsChanged() ? (shortmess(SHM_MOD)
3462 ? " [+]" : _(" [Modified]")) : " ",
3463 (curbuf->b_flags & BF_NOTEDITED)
3464#ifdef FEAT_QUICKFIX
3465 && !bt_dontwrite(curbuf)
3466#endif
3467 ? _("[Not edited]") : "",
3468 (curbuf->b_flags & BF_NEW)
3469#ifdef FEAT_QUICKFIX
3470 && !bt_dontwrite(curbuf)
3471#endif
3472 ? _("[New file]") : "",
3473 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003474 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 : _("[readonly]")) : "",
3476 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3477 || curbuf->b_p_ro) ?
3478 " " : "");
3479 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3480 * causes an overflow, thus for large numbers divide instead. */
3481 if (curwin->w_cursor.lnum > 1000000L)
3482 n = (int)(((long)curwin->w_cursor.lnum) /
3483 ((long)curbuf->b_ml.ml_line_count / 100L));
3484 else
3485 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3486 (long)curbuf->b_ml.ml_line_count);
3487 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3488 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003489 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 }
3491#ifdef FEAT_CMDL_INFO
3492 else if (p_ru)
3493 {
3494 /* Current line and column are already on the screen -- webb */
3495 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003496 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003498 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 (long)curbuf->b_ml.ml_line_count, n);
3500 }
3501#endif
3502 else
3503 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003504 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 _("line %ld of %ld --%d%%-- col "),
3506 (long)curwin->w_cursor.lnum,
3507 (long)curbuf->b_ml.ml_line_count,
3508 n);
3509 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003510 len = STRLEN(buffer);
3511 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3513 }
3514
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003515 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
3517 if (dont_truncate)
3518 {
3519 /* Temporarily set msg_scroll to avoid the message being truncated.
3520 * First call msg_start() to get the message in the right place. */
3521 msg_start();
3522 n = msg_scroll;
3523 msg_scroll = TRUE;
3524 msg(buffer);
3525 msg_scroll = n;
3526 }
3527 else
3528 {
3529 p = msg_trunc_attr(buffer, FALSE, 0);
3530 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 /* Need to repeat the message after redrawing when:
3532 * - When restart_edit is set (otherwise there will be a delay
3533 * before redrawing).
3534 * - When the screen was scrolled but there is no wait-return
3535 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003536 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003537 }
3538
3539 vim_free(buffer);
3540}
3541
3542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003543col_print(
3544 char_u *buf,
3545 size_t buflen,
3546 int col,
3547 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548{
3549 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003550 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003552 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553}
3554
3555#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556static char_u *lasttitle = NULL;
3557static char_u *lasticon = NULL;
3558
Bram Moolenaar84a93082018-06-16 22:58:15 +02003559/*
3560 * Put the file name in the title bar and icon of the window.
3561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003563maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564{
3565 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003566 char_u *title_str = NULL;
3567 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 int maxlen = 0;
3569 int len;
3570 int mustset;
3571 char_u buf[IOSIZE];
3572 int off;
3573
3574 if (!redrawing())
3575 {
3576 /* Postpone updating the title when 'lazyredraw' is set. */
3577 need_maketitle = TRUE;
3578 return;
3579 }
3580
3581 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003582 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003583 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584
3585 if (p_title)
3586 {
3587 if (p_titlelen > 0)
3588 {
3589 maxlen = p_titlelen * Columns / 100;
3590 if (maxlen < 10)
3591 maxlen = 10;
3592 }
3593
Bram Moolenaar84a93082018-06-16 22:58:15 +02003594 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (*p_titlestring != NUL)
3596 {
3597#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003598 if (stl_syntax & STL_IN_TITLE)
3599 {
3600 int use_sandbox = FALSE;
3601 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003602
3603# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003604 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003605# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003606 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003607 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003608 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003609 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003610 if (called_emsg)
3611 set_string_option_direct((char_u *)"titlestring", -1,
3612 (char_u *)"", OPT_FREE, SID_ERROR);
3613 called_emsg |= save_called_emsg;
3614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 else
3616#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003617 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 else
3620 {
3621 /* format: "fname + (path) (1 of 2) - VIM" */
3622
Bram Moolenaar2c666692012-09-05 13:30:40 +02003623#define SPACE_FOR_FNAME (IOSIZE - 100)
3624#define SPACE_FOR_DIR (IOSIZE - 20)
3625#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003627 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003628#ifdef FEAT_TERMINAL
3629 else if (curbuf->b_term != NULL)
3630 {
3631 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3632 SPACE_FOR_FNAME);
3633 }
3634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 else
3636 {
3637 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003638 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 }
3641
Bram Moolenaar21554412017-07-24 21:44:43 +02003642#ifdef FEAT_TERMINAL
3643 if (curbuf->b_term == NULL)
3644#endif
3645 switch (bufIsChanged(curbuf)
3646 + (curbuf->b_p_ro * 2)
3647 + (!curbuf->b_p_ma * 4))
3648 {
3649 case 1: STRCAT(buf, " +"); break;
3650 case 2: STRCAT(buf, " ="); break;
3651 case 3: STRCAT(buf, " =+"); break;
3652 case 4:
3653 case 6: STRCAT(buf, " -"); break;
3654 case 5:
3655 case 7: STRCAT(buf, " -+"); break;
3656 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar21554412017-07-24 21:44:43 +02003658 if (curbuf->b_fname != NULL
3659#ifdef FEAT_TERMINAL
3660 && curbuf->b_term == NULL
3661#endif
3662 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
3664 /* Get path of file, replace home dir with ~ */
3665 off = (int)STRLEN(buf);
3666 buf[off++] = ' ';
3667 buf[off++] = '(';
3668 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003669 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670#ifdef BACKSLASH_IN_FILENAME
3671 /* avoid "c:/name" to be reduced to "c" */
3672 if (isalpha(buf[off]) && buf[off + 1] == ':')
3673 off += 2;
3674#endif
3675 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003676 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003678 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003679 /* must be a help buffer */
3680 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003681 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003685
Bram Moolenaar2c666692012-09-05 13:30:40 +02003686 /* Translate unprintable chars and concatenate. Keep some
3687 * room for the server name. When there is no room (very long
3688 * file name) use (...). */
3689 if (off < SPACE_FOR_DIR)
3690 {
3691 p = transstr(buf + off);
3692 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3693 vim_free(p);
3694 }
3695 else
3696 {
3697 vim_strncpy(buf + off, (char_u *)"...",
3698 (size_t)(SPACE_FOR_ARGNR - off));
3699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 STRCAT(buf, ")");
3701 }
3702
Bram Moolenaar2c666692012-09-05 13:30:40 +02003703 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705#if defined(FEAT_CLIENTSERVER)
3706 if (serverName != NULL)
3707 {
3708 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003709 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 }
3711 else
3712#endif
3713 STRCAT(buf, " - VIM");
3714
3715 if (maxlen > 0)
3716 {
3717 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003718 if (vim_strsize(buf) > maxlen)
3719 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 }
3721 }
3722 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003723 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724
3725 if (p_icon)
3726 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003727 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 if (*p_iconstring != NUL)
3729 {
3730#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003731 if (stl_syntax & STL_IN_ICON)
3732 {
3733 int use_sandbox = FALSE;
3734 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003735
3736# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003737 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003738# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003739 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003740 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003741 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003742 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003743 if (called_emsg)
3744 set_string_option_direct((char_u *)"iconstring", -1,
3745 (char_u *)"", OPT_FREE, SID_ERROR);
3746 called_emsg |= save_called_emsg;
3747 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 else
3749#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003750 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752 else
3753 {
3754 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003755 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003757 p = gettail(curbuf->b_ffname);
3758 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003760 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 if (len > 100)
3762 {
3763 len -= 100;
3764#ifdef FEAT_MBYTE
3765 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003766 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003768 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003770 STRCPY(icon_str, p);
3771 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 }
3773 }
3774
Bram Moolenaar84a93082018-06-16 22:58:15 +02003775 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776
3777 if (mustset)
3778 resettitle();
3779}
3780
3781/*
3782 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3783 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003784 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 */
3786 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003787value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788{
3789 if ((str == NULL) != (*last == NULL)
3790 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3791 {
3792 vim_free(*last);
3793 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003794 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 *last = NULL;
Bram Moolenaar40385db2018-08-07 22:31:44 +02003796 mch_restore_title(
3797 last == &lasttitle ? SAVE_RESTORE_TITLE : SAVE_RESTORE_ICON);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003800 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003802 return TRUE;
3803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 return FALSE;
3806}
3807
3808/*
3809 * Put current window title back (used after calling a shell)
3810 */
3811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003812resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813{
3814 mch_settitle(lasttitle, lasticon);
3815}
Bram Moolenaarea408852005-06-25 22:49:46 +00003816
3817# if defined(EXITFREE) || defined(PROTO)
3818 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003819free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003820{
3821 vim_free(lasttitle);
3822 vim_free(lasticon);
3823}
3824# endif
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826#endif /* FEAT_TITLE */
3827
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003828#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003830 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 * Return length of string in screen cells.
3832 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003833 * Normally works for window "wp", except when working for 'tabline' then it
3834 * is "curwin".
3835 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 * Items are drawn interspersed with the text that surrounds it
3837 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3838 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3839 *
3840 * If maxwidth is not zero, the string will be filled at any middle marker
3841 * or truncated if too long, fillchar is used for all whitespace.
3842 */
3843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003844build_stl_str_hl(
3845 win_T *wp,
3846 char_u *out, /* buffer to write into != NameBuff */
3847 size_t outlen, /* length of out[] */
3848 char_u *fmt,
3849 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3850 int fillchar,
3851 int maxwidth,
3852 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3853 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854{
3855 char_u *p;
3856 char_u *s;
3857 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003858 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003860 win_T *save_curwin;
3861 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862#endif
3863 int empty_line;
3864 colnr_T virtcol;
3865 long l;
3866 long n;
3867 int prevchar_isflag;
3868 int prevchar_isitem;
3869 int itemisflag;
3870 int fillable;
3871 char_u *str;
3872 long num;
3873 int width;
3874 int itemcnt;
3875 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003876 int group_end_userhl;
3877 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 int groupitem[STL_MAX_ITEM];
3879 int groupdepth;
3880 struct stl_item
3881 {
3882 char_u *start;
3883 int minwid;
3884 int maxwid;
3885 enum
3886 {
3887 Normal,
3888 Empty,
3889 Group,
3890 Middle,
3891 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003892 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 Trunc
3894 } type;
3895 } item[STL_MAX_ITEM];
3896 int minwid;
3897 int maxwid;
3898 int zeropad;
3899 char_u base;
3900 char_u opt;
3901#define TMPLEN 70
3902 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003903 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003904 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003905 int save_must_redraw = must_redraw;
3906 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003907
3908#ifdef FEAT_EVAL
3909 /*
3910 * When the format starts with "%!" then evaluate it as an expression and
3911 * use the result as the actual format string.
3912 */
3913 if (fmt[0] == '%' && fmt[1] == '!')
3914 {
3915 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3916 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003917 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003918 }
3919#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920
3921 if (fillchar == 0)
3922 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003923#ifdef FEAT_MBYTE
3924 /* Can't handle a multi-byte fill character yet. */
3925 else if (mb_char2len(fillchar) > 1)
3926 fillchar = '-';
3927#endif
3928
Bram Moolenaar567199b2013-04-24 16:52:36 +02003929 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3930 * p will become invalid when getting another buffer line. */
3931 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3932 empty_line = (*p == NUL);
3933
3934 /* Get the byte value now, in case we need it below. This is more
3935 * efficient than making a copy of the line. */
3936 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3937 byteval = 0;
3938 else
3939#ifdef FEAT_MBYTE
3940 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3941#else
3942 byteval = p[wp->w_cursor.col];
3943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944
3945 groupdepth = 0;
3946 p = out;
3947 curitem = 0;
3948 prevchar_isflag = TRUE;
3949 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003950 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003952 if (curitem == STL_MAX_ITEM)
3953 {
3954 /* There are too many items. Add the error code to the statusline
3955 * to give the user a hint about what went wrong. */
3956 if (p + 6 < out + outlen)
3957 {
3958 mch_memmove(p, " E541", (size_t)5);
3959 p += 5;
3960 }
3961 break;
3962 }
3963
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 if (*s != NUL && *s != '%')
3965 prevchar_isflag = prevchar_isitem = FALSE;
3966
3967 /*
3968 * Handle up to the next '%' or the end.
3969 */
3970 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3971 *p++ = *s++;
3972 if (*s == NUL || p + 1 >= out + outlen)
3973 break;
3974
3975 /*
3976 * Handle one '%' item.
3977 */
3978 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003979 if (*s == NUL) /* ignore trailing % */
3980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (*s == '%')
3982 {
3983 if (p + 1 >= out + outlen)
3984 break;
3985 *p++ = *s++;
3986 prevchar_isflag = prevchar_isitem = FALSE;
3987 continue;
3988 }
3989 if (*s == STL_MIDDLEMARK)
3990 {
3991 s++;
3992 if (groupdepth > 0)
3993 continue;
3994 item[curitem].type = Middle;
3995 item[curitem++].start = p;
3996 continue;
3997 }
3998 if (*s == STL_TRUNCMARK)
3999 {
4000 s++;
4001 item[curitem].type = Trunc;
4002 item[curitem++].start = p;
4003 continue;
4004 }
4005 if (*s == ')')
4006 {
4007 s++;
4008 if (groupdepth < 1)
4009 continue;
4010 groupdepth--;
4011
4012 t = item[groupitem[groupdepth]].start;
4013 *p = NUL;
4014 l = vim_strsize(t);
4015 if (curitem > groupitem[groupdepth] + 1
4016 && item[groupitem[groupdepth]].minwid == 0)
4017 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004018 /* remove group if all items are empty and highlight group
4019 * doesn't change */
4020 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004021 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4022 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004023 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004024 {
4025 group_start_userhl = group_end_userhl = item[n].minwid;
4026 break;
4027 }
4028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004030 {
4031 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004033 if (item[n].type == Highlight)
4034 group_end_userhl = item[n].minwid;
4035 }
4036 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038 p = t;
4039 l = 0;
4040 }
4041 }
4042 if (l > item[groupitem[groupdepth]].maxwid)
4043 {
4044 /* truncate, remove n bytes of text at the start */
4045#ifdef FEAT_MBYTE
4046 if (has_mbyte)
4047 {
4048 /* Find the first character that should be included. */
4049 n = 0;
4050 while (l >= item[groupitem[groupdepth]].maxwid)
4051 {
4052 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004053 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 }
4055 }
4056 else
4057#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004058 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059
4060 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004061 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 p = p - n + 1;
4063#ifdef FEAT_MBYTE
4064 /* Fill up space left over by half a double-wide char. */
4065 while (++l < item[groupitem[groupdepth]].minwid)
4066 *p++ = fillchar;
4067#endif
4068
4069 /* correct the start of the items for the truncation */
4070 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4071 {
4072 item[l].start -= n;
4073 if (item[l].start < t)
4074 item[l].start = t;
4075 }
4076 }
4077 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4078 {
4079 /* fill */
4080 n = item[groupitem[groupdepth]].minwid;
4081 if (n < 0)
4082 {
4083 /* fill by appending characters */
4084 n = 0 - n;
4085 while (l++ < n && p + 1 < out + outlen)
4086 *p++ = fillchar;
4087 }
4088 else
4089 {
4090 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004091 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 l = n - l;
4093 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004094 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 p += l;
4096 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4097 item[n].start += l;
4098 for ( ; l > 0; l--)
4099 *t++ = fillchar;
4100 }
4101 }
4102 continue;
4103 }
4104 minwid = 0;
4105 maxwid = 9999;
4106 zeropad = FALSE;
4107 l = 1;
4108 if (*s == '0')
4109 {
4110 s++;
4111 zeropad = TRUE;
4112 }
4113 if (*s == '-')
4114 {
4115 s++;
4116 l = -1;
4117 }
4118 if (VIM_ISDIGIT(*s))
4119 {
4120 minwid = (int)getdigits(&s);
4121 if (minwid < 0) /* overflow */
4122 minwid = 0;
4123 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004124 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
4126 item[curitem].type = Highlight;
4127 item[curitem].start = p;
4128 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4129 s++;
4130 curitem++;
4131 continue;
4132 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004133 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4134 {
4135 if (*s == STL_TABCLOSENR)
4136 {
4137 if (minwid == 0)
4138 {
4139 /* %X ends the close label, go back to the previously
4140 * define tab label nr. */
4141 for (n = curitem - 1; n >= 0; --n)
4142 if (item[n].type == TabPage && item[n].minwid >= 0)
4143 {
4144 minwid = item[n].minwid;
4145 break;
4146 }
4147 }
4148 else
4149 /* close nrs are stored as negative values */
4150 minwid = - minwid;
4151 }
4152 item[curitem].type = TabPage;
4153 item[curitem].start = p;
4154 item[curitem].minwid = minwid;
4155 s++;
4156 curitem++;
4157 continue;
4158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 if (*s == '.')
4160 {
4161 s++;
4162 if (VIM_ISDIGIT(*s))
4163 {
4164 maxwid = (int)getdigits(&s);
4165 if (maxwid <= 0) /* overflow */
4166 maxwid = 50;
4167 }
4168 }
4169 minwid = (minwid > 50 ? 50 : minwid) * l;
4170 if (*s == '(')
4171 {
4172 groupitem[groupdepth++] = curitem;
4173 item[curitem].type = Group;
4174 item[curitem].start = p;
4175 item[curitem].minwid = minwid;
4176 item[curitem].maxwid = maxwid;
4177 s++;
4178 curitem++;
4179 continue;
4180 }
4181 if (vim_strchr(STL_ALL, *s) == NULL)
4182 {
4183 s++;
4184 continue;
4185 }
4186 opt = *s++;
4187
4188 /* OK - now for the real work */
4189 base = 'D';
4190 itemisflag = FALSE;
4191 fillable = TRUE;
4192 num = -1;
4193 str = NULL;
4194 switch (opt)
4195 {
4196 case STL_FILEPATH:
4197 case STL_FULLPATH:
4198 case STL_FILENAME:
4199 fillable = FALSE; /* don't change ' ' to fillchar */
4200 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004201 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 else
4203 {
4204 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004205 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4207 }
4208 trans_characters(NameBuff, MAXPATHL);
4209 if (opt != STL_FILENAME)
4210 str = NameBuff;
4211 else
4212 str = gettail(NameBuff);
4213 break;
4214
4215 case STL_VIM_EXPR: /* '{' */
4216 itemisflag = TRUE;
4217 t = p;
4218 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4219 *p++ = *s++;
4220 if (*s != '}') /* missing '}' or out of space */
4221 break;
4222 s++;
4223 *p = 0;
4224 p = t;
4225
4226#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004227 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar3cb44482018-08-05 13:22:26 +02004228 set_internal_string_var((char_u *)"g:actual_curbuf", tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004230 save_curbuf = curbuf;
4231 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 curwin = wp;
4233 curbuf = wp->w_buffer;
4234
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004235 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004237 curwin = save_curwin;
4238 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004239 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
4241 if (str != NULL && *str != 0)
4242 {
4243 if (*skipdigits(str) == NUL)
4244 {
4245 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004246 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 itemisflag = FALSE;
4248 }
4249 }
4250#endif
4251 break;
4252
4253 case STL_LINE:
4254 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4255 ? 0L : (long)(wp->w_cursor.lnum);
4256 break;
4257
4258 case STL_NUMLINES:
4259 num = wp->w_buffer->b_ml.ml_line_count;
4260 break;
4261
4262 case STL_COLUMN:
4263 num = !(State & INSERT) && empty_line
4264 ? 0 : (int)wp->w_cursor.col + 1;
4265 break;
4266
4267 case STL_VIRTCOL:
4268 case STL_VIRTCOL_ALT:
4269 /* In list mode virtcol needs to be recomputed */
4270 virtcol = wp->w_virtcol;
4271 if (wp->w_p_list && lcs_tab1 == NUL)
4272 {
4273 wp->w_p_list = FALSE;
4274 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4275 wp->w_p_list = TRUE;
4276 }
4277 ++virtcol;
4278 /* Don't display %V if it's the same as %c. */
4279 if (opt == STL_VIRTCOL_ALT
4280 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4281 ? 0 : (int)wp->w_cursor.col + 1)))
4282 break;
4283 num = (long)virtcol;
4284 break;
4285
4286 case STL_PERCENTAGE:
4287 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4288 (long)wp->w_buffer->b_ml.ml_line_count);
4289 break;
4290
4291 case STL_ALTPERCENT:
4292 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004293 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 break;
4295
4296 case STL_ARGLISTSTAT:
4297 fillable = FALSE;
4298 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004299 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 str = tmp;
4301 break;
4302
4303 case STL_KEYMAP:
4304 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004305 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 str = tmp;
4307 break;
4308 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004309#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004310 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311#else
4312 num = 0;
4313#endif
4314 break;
4315
4316 case STL_BUFNO:
4317 num = wp->w_buffer->b_fnum;
4318 break;
4319
4320 case STL_OFFSET_X:
4321 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004322 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 case STL_OFFSET:
4324#ifdef FEAT_BYTEOFF
4325 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4326 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4327 0L : l + 1 + (!(State & INSERT) && empty_line ?
4328 0 : (int)wp->w_cursor.col);
4329#endif
4330 break;
4331
4332 case STL_BYTEVAL_X:
4333 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004334 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004336 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 if (num == NL)
4338 num = 0;
4339 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4340 num = NL;
4341 break;
4342
4343 case STL_ROFLAG:
4344 case STL_ROFLAG_ALT:
4345 itemisflag = TRUE;
4346 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004347 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 break;
4349
4350 case STL_HELPFLAG:
4351 case STL_HELPFLAG_ALT:
4352 itemisflag = TRUE;
4353 if (wp->w_buffer->b_help)
4354 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004355 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 break;
4357
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 case STL_FILETYPE:
4359 if (*wp->w_buffer->b_p_ft != NUL
4360 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4361 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004362 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4363 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 str = tmp;
4365 }
4366 break;
4367
4368 case STL_FILETYPE_ALT:
4369 itemisflag = TRUE;
4370 if (*wp->w_buffer->b_p_ft != NUL
4371 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4372 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004373 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4374 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 for (t = tmp; *t != 0; t++)
4376 *t = TOUPPER_LOC(*t);
4377 str = tmp;
4378 }
4379 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
Bram Moolenaar4033c552017-09-16 20:54:51 +02004381#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 case STL_PREVIEWFLAG:
4383 case STL_PREVIEWFLAG_ALT:
4384 itemisflag = TRUE;
4385 if (wp->w_p_pvw)
4386 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4387 : _("[Preview]"));
4388 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004389
4390 case STL_QUICKFIX:
4391 if (bt_quickfix(wp->w_buffer))
4392 str = (char_u *)(wp->w_llist_ref
4393 ? _(msg_loclist)
4394 : _(msg_qflist));
4395 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396#endif
4397
4398 case STL_MODIFIED:
4399 case STL_MODIFIED_ALT:
4400 itemisflag = TRUE;
4401 switch ((opt == STL_MODIFIED_ALT)
4402 + bufIsChanged(wp->w_buffer) * 2
4403 + (!wp->w_buffer->b_p_ma) * 4)
4404 {
4405 case 2: str = (char_u *)"[+]"; break;
4406 case 3: str = (char_u *)",+"; break;
4407 case 4: str = (char_u *)"[-]"; break;
4408 case 5: str = (char_u *)",-"; break;
4409 case 6: str = (char_u *)"[+-]"; break;
4410 case 7: str = (char_u *)",+-"; break;
4411 }
4412 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004413
4414 case STL_HIGHLIGHT:
4415 t = s;
4416 while (*s != '#' && *s != NUL)
4417 ++s;
4418 if (*s == '#')
4419 {
4420 item[curitem].type = Highlight;
4421 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004422 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004423 curitem++;
4424 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004425 if (*s != NUL)
4426 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004427 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429
4430 item[curitem].start = p;
4431 item[curitem].type = Normal;
4432 if (str != NULL && *str)
4433 {
4434 t = str;
4435 if (itemisflag)
4436 {
4437 if ((t[0] && t[1])
4438 && ((!prevchar_isitem && *t == ',')
4439 || (prevchar_isflag && *t == ' ')))
4440 t++;
4441 prevchar_isflag = TRUE;
4442 }
4443 l = vim_strsize(t);
4444 if (l > 0)
4445 prevchar_isitem = TRUE;
4446 if (l > maxwid)
4447 {
4448 while (l >= maxwid)
4449#ifdef FEAT_MBYTE
4450 if (has_mbyte)
4451 {
4452 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004453 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 }
4455 else
4456#endif
4457 l -= byte2cells(*t++);
4458 if (p + 1 >= out + outlen)
4459 break;
4460 *p++ = '<';
4461 }
4462 if (minwid > 0)
4463 {
4464 for (; l < minwid && p + 1 < out + outlen; l++)
4465 {
4466 /* Don't put a "-" in front of a digit. */
4467 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4468 *p++ = ' ';
4469 else
4470 *p++ = fillchar;
4471 }
4472 minwid = 0;
4473 }
4474 else
4475 minwid *= -1;
4476 while (*t && p + 1 < out + outlen)
4477 {
4478 *p++ = *t++;
4479 /* Change a space by fillchar, unless fillchar is '-' and a
4480 * digit follows. */
4481 if (fillable && p[-1] == ' '
4482 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4483 p[-1] = fillchar;
4484 }
4485 for (; l < minwid && p + 1 < out + outlen; l++)
4486 *p++ = fillchar;
4487 }
4488 else if (num >= 0)
4489 {
4490 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4491 char_u nstr[20];
4492
4493 if (p + 20 >= out + outlen)
4494 break; /* not sufficient space */
4495 prevchar_isitem = TRUE;
4496 t = nstr;
4497 if (opt == STL_VIRTCOL_ALT)
4498 {
4499 *t++ = '-';
4500 minwid--;
4501 }
4502 *t++ = '%';
4503 if (zeropad)
4504 *t++ = '0';
4505 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004506 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 *t = 0;
4508
4509 for (n = num, l = 1; n >= nbase; n /= nbase)
4510 l++;
4511 if (opt == STL_VIRTCOL_ALT)
4512 l++;
4513 if (l > maxwid)
4514 {
4515 l += 2;
4516 n = l - maxwid;
4517 while (l-- > maxwid)
4518 num /= nbase;
4519 *t++ = '>';
4520 *t++ = '%';
4521 *t = t[-3];
4522 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004523 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4524 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4528 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 p += STRLEN(p);
4530 }
4531 else
4532 item[curitem].type = Empty;
4533
4534 if (opt == STL_VIM_EXPR)
4535 vim_free(str);
4536
4537 if (num >= 0 || (!itemisflag && str && *str))
4538 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4539 curitem++;
4540 }
4541 *p = NUL;
4542 itemcnt = curitem;
4543
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004544#ifdef FEAT_EVAL
4545 if (usefmt != fmt)
4546 vim_free(usefmt);
4547#endif
4548
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 width = vim_strsize(out);
4550 if (maxwidth > 0 && width > maxwidth)
4551 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004552 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 l = 0;
4554 if (itemcnt == 0)
4555 s = out;
4556 else
4557 {
4558 for ( ; l < itemcnt; l++)
4559 if (item[l].type == Trunc)
4560 {
4561 /* Truncate at %< item. */
4562 s = item[l].start;
4563 break;
4564 }
4565 if (l == itemcnt)
4566 {
4567 /* No %< item, truncate first item. */
4568 s = item[0].start;
4569 l = 0;
4570 }
4571 }
4572
4573 if (width - vim_strsize(s) >= maxwidth)
4574 {
4575 /* Truncation mark is beyond max length */
4576#ifdef FEAT_MBYTE
4577 if (has_mbyte)
4578 {
4579 s = out;
4580 width = 0;
4581 for (;;)
4582 {
4583 width += ptr2cells(s);
4584 if (width >= maxwidth)
4585 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004586 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 /* Fill up for half a double-wide character. */
4589 while (++width < maxwidth)
4590 *s++ = fillchar;
4591 }
4592 else
4593#endif
4594 s = out + maxwidth - 1;
4595 for (l = 0; l < itemcnt; l++)
4596 if (item[l].start > s)
4597 break;
4598 itemcnt = l;
4599 *s++ = '>';
4600 *s = 0;
4601 }
4602 else
4603 {
4604#ifdef FEAT_MBYTE
4605 if (has_mbyte)
4606 {
4607 n = 0;
4608 while (width >= maxwidth)
4609 {
4610 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004611 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613 }
4614 else
4615#endif
4616 n = width - maxwidth + 1;
4617 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004618 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 *s = '<';
4620
4621 /* Fill up for half a double-wide character. */
4622 while (++width < maxwidth)
4623 {
4624 s = s + STRLEN(s);
4625 *s++ = fillchar;
4626 *s = NUL;
4627 }
4628
4629 --n; /* count the '<' */
4630 for (; l < itemcnt; l++)
4631 {
4632 if (item[l].start - n >= s)
4633 item[l].start -= n;
4634 else
4635 item[l].start = s;
4636 }
4637 }
4638 width = maxwidth;
4639 }
4640 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4641 {
4642 /* Apply STL_MIDDLE if any */
4643 for (l = 0; l < itemcnt; l++)
4644 if (item[l].type == Middle)
4645 break;
4646 if (l < itemcnt)
4647 {
4648 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004649 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 for (s = item[l].start; s < p; s++)
4651 *s = fillchar;
4652 for (l++; l < itemcnt; l++)
4653 item[l].start += maxwidth - width;
4654 width = maxwidth;
4655 }
4656 }
4657
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004658 /* Store the info about highlighting. */
4659 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004661 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 for (l = 0; l < itemcnt; l++)
4663 {
4664 if (item[l].type == Highlight)
4665 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004666 sp->start = item[l].start;
4667 sp->userhl = item[l].minwid;
4668 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 }
4670 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004671 sp->start = NULL;
4672 sp->userhl = 0;
4673 }
4674
4675 /* Store the info about tab pages labels. */
4676 if (tabtab != NULL)
4677 {
4678 sp = tabtab;
4679 for (l = 0; l < itemcnt; l++)
4680 {
4681 if (item[l].type == TabPage)
4682 {
4683 sp->start = item[l].start;
4684 sp->userhl = item[l].minwid;
4685 sp++;
4686 }
4687 }
4688 sp->start = NULL;
4689 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 }
4691
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004692 /* When inside update_screen we do not want redrawing a stausline, ruler,
4693 * title, etc. to trigger another redraw, it may cause an endless loop. */
4694 if (updating_screen)
4695 {
4696 must_redraw = save_must_redraw;
4697 curwin->w_redr_type = save_redr_type;
4698 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004699
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 return width;
4701}
4702#endif /* FEAT_STL_OPT */
4703
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004704#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4705 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004707 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4708 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 */
4710 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004711get_rel_pos(
4712 win_T *wp,
4713 char_u *buf,
4714 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715{
4716 long above; /* number of lines above window */
4717 long below; /* number of lines below window */
4718
Bram Moolenaar0027c212015-01-07 13:31:52 +01004719 if (buflen < 3) /* need at least 3 chars for writing */
4720 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 above = wp->w_topline - 1;
4722#ifdef FEAT_DIFF
4723 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004724 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4725 above = 0; /* All buffer lines are displayed and there is an
4726 * indication of filler lines, that can be considered
4727 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728#endif
4729 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4730 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004731 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4732 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004734 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004736 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 ? (int)(above / ((above + below) / 100L))
4738 : (int)(above * 100L / (above + below)));
4739}
4740#endif
4741
4742/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004743 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * Return TRUE if it was appended.
4745 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004746 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004747append_arg_number(
4748 win_T *wp,
4749 char_u *buf,
4750 int buflen,
4751 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752{
4753 char_u *p;
4754
4755 if (ARGCOUNT <= 1) /* nothing to do */
4756 return FALSE;
4757
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004758 p = buf + STRLEN(buf); /* go to the end of the buffer */
4759 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 return FALSE;
4761 *p++ = ' ';
4762 *p++ = '(';
4763 if (add_file)
4764 {
4765 STRCPY(p, "file ");
4766 p += 5;
4767 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004768 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4769 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4771 return TRUE;
4772}
4773
4774/*
4775 * If fname is not a full path, make it a full path.
4776 * Returns pointer to allocated memory (NULL for failure).
4777 */
4778 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004779fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 /*
4782 * Force expanding the path always for Unix, because symbolic links may
4783 * mess up the full path name, even though it starts with a '/'.
4784 * Also expand when there is ".." in the file name, try to remove it,
4785 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004786 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 * For MS-Windows also expand names like "longna~1" to "longname".
4788 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004789#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 return FullName_save(fname, TRUE);
4791#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004792 if (!vim_isAbsName(fname)
4793 || strstr((char *)fname, "..") != NULL
4794 || strstr((char *)fname, "//") != NULL
4795# ifdef BACKSLASH_IN_FILENAME
4796 || strstr((char *)fname, "\\\\") != NULL
4797# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004798# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 )
4802 return FullName_save(fname, FALSE);
4803
4804 fname = vim_strsave(fname);
4805
Bram Moolenaar9b942202007-10-03 12:31:33 +00004806# ifdef USE_FNAME_CASE
4807# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004809# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 {
4811 if (fname != NULL)
4812 fname_case(fname, 0); /* set correct case for file name */
4813 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815
4816 return fname;
4817#endif
4818}
4819
4820/*
4821 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4822 * "ffname" becomes a pointer to allocated memory (or NULL).
4823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004825fname_expand(
4826 buf_T *buf UNUSED,
4827 char_u **ffname,
4828 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829{
4830 if (*ffname == NULL) /* if no file name given, nothing to do */
4831 return;
4832 if (*sfname == NULL) /* if no short file name given, use ffname */
4833 *sfname = *ffname;
4834 *ffname = fix_fname(*ffname); /* expand to full path */
4835
4836#ifdef FEAT_SHORTCUT
4837 if (!buf->b_p_bin)
4838 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004839 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840
4841 /* If the file name is a shortcut file, use the file it links to. */
4842 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004843 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 {
4845 vim_free(*ffname);
4846 *ffname = rfname;
4847 *sfname = rfname;
4848 }
4849 }
4850#endif
4851}
4852
4853/*
4854 * Get the file name for an argument list entry.
4855 */
4856 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004857alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858{
4859 buf_T *bp;
4860
4861 /* Use the name from the associated buffer if it exists. */
4862 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004863 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 return aep->ae_fname;
4865 return bp->b_fname;
4866}
4867
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868/*
4869 * do_arg_all(): Open up to 'count' windows, one for each argument.
4870 */
4871 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872do_arg_all(
4873 int count,
4874 int forceit, /* hide buffers in current windows */
4875 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876{
4877 int i;
4878 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004879 char_u *opened; /* Array of weight for which args are open:
4880 * 0: not opened
4881 * 1: opened in other tab
4882 * 2: opened in curtab
4883 * 3: opened in curtab and curwin
4884 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004885 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 int use_firstwin = FALSE; /* use first window for arglist */
4887 int split_ret = OK;
4888 int p_ea_save;
4889 alist_T *alist; /* argument list to be used */
4890 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004891 tabpage_T *tpnext;
4892 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004893 win_T *old_curwin, *last_curwin;
4894 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004895 win_T *new_curwin = NULL;
4896 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897
4898 if (ARGCOUNT <= 0)
4899 {
4900 /* Don't give an error message. We don't want it when the ":all"
4901 * command is in the .vimrc. */
4902 return;
4903 }
4904 setpcmark();
4905
4906 opened_len = ARGCOUNT;
4907 opened = alloc_clear((unsigned)opened_len);
4908 if (opened == NULL)
4909 return;
4910
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004911 /* Autocommands may do anything to the argument list. Make sure it's not
4912 * freed while we are working here by "locking" it. We still have to
4913 * watch out for its size to be changed. */
4914 alist = curwin->w_alist;
4915 ++alist->al_refcount;
4916
4917 old_curwin = curwin;
4918 old_curtab = curtab;
4919
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004920# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004922# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923
4924 /*
4925 * Try closing all windows that are not in the argument list.
4926 * Also close windows that are not full width;
4927 * When 'hidden' or "forceit" set the buffer becomes hidden.
4928 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004931 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004932 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004933 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004935 tpnext = curtab->tp_next;
4936 for (wp = firstwin; wp != NULL; wp = wpnext)
4937 {
4938 wpnext = wp->w_next;
4939 buf = wp->w_buffer;
4940 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004941 || (!keep_tabs && (buf->b_nwindows > 1
4942 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004943 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004944 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004946 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004947 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004949 if (i < alist->al_ga.ga_len
4950 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4951 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4952 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004954 int weight = 1;
4955
4956 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004957 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004958 ++weight;
4959 if (old_curwin == wp)
4960 ++weight;
4961 }
4962
4963 if (weight > (int)opened[i])
4964 {
4965 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004966 if (i == 0)
4967 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004968 if (new_curwin != NULL)
4969 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004970 new_curwin = wp;
4971 new_curtab = curtab;
4972 }
4973 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004974 else if (keep_tabs)
4975 i = opened_len;
4976
4977 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004978 {
4979 /* Use the current argument list for all windows
4980 * containing a file from it. */
4981 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004982 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004983 ++wp->w_alist->al_refcount;
4984 }
4985 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
4988 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004989 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004991 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004993 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004994 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004996 /* If the buffer was changed, and we would like to hide it,
4997 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02004998 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004999 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005001 bufref_T bufref;
5002
5003 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005004
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005005 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005006
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005007 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005008 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005009 {
5010 wpnext = firstwin; /* start all over... */
5011 continue;
5012 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005013 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005014 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005015 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005016 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005017 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005018 else
5019 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005020 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005021
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005022 /* check if autocommands removed the next window */
5023 if (!win_valid(wpnext))
5024 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005029
5030 /* Without the ":tab" modifier only do the current tab page. */
5031 if (had_tab == 0 || tpnext == NULL)
5032 break;
5033
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005034 /* check if autocommands removed the next tab page */
5035 if (!valid_tabpage(tpnext))
5036 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005037
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005038 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040
5041 /*
5042 * Open a window for files in the argument list that don't have one.
5043 * ARGCOUNT may change while doing this, because of autocommands.
5044 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005045 if (count > opened_len || count <= 0)
5046 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5049 ++autocmd_no_enter;
5050 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005051 last_curwin = curwin;
5052 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005054 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5055 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005056 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005057 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5058 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005059
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005060 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
5062 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5063 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005064 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 /* Move the already present window to below the current window */
5067 if (curwin->w_arg_idx != i)
5068 {
5069 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5070 {
5071 if (wpnext->w_arg_idx == i)
5072 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005073 if (keep_tabs)
5074 {
5075 new_curwin = wpnext;
5076 new_curtab = curtab;
5077 }
5078 else
5079 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 break;
5081 }
5082 }
5083 }
5084 }
5085 else if (split_ret == OK)
5086 {
5087 if (!use_firstwin) /* split current window */
5088 {
5089 p_ea_save = p_ea;
5090 p_ea = TRUE; /* use space from all windows */
5091 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5092 p_ea = p_ea_save;
5093 if (split_ret == FAIL)
5094 continue;
5095 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 else /* first window: do autocmd for leaving this buffer */
5097 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
5099 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005100 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 */
5102 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005103 if (i == 0)
5104 {
5105 new_curwin = curwin;
5106 new_curtab = curtab;
5107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5109 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005110 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005112 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 if (use_firstwin)
5114 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 use_firstwin = FALSE;
5116 }
5117 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005118
5119 /* When ":tab" was used open a new tab for a new window repeatedly. */
5120 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5121 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 }
5123
5124 /* Remove the "lock" on the argument list. */
5125 alist_unlink(alist);
5126
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005128
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005129 /* restore last referenced tabpage's curwin */
5130 if (last_curtab != new_curtab)
5131 {
5132 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005133 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005134 if (win_valid(last_curwin))
5135 win_enter(last_curwin, FALSE);
5136 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005137 /* to window with first arg */
5138 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005139 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005140 if (win_valid(new_curwin))
5141 win_enter(new_curwin, FALSE);
5142
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 vim_free(opened);
5145}
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147/*
5148 * Open a window for a number of buffers.
5149 */
5150 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005151ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152{
5153 buf_T *buf;
5154 win_T *wp, *wpnext;
5155 int split_ret = OK;
5156 int p_ea_save;
5157 int open_wins = 0;
5158 int r;
5159 int count; /* Maximum number of windows to open. */
5160 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005161 int had_tab = cmdmod.tab;
5162 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 if (eap->addr_count == 0) /* make as many windows as possible */
5165 count = 9999;
5166 else
5167 count = eap->line2; /* make as many windows as specified */
5168 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5169 all = FALSE;
5170 else
5171 all = TRUE;
5172
5173 setpcmark();
5174
5175#ifdef FEAT_GUI
5176 need_mouse_correct = TRUE;
5177#endif
5178
5179 /*
5180 * Close superfluous windows (two windows for the same buffer).
5181 * Also close windows that are not full-width.
5182 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005183 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005184 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005185 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005187 tpnext = curtab->tp_next;
5188 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005190 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005191 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005192 || ((cmdmod.split & WSP_VERT)
5193 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005194 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005195 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005196 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005197 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005198 {
5199 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005200 wpnext = firstwin; /* just in case an autocommand does
5201 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005202 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005203 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005204 }
5205 else
5206 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005208
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005209 /* Without the ":tab" modifier only do the current tab page. */
5210 if (had_tab == 0 || tpnext == NULL)
5211 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005212 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
5214
5215 /*
5216 * Go through the buffer list. When a buffer doesn't have a window yet,
5217 * open one. Otherwise move the window to the right position.
5218 * Watch out for autocommands that delete buffers or windows!
5219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5221 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5225 {
5226 /* Check if this buffer needs a window */
5227 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5228 continue;
5229
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005230 if (had_tab != 0)
5231 {
5232 /* With the ":tab" modifier don't move the window. */
5233 if (buf->b_nwindows > 0)
5234 wp = lastwin; /* buffer has a window, skip it */
5235 else
5236 wp = NULL;
5237 }
5238 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005239 {
5240 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005241 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005242 if (wp->w_buffer == buf)
5243 break;
5244 /* If the buffer already has a window, move it */
5245 if (wp != NULL)
5246 win_move_after(wp, curwin);
5247 }
5248
5249 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005251 bufref_T bufref;
5252
5253 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 /* Split the window and put the buffer in it */
5256 p_ea_save = p_ea;
5257 p_ea = TRUE; /* use space from all windows */
5258 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5259 ++open_wins;
5260 p_ea = p_ea_save;
5261 if (split_ret == FAIL)
5262 continue;
5263
5264 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005265#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 swap_exists_action = SEA_DIALOG;
5267#endif
5268 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005269 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005271 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005272#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 swap_exists_action = SEA_NONE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 break;
5276 }
Bram Moolenaare64ac772005-12-07 20:54:59 +00005277#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 if (swap_exists_action == SEA_QUIT)
5279 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005280# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005281 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005282
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005283 /* Reset the error/interrupt/exception state here so that
5284 * aborting() returns FALSE when closing a window. */
5285 enter_cleanup(&cs);
5286# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005287
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005288 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 win_close(curwin, TRUE);
5290 --open_wins;
5291 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005292 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005293
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005294# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005295 /* Restore the error/interrupt/exception state if not
5296 * discarded by a new aborting error, interrupt, or uncaught
5297 * exception. */
5298 leave_cleanup(&cs);
5299# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
5301 else
5302 handle_swap_exists(NULL);
5303#endif
5304 }
5305
5306 ui_breakcheck();
5307 if (got_int)
5308 {
5309 (void)vgetc(); /* only break the file loading, not the rest */
5310 break;
5311 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005312#ifdef FEAT_EVAL
5313 /* Autocommands deleted the buffer or aborted script processing!!! */
5314 if (aborting())
5315 break;
5316#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005317 /* When ":tab" was used open a new tab for a new window repeatedly. */
5318 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5319 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324
5325 /*
5326 * Close superfluous windows.
5327 */
5328 for (wp = lastwin; open_wins > count; )
5329 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005330 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 if (!win_valid(wp))
5333 {
5334 /* BufWrite Autocommands made the window invalid, start over */
5335 wp = lastwin;
5336 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005337 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005339 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 --open_wins;
5341 wp = lastwin;
5342 }
5343 else
5344 {
5345 wp = wp->w_prev;
5346 if (wp == NULL)
5347 break;
5348 }
5349 }
5350}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005353static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005354
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355/*
5356 * do_modelines() - process mode lines for the current file
5357 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005358 * "flags" can be:
5359 * OPT_WINONLY only set options local to window
5360 * OPT_NOWIN don't set options local to window
5361 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 * Returns immediately if the "ml" option isn't set.
5363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005365do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005367 linenr_T lnum;
5368 int nmlines;
5369 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
5371 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5372 return;
5373
5374 /* Disallow recursive entry here. Can happen when executing a modeline
5375 * triggers an autocommand, which reloads modelines with a ":do". */
5376 if (entered)
5377 return;
5378
5379 ++entered;
5380 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5381 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005382 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005383 nmlines = 0;
5384
5385 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5386 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005387 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 nmlines = 0;
5389 --entered;
5390}
5391
5392#include "version.h" /* for version number */
5393
5394/*
5395 * chk_modeline() - check a single line for a mode string
5396 * Return FAIL if an error encountered.
5397 */
5398 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005399chk_modeline(
5400 linenr_T lnum,
5401 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 char_u *s;
5404 char_u *e;
5405 char_u *linecopy; /* local copy of any modeline found */
5406 int prev;
5407 int vers;
5408 int end;
5409 int retval = OK;
5410 char_u *save_sourcing_name;
5411 linenr_T save_sourcing_lnum;
5412#ifdef FEAT_EVAL
5413 scid_T save_SID;
5414#endif
5415
5416 prev = -1;
5417 for (s = ml_get(lnum); *s != NUL; ++s)
5418 {
5419 if (prev == -1 || vim_isspace(prev))
5420 {
5421 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5422 || STRNCMP(s, "vi:", (size_t)3) == 0)
5423 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005424 /* Accept both "vim" and "Vim". */
5425 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 {
5427 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5428 e = s + 4;
5429 else
5430 e = s + 3;
5431 vers = getdigits(&e);
5432 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005433 && (s[0] != 'V'
5434 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 && (s[3] == ':'
5436 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5437 || (VIM_VERSION_100 < vers && s[3] == '<')
5438 || (VIM_VERSION_100 > vers && s[3] == '>')
5439 || (VIM_VERSION_100 == vers && s[3] == '=')))
5440 break;
5441 }
5442 }
5443 prev = *s;
5444 }
5445
5446 if (*s)
5447 {
5448 do /* skip over "ex:", "vi:" or "vim:" */
5449 ++s;
5450 while (s[-1] != ':');
5451
5452 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5453 if (linecopy == NULL)
5454 return FAIL;
5455
5456 save_sourcing_lnum = sourcing_lnum;
5457 save_sourcing_name = sourcing_name;
5458 sourcing_lnum = lnum; /* prepare for emsg() */
5459 sourcing_name = (char_u *)"modelines";
5460
5461 end = FALSE;
5462 while (end == FALSE)
5463 {
5464 s = skipwhite(s);
5465 if (*s == NUL)
5466 break;
5467
5468 /*
5469 * Find end of set command: ':' or end of line.
5470 * Skip over "\:", replacing it with ":".
5471 */
5472 for (e = s; *e != ':' && *e != NUL; ++e)
5473 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005474 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 if (*e == NUL)
5476 end = TRUE;
5477
5478 /*
5479 * If there is a "set" command, require a terminating ':' and
5480 * ignore the stuff after the ':'.
5481 * "vi:set opt opt opt: foo" -- foo not interpreted
5482 * "vi:opt opt opt: foo" -- foo interpreted
5483 * Accept "se" for compatibility with Elvis.
5484 */
5485 if (STRNCMP(s, "set ", (size_t)4) == 0
5486 || STRNCMP(s, "se ", (size_t)3) == 0)
5487 {
5488 if (*e != ':') /* no terminating ':'? */
5489 break;
5490 end = TRUE;
5491 s = vim_strchr(s, ' ') + 1;
5492 }
5493 *e = NUL; /* truncate the set command */
5494
5495 if (*s != NUL) /* skip over an empty "::" */
5496 {
5497#ifdef FEAT_EVAL
5498 save_SID = current_SID;
5499 current_SID = SID_MODELINE;
5500#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005501 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502#ifdef FEAT_EVAL
5503 current_SID = save_SID;
5504#endif
5505 if (retval == FAIL) /* stop if error found */
5506 break;
5507 }
5508 s = e + 1; /* advance to next part */
5509 }
5510
5511 sourcing_lnum = save_sourcing_lnum;
5512 sourcing_name = save_sourcing_name;
5513
5514 vim_free(linecopy);
5515 }
5516 return retval;
5517}
5518
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005519#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005521read_viminfo_bufferlist(
5522 vir_T *virp,
5523 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524{
5525 char_u *tab;
5526 linenr_T lnum;
5527 colnr_T col;
5528 buf_T *buf;
5529 char_u *sfname;
5530 char_u *xline;
5531
5532 /* Handle long line and escaped characters. */
5533 xline = viminfo_readstring(virp, 1, FALSE);
5534
5535 /* don't read in if there are files on the command-line or if writing: */
5536 if (xline != NULL && !writing && ARGCOUNT == 0
5537 && find_viminfo_parameter('%') != NULL)
5538 {
5539 /* Format is: <fname> Tab <lnum> Tab <col>.
5540 * Watch out for a Tab in the file name, work from the end. */
5541 lnum = 0;
5542 col = 0;
5543 tab = vim_strrchr(xline, '\t');
5544 if (tab != NULL)
5545 {
5546 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005547 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 tab = vim_strrchr(xline, '\t');
5549 if (tab != NULL)
5550 {
5551 *tab++ = '\0';
5552 lnum = atol((char *)tab);
5553 }
5554 }
5555
5556 /* Expand "~/" in the file name at "line + 1" to a full path.
5557 * Then try shortening it by comparing with the current directory */
5558 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005559 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560
5561 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5562 if (buf != NULL) /* just in case... */
5563 {
5564 buf->b_last_cursor.lnum = lnum;
5565 buf->b_last_cursor.col = col;
5566 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5567 }
5568 }
5569 vim_free(xline);
5570
5571 return viminfo_readline(virp);
5572}
5573
5574 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005575write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576{
5577 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005579 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005581 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
5583 if (find_viminfo_parameter('%') == NULL)
5584 return;
5585
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005586 /* Without a number -1 is returned: do all buffers. */
5587 max_buffers = get_viminfo_parameter('%');
5588
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005590#define LINE_BUF_LEN (MAXPATHL + 40)
5591 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 if (line == NULL)
5593 return;
5594
Bram Moolenaarf740b292006-02-16 22:11:02 +00005595 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005598 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005599 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
5601 if (buf->b_fname == NULL
5602 || !buf->b_p_bl
5603#ifdef FEAT_QUICKFIX
5604 || bt_quickfix(buf)
5605#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005606#ifdef FEAT_TERMINAL
5607 || bt_terminal(buf)
5608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 || removable(buf->b_ffname))
5610 continue;
5611
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005612 if (max_buffers-- == 0)
5613 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 putc('%', fp);
5615 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005616 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 (long)buf->b_last_cursor.lnum,
5618 buf->b_last_cursor.col);
5619 viminfo_writestring(fp, line);
5620 }
5621 vim_free(line);
5622}
5623#endif
5624
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005625/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005626 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5627 */
5628 int
5629bt_normal(buf_T *buf)
5630{
5631 return buf != NULL && buf->b_p_bt[0] == NUL;
5632}
5633
5634/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005635 * Return TRUE if "buf" is the quickfix buffer.
5636 */
5637 int
5638bt_quickfix(buf_T *buf)
5639{
5640 return buf != NULL && buf->b_p_bt[0] == 'q';
5641}
5642
5643/*
5644 * Return TRUE if "buf" is a terminal buffer.
5645 */
5646 int
5647bt_terminal(buf_T *buf)
5648{
5649 return buf != NULL && buf->b_p_bt[0] == 't';
5650}
5651
5652/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005653 * Return TRUE if "buf" is a help buffer.
5654 */
5655 int
5656bt_help(buf_T *buf)
5657{
5658 return buf != NULL && buf->b_help;
5659}
5660
5661/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005662 * Return TRUE if "buf" is a prompt buffer.
5663 */
5664 int
5665bt_prompt(buf_T *buf)
5666{
5667 return buf != NULL && buf->b_p_bt[0] == 'p';
5668}
5669
5670/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005671 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5672 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005673 */
5674 int
5675bt_nofile(buf_T *buf)
5676{
5677 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5678 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005679 || buf->b_p_bt[0] == 't'
5680 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005681}
5682
5683/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005684 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5685 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005686 */
5687 int
5688bt_dontwrite(buf_T *buf)
5689{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005690 return buf != NULL && (buf->b_p_bt[0] == 'n'
5691 || buf->b_p_bt[0] == 't'
5692 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005693}
5694
5695 int
5696bt_dontwrite_msg(buf_T *buf)
5697{
5698 if (bt_dontwrite(buf))
5699 {
5700 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5701 return TRUE;
5702 }
5703 return FALSE;
5704}
5705
5706/*
5707 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5708 * and 'bufhidden'.
5709 */
5710 int
5711buf_hide(buf_T *buf)
5712{
5713 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5714 switch (buf->b_p_bh[0])
5715 {
5716 case 'u': /* "unload" */
5717 case 'w': /* "wipe" */
5718 case 'd': return FALSE; /* "delete" */
5719 case 'h': return TRUE; /* "hide" */
5720 }
5721 return (p_hid || cmdmod.hide);
5722}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723
5724/*
5725 * Return special buffer name.
5726 * Returns NULL when the buffer has a normal file name.
5727 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005728 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005729buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005731#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005733 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005734 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005735 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005736
5737 /*
5738 * For location list window, w_llist_ref points to the location list.
5739 * For quickfix window, w_llist_ref is NULL.
5740 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005741 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005742 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005743 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005744 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005747
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005749 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 if (bt_nofile(buf))
5751 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005752#ifdef FEAT_TERMINAL
5753 if (buf->b_term != NULL)
5754 return term_get_status_text(buf->b_term);
5755#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005756 if (buf->b_fname != NULL)
5757 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005758#ifdef FEAT_JOB_CHANNEL
5759 if (bt_prompt(buf))
5760 return (char_u *)_("[Prompt]");
5761#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005762 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005764
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005766 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 return NULL;
5768}
5769
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005770#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005771 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5772 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005773# define SWITCH_TO_WIN
5774
5775/*
5776 * Find a window that contains "buf" and switch to it.
5777 * If there is no such window, use the current window and change "curbuf".
5778 * Caller must initialize save_curbuf to NULL.
5779 * restore_win_for_buf() MUST be called later!
5780 */
5781 void
5782switch_to_win_for_buf(
5783 buf_T *buf,
5784 win_T **save_curwinp,
5785 tabpage_T **save_curtabp,
5786 bufref_T *save_curbuf)
5787{
5788 win_T *wp;
5789 tabpage_T *tp;
5790
5791 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5792 switch_buffer(save_curbuf, buf);
5793 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5794 {
5795 restore_win(*save_curwinp, *save_curtabp, TRUE);
5796 switch_buffer(save_curbuf, buf);
5797 }
5798}
5799
5800 void
5801restore_win_for_buf(
5802 win_T *save_curwin,
5803 tabpage_T *save_curtab,
5804 bufref_T *save_curbuf)
5805{
5806 if (save_curbuf->br_buf == NULL)
5807 restore_win(save_curwin, save_curtab, TRUE);
5808 else
5809 restore_buffer(save_curbuf);
5810}
5811#endif
5812
Bram Moolenaar4033c552017-09-16 20:54:51 +02005813#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005814/*
5815 * Find a window for buffer "buf".
5816 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5817 * If not found FAIL is returned.
5818 */
5819 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005820find_win_for_buf(
5821 buf_T *buf,
5822 win_T **wp,
5823 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005824{
5825 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5826 if ((*wp)->w_buffer == buf)
5827 goto win_found;
5828 return FAIL;
5829win_found:
5830 return OK;
5831}
5832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833
5834#if defined(FEAT_SIGNS) || defined(PROTO)
5835/*
5836 * Insert the sign into the signlist.
5837 */
5838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005839insert_sign(
5840 buf_T *buf, /* buffer to store sign in */
5841 signlist_T *prev, /* previous sign entry */
5842 signlist_T *next, /* next sign entry */
5843 int id, /* sign ID */
5844 linenr_T lnum, /* line number which gets the mark */
5845 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846{
5847 signlist_T *newsign;
5848
5849 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5850 if (newsign != NULL)
5851 {
5852 newsign->id = id;
5853 newsign->lnum = lnum;
5854 newsign->typenr = typenr;
5855 newsign->next = next;
5856#ifdef FEAT_NETBEANS_INTG
5857 newsign->prev = prev;
5858 if (next != NULL)
5859 next->prev = newsign;
5860#endif
5861
5862 if (prev == NULL)
5863 {
5864 /* When adding first sign need to redraw the windows to create the
5865 * column for signs. */
5866 if (buf->b_signlist == NULL)
5867 {
5868 redraw_buf_later(buf, NOT_VALID);
5869 changed_cline_bef_curs();
5870 }
5871
5872 /* first sign in signlist */
5873 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005874#ifdef FEAT_NETBEANS_INTG
5875 if (netbeans_active())
5876 buf->b_has_sign_column = TRUE;
5877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 }
5879 else
5880 prev->next = newsign;
5881 }
5882}
5883
5884/*
5885 * Add the sign into the signlist. Find the right spot to do it though.
5886 */
5887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005888buf_addsign(
5889 buf_T *buf, /* buffer to store sign in */
5890 int id, /* sign ID */
5891 linenr_T lnum, /* line number which gets the mark */
5892 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893{
5894 signlist_T *sign; /* a sign in the signlist */
5895 signlist_T *prev; /* the previous sign */
5896
5897 prev = NULL;
5898 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5899 {
5900 if (lnum == sign->lnum && id == sign->id)
5901 {
5902 sign->typenr = typenr;
5903 return;
5904 }
5905 else if (
5906#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5907 id < 0 &&
5908#endif
5909 lnum < sign->lnum)
5910 {
5911#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5912 /* XXX - GRP: Is this because of sign slide problem? Or is it
5913 * really needed? Or is it because we allow multiple signs per
5914 * line? If so, should I add that feature to FEAT_SIGNS?
5915 */
5916 while (prev != NULL && prev->lnum == lnum)
5917 prev = prev->prev;
5918 if (prev == NULL)
5919 sign = buf->b_signlist;
5920 else
5921 sign = prev->next;
5922#endif
5923 insert_sign(buf, prev, sign, id, lnum, typenr);
5924 return;
5925 }
5926 prev = sign;
5927 }
5928#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5929 /* XXX - GRP: See previous comment */
5930 while (prev != NULL && prev->lnum == lnum)
5931 prev = prev->prev;
5932 if (prev == NULL)
5933 sign = buf->b_signlist;
5934 else
5935 sign = prev->next;
5936#endif
5937 insert_sign(buf, prev, sign, id, lnum, typenr);
5938
5939 return;
5940}
5941
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005942/*
5943 * For an existing, placed sign "markId" change the type to "typenr".
5944 * Returns the line number of the sign, or zero if the sign is not found.
5945 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005946 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005947buf_change_sign_type(
5948 buf_T *buf, /* buffer to store sign in */
5949 int markId, /* sign ID */
5950 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951{
5952 signlist_T *sign; /* a sign in the signlist */
5953
5954 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5955 {
5956 if (sign->id == markId)
5957 {
5958 sign->typenr = typenr;
5959 return sign->lnum;
5960 }
5961 }
5962
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005963 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005964}
5965
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005967buf_getsigntype(
5968 buf_T *buf,
5969 linenr_T lnum,
5970 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005971{
5972 signlist_T *sign; /* a sign in a b_signlist */
5973
5974 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5975 if (sign->lnum == lnum
5976 && (type == SIGN_ANY
5977# ifdef FEAT_SIGN_ICONS
5978 || (type == SIGN_ICON
5979 && sign_get_image(sign->typenr) != NULL)
5980# endif
5981 || (type == SIGN_TEXT
5982 && sign_get_text(sign->typenr) != NULL)
5983 || (type == SIGN_LINEHL
5984 && sign_get_attr(sign->typenr, TRUE) != 0)))
5985 return sign->typenr;
5986 return 0;
5987}
5988
5989
5990 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005991buf_delsign(
5992 buf_T *buf, /* buffer sign is stored in */
5993 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994{
5995 signlist_T **lastp; /* pointer to pointer to current sign */
5996 signlist_T *sign; /* a sign in a b_signlist */
5997 signlist_T *next; /* the next sign in a b_signlist */
5998 linenr_T lnum; /* line number whose sign was deleted */
5999
6000 lastp = &buf->b_signlist;
6001 lnum = 0;
6002 for (sign = buf->b_signlist; sign != NULL; sign = next)
6003 {
6004 next = sign->next;
6005 if (sign->id == id)
6006 {
6007 *lastp = next;
6008#ifdef FEAT_NETBEANS_INTG
6009 if (next != NULL)
6010 next->prev = sign->prev;
6011#endif
6012 lnum = sign->lnum;
6013 vim_free(sign);
6014 break;
6015 }
6016 else
6017 lastp = &sign->next;
6018 }
6019
6020 /* When deleted the last sign need to redraw the windows to remove the
6021 * sign column. */
6022 if (buf->b_signlist == NULL)
6023 {
6024 redraw_buf_later(buf, NOT_VALID);
6025 changed_cline_bef_curs();
6026 }
6027
6028 return lnum;
6029}
6030
6031
6032/*
6033 * Find the line number of the sign with the requested id. If the sign does
6034 * not exist, return 0 as the line number. This will still let the correct file
6035 * get loaded.
6036 */
6037 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006038buf_findsign(
6039 buf_T *buf, /* buffer to store sign in */
6040 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041{
6042 signlist_T *sign; /* a sign in the signlist */
6043
6044 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6045 if (sign->id == id)
6046 return sign->lnum;
6047
6048 return 0;
6049}
6050
6051 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006052buf_findsign_id(
6053 buf_T *buf, /* buffer whose sign we are searching for */
6054 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006055{
6056 signlist_T *sign; /* a sign in the signlist */
6057
6058 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6059 if (sign->lnum == lnum)
6060 return sign->id;
6061
6062 return 0;
6063}
6064
6065
6066# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6067/* see if a given type of sign exists on a specific line */
6068 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006069buf_findsigntype_id(
6070 buf_T *buf, /* buffer whose sign we are searching for */
6071 linenr_T lnum, /* line number of sign */
6072 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073{
6074 signlist_T *sign; /* a sign in the signlist */
6075
6076 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6077 if (sign->lnum == lnum && sign->typenr == typenr)
6078 return sign->id;
6079
6080 return 0;
6081}
6082
6083
6084# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6085/* return the number of icons on the given line */
6086 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088{
6089 signlist_T *sign; /* a sign in the signlist */
6090 int count = 0;
6091
6092 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6093 if (sign->lnum == lnum)
6094 if (sign_get_image(sign->typenr) != NULL)
6095 count++;
6096
6097 return count;
6098}
6099# endif /* FEAT_SIGN_ICONS */
6100# endif /* FEAT_NETBEANS_INTG */
6101
6102
6103/*
6104 * Delete signs in buffer "buf".
6105 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108{
6109 signlist_T *next;
6110
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006111 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006112 * sign column. Not when curwin is NULL (this means we're exiting). */
6113 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006114 {
6115 redraw_buf_later(buf, NOT_VALID);
6116 changed_cline_bef_curs();
6117 }
6118
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 while (buf->b_signlist != NULL)
6120 {
6121 next = buf->b_signlist->next;
6122 vim_free(buf->b_signlist);
6123 buf->b_signlist = next;
6124 }
6125}
6126
6127/*
6128 * Delete all signs in all buffers.
6129 */
6130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006131buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132{
6133 buf_T *buf; /* buffer we are checking for signs */
6134
Bram Moolenaar29323592016-07-24 22:04:11 +02006135 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138}
6139
6140/*
6141 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6142 */
6143 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006144sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145{
6146 buf_T *buf;
6147 signlist_T *p;
6148 char lbuf[BUFSIZ];
6149
6150 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6151 msg_putchar('\n');
6152 if (rbuf == NULL)
6153 buf = firstbuf;
6154 else
6155 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006156 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 {
6158 if (buf->b_signlist != NULL)
6159 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006160 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006161 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 msg_putchar('\n');
6163 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006164 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006166 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6168 MSG_PUTS(lbuf);
6169 msg_putchar('\n');
6170 }
6171 if (rbuf != NULL)
6172 break;
6173 buf = buf->b_next;
6174 }
6175}
6176
6177/*
6178 * Adjust a placed sign for inserted/deleted lines.
6179 */
6180 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006181sign_mark_adjust(
6182 linenr_T line1,
6183 linenr_T line2,
6184 long amount,
6185 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006186{
6187 signlist_T *sign; /* a sign in a b_signlist */
6188
6189 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6190 {
6191 if (sign->lnum >= line1 && sign->lnum <= line2)
6192 {
6193 if (amount == MAXLNUM)
6194 sign->lnum = line1;
6195 else
6196 sign->lnum += amount;
6197 }
6198 else if (sign->lnum > line2)
6199 sign->lnum += amount_after;
6200 }
6201}
6202#endif /* FEAT_SIGNS */
6203
6204/*
6205 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6206 */
6207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006208set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006209{
6210 if (on != curbuf->b_p_bl)
6211 {
6212 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213 if (on)
6214 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6215 else
6216 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 }
6218}
6219
6220/*
6221 * Read the file for "buf" again and check if the contents changed.
6222 * Return TRUE if it changed or this could not be checked.
6223 */
6224 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006225buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226{
6227 buf_T *newbuf;
6228 int differ = TRUE;
6229 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 exarg_T ea;
6232
6233 /* Allocate a buffer without putting it in the buffer list. */
6234 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6235 if (newbuf == NULL)
6236 return TRUE;
6237
6238 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6239 if (prep_exarg(&ea, buf) == FAIL)
6240 {
6241 wipe_buffer(newbuf, FALSE);
6242 return TRUE;
6243 }
6244
Bram Moolenaar071d4272004-06-13 20:20:40 +00006245 /* set curwin/curbuf to buf and save a few things */
6246 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006247
Bram Moolenaar4770d092006-01-12 23:22:24 +00006248 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 && readfile(buf->b_ffname, buf->b_fname,
6250 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6251 &ea, READ_NEW | READ_DUMMY) == OK)
6252 {
6253 /* compare the two files line by line */
6254 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6255 {
6256 differ = FALSE;
6257 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6258 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6259 {
6260 differ = TRUE;
6261 break;
6262 }
6263 }
6264 }
6265 vim_free(ea.cmd);
6266
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 /* restore curwin/curbuf and a few other things */
6268 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269
6270 if (curbuf != newbuf) /* safety check */
6271 wipe_buffer(newbuf, FALSE);
6272
6273 return differ;
6274}
6275
6276/*
6277 * Wipe out a buffer and decrement the last buffer number if it was used for
6278 * this buffer. Call this to wipe out a temp buffer that does not contain any
6279 * marks.
6280 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006282wipe_buffer(
6283 buf_T *buf,
6284 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285{
6286 if (buf->b_fnum == top_file_num - 1)
6287 --top_file_num;
6288
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006290 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006291
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006292 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006293
Bram Moolenaar071d4272004-06-13 20:20:40 +00006294 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006295 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006296}