blob: f375c1f64947d762b333c8ef8abb6b737de43a3f [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. */
114 if (!readonlymode && !bufempty())
115 changed();
116 else if (retval != FAIL)
117 unchanged(curbuf, FALSE);
118
119#ifdef FEAT_AUTOCMD
120# ifdef FEAT_EVAL
121 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
122 curbuf, &retval);
123# else
124 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
125# endif
126#endif
127 }
128 return retval;
129}
130
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200132 * Open current buffer, that is: open the memfile and read the file into
133 * memory.
134 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 */
136 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137open_buffer(
138 int read_stdin, /* read file from stdin */
139 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
140 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141{
142 int retval = OK;
143#ifdef FEAT_AUTOCMD
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200144 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100146#ifdef FEAT_SYN_HL
147 long old_tw = curbuf->b_p_tw;
148#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200149 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151 /*
152 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
153 * When re-entering the same buffer, it should not change, because the
154 * user may have reset the flag by hand.
155 */
156 if (readonlymode && curbuf->b_ffname != NULL
157 && (curbuf->b_flags & BF_NEVERLOADED))
158 curbuf->b_p_ro = TRUE;
159
Bram Moolenaar4770d092006-01-12 23:22:24 +0000160 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 {
162 /*
163 * There MUST be a memfile, otherwise we can't do anything
164 * If we can't create one for the current buffer, take another buffer
165 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100166 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200167 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (curbuf->b_ml.ml_mfp != NULL)
169 break;
170 /*
171 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000172 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 */
174 if (curbuf == NULL)
175 {
176 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
177 getout(2);
178 }
179 EMSG(_("E83: Cannot allocate buffer, using other one..."));
180 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100181#ifdef FEAT_SYN_HL
182 if (old_tw != curbuf->b_p_tw)
183 check_colorcolumn(curwin);
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 return FAIL;
186 }
187
188#ifdef FEAT_AUTOCMD
189 /* The autocommands in readfile() may change the buffer, but only AFTER
190 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200191 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 modified_was_set = FALSE;
193#endif
194
195 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100196 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
198 if (curbuf->b_ffname != NULL
199#ifdef FEAT_NETBEANS_INTG
200 && netbeansReadFile
201#endif
202 )
203 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100204 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200205#ifdef UNIX
206 int save_bin = curbuf->b_p_bin;
207 int perm;
208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#ifdef FEAT_NETBEANS_INTG
210 int oldFire = netbeansFireChanges;
211
212 netbeansFireChanges = 0;
213#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200214#ifdef UNIX
215 perm = mch_getperm(curbuf->b_ffname);
216 if (perm >= 0 && (0
217# ifdef S_ISFIFO
218 || S_ISFIFO(perm)
219# endif
220# ifdef S_ISSOCK
221 || S_ISSOCK(perm)
222# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200223# ifdef OPEN_CHR_FILES
224 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
225# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200226 ))
227 read_fifo = TRUE;
228 if (read_fifo)
229 curbuf->b_p_bin = TRUE;
230#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100231 if (shortmess(SHM_FILEINFO))
232 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200234 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200235 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
236#ifdef UNIX
237 if (read_fifo)
238 {
239 curbuf->b_p_bin = save_bin;
240 if (retval == OK)
241 retval = read_buffer(FALSE, eap, flags);
242 }
243#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100244 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_NETBEANS_INTG
246 netbeansFireChanges = oldFire;
247#endif
248 /* Help buffer is filtered. */
249 if (curbuf->b_help)
250 fix_help_buffer();
251 }
252 else if (read_stdin)
253 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256 /*
257 * First read the text in binary mode into the buffer.
258 * Then read from that same buffer and append at the end. This makes
259 * it possible to retry when 'fileformat' or 'fileencoding' was
260 * guessed wrong.
261 */
262 curbuf->b_p_bin = TRUE;
263 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200264 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
265 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 curbuf->b_p_bin = save_bin;
267 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200268 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 }
270
271 /* if first time loading this buffer, init b_chartab[] */
272 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100275#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100276 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100277#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280 /*
281 * Set/reset the Changed flag first, autocmds may change the buffer.
282 * Apply the automatic commands, before processing the modelines.
283 * So the modelines have priority over auto commands.
284 */
285 /* When reading stdin, the buffer contents always needs writing, so set
286 * the changed flag. Unless in readonly mode: "ls | gview -".
287 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000288 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifdef FEAT_AUTOCMD
290 || modified_was_set /* ":set modified" used in autocmd */
291# ifdef FEAT_EVAL
292 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
293# endif
294#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000295 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 changed();
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200297 else if (retval != FAIL && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 unchanged(curbuf, FALSE);
299 save_file_ff(curbuf); /* keep this fileformat */
300
301 /* require "!" to overwrite the file, because it wasn't read completely */
302#ifdef FEAT_EVAL
303 if (aborting())
304#else
305 if (got_int)
306#endif
307 curbuf->b_flags |= BF_READERR;
308
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000309#ifdef FEAT_FOLDING
310 /* Need to update automatic folding. Do this before the autocommands,
311 * they may use the fold info. */
312 foldUpdateAll(curwin);
313#endif
314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_AUTOCMD
316 /* need to set w_topline, unless some autocommand already did that. */
317 if (!(curwin->w_valid & VALID_TOPLINE))
318 {
319 curwin->w_topline = 1;
320# ifdef FEAT_DIFF
321 curwin->w_topfill = 0;
322# endif
323 }
324# ifdef FEAT_EVAL
325 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
326# else
327 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
328# endif
329#endif
330
331 if (retval != FAIL)
332 {
333#ifdef FEAT_AUTOCMD
334 /*
335 * The autocommands may have changed the current buffer. Apply the
336 * modelines to the correct buffer, if it still exists and is loaded.
337 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200338 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
340 aco_save_T aco;
341
342 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200343 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000345 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
347
348#ifdef FEAT_AUTOCMD
349# ifdef FEAT_EVAL
350 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
351 &retval);
352# else
353 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
354# endif
355
356 /* restore curwin/curbuf and a few other things */
357 aucmd_restbuf(&aco);
358 }
359#endif
360 }
361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 return retval;
363}
364
365/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200366 * Store "buf" in "bufref" and set the free count.
367 */
368 void
369set_bufref(bufref_T *bufref, buf_T *buf)
370{
371 bufref->br_buf = buf;
372 bufref->br_buf_free_count = buf_free_count;
373}
374
375/*
376 * Return TRUE if "bufref->br_buf" points to a valid buffer.
377 * Only goes through the buffer list if buf_free_count changed.
378 */
379 int
380bufref_valid(bufref_T *bufref)
381{
382 return bufref->br_buf_free_count == buf_free_count
383 ? TRUE : buf_valid(bufref->br_buf);
384}
385
386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200388 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 */
390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100391buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392{
393 buf_T *bp;
394
Bram Moolenaar82404332016-07-10 17:00:38 +0200395 /* Assume that we more often have a recent buffer, start with the last
396 * one. */
397 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (bp == buf)
399 return TRUE;
400 return FALSE;
401}
402
403/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200404 * A hash table used to quickly lookup a buffer by its number.
405 */
406static hashtab_T buf_hashtab;
407
408 static void
409buf_hashtab_add(buf_T *buf)
410{
411 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
412 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
413 EMSG(_("E931: Buffer cannot be registered"));
414}
415
416 static void
417buf_hashtab_remove(buf_T *buf)
418{
419 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
420
421 if (!HASHITEM_EMPTY(hi))
422 hash_remove(&buf_hashtab, hi);
423}
424
425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 * Close the link to a buffer.
427 * "action" is used when there is no longer a window for the buffer.
428 * It can be:
429 * 0 buffer becomes hidden
430 * DOBUF_UNLOAD buffer is unloaded
431 * DOBUF_DELETE buffer is unloaded and removed from buffer list
432 * DOBUF_WIPE buffer is unloaded and really deleted
433 * When doing all but the first one on the current buffer, the caller should
434 * get a new buffer very soon!
435 *
436 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100437 *
438 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
439 * cause there to be only one window with this buffer. e.g. when ":quit" is
440 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 */
442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100443close_buffer(
444 win_T *win, /* if not NULL, set b_last_cursor */
445 buf_T *buf,
446 int action,
447 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449#ifdef FEAT_AUTOCMD
450 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100451 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200452 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453#endif
454 int unload_buf = (action != 0);
455 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
456 int wipe_buf = (action == DOBUF_WIPE);
457
458#ifdef FEAT_QUICKFIX
459 /*
460 * Force unloading or deleting when 'bufhidden' says so.
461 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
462 * "hide" (otherwise we could never free or delete a buffer).
463 */
464 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
465 {
466 del_buf = TRUE;
467 unload_buf = TRUE;
468 }
469 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
470 {
471 del_buf = TRUE;
472 unload_buf = TRUE;
473 wipe_buf = TRUE;
474 }
475 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
476 unload_buf = TRUE;
477#endif
478
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200479 /* Disallow deleting the buffer when it is locked (already being closed or
480 * halfway a command that relies on it). Unloading is allowed. */
481 if (buf->b_locked > 0 && (del_buf || wipe_buf))
482 {
483 EMSG(_("E937: Attempt to delete a buffer that is in use"));
484 return;
485 }
486
Bram Moolenaar3be85852014-06-12 14:01:31 +0200487 if (win != NULL
488#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200489 && win_valid_any_tab(win) /* in case autocommands closed the window */
Bram Moolenaar3be85852014-06-12 14:01:31 +0200490#endif
491 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492 {
493 /* Set b_last_cursor when closing the last window for the buffer.
494 * Remember the last cursor position and window options of the buffer.
495 * This used to be only for the current window, but then options like
496 * 'foldmethod' may be lost with a ":only" command. */
497 if (buf->b_nwindows == 1)
498 set_last_cursor(win);
499 buflist_setfpos(buf, win,
500 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
501 win->w_cursor.col, TRUE);
502 }
503
504#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200505 set_bufref(&bufref, buf);
506
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 /* When the buffer is no longer in a window, trigger BufWinLeave */
508 if (buf->b_nwindows == 1)
509 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200510 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200511 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
512 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200513 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100514 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200515 /* Autocommands deleted the buffer. */
516aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100517 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100519 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200520 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200521 if (abort_if_last && one_window())
522 /* Autocommands made this the only window. */
523 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
525 /* When the buffer becomes hidden, but is not unloaded, trigger
526 * BufHidden */
527 if (!unload_buf)
528 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200529 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200530 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
531 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200532 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200533 /* Autocommands deleted the buffer. */
534 goto aucmd_abort;
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# ifdef FEAT_EVAL
541 if (aborting()) /* autocmds may abort script processing */
542 return;
543# endif
544 }
545 nwindows = buf->b_nwindows;
546#endif
547
548 /* decrease the link count from windows (unless not in any window) */
549 if (buf->b_nwindows > 0)
550 --buf->b_nwindows;
551
552 /* Return when a window is displaying the buffer or when it's not
553 * unloaded. */
554 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557 /* Always remove the buffer when there is no file name. */
558 if (buf->b_ffname == NULL)
559 del_buf = TRUE;
560
561 /*
562 * Free all things allocated for this buffer.
563 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
564 */
565#ifdef FEAT_AUTOCMD
566 /* Remember if we are closing the current buffer. Restore the number of
567 * windows, so that autocommands in buf_freeall() don't get confused. */
568 is_curbuf = (buf == curbuf);
569 buf->b_nwindows = nwindows;
570#endif
571
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200572 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573
574#ifdef FEAT_AUTOCMD
575 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200576 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 return;
578# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000579 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return;
581# endif
582
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 /*
584 * It's possible that autocommands change curbuf to the one being deleted.
585 * This might cause the previous curbuf to be deleted unexpectedly. But
586 * in some cases it's OK to delete the curbuf, because a new one is
587 * obtained anyway. Therefore only return if curbuf changed to the
588 * deleted buffer.
589 */
590 if (buf == curbuf && !is_curbuf)
591 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200592
593 if (
594#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200595 win_valid_any_tab(win) &&
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200596#else
597 win != NULL &&
598#endif
599 win->w_buffer == buf)
600 win->w_buffer = NULL; /* make sure we don't use the buffer now */
601
602 /* Autocommands may have opened or closed windows for this buffer.
603 * Decrement the count for the close we do here. */
604 if (buf->b_nwindows > 0)
605 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606#endif
607
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000608 /* Change directories when the 'acd' option is set. */
609 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 /*
612 * Remove the buffer from the list.
613 */
614 if (wipe_buf)
615 {
616#ifdef FEAT_SUN_WORKSHOP
617 if (usingSunWorkShop)
618 workshop_file_closed_lineno((char *)buf->b_ffname,
619 (int)buf->b_last_cursor.lnum);
620#endif
621 vim_free(buf->b_ffname);
622 vim_free(buf->b_sfname);
623 if (buf->b_prev == NULL)
624 firstbuf = buf->b_next;
625 else
626 buf->b_prev->b_next = buf->b_next;
627 if (buf->b_next == NULL)
628 lastbuf = buf->b_prev;
629 else
630 buf->b_next->b_prev = buf->b_prev;
631 free_buffer(buf);
632 }
633 else
634 {
635 if (del_buf)
636 {
637 /* Free all internal variables and reset option values, to make
638 * ":bdel" compatible with Vim 5.7. */
639 free_buffer_stuff(buf, TRUE);
640
641 /* Make it look like a new buffer. */
642 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
643
644 /* Init the options when loaded again. */
645 buf->b_p_initialized = FALSE;
646 }
647 buf_clear_file(buf);
648 if (del_buf)
649 buf->b_p_bl = FALSE;
650 }
651}
652
653/*
654 * Make buffer not contain a file.
655 */
656 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100657buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658{
659 buf->b_ml.ml_line_count = 1;
660 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 buf->b_p_eol = TRUE;
663 buf->b_start_eol = TRUE;
664#ifdef FEAT_MBYTE
665 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000666 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667#endif
668 buf->b_ml.ml_mfp = NULL;
669 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
670#ifdef FEAT_NETBEANS_INTG
671 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#endif
673}
674
675/*
676 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200677 * the file. Careful: get here with "curwin" NULL when exiting.
678 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200679 * BFA_DEL buffer is going to be deleted
680 * BFA_WIPE buffer is going to be wiped out
681 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100684buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685{
686#ifdef FEAT_AUTOCMD
687 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200688 bufref_T bufref;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200689# ifdef FEAT_WINDOWS
690 int is_curwin = (curwin!= NULL && curwin->w_buffer == buf);
691 win_T *the_curwin = curwin;
692 tabpage_T *the_curtab = curtab;
693# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694
Bram Moolenaar5a497892016-09-03 16:29:04 +0200695 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200696 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200697 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200698 if (buf->b_ml.ml_mfp != NULL)
699 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200700 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
701 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200702 && !bufref_valid(&bufref))
703 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200704 return;
705 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200706 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200708 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
709 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200710 && !bufref_valid(&bufref))
711 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 return;
713 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200714 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200716 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
717 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200718 && !bufref_valid(&bufref))
719 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 return;
721 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200722 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200723
724# ifdef FEAT_WINDOWS
725 /* If the buffer was in curwin and the window has changed, go back to that
726 * window, if it still exists. This avoids that ":edit x" triggering a
727 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
728 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
729 {
730 block_autocmds();
731 goto_tabpage_win(the_curtab, the_curwin);
732 unblock_autocmds();
733 }
734# endif
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000737 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 return;
739# endif
740
741 /*
742 * It's possible that autocommands change curbuf to the one being deleted.
743 * This might cause curbuf to be deleted unexpectedly. But in some cases
744 * it's OK to delete the curbuf, because a new one is obtained anyway.
745 * Therefore only return if curbuf changed to the deleted buffer.
746 */
747 if (buf == curbuf && !is_curbuf)
748 return;
749#endif
750#ifdef FEAT_DIFF
751 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
752#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200753#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100754 /* Remove any ownsyntax, unless exiting. */
755 if (firstwin != NULL && curwin->w_buffer == buf)
756 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200757#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000758
759#ifdef FEAT_FOLDING
760 /* No folds in an empty buffer. */
761# ifdef FEAT_WINDOWS
762 {
763 win_T *win;
764 tabpage_T *tp;
765
766 FOR_ALL_TAB_WINDOWS(tp, win)
767 if (win->w_buffer == buf)
768 clearFolding(win);
769 }
770# else
771 if (curwin->w_buffer == buf)
772 clearFolding(curwin);
773# endif
774#endif
775
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776#ifdef FEAT_TCL
777 tcl_buffer_free(buf);
778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 ml_close(buf, TRUE); /* close and delete the memline/memfile */
780 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200781 if ((flags & BFA_KEEP_UNDO) == 0)
782 {
783 u_blockfree(buf); /* free the memory allocated for undo */
784 u_clearall(buf); /* reset all undo information */
785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200787 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000789 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790}
791
792/*
793 * Free a buffer structure and the things it contains related to the buffer
794 * itself (not the file, that must have been done already).
795 */
796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100797free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200799 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200801#ifdef FEAT_EVAL
802 unref_var_dict(buf->b_vars);
803#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200804#ifdef FEAT_LUA
805 lua_buffer_free(buf);
806#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000807#ifdef FEAT_MZSCHEME
808 mzscheme_buffer_free(buf);
809#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810#ifdef FEAT_PERL
811 perl_buf_free(buf);
812#endif
813#ifdef FEAT_PYTHON
814 python_buffer_free(buf);
815#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200816#ifdef FEAT_PYTHON3
817 python3_buffer_free(buf);
818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819#ifdef FEAT_RUBY
820 ruby_buffer_free(buf);
821#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200822#ifdef FEAT_JOB_CHANNEL
823 channel_buffer_free(buf);
824#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200825
826 buf_hashtab_remove(buf);
827
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200828#ifdef FEAT_AUTOCMD
829 aubuflocal_remove(buf);
830
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200831 if (autocmd_busy)
832 {
833 /* Do not free the buffer structure while autocommands are executing,
834 * it's still needed. Free it when autocmd_busy is reset. */
835 buf->b_next = au_pending_free_buf;
836 au_pending_free_buf = buf;
837 }
838 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000839#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200840 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841}
842
843/*
844 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
845 */
846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100847free_buffer_stuff(
848 buf_T *buf,
849 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850{
851 if (free_options)
852 {
853 clear_wininfo(buf); /* including window-local options */
854 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200855#ifdef FEAT_SPELL
856 ga_clear(&buf->b_s.b_langp);
857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000858 }
859#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200860 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
861 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862#endif
863#ifdef FEAT_USR_CMDS
864 uc_clear(&buf->b_ucmds); /* clear local user commands */
865#endif
866#ifdef FEAT_SIGNS
867 buf_delete_signs(buf); /* delete any signs */
868#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000869#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200870 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872#ifdef FEAT_LOCALMAP
873 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
874 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
875#endif
876#ifdef FEAT_MBYTE
877 vim_free(buf->b_start_fenc);
878 buf->b_start_fenc = NULL;
879#endif
880}
881
882/*
883 * Free the b_wininfo list for buffer "buf".
884 */
885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100886clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887{
888 wininfo_T *wip;
889
890 while (buf->b_wininfo != NULL)
891 {
892 wip = buf->b_wininfo;
893 buf->b_wininfo = wip->wi_next;
894 if (wip->wi_optset)
895 {
896 clear_winopt(&wip->wi_opt);
897#ifdef FEAT_FOLDING
898 deleteFoldRecurse(&wip->wi_folds);
899#endif
900 }
901 vim_free(wip);
902 }
903}
904
905#if defined(FEAT_LISTCMDS) || defined(PROTO)
906/*
907 * Go to another buffer. Handles the result of the ATTENTION dialog.
908 */
909 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100910goto_buffer(
911 exarg_T *eap,
912 int start,
913 int dir,
914 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000916# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200917 bufref_T old_curbuf;
918
919 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000920
921 swap_exists_action = SEA_DIALOG;
922# endif
923 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
924 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000925# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
927 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000928# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
929 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000930
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000931 /* Reset the error/interrupt/exception state here so that
932 * aborting() returns FALSE when closing a window. */
933 enter_cleanup(&cs);
934# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000935
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000936 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 win_close(curwin, TRUE);
938 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000939 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000940
941# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
942 /* Restore the error/interrupt/exception state if not discarded by a
943 * new aborting error, interrupt, or uncaught exception. */
944 leave_cleanup(&cs);
945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 }
947 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200948 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949# endif
950}
951#endif
952
Bram Moolenaare64ac772005-12-07 20:54:59 +0000953#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954/*
955 * Handle the situation of swap_exists_action being set.
956 * It is allowed for "old_curbuf" to be NULL or invalid.
957 */
958 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200959handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000961# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
962 cleanup_T cs;
963# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100964#ifdef FEAT_SYN_HL
965 long old_tw = curbuf->b_p_tw;
966#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200967 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 if (swap_exists_action == SEA_QUIT)
970 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000971# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
972 /* Reset the error/interrupt/exception state here so that
973 * aborting() returns FALSE when closing a buffer. */
974 enter_cleanup(&cs);
975# endif
976
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 /* User selected Quit at ATTENTION prompt. Go back to previous
978 * buffer. If that buffer is gone or the same as the current one,
979 * open a new, empty buffer. */
980 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000981 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100982 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200983 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
984 || old_curbuf->br_buf == curbuf)
985 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
986 else
987 buf = old_curbuf->br_buf;
988 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100989 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200990 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100991#ifdef FEAT_SYN_HL
992 if (old_tw != curbuf->b_p_tw)
993 check_colorcolumn(curwin);
994#endif
995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000997
998# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
999 /* Restore the error/interrupt/exception state if not discarded by a
1000 * new aborting error, interrupt, or uncaught exception. */
1001 leave_cleanup(&cs);
1002# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003 }
1004 else if (swap_exists_action == SEA_RECOVER)
1005 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001006# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1007 /* Reset the error/interrupt/exception state here so that
1008 * aborting() returns FALSE when closing a buffer. */
1009 enter_cleanup(&cs);
1010# endif
1011
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 /* User selected Recover at ATTENTION prompt. */
1013 msg_scroll = TRUE;
1014 ml_recover();
1015 MSG_PUTS("\n"); /* don't overwrite the last message */
1016 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001017 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001018
1019# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1020 /* Restore the error/interrupt/exception state if not discarded by a
1021 * new aborting error, interrupt, or uncaught exception. */
1022 leave_cleanup(&cs);
1023# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 }
1025 swap_exists_action = SEA_NONE;
1026}
1027#endif
1028
1029#if defined(FEAT_LISTCMDS) || defined(PROTO)
1030/*
1031 * do_bufdel() - delete or unload buffer(s)
1032 *
1033 * addr_count == 0: ":bdel" - delete current buffer
1034 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1035 * buffer "end_bnr", then any other arguments.
1036 * addr_count == 2: ":N,N bdel" - delete buffers in range
1037 *
1038 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1039 * DOBUF_DEL (":bdel")
1040 *
1041 * Returns error message or NULL
1042 */
1043 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001044do_bufdel(
1045 int command,
1046 char_u *arg, /* pointer to extra arguments */
1047 int addr_count,
1048 int start_bnr, /* first buffer number in a range */
1049 int end_bnr, /* buffer nr or last buffer nr in a range */
1050 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
1052 int do_current = 0; /* delete current buffer? */
1053 int deleted = 0; /* number of buffers deleted */
1054 char_u *errormsg = NULL; /* return value */
1055 int bnr; /* buffer number */
1056 char_u *p;
1057
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 if (addr_count == 0)
1059 {
1060 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1061 }
1062 else
1063 {
1064 if (addr_count == 2)
1065 {
1066 if (*arg) /* both range and argument is not allowed */
1067 return (char_u *)_(e_trailing);
1068 bnr = start_bnr;
1069 }
1070 else /* addr_count == 1 */
1071 bnr = end_bnr;
1072
1073 for ( ;!got_int; ui_breakcheck())
1074 {
1075 /*
1076 * delete the current buffer last, otherwise when the
1077 * current buffer is deleted, the next buffer becomes
1078 * the current one and will be loaded, which may then
1079 * also be deleted, etc.
1080 */
1081 if (bnr == curbuf->b_fnum)
1082 do_current = bnr;
1083 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1084 forceit) == OK)
1085 ++deleted;
1086
1087 /*
1088 * find next buffer number to delete/unload
1089 */
1090 if (addr_count == 2)
1091 {
1092 if (++bnr > end_bnr)
1093 break;
1094 }
1095 else /* addr_count == 1 */
1096 {
1097 arg = skipwhite(arg);
1098 if (*arg == NUL)
1099 break;
1100 if (!VIM_ISDIGIT(*arg))
1101 {
1102 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001103 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1104 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 if (bnr < 0) /* failed */
1106 break;
1107 arg = p;
1108 }
1109 else
1110 bnr = getdigits(&arg);
1111 }
1112 }
1113 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1114 FORWARD, do_current, forceit) == OK)
1115 ++deleted;
1116
1117 if (deleted == 0)
1118 {
1119 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001120 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001122 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001124 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 errormsg = IObuff;
1126 }
1127 else if (deleted >= p_report)
1128 {
1129 if (command == DOBUF_UNLOAD)
1130 {
1131 if (deleted == 1)
1132 MSG(_("1 buffer unloaded"));
1133 else
1134 smsg((char_u *)_("%d buffers unloaded"), deleted);
1135 }
1136 else if (command == DOBUF_DEL)
1137 {
1138 if (deleted == 1)
1139 MSG(_("1 buffer deleted"));
1140 else
1141 smsg((char_u *)_("%d buffers deleted"), deleted);
1142 }
1143 else
1144 {
1145 if (deleted == 1)
1146 MSG(_("1 buffer wiped out"));
1147 else
1148 smsg((char_u *)_("%d buffers wiped out"), deleted);
1149 }
1150 }
1151 }
1152
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153
1154 return errormsg;
1155}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001156#endif /* FEAT_LISTCMDS */
1157
1158#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1159 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001161static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001162
1163/*
1164 * Make the current buffer empty.
1165 * Used when it is wiped out and it's the last buffer.
1166 */
1167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001168empty_curbuf(
1169 int close_others,
1170 int forceit,
1171 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001172{
1173 int retval;
1174 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001175 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001176
1177 if (action == DOBUF_UNLOAD)
1178 {
1179 EMSG(_("E90: Cannot unload last buffer"));
1180 return FAIL;
1181 }
1182
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001183 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001184#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001185 if (close_others)
1186 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001187 close_windows(buf, TRUE);
1188#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001189
1190 setpcmark();
1191 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1192 forceit ? ECMD_FORCEIT : 0, curwin);
1193
1194 /*
1195 * do_ecmd() may create a new buffer, then we have to delete
1196 * the old one. But do_ecmd() may have done that already, check
1197 * if the buffer still exists.
1198 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001199 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001200 close_buffer(NULL, buf, action, FALSE);
1201 if (!close_others)
1202 need_fileinfo = FALSE;
1203 return retval;
1204}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001205/*
1206 * Implementation of the commands for the buffer list.
1207 *
1208 * action == DOBUF_GOTO go to specified buffer
1209 * action == DOBUF_SPLIT split window and go to specified buffer
1210 * action == DOBUF_UNLOAD unload specified buffer(s)
1211 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1212 * action == DOBUF_WIPE delete specified buffer(s) really
1213 *
1214 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1215 * start == DOBUF_FIRST go to "count" buffer from first buffer
1216 * start == DOBUF_LAST go to "count" buffer from last buffer
1217 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1218 *
1219 * Return FAIL or OK.
1220 */
1221 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001222do_buffer(
1223 int action,
1224 int start,
1225 int dir, /* FORWARD or BACKWARD */
1226 int count, /* buffer number or number of buffers */
1227 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228{
1229 buf_T *buf;
1230 buf_T *bp;
1231 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1232 || action == DOBUF_WIPE);
1233
1234 switch (start)
1235 {
1236 case DOBUF_FIRST: buf = firstbuf; break;
1237 case DOBUF_LAST: buf = lastbuf; break;
1238 default: buf = curbuf; break;
1239 }
1240 if (start == DOBUF_MOD) /* find next modified buffer */
1241 {
1242 while (count-- > 0)
1243 {
1244 do
1245 {
1246 buf = buf->b_next;
1247 if (buf == NULL)
1248 buf = firstbuf;
1249 }
1250 while (buf != curbuf && !bufIsChanged(buf));
1251 }
1252 if (!bufIsChanged(buf))
1253 {
1254 EMSG(_("E84: No modified buffer found"));
1255 return FAIL;
1256 }
1257 }
1258 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1259 {
1260 while (buf != NULL && buf->b_fnum != count)
1261 buf = buf->b_next;
1262 }
1263 else
1264 {
1265 bp = NULL;
1266 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1267 {
1268 /* remember the buffer where we start, we come back there when all
1269 * buffers are unlisted. */
1270 if (bp == NULL)
1271 bp = buf;
1272 if (dir == FORWARD)
1273 {
1274 buf = buf->b_next;
1275 if (buf == NULL)
1276 buf = firstbuf;
1277 }
1278 else
1279 {
1280 buf = buf->b_prev;
1281 if (buf == NULL)
1282 buf = lastbuf;
1283 }
1284 /* don't count unlisted buffers */
1285 if (unload || buf->b_p_bl)
1286 {
1287 --count;
1288 bp = NULL; /* use this buffer as new starting point */
1289 }
1290 if (bp == buf)
1291 {
1292 /* back where we started, didn't find anything. */
1293 EMSG(_("E85: There is no listed buffer"));
1294 return FAIL;
1295 }
1296 }
1297 }
1298
1299 if (buf == NULL) /* could not find it */
1300 {
1301 if (start == DOBUF_FIRST)
1302 {
1303 /* don't warn when deleting */
1304 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001305 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307 else if (dir == FORWARD)
1308 EMSG(_("E87: Cannot go beyond last buffer"));
1309 else
1310 EMSG(_("E88: Cannot go before first buffer"));
1311 return FAIL;
1312 }
1313
1314#ifdef FEAT_GUI
1315 need_mouse_correct = TRUE;
1316#endif
1317
1318#ifdef FEAT_LISTCMDS
1319 /*
1320 * delete buffer buf from memory and/or the list
1321 */
1322 if (unload)
1323 {
1324 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001325# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1326 bufref_T bufref;
1327
1328 set_bufref(&bufref, buf);
1329# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330
1331 /* When unloading or deleting a buffer that's already unloaded and
1332 * unlisted: fail silently. */
1333 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1334 return FAIL;
1335
1336 if (!forceit && bufIsChanged(buf))
1337 {
1338#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1339 if ((p_confirm || cmdmod.confirm) && p_write)
1340 {
1341 dialog_changed(buf, FALSE);
1342# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001343 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 /* Autocommand deleted buffer, oops! It's not changed
1345 * now. */
1346 return FAIL;
1347# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001348 /* If it's still changed fail silently, the dialog already
1349 * mentioned why it fails. */
1350 if (bufIsChanged(buf))
1351 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001353 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354#endif
1355 {
1356 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1357 buf->b_fnum);
1358 return FAIL;
1359 }
1360 }
1361
1362 /*
1363 * If deleting the last (listed) buffer, make it empty.
1364 * The last (listed) buffer cannot be unloaded.
1365 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001366 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (bp->b_p_bl && bp != buf)
1368 break;
1369 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001370 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
1372#ifdef FEAT_WINDOWS
1373 /*
1374 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001375 * (unless it's the only window). Repeat this so long as we end up in
1376 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001378 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001379# ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001380 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02001381# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001382 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001383 {
1384 if (win_close(curwin, FALSE) == FAIL)
1385 break;
1386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
1389 /*
1390 * If the buffer to be deleted is not the current one, delete it here.
1391 */
1392 if (buf != curbuf)
1393 {
1394#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001395 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001396 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001398 if (buf->b_nwindows <= 0)
1399 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 return OK;
1401 }
1402
1403 /*
1404 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001405 * There should be another, otherwise it would have been handled
1406 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001407 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 * Then prefer the buffer we most recently visited.
1409 * Else try to find one that is loaded, after the current buffer,
1410 * then before the current buffer.
1411 * Finally use any buffer.
1412 */
1413 buf = NULL; /* selected buffer */
1414 bp = NULL; /* used when no loaded buffer found */
1415#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001416 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1417 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418# ifdef FEAT_JUMPLIST
1419 else
1420# endif
1421#endif
1422#ifdef FEAT_JUMPLIST
1423 if (curwin->w_jumplistlen > 0)
1424 {
1425 int jumpidx;
1426
1427 jumpidx = curwin->w_jumplistidx - 1;
1428 if (jumpidx < 0)
1429 jumpidx = curwin->w_jumplistlen - 1;
1430
1431 forward = jumpidx;
1432 while (jumpidx != curwin->w_jumplistidx)
1433 {
1434 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1435 if (buf != NULL)
1436 {
1437 if (buf == curbuf || !buf->b_p_bl)
1438 buf = NULL; /* skip current and unlisted bufs */
1439 else if (buf->b_ml.ml_mfp == NULL)
1440 {
1441 /* skip unloaded buf, but may keep it for later */
1442 if (bp == NULL)
1443 bp = buf;
1444 buf = NULL;
1445 }
1446 }
1447 if (buf != NULL) /* found a valid buffer: stop searching */
1448 break;
1449 /* advance to older entry in jump list */
1450 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1451 break;
1452 if (--jumpidx < 0)
1453 jumpidx = curwin->w_jumplistlen - 1;
1454 if (jumpidx == forward) /* List exhausted for sure */
1455 break;
1456 }
1457 }
1458#endif
1459
1460 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1461 {
1462 forward = TRUE;
1463 buf = curbuf->b_next;
1464 for (;;)
1465 {
1466 if (buf == NULL)
1467 {
1468 if (!forward) /* tried both directions */
1469 break;
1470 buf = curbuf->b_prev;
1471 forward = FALSE;
1472 continue;
1473 }
1474 /* in non-help buffer, try to skip help buffers, and vv */
1475 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1476 {
1477 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1478 break;
1479 if (bp == NULL) /* remember unloaded buf for later */
1480 bp = buf;
1481 }
1482 if (forward)
1483 buf = buf->b_next;
1484 else
1485 buf = buf->b_prev;
1486 }
1487 }
1488 if (buf == NULL) /* No loaded buffer, use unloaded one */
1489 buf = bp;
1490 if (buf == NULL) /* No loaded buffer, find listed one */
1491 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001492 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 if (buf->b_p_bl && buf != curbuf)
1494 break;
1495 }
1496 if (buf == NULL) /* Still no buffer, just take one */
1497 {
1498 if (curbuf->b_next != NULL)
1499 buf = curbuf->b_next;
1500 else
1501 buf = curbuf->b_prev;
1502 }
1503 }
1504
Bram Moolenaara02471e2014-01-10 16:43:14 +01001505 if (buf == NULL)
1506 {
1507 /* Autocommands must have wiped out all other buffers. Only option
1508 * now is to make the current buffer empty. */
1509 return empty_curbuf(FALSE, forceit, action);
1510 }
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 /*
1513 * make buf current buffer
1514 */
1515 if (action == DOBUF_SPLIT) /* split window first */
1516 {
1517# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001518 /* If 'switchbuf' contains "useopen": jump to first window containing
1519 * "buf" if one exists */
1520 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001521 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001522 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001523 * page containing "buf" if one exists */
1524 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 return OK;
1526 if (win_split(0, 0) == FAIL)
1527# endif
1528 return FAIL;
1529 }
1530#endif
1531
1532 /* go to current buffer - nothing to do */
1533 if (buf == curbuf)
1534 return OK;
1535
1536 /*
1537 * Check if the current buffer may be abandoned.
1538 */
1539 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1540 {
1541#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1542 if ((p_confirm || cmdmod.confirm) && p_write)
1543 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001544# ifdef FEAT_AUTOCMD
1545 bufref_T bufref;
1546
1547 set_bufref(&bufref, buf);
1548# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 dialog_changed(curbuf, FALSE);
1550# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001551 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 /* Autocommand deleted buffer, oops! */
1553 return FAIL;
1554# endif
1555 }
1556 if (bufIsChanged(curbuf))
1557#endif
1558 {
1559 EMSG(_(e_nowrtmsg));
1560 return FAIL;
1561 }
1562 }
1563
1564 /* Go to the other buffer. */
1565 set_curbuf(buf, action);
1566
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001567#if defined(FEAT_LISTCMDS) \
1568 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001570 {
1571 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573#endif
1574
1575#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1576 if (aborting()) /* autocmds may abort script processing */
1577 return FAIL;
1578#endif
1579
1580 return OK;
1581}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584/*
1585 * Set current buffer to "buf". Executes autocommands and closes current
1586 * buffer. "action" tells how to close the current buffer:
1587 * DOBUF_GOTO free or hide it
1588 * DOBUF_SPLIT nothing
1589 * DOBUF_UNLOAD unload it
1590 * DOBUF_DEL delete it
1591 * DOBUF_WIPE wipe it out
1592 */
1593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001594set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595{
1596 buf_T *prevbuf;
1597 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1598 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001599#ifdef FEAT_SYN_HL
1600 long old_tw = curbuf->b_p_tw;
1601#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001602 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001605 if (!cmdmod.keepalt)
1606 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001607 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 /* Don't restart Select mode after switching to another buffer. */
1610 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611
1612 /* close_windows() or apply_autocmds() may change curbuf */
1613 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001614 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001617 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001619 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001621 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622# endif
1623#endif
1624 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001625#ifdef FEAT_SYN_HL
1626 if (prevbuf == curwin->w_buffer)
1627 reset_synblock(curwin);
1628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629#ifdef FEAT_WINDOWS
1630 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001631 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#endif
1633#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001634 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001636 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001638 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001639#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001640 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001641#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001642 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001643 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1645 unload ? action : (action == DOBUF_GOTO
1646 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001647 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001648#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001649 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001650 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001651 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001652#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
1655#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001656 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001657 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001658 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1659 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001660# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001661 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001662# endif
1663# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001664 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001665# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001666 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001668 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001670#ifdef FEAT_SYN_HL
1671 if (old_tw != curbuf->b_p_tw)
1672 check_colorcolumn(curwin);
1673#endif
1674 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675}
1676
1677/*
1678 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001679 * Old curbuf must have been abandoned already! This also means "curbuf" may
1680 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 */
1682 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001683enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 /* Copy buffer and window local option values. Not for a help buffer. */
1686 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1687 if (!buf->b_help)
1688 get_winopts(buf);
1689#ifdef FEAT_FOLDING
1690 else
1691 /* Remove all folds in the window. */
1692 clearFolding(curwin);
1693 foldUpdateAll(curwin); /* update folds (later). */
1694#endif
1695
1696 /* Get the buffer in the current window. */
1697 curwin->w_buffer = buf;
1698 curbuf = buf;
1699 ++curbuf->b_nwindows;
1700
1701#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001702 if (curwin->w_p_diff)
1703 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704#endif
1705
Bram Moolenaara971b822011-09-14 14:43:25 +02001706#ifdef FEAT_SYN_HL
1707 curwin->w_s = &(buf->b_s);
1708#endif
1709
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 /* Cursor on first line by default. */
1711 curwin->w_cursor.lnum = 1;
1712 curwin->w_cursor.col = 0;
1713#ifdef FEAT_VIRTUALEDIT
1714 curwin->w_cursor.coladd = 0;
1715#endif
1716 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001717#ifdef FEAT_AUTOCMD
1718 curwin->w_topline_was_set = FALSE;
1719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001721 /* mark cursor position as being invalid */
1722 curwin->w_valid = 0;
1723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 /* Make sure the buffer is loaded. */
1725 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001726 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001727#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001728 /* If there is no filetype, allow for detecting one. Esp. useful for
1729 * ":ball" used in a autocommand. If there already is a filetype we
1730 * might prefer to keep it. */
1731 if (*curbuf->b_p_ft == NUL)
1732 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001733#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001734
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001735 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 else
1738 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001739 if (!msg_silent)
1740 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1742#ifdef FEAT_AUTOCMD
1743 curwin->w_topline = 1;
1744# ifdef FEAT_DIFF
1745 curwin->w_topfill = 0;
1746# endif
1747 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1748 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1749#endif
1750 }
1751
1752 /* If autocommands did not change the cursor position, restore cursor lnum
1753 * and possibly cursor col. */
1754 if (curwin->w_cursor.lnum == 1 && inindent(0))
1755 buflist_getfpos();
1756
1757 check_arg_idx(curwin); /* check for valid arg_idx */
1758#ifdef FEAT_TITLE
1759 maketitle();
1760#endif
1761#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001762 /* when autocmds didn't change it */
1763 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764#endif
1765 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1766
Bram Moolenaar009b2592004-10-24 19:18:58 +00001767#ifdef FEAT_NETBEANS_INTG
1768 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001769 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001770#endif
1771
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001772 /* Change directories when the 'acd' option is set. */
1773 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774
1775#ifdef FEAT_KEYMAP
1776 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001777 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001779#ifdef FEAT_SPELL
1780 /* May need to set the spell language. Can only do this after the buffer
1781 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001782 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1783 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001784#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001785#ifdef FEAT_VIMINFO
1786 curbuf->b_last_used = vim_time();
1787#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 redraw_later(NOT_VALID);
1790}
1791
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001792#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1793/*
1794 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001795 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001796 */
1797 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001798do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001799{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001800 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001801 && curbuf->b_ffname != NULL
1802 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001803 shorten_fnames(TRUE);
1804}
1805#endif
1806
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807/*
1808 * functions for dealing with the buffer list
1809 */
1810
Bram Moolenaar480778b2016-07-14 22:09:39 +02001811static int top_file_num = 1; /* highest file number */
1812
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813/*
1814 * Add a file name to the buffer list. Return a pointer to the buffer.
1815 * If the same file name already exists return a pointer to that buffer.
1816 * If it does not exist, or if fname == NULL, a new entry is created.
1817 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1818 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1819 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001820 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001821 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1822 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 * This is the ONLY way to create a new buffer.
1824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001826buflist_new(
1827 char_u *ffname, /* full path of fname or relative */
1828 char_u *sfname, /* short fname or NULL */
1829 linenr_T lnum, /* preferred cursor line */
1830 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831{
1832 buf_T *buf;
1833#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001834 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#endif
1836
Bram Moolenaar480778b2016-07-14 22:09:39 +02001837 if (top_file_num == 1)
1838 hash_init(&buf_hashtab);
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1841
1842 /*
1843 * If file name already exists in the list, update the entry.
1844 */
1845#ifdef UNIX
1846 /* On Unix we can use inode numbers when the file exists. Works better
1847 * for hard links. */
1848 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1849 st.st_dev = (dev_T)-1;
1850#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001851 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852#ifdef UNIX
1853 buflist_findname_stat(ffname, &st)
1854#else
1855 buflist_findname(ffname)
1856#endif
1857 ) != NULL)
1858 {
1859 vim_free(ffname);
1860 if (lnum != 0)
1861 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001862
1863 if ((flags & BLN_NOOPT) == 0)
1864 /* copy the options now, if 'cpo' doesn't have 's' and not done
1865 * already */
1866 buf_copy_options(buf, 0);
1867
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1869 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001870#ifdef FEAT_AUTOCMD
1871 bufref_T bufref;
1872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 buf->b_p_bl = TRUE;
1874#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001875 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001877 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001878 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001879 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001880 return NULL;
1881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882#endif
1883 }
1884 return buf;
1885 }
1886
1887 /*
1888 * If the current buffer has no name and no contents, use the current
1889 * buffer. Otherwise: Need to allocate a new buffer structure.
1890 *
1891 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001892 * (A spell file buffer is allocated in spell.c, but that's not a normal
1893 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 */
1895 buf = NULL;
1896 if ((flags & BLN_CURBUF)
1897 && curbuf != NULL
1898 && curbuf->b_ffname == NULL
1899 && curbuf->b_nwindows <= 1
1900 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1901 {
1902 buf = curbuf;
1903#ifdef FEAT_AUTOCMD
1904 /* It's like this buffer is deleted. Watch out for autocommands that
1905 * change curbuf! If that happens, allocate a new buffer anyway. */
1906 if (curbuf->b_p_bl)
1907 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1908 if (buf == curbuf)
1909 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1910# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001911 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 return NULL;
1913# endif
1914#endif
1915#ifdef FEAT_QUICKFIX
1916# ifdef FEAT_AUTOCMD
1917 if (buf == curbuf)
1918# endif
1919 {
1920 /* Make sure 'bufhidden' and 'buftype' are empty */
1921 clear_string_option(&buf->b_p_bh);
1922 clear_string_option(&buf->b_p_bt);
1923 }
1924#endif
1925 }
1926 if (buf != curbuf || curbuf == NULL)
1927 {
1928 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1929 if (buf == NULL)
1930 {
1931 vim_free(ffname);
1932 return NULL;
1933 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001934#ifdef FEAT_EVAL
1935 /* init b: variables */
1936 buf->b_vars = dict_alloc();
1937 if (buf->b_vars == NULL)
1938 {
1939 vim_free(ffname);
1940 vim_free(buf);
1941 return NULL;
1942 }
1943 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 }
1946
1947 if (ffname != NULL)
1948 {
1949 buf->b_ffname = ffname;
1950 buf->b_sfname = vim_strsave(sfname);
1951 }
1952
1953 clear_wininfo(buf);
1954 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1955
1956 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1957 || buf->b_wininfo == NULL)
1958 {
1959 vim_free(buf->b_ffname);
1960 buf->b_ffname = NULL;
1961 vim_free(buf->b_sfname);
1962 buf->b_sfname = NULL;
1963 if (buf != curbuf)
1964 free_buffer(buf);
1965 return NULL;
1966 }
1967
1968 if (buf == curbuf)
1969 {
1970 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001971 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (buf != curbuf) /* autocommands deleted the buffer! */
1973 return NULL;
1974#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001975 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 return NULL;
1977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001979
1980 /* Init the options. */
1981 buf->b_p_initialized = FALSE;
1982 buf_copy_options(buf, BCO_ENTER);
1983
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984#ifdef FEAT_KEYMAP
1985 /* need to reload lmaps and set b:keymap_name */
1986 curbuf->b_kmap_state |= KEYMAP_INIT;
1987#endif
1988 }
1989 else
1990 {
1991 /*
1992 * put new buffer at the end of the buffer list
1993 */
1994 buf->b_next = NULL;
1995 if (firstbuf == NULL) /* buffer list is empty */
1996 {
1997 buf->b_prev = NULL;
1998 firstbuf = buf;
1999 }
2000 else /* append new buffer at end of list */
2001 {
2002 lastbuf->b_next = buf;
2003 buf->b_prev = lastbuf;
2004 }
2005 lastbuf = buf;
2006
2007 buf->b_fnum = top_file_num++;
2008 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2009 {
2010 EMSG(_("W14: Warning: List of file names overflow"));
2011 if (emsg_silent == 0)
2012 {
2013 out_flush();
2014 ui_delay(3000L, TRUE); /* make sure it is noticed */
2015 }
2016 top_file_num = 1;
2017 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002018 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019
2020 /*
2021 * Always copy the options from the current buffer.
2022 */
2023 buf_copy_options(buf, BCO_ALWAYS);
2024 }
2025
2026 buf->b_wininfo->wi_fpos.lnum = lnum;
2027 buf->b_wininfo->wi_win = curwin;
2028
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002029#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002030 hash_init(&buf->b_s.b_keywtab);
2031 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032#endif
2033
2034 buf->b_fname = buf->b_sfname;
2035#ifdef UNIX
2036 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002037 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 else
2039 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002040 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 buf->b_dev = st.st_dev;
2042 buf->b_ino = st.st_ino;
2043 }
2044#endif
2045 buf->b_u_synced = TRUE;
2046 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002047 if (flags & BLN_DUMMY)
2048 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 buf_clear_file(buf);
2050 clrallmarks(buf); /* clear marks */
2051 fmarks_check_names(buf); /* check file marks for this file */
2052 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2053#ifdef FEAT_AUTOCMD
2054 if (!(flags & BLN_DUMMY))
2055 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002056 bufref_T bufref;
2057
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002058 /* Tricky: these autocommands may change the buffer list. They could
2059 * also split the window with re-using the one empty buffer. This may
2060 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002061 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002062 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002063 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002064 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002066 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002067 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002068 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002069 return NULL;
2070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002072 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 return NULL;
2074# endif
2075 }
2076#endif
2077
2078 return buf;
2079}
2080
2081/*
2082 * Free the memory for the options of a buffer.
2083 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2084 * 'fileencoding'.
2085 */
2086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002087free_buf_options(
2088 buf_T *buf,
2089 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090{
2091 if (free_p_ff)
2092 {
2093#ifdef FEAT_MBYTE
2094 clear_string_option(&buf->b_p_fenc);
2095#endif
2096 clear_string_option(&buf->b_p_ff);
2097#ifdef FEAT_QUICKFIX
2098 clear_string_option(&buf->b_p_bh);
2099 clear_string_option(&buf->b_p_bt);
2100#endif
2101 }
2102#ifdef FEAT_FIND_ID
2103 clear_string_option(&buf->b_p_def);
2104 clear_string_option(&buf->b_p_inc);
2105# ifdef FEAT_EVAL
2106 clear_string_option(&buf->b_p_inex);
2107# endif
2108#endif
2109#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2110 clear_string_option(&buf->b_p_inde);
2111 clear_string_option(&buf->b_p_indk);
2112#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002113#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2114 clear_string_option(&buf->b_p_bexpr);
2115#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002116#if defined(FEAT_CRYPT)
2117 clear_string_option(&buf->b_p_cm);
2118#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002119#if defined(FEAT_EVAL)
2120 clear_string_option(&buf->b_p_fex);
2121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122#ifdef FEAT_CRYPT
2123 clear_string_option(&buf->b_p_key);
2124#endif
2125 clear_string_option(&buf->b_p_kp);
2126 clear_string_option(&buf->b_p_mps);
2127 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002128 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 clear_string_option(&buf->b_p_isk);
2130#ifdef FEAT_KEYMAP
2131 clear_string_option(&buf->b_p_keymap);
2132 ga_clear(&buf->b_kmap_ga);
2133#endif
2134#ifdef FEAT_COMMENTS
2135 clear_string_option(&buf->b_p_com);
2136#endif
2137#ifdef FEAT_FOLDING
2138 clear_string_option(&buf->b_p_cms);
2139#endif
2140 clear_string_option(&buf->b_p_nf);
2141#ifdef FEAT_SYN_HL
2142 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002143 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002144#endif
2145#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002146 clear_string_option(&buf->b_s.b_p_spc);
2147 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002148 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002149 buf->b_s.b_cap_prog = NULL;
2150 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151#endif
2152#ifdef FEAT_SEARCHPATH
2153 clear_string_option(&buf->b_p_sua);
2154#endif
2155#ifdef FEAT_AUTOCMD
2156 clear_string_option(&buf->b_p_ft);
2157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158#ifdef FEAT_CINDENT
2159 clear_string_option(&buf->b_p_cink);
2160 clear_string_option(&buf->b_p_cino);
2161#endif
2162#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2163 clear_string_option(&buf->b_p_cinw);
2164#endif
2165#ifdef FEAT_INS_EXPAND
2166 clear_string_option(&buf->b_p_cpt);
2167#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002168#ifdef FEAT_COMPL_FUNC
2169 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002170 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172#ifdef FEAT_QUICKFIX
2173 clear_string_option(&buf->b_p_gp);
2174 clear_string_option(&buf->b_p_mp);
2175 clear_string_option(&buf->b_p_efm);
2176#endif
2177 clear_string_option(&buf->b_p_ep);
2178 clear_string_option(&buf->b_p_path);
2179 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002180 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181#ifdef FEAT_INS_EXPAND
2182 clear_string_option(&buf->b_p_dict);
2183 clear_string_option(&buf->b_p_tsr);
2184#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002185#ifdef FEAT_TEXTOBJ
2186 clear_string_option(&buf->b_p_qe);
2187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002189 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002190#ifdef FEAT_LISP
2191 clear_string_option(&buf->b_p_lw);
2192#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002193 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194}
2195
2196/*
2197 * get alternate file n
2198 * set linenr to lnum or altfpos.lnum if lnum == 0
2199 * also set cursor column to altfpos.col if 'startofline' is not set.
2200 * if (options & GETF_SETMARK) call setpcmark()
2201 * if (options & GETF_ALT) we are jumping to an alternate file.
2202 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2203 *
2204 * return FAIL for failure, OK for success
2205 */
2206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002207buflist_getfile(
2208 int n,
2209 linenr_T lnum,
2210 int options,
2211 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212{
2213 buf_T *buf;
2214#ifdef FEAT_WINDOWS
2215 win_T *wp = NULL;
2216#endif
2217 pos_T *fpos;
2218 colnr_T col;
2219
2220 buf = buflist_findnr(n);
2221 if (buf == NULL)
2222 {
2223 if ((options & GETF_ALT) && n == 0)
2224 EMSG(_(e_noalt));
2225 else
2226 EMSGN(_("E92: Buffer %ld not found"), n);
2227 return FAIL;
2228 }
2229
2230 /* if alternate file is the current buffer, nothing to do */
2231 if (buf == curbuf)
2232 return OK;
2233
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002234 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002235 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002236 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002238 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002239#ifdef FEAT_AUTOCMD
2240 if (curbuf_locked())
2241 return FAIL;
2242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243
2244 /* altfpos may be changed by getfile(), get it now */
2245 if (lnum == 0)
2246 {
2247 fpos = buflist_findfpos(buf);
2248 lnum = fpos->lnum;
2249 col = fpos->col;
2250 }
2251 else
2252 col = 0;
2253
2254#ifdef FEAT_WINDOWS
2255 if (options & GETF_SWITCH)
2256 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002257 /* If 'switchbuf' contains "useopen": jump to first window containing
2258 * "buf" if one exists */
2259 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002261
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002262 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002263 * page containing "buf" if one exists */
2264 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002265 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002266
2267 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2268 * current buffer isn't empty: open new tab or window */
2269 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2270 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002272 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002273 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002274 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2275 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002277 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 }
2279 }
2280#endif
2281
2282 ++RedrawingDisabled;
2283 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2284 lnum, forceit) <= 0)
2285 {
2286 --RedrawingDisabled;
2287
2288 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2289 if (!p_sol && col != 0)
2290 {
2291 curwin->w_cursor.col = col;
2292 check_cursor_col();
2293#ifdef FEAT_VIRTUALEDIT
2294 curwin->w_cursor.coladd = 0;
2295#endif
2296 curwin->w_set_curswant = TRUE;
2297 }
2298 return OK;
2299 }
2300 --RedrawingDisabled;
2301 return FAIL;
2302}
2303
2304/*
2305 * go to the last know line number for the current buffer
2306 */
2307 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002308buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309{
2310 pos_T *fpos;
2311
2312 fpos = buflist_findfpos(curbuf);
2313
2314 curwin->w_cursor.lnum = fpos->lnum;
2315 check_cursor_lnum();
2316
2317 if (p_sol)
2318 curwin->w_cursor.col = 0;
2319 else
2320 {
2321 curwin->w_cursor.col = fpos->col;
2322 check_cursor_col();
2323#ifdef FEAT_VIRTUALEDIT
2324 curwin->w_cursor.coladd = 0;
2325#endif
2326 curwin->w_set_curswant = TRUE;
2327 }
2328}
2329
Bram Moolenaar81695252004-12-29 20:58:21 +00002330#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2331/*
2332 * Find file in buffer list by name (it has to be for the current window).
2333 * Returns NULL if not found.
2334 */
2335 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002336buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002337{
2338 char_u *ffname;
2339 buf_T *buf = NULL;
2340
2341 /* First make the name into a full path name */
2342 ffname = FullName_save(fname,
2343#ifdef UNIX
2344 TRUE /* force expansion, get rid of symbolic links */
2345#else
2346 FALSE
2347#endif
2348 );
2349 if (ffname != NULL)
2350 {
2351 buf = buflist_findname(ffname);
2352 vim_free(ffname);
2353 }
2354 return buf;
2355}
2356#endif
2357
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358/*
2359 * Find file in buffer list by name (it has to be for the current window).
2360 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002361 * Skips dummy buffers.
2362 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 */
2364 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002365buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366{
2367#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002368 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369
2370 if (mch_stat((char *)ffname, &st) < 0)
2371 st.st_dev = (dev_T)-1;
2372 return buflist_findname_stat(ffname, &st);
2373}
2374
2375/*
2376 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2377 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002378 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 */
2380 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002381buflist_findname_stat(
2382 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002383 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384{
2385#endif
2386 buf_T *buf;
2387
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002388 /* Start at the last buffer, expect to find a match sooner. */
2389 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002390 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391#ifdef UNIX
2392 , stp
2393#endif
2394 ))
2395 return buf;
2396 return NULL;
2397}
2398
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002399#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2400 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401/*
2402 * Find file in buffer list by a regexp pattern.
2403 * Return fnum of the found buffer.
2404 * Return < 0 for error.
2405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002407buflist_findpat(
2408 char_u *pattern,
2409 char_u *pattern_end, /* pointer to first char after pattern */
2410 int unlisted, /* find unlisted buffers */
2411 int diffmode UNUSED, /* find diff-mode buffers only */
2412 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 int match = -1;
2416 int find_listed;
2417 char_u *pat;
2418 char_u *patend;
2419 int attempt;
2420 char_u *p;
2421 int toggledollar;
2422
2423 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2424 {
2425 if (*pattern == '%')
2426 match = curbuf->b_fnum;
2427 else
2428 match = curwin->w_alt_fnum;
2429#ifdef FEAT_DIFF
2430 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2431 match = -1;
2432#endif
2433 }
2434
2435 /*
2436 * Try four ways of matching a listed buffer:
2437 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002438 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 * attempt == 2: with '$' at end (only match at end)
2440 * attempt == 3: with '^' at start and '$' at end (only full match)
2441 * Repeat this for finding an unlisted buffer if there was no matching
2442 * listed buffer.
2443 */
2444 else
2445 {
2446 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2447 if (pat == NULL)
2448 return -1;
2449 patend = pat + STRLEN(pat) - 1;
2450 toggledollar = (patend > pat && *patend == '$');
2451
2452 /* First try finding a listed buffer. If not found and "unlisted"
2453 * is TRUE, try finding an unlisted buffer. */
2454 find_listed = TRUE;
2455 for (;;)
2456 {
2457 for (attempt = 0; attempt <= 3; ++attempt)
2458 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002459 regmatch_T regmatch;
2460
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 /* may add '^' and '$' */
2462 if (toggledollar)
2463 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2464 p = pat;
2465 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2466 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002467 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2468 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
2470 vim_free(pat);
2471 return -1;
2472 }
2473
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002474 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 if (buf->b_p_bl == find_listed
2476#ifdef FEAT_DIFF
2477 && (!diffmode || diff_mode_buf(buf))
2478#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002479 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002481 if (curtab_only)
2482 {
2483 /* Ignore the match if the buffer is not open in
2484 * the current tab. */
2485#ifdef FEAT_WINDOWS
2486 win_T *wp;
2487
Bram Moolenaar29323592016-07-24 22:04:11 +02002488 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002489 if (wp->w_buffer == buf)
2490 break;
2491 if (wp == NULL)
2492 continue;
2493#else
2494 if (curwin->w_buffer != buf)
2495 continue;
2496#endif
2497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (match >= 0) /* already found a match */
2499 {
2500 match = -2;
2501 break;
2502 }
2503 match = buf->b_fnum; /* remember first match */
2504 }
2505
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002506 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 if (match >= 0) /* found one match */
2508 break;
2509 }
2510
2511 /* Only search for unlisted buffers if there was no match with
2512 * a listed buffer. */
2513 if (!unlisted || !find_listed || match != -1)
2514 break;
2515 find_listed = FALSE;
2516 }
2517
2518 vim_free(pat);
2519 }
2520
2521 if (match == -2)
2522 EMSG2(_("E93: More than one match for %s"), pattern);
2523 else if (match < 0)
2524 EMSG2(_("E94: No matching buffer for %s"), pattern);
2525 return match;
2526}
2527#endif
2528
2529#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2530
2531/*
2532 * Find all buffer names that match.
2533 * For command line expansion of ":buf" and ":sbuf".
2534 * Return OK if matches found, FAIL otherwise.
2535 */
2536 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002537ExpandBufnames(
2538 char_u *pat,
2539 int *num_file,
2540 char_u ***file,
2541 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
2543 int count = 0;
2544 buf_T *buf;
2545 int round;
2546 char_u *p;
2547 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002548 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549
2550 *num_file = 0; /* return values in case of FAIL */
2551 *file = NULL;
2552
Bram Moolenaar05159a02005-02-26 23:04:13 +00002553 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2554 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002556 patc = alloc((unsigned)STRLEN(pat) + 11);
2557 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002559 STRCPY(patc, "\\(^\\|[\\/]\\)");
2560 STRCPY(patc + 11, pat + 1);
2561 }
2562 else
2563 patc = pat;
2564
2565 /*
2566 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002567 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002568 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002569 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002570 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002571 regmatch_T regmatch;
2572
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002573 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002574 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002575 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2576 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002577 {
2578 if (patc != pat)
2579 vim_free(patc);
2580 return FAIL;
2581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582
2583 /*
2584 * round == 1: Count the matches.
2585 * round == 2: Build the array to keep the matches.
2586 */
2587 for (round = 1; round <= 2; ++round)
2588 {
2589 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002590 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 {
2592 if (!buf->b_p_bl) /* skip unlisted buffers */
2593 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002594 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 if (p != NULL)
2596 {
2597 if (round == 1)
2598 ++count;
2599 else
2600 {
2601 if (options & WILD_HOME_REPLACE)
2602 p = home_replace_save(buf, p);
2603 else
2604 p = vim_strsave(p);
2605 (*file)[count++] = p;
2606 }
2607 }
2608 }
2609 if (count == 0) /* no match found, break here */
2610 break;
2611 if (round == 1)
2612 {
2613 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2614 if (*file == NULL)
2615 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002616 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002617 if (patc != pat)
2618 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 return FAIL;
2620 }
2621 }
2622 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002623 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (count) /* match(es) found, break here */
2625 break;
2626 }
2627
Bram Moolenaar05159a02005-02-26 23:04:13 +00002628 if (patc != pat)
2629 vim_free(patc);
2630
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 *num_file = count;
2632 return (count == 0 ? FAIL : OK);
2633}
2634
2635#endif /* FEAT_CMDL_COMPL */
2636
2637#ifdef HAVE_BUFLIST_MATCH
2638/*
2639 * Check for a match on the file name for buffer "buf" with regprog "prog".
2640 */
2641 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002642buflist_match(
2643 regmatch_T *rmp,
2644 buf_T *buf,
2645 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 char_u *match;
2648
2649 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002650 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002652 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653
2654 return match;
2655}
2656
2657/*
2658 * Try matching the regexp in "prog" with file name "name".
2659 * Return "name" when there is a match, NULL when not.
2660 */
2661 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002662fname_match(
2663 regmatch_T *rmp,
2664 char_u *name,
2665 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 char_u *match = NULL;
2668 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
2670 if (name != NULL)
2671 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002672 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002673 rmp->rm_ic = p_fic || ignore_case;
2674 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 match = name;
2676 else
2677 {
2678 /* Replace $(HOME) with '~' and try matching again. */
2679 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002680 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002681 match = name;
2682 vim_free(p);
2683 }
2684 }
2685
2686 return match;
2687}
2688#endif
2689
2690/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002691 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 */
2693 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002694buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002696 char_u key[VIM_SIZEOF_INT * 2 + 1];
2697 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 if (nr == 0)
2700 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002701 sprintf((char *)key, "%x", nr);
2702 hi = hash_find(&buf_hashtab, key);
2703
2704 if (!HASHITEM_EMPTY(hi))
2705 return (buf_T *)(hi->hi_key
2706 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 return NULL;
2708}
2709
2710/*
2711 * Get name of file 'n' in the buffer list.
2712 * When the file has no name an empty string is returned.
2713 * home_replace() is used to shorten the file name (used for marks).
2714 * Returns a pointer to allocated memory, of NULL when failed.
2715 */
2716 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002717buflist_nr2name(
2718 int n,
2719 int fullname,
2720 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721{
2722 buf_T *buf;
2723
2724 buf = buflist_findnr(n);
2725 if (buf == NULL)
2726 return NULL;
2727 return home_replace_save(helptail ? buf : NULL,
2728 fullname ? buf->b_ffname : buf->b_fname);
2729}
2730
2731/*
2732 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2733 * When "copy_options" is TRUE save the local window option values.
2734 * When "lnum" is 0 only do the options.
2735 */
2736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002737buflist_setfpos(
2738 buf_T *buf,
2739 win_T *win,
2740 linenr_T lnum,
2741 colnr_T col,
2742 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 wininfo_T *wip;
2745
2746 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2747 if (wip->wi_win == win)
2748 break;
2749 if (wip == NULL)
2750 {
2751 /* allocate a new entry */
2752 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2753 if (wip == NULL)
2754 return;
2755 wip->wi_win = win;
2756 if (lnum == 0) /* set lnum even when it's 0 */
2757 lnum = 1;
2758 }
2759 else
2760 {
2761 /* remove the entry from the list */
2762 if (wip->wi_prev)
2763 wip->wi_prev->wi_next = wip->wi_next;
2764 else
2765 buf->b_wininfo = wip->wi_next;
2766 if (wip->wi_next)
2767 wip->wi_next->wi_prev = wip->wi_prev;
2768 if (copy_options && wip->wi_optset)
2769 {
2770 clear_winopt(&wip->wi_opt);
2771#ifdef FEAT_FOLDING
2772 deleteFoldRecurse(&wip->wi_folds);
2773#endif
2774 }
2775 }
2776 if (lnum != 0)
2777 {
2778 wip->wi_fpos.lnum = lnum;
2779 wip->wi_fpos.col = col;
2780 }
2781 if (copy_options)
2782 {
2783 /* Save the window-specific option values. */
2784 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2785#ifdef FEAT_FOLDING
2786 wip->wi_fold_manual = win->w_fold_manual;
2787 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2788#endif
2789 wip->wi_optset = TRUE;
2790 }
2791
2792 /* insert the entry in front of the list */
2793 wip->wi_next = buf->b_wininfo;
2794 buf->b_wininfo = wip;
2795 wip->wi_prev = NULL;
2796 if (wip->wi_next)
2797 wip->wi_next->wi_prev = wip;
2798
2799 return;
2800}
2801
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002802#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002803static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002804
2805/*
2806 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2807 * page. That's because a diff is local to a tab page.
2808 */
2809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002811{
2812 win_T *wp;
2813
2814 if (wip->wi_opt.wo_diff)
2815 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002816 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002817 /* return FALSE when it's a window in the current tab page, thus
2818 * the buffer was in diff mode here */
2819 if (wip->wi_win == wp)
2820 return FALSE;
2821 return TRUE;
2822 }
2823 return FALSE;
2824}
2825#endif
2826
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827/*
2828 * Find info for the current window in buffer "buf".
2829 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002830 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2831 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 * Returns NULL when there isn't any info.
2833 */
2834 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002835find_wininfo(
2836 buf_T *buf,
2837 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838{
2839 wininfo_T *wip;
2840
2841 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002842 if (wip->wi_win == curwin
2843#ifdef FEAT_DIFF
2844 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2845#endif
2846 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002848
2849 /* If no wininfo for curwin, use the first in the list (that doesn't have
2850 * 'diff' set and is in another tab page). */
2851 if (wip == NULL)
2852 {
2853#ifdef FEAT_DIFF
2854 if (skip_diff_buffer)
2855 {
2856 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2857 if (!wininfo_other_tab_diff(wip))
2858 break;
2859 }
2860 else
2861#endif
2862 wip = buf->b_wininfo;
2863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 return wip;
2865}
2866
2867/*
2868 * Reset the local window options to the values last used in this window.
2869 * If the buffer wasn't used in this window before, use the values from
2870 * the most recently used window. If the values were never set, use the
2871 * global values for the window.
2872 */
2873 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002874get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875{
2876 wininfo_T *wip;
2877
2878 clear_winopt(&curwin->w_onebuf_opt);
2879#ifdef FEAT_FOLDING
2880 clearFolding(curwin);
2881#endif
2882
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002883 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 if (wip != NULL && wip->wi_optset)
2885 {
2886 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2887#ifdef FEAT_FOLDING
2888 curwin->w_fold_manual = wip->wi_fold_manual;
2889 curwin->w_foldinvalid = TRUE;
2890 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2891#endif
2892 }
2893 else
2894 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2895
2896#ifdef FEAT_FOLDING
2897 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2898 if (p_fdls >= 0)
2899 curwin->w_p_fdl = p_fdls;
2900#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002901#ifdef FEAT_SYN_HL
2902 check_colorcolumn(curwin);
2903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904}
2905
2906/*
2907 * Find the position (lnum and col) for the buffer 'buf' for the current
2908 * window.
2909 * Returns a pointer to no_position if no position is found.
2910 */
2911 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002912buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913{
2914 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002915 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002917 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 if (wip != NULL)
2919 return &(wip->wi_fpos);
2920 else
2921 return &no_position;
2922}
2923
2924/*
2925 * Find the lnum for the buffer 'buf' for the current window.
2926 */
2927 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002928buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929{
2930 return buflist_findfpos(buf)->lnum;
2931}
2932
2933#if defined(FEAT_LISTCMDS) || defined(PROTO)
2934/*
2935 * List all know file names (for :files and :buffers command).
2936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
2940 buf_T *buf;
2941 int len;
2942 int i;
2943
2944 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2945 {
2946 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002947 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2948 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2949 || (vim_strchr(eap->arg, '+')
2950 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2951 || (vim_strchr(eap->arg, 'a')
2952 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2953 || (vim_strchr(eap->arg, 'h')
2954 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2955 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2956 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2957 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2958 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2959 || (vim_strchr(eap->arg, '#')
2960 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002963 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 else
2965 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002966 if (message_filtered(NameBuff))
2967 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002969 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00002970 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 buf->b_fnum,
2972 buf->b_p_bl ? ' ' : 'u',
2973 buf == curbuf ? '%' :
2974 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2975 buf->b_ml.ml_mfp == NULL ? ' ' :
2976 (buf->b_nwindows == 0 ? 'h' : 'a'),
2977 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2978 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002979 : (bufIsChanged(buf) ? '+' : ' '),
2980 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002981 if (len > IOSIZE - 20)
2982 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983
2984 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 i = 40 - vim_strsize(IObuff);
2986 do
2987 {
2988 IObuff[len++] = ' ';
2989 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002990 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2991 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002992 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 msg_outtrans(IObuff);
2994 out_flush(); /* output one line at a time */
2995 ui_breakcheck();
2996 }
2997}
2998#endif
2999
3000/*
3001 * Get file name and line number for file 'fnum'.
3002 * Used by DoOneCmd() for translating '%' and '#'.
3003 * Used by insert_reg() and cmdline_paste() for '#' register.
3004 * Return FAIL if not found, OK for success.
3005 */
3006 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007buflist_name_nr(
3008 int fnum,
3009 char_u **fname,
3010 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011{
3012 buf_T *buf;
3013
3014 buf = buflist_findnr(fnum);
3015 if (buf == NULL || buf->b_fname == NULL)
3016 return FAIL;
3017
3018 *fname = buf->b_fname;
3019 *lnum = buflist_findlnum(buf);
3020
3021 return OK;
3022}
3023
3024/*
3025 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3026 * The file name with the full path is also remembered, for when :cd is used.
3027 * Returns FAIL for failure (file name already in use by other buffer)
3028 * OK otherwise.
3029 */
3030 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003031setfname(
3032 buf_T *buf,
3033 char_u *ffname,
3034 char_u *sfname,
3035 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036{
Bram Moolenaar81695252004-12-29 20:58:21 +00003037 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003039 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#endif
3041
3042 if (ffname == NULL || *ffname == NUL)
3043 {
3044 /* Removing the name. */
3045 vim_free(buf->b_ffname);
3046 vim_free(buf->b_sfname);
3047 buf->b_ffname = NULL;
3048 buf->b_sfname = NULL;
3049#ifdef UNIX
3050 st.st_dev = (dev_T)-1;
3051#endif
3052 }
3053 else
3054 {
3055 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3056 if (ffname == NULL) /* out of memory */
3057 return FAIL;
3058
3059 /*
3060 * if the file name is already used in another buffer:
3061 * - if the buffer is loaded, fail
3062 * - if the buffer is not loaded, delete it from the list
3063 */
3064#ifdef UNIX
3065 if (mch_stat((char *)ffname, &st) < 0)
3066 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003067#endif
3068 if (!(buf->b_flags & BF_DUMMY))
3069#ifdef UNIX
3070 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003072 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#endif
3074 if (obuf != NULL && obuf != buf)
3075 {
3076 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3077 {
3078 if (message)
3079 EMSG(_("E95: Buffer with this name already exists"));
3080 vim_free(ffname);
3081 return FAIL;
3082 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003083 /* delete from the list */
3084 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003085 }
3086 sfname = vim_strsave(sfname);
3087 if (ffname == NULL || sfname == NULL)
3088 {
3089 vim_free(sfname);
3090 vim_free(ffname);
3091 return FAIL;
3092 }
3093#ifdef USE_FNAME_CASE
3094# ifdef USE_LONG_FNAME
3095 if (USE_LONG_FNAME)
3096# endif
3097 fname_case(sfname, 0); /* set correct case for short file name */
3098#endif
3099 vim_free(buf->b_ffname);
3100 vim_free(buf->b_sfname);
3101 buf->b_ffname = ffname;
3102 buf->b_sfname = sfname;
3103 }
3104 buf->b_fname = buf->b_sfname;
3105#ifdef UNIX
3106 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003107 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 else
3109 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003110 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 buf->b_dev = st.st_dev;
3112 buf->b_ino = st.st_ino;
3113 }
3114#endif
3115
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117
3118 buf_name_changed(buf);
3119 return OK;
3120}
3121
3122/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003123 * Crude way of changing the name of a buffer. Use with care!
3124 * The name should be relative to the current directory.
3125 */
3126 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003127buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003128{
3129 buf_T *buf;
3130
3131 buf = buflist_findnr(fnum);
3132 if (buf != NULL)
3133 {
3134 vim_free(buf->b_sfname);
3135 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003136 buf->b_ffname = vim_strsave(name);
3137 buf->b_sfname = NULL;
3138 /* Allocate ffname and expand into full path. Also resolves .lnk
3139 * files on Win32. */
3140 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003141 buf->b_fname = buf->b_sfname;
3142 }
3143}
3144
3145/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 * Take care of what needs to be done when the name of buffer "buf" has
3147 * changed.
3148 */
3149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003150buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151{
3152 /*
3153 * If the file name changed, also change the name of the swapfile
3154 */
3155 if (buf->b_ml.ml_mfp != NULL)
3156 ml_setname(buf);
3157
3158 if (curwin->w_buffer == buf)
3159 check_arg_idx(curwin); /* check file name for arg list */
3160#ifdef FEAT_TITLE
3161 maketitle(); /* set window title */
3162#endif
3163#ifdef FEAT_WINDOWS
3164 status_redraw_all(); /* status lines need to be redrawn */
3165#endif
3166 fmarks_check_names(buf); /* check named file marks */
3167 ml_timestamp(buf); /* reset timestamp */
3168}
3169
3170/*
3171 * set alternate file name for current window
3172 *
3173 * Used by do_one_cmd(), do_write() and do_ecmd().
3174 * Return the buffer.
3175 */
3176 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003177setaltfname(
3178 char_u *ffname,
3179 char_u *sfname,
3180 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181{
3182 buf_T *buf;
3183
3184 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3185 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003186 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 curwin->w_alt_fnum = buf->b_fnum;
3188 return buf;
3189}
3190
3191/*
3192 * Get alternate file name for current window.
3193 * Return NULL if there isn't any, and give error message if requested.
3194 */
3195 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003196getaltfname(
3197 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198{
3199 char_u *fname;
3200 linenr_T dummy;
3201
3202 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3203 {
3204 if (errmsg)
3205 EMSG(_(e_noalt));
3206 return NULL;
3207 }
3208 return fname;
3209}
3210
3211/*
3212 * Add a file name to the buflist and return its number.
3213 * Uses same flags as buflist_new(), except BLN_DUMMY.
3214 *
3215 * used by qf_init(), main() and doarglist()
3216 */
3217 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219{
3220 buf_T *buf;
3221
3222 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3223 if (buf != NULL)
3224 return buf->b_fnum;
3225 return 0;
3226}
3227
3228#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3229/*
3230 * Adjust slashes in file names. Called after 'shellslash' was set.
3231 */
3232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003233buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234{
3235 buf_T *bp;
3236
Bram Moolenaar29323592016-07-24 22:04:11 +02003237 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 {
3239 if (bp->b_ffname != NULL)
3240 slash_adjust(bp->b_ffname);
3241 if (bp->b_sfname != NULL)
3242 slash_adjust(bp->b_sfname);
3243 }
3244}
3245#endif
3246
3247/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003248 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 * Also save the local window option values.
3250 */
3251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003252buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003254 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255}
3256
3257/*
3258 * Return TRUE if 'ffname' is not the same file as current file.
3259 * Fname must have a full path (expanded by mch_FullName()).
3260 */
3261 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003262otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 return otherfile_buf(curbuf, ffname
3265#ifdef UNIX
3266 , NULL
3267#endif
3268 );
3269}
3270
3271 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003272otherfile_buf(
3273 buf_T *buf,
3274 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003276 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003278 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279{
3280 /* no name is different */
3281 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3282 return TRUE;
3283 if (fnamecmp(ffname, buf->b_ffname) == 0)
3284 return FALSE;
3285#ifdef UNIX
3286 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003287 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288
Bram Moolenaar8767f522016-07-01 17:17:39 +02003289 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 if (stp == NULL)
3291 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003292 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 st.st_dev = (dev_T)-1;
3294 stp = &st;
3295 }
3296 /* Use dev/ino to check if the files are the same, even when the names
3297 * are different (possible with links). Still need to compare the
3298 * name above, for when the file doesn't exist yet.
3299 * Problem: The dev/ino changes when a file is deleted (and created
3300 * again) and remains the same when renamed/moved. We don't want to
3301 * mch_stat() each buffer each time, that would be too slow. Get the
3302 * dev/ino again when they appear to match, but not when they appear
3303 * to be different: Could skip a buffer when it's actually the same
3304 * file. */
3305 if (buf_same_ino(buf, stp))
3306 {
3307 buf_setino(buf);
3308 if (buf_same_ino(buf, stp))
3309 return FALSE;
3310 }
3311 }
3312#endif
3313 return TRUE;
3314}
3315
3316#if defined(UNIX) || defined(PROTO)
3317/*
3318 * Set inode and device number for a buffer.
3319 * Must always be called when b_fname is changed!.
3320 */
3321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003322buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003324 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
3326 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3327 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003328 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 buf->b_dev = st.st_dev;
3330 buf->b_ino = st.st_ino;
3331 }
3332 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003333 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334}
3335
3336/*
3337 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3338 */
3339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003340buf_same_ino(
3341 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003342 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003344 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 && stp->st_dev == buf->b_dev
3346 && stp->st_ino == buf->b_ino);
3347}
3348#endif
3349
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003350/*
3351 * Print info about the current buffer.
3352 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003354fileinfo(
3355 int fullname, /* when non-zero print full path */
3356 int shorthelp,
3357 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 char_u *name;
3360 int n;
3361 char_u *p;
3362 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003363 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364
3365 buffer = alloc(IOSIZE);
3366 if (buffer == NULL)
3367 return;
3368
3369 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3370 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003371 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 p = buffer + STRLEN(buffer);
3373 }
3374 else
3375 p = buffer;
3376
3377 *p++ = '"';
3378 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003379 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003380 else
3381 {
3382 if (!fullname && curbuf->b_fname != NULL)
3383 name = curbuf->b_fname;
3384 else
3385 name = curbuf->b_ffname;
3386 home_replace(shorthelp ? curbuf : NULL, name, p,
3387 (int)(IOSIZE - (p - buffer)), TRUE);
3388 }
3389
Bram Moolenaara800b422010-06-27 01:15:55 +02003390 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 curbufIsChanged() ? (shortmess(SHM_MOD)
3392 ? " [+]" : _(" [Modified]")) : " ",
3393 (curbuf->b_flags & BF_NOTEDITED)
3394#ifdef FEAT_QUICKFIX
3395 && !bt_dontwrite(curbuf)
3396#endif
3397 ? _("[Not edited]") : "",
3398 (curbuf->b_flags & BF_NEW)
3399#ifdef FEAT_QUICKFIX
3400 && !bt_dontwrite(curbuf)
3401#endif
3402 ? _("[New file]") : "",
3403 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003404 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 : _("[readonly]")) : "",
3406 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3407 || curbuf->b_p_ro) ?
3408 " " : "");
3409 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3410 * causes an overflow, thus for large numbers divide instead. */
3411 if (curwin->w_cursor.lnum > 1000000L)
3412 n = (int)(((long)curwin->w_cursor.lnum) /
3413 ((long)curbuf->b_ml.ml_line_count / 100L));
3414 else
3415 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3416 (long)curbuf->b_ml.ml_line_count);
3417 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3418 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003419 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 }
3421#ifdef FEAT_CMDL_INFO
3422 else if (p_ru)
3423 {
3424 /* Current line and column are already on the screen -- webb */
3425 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003426 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003428 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 (long)curbuf->b_ml.ml_line_count, n);
3430 }
3431#endif
3432 else
3433 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003434 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 _("line %ld of %ld --%d%%-- col "),
3436 (long)curwin->w_cursor.lnum,
3437 (long)curbuf->b_ml.ml_line_count,
3438 n);
3439 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003440 len = STRLEN(buffer);
3441 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3443 }
3444
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003445 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446
3447 if (dont_truncate)
3448 {
3449 /* Temporarily set msg_scroll to avoid the message being truncated.
3450 * First call msg_start() to get the message in the right place. */
3451 msg_start();
3452 n = msg_scroll;
3453 msg_scroll = TRUE;
3454 msg(buffer);
3455 msg_scroll = n;
3456 }
3457 else
3458 {
3459 p = msg_trunc_attr(buffer, FALSE, 0);
3460 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 /* Need to repeat the message after redrawing when:
3462 * - When restart_edit is set (otherwise there will be a delay
3463 * before redrawing).
3464 * - When the screen was scrolled but there is no wait-return
3465 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003466 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 }
3468
3469 vim_free(buffer);
3470}
3471
3472 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003473col_print(
3474 char_u *buf,
3475 size_t buflen,
3476 int col,
3477 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478{
3479 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003480 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003482 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483}
3484
3485#if defined(FEAT_TITLE) || defined(PROTO)
3486/*
3487 * put file name in title bar of window and in icon title
3488 */
3489
3490static char_u *lasttitle = NULL;
3491static char_u *lasticon = NULL;
3492
3493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003494maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495{
3496 char_u *p;
3497 char_u *t_str = NULL;
3498 char_u *i_name;
3499 char_u *i_str = NULL;
3500 int maxlen = 0;
3501 int len;
3502 int mustset;
3503 char_u buf[IOSIZE];
3504 int off;
3505
3506 if (!redrawing())
3507 {
3508 /* Postpone updating the title when 'lazyredraw' is set. */
3509 need_maketitle = TRUE;
3510 return;
3511 }
3512
3513 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003514 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 return;
3516
3517 if (p_title)
3518 {
3519 if (p_titlelen > 0)
3520 {
3521 maxlen = p_titlelen * Columns / 100;
3522 if (maxlen < 10)
3523 maxlen = 10;
3524 }
3525
3526 t_str = buf;
3527 if (*p_titlestring != NUL)
3528 {
3529#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003530 if (stl_syntax & STL_IN_TITLE)
3531 {
3532 int use_sandbox = FALSE;
3533 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003534
3535# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003536 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003537# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003538 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003540 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003541 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003542 if (called_emsg)
3543 set_string_option_direct((char_u *)"titlestring", -1,
3544 (char_u *)"", OPT_FREE, SID_ERROR);
3545 called_emsg |= save_called_emsg;
3546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 else
3548#endif
3549 t_str = p_titlestring;
3550 }
3551 else
3552 {
3553 /* format: "fname + (path) (1 of 2) - VIM" */
3554
Bram Moolenaar2c666692012-09-05 13:30:40 +02003555#define SPACE_FOR_FNAME (IOSIZE - 100)
3556#define SPACE_FOR_DIR (IOSIZE - 20)
3557#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003559 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 else
3561 {
3562 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003563 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566
3567 switch (bufIsChanged(curbuf)
3568 + (curbuf->b_p_ro * 2)
3569 + (!curbuf->b_p_ma * 4))
3570 {
3571 case 1: STRCAT(buf, " +"); break;
3572 case 2: STRCAT(buf, " ="); break;
3573 case 3: STRCAT(buf, " =+"); break;
3574 case 4:
3575 case 6: STRCAT(buf, " -"); break;
3576 case 5:
3577 case 7: STRCAT(buf, " -+"); break;
3578 }
3579
3580 if (curbuf->b_fname != NULL)
3581 {
3582 /* Get path of file, replace home dir with ~ */
3583 off = (int)STRLEN(buf);
3584 buf[off++] = ' ';
3585 buf[off++] = '(';
3586 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003587 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588#ifdef BACKSLASH_IN_FILENAME
3589 /* avoid "c:/name" to be reduced to "c" */
3590 if (isalpha(buf[off]) && buf[off + 1] == ':')
3591 off += 2;
3592#endif
3593 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003594 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003597 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003598 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003601
Bram Moolenaar2c666692012-09-05 13:30:40 +02003602 /* Translate unprintable chars and concatenate. Keep some
3603 * room for the server name. When there is no room (very long
3604 * file name) use (...). */
3605 if (off < SPACE_FOR_DIR)
3606 {
3607 p = transstr(buf + off);
3608 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3609 vim_free(p);
3610 }
3611 else
3612 {
3613 vim_strncpy(buf + off, (char_u *)"...",
3614 (size_t)(SPACE_FOR_ARGNR - off));
3615 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 STRCAT(buf, ")");
3617 }
3618
Bram Moolenaar2c666692012-09-05 13:30:40 +02003619 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
3621#if defined(FEAT_CLIENTSERVER)
3622 if (serverName != NULL)
3623 {
3624 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003625 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 }
3627 else
3628#endif
3629 STRCAT(buf, " - VIM");
3630
3631 if (maxlen > 0)
3632 {
3633 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003634 if (vim_strsize(buf) > maxlen)
3635 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 }
3638 }
3639 mustset = ti_change(t_str, &lasttitle);
3640
3641 if (p_icon)
3642 {
3643 i_str = buf;
3644 if (*p_iconstring != NUL)
3645 {
3646#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003647 if (stl_syntax & STL_IN_ICON)
3648 {
3649 int use_sandbox = FALSE;
3650 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003651
3652# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003653 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003654# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003655 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003657 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003658 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003659 if (called_emsg)
3660 set_string_option_direct((char_u *)"iconstring", -1,
3661 (char_u *)"", OPT_FREE, SID_ERROR);
3662 called_emsg |= save_called_emsg;
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 else
3665#endif
3666 i_str = p_iconstring;
3667 }
3668 else
3669 {
3670 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003671 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 else /* use file name only in icon */
3673 i_name = gettail(curbuf->b_ffname);
3674 *i_str = NUL;
3675 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003676 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 if (len > 100)
3678 {
3679 len -= 100;
3680#ifdef FEAT_MBYTE
3681 if (has_mbyte)
3682 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3683#endif
3684 i_name += len;
3685 }
3686 STRCPY(i_str, i_name);
3687 trans_characters(i_str, IOSIZE);
3688 }
3689 }
3690
3691 mustset |= ti_change(i_str, &lasticon);
3692
3693 if (mustset)
3694 resettitle();
3695}
3696
3697/*
3698 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3699 * from "str" if it does.
3700 * Return TRUE when "*last" changed.
3701 */
3702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003703ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704{
3705 if ((str == NULL) != (*last == NULL)
3706 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3707 {
3708 vim_free(*last);
3709 if (str == NULL)
3710 *last = NULL;
3711 else
3712 *last = vim_strsave(str);
3713 return TRUE;
3714 }
3715 return FALSE;
3716}
3717
3718/*
3719 * Put current window title back (used after calling a shell)
3720 */
3721 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003722resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723{
3724 mch_settitle(lasttitle, lasticon);
3725}
Bram Moolenaarea408852005-06-25 22:49:46 +00003726
3727# if defined(EXITFREE) || defined(PROTO)
3728 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003729free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003730{
3731 vim_free(lasttitle);
3732 vim_free(lasticon);
3733}
3734# endif
3735
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736#endif /* FEAT_TITLE */
3737
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003738#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003740 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 * Return length of string in screen cells.
3742 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003743 * Normally works for window "wp", except when working for 'tabline' then it
3744 * is "curwin".
3745 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 * Items are drawn interspersed with the text that surrounds it
3747 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3748 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3749 *
3750 * If maxwidth is not zero, the string will be filled at any middle marker
3751 * or truncated if too long, fillchar is used for all whitespace.
3752 */
3753 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003754build_stl_str_hl(
3755 win_T *wp,
3756 char_u *out, /* buffer to write into != NameBuff */
3757 size_t outlen, /* length of out[] */
3758 char_u *fmt,
3759 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3760 int fillchar,
3761 int maxwidth,
3762 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3763 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764{
3765 char_u *p;
3766 char_u *s;
3767 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003768 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769#ifdef FEAT_EVAL
3770 win_T *o_curwin;
3771 buf_T *o_curbuf;
3772#endif
3773 int empty_line;
3774 colnr_T virtcol;
3775 long l;
3776 long n;
3777 int prevchar_isflag;
3778 int prevchar_isitem;
3779 int itemisflag;
3780 int fillable;
3781 char_u *str;
3782 long num;
3783 int width;
3784 int itemcnt;
3785 int curitem;
3786 int groupitem[STL_MAX_ITEM];
3787 int groupdepth;
3788 struct stl_item
3789 {
3790 char_u *start;
3791 int minwid;
3792 int maxwid;
3793 enum
3794 {
3795 Normal,
3796 Empty,
3797 Group,
3798 Middle,
3799 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003800 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 Trunc
3802 } type;
3803 } item[STL_MAX_ITEM];
3804 int minwid;
3805 int maxwid;
3806 int zeropad;
3807 char_u base;
3808 char_u opt;
3809#define TMPLEN 70
3810 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003811 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003812 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003813
3814#ifdef FEAT_EVAL
3815 /*
3816 * When the format starts with "%!" then evaluate it as an expression and
3817 * use the result as the actual format string.
3818 */
3819 if (fmt[0] == '%' && fmt[1] == '!')
3820 {
3821 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3822 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003823 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003824 }
3825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826
3827 if (fillchar == 0)
3828 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003829#ifdef FEAT_MBYTE
3830 /* Can't handle a multi-byte fill character yet. */
3831 else if (mb_char2len(fillchar) > 1)
3832 fillchar = '-';
3833#endif
3834
Bram Moolenaar567199b2013-04-24 16:52:36 +02003835 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3836 * p will become invalid when getting another buffer line. */
3837 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3838 empty_line = (*p == NUL);
3839
3840 /* Get the byte value now, in case we need it below. This is more
3841 * efficient than making a copy of the line. */
3842 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3843 byteval = 0;
3844 else
3845#ifdef FEAT_MBYTE
3846 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3847#else
3848 byteval = p[wp->w_cursor.col];
3849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850
3851 groupdepth = 0;
3852 p = out;
3853 curitem = 0;
3854 prevchar_isflag = TRUE;
3855 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003856 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003858 if (curitem == STL_MAX_ITEM)
3859 {
3860 /* There are too many items. Add the error code to the statusline
3861 * to give the user a hint about what went wrong. */
3862 if (p + 6 < out + outlen)
3863 {
3864 mch_memmove(p, " E541", (size_t)5);
3865 p += 5;
3866 }
3867 break;
3868 }
3869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 if (*s != NUL && *s != '%')
3871 prevchar_isflag = prevchar_isitem = FALSE;
3872
3873 /*
3874 * Handle up to the next '%' or the end.
3875 */
3876 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3877 *p++ = *s++;
3878 if (*s == NUL || p + 1 >= out + outlen)
3879 break;
3880
3881 /*
3882 * Handle one '%' item.
3883 */
3884 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003885 if (*s == NUL) /* ignore trailing % */
3886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 if (*s == '%')
3888 {
3889 if (p + 1 >= out + outlen)
3890 break;
3891 *p++ = *s++;
3892 prevchar_isflag = prevchar_isitem = FALSE;
3893 continue;
3894 }
3895 if (*s == STL_MIDDLEMARK)
3896 {
3897 s++;
3898 if (groupdepth > 0)
3899 continue;
3900 item[curitem].type = Middle;
3901 item[curitem++].start = p;
3902 continue;
3903 }
3904 if (*s == STL_TRUNCMARK)
3905 {
3906 s++;
3907 item[curitem].type = Trunc;
3908 item[curitem++].start = p;
3909 continue;
3910 }
3911 if (*s == ')')
3912 {
3913 s++;
3914 if (groupdepth < 1)
3915 continue;
3916 groupdepth--;
3917
3918 t = item[groupitem[groupdepth]].start;
3919 *p = NUL;
3920 l = vim_strsize(t);
3921 if (curitem > groupitem[groupdepth] + 1
3922 && item[groupitem[groupdepth]].minwid == 0)
3923 {
3924 /* remove group if all items are empty */
3925 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003926 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 break;
3928 if (n == curitem)
3929 {
3930 p = t;
3931 l = 0;
3932 }
3933 }
3934 if (l > item[groupitem[groupdepth]].maxwid)
3935 {
3936 /* truncate, remove n bytes of text at the start */
3937#ifdef FEAT_MBYTE
3938 if (has_mbyte)
3939 {
3940 /* Find the first character that should be included. */
3941 n = 0;
3942 while (l >= item[groupitem[groupdepth]].maxwid)
3943 {
3944 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003945 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 }
3947 }
3948 else
3949#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003950 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
3952 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003953 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 p = p - n + 1;
3955#ifdef FEAT_MBYTE
3956 /* Fill up space left over by half a double-wide char. */
3957 while (++l < item[groupitem[groupdepth]].minwid)
3958 *p++ = fillchar;
3959#endif
3960
3961 /* correct the start of the items for the truncation */
3962 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3963 {
3964 item[l].start -= n;
3965 if (item[l].start < t)
3966 item[l].start = t;
3967 }
3968 }
3969 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3970 {
3971 /* fill */
3972 n = item[groupitem[groupdepth]].minwid;
3973 if (n < 0)
3974 {
3975 /* fill by appending characters */
3976 n = 0 - n;
3977 while (l++ < n && p + 1 < out + outlen)
3978 *p++ = fillchar;
3979 }
3980 else
3981 {
3982 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003983 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 l = n - l;
3985 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003986 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987 p += l;
3988 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3989 item[n].start += l;
3990 for ( ; l > 0; l--)
3991 *t++ = fillchar;
3992 }
3993 }
3994 continue;
3995 }
3996 minwid = 0;
3997 maxwid = 9999;
3998 zeropad = FALSE;
3999 l = 1;
4000 if (*s == '0')
4001 {
4002 s++;
4003 zeropad = TRUE;
4004 }
4005 if (*s == '-')
4006 {
4007 s++;
4008 l = -1;
4009 }
4010 if (VIM_ISDIGIT(*s))
4011 {
4012 minwid = (int)getdigits(&s);
4013 if (minwid < 0) /* overflow */
4014 minwid = 0;
4015 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004016 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 {
4018 item[curitem].type = Highlight;
4019 item[curitem].start = p;
4020 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4021 s++;
4022 curitem++;
4023 continue;
4024 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004025 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4026 {
4027 if (*s == STL_TABCLOSENR)
4028 {
4029 if (minwid == 0)
4030 {
4031 /* %X ends the close label, go back to the previously
4032 * define tab label nr. */
4033 for (n = curitem - 1; n >= 0; --n)
4034 if (item[n].type == TabPage && item[n].minwid >= 0)
4035 {
4036 minwid = item[n].minwid;
4037 break;
4038 }
4039 }
4040 else
4041 /* close nrs are stored as negative values */
4042 minwid = - minwid;
4043 }
4044 item[curitem].type = TabPage;
4045 item[curitem].start = p;
4046 item[curitem].minwid = minwid;
4047 s++;
4048 curitem++;
4049 continue;
4050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 if (*s == '.')
4052 {
4053 s++;
4054 if (VIM_ISDIGIT(*s))
4055 {
4056 maxwid = (int)getdigits(&s);
4057 if (maxwid <= 0) /* overflow */
4058 maxwid = 50;
4059 }
4060 }
4061 minwid = (minwid > 50 ? 50 : minwid) * l;
4062 if (*s == '(')
4063 {
4064 groupitem[groupdepth++] = curitem;
4065 item[curitem].type = Group;
4066 item[curitem].start = p;
4067 item[curitem].minwid = minwid;
4068 item[curitem].maxwid = maxwid;
4069 s++;
4070 curitem++;
4071 continue;
4072 }
4073 if (vim_strchr(STL_ALL, *s) == NULL)
4074 {
4075 s++;
4076 continue;
4077 }
4078 opt = *s++;
4079
4080 /* OK - now for the real work */
4081 base = 'D';
4082 itemisflag = FALSE;
4083 fillable = TRUE;
4084 num = -1;
4085 str = NULL;
4086 switch (opt)
4087 {
4088 case STL_FILEPATH:
4089 case STL_FULLPATH:
4090 case STL_FILENAME:
4091 fillable = FALSE; /* don't change ' ' to fillchar */
4092 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004093 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 else
4095 {
4096 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004097 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4099 }
4100 trans_characters(NameBuff, MAXPATHL);
4101 if (opt != STL_FILENAME)
4102 str = NameBuff;
4103 else
4104 str = gettail(NameBuff);
4105 break;
4106
4107 case STL_VIM_EXPR: /* '{' */
4108 itemisflag = TRUE;
4109 t = p;
4110 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4111 *p++ = *s++;
4112 if (*s != '}') /* missing '}' or out of space */
4113 break;
4114 s++;
4115 *p = 0;
4116 p = t;
4117
4118#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004119 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4121
4122 o_curbuf = curbuf;
4123 o_curwin = curwin;
4124 curwin = wp;
4125 curbuf = wp->w_buffer;
4126
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004127 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
4129 curwin = o_curwin;
4130 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004131 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132
4133 if (str != NULL && *str != 0)
4134 {
4135 if (*skipdigits(str) == NUL)
4136 {
4137 num = atoi((char *)str);
4138 vim_free(str);
4139 str = NULL;
4140 itemisflag = FALSE;
4141 }
4142 }
4143#endif
4144 break;
4145
4146 case STL_LINE:
4147 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4148 ? 0L : (long)(wp->w_cursor.lnum);
4149 break;
4150
4151 case STL_NUMLINES:
4152 num = wp->w_buffer->b_ml.ml_line_count;
4153 break;
4154
4155 case STL_COLUMN:
4156 num = !(State & INSERT) && empty_line
4157 ? 0 : (int)wp->w_cursor.col + 1;
4158 break;
4159
4160 case STL_VIRTCOL:
4161 case STL_VIRTCOL_ALT:
4162 /* In list mode virtcol needs to be recomputed */
4163 virtcol = wp->w_virtcol;
4164 if (wp->w_p_list && lcs_tab1 == NUL)
4165 {
4166 wp->w_p_list = FALSE;
4167 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4168 wp->w_p_list = TRUE;
4169 }
4170 ++virtcol;
4171 /* Don't display %V if it's the same as %c. */
4172 if (opt == STL_VIRTCOL_ALT
4173 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4174 ? 0 : (int)wp->w_cursor.col + 1)))
4175 break;
4176 num = (long)virtcol;
4177 break;
4178
4179 case STL_PERCENTAGE:
4180 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4181 (long)wp->w_buffer->b_ml.ml_line_count);
4182 break;
4183
4184 case STL_ALTPERCENT:
4185 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004186 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 break;
4188
4189 case STL_ARGLISTSTAT:
4190 fillable = FALSE;
4191 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004192 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 str = tmp;
4194 break;
4195
4196 case STL_KEYMAP:
4197 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004198 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 str = tmp;
4200 break;
4201 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004202#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004203 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204#else
4205 num = 0;
4206#endif
4207 break;
4208
4209 case STL_BUFNO:
4210 num = wp->w_buffer->b_fnum;
4211 break;
4212
4213 case STL_OFFSET_X:
4214 base = 'X';
4215 case STL_OFFSET:
4216#ifdef FEAT_BYTEOFF
4217 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4218 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4219 0L : l + 1 + (!(State & INSERT) && empty_line ?
4220 0 : (int)wp->w_cursor.col);
4221#endif
4222 break;
4223
4224 case STL_BYTEVAL_X:
4225 base = 'X';
4226 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004227 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 if (num == NL)
4229 num = 0;
4230 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4231 num = NL;
4232 break;
4233
4234 case STL_ROFLAG:
4235 case STL_ROFLAG_ALT:
4236 itemisflag = TRUE;
4237 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004238 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 break;
4240
4241 case STL_HELPFLAG:
4242 case STL_HELPFLAG_ALT:
4243 itemisflag = TRUE;
4244 if (wp->w_buffer->b_help)
4245 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004246 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 break;
4248
4249#ifdef FEAT_AUTOCMD
4250 case STL_FILETYPE:
4251 if (*wp->w_buffer->b_p_ft != NUL
4252 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4253 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004254 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4255 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 str = tmp;
4257 }
4258 break;
4259
4260 case STL_FILETYPE_ALT:
4261 itemisflag = TRUE;
4262 if (*wp->w_buffer->b_p_ft != NUL
4263 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4264 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004265 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4266 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 for (t = tmp; *t != 0; t++)
4268 *t = TOUPPER_LOC(*t);
4269 str = tmp;
4270 }
4271 break;
4272#endif
4273
4274#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4275 case STL_PREVIEWFLAG:
4276 case STL_PREVIEWFLAG_ALT:
4277 itemisflag = TRUE;
4278 if (wp->w_p_pvw)
4279 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4280 : _("[Preview]"));
4281 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004282
4283 case STL_QUICKFIX:
4284 if (bt_quickfix(wp->w_buffer))
4285 str = (char_u *)(wp->w_llist_ref
4286 ? _(msg_loclist)
4287 : _(msg_qflist));
4288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289#endif
4290
4291 case STL_MODIFIED:
4292 case STL_MODIFIED_ALT:
4293 itemisflag = TRUE;
4294 switch ((opt == STL_MODIFIED_ALT)
4295 + bufIsChanged(wp->w_buffer) * 2
4296 + (!wp->w_buffer->b_p_ma) * 4)
4297 {
4298 case 2: str = (char_u *)"[+]"; break;
4299 case 3: str = (char_u *)",+"; break;
4300 case 4: str = (char_u *)"[-]"; break;
4301 case 5: str = (char_u *)",-"; break;
4302 case 6: str = (char_u *)"[+-]"; break;
4303 case 7: str = (char_u *)",+-"; break;
4304 }
4305 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004306
4307 case STL_HIGHLIGHT:
4308 t = s;
4309 while (*s != '#' && *s != NUL)
4310 ++s;
4311 if (*s == '#')
4312 {
4313 item[curitem].type = Highlight;
4314 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004315 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004316 curitem++;
4317 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004318 if (*s != NUL)
4319 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004320 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 }
4322
4323 item[curitem].start = p;
4324 item[curitem].type = Normal;
4325 if (str != NULL && *str)
4326 {
4327 t = str;
4328 if (itemisflag)
4329 {
4330 if ((t[0] && t[1])
4331 && ((!prevchar_isitem && *t == ',')
4332 || (prevchar_isflag && *t == ' ')))
4333 t++;
4334 prevchar_isflag = TRUE;
4335 }
4336 l = vim_strsize(t);
4337 if (l > 0)
4338 prevchar_isitem = TRUE;
4339 if (l > maxwid)
4340 {
4341 while (l >= maxwid)
4342#ifdef FEAT_MBYTE
4343 if (has_mbyte)
4344 {
4345 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004346 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 }
4348 else
4349#endif
4350 l -= byte2cells(*t++);
4351 if (p + 1 >= out + outlen)
4352 break;
4353 *p++ = '<';
4354 }
4355 if (minwid > 0)
4356 {
4357 for (; l < minwid && p + 1 < out + outlen; l++)
4358 {
4359 /* Don't put a "-" in front of a digit. */
4360 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4361 *p++ = ' ';
4362 else
4363 *p++ = fillchar;
4364 }
4365 minwid = 0;
4366 }
4367 else
4368 minwid *= -1;
4369 while (*t && p + 1 < out + outlen)
4370 {
4371 *p++ = *t++;
4372 /* Change a space by fillchar, unless fillchar is '-' and a
4373 * digit follows. */
4374 if (fillable && p[-1] == ' '
4375 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4376 p[-1] = fillchar;
4377 }
4378 for (; l < minwid && p + 1 < out + outlen; l++)
4379 *p++ = fillchar;
4380 }
4381 else if (num >= 0)
4382 {
4383 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4384 char_u nstr[20];
4385
4386 if (p + 20 >= out + outlen)
4387 break; /* not sufficient space */
4388 prevchar_isitem = TRUE;
4389 t = nstr;
4390 if (opt == STL_VIRTCOL_ALT)
4391 {
4392 *t++ = '-';
4393 minwid--;
4394 }
4395 *t++ = '%';
4396 if (zeropad)
4397 *t++ = '0';
4398 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004399 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 *t = 0;
4401
4402 for (n = num, l = 1; n >= nbase; n /= nbase)
4403 l++;
4404 if (opt == STL_VIRTCOL_ALT)
4405 l++;
4406 if (l > maxwid)
4407 {
4408 l += 2;
4409 n = l - maxwid;
4410 while (l-- > maxwid)
4411 num /= nbase;
4412 *t++ = '>';
4413 *t++ = '%';
4414 *t = t[-3];
4415 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004416 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4417 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 }
4419 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004420 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4421 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 p += STRLEN(p);
4423 }
4424 else
4425 item[curitem].type = Empty;
4426
4427 if (opt == STL_VIM_EXPR)
4428 vim_free(str);
4429
4430 if (num >= 0 || (!itemisflag && str && *str))
4431 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4432 curitem++;
4433 }
4434 *p = NUL;
4435 itemcnt = curitem;
4436
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004437#ifdef FEAT_EVAL
4438 if (usefmt != fmt)
4439 vim_free(usefmt);
4440#endif
4441
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 width = vim_strsize(out);
4443 if (maxwidth > 0 && width > maxwidth)
4444 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004445 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 l = 0;
4447 if (itemcnt == 0)
4448 s = out;
4449 else
4450 {
4451 for ( ; l < itemcnt; l++)
4452 if (item[l].type == Trunc)
4453 {
4454 /* Truncate at %< item. */
4455 s = item[l].start;
4456 break;
4457 }
4458 if (l == itemcnt)
4459 {
4460 /* No %< item, truncate first item. */
4461 s = item[0].start;
4462 l = 0;
4463 }
4464 }
4465
4466 if (width - vim_strsize(s) >= maxwidth)
4467 {
4468 /* Truncation mark is beyond max length */
4469#ifdef FEAT_MBYTE
4470 if (has_mbyte)
4471 {
4472 s = out;
4473 width = 0;
4474 for (;;)
4475 {
4476 width += ptr2cells(s);
4477 if (width >= maxwidth)
4478 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004479 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 }
4481 /* Fill up for half a double-wide character. */
4482 while (++width < maxwidth)
4483 *s++ = fillchar;
4484 }
4485 else
4486#endif
4487 s = out + maxwidth - 1;
4488 for (l = 0; l < itemcnt; l++)
4489 if (item[l].start > s)
4490 break;
4491 itemcnt = l;
4492 *s++ = '>';
4493 *s = 0;
4494 }
4495 else
4496 {
4497#ifdef FEAT_MBYTE
4498 if (has_mbyte)
4499 {
4500 n = 0;
4501 while (width >= maxwidth)
4502 {
4503 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004504 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506 }
4507 else
4508#endif
4509 n = width - maxwidth + 1;
4510 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004511 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 *s = '<';
4513
4514 /* Fill up for half a double-wide character. */
4515 while (++width < maxwidth)
4516 {
4517 s = s + STRLEN(s);
4518 *s++ = fillchar;
4519 *s = NUL;
4520 }
4521
4522 --n; /* count the '<' */
4523 for (; l < itemcnt; l++)
4524 {
4525 if (item[l].start - n >= s)
4526 item[l].start -= n;
4527 else
4528 item[l].start = s;
4529 }
4530 }
4531 width = maxwidth;
4532 }
4533 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4534 {
4535 /* Apply STL_MIDDLE if any */
4536 for (l = 0; l < itemcnt; l++)
4537 if (item[l].type == Middle)
4538 break;
4539 if (l < itemcnt)
4540 {
4541 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004542 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 for (s = item[l].start; s < p; s++)
4544 *s = fillchar;
4545 for (l++; l < itemcnt; l++)
4546 item[l].start += maxwidth - width;
4547 width = maxwidth;
4548 }
4549 }
4550
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004551 /* Store the info about highlighting. */
4552 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004554 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 for (l = 0; l < itemcnt; l++)
4556 {
4557 if (item[l].type == Highlight)
4558 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004559 sp->start = item[l].start;
4560 sp->userhl = item[l].minwid;
4561 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 }
4563 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004564 sp->start = NULL;
4565 sp->userhl = 0;
4566 }
4567
4568 /* Store the info about tab pages labels. */
4569 if (tabtab != NULL)
4570 {
4571 sp = tabtab;
4572 for (l = 0; l < itemcnt; l++)
4573 {
4574 if (item[l].type == TabPage)
4575 {
4576 sp->start = item[l].start;
4577 sp->userhl = item[l].minwid;
4578 sp++;
4579 }
4580 }
4581 sp->start = NULL;
4582 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
4584
4585 return width;
4586}
4587#endif /* FEAT_STL_OPT */
4588
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004589#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4590 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004592 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4593 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 */
4595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004596get_rel_pos(
4597 win_T *wp,
4598 char_u *buf,
4599 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600{
4601 long above; /* number of lines above window */
4602 long below; /* number of lines below window */
4603
Bram Moolenaar0027c212015-01-07 13:31:52 +01004604 if (buflen < 3) /* need at least 3 chars for writing */
4605 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 above = wp->w_topline - 1;
4607#ifdef FEAT_DIFF
4608 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004609 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4610 above = 0; /* All buffer lines are displayed and there is an
4611 * indication of filler lines, that can be considered
4612 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613#endif
4614 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4615 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004616 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4617 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004619 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004621 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 ? (int)(above / ((above + below) / 100L))
4623 : (int)(above * 100L / (above + below)));
4624}
4625#endif
4626
4627/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004628 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 * Return TRUE if it was appended.
4630 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004632append_arg_number(
4633 win_T *wp,
4634 char_u *buf,
4635 int buflen,
4636 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
4638 char_u *p;
4639
4640 if (ARGCOUNT <= 1) /* nothing to do */
4641 return FALSE;
4642
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004643 p = buf + STRLEN(buf); /* go to the end of the buffer */
4644 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 return FALSE;
4646 *p++ = ' ';
4647 *p++ = '(';
4648 if (add_file)
4649 {
4650 STRCPY(p, "file ");
4651 p += 5;
4652 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004653 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4654 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4656 return TRUE;
4657}
4658
4659/*
4660 * If fname is not a full path, make it a full path.
4661 * Returns pointer to allocated memory (NULL for failure).
4662 */
4663 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004664fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665{
4666 /*
4667 * Force expanding the path always for Unix, because symbolic links may
4668 * mess up the full path name, even though it starts with a '/'.
4669 * Also expand when there is ".." in the file name, try to remove it,
4670 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004671 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 * For MS-Windows also expand names like "longna~1" to "longname".
4673 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004674#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 return FullName_save(fname, TRUE);
4676#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004677 if (!vim_isAbsName(fname)
4678 || strstr((char *)fname, "..") != NULL
4679 || strstr((char *)fname, "//") != NULL
4680# ifdef BACKSLASH_IN_FILENAME
4681 || strstr((char *)fname, "\\\\") != NULL
4682# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004683# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 )
4687 return FullName_save(fname, FALSE);
4688
4689 fname = vim_strsave(fname);
4690
Bram Moolenaar9b942202007-10-03 12:31:33 +00004691# ifdef USE_FNAME_CASE
4692# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
4696 if (fname != NULL)
4697 fname_case(fname, 0); /* set correct case for file name */
4698 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004699# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
4701 return fname;
4702#endif
4703}
4704
4705/*
4706 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4707 * "ffname" becomes a pointer to allocated memory (or NULL).
4708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004710fname_expand(
4711 buf_T *buf UNUSED,
4712 char_u **ffname,
4713 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714{
4715 if (*ffname == NULL) /* if no file name given, nothing to do */
4716 return;
4717 if (*sfname == NULL) /* if no short file name given, use ffname */
4718 *sfname = *ffname;
4719 *ffname = fix_fname(*ffname); /* expand to full path */
4720
4721#ifdef FEAT_SHORTCUT
4722 if (!buf->b_p_bin)
4723 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004724 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
4726 /* If the file name is a shortcut file, use the file it links to. */
4727 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004728 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 {
4730 vim_free(*ffname);
4731 *ffname = rfname;
4732 *sfname = rfname;
4733 }
4734 }
4735#endif
4736}
4737
4738/*
4739 * Get the file name for an argument list entry.
4740 */
4741 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004742alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 buf_T *bp;
4745
4746 /* Use the name from the associated buffer if it exists. */
4747 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004748 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 return aep->ae_fname;
4750 return bp->b_fname;
4751}
4752
4753#if defined(FEAT_WINDOWS) || defined(PROTO)
4754/*
4755 * do_arg_all(): Open up to 'count' windows, one for each argument.
4756 */
4757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004758do_arg_all(
4759 int count,
4760 int forceit, /* hide buffers in current windows */
4761 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762{
4763 int i;
4764 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004765 char_u *opened; /* Array of weight for which args are open:
4766 * 0: not opened
4767 * 1: opened in other tab
4768 * 2: opened in curtab
4769 * 3: opened in curtab and curwin
4770 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004771 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 int use_firstwin = FALSE; /* use first window for arglist */
4773 int split_ret = OK;
4774 int p_ea_save;
4775 alist_T *alist; /* argument list to be used */
4776 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004777 tabpage_T *tpnext;
4778 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004779 win_T *old_curwin, *last_curwin;
4780 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004781 win_T *new_curwin = NULL;
4782 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 if (ARGCOUNT <= 0)
4785 {
4786 /* Don't give an error message. We don't want it when the ":all"
4787 * command is in the .vimrc. */
4788 return;
4789 }
4790 setpcmark();
4791
4792 opened_len = ARGCOUNT;
4793 opened = alloc_clear((unsigned)opened_len);
4794 if (opened == NULL)
4795 return;
4796
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004797 /* Autocommands may do anything to the argument list. Make sure it's not
4798 * freed while we are working here by "locking" it. We still have to
4799 * watch out for its size to be changed. */
4800 alist = curwin->w_alist;
4801 ++alist->al_refcount;
4802
4803 old_curwin = curwin;
4804 old_curtab = curtab;
4805
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004806# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809
4810 /*
4811 * Try closing all windows that are not in the argument list.
4812 * Also close windows that are not full width;
4813 * When 'hidden' or "forceit" set the buffer becomes hidden.
4814 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004815 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004817 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004818 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004819 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004821 tpnext = curtab->tp_next;
4822 for (wp = firstwin; wp != NULL; wp = wpnext)
4823 {
4824 wpnext = wp->w_next;
4825 buf = wp->w_buffer;
4826 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004827 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004828 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004829 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004830 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004832 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004833 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004835 if (i < alist->al_ga.ga_len
4836 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4837 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4838 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004840 int weight = 1;
4841
4842 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004843 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004844 ++weight;
4845 if (old_curwin == wp)
4846 ++weight;
4847 }
4848
4849 if (weight > (int)opened[i])
4850 {
4851 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004852 if (i == 0)
4853 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004854 if (new_curwin != NULL)
4855 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004856 new_curwin = wp;
4857 new_curtab = curtab;
4858 }
4859 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004860 else if (keep_tabs)
4861 i = opened_len;
4862
4863 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004864 {
4865 /* Use the current argument list for all windows
4866 * containing a file from it. */
4867 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004868 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004869 ++wp->w_alist->al_refcount;
4870 }
4871 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004875 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004877 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004879 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004880 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004882 /* If the buffer was changed, and we would like to hide it,
4883 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004884 if (!P_HID(buf) && buf->b_nwindows <= 1
4885 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004887#ifdef FEAT_AUTOCMD
4888 bufref_T bufref;
4889
4890 set_bufref(&bufref, buf);
4891#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004892 (void)autowrite(buf, FALSE);
4893#ifdef FEAT_AUTOCMD
4894 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004895 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004896 {
4897 wpnext = firstwin; /* start all over... */
4898 continue;
4899 }
4900#endif
4901 }
4902#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004903 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004904 if (firstwin == lastwin
4905 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004906#endif
4907 use_firstwin = TRUE;
4908#ifdef FEAT_WINDOWS
4909 else
4910 {
4911 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4912# ifdef FEAT_AUTOCMD
4913 /* check if autocommands removed the next window */
4914 if (!win_valid(wpnext))
4915 wpnext = firstwin; /* start all over... */
4916# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918#endif
4919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 }
4921 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004922
4923 /* Without the ":tab" modifier only do the current tab page. */
4924 if (had_tab == 0 || tpnext == NULL)
4925 break;
4926
4927# ifdef FEAT_AUTOCMD
4928 /* check if autocommands removed the next tab page */
4929 if (!valid_tabpage(tpnext))
4930 tpnext = first_tabpage; /* start all over...*/
4931# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004932 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 }
4934
4935 /*
4936 * Open a window for files in the argument list that don't have one.
4937 * ARGCOUNT may change while doing this, because of autocommands.
4938 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004939 if (count > opened_len || count <= 0)
4940 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941
4942#ifdef FEAT_AUTOCMD
4943 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4944 ++autocmd_no_enter;
4945 ++autocmd_no_leave;
4946#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004947 last_curwin = curwin;
4948 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004950#ifdef FEAT_WINDOWS
4951 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4952 * leaving an empty tab page when executed locally. */
4953 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4954 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4955 use_firstwin = TRUE;
4956#endif
4957
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004958 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
4960 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4961 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004962 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 {
4964 /* Move the already present window to below the current window */
4965 if (curwin->w_arg_idx != i)
4966 {
4967 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4968 {
4969 if (wpnext->w_arg_idx == i)
4970 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004971 if (keep_tabs)
4972 {
4973 new_curwin = wpnext;
4974 new_curtab = curtab;
4975 }
4976 else
4977 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 break;
4979 }
4980 }
4981 }
4982 }
4983 else if (split_ret == OK)
4984 {
4985 if (!use_firstwin) /* split current window */
4986 {
4987 p_ea_save = p_ea;
4988 p_ea = TRUE; /* use space from all windows */
4989 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4990 p_ea = p_ea_save;
4991 if (split_ret == FAIL)
4992 continue;
4993 }
4994#ifdef FEAT_AUTOCMD
4995 else /* first window: do autocmd for leaving this buffer */
4996 --autocmd_no_leave;
4997#endif
4998
4999 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005000 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 */
5002 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005003 if (i == 0)
5004 {
5005 new_curwin = curwin;
5006 new_curtab = curtab;
5007 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5009 ECMD_ONE,
5010 ((P_HID(curwin->w_buffer)
5011 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005012 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013#ifdef FEAT_AUTOCMD
5014 if (use_firstwin)
5015 ++autocmd_no_leave;
5016#endif
5017 use_firstwin = FALSE;
5018 }
5019 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005020
5021 /* When ":tab" was used open a new tab for a new window repeatedly. */
5022 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5023 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 }
5025
5026 /* Remove the "lock" on the argument list. */
5027 alist_unlink(alist);
5028
5029#ifdef FEAT_AUTOCMD
5030 --autocmd_no_enter;
5031#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005032 /* restore last referenced tabpage's curwin */
5033 if (last_curtab != new_curtab)
5034 {
5035 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005036 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005037 if (win_valid(last_curwin))
5038 win_enter(last_curwin, FALSE);
5039 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005040 /* to window with first arg */
5041 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005042 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005043 if (win_valid(new_curwin))
5044 win_enter(new_curwin, FALSE);
5045
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#ifdef FEAT_AUTOCMD
5047 --autocmd_no_leave;
5048#endif
5049 vim_free(opened);
5050}
5051
5052# if defined(FEAT_LISTCMDS) || defined(PROTO)
5053/*
5054 * Open a window for a number of buffers.
5055 */
5056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005057ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058{
5059 buf_T *buf;
5060 win_T *wp, *wpnext;
5061 int split_ret = OK;
5062 int p_ea_save;
5063 int open_wins = 0;
5064 int r;
5065 int count; /* Maximum number of windows to open. */
5066 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005067#ifdef FEAT_WINDOWS
5068 int had_tab = cmdmod.tab;
5069 tabpage_T *tpnext;
5070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071
5072 if (eap->addr_count == 0) /* make as many windows as possible */
5073 count = 9999;
5074 else
5075 count = eap->line2; /* make as many windows as specified */
5076 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5077 all = FALSE;
5078 else
5079 all = TRUE;
5080
5081 setpcmark();
5082
5083#ifdef FEAT_GUI
5084 need_mouse_correct = TRUE;
5085#endif
5086
5087 /*
5088 * Close superfluous windows (two windows for the same buffer).
5089 * Also close windows that are not full-width.
5090 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005091#ifdef FEAT_WINDOWS
5092 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005093 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005094 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005097 tpnext = curtab->tp_next;
5098 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005100 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005101 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005102#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005103 || ((cmdmod.split & WSP_VERT)
5104 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005105 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005106 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005107 || (had_tab > 0 && wp != firstwin)
5108#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02005109 ) && firstwin != lastwin
5110#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005111 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005112#endif
5113 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005114 {
5115 win_close(wp, FALSE);
5116#ifdef FEAT_AUTOCMD
5117 wpnext = firstwin; /* just in case an autocommand does
5118 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005119 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005120 open_wins = 0;
5121#endif
5122 }
5123 else
5124 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005126
5127#ifdef FEAT_WINDOWS
5128 /* Without the ":tab" modifier only do the current tab page. */
5129 if (had_tab == 0 || tpnext == NULL)
5130 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005131 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 /*
5136 * Go through the buffer list. When a buffer doesn't have a window yet,
5137 * open one. Otherwise move the window to the right position.
5138 * Watch out for autocommands that delete buffers or windows!
5139 */
5140#ifdef FEAT_AUTOCMD
5141 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5142 ++autocmd_no_enter;
5143#endif
5144 win_enter(lastwin, FALSE);
5145#ifdef FEAT_AUTOCMD
5146 ++autocmd_no_leave;
5147#endif
5148 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5149 {
5150 /* Check if this buffer needs a window */
5151 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5152 continue;
5153
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005154#ifdef FEAT_WINDOWS
5155 if (had_tab != 0)
5156 {
5157 /* With the ":tab" modifier don't move the window. */
5158 if (buf->b_nwindows > 0)
5159 wp = lastwin; /* buffer has a window, skip it */
5160 else
5161 wp = NULL;
5162 }
5163 else
5164#endif
5165 {
5166 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005167 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005168 if (wp->w_buffer == buf)
5169 break;
5170 /* If the buffer already has a window, move it */
5171 if (wp != NULL)
5172 win_move_after(wp, curwin);
5173 }
5174
5175 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005177#ifdef FEAT_AUTOCMD
5178 bufref_T bufref;
5179
5180 set_bufref(&bufref, buf);
5181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 /* Split the window and put the buffer in it */
5183 p_ea_save = p_ea;
5184 p_ea = TRUE; /* use space from all windows */
5185 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5186 ++open_wins;
5187 p_ea = p_ea_save;
5188 if (split_ret == FAIL)
5189 continue;
5190
5191 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005192#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 swap_exists_action = SEA_DIALOG;
5194#endif
5195 set_curbuf(buf, DOBUF_GOTO);
5196#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005197 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005199 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005200#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005202# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 break;
5204 }
5205#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005206#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 if (swap_exists_action == SEA_QUIT)
5208 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005209# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5210 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005211
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005212 /* Reset the error/interrupt/exception state here so that
5213 * aborting() returns FALSE when closing a window. */
5214 enter_cleanup(&cs);
5215# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005216
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005217 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 win_close(curwin, TRUE);
5219 --open_wins;
5220 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005221 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005222
5223# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5224 /* Restore the error/interrupt/exception state if not
5225 * discarded by a new aborting error, interrupt, or uncaught
5226 * exception. */
5227 leave_cleanup(&cs);
5228# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 else
5231 handle_swap_exists(NULL);
5232#endif
5233 }
5234
5235 ui_breakcheck();
5236 if (got_int)
5237 {
5238 (void)vgetc(); /* only break the file loading, not the rest */
5239 break;
5240 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005241#ifdef FEAT_EVAL
5242 /* Autocommands deleted the buffer or aborted script processing!!! */
5243 if (aborting())
5244 break;
5245#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005246#ifdef FEAT_WINDOWS
5247 /* When ":tab" was used open a new tab for a new window repeatedly. */
5248 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5249 cmdmod.tab = 9999;
5250#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 }
5252#ifdef FEAT_AUTOCMD
5253 --autocmd_no_enter;
5254#endif
5255 win_enter(firstwin, FALSE); /* back to first window */
5256#ifdef FEAT_AUTOCMD
5257 --autocmd_no_leave;
5258#endif
5259
5260 /*
5261 * Close superfluous windows.
5262 */
5263 for (wp = lastwin; open_wins > count; )
5264 {
5265 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5266 || autowrite(wp->w_buffer, FALSE) == OK);
5267#ifdef FEAT_AUTOCMD
5268 if (!win_valid(wp))
5269 {
5270 /* BufWrite Autocommands made the window invalid, start over */
5271 wp = lastwin;
5272 }
5273 else
5274#endif
5275 if (r)
5276 {
5277 win_close(wp, !P_HID(wp->w_buffer));
5278 --open_wins;
5279 wp = lastwin;
5280 }
5281 else
5282 {
5283 wp = wp->w_prev;
5284 if (wp == NULL)
5285 break;
5286 }
5287 }
5288}
5289# endif /* FEAT_LISTCMDS */
5290
5291#endif /* FEAT_WINDOWS */
5292
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005293static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005294
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295/*
5296 * do_modelines() - process mode lines for the current file
5297 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005298 * "flags" can be:
5299 * OPT_WINONLY only set options local to window
5300 * OPT_NOWIN don't set options local to window
5301 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 * Returns immediately if the "ml" option isn't set.
5303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005305do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005307 linenr_T lnum;
5308 int nmlines;
5309 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310
5311 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5312 return;
5313
5314 /* Disallow recursive entry here. Can happen when executing a modeline
5315 * triggers an autocommand, which reloads modelines with a ":do". */
5316 if (entered)
5317 return;
5318
5319 ++entered;
5320 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5321 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005322 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 nmlines = 0;
5324
5325 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5326 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005327 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 nmlines = 0;
5329 --entered;
5330}
5331
5332#include "version.h" /* for version number */
5333
5334/*
5335 * chk_modeline() - check a single line for a mode string
5336 * Return FAIL if an error encountered.
5337 */
5338 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005339chk_modeline(
5340 linenr_T lnum,
5341 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342{
5343 char_u *s;
5344 char_u *e;
5345 char_u *linecopy; /* local copy of any modeline found */
5346 int prev;
5347 int vers;
5348 int end;
5349 int retval = OK;
5350 char_u *save_sourcing_name;
5351 linenr_T save_sourcing_lnum;
5352#ifdef FEAT_EVAL
5353 scid_T save_SID;
5354#endif
5355
5356 prev = -1;
5357 for (s = ml_get(lnum); *s != NUL; ++s)
5358 {
5359 if (prev == -1 || vim_isspace(prev))
5360 {
5361 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5362 || STRNCMP(s, "vi:", (size_t)3) == 0)
5363 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005364 /* Accept both "vim" and "Vim". */
5365 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 {
5367 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5368 e = s + 4;
5369 else
5370 e = s + 3;
5371 vers = getdigits(&e);
5372 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005373 && (s[0] != 'V'
5374 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 && (s[3] == ':'
5376 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5377 || (VIM_VERSION_100 < vers && s[3] == '<')
5378 || (VIM_VERSION_100 > vers && s[3] == '>')
5379 || (VIM_VERSION_100 == vers && s[3] == '=')))
5380 break;
5381 }
5382 }
5383 prev = *s;
5384 }
5385
5386 if (*s)
5387 {
5388 do /* skip over "ex:", "vi:" or "vim:" */
5389 ++s;
5390 while (s[-1] != ':');
5391
5392 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5393 if (linecopy == NULL)
5394 return FAIL;
5395
5396 save_sourcing_lnum = sourcing_lnum;
5397 save_sourcing_name = sourcing_name;
5398 sourcing_lnum = lnum; /* prepare for emsg() */
5399 sourcing_name = (char_u *)"modelines";
5400
5401 end = FALSE;
5402 while (end == FALSE)
5403 {
5404 s = skipwhite(s);
5405 if (*s == NUL)
5406 break;
5407
5408 /*
5409 * Find end of set command: ':' or end of line.
5410 * Skip over "\:", replacing it with ":".
5411 */
5412 for (e = s; *e != ':' && *e != NUL; ++e)
5413 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005414 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 if (*e == NUL)
5416 end = TRUE;
5417
5418 /*
5419 * If there is a "set" command, require a terminating ':' and
5420 * ignore the stuff after the ':'.
5421 * "vi:set opt opt opt: foo" -- foo not interpreted
5422 * "vi:opt opt opt: foo" -- foo interpreted
5423 * Accept "se" for compatibility with Elvis.
5424 */
5425 if (STRNCMP(s, "set ", (size_t)4) == 0
5426 || STRNCMP(s, "se ", (size_t)3) == 0)
5427 {
5428 if (*e != ':') /* no terminating ':'? */
5429 break;
5430 end = TRUE;
5431 s = vim_strchr(s, ' ') + 1;
5432 }
5433 *e = NUL; /* truncate the set command */
5434
5435 if (*s != NUL) /* skip over an empty "::" */
5436 {
5437#ifdef FEAT_EVAL
5438 save_SID = current_SID;
5439 current_SID = SID_MODELINE;
5440#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005441 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442#ifdef FEAT_EVAL
5443 current_SID = save_SID;
5444#endif
5445 if (retval == FAIL) /* stop if error found */
5446 break;
5447 }
5448 s = e + 1; /* advance to next part */
5449 }
5450
5451 sourcing_lnum = save_sourcing_lnum;
5452 sourcing_name = save_sourcing_name;
5453
5454 vim_free(linecopy);
5455 }
5456 return retval;
5457}
5458
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005459#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005461read_viminfo_bufferlist(
5462 vir_T *virp,
5463 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464{
5465 char_u *tab;
5466 linenr_T lnum;
5467 colnr_T col;
5468 buf_T *buf;
5469 char_u *sfname;
5470 char_u *xline;
5471
5472 /* Handle long line and escaped characters. */
5473 xline = viminfo_readstring(virp, 1, FALSE);
5474
5475 /* don't read in if there are files on the command-line or if writing: */
5476 if (xline != NULL && !writing && ARGCOUNT == 0
5477 && find_viminfo_parameter('%') != NULL)
5478 {
5479 /* Format is: <fname> Tab <lnum> Tab <col>.
5480 * Watch out for a Tab in the file name, work from the end. */
5481 lnum = 0;
5482 col = 0;
5483 tab = vim_strrchr(xline, '\t');
5484 if (tab != NULL)
5485 {
5486 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005487 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 tab = vim_strrchr(xline, '\t');
5489 if (tab != NULL)
5490 {
5491 *tab++ = '\0';
5492 lnum = atol((char *)tab);
5493 }
5494 }
5495
5496 /* Expand "~/" in the file name at "line + 1" to a full path.
5497 * Then try shortening it by comparing with the current directory */
5498 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005499 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500
5501 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5502 if (buf != NULL) /* just in case... */
5503 {
5504 buf->b_last_cursor.lnum = lnum;
5505 buf->b_last_cursor.col = col;
5506 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5507 }
5508 }
5509 vim_free(xline);
5510
5511 return viminfo_readline(virp);
5512}
5513
5514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005515write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516{
5517 buf_T *buf;
5518#ifdef FEAT_WINDOWS
5519 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005520 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521#endif
5522 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005523 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 if (find_viminfo_parameter('%') == NULL)
5526 return;
5527
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005528 /* Without a number -1 is returned: do all buffers. */
5529 max_buffers = get_viminfo_parameter('%');
5530
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005532#define LINE_BUF_LEN (MAXPATHL + 40)
5533 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 if (line == NULL)
5535 return;
5536
5537#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005538 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 set_last_cursor(win);
5540#else
5541 set_last_cursor(curwin);
5542#endif
5543
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005544 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005545 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 {
5547 if (buf->b_fname == NULL
5548 || !buf->b_p_bl
5549#ifdef FEAT_QUICKFIX
5550 || bt_quickfix(buf)
5551#endif
5552 || removable(buf->b_ffname))
5553 continue;
5554
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005555 if (max_buffers-- == 0)
5556 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 putc('%', fp);
5558 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005559 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 (long)buf->b_last_cursor.lnum,
5561 buf->b_last_cursor.col);
5562 viminfo_writestring(fp, line);
5563 }
5564 vim_free(line);
5565}
5566#endif
5567
5568
5569/*
5570 * Return special buffer name.
5571 * Returns NULL when the buffer has a normal file name.
5572 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005573 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005574buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5577 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005578 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005579 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005580 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005581
5582 /*
5583 * For location list window, w_llist_ref points to the location list.
5584 * For quickfix window, w_llist_ref is NULL.
5585 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005586 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005587 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005588 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005589 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591#endif
5592#ifdef FEAT_QUICKFIX
5593 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5594 * contains the name as specified by the user */
5595 if (bt_nofile(buf))
5596 {
5597 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005598 return buf->b_sfname;
5599 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 }
5601#endif
5602 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005603 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 return NULL;
5605}
5606
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005607#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5608 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5609 || defined(PROTO)
5610/*
5611 * Find a window for buffer "buf".
5612 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5613 * If not found FAIL is returned.
5614 */
5615 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005616find_win_for_buf(
5617 buf_T *buf,
5618 win_T **wp,
5619 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005620{
5621 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5622 if ((*wp)->w_buffer == buf)
5623 goto win_found;
5624 return FAIL;
5625win_found:
5626 return OK;
5627}
5628#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629
5630#if defined(FEAT_SIGNS) || defined(PROTO)
5631/*
5632 * Insert the sign into the signlist.
5633 */
5634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005635insert_sign(
5636 buf_T *buf, /* buffer to store sign in */
5637 signlist_T *prev, /* previous sign entry */
5638 signlist_T *next, /* next sign entry */
5639 int id, /* sign ID */
5640 linenr_T lnum, /* line number which gets the mark */
5641 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 signlist_T *newsign;
5644
5645 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5646 if (newsign != NULL)
5647 {
5648 newsign->id = id;
5649 newsign->lnum = lnum;
5650 newsign->typenr = typenr;
5651 newsign->next = next;
5652#ifdef FEAT_NETBEANS_INTG
5653 newsign->prev = prev;
5654 if (next != NULL)
5655 next->prev = newsign;
5656#endif
5657
5658 if (prev == NULL)
5659 {
5660 /* When adding first sign need to redraw the windows to create the
5661 * column for signs. */
5662 if (buf->b_signlist == NULL)
5663 {
5664 redraw_buf_later(buf, NOT_VALID);
5665 changed_cline_bef_curs();
5666 }
5667
5668 /* first sign in signlist */
5669 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005670#ifdef FEAT_NETBEANS_INTG
5671 if (netbeans_active())
5672 buf->b_has_sign_column = TRUE;
5673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674 }
5675 else
5676 prev->next = newsign;
5677 }
5678}
5679
5680/*
5681 * Add the sign into the signlist. Find the right spot to do it though.
5682 */
5683 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005684buf_addsign(
5685 buf_T *buf, /* buffer to store sign in */
5686 int id, /* sign ID */
5687 linenr_T lnum, /* line number which gets the mark */
5688 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689{
5690 signlist_T *sign; /* a sign in the signlist */
5691 signlist_T *prev; /* the previous sign */
5692
5693 prev = NULL;
5694 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5695 {
5696 if (lnum == sign->lnum && id == sign->id)
5697 {
5698 sign->typenr = typenr;
5699 return;
5700 }
5701 else if (
5702#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5703 id < 0 &&
5704#endif
5705 lnum < sign->lnum)
5706 {
5707#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5708 /* XXX - GRP: Is this because of sign slide problem? Or is it
5709 * really needed? Or is it because we allow multiple signs per
5710 * line? If so, should I add that feature to FEAT_SIGNS?
5711 */
5712 while (prev != NULL && prev->lnum == lnum)
5713 prev = prev->prev;
5714 if (prev == NULL)
5715 sign = buf->b_signlist;
5716 else
5717 sign = prev->next;
5718#endif
5719 insert_sign(buf, prev, sign, id, lnum, typenr);
5720 return;
5721 }
5722 prev = sign;
5723 }
5724#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5725 /* XXX - GRP: See previous comment */
5726 while (prev != NULL && prev->lnum == lnum)
5727 prev = prev->prev;
5728 if (prev == NULL)
5729 sign = buf->b_signlist;
5730 else
5731 sign = prev->next;
5732#endif
5733 insert_sign(buf, prev, sign, id, lnum, typenr);
5734
5735 return;
5736}
5737
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005738/*
5739 * For an existing, placed sign "markId" change the type to "typenr".
5740 * Returns the line number of the sign, or zero if the sign is not found.
5741 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005742 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005743buf_change_sign_type(
5744 buf_T *buf, /* buffer to store sign in */
5745 int markId, /* sign ID */
5746 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747{
5748 signlist_T *sign; /* a sign in the signlist */
5749
5750 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5751 {
5752 if (sign->id == markId)
5753 {
5754 sign->typenr = typenr;
5755 return sign->lnum;
5756 }
5757 }
5758
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005759 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760}
5761
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763buf_getsigntype(
5764 buf_T *buf,
5765 linenr_T lnum,
5766 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 signlist_T *sign; /* a sign in a b_signlist */
5769
5770 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5771 if (sign->lnum == lnum
5772 && (type == SIGN_ANY
5773# ifdef FEAT_SIGN_ICONS
5774 || (type == SIGN_ICON
5775 && sign_get_image(sign->typenr) != NULL)
5776# endif
5777 || (type == SIGN_TEXT
5778 && sign_get_text(sign->typenr) != NULL)
5779 || (type == SIGN_LINEHL
5780 && sign_get_attr(sign->typenr, TRUE) != 0)))
5781 return sign->typenr;
5782 return 0;
5783}
5784
5785
5786 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005787buf_delsign(
5788 buf_T *buf, /* buffer sign is stored in */
5789 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 signlist_T **lastp; /* pointer to pointer to current sign */
5792 signlist_T *sign; /* a sign in a b_signlist */
5793 signlist_T *next; /* the next sign in a b_signlist */
5794 linenr_T lnum; /* line number whose sign was deleted */
5795
5796 lastp = &buf->b_signlist;
5797 lnum = 0;
5798 for (sign = buf->b_signlist; sign != NULL; sign = next)
5799 {
5800 next = sign->next;
5801 if (sign->id == id)
5802 {
5803 *lastp = next;
5804#ifdef FEAT_NETBEANS_INTG
5805 if (next != NULL)
5806 next->prev = sign->prev;
5807#endif
5808 lnum = sign->lnum;
5809 vim_free(sign);
5810 break;
5811 }
5812 else
5813 lastp = &sign->next;
5814 }
5815
5816 /* When deleted the last sign need to redraw the windows to remove the
5817 * sign column. */
5818 if (buf->b_signlist == NULL)
5819 {
5820 redraw_buf_later(buf, NOT_VALID);
5821 changed_cline_bef_curs();
5822 }
5823
5824 return lnum;
5825}
5826
5827
5828/*
5829 * Find the line number of the sign with the requested id. If the sign does
5830 * not exist, return 0 as the line number. This will still let the correct file
5831 * get loaded.
5832 */
5833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005834buf_findsign(
5835 buf_T *buf, /* buffer to store sign in */
5836 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837{
5838 signlist_T *sign; /* a sign in the signlist */
5839
5840 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5841 if (sign->id == id)
5842 return sign->lnum;
5843
5844 return 0;
5845}
5846
5847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005848buf_findsign_id(
5849 buf_T *buf, /* buffer whose sign we are searching for */
5850 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851{
5852 signlist_T *sign; /* a sign in the signlist */
5853
5854 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5855 if (sign->lnum == lnum)
5856 return sign->id;
5857
5858 return 0;
5859}
5860
5861
5862# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5863/* see if a given type of sign exists on a specific line */
5864 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005865buf_findsigntype_id(
5866 buf_T *buf, /* buffer whose sign we are searching for */
5867 linenr_T lnum, /* line number of sign */
5868 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869{
5870 signlist_T *sign; /* a sign in the signlist */
5871
5872 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5873 if (sign->lnum == lnum && sign->typenr == typenr)
5874 return sign->id;
5875
5876 return 0;
5877}
5878
5879
5880# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5881/* return the number of icons on the given line */
5882 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005883buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884{
5885 signlist_T *sign; /* a sign in the signlist */
5886 int count = 0;
5887
5888 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5889 if (sign->lnum == lnum)
5890 if (sign_get_image(sign->typenr) != NULL)
5891 count++;
5892
5893 return count;
5894}
5895# endif /* FEAT_SIGN_ICONS */
5896# endif /* FEAT_NETBEANS_INTG */
5897
5898
5899/*
5900 * Delete signs in buffer "buf".
5901 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005903buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 signlist_T *next;
5906
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005907 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005908 * sign column. Not when curwin is NULL (this means we're exiting). */
5909 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005910 {
5911 redraw_buf_later(buf, NOT_VALID);
5912 changed_cline_bef_curs();
5913 }
5914
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 while (buf->b_signlist != NULL)
5916 {
5917 next = buf->b_signlist->next;
5918 vim_free(buf->b_signlist);
5919 buf->b_signlist = next;
5920 }
5921}
5922
5923/*
5924 * Delete all signs in all buffers.
5925 */
5926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005927buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005928{
5929 buf_T *buf; /* buffer we are checking for signs */
5930
Bram Moolenaar29323592016-07-24 22:04:11 +02005931 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934}
5935
5936/*
5937 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5938 */
5939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005940sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
5942 buf_T *buf;
5943 signlist_T *p;
5944 char lbuf[BUFSIZ];
5945
5946 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5947 msg_putchar('\n');
5948 if (rbuf == NULL)
5949 buf = firstbuf;
5950 else
5951 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005952 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
5954 if (buf->b_signlist != NULL)
5955 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005956 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5958 msg_putchar('\n');
5959 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005960 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005962 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5964 MSG_PUTS(lbuf);
5965 msg_putchar('\n');
5966 }
5967 if (rbuf != NULL)
5968 break;
5969 buf = buf->b_next;
5970 }
5971}
5972
5973/*
5974 * Adjust a placed sign for inserted/deleted lines.
5975 */
5976 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005977sign_mark_adjust(
5978 linenr_T line1,
5979 linenr_T line2,
5980 long amount,
5981 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982{
5983 signlist_T *sign; /* a sign in a b_signlist */
5984
5985 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5986 {
5987 if (sign->lnum >= line1 && sign->lnum <= line2)
5988 {
5989 if (amount == MAXLNUM)
5990 sign->lnum = line1;
5991 else
5992 sign->lnum += amount;
5993 }
5994 else if (sign->lnum > line2)
5995 sign->lnum += amount_after;
5996 }
5997}
5998#endif /* FEAT_SIGNS */
5999
6000/*
6001 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6002 */
6003 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005{
6006 if (on != curbuf->b_p_bl)
6007 {
6008 curbuf->b_p_bl = on;
6009#ifdef FEAT_AUTOCMD
6010 if (on)
6011 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6012 else
6013 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6014#endif
6015 }
6016}
6017
6018/*
6019 * Read the file for "buf" again and check if the contents changed.
6020 * Return TRUE if it changed or this could not be checked.
6021 */
6022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006023buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024{
6025 buf_T *newbuf;
6026 int differ = TRUE;
6027 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006029 exarg_T ea;
6030
6031 /* Allocate a buffer without putting it in the buffer list. */
6032 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6033 if (newbuf == NULL)
6034 return TRUE;
6035
6036 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6037 if (prep_exarg(&ea, buf) == FAIL)
6038 {
6039 wipe_buffer(newbuf, FALSE);
6040 return TRUE;
6041 }
6042
Bram Moolenaar071d4272004-06-13 20:20:40 +00006043 /* set curwin/curbuf to buf and save a few things */
6044 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045
Bram Moolenaar4770d092006-01-12 23:22:24 +00006046 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 && readfile(buf->b_ffname, buf->b_fname,
6048 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6049 &ea, READ_NEW | READ_DUMMY) == OK)
6050 {
6051 /* compare the two files line by line */
6052 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6053 {
6054 differ = FALSE;
6055 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6056 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6057 {
6058 differ = TRUE;
6059 break;
6060 }
6061 }
6062 }
6063 vim_free(ea.cmd);
6064
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 /* restore curwin/curbuf and a few other things */
6066 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067
6068 if (curbuf != newbuf) /* safety check */
6069 wipe_buffer(newbuf, FALSE);
6070
6071 return differ;
6072}
6073
6074/*
6075 * Wipe out a buffer and decrement the last buffer number if it was used for
6076 * this buffer. Call this to wipe out a temp buffer that does not contain any
6077 * marks.
6078 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006080wipe_buffer(
6081 buf_T *buf,
6082 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083{
6084 if (buf->b_fnum == top_file_num - 1)
6085 --top_file_num;
6086
6087#ifdef FEAT_AUTOCMD
6088 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006089 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006090#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006091 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092#ifdef FEAT_AUTOCMD
6093 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006094 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095#endif
6096}