blob: 64ba5ab0e6bbf8c02186860fab0b58e8b638d70b [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);
207 if (perm >= 0 && (0
208# ifdef S_ISFIFO
209 || S_ISFIFO(perm)
210# endif
211# ifdef S_ISSOCK
212 || S_ISSOCK(perm)
213# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200214# ifdef OPEN_CHR_FILES
215 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
216# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200217 ))
218 read_fifo = TRUE;
219 if (read_fifo)
220 curbuf->b_p_bin = TRUE;
221#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100222 if (shortmess(SHM_FILEINFO))
223 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200225 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200226 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
227#ifdef UNIX
228 if (read_fifo)
229 {
230 curbuf->b_p_bin = save_bin;
231 if (retval == OK)
232 retval = read_buffer(FALSE, eap, flags);
233 }
234#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100235 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236#ifdef FEAT_NETBEANS_INTG
237 netbeansFireChanges = oldFire;
238#endif
239 /* Help buffer is filtered. */
Bram Moolenaard28cc3f2017-07-27 22:03:50 +0200240 if (bt_help(curbuf))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 fix_help_buffer();
242 }
243 else if (read_stdin)
244 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200245 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246
247 /*
248 * First read the text in binary mode into the buffer.
249 * Then read from that same buffer and append at the end. This makes
250 * it possible to retry when 'fileformat' or 'fileencoding' was
251 * guessed wrong.
252 */
253 curbuf->b_p_bin = TRUE;
254 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200255 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
256 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257 curbuf->b_p_bin = save_bin;
258 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200259 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260 }
261
262 /* if first time loading this buffer, init b_chartab[] */
263 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100264 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100266#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100267 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100268#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100269 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271 /*
272 * Set/reset the Changed flag first, autocmds may change the buffer.
273 * Apply the automatic commands, before processing the modelines.
Bram Moolenaar04958cb2018-06-23 19:23:02 +0200274 * So the modelines have priority over autocommands.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 */
276 /* When reading stdin, the buffer contents always needs writing, so set
277 * the changed flag. Unless in readonly mode: "ls | gview -".
278 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000279 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 || modified_was_set /* ":set modified" used in autocmd */
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100281#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000284 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100286 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 unchanged(curbuf, FALSE);
288 save_file_ff(curbuf); /* keep this fileformat */
289
Bram Moolenaar8c64a362018-03-23 22:39:31 +0100290 /* Set last_changedtick to avoid triggering a TextChanged autocommand right
291 * after it was added. */
292 curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
293#ifdef FEAT_INS_EXPAND
294 curbuf->b_last_changedtick_pum = CHANGEDTICK(curbuf);
295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297 /* require "!" to overwrite the file, because it wasn't read completely */
298#ifdef FEAT_EVAL
299 if (aborting())
300#else
301 if (got_int)
302#endif
303 curbuf->b_flags |= BF_READERR;
304
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000305#ifdef FEAT_FOLDING
306 /* Need to update automatic folding. Do this before the autocommands,
307 * they may use the fold info. */
308 foldUpdateAll(curwin);
309#endif
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 /* need to set w_topline, unless some autocommand already did that. */
312 if (!(curwin->w_valid & VALID_TOPLINE))
313 {
314 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100315#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100317#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100319#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000320 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100321#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323#endif
324
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100325 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 /*
328 * The autocommands may have changed the current buffer. Apply the
329 * modelines to the correct buffer, if it still exists and is loaded.
330 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200331 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 {
333 aco_save_T aco;
334
335 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200336 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaara3227e22006-03-08 21:32:40 +0000337 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
339
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100340#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100342 &retval);
343#else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
347 /* restore curwin/curbuf and a few other things */
348 aucmd_restbuf(&aco);
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 }
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 return retval;
353}
354
355/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200356 * Store "buf" in "bufref" and set the free count.
357 */
358 void
359set_bufref(bufref_T *bufref, buf_T *buf)
360{
361 bufref->br_buf = buf;
Bram Moolenaarfadacf02017-06-19 20:35:32 +0200362 bufref->br_fnum = buf == NULL ? 0 : buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200363 bufref->br_buf_free_count = buf_free_count;
364}
365
366/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200367 * Return TRUE if "bufref->br_buf" points to the same buffer as when
368 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200369 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200370 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
371 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200372 */
373 int
374bufref_valid(bufref_T *bufref)
375{
376 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200377 ? TRUE : buf_valid(bufref->br_buf)
378 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200379}
380
381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200383 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384 */
385 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100386buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387{
388 buf_T *bp;
389
Bram Moolenaar82404332016-07-10 17:00:38 +0200390 /* Assume that we more often have a recent buffer, start with the last
391 * one. */
392 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 if (bp == buf)
394 return TRUE;
395 return FALSE;
396}
397
398/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200399 * A hash table used to quickly lookup a buffer by its number.
400 */
401static hashtab_T buf_hashtab;
402
403 static void
404buf_hashtab_add(buf_T *buf)
405{
406 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
407 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
408 EMSG(_("E931: Buffer cannot be registered"));
409}
410
411 static void
412buf_hashtab_remove(buf_T *buf)
413{
414 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
415
416 if (!HASHITEM_EMPTY(hi))
417 hash_remove(&buf_hashtab, hi);
418}
419
Bram Moolenaara997b452018-04-17 23:24:06 +0200420static char *e_buflocked = N_("E937: Attempt to delete a buffer that is in use");
421
Bram Moolenaar480778b2016-07-14 22:09:39 +0200422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 * Close the link to a buffer.
424 * "action" is used when there is no longer a window for the buffer.
425 * It can be:
426 * 0 buffer becomes hidden
427 * DOBUF_UNLOAD buffer is unloaded
428 * DOBUF_DELETE buffer is unloaded and removed from buffer list
429 * DOBUF_WIPE buffer is unloaded and really deleted
430 * When doing all but the first one on the current buffer, the caller should
431 * get a new buffer very soon!
432 *
433 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100434 *
435 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
436 * cause there to be only one window with this buffer. e.g. when ":quit" is
437 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 */
439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100440close_buffer(
441 win_T *win, /* if not NULL, set b_last_cursor */
442 buf_T *buf,
443 int action,
444 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000445{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100447 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200448 bufref_T bufref;
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100449 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200450 win_T *the_curwin = curwin;
451 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 int unload_buf = (action != 0);
453 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
454 int wipe_buf = (action == DOBUF_WIPE);
455
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200456 /*
457 * Force unloading or deleting when 'bufhidden' says so.
458 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
459 * "hide" (otherwise we could never free or delete a buffer).
460 */
461 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
462 {
463 del_buf = TRUE;
464 unload_buf = TRUE;
465 }
466 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
467 {
468 del_buf = TRUE;
469 unload_buf = TRUE;
470 wipe_buf = TRUE;
471 }
472 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
473 unload_buf = TRUE;
474
Bram Moolenaar94053a52017-08-01 21:44:33 +0200475#ifdef FEAT_TERMINAL
Bram Moolenaar8adb0d02017-09-17 19:08:02 +0200476 if (bt_terminal(buf) && (buf->b_nwindows == 1 || del_buf))
Bram Moolenaar94053a52017-08-01 21:44:33 +0200477 {
478 if (term_job_running(buf->b_term))
479 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200480 if (wipe_buf || unload_buf)
Bram Moolenaara997b452018-04-17 23:24:06 +0200481 {
482 if (buf->b_locked)
483 {
484 EMSG(_(e_buflocked));
485 return;
486 }
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200487 /* Wiping out or unloading a terminal buffer kills the job. */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200488 free_terminal(buf);
Bram Moolenaara997b452018-04-17 23:24:06 +0200489 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200490 else
491 {
492 /* The job keeps running, hide the buffer. */
493 del_buf = FALSE;
494 unload_buf = FALSE;
495 }
496 }
497 else
498 {
499 /* A terminal buffer is wiped out if the job has finished. */
500 del_buf = TRUE;
501 unload_buf = TRUE;
502 wipe_buf = TRUE;
503 }
504 }
Bram Moolenaar94053a52017-08-01 21:44:33 +0200505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200507 /* Disallow deleting the buffer when it is locked (already being closed or
508 * halfway a command that relies on it). Unloading is allowed. */
509 if (buf->b_locked > 0 && (del_buf || wipe_buf))
510 {
Bram Moolenaara997b452018-04-17 23:24:06 +0200511 EMSG(_(e_buflocked));
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200512 return;
513 }
514
Bram Moolenaar4033c552017-09-16 20:54:51 +0200515 /* check no autocommands closed the window */
516 if (win != NULL && win_valid_any_tab(win))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 {
518 /* Set b_last_cursor when closing the last window for the buffer.
519 * Remember the last cursor position and window options of the buffer.
520 * This used to be only for the current window, but then options like
521 * 'foldmethod' may be lost with a ":only" command. */
522 if (buf->b_nwindows == 1)
523 set_last_cursor(win);
524 buflist_setfpos(buf, win,
525 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
526 win->w_cursor.col, TRUE);
527 }
528
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200529 set_bufref(&bufref, buf);
530
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531 /* When the buffer is no longer in a window, trigger BufWinLeave */
532 if (buf->b_nwindows == 1)
533 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200534 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200535 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
536 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200537 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100538 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200539 /* Autocommands deleted the buffer. */
540aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100541 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100543 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200544 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200545 if (abort_if_last && one_window())
546 /* Autocommands made this the only window. */
547 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549 /* When the buffer becomes hidden, but is not unloaded, trigger
550 * BufHidden */
551 if (!unload_buf)
552 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200553 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200554 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
555 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200556 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200557 /* Autocommands deleted the buffer. */
558 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200559 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200560 if (abort_if_last && one_window())
561 /* Autocommands made this the only window. */
562 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100564#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 if (aborting()) /* autocmds may abort script processing */
566 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200569
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200570 /* If the buffer was in curwin and the window has changed, go back to that
571 * window, if it still exists. This avoids that ":edit x" triggering a
572 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
573 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
574 {
575 block_autocmds();
576 goto_tabpage_win(the_curtab, the_curwin);
577 unblock_autocmds();
578 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200579
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 nwindows = buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581
582 /* decrease the link count from windows (unless not in any window) */
583 if (buf->b_nwindows > 0)
584 --buf->b_nwindows;
585
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100586#ifdef FEAT_DIFF
587 if (diffopt_hiddenoff() && !unload_buf && buf->b_nwindows == 0)
Bram Moolenaarb7407d32018-02-03 17:36:27 +0100588 diff_buf_delete(buf); /* Clear 'diff' for hidden buffer. */
Bram Moolenaar97ce4192017-12-01 20:35:58 +0100589#endif
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 /* Return when a window is displaying the buffer or when it's not
592 * unloaded. */
593 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595
596 /* Always remove the buffer when there is no file name. */
597 if (buf->b_ffname == NULL)
598 del_buf = TRUE;
599
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200600 /* When closing the current buffer stop Visual mode before freeing
601 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200602 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200603#if defined(EXITFREE)
604 && !entered_free_all_mem
605#endif
606 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200607 end_visual_mode();
608
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 /*
610 * Free all things allocated for this buffer.
611 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 /* Remember if we are closing the current buffer. Restore the number of
614 * windows, so that autocommands in buf_freeall() don't get confused. */
615 is_curbuf = (buf == curbuf);
616 buf->b_nwindows = nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200618 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200621 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100623#ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000624 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 /*
629 * It's possible that autocommands change curbuf to the one being deleted.
630 * This might cause the previous curbuf to be deleted unexpectedly. But
631 * in some cases it's OK to delete the curbuf, because a new one is
632 * obtained anyway. Therefore only return if curbuf changed to the
633 * deleted buffer.
634 */
635 if (buf == curbuf && !is_curbuf)
636 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200637
Bram Moolenaar4033c552017-09-16 20:54:51 +0200638 if (win_valid_any_tab(win) && win->w_buffer == buf)
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200639 win->w_buffer = NULL; /* make sure we don't use the buffer now */
640
641 /* Autocommands may have opened or closed windows for this buffer.
642 * Decrement the count for the close we do here. */
643 if (buf->b_nwindows > 0)
644 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 /*
647 * Remove the buffer from the list.
648 */
649 if (wipe_buf)
650 {
651#ifdef FEAT_SUN_WORKSHOP
652 if (usingSunWorkShop)
653 workshop_file_closed_lineno((char *)buf->b_ffname,
654 (int)buf->b_last_cursor.lnum);
655#endif
656 vim_free(buf->b_ffname);
657 vim_free(buf->b_sfname);
658 if (buf->b_prev == NULL)
659 firstbuf = buf->b_next;
660 else
661 buf->b_prev->b_next = buf->b_next;
662 if (buf->b_next == NULL)
663 lastbuf = buf->b_prev;
664 else
665 buf->b_next->b_prev = buf->b_prev;
666 free_buffer(buf);
667 }
668 else
669 {
670 if (del_buf)
671 {
672 /* Free all internal variables and reset option values, to make
673 * ":bdel" compatible with Vim 5.7. */
674 free_buffer_stuff(buf, TRUE);
675
676 /* Make it look like a new buffer. */
677 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
678
679 /* Init the options when loaded again. */
680 buf->b_p_initialized = FALSE;
681 }
682 buf_clear_file(buf);
683 if (del_buf)
684 buf->b_p_bl = FALSE;
685 }
686}
687
688/*
689 * Make buffer not contain a file.
690 */
691 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100692buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693{
694 buf->b_ml.ml_line_count = 1;
695 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 buf->b_p_eol = TRUE;
698 buf->b_start_eol = TRUE;
699#ifdef FEAT_MBYTE
700 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000701 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703 buf->b_ml.ml_mfp = NULL;
704 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
705#ifdef FEAT_NETBEANS_INTG
706 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707#endif
708}
709
710/*
711 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200712 * the file. Careful: get here with "curwin" NULL when exiting.
713 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200714 * BFA_DEL buffer is going to be deleted
715 * BFA_WIPE buffer is going to be wiped out
716 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100719buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200722 bufref_T bufref;
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200723 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200724 win_T *the_curwin = curwin;
725 tabpage_T *the_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726
Bram Moolenaar5a497892016-09-03 16:29:04 +0200727 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200728 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200729 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200730 if (buf->b_ml.ml_mfp != NULL)
731 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200732 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
733 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200734 && !bufref_valid(&bufref))
735 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200736 return;
737 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200738 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200740 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
741 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200742 && !bufref_valid(&bufref))
743 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 return;
745 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200746 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200748 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
749 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200750 && !bufref_valid(&bufref))
751 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 return;
753 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200754 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200755
Bram Moolenaar5a497892016-09-03 16:29:04 +0200756 /* If the buffer was in curwin and the window has changed, go back to that
757 * window, if it still exists. This avoids that ":edit x" triggering a
758 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
759 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
760 {
761 block_autocmds();
762 goto_tabpage_win(the_curtab, the_curwin);
763 unblock_autocmds();
764 }
Bram Moolenaar5a497892016-09-03 16:29:04 +0200765
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100766#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000767 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 return;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +0100769#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770
771 /*
772 * It's possible that autocommands change curbuf to the one being deleted.
773 * This might cause curbuf to be deleted unexpectedly. But in some cases
774 * it's OK to delete the curbuf, because a new one is obtained anyway.
775 * Therefore only return if curbuf changed to the deleted buffer.
776 */
777 if (buf == curbuf && !is_curbuf)
778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779#ifdef FEAT_DIFF
780 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
781#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200782#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100783 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200784 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100785 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200786#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000787
788#ifdef FEAT_FOLDING
789 /* No folds in an empty buffer. */
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000790 {
791 win_T *win;
792 tabpage_T *tp;
793
794 FOR_ALL_TAB_WINDOWS(tp, win)
795 if (win->w_buffer == buf)
796 clearFolding(win);
797 }
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000798#endif
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800#ifdef FEAT_TCL
801 tcl_buffer_free(buf);
802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 ml_close(buf, TRUE); /* close and delete the memline/memfile */
804 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200805 if ((flags & BFA_KEEP_UNDO) == 0)
806 {
807 u_blockfree(buf); /* free the memory allocated for undo */
808 u_clearall(buf); /* reset all undo information */
809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200811 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000813 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814}
815
816/*
817 * Free a buffer structure and the things it contains related to the buffer
818 * itself (not the file, that must have been done already).
819 */
820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100821free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200823 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200825#ifdef FEAT_EVAL
Bram Moolenaarb2c87502017-10-14 21:15:58 +0200826 /* b:changedtick uses an item in buf_T, remove it now */
827 dictitem_remove(buf->b_vars, (dictitem_T *)&buf->b_ct_di);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200828 unref_var_dict(buf->b_vars);
829#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200830#ifdef FEAT_LUA
831 lua_buffer_free(buf);
832#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000833#ifdef FEAT_MZSCHEME
834 mzscheme_buffer_free(buf);
835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836#ifdef FEAT_PERL
837 perl_buf_free(buf);
838#endif
839#ifdef FEAT_PYTHON
840 python_buffer_free(buf);
841#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200842#ifdef FEAT_PYTHON3
843 python3_buffer_free(buf);
844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#ifdef FEAT_RUBY
846 ruby_buffer_free(buf);
847#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200848#ifdef FEAT_JOB_CHANNEL
849 channel_buffer_free(buf);
850#endif
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200851#ifdef FEAT_TERMINAL
Bram Moolenaard85f2712017-07-28 21:51:57 +0200852 free_terminal(buf);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200853#endif
Bram Moolenaarf2732452018-06-03 14:47:35 +0200854#ifdef FEAT_JOB_CHANNEL
855 vim_free(buf->b_prompt_text);
856 free_callback(buf->b_prompt_callback, buf->b_prompt_partial);
857#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200858
859 buf_hashtab_remove(buf);
860
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200861 aubuflocal_remove(buf);
862
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200863 if (autocmd_busy)
864 {
865 /* Do not free the buffer structure while autocommands are executing,
866 * it's still needed. Free it when autocmd_busy is reset. */
867 buf->b_next = au_pending_free_buf;
868 au_pending_free_buf = buf;
869 }
870 else
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200871 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872}
873
874/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100875 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100876 */
877 static void
878init_changedtick(buf_T *buf)
879{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100880 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100881
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100882 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
883 di->di_tv.v_type = VAR_NUMBER;
884 di->di_tv.v_lock = VAR_FIXED;
885 di->di_tv.vval.v_number = 0;
886
Bram Moolenaar92769c32017-02-25 15:41:37 +0100887#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100888 STRCPY(buf->b_ct_di.di_key, "changedtick");
889 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100890#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100891}
892
893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
895 */
896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100897free_buffer_stuff(
898 buf_T *buf,
899 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900{
901 if (free_options)
902 {
903 clear_wininfo(buf); /* including window-local options */
904 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200905#ifdef FEAT_SPELL
906 ga_clear(&buf->b_s.b_langp);
907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 }
909#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100910 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100911 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100912
913 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
914 hash_init(&buf->b_vars->dv_hashtab);
915 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100916 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918#endif
919#ifdef FEAT_USR_CMDS
920 uc_clear(&buf->b_ucmds); /* clear local user commands */
921#endif
922#ifdef FEAT_SIGNS
923 buf_delete_signs(buf); /* delete any signs */
924#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000925#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200926 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928#ifdef FEAT_LOCALMAP
929 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
930 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
931#endif
932#ifdef FEAT_MBYTE
Bram Moolenaard23a8232018-02-10 18:45:26 +0100933 VIM_CLEAR(buf->b_start_fenc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#endif
935}
936
937/*
938 * Free the b_wininfo list for buffer "buf".
939 */
940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100941clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942{
943 wininfo_T *wip;
944
945 while (buf->b_wininfo != NULL)
946 {
947 wip = buf->b_wininfo;
948 buf->b_wininfo = wip->wi_next;
949 if (wip->wi_optset)
950 {
951 clear_winopt(&wip->wi_opt);
952#ifdef FEAT_FOLDING
953 deleteFoldRecurse(&wip->wi_folds);
954#endif
955 }
956 vim_free(wip);
957 }
958}
959
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960/*
961 * Go to another buffer. Handles the result of the ATTENTION dialog.
962 */
963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100964goto_buffer(
965 exarg_T *eap,
966 int start,
967 int dir,
968 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969{
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200970#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200971 bufref_T old_curbuf;
972
973 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974
975 swap_exists_action = SEA_DIALOG;
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200976#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
978 start, dir, count, eap->forceit);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200979#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
981 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200982# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000983 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000984
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000985 /* Reset the error/interrupt/exception state here so that
986 * aborting() returns FALSE when closing a window. */
987 enter_cleanup(&cs);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200988# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000989
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000990 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000991 win_close(curwin, TRUE);
992 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000993 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000994
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200995# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000996 /* Restore the error/interrupt/exception state if not discarded by a
997 * new aborting error, interrupt, or uncaught exception. */
998 leave_cleanup(&cs);
Bram Moolenaar0c72fe42018-03-29 16:04:08 +0200999# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 }
1001 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001002 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003#endif
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001004}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005
Bram Moolenaare64ac772005-12-07 20:54:59 +00001006#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007/*
1008 * Handle the situation of swap_exists_action being set.
1009 * It is allowed for "old_curbuf" to be NULL or invalid.
1010 */
1011 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001012handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013{
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001014# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001015 cleanup_T cs;
1016# endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001017# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001018 long old_tw = curbuf->b_p_tw;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001019# endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001020 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001021
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022 if (swap_exists_action == SEA_QUIT)
1023 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001024# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001025 /* Reset the error/interrupt/exception state here so that
1026 * aborting() returns FALSE when closing a buffer. */
1027 enter_cleanup(&cs);
1028# endif
1029
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 /* User selected Quit at ATTENTION prompt. Go back to previous
1031 * buffer. If that buffer is gone or the same as the current one,
1032 * open a new, empty buffer. */
1033 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001034 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001035 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001036 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1037 || old_curbuf->br_buf == curbuf)
1038 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1039 else
1040 buf = old_curbuf->br_buf;
1041 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001042 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001043 enter_buffer(buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001044# ifdef FEAT_SYN_HL
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001045 if (old_tw != curbuf->b_p_tw)
1046 check_colorcolumn(curwin);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001047# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001048 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001050
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001051# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001052 /* Restore the error/interrupt/exception state if not discarded by a
1053 * new aborting error, interrupt, or uncaught exception. */
1054 leave_cleanup(&cs);
1055# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 }
1057 else if (swap_exists_action == SEA_RECOVER)
1058 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001059# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001060 /* Reset the error/interrupt/exception state here so that
1061 * aborting() returns FALSE when closing a buffer. */
1062 enter_cleanup(&cs);
1063# endif
1064
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 /* User selected Recover at ATTENTION prompt. */
1066 msg_scroll = TRUE;
1067 ml_recover();
1068 MSG_PUTS("\n"); /* don't overwrite the last message */
1069 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001070 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001071
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001072# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001073 /* Restore the error/interrupt/exception state if not discarded by a
1074 * new aborting error, interrupt, or uncaught exception. */
1075 leave_cleanup(&cs);
1076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 }
1078 swap_exists_action = SEA_NONE;
1079}
1080#endif
1081
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082/*
1083 * do_bufdel() - delete or unload buffer(s)
1084 *
1085 * addr_count == 0: ":bdel" - delete current buffer
1086 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1087 * buffer "end_bnr", then any other arguments.
1088 * addr_count == 2: ":N,N bdel" - delete buffers in range
1089 *
1090 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1091 * DOBUF_DEL (":bdel")
1092 *
1093 * Returns error message or NULL
1094 */
1095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001096do_bufdel(
1097 int command,
1098 char_u *arg, /* pointer to extra arguments */
1099 int addr_count,
1100 int start_bnr, /* first buffer number in a range */
1101 int end_bnr, /* buffer nr or last buffer nr in a range */
1102 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103{
1104 int do_current = 0; /* delete current buffer? */
1105 int deleted = 0; /* number of buffers deleted */
1106 char_u *errormsg = NULL; /* return value */
1107 int bnr; /* buffer number */
1108 char_u *p;
1109
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 if (addr_count == 0)
1111 {
1112 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1113 }
1114 else
1115 {
1116 if (addr_count == 2)
1117 {
1118 if (*arg) /* both range and argument is not allowed */
1119 return (char_u *)_(e_trailing);
1120 bnr = start_bnr;
1121 }
1122 else /* addr_count == 1 */
1123 bnr = end_bnr;
1124
1125 for ( ;!got_int; ui_breakcheck())
1126 {
1127 /*
1128 * delete the current buffer last, otherwise when the
1129 * current buffer is deleted, the next buffer becomes
1130 * the current one and will be loaded, which may then
1131 * also be deleted, etc.
1132 */
1133 if (bnr == curbuf->b_fnum)
1134 do_current = bnr;
1135 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1136 forceit) == OK)
1137 ++deleted;
1138
1139 /*
1140 * find next buffer number to delete/unload
1141 */
1142 if (addr_count == 2)
1143 {
1144 if (++bnr > end_bnr)
1145 break;
1146 }
1147 else /* addr_count == 1 */
1148 {
1149 arg = skipwhite(arg);
1150 if (*arg == NUL)
1151 break;
1152 if (!VIM_ISDIGIT(*arg))
1153 {
1154 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001155 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1156 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 if (bnr < 0) /* failed */
1158 break;
1159 arg = p;
1160 }
1161 else
1162 bnr = getdigits(&arg);
1163 }
1164 }
1165 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1166 FORWARD, do_current, forceit) == OK)
1167 ++deleted;
1168
1169 if (deleted == 0)
1170 {
1171 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001172 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001174 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001176 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 errormsg = IObuff;
1178 }
1179 else if (deleted >= p_report)
1180 {
1181 if (command == DOBUF_UNLOAD)
1182 {
1183 if (deleted == 1)
1184 MSG(_("1 buffer unloaded"));
1185 else
1186 smsg((char_u *)_("%d buffers unloaded"), deleted);
1187 }
1188 else if (command == DOBUF_DEL)
1189 {
1190 if (deleted == 1)
1191 MSG(_("1 buffer deleted"));
1192 else
1193 smsg((char_u *)_("%d buffers deleted"), deleted);
1194 }
1195 else
1196 {
1197 if (deleted == 1)
1198 MSG(_("1 buffer wiped out"));
1199 else
1200 smsg((char_u *)_("%d buffers wiped out"), deleted);
1201 }
1202 }
1203 }
1204
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205
1206 return errormsg;
1207}
1208
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001209static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001210
1211/*
1212 * Make the current buffer empty.
1213 * Used when it is wiped out and it's the last buffer.
1214 */
1215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001216empty_curbuf(
1217 int close_others,
1218 int forceit,
1219 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001220{
1221 int retval;
1222 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001223 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001224
1225 if (action == DOBUF_UNLOAD)
1226 {
1227 EMSG(_("E90: Cannot unload last buffer"));
1228 return FAIL;
1229 }
1230
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001231 set_bufref(&bufref, buf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001232 if (close_others)
1233 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001234 close_windows(buf, TRUE);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001235
1236 setpcmark();
1237 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1238 forceit ? ECMD_FORCEIT : 0, curwin);
1239
1240 /*
1241 * do_ecmd() may create a new buffer, then we have to delete
1242 * the old one. But do_ecmd() may have done that already, check
1243 * if the buffer still exists.
1244 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001245 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001246 close_buffer(NULL, buf, action, FALSE);
1247 if (!close_others)
1248 need_fileinfo = FALSE;
1249 return retval;
1250}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251/*
1252 * Implementation of the commands for the buffer list.
1253 *
1254 * action == DOBUF_GOTO go to specified buffer
1255 * action == DOBUF_SPLIT split window and go to specified buffer
1256 * action == DOBUF_UNLOAD unload specified buffer(s)
1257 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1258 * action == DOBUF_WIPE delete specified buffer(s) really
1259 *
1260 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1261 * start == DOBUF_FIRST go to "count" buffer from first buffer
1262 * start == DOBUF_LAST go to "count" buffer from last buffer
1263 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1264 *
1265 * Return FAIL or OK.
1266 */
1267 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001268do_buffer(
1269 int action,
1270 int start,
1271 int dir, /* FORWARD or BACKWARD */
1272 int count, /* buffer number or number of buffers */
1273 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 buf_T *buf;
1276 buf_T *bp;
1277 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1278 || action == DOBUF_WIPE);
1279
1280 switch (start)
1281 {
1282 case DOBUF_FIRST: buf = firstbuf; break;
1283 case DOBUF_LAST: buf = lastbuf; break;
1284 default: buf = curbuf; break;
1285 }
1286 if (start == DOBUF_MOD) /* find next modified buffer */
1287 {
1288 while (count-- > 0)
1289 {
1290 do
1291 {
1292 buf = buf->b_next;
1293 if (buf == NULL)
1294 buf = firstbuf;
1295 }
1296 while (buf != curbuf && !bufIsChanged(buf));
1297 }
1298 if (!bufIsChanged(buf))
1299 {
1300 EMSG(_("E84: No modified buffer found"));
1301 return FAIL;
1302 }
1303 }
1304 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1305 {
1306 while (buf != NULL && buf->b_fnum != count)
1307 buf = buf->b_next;
1308 }
1309 else
1310 {
1311 bp = NULL;
1312 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1313 {
1314 /* remember the buffer where we start, we come back there when all
1315 * buffers are unlisted. */
1316 if (bp == NULL)
1317 bp = buf;
1318 if (dir == FORWARD)
1319 {
1320 buf = buf->b_next;
1321 if (buf == NULL)
1322 buf = firstbuf;
1323 }
1324 else
1325 {
1326 buf = buf->b_prev;
1327 if (buf == NULL)
1328 buf = lastbuf;
1329 }
1330 /* don't count unlisted buffers */
1331 if (unload || buf->b_p_bl)
1332 {
1333 --count;
1334 bp = NULL; /* use this buffer as new starting point */
1335 }
1336 if (bp == buf)
1337 {
1338 /* back where we started, didn't find anything. */
1339 EMSG(_("E85: There is no listed buffer"));
1340 return FAIL;
1341 }
1342 }
1343 }
1344
1345 if (buf == NULL) /* could not find it */
1346 {
1347 if (start == DOBUF_FIRST)
1348 {
1349 /* don't warn when deleting */
1350 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001351 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 else if (dir == FORWARD)
1354 EMSG(_("E87: Cannot go beyond last buffer"));
1355 else
1356 EMSG(_("E88: Cannot go before first buffer"));
1357 return FAIL;
1358 }
1359
1360#ifdef FEAT_GUI
1361 need_mouse_correct = TRUE;
1362#endif
1363
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 /*
1365 * delete buffer buf from memory and/or the list
1366 */
1367 if (unload)
1368 {
1369 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001370 bufref_T bufref;
1371
Bram Moolenaara997b452018-04-17 23:24:06 +02001372 if (buf->b_locked)
1373 {
1374 EMSG(_(e_buflocked));
1375 return FAIL;
1376 }
1377
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001378 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379
1380 /* When unloading or deleting a buffer that's already unloaded and
1381 * unlisted: fail silently. */
1382 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1383 return FAIL;
1384
1385 if (!forceit && bufIsChanged(buf))
1386 {
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001387#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if ((p_confirm || cmdmod.confirm) && p_write)
1389 {
1390 dialog_changed(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001391 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 /* Autocommand deleted buffer, oops! It's not changed
1393 * now. */
1394 return FAIL;
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001395 /* If it's still changed fail silently, the dialog already
1396 * mentioned why it fails. */
1397 if (bufIsChanged(buf))
1398 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001400 else
Bram Moolenaar0c72fe42018-03-29 16:04:08 +02001401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 {
1403 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1404 buf->b_fnum);
1405 return FAIL;
1406 }
1407 }
1408
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001409 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001410 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001411 end_visual_mode();
1412
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 /*
1414 * If deleting the last (listed) buffer, make it empty.
1415 * The last (listed) buffer cannot be unloaded.
1416 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001417 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 if (bp->b_p_bl && bp != buf)
1419 break;
1420 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001421 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 /*
1424 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001425 * (unless it's the only window). Repeat this so long as we end up in
1426 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001428 while (buf == curbuf
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001429 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar459ca562016-11-10 18:16:33 +01001430 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001431 {
1432 if (win_close(curwin, FALSE) == FAIL)
1433 break;
1434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436 /*
1437 * If the buffer to be deleted is not the current one, delete it here.
1438 */
1439 if (buf != curbuf)
1440 {
Bram Moolenaarf740b292006-02-16 22:11:02 +00001441 close_windows(buf, FALSE);
Bram Moolenaar4033c552017-09-16 20:54:51 +02001442 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001443 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 return OK;
1445 }
1446
1447 /*
1448 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001449 * There should be another, otherwise it would have been handled
1450 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001451 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 * Then prefer the buffer we most recently visited.
1453 * Else try to find one that is loaded, after the current buffer,
1454 * then before the current buffer.
1455 * Finally use any buffer.
1456 */
1457 buf = NULL; /* selected buffer */
1458 bp = NULL; /* used when no loaded buffer found */
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001459 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1460 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461#ifdef FEAT_JUMPLIST
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001462 else if (curwin->w_jumplistlen > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
1464 int jumpidx;
1465
1466 jumpidx = curwin->w_jumplistidx - 1;
1467 if (jumpidx < 0)
1468 jumpidx = curwin->w_jumplistlen - 1;
1469
1470 forward = jumpidx;
1471 while (jumpidx != curwin->w_jumplistidx)
1472 {
1473 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1474 if (buf != NULL)
1475 {
1476 if (buf == curbuf || !buf->b_p_bl)
1477 buf = NULL; /* skip current and unlisted bufs */
1478 else if (buf->b_ml.ml_mfp == NULL)
1479 {
1480 /* skip unloaded buf, but may keep it for later */
1481 if (bp == NULL)
1482 bp = buf;
1483 buf = NULL;
1484 }
1485 }
1486 if (buf != NULL) /* found a valid buffer: stop searching */
1487 break;
1488 /* advance to older entry in jump list */
1489 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1490 break;
1491 if (--jumpidx < 0)
1492 jumpidx = curwin->w_jumplistlen - 1;
1493 if (jumpidx == forward) /* List exhausted for sure */
1494 break;
1495 }
1496 }
1497#endif
1498
1499 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1500 {
1501 forward = TRUE;
1502 buf = curbuf->b_next;
1503 for (;;)
1504 {
1505 if (buf == NULL)
1506 {
1507 if (!forward) /* tried both directions */
1508 break;
1509 buf = curbuf->b_prev;
1510 forward = FALSE;
1511 continue;
1512 }
1513 /* in non-help buffer, try to skip help buffers, and vv */
1514 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1515 {
1516 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1517 break;
1518 if (bp == NULL) /* remember unloaded buf for later */
1519 bp = buf;
1520 }
1521 if (forward)
1522 buf = buf->b_next;
1523 else
1524 buf = buf->b_prev;
1525 }
1526 }
1527 if (buf == NULL) /* No loaded buffer, use unloaded one */
1528 buf = bp;
1529 if (buf == NULL) /* No loaded buffer, find listed one */
1530 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001531 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 if (buf->b_p_bl && buf != curbuf)
1533 break;
1534 }
1535 if (buf == NULL) /* Still no buffer, just take one */
1536 {
1537 if (curbuf->b_next != NULL)
1538 buf = curbuf->b_next;
1539 else
1540 buf = curbuf->b_prev;
1541 }
1542 }
1543
Bram Moolenaara02471e2014-01-10 16:43:14 +01001544 if (buf == NULL)
1545 {
1546 /* Autocommands must have wiped out all other buffers. Only option
1547 * now is to make the current buffer empty. */
1548 return empty_curbuf(FALSE, forceit, action);
1549 }
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 /*
1552 * make buf current buffer
1553 */
1554 if (action == DOBUF_SPLIT) /* split window first */
1555 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001556 /* If 'switchbuf' contains "useopen": jump to first window containing
1557 * "buf" if one exists */
1558 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001559 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001560 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001561 * page containing "buf" if one exists */
1562 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 return OK;
1564 if (win_split(0, 0) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 return FAIL;
1566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567
1568 /* go to current buffer - nothing to do */
1569 if (buf == curbuf)
1570 return OK;
1571
1572 /*
1573 * Check if the current buffer may be abandoned.
1574 */
1575 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1576 {
1577#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1578 if ((p_confirm || cmdmod.confirm) && p_write)
1579 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001580 bufref_T bufref;
1581
1582 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 dialog_changed(curbuf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001584 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 /* Autocommand deleted buffer, oops! */
1586 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 }
1588 if (bufIsChanged(curbuf))
1589#endif
1590 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001591 no_write_message();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 return FAIL;
1593 }
1594 }
1595
1596 /* Go to the other buffer. */
1597 set_curbuf(buf, action);
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001600 {
1601 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001604#if defined(FEAT_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 if (aborting()) /* autocmds may abort script processing */
1606 return FAIL;
1607#endif
1608
1609 return OK;
1610}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612/*
1613 * Set current buffer to "buf". Executes autocommands and closes current
1614 * buffer. "action" tells how to close the current buffer:
1615 * DOBUF_GOTO free or hide it
1616 * DOBUF_SPLIT nothing
1617 * DOBUF_UNLOAD unload it
1618 * DOBUF_DEL delete it
1619 * DOBUF_WIPE wipe it out
1620 */
1621 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001622set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623{
1624 buf_T *prevbuf;
1625 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1626 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001627#ifdef FEAT_SYN_HL
1628 long old_tw = curbuf->b_p_tw;
1629#endif
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001630 bufref_T newbufref;
1631 bufref_T prevbufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001634 if (!cmdmod.keepalt)
1635 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001636 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 /* Don't restart Select mode after switching to another buffer. */
1639 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001641 /* close_windows() or apply_autocmds() may change curbuf and wipe out "buf"
1642 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 prevbuf = curbuf;
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001644 set_bufref(&prevbufref, prevbuf);
1645 set_bufref(&newbufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001647 /* Autocommands may delete the curren buffer and/or the buffer we wan to go
1648 * to. In those cases don't close the buffer. */
Bram Moolenaar82404332016-07-10 17:00:38 +02001649 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001650 || (bufref_valid(&prevbufref)
1651 && bufref_valid(&newbufref)
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001652#ifdef FEAT_EVAL
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001653 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001655 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001657#ifdef FEAT_SYN_HL
1658 if (prevbuf == curwin->w_buffer)
1659 reset_synblock(curwin);
1660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001662 close_windows(prevbuf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001663#if defined(FEAT_EVAL)
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001664 if (bufref_valid(&prevbufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665#else
Bram Moolenaar9bca8052017-12-18 12:37:55 +01001666 if (bufref_valid(&prevbufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001668 {
Bram Moolenaare25865a2012-07-06 16:22:02 +02001669 win_T *previouswin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001670 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001671 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1673 unload ? action : (action == DOBUF_GOTO
Bram Moolenaareb44a682017-08-03 22:44:55 +02001674 && !buf_hide(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001675 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaare25865a2012-07-06 16:22:02 +02001676 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001677 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001678 curwin = previouswin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001681 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001682 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001683 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1684 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001685#ifdef FEAT_EVAL
Bram Moolenaar4033c552017-09-16 20:54:51 +02001686 && !aborting()
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001688 ) || curwin->w_buffer == NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001689 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001691#ifdef FEAT_SYN_HL
1692 if (old_tw != curbuf->b_p_tw)
1693 check_colorcolumn(curwin);
1694#endif
1695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696}
1697
1698/*
1699 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001700 * Old curbuf must have been abandoned already! This also means "curbuf" may
1701 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 */
1703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001704enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705{
1706 /* Copy buffer and window local option values. Not for a help buffer. */
1707 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1708 if (!buf->b_help)
1709 get_winopts(buf);
1710#ifdef FEAT_FOLDING
1711 else
1712 /* Remove all folds in the window. */
1713 clearFolding(curwin);
1714 foldUpdateAll(curwin); /* update folds (later). */
1715#endif
1716
1717 /* Get the buffer in the current window. */
1718 curwin->w_buffer = buf;
1719 curbuf = buf;
1720 ++curbuf->b_nwindows;
1721
1722#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001723 if (curwin->w_p_diff)
1724 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725#endif
1726
Bram Moolenaara971b822011-09-14 14:43:25 +02001727#ifdef FEAT_SYN_HL
Bram Moolenaar96ca27a2017-07-17 23:20:24 +02001728 curwin->w_s = &(curbuf->b_s);
Bram Moolenaara971b822011-09-14 14:43:25 +02001729#endif
1730
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 /* Cursor on first line by default. */
1732 curwin->w_cursor.lnum = 1;
1733 curwin->w_cursor.col = 0;
1734#ifdef FEAT_VIRTUALEDIT
1735 curwin->w_cursor.coladd = 0;
1736#endif
1737 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001738 curwin->w_topline_was_set = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001740 /* mark cursor position as being invalid */
1741 curwin->w_valid = 0;
1742
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 /* Make sure the buffer is loaded. */
1744 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001745 {
1746 /* If there is no filetype, allow for detecting one. Esp. useful for
1747 * ":ball" used in a autocommand. If there already is a filetype we
1748 * might prefer to keep it. */
1749 if (*curbuf->b_p_ft == NUL)
1750 did_filetype = FALSE;
1751
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001752 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 else
1755 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001756 if (!msg_silent)
1757 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 curwin->w_topline = 1;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001760#ifdef FEAT_DIFF
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 curwin->w_topfill = 0;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1764 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
1766
1767 /* If autocommands did not change the cursor position, restore cursor lnum
1768 * and possibly cursor col. */
1769 if (curwin->w_cursor.lnum == 1 && inindent(0))
1770 buflist_getfpos();
1771
1772 check_arg_idx(curwin); /* check for valid arg_idx */
1773#ifdef FEAT_TITLE
1774 maketitle();
1775#endif
Bram Moolenaard4153d42008-11-15 15:06:17 +00001776 /* when autocmds didn't change it */
1777 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1779
Bram Moolenaar009b2592004-10-24 19:18:58 +00001780#ifdef FEAT_NETBEANS_INTG
1781 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001782 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001783#endif
1784
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001785 /* Change directories when the 'acd' option is set. */
Bram Moolenaar6f470022018-04-10 18:47:20 +02001786 DO_AUTOCHDIR;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
1788#ifdef FEAT_KEYMAP
1789 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001790 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001792#ifdef FEAT_SPELL
1793 /* May need to set the spell language. Can only do this after the buffer
1794 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001795 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1796 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001797#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001798#ifdef FEAT_VIMINFO
1799 curbuf->b_last_used = vim_time();
1800#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001801
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 redraw_later(NOT_VALID);
1803}
1804
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001805#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1806/*
1807 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001808 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001809 */
1810 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001811do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001812{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001813 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001814 && curbuf->b_ffname != NULL
Bram Moolenaarb7407d32018-02-03 17:36:27 +01001815 && vim_chdirfile(curbuf->b_ffname, "auto") == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001816 shorten_fnames(TRUE);
1817}
1818#endif
1819
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001820 void
1821no_write_message(void)
1822{
1823#ifdef FEAT_TERMINAL
1824 if (term_job_running(curbuf->b_term))
1825 EMSG(_("E948: Job still running (add ! to end the job)"));
1826 else
1827#endif
1828 EMSG(_("E37: No write since last change (add ! to override)"));
1829}
1830
1831 void
Bram Moolenaar7a760922018-02-19 23:10:02 +01001832no_write_message_nobang(buf_T *buf UNUSED)
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001833{
1834#ifdef FEAT_TERMINAL
Bram Moolenaar7a760922018-02-19 23:10:02 +01001835 if (term_job_running(buf->b_term))
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +02001836 EMSG(_("E948: Job still running"));
1837 else
1838#endif
1839 EMSG(_("E37: No write since last change"));
1840}
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/*
1843 * functions for dealing with the buffer list
1844 */
1845
Bram Moolenaar480778b2016-07-14 22:09:39 +02001846static int top_file_num = 1; /* highest file number */
1847
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848/*
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001849 * Return TRUE if the current buffer is empty, unnamed, unmodified and used in
1850 * only one window. That means it can be re-used.
1851 */
1852 int
1853curbuf_reusable(void)
1854{
1855 return (curbuf != NULL
1856 && curbuf->b_ffname == NULL
1857 && curbuf->b_nwindows <= 1
1858 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY())
1859 && !curbufIsChanged());
1860}
1861
1862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 * Add a file name to the buffer list. Return a pointer to the buffer.
1864 * If the same file name already exists return a pointer to that buffer.
1865 * If it does not exist, or if fname == NULL, a new entry is created.
1866 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1867 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1868 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001869 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001870 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1871 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 * This is the ONLY way to create a new buffer.
1873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001875buflist_new(
1876 char_u *ffname, /* full path of fname or relative */
1877 char_u *sfname, /* short fname or NULL */
1878 linenr_T lnum, /* preferred cursor line */
1879 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880{
1881 buf_T *buf;
1882#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001883 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884#endif
1885
Bram Moolenaar480778b2016-07-14 22:09:39 +02001886 if (top_file_num == 1)
1887 hash_init(&buf_hashtab);
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1890
1891 /*
1892 * If file name already exists in the list, update the entry.
1893 */
1894#ifdef UNIX
1895 /* On Unix we can use inode numbers when the file exists. Works better
1896 * for hard links. */
1897 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1898 st.st_dev = (dev_T)-1;
1899#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001900 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901#ifdef UNIX
1902 buflist_findname_stat(ffname, &st)
1903#else
1904 buflist_findname(ffname)
1905#endif
1906 ) != NULL)
1907 {
1908 vim_free(ffname);
1909 if (lnum != 0)
1910 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001911
1912 if ((flags & BLN_NOOPT) == 0)
1913 /* copy the options now, if 'cpo' doesn't have 's' and not done
1914 * already */
1915 buf_copy_options(buf, 0);
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1918 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001919 bufref_T bufref;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 buf->b_p_bl = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001922 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001924 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001925 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001926 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001927 return NULL;
1928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 }
1930 return buf;
1931 }
1932
1933 /*
1934 * If the current buffer has no name and no contents, use the current
1935 * buffer. Otherwise: Need to allocate a new buffer structure.
1936 *
1937 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001938 * (A spell file buffer is allocated in spell.c, but that's not a normal
1939 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 */
1941 buf = NULL;
Bram Moolenaar46a53df2018-04-24 21:58:51 +02001942 if ((flags & BLN_CURBUF) && curbuf_reusable())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 {
1944 buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 /* It's like this buffer is deleted. Watch out for autocommands that
1946 * change curbuf! If that happens, allocate a new buffer anyway. */
1947 if (curbuf->b_p_bl)
1948 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1949 if (buf == curbuf)
1950 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01001951#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001952 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (buf == curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 /* Make sure 'bufhidden' and 'buftype' are empty */
1958 clear_string_option(&buf->b_p_bh);
1959 clear_string_option(&buf->b_p_bt);
1960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962 if (buf != curbuf || curbuf == NULL)
1963 {
1964 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1965 if (buf == NULL)
1966 {
1967 vim_free(ffname);
1968 return NULL;
1969 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001970#ifdef FEAT_EVAL
1971 /* init b: variables */
1972 buf->b_vars = dict_alloc();
1973 if (buf->b_vars == NULL)
1974 {
1975 vim_free(ffname);
1976 vim_free(buf);
1977 return NULL;
1978 }
1979 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1980#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01001981 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983
1984 if (ffname != NULL)
1985 {
1986 buf->b_ffname = ffname;
1987 buf->b_sfname = vim_strsave(sfname);
1988 }
1989
1990 clear_wininfo(buf);
1991 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1992
1993 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1994 || buf->b_wininfo == NULL)
1995 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01001996 VIM_CLEAR(buf->b_ffname);
1997 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 if (buf != curbuf)
1999 free_buffer(buf);
2000 return NULL;
2001 }
2002
2003 if (buf == curbuf)
2004 {
2005 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002006 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 if (buf != curbuf) /* autocommands deleted the buffer! */
2008 return NULL;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002009#if defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002010 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 return NULL;
2012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002014
2015 /* Init the options. */
2016 buf->b_p_initialized = FALSE;
2017 buf_copy_options(buf, BCO_ENTER);
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019#ifdef FEAT_KEYMAP
2020 /* need to reload lmaps and set b:keymap_name */
2021 curbuf->b_kmap_state |= KEYMAP_INIT;
2022#endif
2023 }
2024 else
2025 {
2026 /*
2027 * put new buffer at the end of the buffer list
2028 */
2029 buf->b_next = NULL;
2030 if (firstbuf == NULL) /* buffer list is empty */
2031 {
2032 buf->b_prev = NULL;
2033 firstbuf = buf;
2034 }
2035 else /* append new buffer at end of list */
2036 {
2037 lastbuf->b_next = buf;
2038 buf->b_prev = lastbuf;
2039 }
2040 lastbuf = buf;
2041
2042 buf->b_fnum = top_file_num++;
2043 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2044 {
2045 EMSG(_("W14: Warning: List of file names overflow"));
2046 if (emsg_silent == 0)
2047 {
2048 out_flush();
2049 ui_delay(3000L, TRUE); /* make sure it is noticed */
2050 }
2051 top_file_num = 1;
2052 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002053 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054
2055 /*
2056 * Always copy the options from the current buffer.
2057 */
2058 buf_copy_options(buf, BCO_ALWAYS);
2059 }
2060
2061 buf->b_wininfo->wi_fpos.lnum = lnum;
2062 buf->b_wininfo->wi_win = curwin;
2063
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002064#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002065 hash_init(&buf->b_s.b_keywtab);
2066 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067#endif
2068
2069 buf->b_fname = buf->b_sfname;
2070#ifdef UNIX
2071 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002072 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 else
2074 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002075 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 buf->b_dev = st.st_dev;
2077 buf->b_ino = st.st_ino;
2078 }
2079#endif
2080 buf->b_u_synced = TRUE;
2081 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002082 if (flags & BLN_DUMMY)
2083 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 buf_clear_file(buf);
2085 clrallmarks(buf); /* clear marks */
2086 fmarks_check_names(buf); /* check file marks for this file */
2087 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 if (!(flags & BLN_DUMMY))
2089 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002090 bufref_T bufref;
2091
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002092 /* Tricky: these autocommands may change the buffer list. They could
2093 * also split the window with re-using the one empty buffer. This may
2094 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002095 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002096 if (apply_autocmds(EVENT_BUFNEW, 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;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002100 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002101 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002102 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002103 return NULL;
2104 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002105#ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002106 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002108#endif
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01002109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110
2111 return buf;
2112}
2113
2114/*
2115 * Free the memory for the options of a buffer.
2116 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2117 * 'fileencoding'.
2118 */
2119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002120free_buf_options(
2121 buf_T *buf,
2122 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123{
2124 if (free_p_ff)
2125 {
2126#ifdef FEAT_MBYTE
2127 clear_string_option(&buf->b_p_fenc);
2128#endif
2129 clear_string_option(&buf->b_p_ff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 clear_string_option(&buf->b_p_bh);
2131 clear_string_option(&buf->b_p_bt);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 }
2133#ifdef FEAT_FIND_ID
2134 clear_string_option(&buf->b_p_def);
2135 clear_string_option(&buf->b_p_inc);
2136# ifdef FEAT_EVAL
2137 clear_string_option(&buf->b_p_inex);
2138# endif
2139#endif
2140#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2141 clear_string_option(&buf->b_p_inde);
2142 clear_string_option(&buf->b_p_indk);
2143#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002144#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2145 clear_string_option(&buf->b_p_bexpr);
2146#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002147#if defined(FEAT_CRYPT)
2148 clear_string_option(&buf->b_p_cm);
2149#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002150 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002151#if defined(FEAT_EVAL)
2152 clear_string_option(&buf->b_p_fex);
2153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154#ifdef FEAT_CRYPT
2155 clear_string_option(&buf->b_p_key);
2156#endif
2157 clear_string_option(&buf->b_p_kp);
2158 clear_string_option(&buf->b_p_mps);
2159 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002160 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 clear_string_option(&buf->b_p_isk);
Bram Moolenaar04958cb2018-06-23 19:23:02 +02002162#ifdef FEAT_VARTABS
2163 clear_string_option(&buf->b_p_vsts);
2164 if (buf->b_p_vsts_nopaste)
2165 vim_free(buf->b_p_vsts_nopaste);
2166 buf->b_p_vsts_nopaste = NULL;
2167 if (buf->b_p_vsts_array)
2168 vim_free(buf->b_p_vsts_array);
2169 buf->b_p_vsts_array = NULL;
2170 clear_string_option(&buf->b_p_vts);
2171 if (buf->b_p_vts_array)
2172 vim_free(buf->b_p_vts_array);
2173 buf->b_p_vts_array = NULL;
2174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175#ifdef FEAT_KEYMAP
2176 clear_string_option(&buf->b_p_keymap);
Bram Moolenaar50138322018-01-28 17:05:16 +01002177 keymap_clear(&buf->b_kmap_ga);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 ga_clear(&buf->b_kmap_ga);
2179#endif
2180#ifdef FEAT_COMMENTS
2181 clear_string_option(&buf->b_p_com);
2182#endif
2183#ifdef FEAT_FOLDING
2184 clear_string_option(&buf->b_p_cms);
2185#endif
2186 clear_string_option(&buf->b_p_nf);
2187#ifdef FEAT_SYN_HL
2188 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002189 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002190#endif
2191#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002192 clear_string_option(&buf->b_s.b_p_spc);
2193 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002194 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002195 buf->b_s.b_cap_prog = NULL;
2196 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197#endif
2198#ifdef FEAT_SEARCHPATH
2199 clear_string_option(&buf->b_p_sua);
2200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 clear_string_option(&buf->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202#ifdef FEAT_CINDENT
2203 clear_string_option(&buf->b_p_cink);
2204 clear_string_option(&buf->b_p_cino);
2205#endif
2206#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2207 clear_string_option(&buf->b_p_cinw);
2208#endif
2209#ifdef FEAT_INS_EXPAND
2210 clear_string_option(&buf->b_p_cpt);
2211#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002212#ifdef FEAT_COMPL_FUNC
2213 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002214 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002215#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216#ifdef FEAT_QUICKFIX
2217 clear_string_option(&buf->b_p_gp);
2218 clear_string_option(&buf->b_p_mp);
2219 clear_string_option(&buf->b_p_efm);
2220#endif
2221 clear_string_option(&buf->b_p_ep);
2222 clear_string_option(&buf->b_p_path);
2223 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002224 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225#ifdef FEAT_INS_EXPAND
2226 clear_string_option(&buf->b_p_dict);
2227 clear_string_option(&buf->b_p_tsr);
2228#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002229#ifdef FEAT_TEXTOBJ
2230 clear_string_option(&buf->b_p_qe);
2231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002233 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002234#ifdef FEAT_LISP
2235 clear_string_option(&buf->b_p_lw);
2236#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002237 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002238#ifdef FEAT_MBYTE
2239 clear_string_option(&buf->b_p_menc);
2240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241}
2242
2243/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002244 * Get alternate file "n".
2245 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2246 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 * if (options & GETF_SETMARK) call setpcmark()
2248 * if (options & GETF_ALT) we are jumping to an alternate file.
2249 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2250 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002251 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 */
2253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002254buflist_getfile(
2255 int n,
2256 linenr_T lnum,
2257 int options,
2258 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259{
2260 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 win_T *wp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 pos_T *fpos;
2263 colnr_T col;
2264
2265 buf = buflist_findnr(n);
2266 if (buf == NULL)
2267 {
2268 if ((options & GETF_ALT) && n == 0)
2269 EMSG(_(e_noalt));
2270 else
2271 EMSGN(_("E92: Buffer %ld not found"), n);
2272 return FAIL;
2273 }
2274
2275 /* if alternate file is the current buffer, nothing to do */
2276 if (buf == curbuf)
2277 return OK;
2278
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002279 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002280 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002281 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002283 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002284 if (curbuf_locked())
2285 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286
2287 /* altfpos may be changed by getfile(), get it now */
2288 if (lnum == 0)
2289 {
2290 fpos = buflist_findfpos(buf);
2291 lnum = fpos->lnum;
2292 col = fpos->col;
2293 }
2294 else
2295 col = 0;
2296
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 if (options & GETF_SWITCH)
2298 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002299 /* If 'switchbuf' contains "useopen": jump to first window containing
2300 * "buf" if one exists */
2301 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002303
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002304 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002305 * page containing "buf" if one exists */
2306 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002307 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002308
2309 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2310 * current buffer isn't empty: open new tab or window */
2311 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002312 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002314 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002315 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002316 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2317 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002319 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322
2323 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002324 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2325 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 {
2327 --RedrawingDisabled;
2328
2329 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2330 if (!p_sol && col != 0)
2331 {
2332 curwin->w_cursor.col = col;
2333 check_cursor_col();
2334#ifdef FEAT_VIRTUALEDIT
2335 curwin->w_cursor.coladd = 0;
2336#endif
2337 curwin->w_set_curswant = TRUE;
2338 }
2339 return OK;
2340 }
2341 --RedrawingDisabled;
2342 return FAIL;
2343}
2344
2345/*
2346 * go to the last know line number for the current buffer
2347 */
2348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002349buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350{
2351 pos_T *fpos;
2352
2353 fpos = buflist_findfpos(curbuf);
2354
2355 curwin->w_cursor.lnum = fpos->lnum;
2356 check_cursor_lnum();
2357
2358 if (p_sol)
2359 curwin->w_cursor.col = 0;
2360 else
2361 {
2362 curwin->w_cursor.col = fpos->col;
2363 check_cursor_col();
2364#ifdef FEAT_VIRTUALEDIT
2365 curwin->w_cursor.coladd = 0;
2366#endif
2367 curwin->w_set_curswant = TRUE;
2368 }
2369}
2370
Bram Moolenaar81695252004-12-29 20:58:21 +00002371#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2372/*
2373 * Find file in buffer list by name (it has to be for the current window).
2374 * Returns NULL if not found.
2375 */
2376 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002377buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002378{
2379 char_u *ffname;
2380 buf_T *buf = NULL;
2381
2382 /* First make the name into a full path name */
2383 ffname = FullName_save(fname,
2384#ifdef UNIX
2385 TRUE /* force expansion, get rid of symbolic links */
2386#else
2387 FALSE
2388#endif
2389 );
2390 if (ffname != NULL)
2391 {
2392 buf = buflist_findname(ffname);
2393 vim_free(ffname);
2394 }
2395 return buf;
2396}
2397#endif
2398
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399/*
2400 * Find file in buffer list by name (it has to be for the current window).
2401 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002402 * Skips dummy buffers.
2403 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 */
2405 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002406buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407{
2408#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002409 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410
2411 if (mch_stat((char *)ffname, &st) < 0)
2412 st.st_dev = (dev_T)-1;
2413 return buflist_findname_stat(ffname, &st);
2414}
2415
2416/*
2417 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2418 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002419 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 */
2421 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002422buflist_findname_stat(
2423 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002424 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425{
2426#endif
2427 buf_T *buf;
2428
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002429 /* Start at the last buffer, expect to find a match sooner. */
2430 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002431 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432#ifdef UNIX
2433 , stp
2434#endif
2435 ))
2436 return buf;
2437 return NULL;
2438}
2439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440/*
2441 * Find file in buffer list by a regexp pattern.
2442 * Return fnum of the found buffer.
2443 * Return < 0 for error.
2444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002446buflist_findpat(
2447 char_u *pattern,
2448 char_u *pattern_end, /* pointer to first char after pattern */
2449 int unlisted, /* find unlisted buffers */
2450 int diffmode UNUSED, /* find diff-mode buffers only */
2451 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002452{
2453 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 int match = -1;
2455 int find_listed;
2456 char_u *pat;
2457 char_u *patend;
2458 int attempt;
2459 char_u *p;
2460 int toggledollar;
2461
2462 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2463 {
2464 if (*pattern == '%')
2465 match = curbuf->b_fnum;
2466 else
2467 match = curwin->w_alt_fnum;
2468#ifdef FEAT_DIFF
2469 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2470 match = -1;
2471#endif
2472 }
2473
2474 /*
2475 * Try four ways of matching a listed buffer:
2476 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002477 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 * attempt == 2: with '$' at end (only match at end)
2479 * attempt == 3: with '^' at start and '$' at end (only full match)
2480 * Repeat this for finding an unlisted buffer if there was no matching
2481 * listed buffer.
2482 */
2483 else
2484 {
2485 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2486 if (pat == NULL)
2487 return -1;
2488 patend = pat + STRLEN(pat) - 1;
2489 toggledollar = (patend > pat && *patend == '$');
2490
2491 /* First try finding a listed buffer. If not found and "unlisted"
2492 * is TRUE, try finding an unlisted buffer. */
2493 find_listed = TRUE;
2494 for (;;)
2495 {
2496 for (attempt = 0; attempt <= 3; ++attempt)
2497 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002498 regmatch_T regmatch;
2499
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 /* may add '^' and '$' */
2501 if (toggledollar)
2502 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2503 p = pat;
2504 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2505 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002506 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2507 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
2509 vim_free(pat);
2510 return -1;
2511 }
2512
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002513 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (buf->b_p_bl == find_listed
2515#ifdef FEAT_DIFF
2516 && (!diffmode || diff_mode_buf(buf))
2517#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002518 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002520 if (curtab_only)
2521 {
2522 /* Ignore the match if the buffer is not open in
2523 * the current tab. */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002524 win_T *wp;
2525
Bram Moolenaar29323592016-07-24 22:04:11 +02002526 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002527 if (wp->w_buffer == buf)
2528 break;
2529 if (wp == NULL)
2530 continue;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (match >= 0) /* already found a match */
2533 {
2534 match = -2;
2535 break;
2536 }
2537 match = buf->b_fnum; /* remember first match */
2538 }
2539
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002540 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 if (match >= 0) /* found one match */
2542 break;
2543 }
2544
2545 /* Only search for unlisted buffers if there was no match with
2546 * a listed buffer. */
2547 if (!unlisted || !find_listed || match != -1)
2548 break;
2549 find_listed = FALSE;
2550 }
2551
2552 vim_free(pat);
2553 }
2554
2555 if (match == -2)
2556 EMSG2(_("E93: More than one match for %s"), pattern);
2557 else if (match < 0)
2558 EMSG2(_("E94: No matching buffer for %s"), pattern);
2559 return match;
2560}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2563
2564/*
2565 * Find all buffer names that match.
2566 * For command line expansion of ":buf" and ":sbuf".
2567 * Return OK if matches found, FAIL otherwise.
2568 */
2569 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002570ExpandBufnames(
2571 char_u *pat,
2572 int *num_file,
2573 char_u ***file,
2574 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575{
2576 int count = 0;
2577 buf_T *buf;
2578 int round;
2579 char_u *p;
2580 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002581 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 *num_file = 0; /* return values in case of FAIL */
2584 *file = NULL;
2585
Bram Moolenaar05159a02005-02-26 23:04:13 +00002586 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2587 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002589 patc = alloc((unsigned)STRLEN(pat) + 11);
2590 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002592 STRCPY(patc, "\\(^\\|[\\/]\\)");
2593 STRCPY(patc + 11, pat + 1);
2594 }
2595 else
2596 patc = pat;
2597
2598 /*
2599 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002600 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002601 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002602 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002603 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002604 regmatch_T regmatch;
2605
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002606 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002607 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002608 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2609 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002610 {
2611 if (patc != pat)
2612 vim_free(patc);
2613 return FAIL;
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615
2616 /*
2617 * round == 1: Count the matches.
2618 * round == 2: Build the array to keep the matches.
2619 */
2620 for (round = 1; round <= 2; ++round)
2621 {
2622 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002623 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 {
2625 if (!buf->b_p_bl) /* skip unlisted buffers */
2626 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002627 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002628 if (p != NULL)
2629 {
2630 if (round == 1)
2631 ++count;
2632 else
2633 {
2634 if (options & WILD_HOME_REPLACE)
2635 p = home_replace_save(buf, p);
2636 else
2637 p = vim_strsave(p);
2638 (*file)[count++] = p;
2639 }
2640 }
2641 }
2642 if (count == 0) /* no match found, break here */
2643 break;
2644 if (round == 1)
2645 {
2646 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2647 if (*file == NULL)
2648 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002649 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002650 if (patc != pat)
2651 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 return FAIL;
2653 }
2654 }
2655 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002656 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657 if (count) /* match(es) found, break here */
2658 break;
2659 }
2660
Bram Moolenaar05159a02005-02-26 23:04:13 +00002661 if (patc != pat)
2662 vim_free(patc);
2663
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 *num_file = count;
2665 return (count == 0 ? FAIL : OK);
2666}
2667
2668#endif /* FEAT_CMDL_COMPL */
2669
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670/*
2671 * Check for a match on the file name for buffer "buf" with regprog "prog".
2672 */
2673 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002674buflist_match(
2675 regmatch_T *rmp,
2676 buf_T *buf,
2677 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678{
2679 char_u *match;
2680
2681 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002682 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002684 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685
2686 return match;
2687}
2688
2689/*
2690 * Try matching the regexp in "prog" with file name "name".
2691 * Return "name" when there is a match, NULL when not.
2692 */
2693 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002694fname_match(
2695 regmatch_T *rmp,
2696 char_u *name,
2697 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698{
2699 char_u *match = NULL;
2700 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701
2702 if (name != NULL)
2703 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002704 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002705 rmp->rm_ic = p_fic || ignore_case;
2706 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 match = name;
2708 else
2709 {
2710 /* Replace $(HOME) with '~' and try matching again. */
2711 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002712 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713 match = name;
2714 vim_free(p);
2715 }
2716 }
2717
2718 return match;
2719}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002722 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 */
2724 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002725buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002727 char_u key[VIM_SIZEOF_INT * 2 + 1];
2728 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729
2730 if (nr == 0)
2731 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002732 sprintf((char *)key, "%x", nr);
2733 hi = hash_find(&buf_hashtab, key);
2734
2735 if (!HASHITEM_EMPTY(hi))
2736 return (buf_T *)(hi->hi_key
2737 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 return NULL;
2739}
2740
2741/*
2742 * Get name of file 'n' in the buffer list.
2743 * When the file has no name an empty string is returned.
2744 * home_replace() is used to shorten the file name (used for marks).
2745 * Returns a pointer to allocated memory, of NULL when failed.
2746 */
2747 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002748buflist_nr2name(
2749 int n,
2750 int fullname,
2751 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752{
2753 buf_T *buf;
2754
2755 buf = buflist_findnr(n);
2756 if (buf == NULL)
2757 return NULL;
2758 return home_replace_save(helptail ? buf : NULL,
2759 fullname ? buf->b_ffname : buf->b_fname);
2760}
2761
2762/*
2763 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2764 * When "copy_options" is TRUE save the local window option values.
2765 * When "lnum" is 0 only do the options.
2766 */
2767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002768buflist_setfpos(
2769 buf_T *buf,
2770 win_T *win,
2771 linenr_T lnum,
2772 colnr_T col,
2773 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774{
2775 wininfo_T *wip;
2776
2777 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2778 if (wip->wi_win == win)
2779 break;
2780 if (wip == NULL)
2781 {
2782 /* allocate a new entry */
2783 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2784 if (wip == NULL)
2785 return;
2786 wip->wi_win = win;
2787 if (lnum == 0) /* set lnum even when it's 0 */
2788 lnum = 1;
2789 }
2790 else
2791 {
2792 /* remove the entry from the list */
2793 if (wip->wi_prev)
2794 wip->wi_prev->wi_next = wip->wi_next;
2795 else
2796 buf->b_wininfo = wip->wi_next;
2797 if (wip->wi_next)
2798 wip->wi_next->wi_prev = wip->wi_prev;
2799 if (copy_options && wip->wi_optset)
2800 {
2801 clear_winopt(&wip->wi_opt);
2802#ifdef FEAT_FOLDING
2803 deleteFoldRecurse(&wip->wi_folds);
2804#endif
2805 }
2806 }
2807 if (lnum != 0)
2808 {
2809 wip->wi_fpos.lnum = lnum;
2810 wip->wi_fpos.col = col;
2811 }
2812 if (copy_options)
2813 {
2814 /* Save the window-specific option values. */
2815 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2816#ifdef FEAT_FOLDING
2817 wip->wi_fold_manual = win->w_fold_manual;
2818 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2819#endif
2820 wip->wi_optset = TRUE;
2821 }
2822
2823 /* insert the entry in front of the list */
2824 wip->wi_next = buf->b_wininfo;
2825 buf->b_wininfo = wip;
2826 wip->wi_prev = NULL;
2827 if (wip->wi_next)
2828 wip->wi_next->wi_prev = wip;
2829
2830 return;
2831}
2832
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002833#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002834static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002835
2836/*
2837 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2838 * page. That's because a diff is local to a tab page.
2839 */
2840 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002842{
2843 win_T *wp;
2844
2845 if (wip->wi_opt.wo_diff)
2846 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002847 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002848 /* return FALSE when it's a window in the current tab page, thus
2849 * the buffer was in diff mode here */
2850 if (wip->wi_win == wp)
2851 return FALSE;
2852 return TRUE;
2853 }
2854 return FALSE;
2855}
2856#endif
2857
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858/*
2859 * Find info for the current window in buffer "buf".
2860 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002861 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2862 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 * Returns NULL when there isn't any info.
2864 */
2865 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002866find_wininfo(
2867 buf_T *buf,
2868 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869{
2870 wininfo_T *wip;
2871
2872 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002873 if (wip->wi_win == curwin
2874#ifdef FEAT_DIFF
2875 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2876#endif
2877 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002879
2880 /* If no wininfo for curwin, use the first in the list (that doesn't have
2881 * 'diff' set and is in another tab page). */
2882 if (wip == NULL)
2883 {
2884#ifdef FEAT_DIFF
2885 if (skip_diff_buffer)
2886 {
2887 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2888 if (!wininfo_other_tab_diff(wip))
2889 break;
2890 }
2891 else
2892#endif
2893 wip = buf->b_wininfo;
2894 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 return wip;
2896}
2897
2898/*
2899 * Reset the local window options to the values last used in this window.
2900 * If the buffer wasn't used in this window before, use the values from
2901 * the most recently used window. If the values were never set, use the
2902 * global values for the window.
2903 */
2904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002905get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
2907 wininfo_T *wip;
2908
2909 clear_winopt(&curwin->w_onebuf_opt);
2910#ifdef FEAT_FOLDING
2911 clearFolding(curwin);
2912#endif
2913
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002914 wip = find_wininfo(buf, TRUE);
Bram Moolenaar25782a72018-05-13 18:05:33 +02002915 if (wip != NULL && wip->wi_win != NULL
2916 && wip->wi_win != curwin && wip->wi_win->w_buffer == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 {
Bram Moolenaar25782a72018-05-13 18:05:33 +02002918 /* The buffer is currently displayed in the window: use the actual
2919 * option values instead of the saved (possibly outdated) values. */
2920 win_T *wp = wip->wi_win;
2921
2922 copy_winopt(&wp->w_onebuf_opt, &curwin->w_onebuf_opt);
2923#ifdef FEAT_FOLDING
2924 curwin->w_fold_manual = wp->w_fold_manual;
2925 curwin->w_foldinvalid = TRUE;
2926 cloneFoldGrowArray(&wp->w_folds, &curwin->w_folds);
2927#endif
2928 }
2929 else if (wip != NULL && wip->wi_optset)
2930 {
2931 /* the buffer was displayed in the current window earlier */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2933#ifdef FEAT_FOLDING
2934 curwin->w_fold_manual = wip->wi_fold_manual;
2935 curwin->w_foldinvalid = TRUE;
2936 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2937#endif
2938 }
2939 else
2940 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2941
2942#ifdef FEAT_FOLDING
2943 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2944 if (p_fdls >= 0)
2945 curwin->w_p_fdl = p_fdls;
2946#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002947#ifdef FEAT_SYN_HL
2948 check_colorcolumn(curwin);
2949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950}
2951
2952/*
2953 * Find the position (lnum and col) for the buffer 'buf' for the current
2954 * window.
2955 * Returns a pointer to no_position if no position is found.
2956 */
2957 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002958buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002961 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002963 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (wip != NULL)
2965 return &(wip->wi_fpos);
2966 else
2967 return &no_position;
2968}
2969
2970/*
2971 * Find the lnum for the buffer 'buf' for the current window.
2972 */
2973 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002974buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975{
2976 return buflist_findfpos(buf)->lnum;
2977}
2978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002980 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002983buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984{
2985 buf_T *buf;
2986 int len;
2987 int i;
Bram Moolenaar304b64c2017-08-13 20:43:48 +02002988 int ro_char;
2989 int changed_char;
Bram Moolenaar0751f512018-03-29 16:37:16 +02002990#ifdef FEAT_TERMINAL
2991 int job_running;
2992 int job_none_open;
2993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
2995 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2996 {
Bram Moolenaar0751f512018-03-29 16:37:16 +02002997#ifdef FEAT_TERMINAL
2998 job_running = term_job_running(buf->b_term);
2999 job_none_open = job_running && term_none_open(buf->b_term);
3000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003002 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3003 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3004 || (vim_strchr(eap->arg, '+')
3005 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3006 || (vim_strchr(eap->arg, 'a')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003007 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
Bram Moolenaard51cb702015-07-21 15:03:06 +02003008 || (vim_strchr(eap->arg, 'h')
Bram Moolenaar0751f512018-03-29 16:37:16 +02003009 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3010#ifdef FEAT_TERMINAL
3011 || (vim_strchr(eap->arg, 'R')
3012 && (!job_running || (job_running && job_none_open)))
3013 || (vim_strchr(eap->arg, '?')
3014 && (!job_running || (job_running && !job_none_open)))
3015 || (vim_strchr(eap->arg, 'F')
3016 && (job_running || buf->b_term == NULL))
3017#endif
Bram Moolenaard51cb702015-07-21 15:03:06 +02003018 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3019 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3020 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3021 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3022 || (vim_strchr(eap->arg, '#')
3023 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003026 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 else
3028 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003029 if (message_filtered(NameBuff))
3030 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003032 changed_char = (buf->b_flags & BF_READERR) ? 'x'
3033 : (bufIsChanged(buf) ? '+' : ' ');
3034#ifdef FEAT_TERMINAL
3035 if (term_job_running(buf->b_term))
3036 {
Bram Moolenaar4033c552017-09-16 20:54:51 +02003037 if (term_none_open(buf->b_term))
3038 ro_char = '?';
3039 else
3040 ro_char = 'R';
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003041 changed_char = ' '; /* bufIsChanged() returns TRUE to avoid
3042 * closing, but it's not actually changed. */
3043 }
3044 else if (buf->b_term != NULL)
3045 ro_char = 'F';
3046 else
3047#endif
3048 ro_char = !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' ');
3049
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003050 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003051 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 buf->b_fnum,
3053 buf->b_p_bl ? ' ' : 'u',
3054 buf == curbuf ? '%' :
3055 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3056 buf->b_ml.ml_mfp == NULL ? ' ' :
3057 (buf->b_nwindows == 0 ? 'h' : 'a'),
Bram Moolenaar304b64c2017-08-13 20:43:48 +02003058 ro_char,
3059 changed_char,
Bram Moolenaar51485f02005-06-04 21:55:20 +00003060 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003061 if (len > IOSIZE - 20)
3062 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063
3064 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 i = 40 - vim_strsize(IObuff);
3066 do
3067 {
3068 IObuff[len++] = ' ';
3069 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003070 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3071 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003072 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 msg_outtrans(IObuff);
3074 out_flush(); /* output one line at a time */
3075 ui_breakcheck();
3076 }
3077}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
3079/*
3080 * Get file name and line number for file 'fnum'.
3081 * Used by DoOneCmd() for translating '%' and '#'.
3082 * Used by insert_reg() and cmdline_paste() for '#' register.
3083 * Return FAIL if not found, OK for success.
3084 */
3085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086buflist_name_nr(
3087 int fnum,
3088 char_u **fname,
3089 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090{
3091 buf_T *buf;
3092
3093 buf = buflist_findnr(fnum);
3094 if (buf == NULL || buf->b_fname == NULL)
3095 return FAIL;
3096
3097 *fname = buf->b_fname;
3098 *lnum = buflist_findlnum(buf);
3099
3100 return OK;
3101}
3102
3103/*
3104 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3105 * The file name with the full path is also remembered, for when :cd is used.
3106 * Returns FAIL for failure (file name already in use by other buffer)
3107 * OK otherwise.
3108 */
3109 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003110setfname(
3111 buf_T *buf,
3112 char_u *ffname,
3113 char_u *sfname,
3114 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115{
Bram Moolenaar81695252004-12-29 20:58:21 +00003116 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003118 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119#endif
3120
3121 if (ffname == NULL || *ffname == NUL)
3122 {
3123 /* Removing the name. */
Bram Moolenaard23a8232018-02-10 18:45:26 +01003124 VIM_CLEAR(buf->b_ffname);
3125 VIM_CLEAR(buf->b_sfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126#ifdef UNIX
3127 st.st_dev = (dev_T)-1;
3128#endif
3129 }
3130 else
3131 {
3132 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3133 if (ffname == NULL) /* out of memory */
3134 return FAIL;
3135
3136 /*
3137 * if the file name is already used in another buffer:
3138 * - if the buffer is loaded, fail
3139 * - if the buffer is not loaded, delete it from the list
3140 */
3141#ifdef UNIX
3142 if (mch_stat((char *)ffname, &st) < 0)
3143 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003144#endif
3145 if (!(buf->b_flags & BF_DUMMY))
3146#ifdef UNIX
3147 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003149 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150#endif
3151 if (obuf != NULL && obuf != buf)
3152 {
3153 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3154 {
3155 if (message)
3156 EMSG(_("E95: Buffer with this name already exists"));
3157 vim_free(ffname);
3158 return FAIL;
3159 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003160 /* delete from the list */
3161 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 }
3163 sfname = vim_strsave(sfname);
3164 if (ffname == NULL || sfname == NULL)
3165 {
3166 vim_free(sfname);
3167 vim_free(ffname);
3168 return FAIL;
3169 }
3170#ifdef USE_FNAME_CASE
3171# ifdef USE_LONG_FNAME
3172 if (USE_LONG_FNAME)
3173# endif
3174 fname_case(sfname, 0); /* set correct case for short file name */
3175#endif
3176 vim_free(buf->b_ffname);
3177 vim_free(buf->b_sfname);
3178 buf->b_ffname = ffname;
3179 buf->b_sfname = sfname;
3180 }
3181 buf->b_fname = buf->b_sfname;
3182#ifdef UNIX
3183 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003184 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 else
3186 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003187 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 buf->b_dev = st.st_dev;
3189 buf->b_ino = st.st_ino;
3190 }
3191#endif
3192
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194
3195 buf_name_changed(buf);
3196 return OK;
3197}
3198
3199/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003200 * Crude way of changing the name of a buffer. Use with care!
3201 * The name should be relative to the current directory.
3202 */
3203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003205{
3206 buf_T *buf;
3207
3208 buf = buflist_findnr(fnum);
3209 if (buf != NULL)
3210 {
3211 vim_free(buf->b_sfname);
3212 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003213 buf->b_ffname = vim_strsave(name);
3214 buf->b_sfname = NULL;
3215 /* Allocate ffname and expand into full path. Also resolves .lnk
3216 * files on Win32. */
3217 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003218 buf->b_fname = buf->b_sfname;
3219 }
3220}
3221
3222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 * Take care of what needs to be done when the name of buffer "buf" has
3224 * changed.
3225 */
3226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003227buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228{
3229 /*
3230 * If the file name changed, also change the name of the swapfile
3231 */
3232 if (buf->b_ml.ml_mfp != NULL)
3233 ml_setname(buf);
3234
3235 if (curwin->w_buffer == buf)
3236 check_arg_idx(curwin); /* check file name for arg list */
3237#ifdef FEAT_TITLE
3238 maketitle(); /* set window title */
3239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 status_redraw_all(); /* status lines need to be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 fmarks_check_names(buf); /* check named file marks */
3242 ml_timestamp(buf); /* reset timestamp */
3243}
3244
3245/*
3246 * set alternate file name for current window
3247 *
3248 * Used by do_one_cmd(), do_write() and do_ecmd().
3249 * Return the buffer.
3250 */
3251 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252setaltfname(
3253 char_u *ffname,
3254 char_u *sfname,
3255 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256{
3257 buf_T *buf;
3258
3259 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3260 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003261 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 curwin->w_alt_fnum = buf->b_fnum;
3263 return buf;
3264}
3265
3266/*
3267 * Get alternate file name for current window.
3268 * Return NULL if there isn't any, and give error message if requested.
3269 */
3270 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003271getaltfname(
3272 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
3274 char_u *fname;
3275 linenr_T dummy;
3276
3277 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3278 {
3279 if (errmsg)
3280 EMSG(_(e_noalt));
3281 return NULL;
3282 }
3283 return fname;
3284}
3285
3286/*
3287 * Add a file name to the buflist and return its number.
3288 * Uses same flags as buflist_new(), except BLN_DUMMY.
3289 *
3290 * used by qf_init(), main() and doarglist()
3291 */
3292 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003293buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294{
3295 buf_T *buf;
3296
3297 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3298 if (buf != NULL)
3299 return buf->b_fnum;
3300 return 0;
3301}
3302
3303#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3304/*
3305 * Adjust slashes in file names. Called after 'shellslash' was set.
3306 */
3307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003308buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309{
3310 buf_T *bp;
3311
Bram Moolenaar29323592016-07-24 22:04:11 +02003312 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 {
3314 if (bp->b_ffname != NULL)
3315 slash_adjust(bp->b_ffname);
3316 if (bp->b_sfname != NULL)
3317 slash_adjust(bp->b_sfname);
3318 }
3319}
3320#endif
3321
3322/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003323 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 * Also save the local window option values.
3325 */
3326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003329 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330}
3331
3332/*
3333 * Return TRUE if 'ffname' is not the same file as current file.
3334 * Fname must have a full path (expanded by mch_FullName()).
3335 */
3336 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003337otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338{
3339 return otherfile_buf(curbuf, ffname
3340#ifdef UNIX
3341 , NULL
3342#endif
3343 );
3344}
3345
3346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003347otherfile_buf(
3348 buf_T *buf,
3349 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003351 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354{
3355 /* no name is different */
3356 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3357 return TRUE;
3358 if (fnamecmp(ffname, buf->b_ffname) == 0)
3359 return FALSE;
3360#ifdef UNIX
3361 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003362 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaar8767f522016-07-01 17:17:39 +02003364 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (stp == NULL)
3366 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003367 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 st.st_dev = (dev_T)-1;
3369 stp = &st;
3370 }
3371 /* Use dev/ino to check if the files are the same, even when the names
3372 * are different (possible with links). Still need to compare the
3373 * name above, for when the file doesn't exist yet.
3374 * Problem: The dev/ino changes when a file is deleted (and created
3375 * again) and remains the same when renamed/moved. We don't want to
3376 * mch_stat() each buffer each time, that would be too slow. Get the
3377 * dev/ino again when they appear to match, but not when they appear
3378 * to be different: Could skip a buffer when it's actually the same
3379 * file. */
3380 if (buf_same_ino(buf, stp))
3381 {
3382 buf_setino(buf);
3383 if (buf_same_ino(buf, stp))
3384 return FALSE;
3385 }
3386 }
3387#endif
3388 return TRUE;
3389}
3390
3391#if defined(UNIX) || defined(PROTO)
3392/*
3393 * Set inode and device number for a buffer.
3394 * Must always be called when b_fname is changed!.
3395 */
3396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003397buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003399 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400
3401 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3402 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003403 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404 buf->b_dev = st.st_dev;
3405 buf->b_ino = st.st_ino;
3406 }
3407 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003408 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409}
3410
3411/*
3412 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3413 */
3414 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003415buf_same_ino(
3416 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003417 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003419 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 && stp->st_dev == buf->b_dev
3421 && stp->st_ino == buf->b_ino);
3422}
3423#endif
3424
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003425/*
3426 * Print info about the current buffer.
3427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429fileinfo(
3430 int fullname, /* when non-zero print full path */
3431 int shorthelp,
3432 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 char_u *name;
3435 int n;
3436 char_u *p;
3437 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003438 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 buffer = alloc(IOSIZE);
3441 if (buffer == NULL)
3442 return;
3443
3444 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3445 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003446 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 p = buffer + STRLEN(buffer);
3448 }
3449 else
3450 p = buffer;
3451
3452 *p++ = '"';
3453 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003454 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 else
3456 {
3457 if (!fullname && curbuf->b_fname != NULL)
3458 name = curbuf->b_fname;
3459 else
3460 name = curbuf->b_ffname;
3461 home_replace(shorthelp ? curbuf : NULL, name, p,
3462 (int)(IOSIZE - (p - buffer)), TRUE);
3463 }
3464
Bram Moolenaara800b422010-06-27 01:15:55 +02003465 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 curbufIsChanged() ? (shortmess(SHM_MOD)
3467 ? " [+]" : _(" [Modified]")) : " ",
3468 (curbuf->b_flags & BF_NOTEDITED)
3469#ifdef FEAT_QUICKFIX
3470 && !bt_dontwrite(curbuf)
3471#endif
3472 ? _("[Not edited]") : "",
3473 (curbuf->b_flags & BF_NEW)
3474#ifdef FEAT_QUICKFIX
3475 && !bt_dontwrite(curbuf)
3476#endif
3477 ? _("[New file]") : "",
3478 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003479 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 : _("[readonly]")) : "",
3481 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3482 || curbuf->b_p_ro) ?
3483 " " : "");
3484 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3485 * causes an overflow, thus for large numbers divide instead. */
3486 if (curwin->w_cursor.lnum > 1000000L)
3487 n = (int)(((long)curwin->w_cursor.lnum) /
3488 ((long)curbuf->b_ml.ml_line_count / 100L));
3489 else
3490 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3491 (long)curbuf->b_ml.ml_line_count);
3492 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3493 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003494 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 }
3496#ifdef FEAT_CMDL_INFO
3497 else if (p_ru)
3498 {
3499 /* Current line and column are already on the screen -- webb */
3500 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003501 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003503 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 (long)curbuf->b_ml.ml_line_count, n);
3505 }
3506#endif
3507 else
3508 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003509 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 _("line %ld of %ld --%d%%-- col "),
3511 (long)curwin->w_cursor.lnum,
3512 (long)curbuf->b_ml.ml_line_count,
3513 n);
3514 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003515 len = STRLEN(buffer);
3516 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3518 }
3519
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003520 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521
3522 if (dont_truncate)
3523 {
3524 /* Temporarily set msg_scroll to avoid the message being truncated.
3525 * First call msg_start() to get the message in the right place. */
3526 msg_start();
3527 n = msg_scroll;
3528 msg_scroll = TRUE;
3529 msg(buffer);
3530 msg_scroll = n;
3531 }
3532 else
3533 {
3534 p = msg_trunc_attr(buffer, FALSE, 0);
3535 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 /* Need to repeat the message after redrawing when:
3537 * - When restart_edit is set (otherwise there will be a delay
3538 * before redrawing).
3539 * - When the screen was scrolled but there is no wait-return
3540 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003541 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 }
3543
3544 vim_free(buffer);
3545}
3546
3547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548col_print(
3549 char_u *buf,
3550 size_t buflen,
3551 int col,
3552 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553{
3554 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003555 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003557 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558}
3559
3560#if defined(FEAT_TITLE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561static char_u *lasttitle = NULL;
3562static char_u *lasticon = NULL;
3563
Bram Moolenaar84a93082018-06-16 22:58:15 +02003564/*
3565 * Put the file name in the title bar and icon of the window.
3566 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003568maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569{
3570 char_u *p;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003571 char_u *title_str = NULL;
3572 char_u *icon_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 int maxlen = 0;
3574 int len;
3575 int mustset;
3576 char_u buf[IOSIZE];
3577 int off;
3578
3579 if (!redrawing())
3580 {
3581 /* Postpone updating the title when 'lazyredraw' is set. */
3582 need_maketitle = TRUE;
3583 return;
3584 }
3585
3586 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003587 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003588 return; // nothing to do
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589
3590 if (p_title)
3591 {
3592 if (p_titlelen > 0)
3593 {
3594 maxlen = p_titlelen * Columns / 100;
3595 if (maxlen < 10)
3596 maxlen = 10;
3597 }
3598
Bram Moolenaar84a93082018-06-16 22:58:15 +02003599 title_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 if (*p_titlestring != NUL)
3601 {
3602#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003603 if (stl_syntax & STL_IN_TITLE)
3604 {
3605 int use_sandbox = FALSE;
3606 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003607
3608# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003609 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003610# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003611 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003612 build_stl_str_hl(curwin, title_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003613 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003614 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003615 if (called_emsg)
3616 set_string_option_direct((char_u *)"titlestring", -1,
3617 (char_u *)"", OPT_FREE, SID_ERROR);
3618 called_emsg |= save_called_emsg;
3619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 else
3621#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003622 title_str = p_titlestring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 }
3624 else
3625 {
3626 /* format: "fname + (path) (1 of 2) - VIM" */
3627
Bram Moolenaar2c666692012-09-05 13:30:40 +02003628#define SPACE_FOR_FNAME (IOSIZE - 100)
3629#define SPACE_FOR_DIR (IOSIZE - 20)
3630#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003632 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar21554412017-07-24 21:44:43 +02003633#ifdef FEAT_TERMINAL
3634 else if (curbuf->b_term != NULL)
3635 {
3636 vim_strncpy(buf, term_get_status_text(curbuf->b_term),
3637 SPACE_FOR_FNAME);
3638 }
3639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 else
3641 {
3642 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003643 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 }
3646
Bram Moolenaar21554412017-07-24 21:44:43 +02003647#ifdef FEAT_TERMINAL
3648 if (curbuf->b_term == NULL)
3649#endif
3650 switch (bufIsChanged(curbuf)
3651 + (curbuf->b_p_ro * 2)
3652 + (!curbuf->b_p_ma * 4))
3653 {
3654 case 1: STRCAT(buf, " +"); break;
3655 case 2: STRCAT(buf, " ="); break;
3656 case 3: STRCAT(buf, " =+"); break;
3657 case 4:
3658 case 6: STRCAT(buf, " -"); break;
3659 case 5:
3660 case 7: STRCAT(buf, " -+"); break;
3661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662
Bram Moolenaar21554412017-07-24 21:44:43 +02003663 if (curbuf->b_fname != NULL
3664#ifdef FEAT_TERMINAL
3665 && curbuf->b_term == NULL
3666#endif
3667 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 /* Get path of file, replace home dir with ~ */
3670 off = (int)STRLEN(buf);
3671 buf[off++] = ' ';
3672 buf[off++] = '(';
3673 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003674 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675#ifdef BACKSLASH_IN_FILENAME
3676 /* avoid "c:/name" to be reduced to "c" */
3677 if (isalpha(buf[off]) && buf[off + 1] == ':')
3678 off += 2;
3679#endif
3680 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003681 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if (p == buf + off)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003683 {
Bram Moolenaar21554412017-07-24 21:44:43 +02003684 /* must be a help buffer */
3685 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003686 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02003687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003690
Bram Moolenaar2c666692012-09-05 13:30:40 +02003691 /* Translate unprintable chars and concatenate. Keep some
3692 * room for the server name. When there is no room (very long
3693 * file name) use (...). */
3694 if (off < SPACE_FOR_DIR)
3695 {
3696 p = transstr(buf + off);
3697 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3698 vim_free(p);
3699 }
3700 else
3701 {
3702 vim_strncpy(buf + off, (char_u *)"...",
3703 (size_t)(SPACE_FOR_ARGNR - off));
3704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 STRCAT(buf, ")");
3706 }
3707
Bram Moolenaar2c666692012-09-05 13:30:40 +02003708 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709
3710#if defined(FEAT_CLIENTSERVER)
3711 if (serverName != NULL)
3712 {
3713 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003714 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716 else
3717#endif
3718 STRCAT(buf, " - VIM");
3719
3720 if (maxlen > 0)
3721 {
3722 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003723 if (vim_strsize(buf) > maxlen)
3724 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726 }
3727 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003728 mustset = value_changed(title_str, &lasttitle);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729
3730 if (p_icon)
3731 {
Bram Moolenaar84a93082018-06-16 22:58:15 +02003732 icon_str = buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 if (*p_iconstring != NUL)
3734 {
3735#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003736 if (stl_syntax & STL_IN_ICON)
3737 {
3738 int use_sandbox = FALSE;
3739 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003740
3741# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003742 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003743# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003744 called_emsg = FALSE;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003745 build_stl_str_hl(curwin, icon_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003746 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003747 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003748 if (called_emsg)
3749 set_string_option_direct((char_u *)"iconstring", -1,
3750 (char_u *)"", OPT_FREE, SID_ERROR);
3751 called_emsg |= save_called_emsg;
3752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753 else
3754#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003755 icon_str = p_iconstring;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 else
3758 {
3759 if (buf_spname(curbuf) != NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003760 p = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 else /* use file name only in icon */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003762 p = gettail(curbuf->b_ffname);
3763 *icon_str = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 /* Truncate name at 100 bytes. */
Bram Moolenaar84a93082018-06-16 22:58:15 +02003765 len = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 if (len > 100)
3767 {
3768 len -= 100;
3769#ifdef FEAT_MBYTE
3770 if (has_mbyte)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003771 len += (*mb_tail_off)(p, p + len) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772#endif
Bram Moolenaar84a93082018-06-16 22:58:15 +02003773 p += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar84a93082018-06-16 22:58:15 +02003775 STRCPY(icon_str, p);
3776 trans_characters(icon_str, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
3779
Bram Moolenaar84a93082018-06-16 22:58:15 +02003780 mustset |= value_changed(icon_str, &lasticon);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781
3782 if (mustset)
3783 resettitle();
3784}
3785
3786/*
3787 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3788 * from "str" if it does.
Bram Moolenaar84a93082018-06-16 22:58:15 +02003789 * Return TRUE if resettitle() is to be called.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
3791 static int
Bram Moolenaar84a93082018-06-16 22:58:15 +02003792value_changed(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 if ((str == NULL) != (*last == NULL)
3795 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3796 {
3797 vim_free(*last);
3798 if (str == NULL)
Bram Moolenaar84a93082018-06-16 22:58:15 +02003799 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003800 *last = NULL;
Bram Moolenaar84a93082018-06-16 22:58:15 +02003801 mch_restore_title(last == &lasttitle ? 1 : 2);
3802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 else
Bram Moolenaar84a93082018-06-16 22:58:15 +02003804 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 *last = vim_strsave(str);
Bram Moolenaar84a93082018-06-16 22:58:15 +02003806 return TRUE;
3807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809 return FALSE;
3810}
3811
3812/*
3813 * Put current window title back (used after calling a shell)
3814 */
3815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003816resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817{
3818 mch_settitle(lasttitle, lasticon);
3819}
Bram Moolenaarea408852005-06-25 22:49:46 +00003820
3821# if defined(EXITFREE) || defined(PROTO)
3822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003823free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003824{
3825 vim_free(lasttitle);
3826 vim_free(lasticon);
3827}
3828# endif
3829
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830#endif /* FEAT_TITLE */
3831
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003832#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003834 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 * Return length of string in screen cells.
3836 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003837 * Normally works for window "wp", except when working for 'tabline' then it
3838 * is "curwin".
3839 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 * Items are drawn interspersed with the text that surrounds it
3841 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3842 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3843 *
3844 * If maxwidth is not zero, the string will be filled at any middle marker
3845 * or truncated if too long, fillchar is used for all whitespace.
3846 */
3847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003848build_stl_str_hl(
3849 win_T *wp,
3850 char_u *out, /* buffer to write into != NameBuff */
3851 size_t outlen, /* length of out[] */
3852 char_u *fmt,
3853 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3854 int fillchar,
3855 int maxwidth,
3856 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3857 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858{
3859 char_u *p;
3860 char_u *s;
3861 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003862 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863#ifdef FEAT_EVAL
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003864 win_T *save_curwin;
3865 buf_T *save_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866#endif
3867 int empty_line;
3868 colnr_T virtcol;
3869 long l;
3870 long n;
3871 int prevchar_isflag;
3872 int prevchar_isitem;
3873 int itemisflag;
3874 int fillable;
3875 char_u *str;
3876 long num;
3877 int width;
3878 int itemcnt;
3879 int curitem;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02003880 int group_end_userhl;
3881 int group_start_userhl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 int groupitem[STL_MAX_ITEM];
3883 int groupdepth;
3884 struct stl_item
3885 {
3886 char_u *start;
3887 int minwid;
3888 int maxwid;
3889 enum
3890 {
3891 Normal,
3892 Empty,
3893 Group,
3894 Middle,
3895 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003896 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 Trunc
3898 } type;
3899 } item[STL_MAX_ITEM];
3900 int minwid;
3901 int maxwid;
3902 int zeropad;
3903 char_u base;
3904 char_u opt;
3905#define TMPLEN 70
3906 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003907 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003908 struct stl_hlrec *sp;
Bram Moolenaarba2929b2017-09-08 13:59:21 +02003909 int save_must_redraw = must_redraw;
3910 int save_redr_type = curwin->w_redr_type;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003911
3912#ifdef FEAT_EVAL
3913 /*
3914 * When the format starts with "%!" then evaluate it as an expression and
3915 * use the result as the actual format string.
3916 */
3917 if (fmt[0] == '%' && fmt[1] == '!')
3918 {
3919 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3920 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003921 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003922 }
3923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924
3925 if (fillchar == 0)
3926 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003927#ifdef FEAT_MBYTE
3928 /* Can't handle a multi-byte fill character yet. */
3929 else if (mb_char2len(fillchar) > 1)
3930 fillchar = '-';
3931#endif
3932
Bram Moolenaar567199b2013-04-24 16:52:36 +02003933 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3934 * p will become invalid when getting another buffer line. */
3935 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3936 empty_line = (*p == NUL);
3937
3938 /* Get the byte value now, in case we need it below. This is more
3939 * efficient than making a copy of the line. */
3940 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3941 byteval = 0;
3942 else
3943#ifdef FEAT_MBYTE
3944 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3945#else
3946 byteval = p[wp->w_cursor.col];
3947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948
3949 groupdepth = 0;
3950 p = out;
3951 curitem = 0;
3952 prevchar_isflag = TRUE;
3953 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003954 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003956 if (curitem == STL_MAX_ITEM)
3957 {
3958 /* There are too many items. Add the error code to the statusline
3959 * to give the user a hint about what went wrong. */
3960 if (p + 6 < out + outlen)
3961 {
3962 mch_memmove(p, " E541", (size_t)5);
3963 p += 5;
3964 }
3965 break;
3966 }
3967
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 if (*s != NUL && *s != '%')
3969 prevchar_isflag = prevchar_isitem = FALSE;
3970
3971 /*
3972 * Handle up to the next '%' or the end.
3973 */
3974 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3975 *p++ = *s++;
3976 if (*s == NUL || p + 1 >= out + outlen)
3977 break;
3978
3979 /*
3980 * Handle one '%' item.
3981 */
3982 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003983 if (*s == NUL) /* ignore trailing % */
3984 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 if (*s == '%')
3986 {
3987 if (p + 1 >= out + outlen)
3988 break;
3989 *p++ = *s++;
3990 prevchar_isflag = prevchar_isitem = FALSE;
3991 continue;
3992 }
3993 if (*s == STL_MIDDLEMARK)
3994 {
3995 s++;
3996 if (groupdepth > 0)
3997 continue;
3998 item[curitem].type = Middle;
3999 item[curitem++].start = p;
4000 continue;
4001 }
4002 if (*s == STL_TRUNCMARK)
4003 {
4004 s++;
4005 item[curitem].type = Trunc;
4006 item[curitem++].start = p;
4007 continue;
4008 }
4009 if (*s == ')')
4010 {
4011 s++;
4012 if (groupdepth < 1)
4013 continue;
4014 groupdepth--;
4015
4016 t = item[groupitem[groupdepth]].start;
4017 *p = NUL;
4018 l = vim_strsize(t);
4019 if (curitem > groupitem[groupdepth] + 1
4020 && item[groupitem[groupdepth]].minwid == 0)
4021 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004022 /* remove group if all items are empty and highlight group
4023 * doesn't change */
4024 group_start_userhl = group_end_userhl = 0;
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004025 for (n = groupitem[groupdepth] - 1; n >= 0; n--)
4026 {
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004027 if (item[n].type == Highlight)
Bram Moolenaar235dddf2017-10-26 18:21:24 +02004028 {
4029 group_start_userhl = group_end_userhl = item[n].minwid;
4030 break;
4031 }
4032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004034 {
4035 if (item[n].type == Normal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 break;
Bram Moolenaar6b89dbb2017-10-22 14:22:16 +02004037 if (item[n].type == Highlight)
4038 group_end_userhl = item[n].minwid;
4039 }
4040 if (n == curitem && group_start_userhl == group_end_userhl)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
4042 p = t;
4043 l = 0;
4044 }
4045 }
4046 if (l > item[groupitem[groupdepth]].maxwid)
4047 {
4048 /* truncate, remove n bytes of text at the start */
4049#ifdef FEAT_MBYTE
4050 if (has_mbyte)
4051 {
4052 /* Find the first character that should be included. */
4053 n = 0;
4054 while (l >= item[groupitem[groupdepth]].maxwid)
4055 {
4056 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004057 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 }
4059 }
4060 else
4061#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004062 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063
4064 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004065 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 p = p - n + 1;
4067#ifdef FEAT_MBYTE
4068 /* Fill up space left over by half a double-wide char. */
4069 while (++l < item[groupitem[groupdepth]].minwid)
4070 *p++ = fillchar;
4071#endif
4072
4073 /* correct the start of the items for the truncation */
4074 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4075 {
4076 item[l].start -= n;
4077 if (item[l].start < t)
4078 item[l].start = t;
4079 }
4080 }
4081 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4082 {
4083 /* fill */
4084 n = item[groupitem[groupdepth]].minwid;
4085 if (n < 0)
4086 {
4087 /* fill by appending characters */
4088 n = 0 - n;
4089 while (l++ < n && p + 1 < out + outlen)
4090 *p++ = fillchar;
4091 }
4092 else
4093 {
4094 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004095 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 l = n - l;
4097 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004098 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 p += l;
4100 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4101 item[n].start += l;
4102 for ( ; l > 0; l--)
4103 *t++ = fillchar;
4104 }
4105 }
4106 continue;
4107 }
4108 minwid = 0;
4109 maxwid = 9999;
4110 zeropad = FALSE;
4111 l = 1;
4112 if (*s == '0')
4113 {
4114 s++;
4115 zeropad = TRUE;
4116 }
4117 if (*s == '-')
4118 {
4119 s++;
4120 l = -1;
4121 }
4122 if (VIM_ISDIGIT(*s))
4123 {
4124 minwid = (int)getdigits(&s);
4125 if (minwid < 0) /* overflow */
4126 minwid = 0;
4127 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004128 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 item[curitem].type = Highlight;
4131 item[curitem].start = p;
4132 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4133 s++;
4134 curitem++;
4135 continue;
4136 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004137 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4138 {
4139 if (*s == STL_TABCLOSENR)
4140 {
4141 if (minwid == 0)
4142 {
4143 /* %X ends the close label, go back to the previously
4144 * define tab label nr. */
4145 for (n = curitem - 1; n >= 0; --n)
4146 if (item[n].type == TabPage && item[n].minwid >= 0)
4147 {
4148 minwid = item[n].minwid;
4149 break;
4150 }
4151 }
4152 else
4153 /* close nrs are stored as negative values */
4154 minwid = - minwid;
4155 }
4156 item[curitem].type = TabPage;
4157 item[curitem].start = p;
4158 item[curitem].minwid = minwid;
4159 s++;
4160 curitem++;
4161 continue;
4162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 if (*s == '.')
4164 {
4165 s++;
4166 if (VIM_ISDIGIT(*s))
4167 {
4168 maxwid = (int)getdigits(&s);
4169 if (maxwid <= 0) /* overflow */
4170 maxwid = 50;
4171 }
4172 }
4173 minwid = (minwid > 50 ? 50 : minwid) * l;
4174 if (*s == '(')
4175 {
4176 groupitem[groupdepth++] = curitem;
4177 item[curitem].type = Group;
4178 item[curitem].start = p;
4179 item[curitem].minwid = minwid;
4180 item[curitem].maxwid = maxwid;
4181 s++;
4182 curitem++;
4183 continue;
4184 }
4185 if (vim_strchr(STL_ALL, *s) == NULL)
4186 {
4187 s++;
4188 continue;
4189 }
4190 opt = *s++;
4191
4192 /* OK - now for the real work */
4193 base = 'D';
4194 itemisflag = FALSE;
4195 fillable = TRUE;
4196 num = -1;
4197 str = NULL;
4198 switch (opt)
4199 {
4200 case STL_FILEPATH:
4201 case STL_FULLPATH:
4202 case STL_FILENAME:
4203 fillable = FALSE; /* don't change ' ' to fillchar */
4204 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004205 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 else
4207 {
4208 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004209 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4211 }
4212 trans_characters(NameBuff, MAXPATHL);
4213 if (opt != STL_FILENAME)
4214 str = NameBuff;
4215 else
4216 str = gettail(NameBuff);
4217 break;
4218
4219 case STL_VIM_EXPR: /* '{' */
4220 itemisflag = TRUE;
4221 t = p;
4222 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4223 *p++ = *s++;
4224 if (*s != '}') /* missing '}' or out of space */
4225 break;
4226 s++;
4227 *p = 0;
4228 p = t;
4229
4230#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004231 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar3cb44482018-08-05 13:22:26 +02004232 set_internal_string_var((char_u *)"g:actual_curbuf", tmp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004234 save_curbuf = curbuf;
4235 save_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 curwin = wp;
4237 curbuf = wp->w_buffer;
4238
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004239 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004241 curwin = save_curwin;
4242 curbuf = save_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004243 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 if (str != NULL && *str != 0)
4246 {
4247 if (*skipdigits(str) == NUL)
4248 {
4249 num = atoi((char *)str);
Bram Moolenaard23a8232018-02-10 18:45:26 +01004250 VIM_CLEAR(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 itemisflag = FALSE;
4252 }
4253 }
4254#endif
4255 break;
4256
4257 case STL_LINE:
4258 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4259 ? 0L : (long)(wp->w_cursor.lnum);
4260 break;
4261
4262 case STL_NUMLINES:
4263 num = wp->w_buffer->b_ml.ml_line_count;
4264 break;
4265
4266 case STL_COLUMN:
4267 num = !(State & INSERT) && empty_line
4268 ? 0 : (int)wp->w_cursor.col + 1;
4269 break;
4270
4271 case STL_VIRTCOL:
4272 case STL_VIRTCOL_ALT:
4273 /* In list mode virtcol needs to be recomputed */
4274 virtcol = wp->w_virtcol;
4275 if (wp->w_p_list && lcs_tab1 == NUL)
4276 {
4277 wp->w_p_list = FALSE;
4278 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4279 wp->w_p_list = TRUE;
4280 }
4281 ++virtcol;
4282 /* Don't display %V if it's the same as %c. */
4283 if (opt == STL_VIRTCOL_ALT
4284 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4285 ? 0 : (int)wp->w_cursor.col + 1)))
4286 break;
4287 num = (long)virtcol;
4288 break;
4289
4290 case STL_PERCENTAGE:
4291 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4292 (long)wp->w_buffer->b_ml.ml_line_count);
4293 break;
4294
4295 case STL_ALTPERCENT:
4296 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004297 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 break;
4299
4300 case STL_ARGLISTSTAT:
4301 fillable = FALSE;
4302 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004303 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 str = tmp;
4305 break;
4306
4307 case STL_KEYMAP:
4308 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004309 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004310 str = tmp;
4311 break;
4312 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004313#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004314 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315#else
4316 num = 0;
4317#endif
4318 break;
4319
4320 case STL_BUFNO:
4321 num = wp->w_buffer->b_fnum;
4322 break;
4323
4324 case STL_OFFSET_X:
4325 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004326 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 case STL_OFFSET:
4328#ifdef FEAT_BYTEOFF
4329 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4330 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4331 0L : l + 1 + (!(State & INSERT) && empty_line ?
4332 0 : (int)wp->w_cursor.col);
4333#endif
4334 break;
4335
4336 case STL_BYTEVAL_X:
4337 base = 'X';
Bram Moolenaar2f40d122017-10-24 21:49:36 +02004338 /* FALLTHROUGH */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004340 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 if (num == NL)
4342 num = 0;
4343 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4344 num = NL;
4345 break;
4346
4347 case STL_ROFLAG:
4348 case STL_ROFLAG_ALT:
4349 itemisflag = TRUE;
4350 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004351 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 break;
4353
4354 case STL_HELPFLAG:
4355 case STL_HELPFLAG_ALT:
4356 itemisflag = TRUE;
4357 if (wp->w_buffer->b_help)
4358 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004359 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 break;
4361
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 case STL_FILETYPE:
4363 if (*wp->w_buffer->b_p_ft != NUL
4364 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4365 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004366 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4367 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 str = tmp;
4369 }
4370 break;
4371
4372 case STL_FILETYPE_ALT:
4373 itemisflag = TRUE;
4374 if (*wp->w_buffer->b_p_ft != NUL
4375 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4376 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004377 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4378 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 for (t = tmp; *t != 0; t++)
4380 *t = TOUPPER_LOC(*t);
4381 str = tmp;
4382 }
4383 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384
Bram Moolenaar4033c552017-09-16 20:54:51 +02004385#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 case STL_PREVIEWFLAG:
4387 case STL_PREVIEWFLAG_ALT:
4388 itemisflag = TRUE;
4389 if (wp->w_p_pvw)
4390 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4391 : _("[Preview]"));
4392 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004393
4394 case STL_QUICKFIX:
4395 if (bt_quickfix(wp->w_buffer))
4396 str = (char_u *)(wp->w_llist_ref
4397 ? _(msg_loclist)
4398 : _(msg_qflist));
4399 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400#endif
4401
4402 case STL_MODIFIED:
4403 case STL_MODIFIED_ALT:
4404 itemisflag = TRUE;
4405 switch ((opt == STL_MODIFIED_ALT)
4406 + bufIsChanged(wp->w_buffer) * 2
4407 + (!wp->w_buffer->b_p_ma) * 4)
4408 {
4409 case 2: str = (char_u *)"[+]"; break;
4410 case 3: str = (char_u *)",+"; break;
4411 case 4: str = (char_u *)"[-]"; break;
4412 case 5: str = (char_u *)",-"; break;
4413 case 6: str = (char_u *)"[+-]"; break;
4414 case 7: str = (char_u *)",+-"; break;
4415 }
4416 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004417
4418 case STL_HIGHLIGHT:
4419 t = s;
4420 while (*s != '#' && *s != NUL)
4421 ++s;
4422 if (*s == '#')
4423 {
4424 item[curitem].type = Highlight;
4425 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004426 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004427 curitem++;
4428 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004429 if (*s != NUL)
4430 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004431 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 }
4433
4434 item[curitem].start = p;
4435 item[curitem].type = Normal;
4436 if (str != NULL && *str)
4437 {
4438 t = str;
4439 if (itemisflag)
4440 {
4441 if ((t[0] && t[1])
4442 && ((!prevchar_isitem && *t == ',')
4443 || (prevchar_isflag && *t == ' ')))
4444 t++;
4445 prevchar_isflag = TRUE;
4446 }
4447 l = vim_strsize(t);
4448 if (l > 0)
4449 prevchar_isitem = TRUE;
4450 if (l > maxwid)
4451 {
4452 while (l >= maxwid)
4453#ifdef FEAT_MBYTE
4454 if (has_mbyte)
4455 {
4456 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004457 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459 else
4460#endif
4461 l -= byte2cells(*t++);
4462 if (p + 1 >= out + outlen)
4463 break;
4464 *p++ = '<';
4465 }
4466 if (minwid > 0)
4467 {
4468 for (; l < minwid && p + 1 < out + outlen; l++)
4469 {
4470 /* Don't put a "-" in front of a digit. */
4471 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4472 *p++ = ' ';
4473 else
4474 *p++ = fillchar;
4475 }
4476 minwid = 0;
4477 }
4478 else
4479 minwid *= -1;
4480 while (*t && p + 1 < out + outlen)
4481 {
4482 *p++ = *t++;
4483 /* Change a space by fillchar, unless fillchar is '-' and a
4484 * digit follows. */
4485 if (fillable && p[-1] == ' '
4486 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4487 p[-1] = fillchar;
4488 }
4489 for (; l < minwid && p + 1 < out + outlen; l++)
4490 *p++ = fillchar;
4491 }
4492 else if (num >= 0)
4493 {
4494 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4495 char_u nstr[20];
4496
4497 if (p + 20 >= out + outlen)
4498 break; /* not sufficient space */
4499 prevchar_isitem = TRUE;
4500 t = nstr;
4501 if (opt == STL_VIRTCOL_ALT)
4502 {
4503 *t++ = '-';
4504 minwid--;
4505 }
4506 *t++ = '%';
4507 if (zeropad)
4508 *t++ = '0';
4509 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004510 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 *t = 0;
4512
4513 for (n = num, l = 1; n >= nbase; n /= nbase)
4514 l++;
4515 if (opt == STL_VIRTCOL_ALT)
4516 l++;
4517 if (l > maxwid)
4518 {
4519 l += 2;
4520 n = l - maxwid;
4521 while (l-- > maxwid)
4522 num /= nbase;
4523 *t++ = '>';
4524 *t++ = '%';
4525 *t = t[-3];
4526 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004527 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4528 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004531 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4532 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 p += STRLEN(p);
4534 }
4535 else
4536 item[curitem].type = Empty;
4537
4538 if (opt == STL_VIM_EXPR)
4539 vim_free(str);
4540
4541 if (num >= 0 || (!itemisflag && str && *str))
4542 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4543 curitem++;
4544 }
4545 *p = NUL;
4546 itemcnt = curitem;
4547
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004548#ifdef FEAT_EVAL
4549 if (usefmt != fmt)
4550 vim_free(usefmt);
4551#endif
4552
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 width = vim_strsize(out);
4554 if (maxwidth > 0 && width > maxwidth)
4555 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004556 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 l = 0;
4558 if (itemcnt == 0)
4559 s = out;
4560 else
4561 {
4562 for ( ; l < itemcnt; l++)
4563 if (item[l].type == Trunc)
4564 {
4565 /* Truncate at %< item. */
4566 s = item[l].start;
4567 break;
4568 }
4569 if (l == itemcnt)
4570 {
4571 /* No %< item, truncate first item. */
4572 s = item[0].start;
4573 l = 0;
4574 }
4575 }
4576
4577 if (width - vim_strsize(s) >= maxwidth)
4578 {
4579 /* Truncation mark is beyond max length */
4580#ifdef FEAT_MBYTE
4581 if (has_mbyte)
4582 {
4583 s = out;
4584 width = 0;
4585 for (;;)
4586 {
4587 width += ptr2cells(s);
4588 if (width >= maxwidth)
4589 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004590 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 /* Fill up for half a double-wide character. */
4593 while (++width < maxwidth)
4594 *s++ = fillchar;
4595 }
4596 else
4597#endif
4598 s = out + maxwidth - 1;
4599 for (l = 0; l < itemcnt; l++)
4600 if (item[l].start > s)
4601 break;
4602 itemcnt = l;
4603 *s++ = '>';
4604 *s = 0;
4605 }
4606 else
4607 {
4608#ifdef FEAT_MBYTE
4609 if (has_mbyte)
4610 {
4611 n = 0;
4612 while (width >= maxwidth)
4613 {
4614 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004615 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 }
4617 }
4618 else
4619#endif
4620 n = width - maxwidth + 1;
4621 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004622 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 *s = '<';
4624
4625 /* Fill up for half a double-wide character. */
4626 while (++width < maxwidth)
4627 {
4628 s = s + STRLEN(s);
4629 *s++ = fillchar;
4630 *s = NUL;
4631 }
4632
4633 --n; /* count the '<' */
4634 for (; l < itemcnt; l++)
4635 {
4636 if (item[l].start - n >= s)
4637 item[l].start -= n;
4638 else
4639 item[l].start = s;
4640 }
4641 }
4642 width = maxwidth;
4643 }
4644 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4645 {
4646 /* Apply STL_MIDDLE if any */
4647 for (l = 0; l < itemcnt; l++)
4648 if (item[l].type == Middle)
4649 break;
4650 if (l < itemcnt)
4651 {
4652 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004653 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 for (s = item[l].start; s < p; s++)
4655 *s = fillchar;
4656 for (l++; l < itemcnt; l++)
4657 item[l].start += maxwidth - width;
4658 width = maxwidth;
4659 }
4660 }
4661
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004662 /* Store the info about highlighting. */
4663 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004665 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 for (l = 0; l < itemcnt; l++)
4667 {
4668 if (item[l].type == Highlight)
4669 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004670 sp->start = item[l].start;
4671 sp->userhl = item[l].minwid;
4672 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 }
4674 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004675 sp->start = NULL;
4676 sp->userhl = 0;
4677 }
4678
4679 /* Store the info about tab pages labels. */
4680 if (tabtab != NULL)
4681 {
4682 sp = tabtab;
4683 for (l = 0; l < itemcnt; l++)
4684 {
4685 if (item[l].type == TabPage)
4686 {
4687 sp->start = item[l].start;
4688 sp->userhl = item[l].minwid;
4689 sp++;
4690 }
4691 }
4692 sp->start = NULL;
4693 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695
Bram Moolenaar65ed1362017-09-30 16:00:14 +02004696 /* When inside update_screen we do not want redrawing a stausline, ruler,
4697 * title, etc. to trigger another redraw, it may cause an endless loop. */
4698 if (updating_screen)
4699 {
4700 must_redraw = save_must_redraw;
4701 curwin->w_redr_type = save_redr_type;
4702 }
Bram Moolenaarba2929b2017-09-08 13:59:21 +02004703
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return width;
4705}
4706#endif /* FEAT_STL_OPT */
4707
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004708#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4709 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004711 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4712 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 */
4714 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004715get_rel_pos(
4716 win_T *wp,
4717 char_u *buf,
4718 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719{
4720 long above; /* number of lines above window */
4721 long below; /* number of lines below window */
4722
Bram Moolenaar0027c212015-01-07 13:31:52 +01004723 if (buflen < 3) /* need at least 3 chars for writing */
4724 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 above = wp->w_topline - 1;
4726#ifdef FEAT_DIFF
4727 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004728 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4729 above = 0; /* All buffer lines are displayed and there is an
4730 * indication of filler lines, that can be considered
4731 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732#endif
4733 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4734 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004735 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4736 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004738 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004740 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 ? (int)(above / ((above + below) / 100L))
4742 : (int)(above * 100L / (above + below)));
4743}
4744#endif
4745
4746/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004747 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 * Return TRUE if it was appended.
4749 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004750 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004751append_arg_number(
4752 win_T *wp,
4753 char_u *buf,
4754 int buflen,
4755 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756{
4757 char_u *p;
4758
4759 if (ARGCOUNT <= 1) /* nothing to do */
4760 return FALSE;
4761
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004762 p = buf + STRLEN(buf); /* go to the end of the buffer */
4763 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 return FALSE;
4765 *p++ = ' ';
4766 *p++ = '(';
4767 if (add_file)
4768 {
4769 STRCPY(p, "file ");
4770 p += 5;
4771 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004772 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4773 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4775 return TRUE;
4776}
4777
4778/*
4779 * If fname is not a full path, make it a full path.
4780 * Returns pointer to allocated memory (NULL for failure).
4781 */
4782 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004783fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784{
4785 /*
4786 * Force expanding the path always for Unix, because symbolic links may
4787 * mess up the full path name, even though it starts with a '/'.
4788 * Also expand when there is ".." in the file name, try to remove it,
4789 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004790 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 * For MS-Windows also expand names like "longna~1" to "longname".
4792 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004793#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 return FullName_save(fname, TRUE);
4795#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004796 if (!vim_isAbsName(fname)
4797 || strstr((char *)fname, "..") != NULL
4798 || strstr((char *)fname, "//") != NULL
4799# ifdef BACKSLASH_IN_FILENAME
4800 || strstr((char *)fname, "\\\\") != NULL
4801# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004802# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 )
4806 return FullName_save(fname, FALSE);
4807
4808 fname = vim_strsave(fname);
4809
Bram Moolenaar9b942202007-10-03 12:31:33 +00004810# ifdef USE_FNAME_CASE
4811# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004813# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 {
4815 if (fname != NULL)
4816 fname_case(fname, 0); /* set correct case for file name */
4817 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004818# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 return fname;
4821#endif
4822}
4823
4824/*
4825 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4826 * "ffname" becomes a pointer to allocated memory (or NULL).
4827 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004829fname_expand(
4830 buf_T *buf UNUSED,
4831 char_u **ffname,
4832 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833{
4834 if (*ffname == NULL) /* if no file name given, nothing to do */
4835 return;
4836 if (*sfname == NULL) /* if no short file name given, use ffname */
4837 *sfname = *ffname;
4838 *ffname = fix_fname(*ffname); /* expand to full path */
4839
4840#ifdef FEAT_SHORTCUT
4841 if (!buf->b_p_bin)
4842 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004843 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844
4845 /* If the file name is a shortcut file, use the file it links to. */
4846 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004847 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 {
4849 vim_free(*ffname);
4850 *ffname = rfname;
4851 *sfname = rfname;
4852 }
4853 }
4854#endif
4855}
4856
4857/*
4858 * Get the file name for an argument list entry.
4859 */
4860 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004861alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862{
4863 buf_T *bp;
4864
4865 /* Use the name from the associated buffer if it exists. */
4866 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004867 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 return aep->ae_fname;
4869 return bp->b_fname;
4870}
4871
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872/*
4873 * do_arg_all(): Open up to 'count' windows, one for each argument.
4874 */
4875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004876do_arg_all(
4877 int count,
4878 int forceit, /* hide buffers in current windows */
4879 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880{
4881 int i;
4882 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004883 char_u *opened; /* Array of weight for which args are open:
4884 * 0: not opened
4885 * 1: opened in other tab
4886 * 2: opened in curtab
4887 * 3: opened in curtab and curwin
4888 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004889 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 int use_firstwin = FALSE; /* use first window for arglist */
4891 int split_ret = OK;
4892 int p_ea_save;
4893 alist_T *alist; /* argument list to be used */
4894 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004895 tabpage_T *tpnext;
4896 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004897 win_T *old_curwin, *last_curwin;
4898 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004899 win_T *new_curwin = NULL;
4900 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901
4902 if (ARGCOUNT <= 0)
4903 {
4904 /* Don't give an error message. We don't want it when the ":all"
4905 * command is in the .vimrc. */
4906 return;
4907 }
4908 setpcmark();
4909
4910 opened_len = ARGCOUNT;
4911 opened = alloc_clear((unsigned)opened_len);
4912 if (opened == NULL)
4913 return;
4914
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004915 /* Autocommands may do anything to the argument list. Make sure it's not
4916 * freed while we are working here by "locking" it. We still have to
4917 * watch out for its size to be changed. */
4918 alist = curwin->w_alist;
4919 ++alist->al_refcount;
4920
4921 old_curwin = curwin;
4922 old_curtab = curtab;
4923
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004924# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004926# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927
4928 /*
4929 * Try closing all windows that are not in the argument list.
4930 * Also close windows that are not full width;
4931 * When 'hidden' or "forceit" set the buffer becomes hidden.
4932 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004933 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004935 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004936 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004937 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004939 tpnext = curtab->tp_next;
4940 for (wp = firstwin; wp != NULL; wp = wpnext)
4941 {
4942 wpnext = wp->w_next;
4943 buf = wp->w_buffer;
4944 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004945 || (!keep_tabs && (buf->b_nwindows > 1
4946 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004947 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004948 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004950 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004951 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004953 if (i < alist->al_ga.ga_len
4954 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4955 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4956 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004958 int weight = 1;
4959
4960 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004961 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004962 ++weight;
4963 if (old_curwin == wp)
4964 ++weight;
4965 }
4966
4967 if (weight > (int)opened[i])
4968 {
4969 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004970 if (i == 0)
4971 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004972 if (new_curwin != NULL)
4973 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004974 new_curwin = wp;
4975 new_curtab = curtab;
4976 }
4977 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004978 else if (keep_tabs)
4979 i = opened_len;
4980
4981 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004982 {
4983 /* Use the current argument list for all windows
4984 * containing a file from it. */
4985 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004986 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004987 ++wp->w_alist->al_refcount;
4988 }
4989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004993 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004995 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02004997 if (buf_hide(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004998 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005000 /* If the buffer was changed, and we would like to hide it,
5001 * try autowriting. */
Bram Moolenaareb44a682017-08-03 22:44:55 +02005002 if (!buf_hide(buf) && buf->b_nwindows <= 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005003 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005005 bufref_T bufref;
5006
5007 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005008
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005009 (void)autowrite(buf, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005010
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005011 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005012 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005013 {
5014 wpnext = firstwin; /* start all over... */
5015 continue;
5016 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005017 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005018 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01005019 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005020 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005021 use_firstwin = TRUE;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005022 else
5023 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005024 win_close(wp, !buf_hide(buf) && !bufIsChanged(buf));
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005025
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005026 /* check if autocommands removed the next window */
5027 if (!win_valid(wpnext))
5028 wpnext = firstwin; /* start all over... */
Bram Moolenaar4033c552017-09-16 20:54:51 +02005029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
5032 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005033
5034 /* Without the ":tab" modifier only do the current tab page. */
5035 if (had_tab == 0 || tpnext == NULL)
5036 break;
5037
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005038 /* check if autocommands removed the next tab page */
5039 if (!valid_tabpage(tpnext))
5040 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005041
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005042 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044
5045 /*
5046 * Open a window for files in the argument list that don't have one.
5047 * ARGCOUNT may change while doing this, because of autocommands.
5048 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005049 if (count > opened_len || count <= 0)
5050 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5053 ++autocmd_no_enter;
5054 ++autocmd_no_leave;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005055 last_curwin = curwin;
5056 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005058 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5059 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005060 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005061 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5062 use_firstwin = TRUE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005063
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005064 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 {
5066 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5067 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005068 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 /* Move the already present window to below the current window */
5071 if (curwin->w_arg_idx != i)
5072 {
5073 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5074 {
5075 if (wpnext->w_arg_idx == i)
5076 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005077 if (keep_tabs)
5078 {
5079 new_curwin = wpnext;
5080 new_curtab = curtab;
5081 }
5082 else
5083 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 break;
5085 }
5086 }
5087 }
5088 }
5089 else if (split_ret == OK)
5090 {
5091 if (!use_firstwin) /* split current window */
5092 {
5093 p_ea_save = p_ea;
5094 p_ea = TRUE; /* use space from all windows */
5095 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5096 p_ea = p_ea_save;
5097 if (split_ret == FAIL)
5098 continue;
5099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 else /* first window: do autocmd for leaving this buffer */
5101 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102
5103 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005104 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 */
5106 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005107 if (i == 0)
5108 {
5109 new_curwin = curwin;
5110 new_curtab = curtab;
5111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5113 ECMD_ONE,
Bram Moolenaareb44a682017-08-03 22:44:55 +02005114 ((buf_hide(curwin->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005116 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (use_firstwin)
5118 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 use_firstwin = FALSE;
5120 }
5121 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005122
5123 /* When ":tab" was used open a new tab for a new window repeatedly. */
5124 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5125 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127
5128 /* Remove the "lock" on the argument list. */
5129 alist_unlink(alist);
5130
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 --autocmd_no_enter;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005132
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005133 /* restore last referenced tabpage's curwin */
5134 if (last_curtab != new_curtab)
5135 {
5136 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005137 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005138 if (win_valid(last_curwin))
5139 win_enter(last_curwin, FALSE);
5140 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005141 /* to window with first arg */
5142 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005143 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005144 if (win_valid(new_curwin))
5145 win_enter(new_curwin, FALSE);
5146
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 vim_free(opened);
5149}
5150
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151/*
5152 * Open a window for a number of buffers.
5153 */
5154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005155ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156{
5157 buf_T *buf;
5158 win_T *wp, *wpnext;
5159 int split_ret = OK;
5160 int p_ea_save;
5161 int open_wins = 0;
5162 int r;
5163 int count; /* Maximum number of windows to open. */
5164 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005165 int had_tab = cmdmod.tab;
5166 tabpage_T *tpnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167
5168 if (eap->addr_count == 0) /* make as many windows as possible */
5169 count = 9999;
5170 else
5171 count = eap->line2; /* make as many windows as specified */
5172 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5173 all = FALSE;
5174 else
5175 all = TRUE;
5176
5177 setpcmark();
5178
5179#ifdef FEAT_GUI
5180 need_mouse_correct = TRUE;
5181#endif
5182
5183 /*
5184 * Close superfluous windows (two windows for the same buffer).
5185 * Also close windows that are not full-width.
5186 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005187 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005188 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005189 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005191 tpnext = curtab->tp_next;
5192 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005194 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005195 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005196 || ((cmdmod.split & WSP_VERT)
5197 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005198 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005199 : wp->w_width != Columns)
Bram Moolenaar4033c552017-09-16 20:54:51 +02005200 || (had_tab > 0 && wp != firstwin)) && !ONE_WINDOW
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005201 && !(wp->w_closing || wp->w_buffer->b_locked > 0))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005202 {
5203 win_close(wp, FALSE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005204 wpnext = firstwin; /* just in case an autocommand does
5205 something strange with windows */
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005206 tpnext = first_tabpage; /* start all over... */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005207 open_wins = 0;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005208 }
5209 else
5210 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005212
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005213 /* Without the ":tab" modifier only do the current tab page. */
5214 if (had_tab == 0 || tpnext == NULL)
5215 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005216 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218
5219 /*
5220 * Go through the buffer list. When a buffer doesn't have a window yet,
5221 * open one. Otherwise move the window to the right position.
5222 * Watch out for autocommands that delete buffers or windows!
5223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5225 ++autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 win_enter(lastwin, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 ++autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5229 {
5230 /* Check if this buffer needs a window */
5231 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5232 continue;
5233
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005234 if (had_tab != 0)
5235 {
5236 /* With the ":tab" modifier don't move the window. */
5237 if (buf->b_nwindows > 0)
5238 wp = lastwin; /* buffer has a window, skip it */
5239 else
5240 wp = NULL;
5241 }
5242 else
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005243 {
5244 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005245 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005246 if (wp->w_buffer == buf)
5247 break;
5248 /* If the buffer already has a window, move it */
5249 if (wp != NULL)
5250 win_move_after(wp, curwin);
5251 }
5252
5253 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005255 bufref_T bufref;
5256
5257 set_bufref(&bufref, buf);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005258
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 /* Split the window and put the buffer in it */
5260 p_ea_save = p_ea;
5261 p_ea = TRUE; /* use space from all windows */
5262 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5263 ++open_wins;
5264 p_ea = p_ea_save;
5265 if (split_ret == FAIL)
5266 continue;
5267
5268 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005269#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 swap_exists_action = SEA_DIALOG;
5271#endif
5272 set_curbuf(buf, DOBUF_GOTO);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005273 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005275 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005276#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 swap_exists_action = SEA_NONE;
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 break;
5280 }
Bram Moolenaare64ac772005-12-07 20:54:59 +00005281#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 if (swap_exists_action == SEA_QUIT)
5283 {
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005284# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005285 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005286
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005287 /* Reset the error/interrupt/exception state here so that
5288 * aborting() returns FALSE when closing a window. */
5289 enter_cleanup(&cs);
5290# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005291
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005292 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 win_close(curwin, TRUE);
5294 --open_wins;
5295 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005296 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005297
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005298# if defined(FEAT_EVAL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005299 /* Restore the error/interrupt/exception state if not
5300 * discarded by a new aborting error, interrupt, or uncaught
5301 * exception. */
5302 leave_cleanup(&cs);
5303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 else
5306 handle_swap_exists(NULL);
5307#endif
5308 }
5309
5310 ui_breakcheck();
5311 if (got_int)
5312 {
5313 (void)vgetc(); /* only break the file loading, not the rest */
5314 break;
5315 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005316#ifdef FEAT_EVAL
5317 /* Autocommands deleted the buffer or aborted script processing!!! */
5318 if (aborting())
5319 break;
5320#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005321 /* When ":tab" was used open a new tab for a new window repeatedly. */
5322 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5323 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 --autocmd_no_enter;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326 win_enter(firstwin, FALSE); /* back to first window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 --autocmd_no_leave;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328
5329 /*
5330 * Close superfluous windows.
5331 */
5332 for (wp = lastwin; open_wins > count; )
5333 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005334 r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 || autowrite(wp->w_buffer, FALSE) == OK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 if (!win_valid(wp))
5337 {
5338 /* BufWrite Autocommands made the window invalid, start over */
5339 wp = lastwin;
5340 }
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01005341 else if (r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 {
Bram Moolenaareb44a682017-08-03 22:44:55 +02005343 win_close(wp, !buf_hide(wp->w_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 --open_wins;
5345 wp = lastwin;
5346 }
5347 else
5348 {
5349 wp = wp->w_prev;
5350 if (wp == NULL)
5351 break;
5352 }
5353 }
5354}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005357static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005358
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359/*
5360 * do_modelines() - process mode lines for the current file
5361 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005362 * "flags" can be:
5363 * OPT_WINONLY only set options local to window
5364 * OPT_NOWIN don't set options local to window
5365 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 * Returns immediately if the "ml" option isn't set.
5367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005369do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005371 linenr_T lnum;
5372 int nmlines;
5373 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374
5375 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5376 return;
5377
5378 /* Disallow recursive entry here. Can happen when executing a modeline
5379 * triggers an autocommand, which reloads modelines with a ":do". */
5380 if (entered)
5381 return;
5382
5383 ++entered;
5384 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5385 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005386 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 nmlines = 0;
5388
5389 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5390 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005391 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 nmlines = 0;
5393 --entered;
5394}
5395
5396#include "version.h" /* for version number */
5397
5398/*
5399 * chk_modeline() - check a single line for a mode string
5400 * Return FAIL if an error encountered.
5401 */
5402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005403chk_modeline(
5404 linenr_T lnum,
5405 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406{
5407 char_u *s;
5408 char_u *e;
5409 char_u *linecopy; /* local copy of any modeline found */
5410 int prev;
5411 int vers;
5412 int end;
5413 int retval = OK;
5414 char_u *save_sourcing_name;
5415 linenr_T save_sourcing_lnum;
5416#ifdef FEAT_EVAL
5417 scid_T save_SID;
5418#endif
5419
5420 prev = -1;
5421 for (s = ml_get(lnum); *s != NUL; ++s)
5422 {
5423 if (prev == -1 || vim_isspace(prev))
5424 {
5425 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5426 || STRNCMP(s, "vi:", (size_t)3) == 0)
5427 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005428 /* Accept both "vim" and "Vim". */
5429 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 {
5431 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5432 e = s + 4;
5433 else
5434 e = s + 3;
5435 vers = getdigits(&e);
5436 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005437 && (s[0] != 'V'
5438 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 && (s[3] == ':'
5440 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5441 || (VIM_VERSION_100 < vers && s[3] == '<')
5442 || (VIM_VERSION_100 > vers && s[3] == '>')
5443 || (VIM_VERSION_100 == vers && s[3] == '=')))
5444 break;
5445 }
5446 }
5447 prev = *s;
5448 }
5449
5450 if (*s)
5451 {
5452 do /* skip over "ex:", "vi:" or "vim:" */
5453 ++s;
5454 while (s[-1] != ':');
5455
5456 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5457 if (linecopy == NULL)
5458 return FAIL;
5459
5460 save_sourcing_lnum = sourcing_lnum;
5461 save_sourcing_name = sourcing_name;
5462 sourcing_lnum = lnum; /* prepare for emsg() */
5463 sourcing_name = (char_u *)"modelines";
5464
5465 end = FALSE;
5466 while (end == FALSE)
5467 {
5468 s = skipwhite(s);
5469 if (*s == NUL)
5470 break;
5471
5472 /*
5473 * Find end of set command: ':' or end of line.
5474 * Skip over "\:", replacing it with ":".
5475 */
5476 for (e = s; *e != ':' && *e != NUL; ++e)
5477 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005478 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 if (*e == NUL)
5480 end = TRUE;
5481
5482 /*
5483 * If there is a "set" command, require a terminating ':' and
5484 * ignore the stuff after the ':'.
5485 * "vi:set opt opt opt: foo" -- foo not interpreted
5486 * "vi:opt opt opt: foo" -- foo interpreted
5487 * Accept "se" for compatibility with Elvis.
5488 */
5489 if (STRNCMP(s, "set ", (size_t)4) == 0
5490 || STRNCMP(s, "se ", (size_t)3) == 0)
5491 {
5492 if (*e != ':') /* no terminating ':'? */
5493 break;
5494 end = TRUE;
5495 s = vim_strchr(s, ' ') + 1;
5496 }
5497 *e = NUL; /* truncate the set command */
5498
5499 if (*s != NUL) /* skip over an empty "::" */
5500 {
5501#ifdef FEAT_EVAL
5502 save_SID = current_SID;
5503 current_SID = SID_MODELINE;
5504#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005505 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506#ifdef FEAT_EVAL
5507 current_SID = save_SID;
5508#endif
5509 if (retval == FAIL) /* stop if error found */
5510 break;
5511 }
5512 s = e + 1; /* advance to next part */
5513 }
5514
5515 sourcing_lnum = save_sourcing_lnum;
5516 sourcing_name = save_sourcing_name;
5517
5518 vim_free(linecopy);
5519 }
5520 return retval;
5521}
5522
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005523#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005525read_viminfo_bufferlist(
5526 vir_T *virp,
5527 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 char_u *tab;
5530 linenr_T lnum;
5531 colnr_T col;
5532 buf_T *buf;
5533 char_u *sfname;
5534 char_u *xline;
5535
5536 /* Handle long line and escaped characters. */
5537 xline = viminfo_readstring(virp, 1, FALSE);
5538
5539 /* don't read in if there are files on the command-line or if writing: */
5540 if (xline != NULL && !writing && ARGCOUNT == 0
5541 && find_viminfo_parameter('%') != NULL)
5542 {
5543 /* Format is: <fname> Tab <lnum> Tab <col>.
5544 * Watch out for a Tab in the file name, work from the end. */
5545 lnum = 0;
5546 col = 0;
5547 tab = vim_strrchr(xline, '\t');
5548 if (tab != NULL)
5549 {
5550 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005551 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 tab = vim_strrchr(xline, '\t');
5553 if (tab != NULL)
5554 {
5555 *tab++ = '\0';
5556 lnum = atol((char *)tab);
5557 }
5558 }
5559
5560 /* Expand "~/" in the file name at "line + 1" to a full path.
5561 * Then try shortening it by comparing with the current directory */
5562 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005563 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564
5565 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5566 if (buf != NULL) /* just in case... */
5567 {
5568 buf->b_last_cursor.lnum = lnum;
5569 buf->b_last_cursor.col = col;
5570 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5571 }
5572 }
5573 vim_free(xline);
5574
5575 return viminfo_readline(virp);
5576}
5577
5578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005579write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580{
5581 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005583 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005585 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586
5587 if (find_viminfo_parameter('%') == NULL)
5588 return;
5589
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005590 /* Without a number -1 is returned: do all buffers. */
5591 max_buffers = get_viminfo_parameter('%');
5592
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005594#define LINE_BUF_LEN (MAXPATHL + 40)
5595 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 if (line == NULL)
5597 return;
5598
Bram Moolenaarf740b292006-02-16 22:11:02 +00005599 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 set_last_cursor(win);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005602 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005603 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 {
5605 if (buf->b_fname == NULL
5606 || !buf->b_p_bl
5607#ifdef FEAT_QUICKFIX
5608 || bt_quickfix(buf)
5609#endif
Bram Moolenaare6278052017-08-13 18:11:17 +02005610#ifdef FEAT_TERMINAL
5611 || bt_terminal(buf)
5612#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 || removable(buf->b_ffname))
5614 continue;
5615
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005616 if (max_buffers-- == 0)
5617 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 putc('%', fp);
5619 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005620 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 (long)buf->b_last_cursor.lnum,
5622 buf->b_last_cursor.col);
5623 viminfo_writestring(fp, line);
5624 }
5625 vim_free(line);
5626}
5627#endif
5628
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005629/*
Bram Moolenaar91335e52018-08-01 17:53:12 +02005630 * Return TRUE if "buf" is a normal buffer, 'buftype' is empty.
5631 */
5632 int
5633bt_normal(buf_T *buf)
5634{
5635 return buf != NULL && buf->b_p_bt[0] == NUL;
5636}
5637
5638/*
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005639 * Return TRUE if "buf" is the quickfix buffer.
5640 */
5641 int
5642bt_quickfix(buf_T *buf)
5643{
5644 return buf != NULL && buf->b_p_bt[0] == 'q';
5645}
5646
5647/*
5648 * Return TRUE if "buf" is a terminal buffer.
5649 */
5650 int
5651bt_terminal(buf_T *buf)
5652{
5653 return buf != NULL && buf->b_p_bt[0] == 't';
5654}
5655
5656/*
Bram Moolenaard28cc3f2017-07-27 22:03:50 +02005657 * Return TRUE if "buf" is a help buffer.
5658 */
5659 int
5660bt_help(buf_T *buf)
5661{
5662 return buf != NULL && buf->b_help;
5663}
5664
5665/*
Bram Moolenaarf2732452018-06-03 14:47:35 +02005666 * Return TRUE if "buf" is a prompt buffer.
5667 */
5668 int
5669bt_prompt(buf_T *buf)
5670{
5671 return buf != NULL && buf->b_p_bt[0] == 'p';
5672}
5673
5674/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005675 * Return TRUE if "buf" is a "nofile", "acwrite", "terminal" or "prompt"
5676 * buffer. This means the buffer name is not a file name.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005677 */
5678 int
5679bt_nofile(buf_T *buf)
5680{
5681 return buf != NULL && ((buf->b_p_bt[0] == 'n' && buf->b_p_bt[2] == 'f')
5682 || buf->b_p_bt[0] == 'a'
Bram Moolenaarf2732452018-06-03 14:47:35 +02005683 || buf->b_p_bt[0] == 't'
5684 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005685}
5686
5687/*
Bram Moolenaar04958cb2018-06-23 19:23:02 +02005688 * Return TRUE if "buf" is a "nowrite", "nofile", "terminal" or "prompt"
5689 * buffer.
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005690 */
5691 int
5692bt_dontwrite(buf_T *buf)
5693{
Bram Moolenaarf2732452018-06-03 14:47:35 +02005694 return buf != NULL && (buf->b_p_bt[0] == 'n'
5695 || buf->b_p_bt[0] == 't'
5696 || buf->b_p_bt[0] == 'p');
Bram Moolenaarf0a521f2017-07-25 23:31:12 +02005697}
5698
5699 int
5700bt_dontwrite_msg(buf_T *buf)
5701{
5702 if (bt_dontwrite(buf))
5703 {
5704 EMSG(_("E382: Cannot write, 'buftype' option is set"));
5705 return TRUE;
5706 }
5707 return FALSE;
5708}
5709
5710/*
5711 * Return TRUE if the buffer should be hidden, according to 'hidden', ":hide"
5712 * and 'bufhidden'.
5713 */
5714 int
5715buf_hide(buf_T *buf)
5716{
5717 /* 'bufhidden' overrules 'hidden' and ":hide", check it first */
5718 switch (buf->b_p_bh[0])
5719 {
5720 case 'u': /* "unload" */
5721 case 'w': /* "wipe" */
5722 case 'd': return FALSE; /* "delete" */
5723 case 'h': return TRUE; /* "hide" */
5724 }
5725 return (p_hid || cmdmod.hide);
5726}
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
5728/*
5729 * Return special buffer name.
5730 * Returns NULL when the buffer has a normal file name.
5731 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005732 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005733buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734{
Bram Moolenaar4033c552017-09-16 20:54:51 +02005735#if defined(FEAT_QUICKFIX)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005737 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005738 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005739 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005740
5741 /*
5742 * For location list window, w_llist_ref points to the location list.
5743 * For quickfix window, w_llist_ref is NULL.
5744 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005745 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005746 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005747 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005748 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750#endif
Bram Moolenaar21554412017-07-24 21:44:43 +02005751
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752 /* There is no _file_ when 'buftype' is "nofile", b_sfname
Bram Moolenaar21554412017-07-24 21:44:43 +02005753 * contains the name as specified by the user. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754 if (bt_nofile(buf))
5755 {
Bram Moolenaar21554412017-07-24 21:44:43 +02005756#ifdef FEAT_TERMINAL
5757 if (buf->b_term != NULL)
5758 return term_get_status_text(buf->b_term);
5759#endif
Bram Moolenaare561a7e2017-08-29 22:44:59 +02005760 if (buf->b_fname != NULL)
5761 return buf->b_fname;
Bram Moolenaar891e1fd2018-06-06 18:02:39 +02005762#ifdef FEAT_JOB_CHANNEL
5763 if (bt_prompt(buf))
5764 return (char_u *)_("[Prompt]");
5765#endif
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005766 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 }
Bram Moolenaar21554412017-07-24 21:44:43 +02005768
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005770 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 return NULL;
5772}
5773
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005774#if defined(FEAT_JOB_CHANNEL) \
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005775 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5776 || defined(PROTO)
Bram Moolenaar6b7355a2017-08-04 21:37:54 +02005777# define SWITCH_TO_WIN
5778
5779/*
5780 * Find a window that contains "buf" and switch to it.
5781 * If there is no such window, use the current window and change "curbuf".
5782 * Caller must initialize save_curbuf to NULL.
5783 * restore_win_for_buf() MUST be called later!
5784 */
5785 void
5786switch_to_win_for_buf(
5787 buf_T *buf,
5788 win_T **save_curwinp,
5789 tabpage_T **save_curtabp,
5790 bufref_T *save_curbuf)
5791{
5792 win_T *wp;
5793 tabpage_T *tp;
5794
5795 if (find_win_for_buf(buf, &wp, &tp) == FAIL)
5796 switch_buffer(save_curbuf, buf);
5797 else if (switch_win(save_curwinp, save_curtabp, wp, tp, TRUE) == FAIL)
5798 {
5799 restore_win(*save_curwinp, *save_curtabp, TRUE);
5800 switch_buffer(save_curbuf, buf);
5801 }
5802}
5803
5804 void
5805restore_win_for_buf(
5806 win_T *save_curwin,
5807 tabpage_T *save_curtab,
5808 bufref_T *save_curbuf)
5809{
5810 if (save_curbuf->br_buf == NULL)
5811 restore_win(save_curwin, save_curtab, TRUE);
5812 else
5813 restore_buffer(save_curbuf);
5814}
5815#endif
5816
Bram Moolenaar4033c552017-09-16 20:54:51 +02005817#if defined(FEAT_QUICKFIX) || defined(SWITCH_TO_WIN) || defined(PROTO)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005818/*
5819 * Find a window for buffer "buf".
5820 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5821 * If not found FAIL is returned.
5822 */
5823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005824find_win_for_buf(
5825 buf_T *buf,
5826 win_T **wp,
5827 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005828{
5829 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5830 if ((*wp)->w_buffer == buf)
5831 goto win_found;
5832 return FAIL;
5833win_found:
5834 return OK;
5835}
5836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837
5838#if defined(FEAT_SIGNS) || defined(PROTO)
5839/*
5840 * Insert the sign into the signlist.
5841 */
5842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005843insert_sign(
5844 buf_T *buf, /* buffer to store sign in */
5845 signlist_T *prev, /* previous sign entry */
5846 signlist_T *next, /* next sign entry */
5847 int id, /* sign ID */
5848 linenr_T lnum, /* line number which gets the mark */
5849 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850{
5851 signlist_T *newsign;
5852
5853 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5854 if (newsign != NULL)
5855 {
5856 newsign->id = id;
5857 newsign->lnum = lnum;
5858 newsign->typenr = typenr;
5859 newsign->next = next;
5860#ifdef FEAT_NETBEANS_INTG
5861 newsign->prev = prev;
5862 if (next != NULL)
5863 next->prev = newsign;
5864#endif
5865
5866 if (prev == NULL)
5867 {
5868 /* When adding first sign need to redraw the windows to create the
5869 * column for signs. */
5870 if (buf->b_signlist == NULL)
5871 {
5872 redraw_buf_later(buf, NOT_VALID);
5873 changed_cline_bef_curs();
5874 }
5875
5876 /* first sign in signlist */
5877 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005878#ifdef FEAT_NETBEANS_INTG
5879 if (netbeans_active())
5880 buf->b_has_sign_column = TRUE;
5881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 }
5883 else
5884 prev->next = newsign;
5885 }
5886}
5887
5888/*
5889 * Add the sign into the signlist. Find the right spot to do it though.
5890 */
5891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005892buf_addsign(
5893 buf_T *buf, /* buffer to store sign in */
5894 int id, /* sign ID */
5895 linenr_T lnum, /* line number which gets the mark */
5896 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897{
5898 signlist_T *sign; /* a sign in the signlist */
5899 signlist_T *prev; /* the previous sign */
5900
5901 prev = NULL;
5902 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5903 {
5904 if (lnum == sign->lnum && id == sign->id)
5905 {
5906 sign->typenr = typenr;
5907 return;
5908 }
5909 else if (
5910#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5911 id < 0 &&
5912#endif
5913 lnum < sign->lnum)
5914 {
5915#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5916 /* XXX - GRP: Is this because of sign slide problem? Or is it
5917 * really needed? Or is it because we allow multiple signs per
5918 * line? If so, should I add that feature to FEAT_SIGNS?
5919 */
5920 while (prev != NULL && prev->lnum == lnum)
5921 prev = prev->prev;
5922 if (prev == NULL)
5923 sign = buf->b_signlist;
5924 else
5925 sign = prev->next;
5926#endif
5927 insert_sign(buf, prev, sign, id, lnum, typenr);
5928 return;
5929 }
5930 prev = sign;
5931 }
5932#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5933 /* XXX - GRP: See previous comment */
5934 while (prev != NULL && prev->lnum == lnum)
5935 prev = prev->prev;
5936 if (prev == NULL)
5937 sign = buf->b_signlist;
5938 else
5939 sign = prev->next;
5940#endif
5941 insert_sign(buf, prev, sign, id, lnum, typenr);
5942
5943 return;
5944}
5945
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005946/*
5947 * For an existing, placed sign "markId" change the type to "typenr".
5948 * Returns the line number of the sign, or zero if the sign is not found.
5949 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005950 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005951buf_change_sign_type(
5952 buf_T *buf, /* buffer to store sign in */
5953 int markId, /* sign ID */
5954 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955{
5956 signlist_T *sign; /* a sign in the signlist */
5957
5958 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5959 {
5960 if (sign->id == markId)
5961 {
5962 sign->typenr = typenr;
5963 return sign->lnum;
5964 }
5965 }
5966
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005967 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005968}
5969
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005970 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005971buf_getsigntype(
5972 buf_T *buf,
5973 linenr_T lnum,
5974 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975{
5976 signlist_T *sign; /* a sign in a b_signlist */
5977
5978 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5979 if (sign->lnum == lnum
5980 && (type == SIGN_ANY
5981# ifdef FEAT_SIGN_ICONS
5982 || (type == SIGN_ICON
5983 && sign_get_image(sign->typenr) != NULL)
5984# endif
5985 || (type == SIGN_TEXT
5986 && sign_get_text(sign->typenr) != NULL)
5987 || (type == SIGN_LINEHL
5988 && sign_get_attr(sign->typenr, TRUE) != 0)))
5989 return sign->typenr;
5990 return 0;
5991}
5992
5993
5994 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005995buf_delsign(
5996 buf_T *buf, /* buffer sign is stored in */
5997 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998{
5999 signlist_T **lastp; /* pointer to pointer to current sign */
6000 signlist_T *sign; /* a sign in a b_signlist */
6001 signlist_T *next; /* the next sign in a b_signlist */
6002 linenr_T lnum; /* line number whose sign was deleted */
6003
6004 lastp = &buf->b_signlist;
6005 lnum = 0;
6006 for (sign = buf->b_signlist; sign != NULL; sign = next)
6007 {
6008 next = sign->next;
6009 if (sign->id == id)
6010 {
6011 *lastp = next;
6012#ifdef FEAT_NETBEANS_INTG
6013 if (next != NULL)
6014 next->prev = sign->prev;
6015#endif
6016 lnum = sign->lnum;
6017 vim_free(sign);
6018 break;
6019 }
6020 else
6021 lastp = &sign->next;
6022 }
6023
6024 /* When deleted the last sign need to redraw the windows to remove the
6025 * sign column. */
6026 if (buf->b_signlist == NULL)
6027 {
6028 redraw_buf_later(buf, NOT_VALID);
6029 changed_cline_bef_curs();
6030 }
6031
6032 return lnum;
6033}
6034
6035
6036/*
6037 * Find the line number of the sign with the requested id. If the sign does
6038 * not exist, return 0 as the line number. This will still let the correct file
6039 * get loaded.
6040 */
6041 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042buf_findsign(
6043 buf_T *buf, /* buffer to store sign in */
6044 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045{
6046 signlist_T *sign; /* a sign in the signlist */
6047
6048 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6049 if (sign->id == id)
6050 return sign->lnum;
6051
6052 return 0;
6053}
6054
6055 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006056buf_findsign_id(
6057 buf_T *buf, /* buffer whose sign we are searching for */
6058 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059{
6060 signlist_T *sign; /* a sign in the signlist */
6061
6062 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6063 if (sign->lnum == lnum)
6064 return sign->id;
6065
6066 return 0;
6067}
6068
6069
6070# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
6071/* see if a given type of sign exists on a specific line */
6072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073buf_findsigntype_id(
6074 buf_T *buf, /* buffer whose sign we are searching for */
6075 linenr_T lnum, /* line number of sign */
6076 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 signlist_T *sign; /* a sign in the signlist */
6079
6080 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6081 if (sign->lnum == lnum && sign->typenr == typenr)
6082 return sign->id;
6083
6084 return 0;
6085}
6086
6087
6088# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
6089/* return the number of icons on the given line */
6090 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092{
6093 signlist_T *sign; /* a sign in the signlist */
6094 int count = 0;
6095
6096 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
6097 if (sign->lnum == lnum)
6098 if (sign_get_image(sign->typenr) != NULL)
6099 count++;
6100
6101 return count;
6102}
6103# endif /* FEAT_SIGN_ICONS */
6104# endif /* FEAT_NETBEANS_INTG */
6105
6106
6107/*
6108 * Delete signs in buffer "buf".
6109 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02006110 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112{
6113 signlist_T *next;
6114
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006115 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02006116 * sign column. Not when curwin is NULL (this means we're exiting). */
6117 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02006118 {
6119 redraw_buf_later(buf, NOT_VALID);
6120 changed_cline_bef_curs();
6121 }
6122
Bram Moolenaar071d4272004-06-13 20:20:40 +00006123 while (buf->b_signlist != NULL)
6124 {
6125 next = buf->b_signlist->next;
6126 vim_free(buf->b_signlist);
6127 buf->b_signlist = next;
6128 }
6129}
6130
6131/*
6132 * Delete all signs in all buffers.
6133 */
6134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006135buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136{
6137 buf_T *buf; /* buffer we are checking for signs */
6138
Bram Moolenaar29323592016-07-24 22:04:11 +02006139 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006140 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142}
6143
6144/*
6145 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6146 */
6147 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006148sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149{
6150 buf_T *buf;
6151 signlist_T *p;
6152 char lbuf[BUFSIZ];
6153
6154 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6155 msg_putchar('\n');
6156 if (rbuf == NULL)
6157 buf = firstbuf;
6158 else
6159 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006160 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006161 {
6162 if (buf->b_signlist != NULL)
6163 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006164 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006165 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006166 msg_putchar('\n');
6167 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006168 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006170 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6172 MSG_PUTS(lbuf);
6173 msg_putchar('\n');
6174 }
6175 if (rbuf != NULL)
6176 break;
6177 buf = buf->b_next;
6178 }
6179}
6180
6181/*
6182 * Adjust a placed sign for inserted/deleted lines.
6183 */
6184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006185sign_mark_adjust(
6186 linenr_T line1,
6187 linenr_T line2,
6188 long amount,
6189 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006190{
6191 signlist_T *sign; /* a sign in a b_signlist */
6192
6193 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6194 {
6195 if (sign->lnum >= line1 && sign->lnum <= line2)
6196 {
6197 if (amount == MAXLNUM)
6198 sign->lnum = line1;
6199 else
6200 sign->lnum += amount;
6201 }
6202 else if (sign->lnum > line2)
6203 sign->lnum += amount_after;
6204 }
6205}
6206#endif /* FEAT_SIGNS */
6207
6208/*
6209 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6210 */
6211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006212set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006213{
6214 if (on != curbuf->b_p_bl)
6215 {
6216 curbuf->b_p_bl = on;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006217 if (on)
6218 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6219 else
6220 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006221 }
6222}
6223
6224/*
6225 * Read the file for "buf" again and check if the contents changed.
6226 * Return TRUE if it changed or this could not be checked.
6227 */
6228 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006229buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230{
6231 buf_T *newbuf;
6232 int differ = TRUE;
6233 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 exarg_T ea;
6236
6237 /* Allocate a buffer without putting it in the buffer list. */
6238 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6239 if (newbuf == NULL)
6240 return TRUE;
6241
6242 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6243 if (prep_exarg(&ea, buf) == FAIL)
6244 {
6245 wipe_buffer(newbuf, FALSE);
6246 return TRUE;
6247 }
6248
Bram Moolenaar071d4272004-06-13 20:20:40 +00006249 /* set curwin/curbuf to buf and save a few things */
6250 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006251
Bram Moolenaar4770d092006-01-12 23:22:24 +00006252 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006253 && readfile(buf->b_ffname, buf->b_fname,
6254 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6255 &ea, READ_NEW | READ_DUMMY) == OK)
6256 {
6257 /* compare the two files line by line */
6258 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6259 {
6260 differ = FALSE;
6261 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6262 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6263 {
6264 differ = TRUE;
6265 break;
6266 }
6267 }
6268 }
6269 vim_free(ea.cmd);
6270
Bram Moolenaar071d4272004-06-13 20:20:40 +00006271 /* restore curwin/curbuf and a few other things */
6272 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273
6274 if (curbuf != newbuf) /* safety check */
6275 wipe_buffer(newbuf, FALSE);
6276
6277 return differ;
6278}
6279
6280/*
6281 * Wipe out a buffer and decrement the last buffer number if it was used for
6282 * this buffer. Call this to wipe out a temp buffer that does not contain any
6283 * marks.
6284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006286wipe_buffer(
6287 buf_T *buf,
6288 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006289{
6290 if (buf->b_fnum == top_file_num - 1)
6291 --top_file_num;
6292
Bram Moolenaar071d4272004-06-13 20:20:40 +00006293 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006294 block_autocmds();
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006295
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006296 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaarf2bd8ef2018-03-04 18:08:14 +01006297
Bram Moolenaar071d4272004-06-13 20:20:40 +00006298 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006299 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006300}