blob: 2f574e03d7b2b9887dc3f2adfb62a1fb26412563 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
36static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020038static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
40static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar7fd73202010-07-25 16:58:46 +020062#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
63static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020070/* Number of times free_buffer() was called. */
71static int buf_free_count = 0;
72
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020073/* Read data from buffer for retrying. */
74 static int
75read_buffer(
76 int read_stdin, /* read file from stdin, otherwise fifo */
77 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
78 int flags) /* extra flags for readfile() */
79{
80 int retval = OK;
81 linenr_T line_count;
82
83 /*
84 * Read from the buffer which the text is already filled in and append at
85 * the end. This makes it possible to retry when 'fileformat' or
86 * 'fileencoding' was guessed wrong.
87 */
88 line_count = curbuf->b_ml.ml_line_count;
89 retval = readfile(
90 read_stdin ? NULL : curbuf->b_ffname,
91 read_stdin ? NULL : curbuf->b_fname,
92 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
93 flags | READ_BUFFER);
94 if (retval == OK)
95 {
96 /* Delete the binary lines. */
97 while (--line_count >= 0)
98 ml_delete((linenr_T)1, FALSE);
99 }
100 else
101 {
102 /* Delete the converted lines. */
103 while (curbuf->b_ml.ml_line_count > line_count)
104 ml_delete(line_count, FALSE);
105 }
106 /* Put the cursor on the first line. */
107 curwin->w_cursor.lnum = 1;
108 curwin->w_cursor.col = 0;
109
110 if (read_stdin)
111 {
112 /* Set or reset 'modified' before executing autocommands, so that
113 * it can be changed there. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +0100114 if (!readonlymode && !BUFEMPTY())
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200115 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100116 else if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200117 unchanged(curbuf, FALSE);
118
119#ifdef FEAT_AUTOCMD
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100120 if (retval == OK)
121 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200122# ifdef FEAT_EVAL
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100123 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200124 curbuf, &retval);
125# else
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100126 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200127# endif
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100128 }
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200129#endif
130 }
131 return retval;
132}
133
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200135 * Open current buffer, that is: open the memfile and read the file into
136 * memory.
137 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138 */
139 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100140open_buffer(
141 int read_stdin, /* read file from stdin */
142 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
143 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144{
145 int retval = OK;
146#ifdef FEAT_AUTOCMD
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200147 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100149#ifdef FEAT_SYN_HL
150 long old_tw = curbuf->b_p_tw;
151#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200152 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154 /*
155 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
156 * When re-entering the same buffer, it should not change, because the
157 * user may have reset the flag by hand.
158 */
159 if (readonlymode && curbuf->b_ffname != NULL
160 && (curbuf->b_flags & BF_NEVERLOADED))
161 curbuf->b_p_ro = TRUE;
162
Bram Moolenaar4770d092006-01-12 23:22:24 +0000163 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 {
165 /*
166 * There MUST be a memfile, otherwise we can't do anything
167 * If we can't create one for the current buffer, take another buffer
168 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100169 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200170 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171 if (curbuf->b_ml.ml_mfp != NULL)
172 break;
173 /*
174 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000175 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 */
177 if (curbuf == NULL)
178 {
179 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
180 getout(2);
181 }
182 EMSG(_("E83: Cannot allocate buffer, using other one..."));
183 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100184#ifdef FEAT_SYN_HL
185 if (old_tw != curbuf->b_p_tw)
186 check_colorcolumn(curwin);
187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188 return FAIL;
189 }
190
191#ifdef FEAT_AUTOCMD
192 /* The autocommands in readfile() may change the buffer, but only AFTER
193 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200194 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 modified_was_set = FALSE;
196#endif
197
198 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100199 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200
201 if (curbuf->b_ffname != NULL
202#ifdef FEAT_NETBEANS_INTG
203 && netbeansReadFile
204#endif
205 )
206 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100207 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200208#ifdef UNIX
209 int save_bin = curbuf->b_p_bin;
210 int perm;
211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212#ifdef FEAT_NETBEANS_INTG
213 int oldFire = netbeansFireChanges;
214
215 netbeansFireChanges = 0;
216#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200217#ifdef UNIX
218 perm = mch_getperm(curbuf->b_ffname);
219 if (perm >= 0 && (0
220# ifdef S_ISFIFO
221 || S_ISFIFO(perm)
222# endif
223# ifdef S_ISSOCK
224 || S_ISSOCK(perm)
225# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200226# ifdef OPEN_CHR_FILES
227 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
228# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200229 ))
230 read_fifo = TRUE;
231 if (read_fifo)
232 curbuf->b_p_bin = TRUE;
233#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100234 if (shortmess(SHM_FILEINFO))
235 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200237 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200238 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
239#ifdef UNIX
240 if (read_fifo)
241 {
242 curbuf->b_p_bin = save_bin;
243 if (retval == OK)
244 retval = read_buffer(FALSE, eap, flags);
245 }
246#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100247 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_NETBEANS_INTG
249 netbeansFireChanges = oldFire;
250#endif
251 /* Help buffer is filtered. */
252 if (curbuf->b_help)
253 fix_help_buffer();
254 }
255 else if (read_stdin)
256 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200257 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258
259 /*
260 * First read the text in binary mode into the buffer.
261 * Then read from that same buffer and append at the end. This makes
262 * it possible to retry when 'fileformat' or 'fileencoding' was
263 * guessed wrong.
264 */
265 curbuf->b_p_bin = TRUE;
266 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200267 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
268 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 curbuf->b_p_bin = save_bin;
270 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200271 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272 }
273
274 /* if first time loading this buffer, init b_chartab[] */
275 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100276 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100278#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100279 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100280#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283 /*
284 * Set/reset the Changed flag first, autocmds may change the buffer.
285 * Apply the automatic commands, before processing the modelines.
286 * So the modelines have priority over auto commands.
287 */
288 /* When reading stdin, the buffer contents always needs writing, so set
289 * the changed flag. Unless in readonly mode: "ls | gview -".
290 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000291 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#ifdef FEAT_AUTOCMD
293 || modified_was_set /* ":set modified" used in autocmd */
294# ifdef FEAT_EVAL
295 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
296# endif
297#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 changed();
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100300 else if (retval == OK && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000301 unchanged(curbuf, FALSE);
302 save_file_ff(curbuf); /* keep this fileformat */
303
304 /* require "!" to overwrite the file, because it wasn't read completely */
305#ifdef FEAT_EVAL
306 if (aborting())
307#else
308 if (got_int)
309#endif
310 curbuf->b_flags |= BF_READERR;
311
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000312#ifdef FEAT_FOLDING
313 /* Need to update automatic folding. Do this before the autocommands,
314 * they may use the fold info. */
315 foldUpdateAll(curwin);
316#endif
317
Bram Moolenaar071d4272004-06-13 20:20:40 +0000318#ifdef FEAT_AUTOCMD
319 /* need to set w_topline, unless some autocommand already did that. */
320 if (!(curwin->w_valid & VALID_TOPLINE))
321 {
322 curwin->w_topline = 1;
323# ifdef FEAT_DIFF
324 curwin->w_topfill = 0;
325# endif
326 }
327# ifdef FEAT_EVAL
328 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
329# else
330 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
331# endif
332#endif
333
Bram Moolenaare13b9af2017-01-13 22:01:02 +0100334 if (retval == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 {
336#ifdef FEAT_AUTOCMD
337 /*
338 * The autocommands may have changed the current buffer. Apply the
339 * modelines to the correct buffer, if it still exists and is loaded.
340 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200341 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 aco_save_T aco;
344
345 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200346 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000348 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000349 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
350
351#ifdef FEAT_AUTOCMD
352# ifdef FEAT_EVAL
353 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
354 &retval);
355# else
356 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
357# endif
358
359 /* restore curwin/curbuf and a few other things */
360 aucmd_restbuf(&aco);
361 }
362#endif
363 }
364
Bram Moolenaar071d4272004-06-13 20:20:40 +0000365 return retval;
366}
367
368/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200369 * Store "buf" in "bufref" and set the free count.
370 */
371 void
372set_bufref(bufref_T *bufref, buf_T *buf)
373{
374 bufref->br_buf = buf;
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200375 bufref->br_fnum = buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200376 bufref->br_buf_free_count = buf_free_count;
377}
378
379/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200380 * Return TRUE if "bufref->br_buf" points to the same buffer as when
381 * set_bufref() was called and it is a valid buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200382 * Only goes through the buffer list if buf_free_count changed.
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200383 * Also checks if b_fnum is still the same, a :bwipe followed by :new might get
384 * the same allocated memory, but it's a different buffer.
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200385 */
386 int
387bufref_valid(bufref_T *bufref)
388{
389 return bufref->br_buf_free_count == buf_free_count
Bram Moolenaar45e5fd12017-06-04 14:58:02 +0200390 ? TRUE : buf_valid(bufref->br_buf)
391 && bufref->br_fnum == bufref->br_buf->b_fnum;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200392}
393
394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200396 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000397 */
398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100399buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400{
401 buf_T *bp;
402
Bram Moolenaar82404332016-07-10 17:00:38 +0200403 /* Assume that we more often have a recent buffer, start with the last
404 * one. */
405 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 if (bp == buf)
407 return TRUE;
408 return FALSE;
409}
410
411/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200412 * A hash table used to quickly lookup a buffer by its number.
413 */
414static hashtab_T buf_hashtab;
415
416 static void
417buf_hashtab_add(buf_T *buf)
418{
419 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
420 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
421 EMSG(_("E931: Buffer cannot be registered"));
422}
423
424 static void
425buf_hashtab_remove(buf_T *buf)
426{
427 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
428
429 if (!HASHITEM_EMPTY(hi))
430 hash_remove(&buf_hashtab, hi);
431}
432
433/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 * Close the link to a buffer.
435 * "action" is used when there is no longer a window for the buffer.
436 * It can be:
437 * 0 buffer becomes hidden
438 * DOBUF_UNLOAD buffer is unloaded
439 * DOBUF_DELETE buffer is unloaded and removed from buffer list
440 * DOBUF_WIPE buffer is unloaded and really deleted
441 * When doing all but the first one on the current buffer, the caller should
442 * get a new buffer very soon!
443 *
444 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100445 *
446 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
447 * cause there to be only one window with this buffer. e.g. when ":quit" is
448 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 */
450 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100451close_buffer(
452 win_T *win, /* if not NULL, set b_last_cursor */
453 buf_T *buf,
454 int action,
455 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000456{
457#ifdef FEAT_AUTOCMD
458 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100459 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200460 bufref_T bufref;
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200461# ifdef FEAT_WINDOWS
Bram Moolenaar3a117e12016-10-30 21:57:52 +0100462 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200463 win_T *the_curwin = curwin;
464 tabpage_T *the_curtab = curtab;
465# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#endif
467 int unload_buf = (action != 0);
468 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
469 int wipe_buf = (action == DOBUF_WIPE);
470
471#ifdef FEAT_QUICKFIX
472 /*
473 * Force unloading or deleting when 'bufhidden' says so.
474 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
475 * "hide" (otherwise we could never free or delete a buffer).
476 */
477 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
478 {
479 del_buf = TRUE;
480 unload_buf = TRUE;
481 }
482 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
483 {
484 del_buf = TRUE;
485 unload_buf = TRUE;
486 wipe_buf = TRUE;
487 }
488 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
489 unload_buf = TRUE;
490#endif
491
Bram Moolenaar30180b82016-09-04 19:57:56 +0200492#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200493 /* Disallow deleting the buffer when it is locked (already being closed or
494 * halfway a command that relies on it). Unloading is allowed. */
495 if (buf->b_locked > 0 && (del_buf || wipe_buf))
496 {
497 EMSG(_("E937: Attempt to delete a buffer that is in use"));
498 return;
499 }
Bram Moolenaar30180b82016-09-04 19:57:56 +0200500#endif
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200501
Bram Moolenaar3be85852014-06-12 14:01:31 +0200502 if (win != NULL
503#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200504 && win_valid_any_tab(win) /* in case autocommands closed the window */
Bram Moolenaar3be85852014-06-12 14:01:31 +0200505#endif
506 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 {
508 /* Set b_last_cursor when closing the last window for the buffer.
509 * Remember the last cursor position and window options of the buffer.
510 * This used to be only for the current window, but then options like
511 * 'foldmethod' may be lost with a ":only" command. */
512 if (buf->b_nwindows == 1)
513 set_last_cursor(win);
514 buflist_setfpos(buf, win,
515 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
516 win->w_cursor.col, TRUE);
517 }
518
519#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200520 set_bufref(&bufref, buf);
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 /* When the buffer is no longer in a window, trigger BufWinLeave */
523 if (buf->b_nwindows == 1)
524 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200525 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200526 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
527 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200528 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100529 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200530 /* Autocommands deleted the buffer. */
531aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100532 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100534 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200535 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200536 if (abort_if_last && one_window())
537 /* Autocommands made this the only window. */
538 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539
540 /* When the buffer becomes hidden, but is not unloaded, trigger
541 * BufHidden */
542 if (!unload_buf)
543 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200544 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200545 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
546 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200547 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200548 /* Autocommands deleted the buffer. */
549 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200550 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200551 if (abort_if_last && one_window())
552 /* Autocommands made this the only window. */
553 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 }
555# ifdef FEAT_EVAL
556 if (aborting()) /* autocmds may abort script processing */
557 return;
558# endif
559 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200560
561# ifdef FEAT_WINDOWS
562 /* If the buffer was in curwin and the window has changed, go back to that
563 * window, if it still exists. This avoids that ":edit x" triggering a
564 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
565 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
566 {
567 block_autocmds();
568 goto_tabpage_win(the_curtab, the_curwin);
569 unblock_autocmds();
570 }
571# endif
572
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 nwindows = buf->b_nwindows;
574#endif
575
576 /* decrease the link count from windows (unless not in any window) */
577 if (buf->b_nwindows > 0)
578 --buf->b_nwindows;
579
580 /* Return when a window is displaying the buffer or when it's not
581 * unloaded. */
582 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584
585 /* Always remove the buffer when there is no file name. */
586 if (buf->b_ffname == NULL)
587 del_buf = TRUE;
588
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200589 /* When closing the current buffer stop Visual mode before freeing
590 * anything. */
Bram Moolenaar4930a762016-09-11 14:39:53 +0200591 if (buf == curbuf && VIsual_active
Bram Moolenaar9a27c7f2016-09-09 12:57:09 +0200592#if defined(EXITFREE)
593 && !entered_free_all_mem
594#endif
595 )
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200596 end_visual_mode();
597
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 /*
599 * Free all things allocated for this buffer.
600 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
601 */
602#ifdef FEAT_AUTOCMD
603 /* Remember if we are closing the current buffer. Restore the number of
604 * windows, so that autocommands in buf_freeall() don't get confused. */
605 is_curbuf = (buf == curbuf);
606 buf->b_nwindows = nwindows;
607#endif
608
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200609 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611#ifdef FEAT_AUTOCMD
612 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200613 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614 return;
615# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000616 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 return;
618# endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 /*
621 * It's possible that autocommands change curbuf to the one being deleted.
622 * This might cause the previous curbuf to be deleted unexpectedly. But
623 * in some cases it's OK to delete the curbuf, because a new one is
624 * obtained anyway. Therefore only return if curbuf changed to the
625 * deleted buffer.
626 */
627 if (buf == curbuf && !is_curbuf)
628 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200629
630 if (
631#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200632 win_valid_any_tab(win) &&
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200633#else
634 win != NULL &&
635#endif
636 win->w_buffer == buf)
637 win->w_buffer = NULL; /* make sure we don't use the buffer now */
638
639 /* Autocommands may have opened or closed windows for this buffer.
640 * Decrement the count for the close we do here. */
641 if (buf->b_nwindows > 0)
642 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#endif
644
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000645 /* Change directories when the 'acd' option is set. */
646 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647
648 /*
649 * Remove the buffer from the list.
650 */
651 if (wipe_buf)
652 {
653#ifdef FEAT_SUN_WORKSHOP
654 if (usingSunWorkShop)
655 workshop_file_closed_lineno((char *)buf->b_ffname,
656 (int)buf->b_last_cursor.lnum);
657#endif
658 vim_free(buf->b_ffname);
659 vim_free(buf->b_sfname);
660 if (buf->b_prev == NULL)
661 firstbuf = buf->b_next;
662 else
663 buf->b_prev->b_next = buf->b_next;
664 if (buf->b_next == NULL)
665 lastbuf = buf->b_prev;
666 else
667 buf->b_next->b_prev = buf->b_prev;
668 free_buffer(buf);
669 }
670 else
671 {
672 if (del_buf)
673 {
674 /* Free all internal variables and reset option values, to make
675 * ":bdel" compatible with Vim 5.7. */
676 free_buffer_stuff(buf, TRUE);
677
678 /* Make it look like a new buffer. */
679 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
680
681 /* Init the options when loaded again. */
682 buf->b_p_initialized = FALSE;
683 }
684 buf_clear_file(buf);
685 if (del_buf)
686 buf->b_p_bl = FALSE;
687 }
688}
689
690/*
691 * Make buffer not contain a file.
692 */
693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100694buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695{
696 buf->b_ml.ml_line_count = 1;
697 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 buf->b_p_eol = TRUE;
700 buf->b_start_eol = TRUE;
701#ifdef FEAT_MBYTE
702 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000703 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704#endif
705 buf->b_ml.ml_mfp = NULL;
706 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
707#ifdef FEAT_NETBEANS_INTG
708 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#endif
710}
711
712/*
713 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200714 * the file. Careful: get here with "curwin" NULL when exiting.
715 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200716 * BFA_DEL buffer is going to be deleted
717 * BFA_WIPE buffer is going to be wiped out
718 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100721buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722{
723#ifdef FEAT_AUTOCMD
724 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200725 bufref_T bufref;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200726# ifdef FEAT_WINDOWS
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200727 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200728 win_T *the_curwin = curwin;
729 tabpage_T *the_curtab = curtab;
730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaar5a497892016-09-03 16:29:04 +0200732 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200733 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200734 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200735 if (buf->b_ml.ml_mfp != NULL)
736 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200737 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
738 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200739 && !bufref_valid(&bufref))
740 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200741 return;
742 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200743 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200745 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
746 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200747 && !bufref_valid(&bufref))
748 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 return;
750 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200751 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200753 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
754 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200755 && !bufref_valid(&bufref))
756 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757 return;
758 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200759 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200760
761# ifdef FEAT_WINDOWS
762 /* If the buffer was in curwin and the window has changed, go back to that
763 * window, if it still exists. This avoids that ":edit x" triggering a
764 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
765 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
766 {
767 block_autocmds();
768 goto_tabpage_win(the_curtab, the_curwin);
769 unblock_autocmds();
770 }
771# endif
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000774 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 return;
776# endif
777
778 /*
779 * It's possible that autocommands change curbuf to the one being deleted.
780 * This might cause curbuf to be deleted unexpectedly. But in some cases
781 * it's OK to delete the curbuf, because a new one is obtained anyway.
782 * Therefore only return if curbuf changed to the deleted buffer.
783 */
784 if (buf == curbuf && !is_curbuf)
785 return;
786#endif
787#ifdef FEAT_DIFF
788 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
789#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200790#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100791 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200792 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100793 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200794#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000795
796#ifdef FEAT_FOLDING
797 /* No folds in an empty buffer. */
798# ifdef FEAT_WINDOWS
799 {
800 win_T *win;
801 tabpage_T *tp;
802
803 FOR_ALL_TAB_WINDOWS(tp, win)
804 if (win->w_buffer == buf)
805 clearFolding(win);
806 }
807# else
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200808 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000809 clearFolding(curwin);
810# endif
811#endif
812
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#ifdef FEAT_TCL
814 tcl_buffer_free(buf);
815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 ml_close(buf, TRUE); /* close and delete the memline/memfile */
817 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200818 if ((flags & BFA_KEEP_UNDO) == 0)
819 {
820 u_blockfree(buf); /* free the memory allocated for undo */
821 u_clearall(buf); /* reset all undo information */
822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200824 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000826 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827}
828
829/*
830 * Free a buffer structure and the things it contains related to the buffer
831 * itself (not the file, that must have been done already).
832 */
833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100834free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200836 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200838#ifdef FEAT_EVAL
839 unref_var_dict(buf->b_vars);
840#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200841#ifdef FEAT_LUA
842 lua_buffer_free(buf);
843#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000844#ifdef FEAT_MZSCHEME
845 mzscheme_buffer_free(buf);
846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847#ifdef FEAT_PERL
848 perl_buf_free(buf);
849#endif
850#ifdef FEAT_PYTHON
851 python_buffer_free(buf);
852#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200853#ifdef FEAT_PYTHON3
854 python3_buffer_free(buf);
855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856#ifdef FEAT_RUBY
857 ruby_buffer_free(buf);
858#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200859#ifdef FEAT_JOB_CHANNEL
860 channel_buffer_free(buf);
861#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200862
863 buf_hashtab_remove(buf);
864
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200865#ifdef FEAT_AUTOCMD
866 aubuflocal_remove(buf);
867
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200868 if (autocmd_busy)
869 {
870 /* Do not free the buffer structure while autocommands are executing,
871 * it's still needed. Free it when autocmd_busy is reset. */
872 buf->b_next = au_pending_free_buf;
873 au_pending_free_buf = buf;
874 }
875 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000876#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200877 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878}
879
880/*
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100881 * Initializes b:changedtick.
Bram Moolenaar79518e22017-02-17 16:31:35 +0100882 */
883 static void
884init_changedtick(buf_T *buf)
885{
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100886 dictitem_T *di = (dictitem_T *)&buf->b_ct_di;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100887
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100888 di->di_flags = DI_FLAGS_FIX | DI_FLAGS_RO;
889 di->di_tv.v_type = VAR_NUMBER;
890 di->di_tv.v_lock = VAR_FIXED;
891 di->di_tv.vval.v_number = 0;
892
Bram Moolenaar92769c32017-02-25 15:41:37 +0100893#ifdef FEAT_EVAL
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100894 STRCPY(buf->b_ct_di.di_key, "changedtick");
895 (void)dict_add(buf->b_vars, di);
Bram Moolenaar92769c32017-02-25 15:41:37 +0100896#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +0100897}
898
899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
901 */
902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100903free_buffer_stuff(
904 buf_T *buf,
905 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
907 if (free_options)
908 {
909 clear_wininfo(buf); /* including window-local options */
910 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200911#ifdef FEAT_SPELL
912 ga_clear(&buf->b_s.b_langp);
913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914 }
915#ifdef FEAT_EVAL
Bram Moolenaar79518e22017-02-17 16:31:35 +0100916 {
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100917 varnumber_T tick = CHANGEDTICK(buf);
Bram Moolenaar79518e22017-02-17 16:31:35 +0100918
919 vars_clear(&buf->b_vars->dv_hashtab); /* free all buffer variables */
920 hash_init(&buf->b_vars->dv_hashtab);
921 init_changedtick(buf);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100922 CHANGEDTICK(buf) = tick;
Bram Moolenaar79518e22017-02-17 16:31:35 +0100923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924#endif
925#ifdef FEAT_USR_CMDS
926 uc_clear(&buf->b_ucmds); /* clear local user commands */
927#endif
928#ifdef FEAT_SIGNS
929 buf_delete_signs(buf); /* delete any signs */
930#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000931#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200932 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934#ifdef FEAT_LOCALMAP
935 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
936 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
937#endif
938#ifdef FEAT_MBYTE
939 vim_free(buf->b_start_fenc);
940 buf->b_start_fenc = NULL;
941#endif
942}
943
944/*
945 * Free the b_wininfo list for buffer "buf".
946 */
947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100948clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949{
950 wininfo_T *wip;
951
952 while (buf->b_wininfo != NULL)
953 {
954 wip = buf->b_wininfo;
955 buf->b_wininfo = wip->wi_next;
956 if (wip->wi_optset)
957 {
958 clear_winopt(&wip->wi_opt);
959#ifdef FEAT_FOLDING
960 deleteFoldRecurse(&wip->wi_folds);
961#endif
962 }
963 vim_free(wip);
964 }
965}
966
967#if defined(FEAT_LISTCMDS) || defined(PROTO)
968/*
969 * Go to another buffer. Handles the result of the ATTENTION dialog.
970 */
971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100972goto_buffer(
973 exarg_T *eap,
974 int start,
975 int dir,
976 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000978# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200979 bufref_T old_curbuf;
980
981 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982
983 swap_exists_action = SEA_DIALOG;
984# endif
985 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
986 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000987# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
989 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000990# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
991 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000992
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000993 /* Reset the error/interrupt/exception state here so that
994 * aborting() returns FALSE when closing a window. */
995 enter_cleanup(&cs);
996# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000997
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000998 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 win_close(curwin, TRUE);
1000 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001001 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001002
1003# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1004 /* Restore the error/interrupt/exception state if not discarded by a
1005 * new aborting error, interrupt, or uncaught exception. */
1006 leave_cleanup(&cs);
1007# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
1009 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001010 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011# endif
1012}
1013#endif
1014
Bram Moolenaare64ac772005-12-07 20:54:59 +00001015#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016/*
1017 * Handle the situation of swap_exists_action being set.
1018 * It is allowed for "old_curbuf" to be NULL or invalid.
1019 */
1020 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001021handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001022{
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001023# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1024 cleanup_T cs;
1025# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001026#ifdef FEAT_SYN_HL
1027 long old_tw = curbuf->b_p_tw;
1028#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001029 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00001030
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 if (swap_exists_action == SEA_QUIT)
1032 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001033# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1034 /* Reset the error/interrupt/exception state here so that
1035 * aborting() returns FALSE when closing a buffer. */
1036 enter_cleanup(&cs);
1037# endif
1038
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 /* User selected Quit at ATTENTION prompt. Go back to previous
1040 * buffer. If that buffer is gone or the same as the current one,
1041 * open a new, empty buffer. */
1042 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001043 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001044 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001045 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1046 || old_curbuf->br_buf == curbuf)
1047 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1048 else
1049 buf = old_curbuf->br_buf;
1050 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001051 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001052 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001053#ifdef FEAT_SYN_HL
1054 if (old_tw != curbuf->b_p_tw)
1055 check_colorcolumn(curwin);
1056#endif
1057 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001059
1060# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1061 /* Restore the error/interrupt/exception state if not discarded by a
1062 * new aborting error, interrupt, or uncaught exception. */
1063 leave_cleanup(&cs);
1064# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 }
1066 else if (swap_exists_action == SEA_RECOVER)
1067 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001068# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1069 /* Reset the error/interrupt/exception state here so that
1070 * aborting() returns FALSE when closing a buffer. */
1071 enter_cleanup(&cs);
1072# endif
1073
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 /* User selected Recover at ATTENTION prompt. */
1075 msg_scroll = TRUE;
1076 ml_recover();
1077 MSG_PUTS("\n"); /* don't overwrite the last message */
1078 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001079 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001080
1081# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1082 /* Restore the error/interrupt/exception state if not discarded by a
1083 * new aborting error, interrupt, or uncaught exception. */
1084 leave_cleanup(&cs);
1085# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
1087 swap_exists_action = SEA_NONE;
1088}
1089#endif
1090
1091#if defined(FEAT_LISTCMDS) || defined(PROTO)
1092/*
1093 * do_bufdel() - delete or unload buffer(s)
1094 *
1095 * addr_count == 0: ":bdel" - delete current buffer
1096 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1097 * buffer "end_bnr", then any other arguments.
1098 * addr_count == 2: ":N,N bdel" - delete buffers in range
1099 *
1100 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1101 * DOBUF_DEL (":bdel")
1102 *
1103 * Returns error message or NULL
1104 */
1105 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001106do_bufdel(
1107 int command,
1108 char_u *arg, /* pointer to extra arguments */
1109 int addr_count,
1110 int start_bnr, /* first buffer number in a range */
1111 int end_bnr, /* buffer nr or last buffer nr in a range */
1112 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 int do_current = 0; /* delete current buffer? */
1115 int deleted = 0; /* number of buffers deleted */
1116 char_u *errormsg = NULL; /* return value */
1117 int bnr; /* buffer number */
1118 char_u *p;
1119
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 if (addr_count == 0)
1121 {
1122 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1123 }
1124 else
1125 {
1126 if (addr_count == 2)
1127 {
1128 if (*arg) /* both range and argument is not allowed */
1129 return (char_u *)_(e_trailing);
1130 bnr = start_bnr;
1131 }
1132 else /* addr_count == 1 */
1133 bnr = end_bnr;
1134
1135 for ( ;!got_int; ui_breakcheck())
1136 {
1137 /*
1138 * delete the current buffer last, otherwise when the
1139 * current buffer is deleted, the next buffer becomes
1140 * the current one and will be loaded, which may then
1141 * also be deleted, etc.
1142 */
1143 if (bnr == curbuf->b_fnum)
1144 do_current = bnr;
1145 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1146 forceit) == OK)
1147 ++deleted;
1148
1149 /*
1150 * find next buffer number to delete/unload
1151 */
1152 if (addr_count == 2)
1153 {
1154 if (++bnr > end_bnr)
1155 break;
1156 }
1157 else /* addr_count == 1 */
1158 {
1159 arg = skipwhite(arg);
1160 if (*arg == NUL)
1161 break;
1162 if (!VIM_ISDIGIT(*arg))
1163 {
1164 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001165 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1166 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 if (bnr < 0) /* failed */
1168 break;
1169 arg = p;
1170 }
1171 else
1172 bnr = getdigits(&arg);
1173 }
1174 }
1175 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1176 FORWARD, do_current, forceit) == OK)
1177 ++deleted;
1178
1179 if (deleted == 0)
1180 {
1181 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001182 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001184 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001186 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 errormsg = IObuff;
1188 }
1189 else if (deleted >= p_report)
1190 {
1191 if (command == DOBUF_UNLOAD)
1192 {
1193 if (deleted == 1)
1194 MSG(_("1 buffer unloaded"));
1195 else
1196 smsg((char_u *)_("%d buffers unloaded"), deleted);
1197 }
1198 else if (command == DOBUF_DEL)
1199 {
1200 if (deleted == 1)
1201 MSG(_("1 buffer deleted"));
1202 else
1203 smsg((char_u *)_("%d buffers deleted"), deleted);
1204 }
1205 else
1206 {
1207 if (deleted == 1)
1208 MSG(_("1 buffer wiped out"));
1209 else
1210 smsg((char_u *)_("%d buffers wiped out"), deleted);
1211 }
1212 }
1213 }
1214
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216 return errormsg;
1217}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001218#endif /* FEAT_LISTCMDS */
1219
1220#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1221 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001223static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001224
1225/*
1226 * Make the current buffer empty.
1227 * Used when it is wiped out and it's the last buffer.
1228 */
1229 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001230empty_curbuf(
1231 int close_others,
1232 int forceit,
1233 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001234{
1235 int retval;
1236 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001237 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001238
1239 if (action == DOBUF_UNLOAD)
1240 {
1241 EMSG(_("E90: Cannot unload last buffer"));
1242 return FAIL;
1243 }
1244
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001245 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001246#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001247 if (close_others)
1248 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001249 close_windows(buf, TRUE);
1250#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001251
1252 setpcmark();
1253 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1254 forceit ? ECMD_FORCEIT : 0, curwin);
1255
1256 /*
1257 * do_ecmd() may create a new buffer, then we have to delete
1258 * the old one. But do_ecmd() may have done that already, check
1259 * if the buffer still exists.
1260 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001261 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001262 close_buffer(NULL, buf, action, FALSE);
1263 if (!close_others)
1264 need_fileinfo = FALSE;
1265 return retval;
1266}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267/*
1268 * Implementation of the commands for the buffer list.
1269 *
1270 * action == DOBUF_GOTO go to specified buffer
1271 * action == DOBUF_SPLIT split window and go to specified buffer
1272 * action == DOBUF_UNLOAD unload specified buffer(s)
1273 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1274 * action == DOBUF_WIPE delete specified buffer(s) really
1275 *
1276 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1277 * start == DOBUF_FIRST go to "count" buffer from first buffer
1278 * start == DOBUF_LAST go to "count" buffer from last buffer
1279 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1280 *
1281 * Return FAIL or OK.
1282 */
1283 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001284do_buffer(
1285 int action,
1286 int start,
1287 int dir, /* FORWARD or BACKWARD */
1288 int count, /* buffer number or number of buffers */
1289 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290{
1291 buf_T *buf;
1292 buf_T *bp;
1293 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1294 || action == DOBUF_WIPE);
1295
1296 switch (start)
1297 {
1298 case DOBUF_FIRST: buf = firstbuf; break;
1299 case DOBUF_LAST: buf = lastbuf; break;
1300 default: buf = curbuf; break;
1301 }
1302 if (start == DOBUF_MOD) /* find next modified buffer */
1303 {
1304 while (count-- > 0)
1305 {
1306 do
1307 {
1308 buf = buf->b_next;
1309 if (buf == NULL)
1310 buf = firstbuf;
1311 }
1312 while (buf != curbuf && !bufIsChanged(buf));
1313 }
1314 if (!bufIsChanged(buf))
1315 {
1316 EMSG(_("E84: No modified buffer found"));
1317 return FAIL;
1318 }
1319 }
1320 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1321 {
1322 while (buf != NULL && buf->b_fnum != count)
1323 buf = buf->b_next;
1324 }
1325 else
1326 {
1327 bp = NULL;
1328 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1329 {
1330 /* remember the buffer where we start, we come back there when all
1331 * buffers are unlisted. */
1332 if (bp == NULL)
1333 bp = buf;
1334 if (dir == FORWARD)
1335 {
1336 buf = buf->b_next;
1337 if (buf == NULL)
1338 buf = firstbuf;
1339 }
1340 else
1341 {
1342 buf = buf->b_prev;
1343 if (buf == NULL)
1344 buf = lastbuf;
1345 }
1346 /* don't count unlisted buffers */
1347 if (unload || buf->b_p_bl)
1348 {
1349 --count;
1350 bp = NULL; /* use this buffer as new starting point */
1351 }
1352 if (bp == buf)
1353 {
1354 /* back where we started, didn't find anything. */
1355 EMSG(_("E85: There is no listed buffer"));
1356 return FAIL;
1357 }
1358 }
1359 }
1360
1361 if (buf == NULL) /* could not find it */
1362 {
1363 if (start == DOBUF_FIRST)
1364 {
1365 /* don't warn when deleting */
1366 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001367 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 }
1369 else if (dir == FORWARD)
1370 EMSG(_("E87: Cannot go beyond last buffer"));
1371 else
1372 EMSG(_("E88: Cannot go before first buffer"));
1373 return FAIL;
1374 }
1375
1376#ifdef FEAT_GUI
1377 need_mouse_correct = TRUE;
1378#endif
1379
1380#ifdef FEAT_LISTCMDS
1381 /*
1382 * delete buffer buf from memory and/or the list
1383 */
1384 if (unload)
1385 {
1386 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001387# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1388 bufref_T bufref;
1389
1390 set_bufref(&bufref, buf);
1391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392
1393 /* When unloading or deleting a buffer that's already unloaded and
1394 * unlisted: fail silently. */
1395 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1396 return FAIL;
1397
1398 if (!forceit && bufIsChanged(buf))
1399 {
1400#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1401 if ((p_confirm || cmdmod.confirm) && p_write)
1402 {
1403 dialog_changed(buf, FALSE);
1404# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001405 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /* Autocommand deleted buffer, oops! It's not changed
1407 * now. */
1408 return FAIL;
1409# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001410 /* If it's still changed fail silently, the dialog already
1411 * mentioned why it fails. */
1412 if (bufIsChanged(buf))
1413 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001415 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416#endif
1417 {
1418 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1419 buf->b_fnum);
1420 return FAIL;
1421 }
1422 }
1423
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001424 /* When closing the current buffer stop Visual mode. */
Bram Moolenaar4930a762016-09-11 14:39:53 +02001425 if (buf == curbuf && VIsual_active)
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001426 end_visual_mode();
1427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 /*
1429 * If deleting the last (listed) buffer, make it empty.
1430 * The last (listed) buffer cannot be unloaded.
1431 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001432 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 if (bp->b_p_bl && bp != buf)
1434 break;
1435 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001436 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437
1438#ifdef FEAT_WINDOWS
1439 /*
1440 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001441 * (unless it's the only window). Repeat this so long as we end up in
1442 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001444 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001445# ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001446 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02001447# endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01001448 && (!ONE_WINDOW || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001449 {
1450 if (win_close(curwin, FALSE) == FAIL)
1451 break;
1452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453#endif
1454
1455 /*
1456 * If the buffer to be deleted is not the current one, delete it here.
1457 */
1458 if (buf != curbuf)
1459 {
1460#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001461 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001462 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001464 if (buf->b_nwindows <= 0)
1465 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 return OK;
1467 }
1468
1469 /*
1470 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001471 * There should be another, otherwise it would have been handled
1472 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001473 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474 * Then prefer the buffer we most recently visited.
1475 * Else try to find one that is loaded, after the current buffer,
1476 * then before the current buffer.
1477 * Finally use any buffer.
1478 */
1479 buf = NULL; /* selected buffer */
1480 bp = NULL; /* used when no loaded buffer found */
1481#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001482 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1483 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484# ifdef FEAT_JUMPLIST
1485 else
1486# endif
1487#endif
1488#ifdef FEAT_JUMPLIST
1489 if (curwin->w_jumplistlen > 0)
1490 {
1491 int jumpidx;
1492
1493 jumpidx = curwin->w_jumplistidx - 1;
1494 if (jumpidx < 0)
1495 jumpidx = curwin->w_jumplistlen - 1;
1496
1497 forward = jumpidx;
1498 while (jumpidx != curwin->w_jumplistidx)
1499 {
1500 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1501 if (buf != NULL)
1502 {
1503 if (buf == curbuf || !buf->b_p_bl)
1504 buf = NULL; /* skip current and unlisted bufs */
1505 else if (buf->b_ml.ml_mfp == NULL)
1506 {
1507 /* skip unloaded buf, but may keep it for later */
1508 if (bp == NULL)
1509 bp = buf;
1510 buf = NULL;
1511 }
1512 }
1513 if (buf != NULL) /* found a valid buffer: stop searching */
1514 break;
1515 /* advance to older entry in jump list */
1516 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1517 break;
1518 if (--jumpidx < 0)
1519 jumpidx = curwin->w_jumplistlen - 1;
1520 if (jumpidx == forward) /* List exhausted for sure */
1521 break;
1522 }
1523 }
1524#endif
1525
1526 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1527 {
1528 forward = TRUE;
1529 buf = curbuf->b_next;
1530 for (;;)
1531 {
1532 if (buf == NULL)
1533 {
1534 if (!forward) /* tried both directions */
1535 break;
1536 buf = curbuf->b_prev;
1537 forward = FALSE;
1538 continue;
1539 }
1540 /* in non-help buffer, try to skip help buffers, and vv */
1541 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1542 {
1543 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1544 break;
1545 if (bp == NULL) /* remember unloaded buf for later */
1546 bp = buf;
1547 }
1548 if (forward)
1549 buf = buf->b_next;
1550 else
1551 buf = buf->b_prev;
1552 }
1553 }
1554 if (buf == NULL) /* No loaded buffer, use unloaded one */
1555 buf = bp;
1556 if (buf == NULL) /* No loaded buffer, find listed one */
1557 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001558 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (buf->b_p_bl && buf != curbuf)
1560 break;
1561 }
1562 if (buf == NULL) /* Still no buffer, just take one */
1563 {
1564 if (curbuf->b_next != NULL)
1565 buf = curbuf->b_next;
1566 else
1567 buf = curbuf->b_prev;
1568 }
1569 }
1570
Bram Moolenaara02471e2014-01-10 16:43:14 +01001571 if (buf == NULL)
1572 {
1573 /* Autocommands must have wiped out all other buffers. Only option
1574 * now is to make the current buffer empty. */
1575 return empty_curbuf(FALSE, forceit, action);
1576 }
1577
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 /*
1579 * make buf current buffer
1580 */
1581 if (action == DOBUF_SPLIT) /* split window first */
1582 {
1583# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001584 /* If 'switchbuf' contains "useopen": jump to first window containing
1585 * "buf" if one exists */
1586 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001587 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001588 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001589 * page containing "buf" if one exists */
1590 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 return OK;
1592 if (win_split(0, 0) == FAIL)
1593# endif
1594 return FAIL;
1595 }
1596#endif
1597
1598 /* go to current buffer - nothing to do */
1599 if (buf == curbuf)
1600 return OK;
1601
1602 /*
1603 * Check if the current buffer may be abandoned.
1604 */
1605 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1606 {
1607#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1608 if ((p_confirm || cmdmod.confirm) && p_write)
1609 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001610# ifdef FEAT_AUTOCMD
1611 bufref_T bufref;
1612
1613 set_bufref(&bufref, buf);
1614# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 dialog_changed(curbuf, FALSE);
1616# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001617 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 /* Autocommand deleted buffer, oops! */
1619 return FAIL;
1620# endif
1621 }
1622 if (bufIsChanged(curbuf))
1623#endif
1624 {
1625 EMSG(_(e_nowrtmsg));
1626 return FAIL;
1627 }
1628 }
1629
1630 /* Go to the other buffer. */
1631 set_curbuf(buf, action);
1632
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001633#if defined(FEAT_LISTCMDS) \
1634 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001636 {
1637 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639#endif
1640
1641#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1642 if (aborting()) /* autocmds may abort script processing */
1643 return FAIL;
1644#endif
1645
1646 return OK;
1647}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650/*
1651 * Set current buffer to "buf". Executes autocommands and closes current
1652 * buffer. "action" tells how to close the current buffer:
1653 * DOBUF_GOTO free or hide it
1654 * DOBUF_SPLIT nothing
1655 * DOBUF_UNLOAD unload it
1656 * DOBUF_DEL delete it
1657 * DOBUF_WIPE wipe it out
1658 */
1659 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001660set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661{
1662 buf_T *prevbuf;
1663 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1664 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001665#ifdef FEAT_SYN_HL
1666 long old_tw = curbuf->b_p_tw;
1667#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001668 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001671 if (!cmdmod.keepalt)
1672 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001673 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 /* Don't restart Select mode after switching to another buffer. */
1676 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 /* close_windows() or apply_autocmds() may change curbuf */
1679 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001680 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681
1682#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001683 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684# ifdef FEAT_EVAL
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001685 || (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686# else
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001687 || bufref_valid(&bufref)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688# endif
Bram Moolenaar3a117e12016-10-30 21:57:52 +01001689 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690#endif
1691 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001692#ifdef FEAT_SYN_HL
1693 if (prevbuf == curwin->w_buffer)
1694 reset_synblock(curwin);
1695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#ifdef FEAT_WINDOWS
1697 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001698 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699#endif
1700#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001701 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001703 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001705 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001706#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001707 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001708#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001709 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001710 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1712 unload ? action : (action == DOBUF_GOTO
1713 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001714 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001715#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001716 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001717 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001718 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001719#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 }
1722#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001723 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001724 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001725 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1726 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001727# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001728 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001729# endif
1730# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001731 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001732# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001733 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001735 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001737#ifdef FEAT_SYN_HL
1738 if (old_tw != curbuf->b_p_tw)
1739 check_colorcolumn(curwin);
1740#endif
1741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742}
1743
1744/*
1745 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001746 * Old curbuf must have been abandoned already! This also means "curbuf" may
1747 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 */
1749 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001750enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751{
1752 /* Copy buffer and window local option values. Not for a help buffer. */
1753 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1754 if (!buf->b_help)
1755 get_winopts(buf);
1756#ifdef FEAT_FOLDING
1757 else
1758 /* Remove all folds in the window. */
1759 clearFolding(curwin);
1760 foldUpdateAll(curwin); /* update folds (later). */
1761#endif
1762
1763 /* Get the buffer in the current window. */
1764 curwin->w_buffer = buf;
1765 curbuf = buf;
1766 ++curbuf->b_nwindows;
1767
1768#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001769 if (curwin->w_p_diff)
1770 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771#endif
1772
Bram Moolenaara971b822011-09-14 14:43:25 +02001773#ifdef FEAT_SYN_HL
1774 curwin->w_s = &(buf->b_s);
1775#endif
1776
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 /* Cursor on first line by default. */
1778 curwin->w_cursor.lnum = 1;
1779 curwin->w_cursor.col = 0;
1780#ifdef FEAT_VIRTUALEDIT
1781 curwin->w_cursor.coladd = 0;
1782#endif
1783 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001784#ifdef FEAT_AUTOCMD
1785 curwin->w_topline_was_set = FALSE;
1786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001788 /* mark cursor position as being invalid */
1789 curwin->w_valid = 0;
1790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 /* Make sure the buffer is loaded. */
1792 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001793 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001794#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001795 /* If there is no filetype, allow for detecting one. Esp. useful for
1796 * ":ball" used in a autocommand. If there already is a filetype we
1797 * might prefer to keep it. */
1798 if (*curbuf->b_p_ft == NUL)
1799 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001800#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001801
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001802 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 else
1805 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001806 if (!msg_silent)
1807 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1809#ifdef FEAT_AUTOCMD
1810 curwin->w_topline = 1;
1811# ifdef FEAT_DIFF
1812 curwin->w_topfill = 0;
1813# endif
1814 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1815 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1816#endif
1817 }
1818
1819 /* If autocommands did not change the cursor position, restore cursor lnum
1820 * and possibly cursor col. */
1821 if (curwin->w_cursor.lnum == 1 && inindent(0))
1822 buflist_getfpos();
1823
1824 check_arg_idx(curwin); /* check for valid arg_idx */
1825#ifdef FEAT_TITLE
1826 maketitle();
1827#endif
1828#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001829 /* when autocmds didn't change it */
1830 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#endif
1832 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1833
Bram Moolenaar009b2592004-10-24 19:18:58 +00001834#ifdef FEAT_NETBEANS_INTG
1835 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001836 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001837#endif
1838
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001839 /* Change directories when the 'acd' option is set. */
1840 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841
1842#ifdef FEAT_KEYMAP
1843 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001844 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001846#ifdef FEAT_SPELL
1847 /* May need to set the spell language. Can only do this after the buffer
1848 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001849 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1850 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001851#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001852#ifdef FEAT_VIMINFO
1853 curbuf->b_last_used = vim_time();
1854#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001855
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 redraw_later(NOT_VALID);
1857}
1858
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001859#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1860/*
1861 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001862 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001866{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001867 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001868 && curbuf->b_ffname != NULL
1869 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001870 shorten_fnames(TRUE);
1871}
1872#endif
1873
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874/*
1875 * functions for dealing with the buffer list
1876 */
1877
Bram Moolenaar480778b2016-07-14 22:09:39 +02001878static int top_file_num = 1; /* highest file number */
1879
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880/*
1881 * Add a file name to the buffer list. Return a pointer to the buffer.
1882 * If the same file name already exists return a pointer to that buffer.
1883 * If it does not exist, or if fname == NULL, a new entry is created.
1884 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1885 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1886 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001887 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001888 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1889 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 * This is the ONLY way to create a new buffer.
1891 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001893buflist_new(
1894 char_u *ffname, /* full path of fname or relative */
1895 char_u *sfname, /* short fname or NULL */
1896 linenr_T lnum, /* preferred cursor line */
1897 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898{
1899 buf_T *buf;
1900#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001901 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902#endif
1903
Bram Moolenaar480778b2016-07-14 22:09:39 +02001904 if (top_file_num == 1)
1905 hash_init(&buf_hashtab);
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1908
1909 /*
1910 * If file name already exists in the list, update the entry.
1911 */
1912#ifdef UNIX
1913 /* On Unix we can use inode numbers when the file exists. Works better
1914 * for hard links. */
1915 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1916 st.st_dev = (dev_T)-1;
1917#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001918 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919#ifdef UNIX
1920 buflist_findname_stat(ffname, &st)
1921#else
1922 buflist_findname(ffname)
1923#endif
1924 ) != NULL)
1925 {
1926 vim_free(ffname);
1927 if (lnum != 0)
1928 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001929
1930 if ((flags & BLN_NOOPT) == 0)
1931 /* copy the options now, if 'cpo' doesn't have 's' and not done
1932 * already */
1933 buf_copy_options(buf, 0);
1934
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1936 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001937#ifdef FEAT_AUTOCMD
1938 bufref_T bufref;
1939#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 buf->b_p_bl = TRUE;
1941#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001942 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001944 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001945 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001946 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001947 return NULL;
1948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949#endif
1950 }
1951 return buf;
1952 }
1953
1954 /*
1955 * If the current buffer has no name and no contents, use the current
1956 * buffer. Otherwise: Need to allocate a new buffer structure.
1957 *
1958 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001959 * (A spell file buffer is allocated in spell.c, but that's not a normal
1960 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 */
1962 buf = NULL;
1963 if ((flags & BLN_CURBUF)
1964 && curbuf != NULL
1965 && curbuf->b_ffname == NULL
1966 && curbuf->b_nwindows <= 1
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01001967 && (curbuf->b_ml.ml_mfp == NULL || BUFEMPTY()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 {
1969 buf = curbuf;
1970#ifdef FEAT_AUTOCMD
1971 /* It's like this buffer is deleted. Watch out for autocommands that
1972 * change curbuf! If that happens, allocate a new buffer anyway. */
1973 if (curbuf->b_p_bl)
1974 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1975 if (buf == curbuf)
1976 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1977# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001978 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 return NULL;
1980# endif
1981#endif
1982#ifdef FEAT_QUICKFIX
1983# ifdef FEAT_AUTOCMD
1984 if (buf == curbuf)
1985# endif
1986 {
1987 /* Make sure 'bufhidden' and 'buftype' are empty */
1988 clear_string_option(&buf->b_p_bh);
1989 clear_string_option(&buf->b_p_bt);
1990 }
1991#endif
1992 }
1993 if (buf != curbuf || curbuf == NULL)
1994 {
1995 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1996 if (buf == NULL)
1997 {
1998 vim_free(ffname);
1999 return NULL;
2000 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02002001#ifdef FEAT_EVAL
2002 /* init b: variables */
2003 buf->b_vars = dict_alloc();
2004 if (buf->b_vars == NULL)
2005 {
2006 vim_free(ffname);
2007 vim_free(buf);
2008 return NULL;
2009 }
2010 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
2011#endif
Bram Moolenaar79518e22017-02-17 16:31:35 +01002012 init_changedtick(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013 }
2014
2015 if (ffname != NULL)
2016 {
2017 buf->b_ffname = ffname;
2018 buf->b_sfname = vim_strsave(sfname);
2019 }
2020
2021 clear_wininfo(buf);
2022 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2023
2024 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
2025 || buf->b_wininfo == NULL)
2026 {
2027 vim_free(buf->b_ffname);
2028 buf->b_ffname = NULL;
2029 vim_free(buf->b_sfname);
2030 buf->b_sfname = NULL;
2031 if (buf != curbuf)
2032 free_buffer(buf);
2033 return NULL;
2034 }
2035
2036 if (buf == curbuf)
2037 {
2038 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002039 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 if (buf != curbuf) /* autocommands deleted the buffer! */
2041 return NULL;
2042#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002043 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 return NULL;
2045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002047
2048 /* Init the options. */
2049 buf->b_p_initialized = FALSE;
2050 buf_copy_options(buf, BCO_ENTER);
2051
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#ifdef FEAT_KEYMAP
2053 /* need to reload lmaps and set b:keymap_name */
2054 curbuf->b_kmap_state |= KEYMAP_INIT;
2055#endif
2056 }
2057 else
2058 {
2059 /*
2060 * put new buffer at the end of the buffer list
2061 */
2062 buf->b_next = NULL;
2063 if (firstbuf == NULL) /* buffer list is empty */
2064 {
2065 buf->b_prev = NULL;
2066 firstbuf = buf;
2067 }
2068 else /* append new buffer at end of list */
2069 {
2070 lastbuf->b_next = buf;
2071 buf->b_prev = lastbuf;
2072 }
2073 lastbuf = buf;
2074
2075 buf->b_fnum = top_file_num++;
2076 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2077 {
2078 EMSG(_("W14: Warning: List of file names overflow"));
2079 if (emsg_silent == 0)
2080 {
2081 out_flush();
2082 ui_delay(3000L, TRUE); /* make sure it is noticed */
2083 }
2084 top_file_num = 1;
2085 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002086 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087
2088 /*
2089 * Always copy the options from the current buffer.
2090 */
2091 buf_copy_options(buf, BCO_ALWAYS);
2092 }
2093
2094 buf->b_wininfo->wi_fpos.lnum = lnum;
2095 buf->b_wininfo->wi_win = curwin;
2096
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002097#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002098 hash_init(&buf->b_s.b_keywtab);
2099 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100#endif
2101
2102 buf->b_fname = buf->b_sfname;
2103#ifdef UNIX
2104 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002105 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 else
2107 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002108 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 buf->b_dev = st.st_dev;
2110 buf->b_ino = st.st_ino;
2111 }
2112#endif
2113 buf->b_u_synced = TRUE;
2114 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002115 if (flags & BLN_DUMMY)
2116 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 buf_clear_file(buf);
2118 clrallmarks(buf); /* clear marks */
2119 fmarks_check_names(buf); /* check file marks for this file */
2120 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2121#ifdef FEAT_AUTOCMD
2122 if (!(flags & BLN_DUMMY))
2123 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002124 bufref_T bufref;
2125
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002126 /* Tricky: these autocommands may change the buffer list. They could
2127 * also split the window with re-using the one empty buffer. This may
2128 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002129 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002130 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002131 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002132 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002134 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002135 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002136 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002137 return NULL;
2138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002139# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002140 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 return NULL;
2142# endif
2143 }
2144#endif
2145
2146 return buf;
2147}
2148
2149/*
2150 * Free the memory for the options of a buffer.
2151 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2152 * 'fileencoding'.
2153 */
2154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002155free_buf_options(
2156 buf_T *buf,
2157 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
2159 if (free_p_ff)
2160 {
2161#ifdef FEAT_MBYTE
2162 clear_string_option(&buf->b_p_fenc);
2163#endif
2164 clear_string_option(&buf->b_p_ff);
2165#ifdef FEAT_QUICKFIX
2166 clear_string_option(&buf->b_p_bh);
2167 clear_string_option(&buf->b_p_bt);
2168#endif
2169 }
2170#ifdef FEAT_FIND_ID
2171 clear_string_option(&buf->b_p_def);
2172 clear_string_option(&buf->b_p_inc);
2173# ifdef FEAT_EVAL
2174 clear_string_option(&buf->b_p_inex);
2175# endif
2176#endif
2177#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2178 clear_string_option(&buf->b_p_inde);
2179 clear_string_option(&buf->b_p_indk);
2180#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002181#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2182 clear_string_option(&buf->b_p_bexpr);
2183#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002184#if defined(FEAT_CRYPT)
2185 clear_string_option(&buf->b_p_cm);
2186#endif
Bram Moolenaar24a2d412017-01-24 17:48:36 +01002187 clear_string_option(&buf->b_p_fp);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002188#if defined(FEAT_EVAL)
2189 clear_string_option(&buf->b_p_fex);
2190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191#ifdef FEAT_CRYPT
2192 clear_string_option(&buf->b_p_key);
2193#endif
2194 clear_string_option(&buf->b_p_kp);
2195 clear_string_option(&buf->b_p_mps);
2196 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002197 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 clear_string_option(&buf->b_p_isk);
2199#ifdef FEAT_KEYMAP
2200 clear_string_option(&buf->b_p_keymap);
2201 ga_clear(&buf->b_kmap_ga);
2202#endif
2203#ifdef FEAT_COMMENTS
2204 clear_string_option(&buf->b_p_com);
2205#endif
2206#ifdef FEAT_FOLDING
2207 clear_string_option(&buf->b_p_cms);
2208#endif
2209 clear_string_option(&buf->b_p_nf);
2210#ifdef FEAT_SYN_HL
2211 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002212 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002213#endif
2214#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002215 clear_string_option(&buf->b_s.b_p_spc);
2216 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002217 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002218 buf->b_s.b_cap_prog = NULL;
2219 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220#endif
2221#ifdef FEAT_SEARCHPATH
2222 clear_string_option(&buf->b_p_sua);
2223#endif
2224#ifdef FEAT_AUTOCMD
2225 clear_string_option(&buf->b_p_ft);
2226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227#ifdef FEAT_CINDENT
2228 clear_string_option(&buf->b_p_cink);
2229 clear_string_option(&buf->b_p_cino);
2230#endif
2231#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2232 clear_string_option(&buf->b_p_cinw);
2233#endif
2234#ifdef FEAT_INS_EXPAND
2235 clear_string_option(&buf->b_p_cpt);
2236#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002237#ifdef FEAT_COMPL_FUNC
2238 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002239 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241#ifdef FEAT_QUICKFIX
2242 clear_string_option(&buf->b_p_gp);
2243 clear_string_option(&buf->b_p_mp);
2244 clear_string_option(&buf->b_p_efm);
2245#endif
2246 clear_string_option(&buf->b_p_ep);
2247 clear_string_option(&buf->b_p_path);
2248 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002249 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250#ifdef FEAT_INS_EXPAND
2251 clear_string_option(&buf->b_p_dict);
2252 clear_string_option(&buf->b_p_tsr);
2253#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002254#ifdef FEAT_TEXTOBJ
2255 clear_string_option(&buf->b_p_qe);
2256#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002258 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002259#ifdef FEAT_LISP
2260 clear_string_option(&buf->b_p_lw);
2261#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002262 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar2c7292d2017-03-05 17:43:31 +01002263#ifdef FEAT_MBYTE
2264 clear_string_option(&buf->b_p_menc);
2265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266}
2267
2268/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002269 * Get alternate file "n".
2270 * Set linenr to "lnum" or altfpos.lnum if "lnum" == 0.
2271 * Also set cursor column to altfpos.col if 'startofline' is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 * if (options & GETF_SETMARK) call setpcmark()
2273 * if (options & GETF_ALT) we are jumping to an alternate file.
2274 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2275 *
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02002276 * Return FAIL for failure, OK for success.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 */
2278 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002279buflist_getfile(
2280 int n,
2281 linenr_T lnum,
2282 int options,
2283 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
2285 buf_T *buf;
2286#ifdef FEAT_WINDOWS
2287 win_T *wp = NULL;
2288#endif
2289 pos_T *fpos;
2290 colnr_T col;
2291
2292 buf = buflist_findnr(n);
2293 if (buf == NULL)
2294 {
2295 if ((options & GETF_ALT) && n == 0)
2296 EMSG(_(e_noalt));
2297 else
2298 EMSGN(_("E92: Buffer %ld not found"), n);
2299 return FAIL;
2300 }
2301
2302 /* if alternate file is the current buffer, nothing to do */
2303 if (buf == curbuf)
2304 return OK;
2305
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002306 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002307 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002308 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002310 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002311#ifdef FEAT_AUTOCMD
2312 if (curbuf_locked())
2313 return FAIL;
2314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315
2316 /* altfpos may be changed by getfile(), get it now */
2317 if (lnum == 0)
2318 {
2319 fpos = buflist_findfpos(buf);
2320 lnum = fpos->lnum;
2321 col = fpos->col;
2322 }
2323 else
2324 col = 0;
2325
2326#ifdef FEAT_WINDOWS
2327 if (options & GETF_SWITCH)
2328 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002329 /* If 'switchbuf' contains "useopen": jump to first window containing
2330 * "buf" if one exists */
2331 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002333
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002334 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002335 * page containing "buf" if one exists */
2336 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002337 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002338
2339 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2340 * current buffer isn't empty: open new tab or window */
2341 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002342 && !BUFEMPTY())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002344 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002345 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002346 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2347 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002349 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351 }
2352#endif
2353
2354 ++RedrawingDisabled;
Bram Moolenaar8ad80de2017-06-05 16:01:59 +02002355 if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2356 (options & GETF_SETMARK), lnum, forceit)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
2358 --RedrawingDisabled;
2359
2360 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2361 if (!p_sol && col != 0)
2362 {
2363 curwin->w_cursor.col = col;
2364 check_cursor_col();
2365#ifdef FEAT_VIRTUALEDIT
2366 curwin->w_cursor.coladd = 0;
2367#endif
2368 curwin->w_set_curswant = TRUE;
2369 }
2370 return OK;
2371 }
2372 --RedrawingDisabled;
2373 return FAIL;
2374}
2375
2376/*
2377 * go to the last know line number for the current buffer
2378 */
2379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002380buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381{
2382 pos_T *fpos;
2383
2384 fpos = buflist_findfpos(curbuf);
2385
2386 curwin->w_cursor.lnum = fpos->lnum;
2387 check_cursor_lnum();
2388
2389 if (p_sol)
2390 curwin->w_cursor.col = 0;
2391 else
2392 {
2393 curwin->w_cursor.col = fpos->col;
2394 check_cursor_col();
2395#ifdef FEAT_VIRTUALEDIT
2396 curwin->w_cursor.coladd = 0;
2397#endif
2398 curwin->w_set_curswant = TRUE;
2399 }
2400}
2401
Bram Moolenaar81695252004-12-29 20:58:21 +00002402#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2403/*
2404 * Find file in buffer list by name (it has to be for the current window).
2405 * Returns NULL if not found.
2406 */
2407 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002408buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002409{
2410 char_u *ffname;
2411 buf_T *buf = NULL;
2412
2413 /* First make the name into a full path name */
2414 ffname = FullName_save(fname,
2415#ifdef UNIX
2416 TRUE /* force expansion, get rid of symbolic links */
2417#else
2418 FALSE
2419#endif
2420 );
2421 if (ffname != NULL)
2422 {
2423 buf = buflist_findname(ffname);
2424 vim_free(ffname);
2425 }
2426 return buf;
2427}
2428#endif
2429
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430/*
2431 * Find file in buffer list by name (it has to be for the current window).
2432 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002433 * Skips dummy buffers.
2434 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 */
2436 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002437buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438{
2439#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002440 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441
2442 if (mch_stat((char *)ffname, &st) < 0)
2443 st.st_dev = (dev_T)-1;
2444 return buflist_findname_stat(ffname, &st);
2445}
2446
2447/*
2448 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2449 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002450 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 */
2452 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002453buflist_findname_stat(
2454 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002455 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457#endif
2458 buf_T *buf;
2459
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002460 /* Start at the last buffer, expect to find a match sooner. */
2461 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002462 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463#ifdef UNIX
2464 , stp
2465#endif
2466 ))
2467 return buf;
2468 return NULL;
2469}
2470
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002471#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2472 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473/*
2474 * Find file in buffer list by a regexp pattern.
2475 * Return fnum of the found buffer.
2476 * Return < 0 for error.
2477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002479buflist_findpat(
2480 char_u *pattern,
2481 char_u *pattern_end, /* pointer to first char after pattern */
2482 int unlisted, /* find unlisted buffers */
2483 int diffmode UNUSED, /* find diff-mode buffers only */
2484 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485{
2486 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 int match = -1;
2488 int find_listed;
2489 char_u *pat;
2490 char_u *patend;
2491 int attempt;
2492 char_u *p;
2493 int toggledollar;
2494
2495 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2496 {
2497 if (*pattern == '%')
2498 match = curbuf->b_fnum;
2499 else
2500 match = curwin->w_alt_fnum;
2501#ifdef FEAT_DIFF
2502 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2503 match = -1;
2504#endif
2505 }
2506
2507 /*
2508 * Try four ways of matching a listed buffer:
2509 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002510 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 * attempt == 2: with '$' at end (only match at end)
2512 * attempt == 3: with '^' at start and '$' at end (only full match)
2513 * Repeat this for finding an unlisted buffer if there was no matching
2514 * listed buffer.
2515 */
2516 else
2517 {
2518 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2519 if (pat == NULL)
2520 return -1;
2521 patend = pat + STRLEN(pat) - 1;
2522 toggledollar = (patend > pat && *patend == '$');
2523
2524 /* First try finding a listed buffer. If not found and "unlisted"
2525 * is TRUE, try finding an unlisted buffer. */
2526 find_listed = TRUE;
2527 for (;;)
2528 {
2529 for (attempt = 0; attempt <= 3; ++attempt)
2530 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002531 regmatch_T regmatch;
2532
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 /* may add '^' and '$' */
2534 if (toggledollar)
2535 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2536 p = pat;
2537 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2538 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002539 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2540 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 {
2542 vim_free(pat);
2543 return -1;
2544 }
2545
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002546 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (buf->b_p_bl == find_listed
2548#ifdef FEAT_DIFF
2549 && (!diffmode || diff_mode_buf(buf))
2550#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002551 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002552 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002553 if (curtab_only)
2554 {
2555 /* Ignore the match if the buffer is not open in
2556 * the current tab. */
2557#ifdef FEAT_WINDOWS
2558 win_T *wp;
2559
Bram Moolenaar29323592016-07-24 22:04:11 +02002560 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002561 if (wp->w_buffer == buf)
2562 break;
2563 if (wp == NULL)
2564 continue;
2565#else
2566 if (curwin->w_buffer != buf)
2567 continue;
2568#endif
2569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 if (match >= 0) /* already found a match */
2571 {
2572 match = -2;
2573 break;
2574 }
2575 match = buf->b_fnum; /* remember first match */
2576 }
2577
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002578 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (match >= 0) /* found one match */
2580 break;
2581 }
2582
2583 /* Only search for unlisted buffers if there was no match with
2584 * a listed buffer. */
2585 if (!unlisted || !find_listed || match != -1)
2586 break;
2587 find_listed = FALSE;
2588 }
2589
2590 vim_free(pat);
2591 }
2592
2593 if (match == -2)
2594 EMSG2(_("E93: More than one match for %s"), pattern);
2595 else if (match < 0)
2596 EMSG2(_("E94: No matching buffer for %s"), pattern);
2597 return match;
2598}
2599#endif
2600
2601#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2602
2603/*
2604 * Find all buffer names that match.
2605 * For command line expansion of ":buf" and ":sbuf".
2606 * Return OK if matches found, FAIL otherwise.
2607 */
2608 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002609ExpandBufnames(
2610 char_u *pat,
2611 int *num_file,
2612 char_u ***file,
2613 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614{
2615 int count = 0;
2616 buf_T *buf;
2617 int round;
2618 char_u *p;
2619 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002620 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621
2622 *num_file = 0; /* return values in case of FAIL */
2623 *file = NULL;
2624
Bram Moolenaar05159a02005-02-26 23:04:13 +00002625 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2626 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002628 patc = alloc((unsigned)STRLEN(pat) + 11);
2629 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002631 STRCPY(patc, "\\(^\\|[\\/]\\)");
2632 STRCPY(patc + 11, pat + 1);
2633 }
2634 else
2635 patc = pat;
2636
2637 /*
2638 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002639 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002640 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002641 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002642 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002643 regmatch_T regmatch;
2644
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002645 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002646 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002647 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2648 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002649 {
2650 if (patc != pat)
2651 vim_free(patc);
2652 return FAIL;
2653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002654
2655 /*
2656 * round == 1: Count the matches.
2657 * round == 2: Build the array to keep the matches.
2658 */
2659 for (round = 1; round <= 2; ++round)
2660 {
2661 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002662 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 {
2664 if (!buf->b_p_bl) /* skip unlisted buffers */
2665 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002666 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 if (p != NULL)
2668 {
2669 if (round == 1)
2670 ++count;
2671 else
2672 {
2673 if (options & WILD_HOME_REPLACE)
2674 p = home_replace_save(buf, p);
2675 else
2676 p = vim_strsave(p);
2677 (*file)[count++] = p;
2678 }
2679 }
2680 }
2681 if (count == 0) /* no match found, break here */
2682 break;
2683 if (round == 1)
2684 {
2685 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2686 if (*file == NULL)
2687 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002688 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002689 if (patc != pat)
2690 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 return FAIL;
2692 }
2693 }
2694 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002695 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 if (count) /* match(es) found, break here */
2697 break;
2698 }
2699
Bram Moolenaar05159a02005-02-26 23:04:13 +00002700 if (patc != pat)
2701 vim_free(patc);
2702
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 *num_file = count;
2704 return (count == 0 ? FAIL : OK);
2705}
2706
2707#endif /* FEAT_CMDL_COMPL */
2708
2709#ifdef HAVE_BUFLIST_MATCH
2710/*
2711 * Check for a match on the file name for buffer "buf" with regprog "prog".
2712 */
2713 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002714buflist_match(
2715 regmatch_T *rmp,
2716 buf_T *buf,
2717 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 char_u *match;
2720
2721 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002722 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002724 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725
2726 return match;
2727}
2728
2729/*
2730 * Try matching the regexp in "prog" with file name "name".
2731 * Return "name" when there is a match, NULL when not.
2732 */
2733 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002734fname_match(
2735 regmatch_T *rmp,
2736 char_u *name,
2737 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738{
2739 char_u *match = NULL;
2740 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741
2742 if (name != NULL)
2743 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002744 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002745 rmp->rm_ic = p_fic || ignore_case;
2746 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 match = name;
2748 else
2749 {
2750 /* Replace $(HOME) with '~' and try matching again. */
2751 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002752 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 match = name;
2754 vim_free(p);
2755 }
2756 }
2757
2758 return match;
2759}
2760#endif
2761
2762/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002763 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 */
2765 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002766buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002768 char_u key[VIM_SIZEOF_INT * 2 + 1];
2769 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770
2771 if (nr == 0)
2772 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002773 sprintf((char *)key, "%x", nr);
2774 hi = hash_find(&buf_hashtab, key);
2775
2776 if (!HASHITEM_EMPTY(hi))
2777 return (buf_T *)(hi->hi_key
2778 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 return NULL;
2780}
2781
2782/*
2783 * Get name of file 'n' in the buffer list.
2784 * When the file has no name an empty string is returned.
2785 * home_replace() is used to shorten the file name (used for marks).
2786 * Returns a pointer to allocated memory, of NULL when failed.
2787 */
2788 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002789buflist_nr2name(
2790 int n,
2791 int fullname,
2792 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 buf_T *buf;
2795
2796 buf = buflist_findnr(n);
2797 if (buf == NULL)
2798 return NULL;
2799 return home_replace_save(helptail ? buf : NULL,
2800 fullname ? buf->b_ffname : buf->b_fname);
2801}
2802
2803/*
2804 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2805 * When "copy_options" is TRUE save the local window option values.
2806 * When "lnum" is 0 only do the options.
2807 */
2808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002809buflist_setfpos(
2810 buf_T *buf,
2811 win_T *win,
2812 linenr_T lnum,
2813 colnr_T col,
2814 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002815{
2816 wininfo_T *wip;
2817
2818 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2819 if (wip->wi_win == win)
2820 break;
2821 if (wip == NULL)
2822 {
2823 /* allocate a new entry */
2824 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2825 if (wip == NULL)
2826 return;
2827 wip->wi_win = win;
2828 if (lnum == 0) /* set lnum even when it's 0 */
2829 lnum = 1;
2830 }
2831 else
2832 {
2833 /* remove the entry from the list */
2834 if (wip->wi_prev)
2835 wip->wi_prev->wi_next = wip->wi_next;
2836 else
2837 buf->b_wininfo = wip->wi_next;
2838 if (wip->wi_next)
2839 wip->wi_next->wi_prev = wip->wi_prev;
2840 if (copy_options && wip->wi_optset)
2841 {
2842 clear_winopt(&wip->wi_opt);
2843#ifdef FEAT_FOLDING
2844 deleteFoldRecurse(&wip->wi_folds);
2845#endif
2846 }
2847 }
2848 if (lnum != 0)
2849 {
2850 wip->wi_fpos.lnum = lnum;
2851 wip->wi_fpos.col = col;
2852 }
2853 if (copy_options)
2854 {
2855 /* Save the window-specific option values. */
2856 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2857#ifdef FEAT_FOLDING
2858 wip->wi_fold_manual = win->w_fold_manual;
2859 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2860#endif
2861 wip->wi_optset = TRUE;
2862 }
2863
2864 /* insert the entry in front of the list */
2865 wip->wi_next = buf->b_wininfo;
2866 buf->b_wininfo = wip;
2867 wip->wi_prev = NULL;
2868 if (wip->wi_next)
2869 wip->wi_next->wi_prev = wip;
2870
2871 return;
2872}
2873
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002874#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002875static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002876
2877/*
2878 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2879 * page. That's because a diff is local to a tab page.
2880 */
2881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002882wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002883{
2884 win_T *wp;
2885
2886 if (wip->wi_opt.wo_diff)
2887 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002888 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002889 /* return FALSE when it's a window in the current tab page, thus
2890 * the buffer was in diff mode here */
2891 if (wip->wi_win == wp)
2892 return FALSE;
2893 return TRUE;
2894 }
2895 return FALSE;
2896}
2897#endif
2898
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899/*
2900 * Find info for the current window in buffer "buf".
2901 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002902 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2903 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 * Returns NULL when there isn't any info.
2905 */
2906 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002907find_wininfo(
2908 buf_T *buf,
2909 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910{
2911 wininfo_T *wip;
2912
2913 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002914 if (wip->wi_win == curwin
2915#ifdef FEAT_DIFF
2916 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2917#endif
2918 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002920
2921 /* If no wininfo for curwin, use the first in the list (that doesn't have
2922 * 'diff' set and is in another tab page). */
2923 if (wip == NULL)
2924 {
2925#ifdef FEAT_DIFF
2926 if (skip_diff_buffer)
2927 {
2928 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2929 if (!wininfo_other_tab_diff(wip))
2930 break;
2931 }
2932 else
2933#endif
2934 wip = buf->b_wininfo;
2935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 return wip;
2937}
2938
2939/*
2940 * Reset the local window options to the values last used in this window.
2941 * If the buffer wasn't used in this window before, use the values from
2942 * the most recently used window. If the values were never set, use the
2943 * global values for the window.
2944 */
2945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002946get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947{
2948 wininfo_T *wip;
2949
2950 clear_winopt(&curwin->w_onebuf_opt);
2951#ifdef FEAT_FOLDING
2952 clearFolding(curwin);
2953#endif
2954
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002955 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 if (wip != NULL && wip->wi_optset)
2957 {
2958 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2959#ifdef FEAT_FOLDING
2960 curwin->w_fold_manual = wip->wi_fold_manual;
2961 curwin->w_foldinvalid = TRUE;
2962 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2963#endif
2964 }
2965 else
2966 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2967
2968#ifdef FEAT_FOLDING
2969 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2970 if (p_fdls >= 0)
2971 curwin->w_p_fdl = p_fdls;
2972#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002973#ifdef FEAT_SYN_HL
2974 check_colorcolumn(curwin);
2975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976}
2977
2978/*
2979 * Find the position (lnum and col) for the buffer 'buf' for the current
2980 * window.
2981 * Returns a pointer to no_position if no position is found.
2982 */
2983 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002984buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
2986 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002987 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002989 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 if (wip != NULL)
2991 return &(wip->wi_fpos);
2992 else
2993 return &no_position;
2994}
2995
2996/*
2997 * Find the lnum for the buffer 'buf' for the current window.
2998 */
2999 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003000buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001{
3002 return buflist_findfpos(buf)->lnum;
3003}
3004
3005#if defined(FEAT_LISTCMDS) || defined(PROTO)
3006/*
Bram Moolenaar45e5fd12017-06-04 14:58:02 +02003007 * List all known file names (for :files and :buffers command).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003010buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 buf_T *buf;
3013 int len;
3014 int i;
3015
3016 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
3017 {
3018 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02003019 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
3020 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
3021 || (vim_strchr(eap->arg, '+')
3022 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
3023 || (vim_strchr(eap->arg, 'a')
3024 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
3025 || (vim_strchr(eap->arg, 'h')
3026 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
3027 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
3028 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
3029 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
3030 || (vim_strchr(eap->arg, '%') && buf != curbuf)
3031 || (vim_strchr(eap->arg, '#')
3032 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003035 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 else
3037 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003038 if (message_filtered(NameBuff))
3039 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040
Bram Moolenaar77401ad2016-08-24 00:12:12 +02003041 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00003042 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043 buf->b_fnum,
3044 buf->b_p_bl ? ' ' : 'u',
3045 buf == curbuf ? '%' :
3046 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3047 buf->b_ml.ml_mfp == NULL ? ' ' :
3048 (buf->b_nwindows == 0 ? 'h' : 'a'),
3049 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
3050 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00003051 : (bufIsChanged(buf) ? '+' : ' '),
3052 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003053 if (len > IOSIZE - 20)
3054 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
3056 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 i = 40 - vim_strsize(IObuff);
3058 do
3059 {
3060 IObuff[len++] = ' ';
3061 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003062 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3063 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003064 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065 msg_outtrans(IObuff);
3066 out_flush(); /* output one line at a time */
3067 ui_breakcheck();
3068 }
3069}
3070#endif
3071
3072/*
3073 * Get file name and line number for file 'fnum'.
3074 * Used by DoOneCmd() for translating '%' and '#'.
3075 * Used by insert_reg() and cmdline_paste() for '#' register.
3076 * Return FAIL if not found, OK for success.
3077 */
3078 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003079buflist_name_nr(
3080 int fnum,
3081 char_u **fname,
3082 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083{
3084 buf_T *buf;
3085
3086 buf = buflist_findnr(fnum);
3087 if (buf == NULL || buf->b_fname == NULL)
3088 return FAIL;
3089
3090 *fname = buf->b_fname;
3091 *lnum = buflist_findlnum(buf);
3092
3093 return OK;
3094}
3095
3096/*
3097 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3098 * The file name with the full path is also remembered, for when :cd is used.
3099 * Returns FAIL for failure (file name already in use by other buffer)
3100 * OK otherwise.
3101 */
3102 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003103setfname(
3104 buf_T *buf,
3105 char_u *ffname,
3106 char_u *sfname,
3107 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108{
Bram Moolenaar81695252004-12-29 20:58:21 +00003109 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003111 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112#endif
3113
3114 if (ffname == NULL || *ffname == NUL)
3115 {
3116 /* Removing the name. */
3117 vim_free(buf->b_ffname);
3118 vim_free(buf->b_sfname);
3119 buf->b_ffname = NULL;
3120 buf->b_sfname = NULL;
3121#ifdef UNIX
3122 st.st_dev = (dev_T)-1;
3123#endif
3124 }
3125 else
3126 {
3127 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3128 if (ffname == NULL) /* out of memory */
3129 return FAIL;
3130
3131 /*
3132 * if the file name is already used in another buffer:
3133 * - if the buffer is loaded, fail
3134 * - if the buffer is not loaded, delete it from the list
3135 */
3136#ifdef UNIX
3137 if (mch_stat((char *)ffname, &st) < 0)
3138 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003139#endif
3140 if (!(buf->b_flags & BF_DUMMY))
3141#ifdef UNIX
3142 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003144 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145#endif
3146 if (obuf != NULL && obuf != buf)
3147 {
3148 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3149 {
3150 if (message)
3151 EMSG(_("E95: Buffer with this name already exists"));
3152 vim_free(ffname);
3153 return FAIL;
3154 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003155 /* delete from the list */
3156 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 }
3158 sfname = vim_strsave(sfname);
3159 if (ffname == NULL || sfname == NULL)
3160 {
3161 vim_free(sfname);
3162 vim_free(ffname);
3163 return FAIL;
3164 }
3165#ifdef USE_FNAME_CASE
3166# ifdef USE_LONG_FNAME
3167 if (USE_LONG_FNAME)
3168# endif
3169 fname_case(sfname, 0); /* set correct case for short file name */
3170#endif
3171 vim_free(buf->b_ffname);
3172 vim_free(buf->b_sfname);
3173 buf->b_ffname = ffname;
3174 buf->b_sfname = sfname;
3175 }
3176 buf->b_fname = buf->b_sfname;
3177#ifdef UNIX
3178 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003179 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 else
3181 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003182 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 buf->b_dev = st.st_dev;
3184 buf->b_ino = st.st_ino;
3185 }
3186#endif
3187
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189
3190 buf_name_changed(buf);
3191 return OK;
3192}
3193
3194/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003195 * Crude way of changing the name of a buffer. Use with care!
3196 * The name should be relative to the current directory.
3197 */
3198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003199buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003200{
3201 buf_T *buf;
3202
3203 buf = buflist_findnr(fnum);
3204 if (buf != NULL)
3205 {
3206 vim_free(buf->b_sfname);
3207 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003208 buf->b_ffname = vim_strsave(name);
3209 buf->b_sfname = NULL;
3210 /* Allocate ffname and expand into full path. Also resolves .lnk
3211 * files on Win32. */
3212 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003213 buf->b_fname = buf->b_sfname;
3214 }
3215}
3216
3217/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 * Take care of what needs to be done when the name of buffer "buf" has
3219 * changed.
3220 */
3221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
3224 /*
3225 * If the file name changed, also change the name of the swapfile
3226 */
3227 if (buf->b_ml.ml_mfp != NULL)
3228 ml_setname(buf);
3229
3230 if (curwin->w_buffer == buf)
3231 check_arg_idx(curwin); /* check file name for arg list */
3232#ifdef FEAT_TITLE
3233 maketitle(); /* set window title */
3234#endif
3235#ifdef FEAT_WINDOWS
3236 status_redraw_all(); /* status lines need to be redrawn */
3237#endif
3238 fmarks_check_names(buf); /* check named file marks */
3239 ml_timestamp(buf); /* reset timestamp */
3240}
3241
3242/*
3243 * set alternate file name for current window
3244 *
3245 * Used by do_one_cmd(), do_write() and do_ecmd().
3246 * Return the buffer.
3247 */
3248 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003249setaltfname(
3250 char_u *ffname,
3251 char_u *sfname,
3252 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
3254 buf_T *buf;
3255
3256 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3257 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003258 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003259 curwin->w_alt_fnum = buf->b_fnum;
3260 return buf;
3261}
3262
3263/*
3264 * Get alternate file name for current window.
3265 * Return NULL if there isn't any, and give error message if requested.
3266 */
3267 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003268getaltfname(
3269 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270{
3271 char_u *fname;
3272 linenr_T dummy;
3273
3274 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3275 {
3276 if (errmsg)
3277 EMSG(_(e_noalt));
3278 return NULL;
3279 }
3280 return fname;
3281}
3282
3283/*
3284 * Add a file name to the buflist and return its number.
3285 * Uses same flags as buflist_new(), except BLN_DUMMY.
3286 *
3287 * used by qf_init(), main() and doarglist()
3288 */
3289 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003290buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291{
3292 buf_T *buf;
3293
3294 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3295 if (buf != NULL)
3296 return buf->b_fnum;
3297 return 0;
3298}
3299
3300#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3301/*
3302 * Adjust slashes in file names. Called after 'shellslash' was set.
3303 */
3304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306{
3307 buf_T *bp;
3308
Bram Moolenaar29323592016-07-24 22:04:11 +02003309 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 {
3311 if (bp->b_ffname != NULL)
3312 slash_adjust(bp->b_ffname);
3313 if (bp->b_sfname != NULL)
3314 slash_adjust(bp->b_sfname);
3315 }
3316}
3317#endif
3318
3319/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003320 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 * Also save the local window option values.
3322 */
3323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003324buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003326 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327}
3328
3329/*
3330 * Return TRUE if 'ffname' is not the same file as current file.
3331 * Fname must have a full path (expanded by mch_FullName()).
3332 */
3333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003334otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
3336 return otherfile_buf(curbuf, ffname
3337#ifdef UNIX
3338 , NULL
3339#endif
3340 );
3341}
3342
3343 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003344otherfile_buf(
3345 buf_T *buf,
3346 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003348 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003350 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351{
3352 /* no name is different */
3353 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3354 return TRUE;
3355 if (fnamecmp(ffname, buf->b_ffname) == 0)
3356 return FALSE;
3357#ifdef UNIX
3358 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003359 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360
Bram Moolenaar8767f522016-07-01 17:17:39 +02003361 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 if (stp == NULL)
3363 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003364 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 st.st_dev = (dev_T)-1;
3366 stp = &st;
3367 }
3368 /* Use dev/ino to check if the files are the same, even when the names
3369 * are different (possible with links). Still need to compare the
3370 * name above, for when the file doesn't exist yet.
3371 * Problem: The dev/ino changes when a file is deleted (and created
3372 * again) and remains the same when renamed/moved. We don't want to
3373 * mch_stat() each buffer each time, that would be too slow. Get the
3374 * dev/ino again when they appear to match, but not when they appear
3375 * to be different: Could skip a buffer when it's actually the same
3376 * file. */
3377 if (buf_same_ino(buf, stp))
3378 {
3379 buf_setino(buf);
3380 if (buf_same_ino(buf, stp))
3381 return FALSE;
3382 }
3383 }
3384#endif
3385 return TRUE;
3386}
3387
3388#if defined(UNIX) || defined(PROTO)
3389/*
3390 * Set inode and device number for a buffer.
3391 * Must always be called when b_fname is changed!.
3392 */
3393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003394buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003396 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
3398 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3399 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003400 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 buf->b_dev = st.st_dev;
3402 buf->b_ino = st.st_ino;
3403 }
3404 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003405 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406}
3407
3408/*
3409 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3410 */
3411 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003412buf_same_ino(
3413 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003414 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003416 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 && stp->st_dev == buf->b_dev
3418 && stp->st_ino == buf->b_ino);
3419}
3420#endif
3421
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003422/*
3423 * Print info about the current buffer.
3424 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003426fileinfo(
3427 int fullname, /* when non-zero print full path */
3428 int shorthelp,
3429 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430{
3431 char_u *name;
3432 int n;
3433 char_u *p;
3434 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003435 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436
3437 buffer = alloc(IOSIZE);
3438 if (buffer == NULL)
3439 return;
3440
3441 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3442 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003443 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 p = buffer + STRLEN(buffer);
3445 }
3446 else
3447 p = buffer;
3448
3449 *p++ = '"';
3450 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003451 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 else
3453 {
3454 if (!fullname && curbuf->b_fname != NULL)
3455 name = curbuf->b_fname;
3456 else
3457 name = curbuf->b_ffname;
3458 home_replace(shorthelp ? curbuf : NULL, name, p,
3459 (int)(IOSIZE - (p - buffer)), TRUE);
3460 }
3461
Bram Moolenaara800b422010-06-27 01:15:55 +02003462 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 curbufIsChanged() ? (shortmess(SHM_MOD)
3464 ? " [+]" : _(" [Modified]")) : " ",
3465 (curbuf->b_flags & BF_NOTEDITED)
3466#ifdef FEAT_QUICKFIX
3467 && !bt_dontwrite(curbuf)
3468#endif
3469 ? _("[Not edited]") : "",
3470 (curbuf->b_flags & BF_NEW)
3471#ifdef FEAT_QUICKFIX
3472 && !bt_dontwrite(curbuf)
3473#endif
3474 ? _("[New file]") : "",
3475 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003476 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 : _("[readonly]")) : "",
3478 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3479 || curbuf->b_p_ro) ?
3480 " " : "");
3481 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3482 * causes an overflow, thus for large numbers divide instead. */
3483 if (curwin->w_cursor.lnum > 1000000L)
3484 n = (int)(((long)curwin->w_cursor.lnum) /
3485 ((long)curbuf->b_ml.ml_line_count / 100L));
3486 else
3487 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3488 (long)curbuf->b_ml.ml_line_count);
3489 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3490 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003491 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 }
3493#ifdef FEAT_CMDL_INFO
3494 else if (p_ru)
3495 {
3496 /* Current line and column are already on the screen -- webb */
3497 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003498 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003500 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 (long)curbuf->b_ml.ml_line_count, n);
3502 }
3503#endif
3504 else
3505 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003506 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 _("line %ld of %ld --%d%%-- col "),
3508 (long)curwin->w_cursor.lnum,
3509 (long)curbuf->b_ml.ml_line_count,
3510 n);
3511 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003512 len = STRLEN(buffer);
3513 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3515 }
3516
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003517 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518
3519 if (dont_truncate)
3520 {
3521 /* Temporarily set msg_scroll to avoid the message being truncated.
3522 * First call msg_start() to get the message in the right place. */
3523 msg_start();
3524 n = msg_scroll;
3525 msg_scroll = TRUE;
3526 msg(buffer);
3527 msg_scroll = n;
3528 }
3529 else
3530 {
3531 p = msg_trunc_attr(buffer, FALSE, 0);
3532 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 /* Need to repeat the message after redrawing when:
3534 * - When restart_edit is set (otherwise there will be a delay
3535 * before redrawing).
3536 * - When the screen was scrolled but there is no wait-return
3537 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003538 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540
3541 vim_free(buffer);
3542}
3543
3544 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003545col_print(
3546 char_u *buf,
3547 size_t buflen,
3548 int col,
3549 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550{
3551 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003552 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003554 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555}
3556
3557#if defined(FEAT_TITLE) || defined(PROTO)
3558/*
3559 * put file name in title bar of window and in icon title
3560 */
3561
3562static char_u *lasttitle = NULL;
3563static char_u *lasticon = NULL;
3564
3565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003566maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567{
3568 char_u *p;
3569 char_u *t_str = NULL;
3570 char_u *i_name;
3571 char_u *i_str = NULL;
3572 int maxlen = 0;
3573 int len;
3574 int mustset;
3575 char_u buf[IOSIZE];
3576 int off;
3577
3578 if (!redrawing())
3579 {
3580 /* Postpone updating the title when 'lazyredraw' is set. */
3581 need_maketitle = TRUE;
3582 return;
3583 }
3584
3585 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003586 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 return;
3588
3589 if (p_title)
3590 {
3591 if (p_titlelen > 0)
3592 {
3593 maxlen = p_titlelen * Columns / 100;
3594 if (maxlen < 10)
3595 maxlen = 10;
3596 }
3597
3598 t_str = buf;
3599 if (*p_titlestring != NUL)
3600 {
3601#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003602 if (stl_syntax & STL_IN_TITLE)
3603 {
3604 int use_sandbox = FALSE;
3605 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003606
3607# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003608 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003609# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003610 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003612 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003613 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003614 if (called_emsg)
3615 set_string_option_direct((char_u *)"titlestring", -1,
3616 (char_u *)"", OPT_FREE, SID_ERROR);
3617 called_emsg |= save_called_emsg;
3618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else
3620#endif
3621 t_str = p_titlestring;
3622 }
3623 else
3624 {
3625 /* format: "fname + (path) (1 of 2) - VIM" */
3626
Bram Moolenaar2c666692012-09-05 13:30:40 +02003627#define SPACE_FOR_FNAME (IOSIZE - 100)
3628#define SPACE_FOR_DIR (IOSIZE - 20)
3629#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003631 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 else
3633 {
3634 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003635 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 }
3638
3639 switch (bufIsChanged(curbuf)
3640 + (curbuf->b_p_ro * 2)
3641 + (!curbuf->b_p_ma * 4))
3642 {
3643 case 1: STRCAT(buf, " +"); break;
3644 case 2: STRCAT(buf, " ="); break;
3645 case 3: STRCAT(buf, " =+"); break;
3646 case 4:
3647 case 6: STRCAT(buf, " -"); break;
3648 case 5:
3649 case 7: STRCAT(buf, " -+"); break;
3650 }
3651
3652 if (curbuf->b_fname != NULL)
3653 {
3654 /* Get path of file, replace home dir with ~ */
3655 off = (int)STRLEN(buf);
3656 buf[off++] = ' ';
3657 buf[off++] = '(';
3658 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003659 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660#ifdef BACKSLASH_IN_FILENAME
3661 /* avoid "c:/name" to be reduced to "c" */
3662 if (isalpha(buf[off]) && buf[off + 1] == ':')
3663 off += 2;
3664#endif
3665 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003666 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003669 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003670 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003673
Bram Moolenaar2c666692012-09-05 13:30:40 +02003674 /* Translate unprintable chars and concatenate. Keep some
3675 * room for the server name. When there is no room (very long
3676 * file name) use (...). */
3677 if (off < SPACE_FOR_DIR)
3678 {
3679 p = transstr(buf + off);
3680 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3681 vim_free(p);
3682 }
3683 else
3684 {
3685 vim_strncpy(buf + off, (char_u *)"...",
3686 (size_t)(SPACE_FOR_ARGNR - off));
3687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 STRCAT(buf, ")");
3689 }
3690
Bram Moolenaar2c666692012-09-05 13:30:40 +02003691 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692
3693#if defined(FEAT_CLIENTSERVER)
3694 if (serverName != NULL)
3695 {
3696 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003697 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 else
3700#endif
3701 STRCAT(buf, " - VIM");
3702
3703 if (maxlen > 0)
3704 {
3705 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003706 if (vim_strsize(buf) > maxlen)
3707 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 }
3709 }
3710 }
3711 mustset = ti_change(t_str, &lasttitle);
3712
3713 if (p_icon)
3714 {
3715 i_str = buf;
3716 if (*p_iconstring != NUL)
3717 {
3718#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003719 if (stl_syntax & STL_IN_ICON)
3720 {
3721 int use_sandbox = FALSE;
3722 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003723
3724# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003725 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003726# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003727 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003729 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003730 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003731 if (called_emsg)
3732 set_string_option_direct((char_u *)"iconstring", -1,
3733 (char_u *)"", OPT_FREE, SID_ERROR);
3734 called_emsg |= save_called_emsg;
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 else
3737#endif
3738 i_str = p_iconstring;
3739 }
3740 else
3741 {
3742 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003743 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 else /* use file name only in icon */
3745 i_name = gettail(curbuf->b_ffname);
3746 *i_str = NUL;
3747 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003748 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 if (len > 100)
3750 {
3751 len -= 100;
3752#ifdef FEAT_MBYTE
3753 if (has_mbyte)
3754 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3755#endif
3756 i_name += len;
3757 }
3758 STRCPY(i_str, i_name);
3759 trans_characters(i_str, IOSIZE);
3760 }
3761 }
3762
3763 mustset |= ti_change(i_str, &lasticon);
3764
3765 if (mustset)
3766 resettitle();
3767}
3768
3769/*
3770 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3771 * from "str" if it does.
3772 * Return TRUE when "*last" changed.
3773 */
3774 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003775ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776{
3777 if ((str == NULL) != (*last == NULL)
3778 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3779 {
3780 vim_free(*last);
3781 if (str == NULL)
3782 *last = NULL;
3783 else
3784 *last = vim_strsave(str);
3785 return TRUE;
3786 }
3787 return FALSE;
3788}
3789
3790/*
3791 * Put current window title back (used after calling a shell)
3792 */
3793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003794resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795{
3796 mch_settitle(lasttitle, lasticon);
3797}
Bram Moolenaarea408852005-06-25 22:49:46 +00003798
3799# if defined(EXITFREE) || defined(PROTO)
3800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003801free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003802{
3803 vim_free(lasttitle);
3804 vim_free(lasticon);
3805}
3806# endif
3807
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808#endif /* FEAT_TITLE */
3809
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003810#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003812 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 * Return length of string in screen cells.
3814 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003815 * Normally works for window "wp", except when working for 'tabline' then it
3816 * is "curwin".
3817 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 * Items are drawn interspersed with the text that surrounds it
3819 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3820 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3821 *
3822 * If maxwidth is not zero, the string will be filled at any middle marker
3823 * or truncated if too long, fillchar is used for all whitespace.
3824 */
3825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003826build_stl_str_hl(
3827 win_T *wp,
3828 char_u *out, /* buffer to write into != NameBuff */
3829 size_t outlen, /* length of out[] */
3830 char_u *fmt,
3831 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3832 int fillchar,
3833 int maxwidth,
3834 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3835 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836{
3837 char_u *p;
3838 char_u *s;
3839 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003840 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841#ifdef FEAT_EVAL
3842 win_T *o_curwin;
3843 buf_T *o_curbuf;
3844#endif
3845 int empty_line;
3846 colnr_T virtcol;
3847 long l;
3848 long n;
3849 int prevchar_isflag;
3850 int prevchar_isitem;
3851 int itemisflag;
3852 int fillable;
3853 char_u *str;
3854 long num;
3855 int width;
3856 int itemcnt;
3857 int curitem;
3858 int groupitem[STL_MAX_ITEM];
3859 int groupdepth;
3860 struct stl_item
3861 {
3862 char_u *start;
3863 int minwid;
3864 int maxwid;
3865 enum
3866 {
3867 Normal,
3868 Empty,
3869 Group,
3870 Middle,
3871 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003872 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 Trunc
3874 } type;
3875 } item[STL_MAX_ITEM];
3876 int minwid;
3877 int maxwid;
3878 int zeropad;
3879 char_u base;
3880 char_u opt;
3881#define TMPLEN 70
3882 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003883 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003884 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003885
3886#ifdef FEAT_EVAL
3887 /*
3888 * When the format starts with "%!" then evaluate it as an expression and
3889 * use the result as the actual format string.
3890 */
3891 if (fmt[0] == '%' && fmt[1] == '!')
3892 {
3893 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3894 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003895 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003896 }
3897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
3899 if (fillchar == 0)
3900 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003901#ifdef FEAT_MBYTE
3902 /* Can't handle a multi-byte fill character yet. */
3903 else if (mb_char2len(fillchar) > 1)
3904 fillchar = '-';
3905#endif
3906
Bram Moolenaar567199b2013-04-24 16:52:36 +02003907 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3908 * p will become invalid when getting another buffer line. */
3909 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3910 empty_line = (*p == NUL);
3911
3912 /* Get the byte value now, in case we need it below. This is more
3913 * efficient than making a copy of the line. */
3914 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3915 byteval = 0;
3916 else
3917#ifdef FEAT_MBYTE
3918 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3919#else
3920 byteval = p[wp->w_cursor.col];
3921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922
3923 groupdepth = 0;
3924 p = out;
3925 curitem = 0;
3926 prevchar_isflag = TRUE;
3927 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003928 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003930 if (curitem == STL_MAX_ITEM)
3931 {
3932 /* There are too many items. Add the error code to the statusline
3933 * to give the user a hint about what went wrong. */
3934 if (p + 6 < out + outlen)
3935 {
3936 mch_memmove(p, " E541", (size_t)5);
3937 p += 5;
3938 }
3939 break;
3940 }
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 if (*s != NUL && *s != '%')
3943 prevchar_isflag = prevchar_isitem = FALSE;
3944
3945 /*
3946 * Handle up to the next '%' or the end.
3947 */
3948 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3949 *p++ = *s++;
3950 if (*s == NUL || p + 1 >= out + outlen)
3951 break;
3952
3953 /*
3954 * Handle one '%' item.
3955 */
3956 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003957 if (*s == NUL) /* ignore trailing % */
3958 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 if (*s == '%')
3960 {
3961 if (p + 1 >= out + outlen)
3962 break;
3963 *p++ = *s++;
3964 prevchar_isflag = prevchar_isitem = FALSE;
3965 continue;
3966 }
3967 if (*s == STL_MIDDLEMARK)
3968 {
3969 s++;
3970 if (groupdepth > 0)
3971 continue;
3972 item[curitem].type = Middle;
3973 item[curitem++].start = p;
3974 continue;
3975 }
3976 if (*s == STL_TRUNCMARK)
3977 {
3978 s++;
3979 item[curitem].type = Trunc;
3980 item[curitem++].start = p;
3981 continue;
3982 }
3983 if (*s == ')')
3984 {
3985 s++;
3986 if (groupdepth < 1)
3987 continue;
3988 groupdepth--;
3989
3990 t = item[groupitem[groupdepth]].start;
3991 *p = NUL;
3992 l = vim_strsize(t);
3993 if (curitem > groupitem[groupdepth] + 1
3994 && item[groupitem[groupdepth]].minwid == 0)
3995 {
3996 /* remove group if all items are empty */
3997 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003998 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 break;
4000 if (n == curitem)
4001 {
4002 p = t;
4003 l = 0;
4004 }
4005 }
4006 if (l > item[groupitem[groupdepth]].maxwid)
4007 {
4008 /* truncate, remove n bytes of text at the start */
4009#ifdef FEAT_MBYTE
4010 if (has_mbyte)
4011 {
4012 /* Find the first character that should be included. */
4013 n = 0;
4014 while (l >= item[groupitem[groupdepth]].maxwid)
4015 {
4016 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004017 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 }
4020 else
4021#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004022 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004025 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 p = p - n + 1;
4027#ifdef FEAT_MBYTE
4028 /* Fill up space left over by half a double-wide char. */
4029 while (++l < item[groupitem[groupdepth]].minwid)
4030 *p++ = fillchar;
4031#endif
4032
4033 /* correct the start of the items for the truncation */
4034 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
4035 {
4036 item[l].start -= n;
4037 if (item[l].start < t)
4038 item[l].start = t;
4039 }
4040 }
4041 else if (abs(item[groupitem[groupdepth]].minwid) > l)
4042 {
4043 /* fill */
4044 n = item[groupitem[groupdepth]].minwid;
4045 if (n < 0)
4046 {
4047 /* fill by appending characters */
4048 n = 0 - n;
4049 while (l++ < n && p + 1 < out + outlen)
4050 *p++ = fillchar;
4051 }
4052 else
4053 {
4054 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004055 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 l = n - l;
4057 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004058 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 p += l;
4060 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4061 item[n].start += l;
4062 for ( ; l > 0; l--)
4063 *t++ = fillchar;
4064 }
4065 }
4066 continue;
4067 }
4068 minwid = 0;
4069 maxwid = 9999;
4070 zeropad = FALSE;
4071 l = 1;
4072 if (*s == '0')
4073 {
4074 s++;
4075 zeropad = TRUE;
4076 }
4077 if (*s == '-')
4078 {
4079 s++;
4080 l = -1;
4081 }
4082 if (VIM_ISDIGIT(*s))
4083 {
4084 minwid = (int)getdigits(&s);
4085 if (minwid < 0) /* overflow */
4086 minwid = 0;
4087 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004088 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
4090 item[curitem].type = Highlight;
4091 item[curitem].start = p;
4092 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4093 s++;
4094 curitem++;
4095 continue;
4096 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004097 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4098 {
4099 if (*s == STL_TABCLOSENR)
4100 {
4101 if (minwid == 0)
4102 {
4103 /* %X ends the close label, go back to the previously
4104 * define tab label nr. */
4105 for (n = curitem - 1; n >= 0; --n)
4106 if (item[n].type == TabPage && item[n].minwid >= 0)
4107 {
4108 minwid = item[n].minwid;
4109 break;
4110 }
4111 }
4112 else
4113 /* close nrs are stored as negative values */
4114 minwid = - minwid;
4115 }
4116 item[curitem].type = TabPage;
4117 item[curitem].start = p;
4118 item[curitem].minwid = minwid;
4119 s++;
4120 curitem++;
4121 continue;
4122 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if (*s == '.')
4124 {
4125 s++;
4126 if (VIM_ISDIGIT(*s))
4127 {
4128 maxwid = (int)getdigits(&s);
4129 if (maxwid <= 0) /* overflow */
4130 maxwid = 50;
4131 }
4132 }
4133 minwid = (minwid > 50 ? 50 : minwid) * l;
4134 if (*s == '(')
4135 {
4136 groupitem[groupdepth++] = curitem;
4137 item[curitem].type = Group;
4138 item[curitem].start = p;
4139 item[curitem].minwid = minwid;
4140 item[curitem].maxwid = maxwid;
4141 s++;
4142 curitem++;
4143 continue;
4144 }
4145 if (vim_strchr(STL_ALL, *s) == NULL)
4146 {
4147 s++;
4148 continue;
4149 }
4150 opt = *s++;
4151
4152 /* OK - now for the real work */
4153 base = 'D';
4154 itemisflag = FALSE;
4155 fillable = TRUE;
4156 num = -1;
4157 str = NULL;
4158 switch (opt)
4159 {
4160 case STL_FILEPATH:
4161 case STL_FULLPATH:
4162 case STL_FILENAME:
4163 fillable = FALSE; /* don't change ' ' to fillchar */
4164 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004165 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 else
4167 {
4168 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004169 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4171 }
4172 trans_characters(NameBuff, MAXPATHL);
4173 if (opt != STL_FILENAME)
4174 str = NameBuff;
4175 else
4176 str = gettail(NameBuff);
4177 break;
4178
4179 case STL_VIM_EXPR: /* '{' */
4180 itemisflag = TRUE;
4181 t = p;
4182 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4183 *p++ = *s++;
4184 if (*s != '}') /* missing '}' or out of space */
4185 break;
4186 s++;
4187 *p = 0;
4188 p = t;
4189
4190#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004191 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4193
4194 o_curbuf = curbuf;
4195 o_curwin = curwin;
4196 curwin = wp;
4197 curbuf = wp->w_buffer;
4198
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004199 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200
4201 curwin = o_curwin;
4202 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004203 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204
4205 if (str != NULL && *str != 0)
4206 {
4207 if (*skipdigits(str) == NUL)
4208 {
4209 num = atoi((char *)str);
4210 vim_free(str);
4211 str = NULL;
4212 itemisflag = FALSE;
4213 }
4214 }
4215#endif
4216 break;
4217
4218 case STL_LINE:
4219 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4220 ? 0L : (long)(wp->w_cursor.lnum);
4221 break;
4222
4223 case STL_NUMLINES:
4224 num = wp->w_buffer->b_ml.ml_line_count;
4225 break;
4226
4227 case STL_COLUMN:
4228 num = !(State & INSERT) && empty_line
4229 ? 0 : (int)wp->w_cursor.col + 1;
4230 break;
4231
4232 case STL_VIRTCOL:
4233 case STL_VIRTCOL_ALT:
4234 /* In list mode virtcol needs to be recomputed */
4235 virtcol = wp->w_virtcol;
4236 if (wp->w_p_list && lcs_tab1 == NUL)
4237 {
4238 wp->w_p_list = FALSE;
4239 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4240 wp->w_p_list = TRUE;
4241 }
4242 ++virtcol;
4243 /* Don't display %V if it's the same as %c. */
4244 if (opt == STL_VIRTCOL_ALT
4245 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4246 ? 0 : (int)wp->w_cursor.col + 1)))
4247 break;
4248 num = (long)virtcol;
4249 break;
4250
4251 case STL_PERCENTAGE:
4252 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4253 (long)wp->w_buffer->b_ml.ml_line_count);
4254 break;
4255
4256 case STL_ALTPERCENT:
4257 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004258 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 break;
4260
4261 case STL_ARGLISTSTAT:
4262 fillable = FALSE;
4263 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004264 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 str = tmp;
4266 break;
4267
4268 case STL_KEYMAP:
4269 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004270 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 str = tmp;
4272 break;
4273 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004274#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004275 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276#else
4277 num = 0;
4278#endif
4279 break;
4280
4281 case STL_BUFNO:
4282 num = wp->w_buffer->b_fnum;
4283 break;
4284
4285 case STL_OFFSET_X:
4286 base = 'X';
4287 case STL_OFFSET:
4288#ifdef FEAT_BYTEOFF
4289 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4290 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4291 0L : l + 1 + (!(State & INSERT) && empty_line ?
4292 0 : (int)wp->w_cursor.col);
4293#endif
4294 break;
4295
4296 case STL_BYTEVAL_X:
4297 base = 'X';
4298 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004299 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 if (num == NL)
4301 num = 0;
4302 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4303 num = NL;
4304 break;
4305
4306 case STL_ROFLAG:
4307 case STL_ROFLAG_ALT:
4308 itemisflag = TRUE;
4309 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004310 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 break;
4312
4313 case STL_HELPFLAG:
4314 case STL_HELPFLAG_ALT:
4315 itemisflag = TRUE;
4316 if (wp->w_buffer->b_help)
4317 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004318 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 break;
4320
4321#ifdef FEAT_AUTOCMD
4322 case STL_FILETYPE:
4323 if (*wp->w_buffer->b_p_ft != NUL
4324 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4325 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004326 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4327 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 str = tmp;
4329 }
4330 break;
4331
4332 case STL_FILETYPE_ALT:
4333 itemisflag = TRUE;
4334 if (*wp->w_buffer->b_p_ft != NUL
4335 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4336 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004337 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4338 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 for (t = tmp; *t != 0; t++)
4340 *t = TOUPPER_LOC(*t);
4341 str = tmp;
4342 }
4343 break;
4344#endif
4345
4346#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4347 case STL_PREVIEWFLAG:
4348 case STL_PREVIEWFLAG_ALT:
4349 itemisflag = TRUE;
4350 if (wp->w_p_pvw)
4351 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4352 : _("[Preview]"));
4353 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004354
4355 case STL_QUICKFIX:
4356 if (bt_quickfix(wp->w_buffer))
4357 str = (char_u *)(wp->w_llist_ref
4358 ? _(msg_loclist)
4359 : _(msg_qflist));
4360 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361#endif
4362
4363 case STL_MODIFIED:
4364 case STL_MODIFIED_ALT:
4365 itemisflag = TRUE;
4366 switch ((opt == STL_MODIFIED_ALT)
4367 + bufIsChanged(wp->w_buffer) * 2
4368 + (!wp->w_buffer->b_p_ma) * 4)
4369 {
4370 case 2: str = (char_u *)"[+]"; break;
4371 case 3: str = (char_u *)",+"; break;
4372 case 4: str = (char_u *)"[-]"; break;
4373 case 5: str = (char_u *)",-"; break;
4374 case 6: str = (char_u *)"[+-]"; break;
4375 case 7: str = (char_u *)",+-"; break;
4376 }
4377 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004378
4379 case STL_HIGHLIGHT:
4380 t = s;
4381 while (*s != '#' && *s != NUL)
4382 ++s;
4383 if (*s == '#')
4384 {
4385 item[curitem].type = Highlight;
4386 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004387 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004388 curitem++;
4389 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004390 if (*s != NUL)
4391 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004392 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
4394
4395 item[curitem].start = p;
4396 item[curitem].type = Normal;
4397 if (str != NULL && *str)
4398 {
4399 t = str;
4400 if (itemisflag)
4401 {
4402 if ((t[0] && t[1])
4403 && ((!prevchar_isitem && *t == ',')
4404 || (prevchar_isflag && *t == ' ')))
4405 t++;
4406 prevchar_isflag = TRUE;
4407 }
4408 l = vim_strsize(t);
4409 if (l > 0)
4410 prevchar_isitem = TRUE;
4411 if (l > maxwid)
4412 {
4413 while (l >= maxwid)
4414#ifdef FEAT_MBYTE
4415 if (has_mbyte)
4416 {
4417 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004418 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 }
4420 else
4421#endif
4422 l -= byte2cells(*t++);
4423 if (p + 1 >= out + outlen)
4424 break;
4425 *p++ = '<';
4426 }
4427 if (minwid > 0)
4428 {
4429 for (; l < minwid && p + 1 < out + outlen; l++)
4430 {
4431 /* Don't put a "-" in front of a digit. */
4432 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4433 *p++ = ' ';
4434 else
4435 *p++ = fillchar;
4436 }
4437 minwid = 0;
4438 }
4439 else
4440 minwid *= -1;
4441 while (*t && p + 1 < out + outlen)
4442 {
4443 *p++ = *t++;
4444 /* Change a space by fillchar, unless fillchar is '-' and a
4445 * digit follows. */
4446 if (fillable && p[-1] == ' '
4447 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4448 p[-1] = fillchar;
4449 }
4450 for (; l < minwid && p + 1 < out + outlen; l++)
4451 *p++ = fillchar;
4452 }
4453 else if (num >= 0)
4454 {
4455 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4456 char_u nstr[20];
4457
4458 if (p + 20 >= out + outlen)
4459 break; /* not sufficient space */
4460 prevchar_isitem = TRUE;
4461 t = nstr;
4462 if (opt == STL_VIRTCOL_ALT)
4463 {
4464 *t++ = '-';
4465 minwid--;
4466 }
4467 *t++ = '%';
4468 if (zeropad)
4469 *t++ = '0';
4470 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004471 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 *t = 0;
4473
4474 for (n = num, l = 1; n >= nbase; n /= nbase)
4475 l++;
4476 if (opt == STL_VIRTCOL_ALT)
4477 l++;
4478 if (l > maxwid)
4479 {
4480 l += 2;
4481 n = l - maxwid;
4482 while (l-- > maxwid)
4483 num /= nbase;
4484 *t++ = '>';
4485 *t++ = '%';
4486 *t = t[-3];
4487 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004488 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4489 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004492 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4493 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 p += STRLEN(p);
4495 }
4496 else
4497 item[curitem].type = Empty;
4498
4499 if (opt == STL_VIM_EXPR)
4500 vim_free(str);
4501
4502 if (num >= 0 || (!itemisflag && str && *str))
4503 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4504 curitem++;
4505 }
4506 *p = NUL;
4507 itemcnt = curitem;
4508
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004509#ifdef FEAT_EVAL
4510 if (usefmt != fmt)
4511 vim_free(usefmt);
4512#endif
4513
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 width = vim_strsize(out);
4515 if (maxwidth > 0 && width > maxwidth)
4516 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004517 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 l = 0;
4519 if (itemcnt == 0)
4520 s = out;
4521 else
4522 {
4523 for ( ; l < itemcnt; l++)
4524 if (item[l].type == Trunc)
4525 {
4526 /* Truncate at %< item. */
4527 s = item[l].start;
4528 break;
4529 }
4530 if (l == itemcnt)
4531 {
4532 /* No %< item, truncate first item. */
4533 s = item[0].start;
4534 l = 0;
4535 }
4536 }
4537
4538 if (width - vim_strsize(s) >= maxwidth)
4539 {
4540 /* Truncation mark is beyond max length */
4541#ifdef FEAT_MBYTE
4542 if (has_mbyte)
4543 {
4544 s = out;
4545 width = 0;
4546 for (;;)
4547 {
4548 width += ptr2cells(s);
4549 if (width >= maxwidth)
4550 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004551 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
4553 /* Fill up for half a double-wide character. */
4554 while (++width < maxwidth)
4555 *s++ = fillchar;
4556 }
4557 else
4558#endif
4559 s = out + maxwidth - 1;
4560 for (l = 0; l < itemcnt; l++)
4561 if (item[l].start > s)
4562 break;
4563 itemcnt = l;
4564 *s++ = '>';
4565 *s = 0;
4566 }
4567 else
4568 {
4569#ifdef FEAT_MBYTE
4570 if (has_mbyte)
4571 {
4572 n = 0;
4573 while (width >= maxwidth)
4574 {
4575 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004576 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 }
4578 }
4579 else
4580#endif
4581 n = width - maxwidth + 1;
4582 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004583 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 *s = '<';
4585
4586 /* Fill up for half a double-wide character. */
4587 while (++width < maxwidth)
4588 {
4589 s = s + STRLEN(s);
4590 *s++ = fillchar;
4591 *s = NUL;
4592 }
4593
4594 --n; /* count the '<' */
4595 for (; l < itemcnt; l++)
4596 {
4597 if (item[l].start - n >= s)
4598 item[l].start -= n;
4599 else
4600 item[l].start = s;
4601 }
4602 }
4603 width = maxwidth;
4604 }
4605 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4606 {
4607 /* Apply STL_MIDDLE if any */
4608 for (l = 0; l < itemcnt; l++)
4609 if (item[l].type == Middle)
4610 break;
4611 if (l < itemcnt)
4612 {
4613 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004614 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 for (s = item[l].start; s < p; s++)
4616 *s = fillchar;
4617 for (l++; l < itemcnt; l++)
4618 item[l].start += maxwidth - width;
4619 width = maxwidth;
4620 }
4621 }
4622
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004623 /* Store the info about highlighting. */
4624 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004626 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 for (l = 0; l < itemcnt; l++)
4628 {
4629 if (item[l].type == Highlight)
4630 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004631 sp->start = item[l].start;
4632 sp->userhl = item[l].minwid;
4633 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 }
4635 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004636 sp->start = NULL;
4637 sp->userhl = 0;
4638 }
4639
4640 /* Store the info about tab pages labels. */
4641 if (tabtab != NULL)
4642 {
4643 sp = tabtab;
4644 for (l = 0; l < itemcnt; l++)
4645 {
4646 if (item[l].type == TabPage)
4647 {
4648 sp->start = item[l].start;
4649 sp->userhl = item[l].minwid;
4650 sp++;
4651 }
4652 }
4653 sp->start = NULL;
4654 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 }
4656
4657 return width;
4658}
4659#endif /* FEAT_STL_OPT */
4660
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004661#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4662 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004664 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4665 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 */
4667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004668get_rel_pos(
4669 win_T *wp,
4670 char_u *buf,
4671 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672{
4673 long above; /* number of lines above window */
4674 long below; /* number of lines below window */
4675
Bram Moolenaar0027c212015-01-07 13:31:52 +01004676 if (buflen < 3) /* need at least 3 chars for writing */
4677 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 above = wp->w_topline - 1;
4679#ifdef FEAT_DIFF
4680 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004681 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4682 above = 0; /* All buffer lines are displayed and there is an
4683 * indication of filler lines, that can be considered
4684 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#endif
4686 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4687 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004688 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4689 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004691 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004693 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 ? (int)(above / ((above + below) / 100L))
4695 : (int)(above * 100L / (above + below)));
4696}
4697#endif
4698
4699/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004700 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 * Return TRUE if it was appended.
4702 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004703 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004704append_arg_number(
4705 win_T *wp,
4706 char_u *buf,
4707 int buflen,
4708 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709{
4710 char_u *p;
4711
4712 if (ARGCOUNT <= 1) /* nothing to do */
4713 return FALSE;
4714
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004715 p = buf + STRLEN(buf); /* go to the end of the buffer */
4716 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 return FALSE;
4718 *p++ = ' ';
4719 *p++ = '(';
4720 if (add_file)
4721 {
4722 STRCPY(p, "file ");
4723 p += 5;
4724 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004725 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4726 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4728 return TRUE;
4729}
4730
4731/*
4732 * If fname is not a full path, make it a full path.
4733 * Returns pointer to allocated memory (NULL for failure).
4734 */
4735 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004736fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737{
4738 /*
4739 * Force expanding the path always for Unix, because symbolic links may
4740 * mess up the full path name, even though it starts with a '/'.
4741 * Also expand when there is ".." in the file name, try to remove it,
4742 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004743 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * For MS-Windows also expand names like "longna~1" to "longname".
4745 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004746#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 return FullName_save(fname, TRUE);
4748#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004749 if (!vim_isAbsName(fname)
4750 || strstr((char *)fname, "..") != NULL
4751 || strstr((char *)fname, "//") != NULL
4752# ifdef BACKSLASH_IN_FILENAME
4753 || strstr((char *)fname, "\\\\") != NULL
4754# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004755# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 )
4759 return FullName_save(fname, FALSE);
4760
4761 fname = vim_strsave(fname);
4762
Bram Moolenaar9b942202007-10-03 12:31:33 +00004763# ifdef USE_FNAME_CASE
4764# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004766# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 {
4768 if (fname != NULL)
4769 fname_case(fname, 0); /* set correct case for file name */
4770 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004771# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773 return fname;
4774#endif
4775}
4776
4777/*
4778 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4779 * "ffname" becomes a pointer to allocated memory (or NULL).
4780 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004782fname_expand(
4783 buf_T *buf UNUSED,
4784 char_u **ffname,
4785 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786{
4787 if (*ffname == NULL) /* if no file name given, nothing to do */
4788 return;
4789 if (*sfname == NULL) /* if no short file name given, use ffname */
4790 *sfname = *ffname;
4791 *ffname = fix_fname(*ffname); /* expand to full path */
4792
4793#ifdef FEAT_SHORTCUT
4794 if (!buf->b_p_bin)
4795 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004796 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797
4798 /* If the file name is a shortcut file, use the file it links to. */
4799 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004800 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 {
4802 vim_free(*ffname);
4803 *ffname = rfname;
4804 *sfname = rfname;
4805 }
4806 }
4807#endif
4808}
4809
4810/*
4811 * Get the file name for an argument list entry.
4812 */
4813 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004814alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815{
4816 buf_T *bp;
4817
4818 /* Use the name from the associated buffer if it exists. */
4819 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004820 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 return aep->ae_fname;
4822 return bp->b_fname;
4823}
4824
4825#if defined(FEAT_WINDOWS) || defined(PROTO)
4826/*
4827 * do_arg_all(): Open up to 'count' windows, one for each argument.
4828 */
4829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004830do_arg_all(
4831 int count,
4832 int forceit, /* hide buffers in current windows */
4833 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834{
4835 int i;
4836 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004837 char_u *opened; /* Array of weight for which args are open:
4838 * 0: not opened
4839 * 1: opened in other tab
4840 * 2: opened in curtab
4841 * 3: opened in curtab and curwin
4842 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004843 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 int use_firstwin = FALSE; /* use first window for arglist */
4845 int split_ret = OK;
4846 int p_ea_save;
4847 alist_T *alist; /* argument list to be used */
4848 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004849 tabpage_T *tpnext;
4850 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004851 win_T *old_curwin, *last_curwin;
4852 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004853 win_T *new_curwin = NULL;
4854 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856 if (ARGCOUNT <= 0)
4857 {
4858 /* Don't give an error message. We don't want it when the ":all"
4859 * command is in the .vimrc. */
4860 return;
4861 }
4862 setpcmark();
4863
4864 opened_len = ARGCOUNT;
4865 opened = alloc_clear((unsigned)opened_len);
4866 if (opened == NULL)
4867 return;
4868
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004869 /* Autocommands may do anything to the argument list. Make sure it's not
4870 * freed while we are working here by "locking" it. We still have to
4871 * watch out for its size to be changed. */
4872 alist = curwin->w_alist;
4873 ++alist->al_refcount;
4874
4875 old_curwin = curwin;
4876 old_curtab = curtab;
4877
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004878# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004880# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881
4882 /*
4883 * Try closing all windows that are not in the argument list.
4884 * Also close windows that are not full width;
4885 * When 'hidden' or "forceit" set the buffer becomes hidden.
4886 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004887 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004889 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004890 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004891 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004893 tpnext = curtab->tp_next;
4894 for (wp = firstwin; wp != NULL; wp = wpnext)
4895 {
4896 wpnext = wp->w_next;
4897 buf = wp->w_buffer;
4898 if (buf->b_ffname == NULL
Bram Moolenaar5a030a52016-12-01 17:48:29 +01004899 || (!keep_tabs && (buf->b_nwindows > 1
4900 || wp->w_width != Columns)))
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004901 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004902 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004904 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004905 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004907 if (i < alist->al_ga.ga_len
4908 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4909 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4910 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004912 int weight = 1;
4913
4914 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004915 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004916 ++weight;
4917 if (old_curwin == wp)
4918 ++weight;
4919 }
4920
4921 if (weight > (int)opened[i])
4922 {
4923 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004924 if (i == 0)
4925 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004926 if (new_curwin != NULL)
4927 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004928 new_curwin = wp;
4929 new_curtab = curtab;
4930 }
4931 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004932 else if (keep_tabs)
4933 i = opened_len;
4934
4935 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004936 {
4937 /* Use the current argument list for all windows
4938 * containing a file from it. */
4939 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004940 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004941 ++wp->w_alist->al_refcount;
4942 }
4943 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 }
4946 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004947 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004949 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004951 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004952 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004954 /* If the buffer was changed, and we would like to hide it,
4955 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004956 if (!P_HID(buf) && buf->b_nwindows <= 1
4957 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004959#ifdef FEAT_AUTOCMD
4960 bufref_T bufref;
4961
4962 set_bufref(&bufref, buf);
4963#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004964 (void)autowrite(buf, FALSE);
4965#ifdef FEAT_AUTOCMD
4966 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004967 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004968 {
4969 wpnext = firstwin; /* start all over... */
4970 continue;
4971 }
4972#endif
4973 }
4974#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004975 /* don't close last window */
Bram Moolenaara1f4cb92016-11-06 15:25:42 +01004976 if (ONE_WINDOW
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004977 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004978#endif
4979 use_firstwin = TRUE;
4980#ifdef FEAT_WINDOWS
4981 else
4982 {
4983 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4984# ifdef FEAT_AUTOCMD
4985 /* check if autocommands removed the next window */
4986 if (!win_valid(wpnext))
4987 wpnext = firstwin; /* start all over... */
4988# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 }
4990#endif
4991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 }
4993 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004994
4995 /* Without the ":tab" modifier only do the current tab page. */
4996 if (had_tab == 0 || tpnext == NULL)
4997 break;
4998
4999# ifdef FEAT_AUTOCMD
5000 /* check if autocommands removed the next tab page */
5001 if (!valid_tabpage(tpnext))
5002 tpnext = first_tabpage; /* start all over...*/
5003# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005004 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006
5007 /*
5008 * Open a window for files in the argument list that don't have one.
5009 * ARGCOUNT may change while doing this, because of autocommands.
5010 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005011 if (count > opened_len || count <= 0)
5012 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013
5014#ifdef FEAT_AUTOCMD
5015 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5016 ++autocmd_no_enter;
5017 ++autocmd_no_leave;
5018#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005019 last_curwin = curwin;
5020 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005022#ifdef FEAT_WINDOWS
5023 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
5024 * leaving an empty tab page when executed locally. */
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01005025 if (keep_tabs && BUFEMPTY() && curbuf->b_nwindows == 1
Bram Moolenaar910f66f2006-04-05 20:41:53 +00005026 && curbuf->b_ffname == NULL && !curbuf->b_changed)
5027 use_firstwin = TRUE;
5028#endif
5029
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005030 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
5032 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
5033 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005034 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 {
5036 /* Move the already present window to below the current window */
5037 if (curwin->w_arg_idx != i)
5038 {
5039 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
5040 {
5041 if (wpnext->w_arg_idx == i)
5042 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005043 if (keep_tabs)
5044 {
5045 new_curwin = wpnext;
5046 new_curtab = curtab;
5047 }
5048 else
5049 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 break;
5051 }
5052 }
5053 }
5054 }
5055 else if (split_ret == OK)
5056 {
5057 if (!use_firstwin) /* split current window */
5058 {
5059 p_ea_save = p_ea;
5060 p_ea = TRUE; /* use space from all windows */
5061 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5062 p_ea = p_ea_save;
5063 if (split_ret == FAIL)
5064 continue;
5065 }
5066#ifdef FEAT_AUTOCMD
5067 else /* first window: do autocmd for leaving this buffer */
5068 --autocmd_no_leave;
5069#endif
5070
5071 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005072 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 */
5074 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005075 if (i == 0)
5076 {
5077 new_curwin = curwin;
5078 new_curtab = curtab;
5079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5081 ECMD_ONE,
5082 ((P_HID(curwin->w_buffer)
5083 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005084 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085#ifdef FEAT_AUTOCMD
5086 if (use_firstwin)
5087 ++autocmd_no_leave;
5088#endif
5089 use_firstwin = FALSE;
5090 }
5091 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005092
5093 /* When ":tab" was used open a new tab for a new window repeatedly. */
5094 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5095 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
5097
5098 /* Remove the "lock" on the argument list. */
5099 alist_unlink(alist);
5100
5101#ifdef FEAT_AUTOCMD
5102 --autocmd_no_enter;
5103#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005104 /* restore last referenced tabpage's curwin */
5105 if (last_curtab != new_curtab)
5106 {
5107 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005108 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005109 if (win_valid(last_curwin))
5110 win_enter(last_curwin, FALSE);
5111 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005112 /* to window with first arg */
5113 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005114 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005115 if (win_valid(new_curwin))
5116 win_enter(new_curwin, FALSE);
5117
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118#ifdef FEAT_AUTOCMD
5119 --autocmd_no_leave;
5120#endif
5121 vim_free(opened);
5122}
5123
5124# if defined(FEAT_LISTCMDS) || defined(PROTO)
5125/*
5126 * Open a window for a number of buffers.
5127 */
5128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005129ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130{
5131 buf_T *buf;
5132 win_T *wp, *wpnext;
5133 int split_ret = OK;
5134 int p_ea_save;
5135 int open_wins = 0;
5136 int r;
5137 int count; /* Maximum number of windows to open. */
5138 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005139#ifdef FEAT_WINDOWS
5140 int had_tab = cmdmod.tab;
5141 tabpage_T *tpnext;
5142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143
5144 if (eap->addr_count == 0) /* make as many windows as possible */
5145 count = 9999;
5146 else
5147 count = eap->line2; /* make as many windows as specified */
5148 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5149 all = FALSE;
5150 else
5151 all = TRUE;
5152
5153 setpcmark();
5154
5155#ifdef FEAT_GUI
5156 need_mouse_correct = TRUE;
5157#endif
5158
5159 /*
5160 * Close superfluous windows (two windows for the same buffer).
5161 * Also close windows that are not full-width.
5162 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005163#ifdef FEAT_WINDOWS
5164 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005165 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005166 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005169 tpnext = curtab->tp_next;
5170 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005172 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005173 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005174#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005175 || ((cmdmod.split & WSP_VERT)
5176 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005177 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005178 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005179 || (had_tab > 0 && wp != firstwin)
5180#endif
Bram Moolenaar459ca562016-11-10 18:16:33 +01005181 ) && !ONE_WINDOW
Bram Moolenaar362ce482012-06-06 19:02:45 +02005182#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005183 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005184#endif
5185 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005186 {
5187 win_close(wp, FALSE);
5188#ifdef FEAT_AUTOCMD
5189 wpnext = firstwin; /* just in case an autocommand does
5190 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005191 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005192 open_wins = 0;
5193#endif
5194 }
5195 else
5196 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005198
5199#ifdef FEAT_WINDOWS
5200 /* Without the ":tab" modifier only do the current tab page. */
5201 if (had_tab == 0 || tpnext == NULL)
5202 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005203 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005205#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206
5207 /*
5208 * Go through the buffer list. When a buffer doesn't have a window yet,
5209 * open one. Otherwise move the window to the right position.
5210 * Watch out for autocommands that delete buffers or windows!
5211 */
5212#ifdef FEAT_AUTOCMD
5213 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5214 ++autocmd_no_enter;
5215#endif
5216 win_enter(lastwin, FALSE);
5217#ifdef FEAT_AUTOCMD
5218 ++autocmd_no_leave;
5219#endif
5220 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5221 {
5222 /* Check if this buffer needs a window */
5223 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5224 continue;
5225
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005226#ifdef FEAT_WINDOWS
5227 if (had_tab != 0)
5228 {
5229 /* With the ":tab" modifier don't move the window. */
5230 if (buf->b_nwindows > 0)
5231 wp = lastwin; /* buffer has a window, skip it */
5232 else
5233 wp = NULL;
5234 }
5235 else
5236#endif
5237 {
5238 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005239 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005240 if (wp->w_buffer == buf)
5241 break;
5242 /* If the buffer already has a window, move it */
5243 if (wp != NULL)
5244 win_move_after(wp, curwin);
5245 }
5246
5247 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005249#ifdef FEAT_AUTOCMD
5250 bufref_T bufref;
5251
5252 set_bufref(&bufref, buf);
5253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 /* Split the window and put the buffer in it */
5255 p_ea_save = p_ea;
5256 p_ea = TRUE; /* use space from all windows */
5257 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5258 ++open_wins;
5259 p_ea = p_ea_save;
5260 if (split_ret == FAIL)
5261 continue;
5262
5263 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005264#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 swap_exists_action = SEA_DIALOG;
5266#endif
5267 set_curbuf(buf, DOBUF_GOTO);
5268#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005269 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005271 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005272#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005274# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 break;
5276 }
5277#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005278#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 if (swap_exists_action == SEA_QUIT)
5280 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005281# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5282 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005283
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005284 /* Reset the error/interrupt/exception state here so that
5285 * aborting() returns FALSE when closing a window. */
5286 enter_cleanup(&cs);
5287# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005288
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005289 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 win_close(curwin, TRUE);
5291 --open_wins;
5292 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005293 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005294
5295# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5296 /* Restore the error/interrupt/exception state if not
5297 * discarded by a new aborting error, interrupt, or uncaught
5298 * exception. */
5299 leave_cleanup(&cs);
5300# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 }
5302 else
5303 handle_swap_exists(NULL);
5304#endif
5305 }
5306
5307 ui_breakcheck();
5308 if (got_int)
5309 {
5310 (void)vgetc(); /* only break the file loading, not the rest */
5311 break;
5312 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005313#ifdef FEAT_EVAL
5314 /* Autocommands deleted the buffer or aborted script processing!!! */
5315 if (aborting())
5316 break;
5317#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005318#ifdef FEAT_WINDOWS
5319 /* When ":tab" was used open a new tab for a new window repeatedly. */
5320 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5321 cmdmod.tab = 9999;
5322#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
5324#ifdef FEAT_AUTOCMD
5325 --autocmd_no_enter;
5326#endif
5327 win_enter(firstwin, FALSE); /* back to first window */
5328#ifdef FEAT_AUTOCMD
5329 --autocmd_no_leave;
5330#endif
5331
5332 /*
5333 * Close superfluous windows.
5334 */
5335 for (wp = lastwin; open_wins > count; )
5336 {
5337 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5338 || autowrite(wp->w_buffer, FALSE) == OK);
5339#ifdef FEAT_AUTOCMD
5340 if (!win_valid(wp))
5341 {
5342 /* BufWrite Autocommands made the window invalid, start over */
5343 wp = lastwin;
5344 }
5345 else
5346#endif
5347 if (r)
5348 {
5349 win_close(wp, !P_HID(wp->w_buffer));
5350 --open_wins;
5351 wp = lastwin;
5352 }
5353 else
5354 {
5355 wp = wp->w_prev;
5356 if (wp == NULL)
5357 break;
5358 }
5359 }
5360}
5361# endif /* FEAT_LISTCMDS */
5362
5363#endif /* FEAT_WINDOWS */
5364
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005365static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005366
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367/*
5368 * do_modelines() - process mode lines for the current file
5369 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005370 * "flags" can be:
5371 * OPT_WINONLY only set options local to window
5372 * OPT_NOWIN don't set options local to window
5373 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 * Returns immediately if the "ml" option isn't set.
5375 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005377do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005379 linenr_T lnum;
5380 int nmlines;
5381 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5384 return;
5385
5386 /* Disallow recursive entry here. Can happen when executing a modeline
5387 * triggers an autocommand, which reloads modelines with a ":do". */
5388 if (entered)
5389 return;
5390
5391 ++entered;
5392 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5393 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005394 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 nmlines = 0;
5396
5397 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5398 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005399 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 nmlines = 0;
5401 --entered;
5402}
5403
5404#include "version.h" /* for version number */
5405
5406/*
5407 * chk_modeline() - check a single line for a mode string
5408 * Return FAIL if an error encountered.
5409 */
5410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005411chk_modeline(
5412 linenr_T lnum,
5413 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414{
5415 char_u *s;
5416 char_u *e;
5417 char_u *linecopy; /* local copy of any modeline found */
5418 int prev;
5419 int vers;
5420 int end;
5421 int retval = OK;
5422 char_u *save_sourcing_name;
5423 linenr_T save_sourcing_lnum;
5424#ifdef FEAT_EVAL
5425 scid_T save_SID;
5426#endif
5427
5428 prev = -1;
5429 for (s = ml_get(lnum); *s != NUL; ++s)
5430 {
5431 if (prev == -1 || vim_isspace(prev))
5432 {
5433 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5434 || STRNCMP(s, "vi:", (size_t)3) == 0)
5435 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005436 /* Accept both "vim" and "Vim". */
5437 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 {
5439 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5440 e = s + 4;
5441 else
5442 e = s + 3;
5443 vers = getdigits(&e);
5444 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005445 && (s[0] != 'V'
5446 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 && (s[3] == ':'
5448 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5449 || (VIM_VERSION_100 < vers && s[3] == '<')
5450 || (VIM_VERSION_100 > vers && s[3] == '>')
5451 || (VIM_VERSION_100 == vers && s[3] == '=')))
5452 break;
5453 }
5454 }
5455 prev = *s;
5456 }
5457
5458 if (*s)
5459 {
5460 do /* skip over "ex:", "vi:" or "vim:" */
5461 ++s;
5462 while (s[-1] != ':');
5463
5464 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5465 if (linecopy == NULL)
5466 return FAIL;
5467
5468 save_sourcing_lnum = sourcing_lnum;
5469 save_sourcing_name = sourcing_name;
5470 sourcing_lnum = lnum; /* prepare for emsg() */
5471 sourcing_name = (char_u *)"modelines";
5472
5473 end = FALSE;
5474 while (end == FALSE)
5475 {
5476 s = skipwhite(s);
5477 if (*s == NUL)
5478 break;
5479
5480 /*
5481 * Find end of set command: ':' or end of line.
5482 * Skip over "\:", replacing it with ":".
5483 */
5484 for (e = s; *e != ':' && *e != NUL; ++e)
5485 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005486 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487 if (*e == NUL)
5488 end = TRUE;
5489
5490 /*
5491 * If there is a "set" command, require a terminating ':' and
5492 * ignore the stuff after the ':'.
5493 * "vi:set opt opt opt: foo" -- foo not interpreted
5494 * "vi:opt opt opt: foo" -- foo interpreted
5495 * Accept "se" for compatibility with Elvis.
5496 */
5497 if (STRNCMP(s, "set ", (size_t)4) == 0
5498 || STRNCMP(s, "se ", (size_t)3) == 0)
5499 {
5500 if (*e != ':') /* no terminating ':'? */
5501 break;
5502 end = TRUE;
5503 s = vim_strchr(s, ' ') + 1;
5504 }
5505 *e = NUL; /* truncate the set command */
5506
5507 if (*s != NUL) /* skip over an empty "::" */
5508 {
5509#ifdef FEAT_EVAL
5510 save_SID = current_SID;
5511 current_SID = SID_MODELINE;
5512#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005513 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514#ifdef FEAT_EVAL
5515 current_SID = save_SID;
5516#endif
5517 if (retval == FAIL) /* stop if error found */
5518 break;
5519 }
5520 s = e + 1; /* advance to next part */
5521 }
5522
5523 sourcing_lnum = save_sourcing_lnum;
5524 sourcing_name = save_sourcing_name;
5525
5526 vim_free(linecopy);
5527 }
5528 return retval;
5529}
5530
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005531#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005533read_viminfo_bufferlist(
5534 vir_T *virp,
5535 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 char_u *tab;
5538 linenr_T lnum;
5539 colnr_T col;
5540 buf_T *buf;
5541 char_u *sfname;
5542 char_u *xline;
5543
5544 /* Handle long line and escaped characters. */
5545 xline = viminfo_readstring(virp, 1, FALSE);
5546
5547 /* don't read in if there are files on the command-line or if writing: */
5548 if (xline != NULL && !writing && ARGCOUNT == 0
5549 && find_viminfo_parameter('%') != NULL)
5550 {
5551 /* Format is: <fname> Tab <lnum> Tab <col>.
5552 * Watch out for a Tab in the file name, work from the end. */
5553 lnum = 0;
5554 col = 0;
5555 tab = vim_strrchr(xline, '\t');
5556 if (tab != NULL)
5557 {
5558 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005559 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 tab = vim_strrchr(xline, '\t');
5561 if (tab != NULL)
5562 {
5563 *tab++ = '\0';
5564 lnum = atol((char *)tab);
5565 }
5566 }
5567
5568 /* Expand "~/" in the file name at "line + 1" to a full path.
5569 * Then try shortening it by comparing with the current directory */
5570 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005571 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5574 if (buf != NULL) /* just in case... */
5575 {
5576 buf->b_last_cursor.lnum = lnum;
5577 buf->b_last_cursor.col = col;
5578 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5579 }
5580 }
5581 vim_free(xline);
5582
5583 return viminfo_readline(virp);
5584}
5585
5586 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005587write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588{
5589 buf_T *buf;
5590#ifdef FEAT_WINDOWS
5591 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005592 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#endif
5594 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005595 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
5597 if (find_viminfo_parameter('%') == NULL)
5598 return;
5599
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005600 /* Without a number -1 is returned: do all buffers. */
5601 max_buffers = get_viminfo_parameter('%');
5602
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005604#define LINE_BUF_LEN (MAXPATHL + 40)
5605 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 if (line == NULL)
5607 return;
5608
5609#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005610 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 set_last_cursor(win);
5612#else
5613 set_last_cursor(curwin);
5614#endif
5615
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005616 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005617 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 {
5619 if (buf->b_fname == NULL
5620 || !buf->b_p_bl
5621#ifdef FEAT_QUICKFIX
5622 || bt_quickfix(buf)
5623#endif
5624 || removable(buf->b_ffname))
5625 continue;
5626
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005627 if (max_buffers-- == 0)
5628 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 putc('%', fp);
5630 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005631 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 (long)buf->b_last_cursor.lnum,
5633 buf->b_last_cursor.col);
5634 viminfo_writestring(fp, line);
5635 }
5636 vim_free(line);
5637}
5638#endif
5639
5640
5641/*
5642 * Return special buffer name.
5643 * Returns NULL when the buffer has a normal file name.
5644 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005645 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005646buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5649 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005650 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005651 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005652 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005653
5654 /*
5655 * For location list window, w_llist_ref points to the location list.
5656 * For quickfix window, w_llist_ref is NULL.
5657 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005658 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005659 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005660 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005661 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005662 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663#endif
5664#ifdef FEAT_QUICKFIX
5665 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5666 * contains the name as specified by the user */
5667 if (bt_nofile(buf))
5668 {
5669 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005670 return buf->b_sfname;
5671 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 }
5673#endif
5674 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005675 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 return NULL;
5677}
5678
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005679#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5680 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5681 || defined(PROTO)
5682/*
5683 * Find a window for buffer "buf".
5684 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5685 * If not found FAIL is returned.
5686 */
5687 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005688find_win_for_buf(
5689 buf_T *buf,
5690 win_T **wp,
5691 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005692{
5693 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5694 if ((*wp)->w_buffer == buf)
5695 goto win_found;
5696 return FAIL;
5697win_found:
5698 return OK;
5699}
5700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701
5702#if defined(FEAT_SIGNS) || defined(PROTO)
5703/*
5704 * Insert the sign into the signlist.
5705 */
5706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005707insert_sign(
5708 buf_T *buf, /* buffer to store sign in */
5709 signlist_T *prev, /* previous sign entry */
5710 signlist_T *next, /* next sign entry */
5711 int id, /* sign ID */
5712 linenr_T lnum, /* line number which gets the mark */
5713 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714{
5715 signlist_T *newsign;
5716
5717 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5718 if (newsign != NULL)
5719 {
5720 newsign->id = id;
5721 newsign->lnum = lnum;
5722 newsign->typenr = typenr;
5723 newsign->next = next;
5724#ifdef FEAT_NETBEANS_INTG
5725 newsign->prev = prev;
5726 if (next != NULL)
5727 next->prev = newsign;
5728#endif
5729
5730 if (prev == NULL)
5731 {
5732 /* When adding first sign need to redraw the windows to create the
5733 * column for signs. */
5734 if (buf->b_signlist == NULL)
5735 {
5736 redraw_buf_later(buf, NOT_VALID);
5737 changed_cline_bef_curs();
5738 }
5739
5740 /* first sign in signlist */
5741 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005742#ifdef FEAT_NETBEANS_INTG
5743 if (netbeans_active())
5744 buf->b_has_sign_column = TRUE;
5745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746 }
5747 else
5748 prev->next = newsign;
5749 }
5750}
5751
5752/*
5753 * Add the sign into the signlist. Find the right spot to do it though.
5754 */
5755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005756buf_addsign(
5757 buf_T *buf, /* buffer to store sign in */
5758 int id, /* sign ID */
5759 linenr_T lnum, /* line number which gets the mark */
5760 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761{
5762 signlist_T *sign; /* a sign in the signlist */
5763 signlist_T *prev; /* the previous sign */
5764
5765 prev = NULL;
5766 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5767 {
5768 if (lnum == sign->lnum && id == sign->id)
5769 {
5770 sign->typenr = typenr;
5771 return;
5772 }
5773 else if (
5774#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5775 id < 0 &&
5776#endif
5777 lnum < sign->lnum)
5778 {
5779#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5780 /* XXX - GRP: Is this because of sign slide problem? Or is it
5781 * really needed? Or is it because we allow multiple signs per
5782 * line? If so, should I add that feature to FEAT_SIGNS?
5783 */
5784 while (prev != NULL && prev->lnum == lnum)
5785 prev = prev->prev;
5786 if (prev == NULL)
5787 sign = buf->b_signlist;
5788 else
5789 sign = prev->next;
5790#endif
5791 insert_sign(buf, prev, sign, id, lnum, typenr);
5792 return;
5793 }
5794 prev = sign;
5795 }
5796#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5797 /* XXX - GRP: See previous comment */
5798 while (prev != NULL && prev->lnum == lnum)
5799 prev = prev->prev;
5800 if (prev == NULL)
5801 sign = buf->b_signlist;
5802 else
5803 sign = prev->next;
5804#endif
5805 insert_sign(buf, prev, sign, id, lnum, typenr);
5806
5807 return;
5808}
5809
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005810/*
5811 * For an existing, placed sign "markId" change the type to "typenr".
5812 * Returns the line number of the sign, or zero if the sign is not found.
5813 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005814 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005815buf_change_sign_type(
5816 buf_T *buf, /* buffer to store sign in */
5817 int markId, /* sign ID */
5818 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 signlist_T *sign; /* a sign in the signlist */
5821
5822 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5823 {
5824 if (sign->id == markId)
5825 {
5826 sign->typenr = typenr;
5827 return sign->lnum;
5828 }
5829 }
5830
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005831 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832}
5833
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005834 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005835buf_getsigntype(
5836 buf_T *buf,
5837 linenr_T lnum,
5838 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 signlist_T *sign; /* a sign in a b_signlist */
5841
5842 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5843 if (sign->lnum == lnum
5844 && (type == SIGN_ANY
5845# ifdef FEAT_SIGN_ICONS
5846 || (type == SIGN_ICON
5847 && sign_get_image(sign->typenr) != NULL)
5848# endif
5849 || (type == SIGN_TEXT
5850 && sign_get_text(sign->typenr) != NULL)
5851 || (type == SIGN_LINEHL
5852 && sign_get_attr(sign->typenr, TRUE) != 0)))
5853 return sign->typenr;
5854 return 0;
5855}
5856
5857
5858 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005859buf_delsign(
5860 buf_T *buf, /* buffer sign is stored in */
5861 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 signlist_T **lastp; /* pointer to pointer to current sign */
5864 signlist_T *sign; /* a sign in a b_signlist */
5865 signlist_T *next; /* the next sign in a b_signlist */
5866 linenr_T lnum; /* line number whose sign was deleted */
5867
5868 lastp = &buf->b_signlist;
5869 lnum = 0;
5870 for (sign = buf->b_signlist; sign != NULL; sign = next)
5871 {
5872 next = sign->next;
5873 if (sign->id == id)
5874 {
5875 *lastp = next;
5876#ifdef FEAT_NETBEANS_INTG
5877 if (next != NULL)
5878 next->prev = sign->prev;
5879#endif
5880 lnum = sign->lnum;
5881 vim_free(sign);
5882 break;
5883 }
5884 else
5885 lastp = &sign->next;
5886 }
5887
5888 /* When deleted the last sign need to redraw the windows to remove the
5889 * sign column. */
5890 if (buf->b_signlist == NULL)
5891 {
5892 redraw_buf_later(buf, NOT_VALID);
5893 changed_cline_bef_curs();
5894 }
5895
5896 return lnum;
5897}
5898
5899
5900/*
5901 * Find the line number of the sign with the requested id. If the sign does
5902 * not exist, return 0 as the line number. This will still let the correct file
5903 * get loaded.
5904 */
5905 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005906buf_findsign(
5907 buf_T *buf, /* buffer to store sign in */
5908 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909{
5910 signlist_T *sign; /* a sign in the signlist */
5911
5912 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5913 if (sign->id == id)
5914 return sign->lnum;
5915
5916 return 0;
5917}
5918
5919 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005920buf_findsign_id(
5921 buf_T *buf, /* buffer whose sign we are searching for */
5922 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923{
5924 signlist_T *sign; /* a sign in the signlist */
5925
5926 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5927 if (sign->lnum == lnum)
5928 return sign->id;
5929
5930 return 0;
5931}
5932
5933
5934# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5935/* see if a given type of sign exists on a specific line */
5936 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005937buf_findsigntype_id(
5938 buf_T *buf, /* buffer whose sign we are searching for */
5939 linenr_T lnum, /* line number of sign */
5940 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 signlist_T *sign; /* a sign in the signlist */
5943
5944 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5945 if (sign->lnum == lnum && sign->typenr == typenr)
5946 return sign->id;
5947
5948 return 0;
5949}
5950
5951
5952# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5953/* return the number of icons on the given line */
5954 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005955buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956{
5957 signlist_T *sign; /* a sign in the signlist */
5958 int count = 0;
5959
5960 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5961 if (sign->lnum == lnum)
5962 if (sign_get_image(sign->typenr) != NULL)
5963 count++;
5964
5965 return count;
5966}
5967# endif /* FEAT_SIGN_ICONS */
5968# endif /* FEAT_NETBEANS_INTG */
5969
5970
5971/*
5972 * Delete signs in buffer "buf".
5973 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005975buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976{
5977 signlist_T *next;
5978
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005979 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005980 * sign column. Not when curwin is NULL (this means we're exiting). */
5981 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005982 {
5983 redraw_buf_later(buf, NOT_VALID);
5984 changed_cline_bef_curs();
5985 }
5986
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 while (buf->b_signlist != NULL)
5988 {
5989 next = buf->b_signlist->next;
5990 vim_free(buf->b_signlist);
5991 buf->b_signlist = next;
5992 }
5993}
5994
5995/*
5996 * Delete all signs in all buffers.
5997 */
5998 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005999buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000{
6001 buf_T *buf; /* buffer we are checking for signs */
6002
Bram Moolenaar29323592016-07-24 22:04:11 +02006003 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006004 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006006}
6007
6008/*
6009 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
6010 */
6011 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006012sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013{
6014 buf_T *buf;
6015 signlist_T *p;
6016 char lbuf[BUFSIZ];
6017
6018 MSG_PUTS_TITLE(_("\n--- Signs ---"));
6019 msg_putchar('\n');
6020 if (rbuf == NULL)
6021 buf = firstbuf;
6022 else
6023 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006024 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025 {
6026 if (buf->b_signlist != NULL)
6027 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006028 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar8820b482017-03-16 17:23:31 +01006029 MSG_PUTS_ATTR(lbuf, HL_ATTR(HLF_D));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 msg_putchar('\n');
6031 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01006032 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006033 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00006034 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
6036 MSG_PUTS(lbuf);
6037 msg_putchar('\n');
6038 }
6039 if (rbuf != NULL)
6040 break;
6041 buf = buf->b_next;
6042 }
6043}
6044
6045/*
6046 * Adjust a placed sign for inserted/deleted lines.
6047 */
6048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006049sign_mark_adjust(
6050 linenr_T line1,
6051 linenr_T line2,
6052 long amount,
6053 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054{
6055 signlist_T *sign; /* a sign in a b_signlist */
6056
6057 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6058 {
6059 if (sign->lnum >= line1 && sign->lnum <= line2)
6060 {
6061 if (amount == MAXLNUM)
6062 sign->lnum = line1;
6063 else
6064 sign->lnum += amount;
6065 }
6066 else if (sign->lnum > line2)
6067 sign->lnum += amount_after;
6068 }
6069}
6070#endif /* FEAT_SIGNS */
6071
6072/*
6073 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6074 */
6075 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006076set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077{
6078 if (on != curbuf->b_p_bl)
6079 {
6080 curbuf->b_p_bl = on;
6081#ifdef FEAT_AUTOCMD
6082 if (on)
6083 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6084 else
6085 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6086#endif
6087 }
6088}
6089
6090/*
6091 * Read the file for "buf" again and check if the contents changed.
6092 * Return TRUE if it changed or this could not be checked.
6093 */
6094 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096{
6097 buf_T *newbuf;
6098 int differ = TRUE;
6099 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 exarg_T ea;
6102
6103 /* Allocate a buffer without putting it in the buffer list. */
6104 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6105 if (newbuf == NULL)
6106 return TRUE;
6107
6108 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6109 if (prep_exarg(&ea, buf) == FAIL)
6110 {
6111 wipe_buffer(newbuf, FALSE);
6112 return TRUE;
6113 }
6114
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 /* set curwin/curbuf to buf and save a few things */
6116 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006117
Bram Moolenaar4770d092006-01-12 23:22:24 +00006118 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 && readfile(buf->b_ffname, buf->b_fname,
6120 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6121 &ea, READ_NEW | READ_DUMMY) == OK)
6122 {
6123 /* compare the two files line by line */
6124 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6125 {
6126 differ = FALSE;
6127 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6128 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6129 {
6130 differ = TRUE;
6131 break;
6132 }
6133 }
6134 }
6135 vim_free(ea.cmd);
6136
Bram Moolenaar071d4272004-06-13 20:20:40 +00006137 /* restore curwin/curbuf and a few other things */
6138 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139
6140 if (curbuf != newbuf) /* safety check */
6141 wipe_buffer(newbuf, FALSE);
6142
6143 return differ;
6144}
6145
6146/*
6147 * Wipe out a buffer and decrement the last buffer number if it was used for
6148 * this buffer. Call this to wipe out a temp buffer that does not contain any
6149 * marks.
6150 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006152wipe_buffer(
6153 buf_T *buf,
6154 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155{
6156 if (buf->b_fnum == top_file_num - 1)
6157 --top_file_num;
6158
6159#ifdef FEAT_AUTOCMD
6160 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006161 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006163 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164#ifdef FEAT_AUTOCMD
6165 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006166 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167#endif
6168}