blob: 648916821442b8d4a70b2f4de5e83dd3b152cfba [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 Moolenaar30180b82016-09-04 19:57:56 +0200479#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200480 /* Disallow deleting the buffer when it is locked (already being closed or
481 * halfway a command that relies on it). Unloading is allowed. */
482 if (buf->b_locked > 0 && (del_buf || wipe_buf))
483 {
484 EMSG(_("E937: Attempt to delete a buffer that is in use"));
485 return;
486 }
Bram Moolenaar30180b82016-09-04 19:57:56 +0200487#endif
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200488
Bram Moolenaar3be85852014-06-12 14:01:31 +0200489 if (win != NULL
490#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200491 && win_valid_any_tab(win) /* in case autocommands closed the window */
Bram Moolenaar3be85852014-06-12 14:01:31 +0200492#endif
493 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494 {
495 /* Set b_last_cursor when closing the last window for the buffer.
496 * Remember the last cursor position and window options of the buffer.
497 * This used to be only for the current window, but then options like
498 * 'foldmethod' may be lost with a ":only" command. */
499 if (buf->b_nwindows == 1)
500 set_last_cursor(win);
501 buflist_setfpos(buf, win,
502 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
503 win->w_cursor.col, TRUE);
504 }
505
506#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200507 set_bufref(&bufref, buf);
508
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 /* When the buffer is no longer in a window, trigger BufWinLeave */
510 if (buf->b_nwindows == 1)
511 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200512 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200513 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
514 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200515 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100516 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200517 /* Autocommands deleted the buffer. */
518aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100519 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100521 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200522 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200523 if (abort_if_last && one_window())
524 /* Autocommands made this the only window. */
525 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526
527 /* When the buffer becomes hidden, but is not unloaded, trigger
528 * BufHidden */
529 if (!unload_buf)
530 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200531 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200532 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
533 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200534 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200535 /* Autocommands deleted the buffer. */
536 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200537 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200538 if (abort_if_last && one_window())
539 /* Autocommands made this the only window. */
540 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
542# ifdef FEAT_EVAL
543 if (aborting()) /* autocmds may abort script processing */
544 return;
545# endif
546 }
547 nwindows = buf->b_nwindows;
548#endif
549
550 /* decrease the link count from windows (unless not in any window) */
551 if (buf->b_nwindows > 0)
552 --buf->b_nwindows;
553
554 /* Return when a window is displaying the buffer or when it's not
555 * unloaded. */
556 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558
559 /* Always remove the buffer when there is no file name. */
560 if (buf->b_ffname == NULL)
561 del_buf = TRUE;
562
563 /*
564 * Free all things allocated for this buffer.
565 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
566 */
567#ifdef FEAT_AUTOCMD
568 /* Remember if we are closing the current buffer. Restore the number of
569 * windows, so that autocommands in buf_freeall() don't get confused. */
570 is_curbuf = (buf == curbuf);
571 buf->b_nwindows = nwindows;
572#endif
573
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200574 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576#ifdef FEAT_AUTOCMD
577 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200578 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 return;
580# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000581 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 return;
583# endif
584
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 /*
586 * It's possible that autocommands change curbuf to the one being deleted.
587 * This might cause the previous curbuf to be deleted unexpectedly. But
588 * in some cases it's OK to delete the curbuf, because a new one is
589 * obtained anyway. Therefore only return if curbuf changed to the
590 * deleted buffer.
591 */
592 if (buf == curbuf && !is_curbuf)
593 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200594
595 if (
596#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200597 win_valid_any_tab(win) &&
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200598#else
599 win != NULL &&
600#endif
601 win->w_buffer == buf)
602 win->w_buffer = NULL; /* make sure we don't use the buffer now */
603
604 /* Autocommands may have opened or closed windows for this buffer.
605 * Decrement the count for the close we do here. */
606 if (buf->b_nwindows > 0)
607 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#endif
609
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000610 /* Change directories when the 'acd' option is set. */
611 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613 /*
614 * Remove the buffer from the list.
615 */
616 if (wipe_buf)
617 {
618#ifdef FEAT_SUN_WORKSHOP
619 if (usingSunWorkShop)
620 workshop_file_closed_lineno((char *)buf->b_ffname,
621 (int)buf->b_last_cursor.lnum);
622#endif
623 vim_free(buf->b_ffname);
624 vim_free(buf->b_sfname);
625 if (buf->b_prev == NULL)
626 firstbuf = buf->b_next;
627 else
628 buf->b_prev->b_next = buf->b_next;
629 if (buf->b_next == NULL)
630 lastbuf = buf->b_prev;
631 else
632 buf->b_next->b_prev = buf->b_prev;
633 free_buffer(buf);
634 }
635 else
636 {
637 if (del_buf)
638 {
639 /* Free all internal variables and reset option values, to make
640 * ":bdel" compatible with Vim 5.7. */
641 free_buffer_stuff(buf, TRUE);
642
643 /* Make it look like a new buffer. */
644 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
645
646 /* Init the options when loaded again. */
647 buf->b_p_initialized = FALSE;
648 }
649 buf_clear_file(buf);
650 if (del_buf)
651 buf->b_p_bl = FALSE;
652 }
653}
654
655/*
656 * Make buffer not contain a file.
657 */
658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100659buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660{
661 buf->b_ml.ml_line_count = 1;
662 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664 buf->b_p_eol = TRUE;
665 buf->b_start_eol = TRUE;
666#ifdef FEAT_MBYTE
667 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000668 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#endif
670 buf->b_ml.ml_mfp = NULL;
671 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
672#ifdef FEAT_NETBEANS_INTG
673 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#endif
675}
676
677/*
678 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200679 * the file. Careful: get here with "curwin" NULL when exiting.
680 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200681 * BFA_DEL buffer is going to be deleted
682 * BFA_WIPE buffer is going to be wiped out
683 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100686buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687{
688#ifdef FEAT_AUTOCMD
689 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200690 bufref_T bufref;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200691# ifdef FEAT_WINDOWS
692 int is_curwin = (curwin!= NULL && curwin->w_buffer == buf);
693 win_T *the_curwin = curwin;
694 tabpage_T *the_curtab = curtab;
695# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696
Bram Moolenaar5a497892016-09-03 16:29:04 +0200697 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200698 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200699 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200700 if (buf->b_ml.ml_mfp != NULL)
701 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200702 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
703 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200704 && !bufref_valid(&bufref))
705 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200706 return;
707 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200708 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200710 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
711 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200712 && !bufref_valid(&bufref))
713 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 return;
715 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200716 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200718 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
719 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200720 && !bufref_valid(&bufref))
721 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 return;
723 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200724 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200725
726# ifdef FEAT_WINDOWS
727 /* If the buffer was in curwin and the window has changed, go back to that
728 * window, if it still exists. This avoids that ":edit x" triggering a
729 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
730 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
731 {
732 block_autocmds();
733 goto_tabpage_win(the_curtab, the_curwin);
734 unblock_autocmds();
735 }
736# endif
737
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000739 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 return;
741# endif
742
743 /*
744 * It's possible that autocommands change curbuf to the one being deleted.
745 * This might cause curbuf to be deleted unexpectedly. But in some cases
746 * it's OK to delete the curbuf, because a new one is obtained anyway.
747 * Therefore only return if curbuf changed to the deleted buffer.
748 */
749 if (buf == curbuf && !is_curbuf)
750 return;
751#endif
752#ifdef FEAT_DIFF
753 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
754#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200755#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100756 /* Remove any ownsyntax, unless exiting. */
757 if (firstwin != NULL && curwin->w_buffer == buf)
758 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200759#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000760
761#ifdef FEAT_FOLDING
762 /* No folds in an empty buffer. */
763# ifdef FEAT_WINDOWS
764 {
765 win_T *win;
766 tabpage_T *tp;
767
768 FOR_ALL_TAB_WINDOWS(tp, win)
769 if (win->w_buffer == buf)
770 clearFolding(win);
771 }
772# else
773 if (curwin->w_buffer == buf)
774 clearFolding(curwin);
775# endif
776#endif
777
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#ifdef FEAT_TCL
779 tcl_buffer_free(buf);
780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 ml_close(buf, TRUE); /* close and delete the memline/memfile */
782 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200783 if ((flags & BFA_KEEP_UNDO) == 0)
784 {
785 u_blockfree(buf); /* free the memory allocated for undo */
786 u_clearall(buf); /* reset all undo information */
787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200789 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000791 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792}
793
794/*
795 * Free a buffer structure and the things it contains related to the buffer
796 * itself (not the file, that must have been done already).
797 */
798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100799free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200801 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200803#ifdef FEAT_EVAL
804 unref_var_dict(buf->b_vars);
805#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200806#ifdef FEAT_LUA
807 lua_buffer_free(buf);
808#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000809#ifdef FEAT_MZSCHEME
810 mzscheme_buffer_free(buf);
811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812#ifdef FEAT_PERL
813 perl_buf_free(buf);
814#endif
815#ifdef FEAT_PYTHON
816 python_buffer_free(buf);
817#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200818#ifdef FEAT_PYTHON3
819 python3_buffer_free(buf);
820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821#ifdef FEAT_RUBY
822 ruby_buffer_free(buf);
823#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200824#ifdef FEAT_JOB_CHANNEL
825 channel_buffer_free(buf);
826#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200827
828 buf_hashtab_remove(buf);
829
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200830#ifdef FEAT_AUTOCMD
831 aubuflocal_remove(buf);
832
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200833 if (autocmd_busy)
834 {
835 /* Do not free the buffer structure while autocommands are executing,
836 * it's still needed. Free it when autocmd_busy is reset. */
837 buf->b_next = au_pending_free_buf;
838 au_pending_free_buf = buf;
839 }
840 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000841#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200842 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843}
844
845/*
846 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
847 */
848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100849free_buffer_stuff(
850 buf_T *buf,
851 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852{
853 if (free_options)
854 {
855 clear_wininfo(buf); /* including window-local options */
856 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200857#ifdef FEAT_SPELL
858 ga_clear(&buf->b_s.b_langp);
859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200862 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
863 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#endif
865#ifdef FEAT_USR_CMDS
866 uc_clear(&buf->b_ucmds); /* clear local user commands */
867#endif
868#ifdef FEAT_SIGNS
869 buf_delete_signs(buf); /* delete any signs */
870#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000871#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200872 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874#ifdef FEAT_LOCALMAP
875 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
876 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
877#endif
878#ifdef FEAT_MBYTE
879 vim_free(buf->b_start_fenc);
880 buf->b_start_fenc = NULL;
881#endif
882}
883
884/*
885 * Free the b_wininfo list for buffer "buf".
886 */
887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100888clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889{
890 wininfo_T *wip;
891
892 while (buf->b_wininfo != NULL)
893 {
894 wip = buf->b_wininfo;
895 buf->b_wininfo = wip->wi_next;
896 if (wip->wi_optset)
897 {
898 clear_winopt(&wip->wi_opt);
899#ifdef FEAT_FOLDING
900 deleteFoldRecurse(&wip->wi_folds);
901#endif
902 }
903 vim_free(wip);
904 }
905}
906
907#if defined(FEAT_LISTCMDS) || defined(PROTO)
908/*
909 * Go to another buffer. Handles the result of the ATTENTION dialog.
910 */
911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100912goto_buffer(
913 exarg_T *eap,
914 int start,
915 int dir,
916 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000918# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200919 bufref_T old_curbuf;
920
921 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
923 swap_exists_action = SEA_DIALOG;
924# endif
925 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
926 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000927# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
929 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000930# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
931 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000932
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000933 /* Reset the error/interrupt/exception state here so that
934 * aborting() returns FALSE when closing a window. */
935 enter_cleanup(&cs);
936# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000937
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000938 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 win_close(curwin, TRUE);
940 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000941 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000942
943# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
944 /* Restore the error/interrupt/exception state if not discarded by a
945 * new aborting error, interrupt, or uncaught exception. */
946 leave_cleanup(&cs);
947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 }
949 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200950 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951# endif
952}
953#endif
954
Bram Moolenaare64ac772005-12-07 20:54:59 +0000955#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956/*
957 * Handle the situation of swap_exists_action being set.
958 * It is allowed for "old_curbuf" to be NULL or invalid.
959 */
960 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200961handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000963# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
964 cleanup_T cs;
965# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100966#ifdef FEAT_SYN_HL
967 long old_tw = curbuf->b_p_tw;
968#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200969 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000970
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 if (swap_exists_action == SEA_QUIT)
972 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000973# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
974 /* Reset the error/interrupt/exception state here so that
975 * aborting() returns FALSE when closing a buffer. */
976 enter_cleanup(&cs);
977# endif
978
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 /* User selected Quit at ATTENTION prompt. Go back to previous
980 * buffer. If that buffer is gone or the same as the current one,
981 * open a new, empty buffer. */
982 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000983 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100984 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200985 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
986 || old_curbuf->br_buf == curbuf)
987 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
988 else
989 buf = old_curbuf->br_buf;
990 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100991 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200992 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100993#ifdef FEAT_SYN_HL
994 if (old_tw != curbuf->b_p_tw)
995 check_colorcolumn(curwin);
996#endif
997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000999
1000# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1001 /* Restore the error/interrupt/exception state if not discarded by a
1002 * new aborting error, interrupt, or uncaught exception. */
1003 leave_cleanup(&cs);
1004# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 }
1006 else if (swap_exists_action == SEA_RECOVER)
1007 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001008# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1009 /* Reset the error/interrupt/exception state here so that
1010 * aborting() returns FALSE when closing a buffer. */
1011 enter_cleanup(&cs);
1012# endif
1013
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 /* User selected Recover at ATTENTION prompt. */
1015 msg_scroll = TRUE;
1016 ml_recover();
1017 MSG_PUTS("\n"); /* don't overwrite the last message */
1018 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001019 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001020
1021# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1022 /* Restore the error/interrupt/exception state if not discarded by a
1023 * new aborting error, interrupt, or uncaught exception. */
1024 leave_cleanup(&cs);
1025# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 }
1027 swap_exists_action = SEA_NONE;
1028}
1029#endif
1030
1031#if defined(FEAT_LISTCMDS) || defined(PROTO)
1032/*
1033 * do_bufdel() - delete or unload buffer(s)
1034 *
1035 * addr_count == 0: ":bdel" - delete current buffer
1036 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1037 * buffer "end_bnr", then any other arguments.
1038 * addr_count == 2: ":N,N bdel" - delete buffers in range
1039 *
1040 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1041 * DOBUF_DEL (":bdel")
1042 *
1043 * Returns error message or NULL
1044 */
1045 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001046do_bufdel(
1047 int command,
1048 char_u *arg, /* pointer to extra arguments */
1049 int addr_count,
1050 int start_bnr, /* first buffer number in a range */
1051 int end_bnr, /* buffer nr or last buffer nr in a range */
1052 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053{
1054 int do_current = 0; /* delete current buffer? */
1055 int deleted = 0; /* number of buffers deleted */
1056 char_u *errormsg = NULL; /* return value */
1057 int bnr; /* buffer number */
1058 char_u *p;
1059
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (addr_count == 0)
1061 {
1062 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1063 }
1064 else
1065 {
1066 if (addr_count == 2)
1067 {
1068 if (*arg) /* both range and argument is not allowed */
1069 return (char_u *)_(e_trailing);
1070 bnr = start_bnr;
1071 }
1072 else /* addr_count == 1 */
1073 bnr = end_bnr;
1074
1075 for ( ;!got_int; ui_breakcheck())
1076 {
1077 /*
1078 * delete the current buffer last, otherwise when the
1079 * current buffer is deleted, the next buffer becomes
1080 * the current one and will be loaded, which may then
1081 * also be deleted, etc.
1082 */
1083 if (bnr == curbuf->b_fnum)
1084 do_current = bnr;
1085 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1086 forceit) == OK)
1087 ++deleted;
1088
1089 /*
1090 * find next buffer number to delete/unload
1091 */
1092 if (addr_count == 2)
1093 {
1094 if (++bnr > end_bnr)
1095 break;
1096 }
1097 else /* addr_count == 1 */
1098 {
1099 arg = skipwhite(arg);
1100 if (*arg == NUL)
1101 break;
1102 if (!VIM_ISDIGIT(*arg))
1103 {
1104 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001105 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1106 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107 if (bnr < 0) /* failed */
1108 break;
1109 arg = p;
1110 }
1111 else
1112 bnr = getdigits(&arg);
1113 }
1114 }
1115 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1116 FORWARD, do_current, forceit) == OK)
1117 ++deleted;
1118
1119 if (deleted == 0)
1120 {
1121 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001122 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001124 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001126 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 errormsg = IObuff;
1128 }
1129 else if (deleted >= p_report)
1130 {
1131 if (command == DOBUF_UNLOAD)
1132 {
1133 if (deleted == 1)
1134 MSG(_("1 buffer unloaded"));
1135 else
1136 smsg((char_u *)_("%d buffers unloaded"), deleted);
1137 }
1138 else if (command == DOBUF_DEL)
1139 {
1140 if (deleted == 1)
1141 MSG(_("1 buffer deleted"));
1142 else
1143 smsg((char_u *)_("%d buffers deleted"), deleted);
1144 }
1145 else
1146 {
1147 if (deleted == 1)
1148 MSG(_("1 buffer wiped out"));
1149 else
1150 smsg((char_u *)_("%d buffers wiped out"), deleted);
1151 }
1152 }
1153 }
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155
1156 return errormsg;
1157}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001158#endif /* FEAT_LISTCMDS */
1159
1160#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1161 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001163static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001164
1165/*
1166 * Make the current buffer empty.
1167 * Used when it is wiped out and it's the last buffer.
1168 */
1169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001170empty_curbuf(
1171 int close_others,
1172 int forceit,
1173 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001174{
1175 int retval;
1176 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001177 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001178
1179 if (action == DOBUF_UNLOAD)
1180 {
1181 EMSG(_("E90: Cannot unload last buffer"));
1182 return FAIL;
1183 }
1184
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001185 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001186#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001187 if (close_others)
1188 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001189 close_windows(buf, TRUE);
1190#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001191
1192 setpcmark();
1193 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1194 forceit ? ECMD_FORCEIT : 0, curwin);
1195
1196 /*
1197 * do_ecmd() may create a new buffer, then we have to delete
1198 * the old one. But do_ecmd() may have done that already, check
1199 * if the buffer still exists.
1200 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001201 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001202 close_buffer(NULL, buf, action, FALSE);
1203 if (!close_others)
1204 need_fileinfo = FALSE;
1205 return retval;
1206}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207/*
1208 * Implementation of the commands for the buffer list.
1209 *
1210 * action == DOBUF_GOTO go to specified buffer
1211 * action == DOBUF_SPLIT split window and go to specified buffer
1212 * action == DOBUF_UNLOAD unload specified buffer(s)
1213 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1214 * action == DOBUF_WIPE delete specified buffer(s) really
1215 *
1216 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1217 * start == DOBUF_FIRST go to "count" buffer from first buffer
1218 * start == DOBUF_LAST go to "count" buffer from last buffer
1219 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1220 *
1221 * Return FAIL or OK.
1222 */
1223 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001224do_buffer(
1225 int action,
1226 int start,
1227 int dir, /* FORWARD or BACKWARD */
1228 int count, /* buffer number or number of buffers */
1229 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230{
1231 buf_T *buf;
1232 buf_T *bp;
1233 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1234 || action == DOBUF_WIPE);
1235
1236 switch (start)
1237 {
1238 case DOBUF_FIRST: buf = firstbuf; break;
1239 case DOBUF_LAST: buf = lastbuf; break;
1240 default: buf = curbuf; break;
1241 }
1242 if (start == DOBUF_MOD) /* find next modified buffer */
1243 {
1244 while (count-- > 0)
1245 {
1246 do
1247 {
1248 buf = buf->b_next;
1249 if (buf == NULL)
1250 buf = firstbuf;
1251 }
1252 while (buf != curbuf && !bufIsChanged(buf));
1253 }
1254 if (!bufIsChanged(buf))
1255 {
1256 EMSG(_("E84: No modified buffer found"));
1257 return FAIL;
1258 }
1259 }
1260 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1261 {
1262 while (buf != NULL && buf->b_fnum != count)
1263 buf = buf->b_next;
1264 }
1265 else
1266 {
1267 bp = NULL;
1268 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1269 {
1270 /* remember the buffer where we start, we come back there when all
1271 * buffers are unlisted. */
1272 if (bp == NULL)
1273 bp = buf;
1274 if (dir == FORWARD)
1275 {
1276 buf = buf->b_next;
1277 if (buf == NULL)
1278 buf = firstbuf;
1279 }
1280 else
1281 {
1282 buf = buf->b_prev;
1283 if (buf == NULL)
1284 buf = lastbuf;
1285 }
1286 /* don't count unlisted buffers */
1287 if (unload || buf->b_p_bl)
1288 {
1289 --count;
1290 bp = NULL; /* use this buffer as new starting point */
1291 }
1292 if (bp == buf)
1293 {
1294 /* back where we started, didn't find anything. */
1295 EMSG(_("E85: There is no listed buffer"));
1296 return FAIL;
1297 }
1298 }
1299 }
1300
1301 if (buf == NULL) /* could not find it */
1302 {
1303 if (start == DOBUF_FIRST)
1304 {
1305 /* don't warn when deleting */
1306 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001307 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 else if (dir == FORWARD)
1310 EMSG(_("E87: Cannot go beyond last buffer"));
1311 else
1312 EMSG(_("E88: Cannot go before first buffer"));
1313 return FAIL;
1314 }
1315
1316#ifdef FEAT_GUI
1317 need_mouse_correct = TRUE;
1318#endif
1319
1320#ifdef FEAT_LISTCMDS
1321 /*
1322 * delete buffer buf from memory and/or the list
1323 */
1324 if (unload)
1325 {
1326 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001327# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1328 bufref_T bufref;
1329
1330 set_bufref(&bufref, buf);
1331# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332
1333 /* When unloading or deleting a buffer that's already unloaded and
1334 * unlisted: fail silently. */
1335 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1336 return FAIL;
1337
1338 if (!forceit && bufIsChanged(buf))
1339 {
1340#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1341 if ((p_confirm || cmdmod.confirm) && p_write)
1342 {
1343 dialog_changed(buf, FALSE);
1344# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001345 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 /* Autocommand deleted buffer, oops! It's not changed
1347 * now. */
1348 return FAIL;
1349# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001350 /* If it's still changed fail silently, the dialog already
1351 * mentioned why it fails. */
1352 if (bufIsChanged(buf))
1353 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001355 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356#endif
1357 {
1358 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1359 buf->b_fnum);
1360 return FAIL;
1361 }
1362 }
1363
1364 /*
1365 * If deleting the last (listed) buffer, make it empty.
1366 * The last (listed) buffer cannot be unloaded.
1367 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001368 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (bp->b_p_bl && bp != buf)
1370 break;
1371 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001372 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
1374#ifdef FEAT_WINDOWS
1375 /*
1376 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001377 * (unless it's the only window). Repeat this so long as we end up in
1378 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001380 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001381# ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001382 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02001383# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001384 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001385 {
1386 if (win_close(curwin, FALSE) == FAIL)
1387 break;
1388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#endif
1390
1391 /*
1392 * If the buffer to be deleted is not the current one, delete it here.
1393 */
1394 if (buf != curbuf)
1395 {
1396#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001398 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001400 if (buf->b_nwindows <= 0)
1401 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 return OK;
1403 }
1404
1405 /*
1406 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001407 * There should be another, otherwise it would have been handled
1408 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001409 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 * Then prefer the buffer we most recently visited.
1411 * Else try to find one that is loaded, after the current buffer,
1412 * then before the current buffer.
1413 * Finally use any buffer.
1414 */
1415 buf = NULL; /* selected buffer */
1416 bp = NULL; /* used when no loaded buffer found */
1417#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001418 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1419 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420# ifdef FEAT_JUMPLIST
1421 else
1422# endif
1423#endif
1424#ifdef FEAT_JUMPLIST
1425 if (curwin->w_jumplistlen > 0)
1426 {
1427 int jumpidx;
1428
1429 jumpidx = curwin->w_jumplistidx - 1;
1430 if (jumpidx < 0)
1431 jumpidx = curwin->w_jumplistlen - 1;
1432
1433 forward = jumpidx;
1434 while (jumpidx != curwin->w_jumplistidx)
1435 {
1436 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1437 if (buf != NULL)
1438 {
1439 if (buf == curbuf || !buf->b_p_bl)
1440 buf = NULL; /* skip current and unlisted bufs */
1441 else if (buf->b_ml.ml_mfp == NULL)
1442 {
1443 /* skip unloaded buf, but may keep it for later */
1444 if (bp == NULL)
1445 bp = buf;
1446 buf = NULL;
1447 }
1448 }
1449 if (buf != NULL) /* found a valid buffer: stop searching */
1450 break;
1451 /* advance to older entry in jump list */
1452 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1453 break;
1454 if (--jumpidx < 0)
1455 jumpidx = curwin->w_jumplistlen - 1;
1456 if (jumpidx == forward) /* List exhausted for sure */
1457 break;
1458 }
1459 }
1460#endif
1461
1462 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1463 {
1464 forward = TRUE;
1465 buf = curbuf->b_next;
1466 for (;;)
1467 {
1468 if (buf == NULL)
1469 {
1470 if (!forward) /* tried both directions */
1471 break;
1472 buf = curbuf->b_prev;
1473 forward = FALSE;
1474 continue;
1475 }
1476 /* in non-help buffer, try to skip help buffers, and vv */
1477 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1478 {
1479 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1480 break;
1481 if (bp == NULL) /* remember unloaded buf for later */
1482 bp = buf;
1483 }
1484 if (forward)
1485 buf = buf->b_next;
1486 else
1487 buf = buf->b_prev;
1488 }
1489 }
1490 if (buf == NULL) /* No loaded buffer, use unloaded one */
1491 buf = bp;
1492 if (buf == NULL) /* No loaded buffer, find listed one */
1493 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001494 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 if (buf->b_p_bl && buf != curbuf)
1496 break;
1497 }
1498 if (buf == NULL) /* Still no buffer, just take one */
1499 {
1500 if (curbuf->b_next != NULL)
1501 buf = curbuf->b_next;
1502 else
1503 buf = curbuf->b_prev;
1504 }
1505 }
1506
Bram Moolenaara02471e2014-01-10 16:43:14 +01001507 if (buf == NULL)
1508 {
1509 /* Autocommands must have wiped out all other buffers. Only option
1510 * now is to make the current buffer empty. */
1511 return empty_curbuf(FALSE, forceit, action);
1512 }
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /*
1515 * make buf current buffer
1516 */
1517 if (action == DOBUF_SPLIT) /* split window first */
1518 {
1519# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001520 /* If 'switchbuf' contains "useopen": jump to first window containing
1521 * "buf" if one exists */
1522 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001523 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001524 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001525 * page containing "buf" if one exists */
1526 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527 return OK;
1528 if (win_split(0, 0) == FAIL)
1529# endif
1530 return FAIL;
1531 }
1532#endif
1533
1534 /* go to current buffer - nothing to do */
1535 if (buf == curbuf)
1536 return OK;
1537
1538 /*
1539 * Check if the current buffer may be abandoned.
1540 */
1541 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1542 {
1543#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1544 if ((p_confirm || cmdmod.confirm) && p_write)
1545 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001546# ifdef FEAT_AUTOCMD
1547 bufref_T bufref;
1548
1549 set_bufref(&bufref, buf);
1550# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 dialog_changed(curbuf, FALSE);
1552# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001553 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 /* Autocommand deleted buffer, oops! */
1555 return FAIL;
1556# endif
1557 }
1558 if (bufIsChanged(curbuf))
1559#endif
1560 {
1561 EMSG(_(e_nowrtmsg));
1562 return FAIL;
1563 }
1564 }
1565
1566 /* Go to the other buffer. */
1567 set_curbuf(buf, action);
1568
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001569#if defined(FEAT_LISTCMDS) \
1570 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001572 {
1573 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575#endif
1576
1577#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1578 if (aborting()) /* autocmds may abort script processing */
1579 return FAIL;
1580#endif
1581
1582 return OK;
1583}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585
1586/*
1587 * Set current buffer to "buf". Executes autocommands and closes current
1588 * buffer. "action" tells how to close the current buffer:
1589 * DOBUF_GOTO free or hide it
1590 * DOBUF_SPLIT nothing
1591 * DOBUF_UNLOAD unload it
1592 * DOBUF_DEL delete it
1593 * DOBUF_WIPE wipe it out
1594 */
1595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001596set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597{
1598 buf_T *prevbuf;
1599 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1600 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001601#ifdef FEAT_SYN_HL
1602 long old_tw = curbuf->b_p_tw;
1603#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001604 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605
1606 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001607 if (!cmdmod.keepalt)
1608 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001609 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 /* Don't restart Select mode after switching to another buffer. */
1612 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614 /* close_windows() or apply_autocmds() may change curbuf */
1615 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001616 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617
1618#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001619 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001621 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001623 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624# endif
1625#endif
1626 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001627#ifdef FEAT_SYN_HL
1628 if (prevbuf == curwin->w_buffer)
1629 reset_synblock(curwin);
1630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631#ifdef FEAT_WINDOWS
1632 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001633 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634#endif
1635#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001636 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001638 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001640 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001641#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001642 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001643#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001644 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001645 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1647 unload ? action : (action == DOBUF_GOTO
1648 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001649 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001650#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001651 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001652 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001653 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001654#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 }
1657#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001658 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001659 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001660 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1661 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001662# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001663 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001664# endif
1665# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001666 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001667# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001668 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001672#ifdef FEAT_SYN_HL
1673 if (old_tw != curbuf->b_p_tw)
1674 check_colorcolumn(curwin);
1675#endif
1676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677}
1678
1679/*
1680 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001681 * Old curbuf must have been abandoned already! This also means "curbuf" may
1682 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 */
1684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001685enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686{
1687 /* Copy buffer and window local option values. Not for a help buffer. */
1688 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1689 if (!buf->b_help)
1690 get_winopts(buf);
1691#ifdef FEAT_FOLDING
1692 else
1693 /* Remove all folds in the window. */
1694 clearFolding(curwin);
1695 foldUpdateAll(curwin); /* update folds (later). */
1696#endif
1697
1698 /* Get the buffer in the current window. */
1699 curwin->w_buffer = buf;
1700 curbuf = buf;
1701 ++curbuf->b_nwindows;
1702
1703#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001704 if (curwin->w_p_diff)
1705 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706#endif
1707
Bram Moolenaara971b822011-09-14 14:43:25 +02001708#ifdef FEAT_SYN_HL
1709 curwin->w_s = &(buf->b_s);
1710#endif
1711
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712 /* Cursor on first line by default. */
1713 curwin->w_cursor.lnum = 1;
1714 curwin->w_cursor.col = 0;
1715#ifdef FEAT_VIRTUALEDIT
1716 curwin->w_cursor.coladd = 0;
1717#endif
1718 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001719#ifdef FEAT_AUTOCMD
1720 curwin->w_topline_was_set = FALSE;
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001723 /* mark cursor position as being invalid */
1724 curwin->w_valid = 0;
1725
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 /* Make sure the buffer is loaded. */
1727 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001728 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001729#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001730 /* If there is no filetype, allow for detecting one. Esp. useful for
1731 * ":ball" used in a autocommand. If there already is a filetype we
1732 * might prefer to keep it. */
1733 if (*curbuf->b_p_ft == NUL)
1734 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001735#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001736
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001737 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 else
1740 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001741 if (!msg_silent)
1742 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1744#ifdef FEAT_AUTOCMD
1745 curwin->w_topline = 1;
1746# ifdef FEAT_DIFF
1747 curwin->w_topfill = 0;
1748# endif
1749 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1750 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1751#endif
1752 }
1753
1754 /* If autocommands did not change the cursor position, restore cursor lnum
1755 * and possibly cursor col. */
1756 if (curwin->w_cursor.lnum == 1 && inindent(0))
1757 buflist_getfpos();
1758
1759 check_arg_idx(curwin); /* check for valid arg_idx */
1760#ifdef FEAT_TITLE
1761 maketitle();
1762#endif
1763#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001764 /* when autocmds didn't change it */
1765 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766#endif
1767 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1768
Bram Moolenaar009b2592004-10-24 19:18:58 +00001769#ifdef FEAT_NETBEANS_INTG
1770 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001771 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001772#endif
1773
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001774 /* Change directories when the 'acd' option is set. */
1775 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776
1777#ifdef FEAT_KEYMAP
1778 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001779 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001781#ifdef FEAT_SPELL
1782 /* May need to set the spell language. Can only do this after the buffer
1783 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001784 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1785 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001786#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001787#ifdef FEAT_VIMINFO
1788 curbuf->b_last_used = vim_time();
1789#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 redraw_later(NOT_VALID);
1792}
1793
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001794#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1795/*
1796 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001797 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001798 */
1799 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001800do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001801{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001802 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001803 && curbuf->b_ffname != NULL
1804 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001805 shorten_fnames(TRUE);
1806}
1807#endif
1808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809/*
1810 * functions for dealing with the buffer list
1811 */
1812
Bram Moolenaar480778b2016-07-14 22:09:39 +02001813static int top_file_num = 1; /* highest file number */
1814
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815/*
1816 * Add a file name to the buffer list. Return a pointer to the buffer.
1817 * If the same file name already exists return a pointer to that buffer.
1818 * If it does not exist, or if fname == NULL, a new entry is created.
1819 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1820 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1821 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001822 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001823 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1824 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 * This is the ONLY way to create a new buffer.
1826 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828buflist_new(
1829 char_u *ffname, /* full path of fname or relative */
1830 char_u *sfname, /* short fname or NULL */
1831 linenr_T lnum, /* preferred cursor line */
1832 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833{
1834 buf_T *buf;
1835#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001836 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837#endif
1838
Bram Moolenaar480778b2016-07-14 22:09:39 +02001839 if (top_file_num == 1)
1840 hash_init(&buf_hashtab);
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1843
1844 /*
1845 * If file name already exists in the list, update the entry.
1846 */
1847#ifdef UNIX
1848 /* On Unix we can use inode numbers when the file exists. Works better
1849 * for hard links. */
1850 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1851 st.st_dev = (dev_T)-1;
1852#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001853 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854#ifdef UNIX
1855 buflist_findname_stat(ffname, &st)
1856#else
1857 buflist_findname(ffname)
1858#endif
1859 ) != NULL)
1860 {
1861 vim_free(ffname);
1862 if (lnum != 0)
1863 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001864
1865 if ((flags & BLN_NOOPT) == 0)
1866 /* copy the options now, if 'cpo' doesn't have 's' and not done
1867 * already */
1868 buf_copy_options(buf, 0);
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1871 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001872#ifdef FEAT_AUTOCMD
1873 bufref_T bufref;
1874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 buf->b_p_bl = TRUE;
1876#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001877 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001879 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001880 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001881 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001882 return NULL;
1883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884#endif
1885 }
1886 return buf;
1887 }
1888
1889 /*
1890 * If the current buffer has no name and no contents, use the current
1891 * buffer. Otherwise: Need to allocate a new buffer structure.
1892 *
1893 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001894 * (A spell file buffer is allocated in spell.c, but that's not a normal
1895 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 */
1897 buf = NULL;
1898 if ((flags & BLN_CURBUF)
1899 && curbuf != NULL
1900 && curbuf->b_ffname == NULL
1901 && curbuf->b_nwindows <= 1
1902 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1903 {
1904 buf = curbuf;
1905#ifdef FEAT_AUTOCMD
1906 /* It's like this buffer is deleted. Watch out for autocommands that
1907 * change curbuf! If that happens, allocate a new buffer anyway. */
1908 if (curbuf->b_p_bl)
1909 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1910 if (buf == curbuf)
1911 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1912# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001913 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 return NULL;
1915# endif
1916#endif
1917#ifdef FEAT_QUICKFIX
1918# ifdef FEAT_AUTOCMD
1919 if (buf == curbuf)
1920# endif
1921 {
1922 /* Make sure 'bufhidden' and 'buftype' are empty */
1923 clear_string_option(&buf->b_p_bh);
1924 clear_string_option(&buf->b_p_bt);
1925 }
1926#endif
1927 }
1928 if (buf != curbuf || curbuf == NULL)
1929 {
1930 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1931 if (buf == NULL)
1932 {
1933 vim_free(ffname);
1934 return NULL;
1935 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001936#ifdef FEAT_EVAL
1937 /* init b: variables */
1938 buf->b_vars = dict_alloc();
1939 if (buf->b_vars == NULL)
1940 {
1941 vim_free(ffname);
1942 vim_free(buf);
1943 return NULL;
1944 }
1945 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948
1949 if (ffname != NULL)
1950 {
1951 buf->b_ffname = ffname;
1952 buf->b_sfname = vim_strsave(sfname);
1953 }
1954
1955 clear_wininfo(buf);
1956 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1957
1958 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1959 || buf->b_wininfo == NULL)
1960 {
1961 vim_free(buf->b_ffname);
1962 buf->b_ffname = NULL;
1963 vim_free(buf->b_sfname);
1964 buf->b_sfname = NULL;
1965 if (buf != curbuf)
1966 free_buffer(buf);
1967 return NULL;
1968 }
1969
1970 if (buf == curbuf)
1971 {
1972 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001973 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 if (buf != curbuf) /* autocommands deleted the buffer! */
1975 return NULL;
1976#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001977 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 return NULL;
1979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001981
1982 /* Init the options. */
1983 buf->b_p_initialized = FALSE;
1984 buf_copy_options(buf, BCO_ENTER);
1985
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986#ifdef FEAT_KEYMAP
1987 /* need to reload lmaps and set b:keymap_name */
1988 curbuf->b_kmap_state |= KEYMAP_INIT;
1989#endif
1990 }
1991 else
1992 {
1993 /*
1994 * put new buffer at the end of the buffer list
1995 */
1996 buf->b_next = NULL;
1997 if (firstbuf == NULL) /* buffer list is empty */
1998 {
1999 buf->b_prev = NULL;
2000 firstbuf = buf;
2001 }
2002 else /* append new buffer at end of list */
2003 {
2004 lastbuf->b_next = buf;
2005 buf->b_prev = lastbuf;
2006 }
2007 lastbuf = buf;
2008
2009 buf->b_fnum = top_file_num++;
2010 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2011 {
2012 EMSG(_("W14: Warning: List of file names overflow"));
2013 if (emsg_silent == 0)
2014 {
2015 out_flush();
2016 ui_delay(3000L, TRUE); /* make sure it is noticed */
2017 }
2018 top_file_num = 1;
2019 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002020 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 /*
2023 * Always copy the options from the current buffer.
2024 */
2025 buf_copy_options(buf, BCO_ALWAYS);
2026 }
2027
2028 buf->b_wininfo->wi_fpos.lnum = lnum;
2029 buf->b_wininfo->wi_win = curwin;
2030
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002031#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002032 hash_init(&buf->b_s.b_keywtab);
2033 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034#endif
2035
2036 buf->b_fname = buf->b_sfname;
2037#ifdef UNIX
2038 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002039 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002040 else
2041 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002042 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 buf->b_dev = st.st_dev;
2044 buf->b_ino = st.st_ino;
2045 }
2046#endif
2047 buf->b_u_synced = TRUE;
2048 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002049 if (flags & BLN_DUMMY)
2050 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 buf_clear_file(buf);
2052 clrallmarks(buf); /* clear marks */
2053 fmarks_check_names(buf); /* check file marks for this file */
2054 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2055#ifdef FEAT_AUTOCMD
2056 if (!(flags & BLN_DUMMY))
2057 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002058 bufref_T bufref;
2059
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002060 /* Tricky: these autocommands may change the buffer list. They could
2061 * also split the window with re-using the one empty buffer. This may
2062 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002063 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002064 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002065 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002066 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002068 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002069 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002070 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002071 return NULL;
2072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002074 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 return NULL;
2076# endif
2077 }
2078#endif
2079
2080 return buf;
2081}
2082
2083/*
2084 * Free the memory for the options of a buffer.
2085 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2086 * 'fileencoding'.
2087 */
2088 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002089free_buf_options(
2090 buf_T *buf,
2091 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092{
2093 if (free_p_ff)
2094 {
2095#ifdef FEAT_MBYTE
2096 clear_string_option(&buf->b_p_fenc);
2097#endif
2098 clear_string_option(&buf->b_p_ff);
2099#ifdef FEAT_QUICKFIX
2100 clear_string_option(&buf->b_p_bh);
2101 clear_string_option(&buf->b_p_bt);
2102#endif
2103 }
2104#ifdef FEAT_FIND_ID
2105 clear_string_option(&buf->b_p_def);
2106 clear_string_option(&buf->b_p_inc);
2107# ifdef FEAT_EVAL
2108 clear_string_option(&buf->b_p_inex);
2109# endif
2110#endif
2111#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2112 clear_string_option(&buf->b_p_inde);
2113 clear_string_option(&buf->b_p_indk);
2114#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002115#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2116 clear_string_option(&buf->b_p_bexpr);
2117#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002118#if defined(FEAT_CRYPT)
2119 clear_string_option(&buf->b_p_cm);
2120#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002121#if defined(FEAT_EVAL)
2122 clear_string_option(&buf->b_p_fex);
2123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124#ifdef FEAT_CRYPT
2125 clear_string_option(&buf->b_p_key);
2126#endif
2127 clear_string_option(&buf->b_p_kp);
2128 clear_string_option(&buf->b_p_mps);
2129 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002130 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 clear_string_option(&buf->b_p_isk);
2132#ifdef FEAT_KEYMAP
2133 clear_string_option(&buf->b_p_keymap);
2134 ga_clear(&buf->b_kmap_ga);
2135#endif
2136#ifdef FEAT_COMMENTS
2137 clear_string_option(&buf->b_p_com);
2138#endif
2139#ifdef FEAT_FOLDING
2140 clear_string_option(&buf->b_p_cms);
2141#endif
2142 clear_string_option(&buf->b_p_nf);
2143#ifdef FEAT_SYN_HL
2144 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002145 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002146#endif
2147#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002148 clear_string_option(&buf->b_s.b_p_spc);
2149 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002150 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002151 buf->b_s.b_cap_prog = NULL;
2152 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153#endif
2154#ifdef FEAT_SEARCHPATH
2155 clear_string_option(&buf->b_p_sua);
2156#endif
2157#ifdef FEAT_AUTOCMD
2158 clear_string_option(&buf->b_p_ft);
2159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160#ifdef FEAT_CINDENT
2161 clear_string_option(&buf->b_p_cink);
2162 clear_string_option(&buf->b_p_cino);
2163#endif
2164#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2165 clear_string_option(&buf->b_p_cinw);
2166#endif
2167#ifdef FEAT_INS_EXPAND
2168 clear_string_option(&buf->b_p_cpt);
2169#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002170#ifdef FEAT_COMPL_FUNC
2171 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002172 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174#ifdef FEAT_QUICKFIX
2175 clear_string_option(&buf->b_p_gp);
2176 clear_string_option(&buf->b_p_mp);
2177 clear_string_option(&buf->b_p_efm);
2178#endif
2179 clear_string_option(&buf->b_p_ep);
2180 clear_string_option(&buf->b_p_path);
2181 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002182 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183#ifdef FEAT_INS_EXPAND
2184 clear_string_option(&buf->b_p_dict);
2185 clear_string_option(&buf->b_p_tsr);
2186#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002187#ifdef FEAT_TEXTOBJ
2188 clear_string_option(&buf->b_p_qe);
2189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002191 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002192#ifdef FEAT_LISP
2193 clear_string_option(&buf->b_p_lw);
2194#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002195 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196}
2197
2198/*
2199 * get alternate file n
2200 * set linenr to lnum or altfpos.lnum if lnum == 0
2201 * also set cursor column to altfpos.col if 'startofline' is not set.
2202 * if (options & GETF_SETMARK) call setpcmark()
2203 * if (options & GETF_ALT) we are jumping to an alternate file.
2204 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2205 *
2206 * return FAIL for failure, OK for success
2207 */
2208 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209buflist_getfile(
2210 int n,
2211 linenr_T lnum,
2212 int options,
2213 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214{
2215 buf_T *buf;
2216#ifdef FEAT_WINDOWS
2217 win_T *wp = NULL;
2218#endif
2219 pos_T *fpos;
2220 colnr_T col;
2221
2222 buf = buflist_findnr(n);
2223 if (buf == NULL)
2224 {
2225 if ((options & GETF_ALT) && n == 0)
2226 EMSG(_(e_noalt));
2227 else
2228 EMSGN(_("E92: Buffer %ld not found"), n);
2229 return FAIL;
2230 }
2231
2232 /* if alternate file is the current buffer, nothing to do */
2233 if (buf == curbuf)
2234 return OK;
2235
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002236 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002237 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002238 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002240 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002241#ifdef FEAT_AUTOCMD
2242 if (curbuf_locked())
2243 return FAIL;
2244#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245
2246 /* altfpos may be changed by getfile(), get it now */
2247 if (lnum == 0)
2248 {
2249 fpos = buflist_findfpos(buf);
2250 lnum = fpos->lnum;
2251 col = fpos->col;
2252 }
2253 else
2254 col = 0;
2255
2256#ifdef FEAT_WINDOWS
2257 if (options & GETF_SWITCH)
2258 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002259 /* If 'switchbuf' contains "useopen": jump to first window containing
2260 * "buf" if one exists */
2261 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002263
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002264 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002265 * page containing "buf" if one exists */
2266 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002267 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002268
2269 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2270 * current buffer isn't empty: open new tab or window */
2271 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2272 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002274 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002275 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002276 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2277 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002279 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 }
2281 }
2282#endif
2283
2284 ++RedrawingDisabled;
2285 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2286 lnum, forceit) <= 0)
2287 {
2288 --RedrawingDisabled;
2289
2290 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2291 if (!p_sol && col != 0)
2292 {
2293 curwin->w_cursor.col = col;
2294 check_cursor_col();
2295#ifdef FEAT_VIRTUALEDIT
2296 curwin->w_cursor.coladd = 0;
2297#endif
2298 curwin->w_set_curswant = TRUE;
2299 }
2300 return OK;
2301 }
2302 --RedrawingDisabled;
2303 return FAIL;
2304}
2305
2306/*
2307 * go to the last know line number for the current buffer
2308 */
2309 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002310buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311{
2312 pos_T *fpos;
2313
2314 fpos = buflist_findfpos(curbuf);
2315
2316 curwin->w_cursor.lnum = fpos->lnum;
2317 check_cursor_lnum();
2318
2319 if (p_sol)
2320 curwin->w_cursor.col = 0;
2321 else
2322 {
2323 curwin->w_cursor.col = fpos->col;
2324 check_cursor_col();
2325#ifdef FEAT_VIRTUALEDIT
2326 curwin->w_cursor.coladd = 0;
2327#endif
2328 curwin->w_set_curswant = TRUE;
2329 }
2330}
2331
Bram Moolenaar81695252004-12-29 20:58:21 +00002332#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2333/*
2334 * Find file in buffer list by name (it has to be for the current window).
2335 * Returns NULL if not found.
2336 */
2337 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002338buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002339{
2340 char_u *ffname;
2341 buf_T *buf = NULL;
2342
2343 /* First make the name into a full path name */
2344 ffname = FullName_save(fname,
2345#ifdef UNIX
2346 TRUE /* force expansion, get rid of symbolic links */
2347#else
2348 FALSE
2349#endif
2350 );
2351 if (ffname != NULL)
2352 {
2353 buf = buflist_findname(ffname);
2354 vim_free(ffname);
2355 }
2356 return buf;
2357}
2358#endif
2359
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360/*
2361 * Find file in buffer list by name (it has to be for the current window).
2362 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002363 * Skips dummy buffers.
2364 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 */
2366 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002367buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368{
2369#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002370 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372 if (mch_stat((char *)ffname, &st) < 0)
2373 st.st_dev = (dev_T)-1;
2374 return buflist_findname_stat(ffname, &st);
2375}
2376
2377/*
2378 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2379 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002380 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 */
2382 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002383buflist_findname_stat(
2384 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002385 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
2387#endif
2388 buf_T *buf;
2389
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002390 /* Start at the last buffer, expect to find a match sooner. */
2391 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002392 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393#ifdef UNIX
2394 , stp
2395#endif
2396 ))
2397 return buf;
2398 return NULL;
2399}
2400
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002401#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2402 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403/*
2404 * Find file in buffer list by a regexp pattern.
2405 * Return fnum of the found buffer.
2406 * Return < 0 for error.
2407 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002409buflist_findpat(
2410 char_u *pattern,
2411 char_u *pattern_end, /* pointer to first char after pattern */
2412 int unlisted, /* find unlisted buffers */
2413 int diffmode UNUSED, /* find diff-mode buffers only */
2414 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415{
2416 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 int match = -1;
2418 int find_listed;
2419 char_u *pat;
2420 char_u *patend;
2421 int attempt;
2422 char_u *p;
2423 int toggledollar;
2424
2425 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2426 {
2427 if (*pattern == '%')
2428 match = curbuf->b_fnum;
2429 else
2430 match = curwin->w_alt_fnum;
2431#ifdef FEAT_DIFF
2432 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2433 match = -1;
2434#endif
2435 }
2436
2437 /*
2438 * Try four ways of matching a listed buffer:
2439 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002440 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 * attempt == 2: with '$' at end (only match at end)
2442 * attempt == 3: with '^' at start and '$' at end (only full match)
2443 * Repeat this for finding an unlisted buffer if there was no matching
2444 * listed buffer.
2445 */
2446 else
2447 {
2448 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2449 if (pat == NULL)
2450 return -1;
2451 patend = pat + STRLEN(pat) - 1;
2452 toggledollar = (patend > pat && *patend == '$');
2453
2454 /* First try finding a listed buffer. If not found and "unlisted"
2455 * is TRUE, try finding an unlisted buffer. */
2456 find_listed = TRUE;
2457 for (;;)
2458 {
2459 for (attempt = 0; attempt <= 3; ++attempt)
2460 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002461 regmatch_T regmatch;
2462
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 /* may add '^' and '$' */
2464 if (toggledollar)
2465 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2466 p = pat;
2467 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2468 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002469 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2470 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 {
2472 vim_free(pat);
2473 return -1;
2474 }
2475
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002476 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (buf->b_p_bl == find_listed
2478#ifdef FEAT_DIFF
2479 && (!diffmode || diff_mode_buf(buf))
2480#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002481 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002483 if (curtab_only)
2484 {
2485 /* Ignore the match if the buffer is not open in
2486 * the current tab. */
2487#ifdef FEAT_WINDOWS
2488 win_T *wp;
2489
Bram Moolenaar29323592016-07-24 22:04:11 +02002490 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002491 if (wp->w_buffer == buf)
2492 break;
2493 if (wp == NULL)
2494 continue;
2495#else
2496 if (curwin->w_buffer != buf)
2497 continue;
2498#endif
2499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (match >= 0) /* already found a match */
2501 {
2502 match = -2;
2503 break;
2504 }
2505 match = buf->b_fnum; /* remember first match */
2506 }
2507
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002508 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (match >= 0) /* found one match */
2510 break;
2511 }
2512
2513 /* Only search for unlisted buffers if there was no match with
2514 * a listed buffer. */
2515 if (!unlisted || !find_listed || match != -1)
2516 break;
2517 find_listed = FALSE;
2518 }
2519
2520 vim_free(pat);
2521 }
2522
2523 if (match == -2)
2524 EMSG2(_("E93: More than one match for %s"), pattern);
2525 else if (match < 0)
2526 EMSG2(_("E94: No matching buffer for %s"), pattern);
2527 return match;
2528}
2529#endif
2530
2531#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2532
2533/*
2534 * Find all buffer names that match.
2535 * For command line expansion of ":buf" and ":sbuf".
2536 * Return OK if matches found, FAIL otherwise.
2537 */
2538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002539ExpandBufnames(
2540 char_u *pat,
2541 int *num_file,
2542 char_u ***file,
2543 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544{
2545 int count = 0;
2546 buf_T *buf;
2547 int round;
2548 char_u *p;
2549 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002550 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 *num_file = 0; /* return values in case of FAIL */
2553 *file = NULL;
2554
Bram Moolenaar05159a02005-02-26 23:04:13 +00002555 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2556 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002558 patc = alloc((unsigned)STRLEN(pat) + 11);
2559 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002561 STRCPY(patc, "\\(^\\|[\\/]\\)");
2562 STRCPY(patc + 11, pat + 1);
2563 }
2564 else
2565 patc = pat;
2566
2567 /*
2568 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002569 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002570 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002571 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002572 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002573 regmatch_T regmatch;
2574
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002575 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002576 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002577 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2578 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002579 {
2580 if (patc != pat)
2581 vim_free(patc);
2582 return FAIL;
2583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
2585 /*
2586 * round == 1: Count the matches.
2587 * round == 2: Build the array to keep the matches.
2588 */
2589 for (round = 1; round <= 2; ++round)
2590 {
2591 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002592 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 if (!buf->b_p_bl) /* skip unlisted buffers */
2595 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002596 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 if (p != NULL)
2598 {
2599 if (round == 1)
2600 ++count;
2601 else
2602 {
2603 if (options & WILD_HOME_REPLACE)
2604 p = home_replace_save(buf, p);
2605 else
2606 p = vim_strsave(p);
2607 (*file)[count++] = p;
2608 }
2609 }
2610 }
2611 if (count == 0) /* no match found, break here */
2612 break;
2613 if (round == 1)
2614 {
2615 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2616 if (*file == NULL)
2617 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002618 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002619 if (patc != pat)
2620 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 return FAIL;
2622 }
2623 }
2624 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002625 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (count) /* match(es) found, break here */
2627 break;
2628 }
2629
Bram Moolenaar05159a02005-02-26 23:04:13 +00002630 if (patc != pat)
2631 vim_free(patc);
2632
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 *num_file = count;
2634 return (count == 0 ? FAIL : OK);
2635}
2636
2637#endif /* FEAT_CMDL_COMPL */
2638
2639#ifdef HAVE_BUFLIST_MATCH
2640/*
2641 * Check for a match on the file name for buffer "buf" with regprog "prog".
2642 */
2643 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002644buflist_match(
2645 regmatch_T *rmp,
2646 buf_T *buf,
2647 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648{
2649 char_u *match;
2650
2651 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002652 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002654 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655
2656 return match;
2657}
2658
2659/*
2660 * Try matching the regexp in "prog" with file name "name".
2661 * Return "name" when there is a match, NULL when not.
2662 */
2663 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002664fname_match(
2665 regmatch_T *rmp,
2666 char_u *name,
2667 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 char_u *match = NULL;
2670 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671
2672 if (name != NULL)
2673 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002674 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002675 rmp->rm_ic = p_fic || ignore_case;
2676 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 match = name;
2678 else
2679 {
2680 /* Replace $(HOME) with '~' and try matching again. */
2681 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002682 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 match = name;
2684 vim_free(p);
2685 }
2686 }
2687
2688 return match;
2689}
2690#endif
2691
2692/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002693 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 */
2695 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002696buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002697{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002698 char_u key[VIM_SIZEOF_INT * 2 + 1];
2699 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700
2701 if (nr == 0)
2702 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002703 sprintf((char *)key, "%x", nr);
2704 hi = hash_find(&buf_hashtab, key);
2705
2706 if (!HASHITEM_EMPTY(hi))
2707 return (buf_T *)(hi->hi_key
2708 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 return NULL;
2710}
2711
2712/*
2713 * Get name of file 'n' in the buffer list.
2714 * When the file has no name an empty string is returned.
2715 * home_replace() is used to shorten the file name (used for marks).
2716 * Returns a pointer to allocated memory, of NULL when failed.
2717 */
2718 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002719buflist_nr2name(
2720 int n,
2721 int fullname,
2722 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 buf_T *buf;
2725
2726 buf = buflist_findnr(n);
2727 if (buf == NULL)
2728 return NULL;
2729 return home_replace_save(helptail ? buf : NULL,
2730 fullname ? buf->b_ffname : buf->b_fname);
2731}
2732
2733/*
2734 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2735 * When "copy_options" is TRUE save the local window option values.
2736 * When "lnum" is 0 only do the options.
2737 */
2738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002739buflist_setfpos(
2740 buf_T *buf,
2741 win_T *win,
2742 linenr_T lnum,
2743 colnr_T col,
2744 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745{
2746 wininfo_T *wip;
2747
2748 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2749 if (wip->wi_win == win)
2750 break;
2751 if (wip == NULL)
2752 {
2753 /* allocate a new entry */
2754 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2755 if (wip == NULL)
2756 return;
2757 wip->wi_win = win;
2758 if (lnum == 0) /* set lnum even when it's 0 */
2759 lnum = 1;
2760 }
2761 else
2762 {
2763 /* remove the entry from the list */
2764 if (wip->wi_prev)
2765 wip->wi_prev->wi_next = wip->wi_next;
2766 else
2767 buf->b_wininfo = wip->wi_next;
2768 if (wip->wi_next)
2769 wip->wi_next->wi_prev = wip->wi_prev;
2770 if (copy_options && wip->wi_optset)
2771 {
2772 clear_winopt(&wip->wi_opt);
2773#ifdef FEAT_FOLDING
2774 deleteFoldRecurse(&wip->wi_folds);
2775#endif
2776 }
2777 }
2778 if (lnum != 0)
2779 {
2780 wip->wi_fpos.lnum = lnum;
2781 wip->wi_fpos.col = col;
2782 }
2783 if (copy_options)
2784 {
2785 /* Save the window-specific option values. */
2786 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2787#ifdef FEAT_FOLDING
2788 wip->wi_fold_manual = win->w_fold_manual;
2789 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2790#endif
2791 wip->wi_optset = TRUE;
2792 }
2793
2794 /* insert the entry in front of the list */
2795 wip->wi_next = buf->b_wininfo;
2796 buf->b_wininfo = wip;
2797 wip->wi_prev = NULL;
2798 if (wip->wi_next)
2799 wip->wi_next->wi_prev = wip;
2800
2801 return;
2802}
2803
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002804#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002805static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002806
2807/*
2808 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2809 * page. That's because a diff is local to a tab page.
2810 */
2811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002812wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002813{
2814 win_T *wp;
2815
2816 if (wip->wi_opt.wo_diff)
2817 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002818 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002819 /* return FALSE when it's a window in the current tab page, thus
2820 * the buffer was in diff mode here */
2821 if (wip->wi_win == wp)
2822 return FALSE;
2823 return TRUE;
2824 }
2825 return FALSE;
2826}
2827#endif
2828
Bram Moolenaar071d4272004-06-13 20:20:40 +00002829/*
2830 * Find info for the current window in buffer "buf".
2831 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002832 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2833 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 * Returns NULL when there isn't any info.
2835 */
2836 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002837find_wininfo(
2838 buf_T *buf,
2839 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840{
2841 wininfo_T *wip;
2842
2843 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002844 if (wip->wi_win == curwin
2845#ifdef FEAT_DIFF
2846 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2847#endif
2848 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002850
2851 /* If no wininfo for curwin, use the first in the list (that doesn't have
2852 * 'diff' set and is in another tab page). */
2853 if (wip == NULL)
2854 {
2855#ifdef FEAT_DIFF
2856 if (skip_diff_buffer)
2857 {
2858 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2859 if (!wininfo_other_tab_diff(wip))
2860 break;
2861 }
2862 else
2863#endif
2864 wip = buf->b_wininfo;
2865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866 return wip;
2867}
2868
2869/*
2870 * Reset the local window options to the values last used in this window.
2871 * If the buffer wasn't used in this window before, use the values from
2872 * the most recently used window. If the values were never set, use the
2873 * global values for the window.
2874 */
2875 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002876get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877{
2878 wininfo_T *wip;
2879
2880 clear_winopt(&curwin->w_onebuf_opt);
2881#ifdef FEAT_FOLDING
2882 clearFolding(curwin);
2883#endif
2884
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002885 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 if (wip != NULL && wip->wi_optset)
2887 {
2888 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2889#ifdef FEAT_FOLDING
2890 curwin->w_fold_manual = wip->wi_fold_manual;
2891 curwin->w_foldinvalid = TRUE;
2892 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2893#endif
2894 }
2895 else
2896 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2897
2898#ifdef FEAT_FOLDING
2899 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2900 if (p_fdls >= 0)
2901 curwin->w_p_fdl = p_fdls;
2902#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002903#ifdef FEAT_SYN_HL
2904 check_colorcolumn(curwin);
2905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906}
2907
2908/*
2909 * Find the position (lnum and col) for the buffer 'buf' for the current
2910 * window.
2911 * Returns a pointer to no_position if no position is found.
2912 */
2913 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002914buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915{
2916 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002917 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002919 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 if (wip != NULL)
2921 return &(wip->wi_fpos);
2922 else
2923 return &no_position;
2924}
2925
2926/*
2927 * Find the lnum for the buffer 'buf' for the current window.
2928 */
2929 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002930buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 return buflist_findfpos(buf)->lnum;
2933}
2934
2935#if defined(FEAT_LISTCMDS) || defined(PROTO)
2936/*
2937 * List all know file names (for :files and :buffers command).
2938 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002940buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941{
2942 buf_T *buf;
2943 int len;
2944 int i;
2945
2946 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2947 {
2948 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002949 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2950 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2951 || (vim_strchr(eap->arg, '+')
2952 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2953 || (vim_strchr(eap->arg, 'a')
2954 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2955 || (vim_strchr(eap->arg, 'h')
2956 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2957 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2958 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2959 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2960 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2961 || (vim_strchr(eap->arg, '#')
2962 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002965 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 else
2967 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002968 if (message_filtered(NameBuff))
2969 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002971 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00002972 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 buf->b_fnum,
2974 buf->b_p_bl ? ' ' : 'u',
2975 buf == curbuf ? '%' :
2976 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2977 buf->b_ml.ml_mfp == NULL ? ' ' :
2978 (buf->b_nwindows == 0 ? 'h' : 'a'),
2979 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2980 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002981 : (bufIsChanged(buf) ? '+' : ' '),
2982 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002983 if (len > IOSIZE - 20)
2984 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985
2986 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 i = 40 - vim_strsize(IObuff);
2988 do
2989 {
2990 IObuff[len++] = ' ';
2991 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002992 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2993 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002994 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 msg_outtrans(IObuff);
2996 out_flush(); /* output one line at a time */
2997 ui_breakcheck();
2998 }
2999}
3000#endif
3001
3002/*
3003 * Get file name and line number for file 'fnum'.
3004 * Used by DoOneCmd() for translating '%' and '#'.
3005 * Used by insert_reg() and cmdline_paste() for '#' register.
3006 * Return FAIL if not found, OK for success.
3007 */
3008 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003009buflist_name_nr(
3010 int fnum,
3011 char_u **fname,
3012 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013{
3014 buf_T *buf;
3015
3016 buf = buflist_findnr(fnum);
3017 if (buf == NULL || buf->b_fname == NULL)
3018 return FAIL;
3019
3020 *fname = buf->b_fname;
3021 *lnum = buflist_findlnum(buf);
3022
3023 return OK;
3024}
3025
3026/*
3027 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3028 * The file name with the full path is also remembered, for when :cd is used.
3029 * Returns FAIL for failure (file name already in use by other buffer)
3030 * OK otherwise.
3031 */
3032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003033setfname(
3034 buf_T *buf,
3035 char_u *ffname,
3036 char_u *sfname,
3037 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038{
Bram Moolenaar81695252004-12-29 20:58:21 +00003039 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003041 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042#endif
3043
3044 if (ffname == NULL || *ffname == NUL)
3045 {
3046 /* Removing the name. */
3047 vim_free(buf->b_ffname);
3048 vim_free(buf->b_sfname);
3049 buf->b_ffname = NULL;
3050 buf->b_sfname = NULL;
3051#ifdef UNIX
3052 st.st_dev = (dev_T)-1;
3053#endif
3054 }
3055 else
3056 {
3057 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3058 if (ffname == NULL) /* out of memory */
3059 return FAIL;
3060
3061 /*
3062 * if the file name is already used in another buffer:
3063 * - if the buffer is loaded, fail
3064 * - if the buffer is not loaded, delete it from the list
3065 */
3066#ifdef UNIX
3067 if (mch_stat((char *)ffname, &st) < 0)
3068 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003069#endif
3070 if (!(buf->b_flags & BF_DUMMY))
3071#ifdef UNIX
3072 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003074 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075#endif
3076 if (obuf != NULL && obuf != buf)
3077 {
3078 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3079 {
3080 if (message)
3081 EMSG(_("E95: Buffer with this name already exists"));
3082 vim_free(ffname);
3083 return FAIL;
3084 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003085 /* delete from the list */
3086 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
3088 sfname = vim_strsave(sfname);
3089 if (ffname == NULL || sfname == NULL)
3090 {
3091 vim_free(sfname);
3092 vim_free(ffname);
3093 return FAIL;
3094 }
3095#ifdef USE_FNAME_CASE
3096# ifdef USE_LONG_FNAME
3097 if (USE_LONG_FNAME)
3098# endif
3099 fname_case(sfname, 0); /* set correct case for short file name */
3100#endif
3101 vim_free(buf->b_ffname);
3102 vim_free(buf->b_sfname);
3103 buf->b_ffname = ffname;
3104 buf->b_sfname = sfname;
3105 }
3106 buf->b_fname = buf->b_sfname;
3107#ifdef UNIX
3108 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003109 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003110 else
3111 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003112 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 buf->b_dev = st.st_dev;
3114 buf->b_ino = st.st_ino;
3115 }
3116#endif
3117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003119
3120 buf_name_changed(buf);
3121 return OK;
3122}
3123
3124/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003125 * Crude way of changing the name of a buffer. Use with care!
3126 * The name should be relative to the current directory.
3127 */
3128 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003129buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003130{
3131 buf_T *buf;
3132
3133 buf = buflist_findnr(fnum);
3134 if (buf != NULL)
3135 {
3136 vim_free(buf->b_sfname);
3137 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003138 buf->b_ffname = vim_strsave(name);
3139 buf->b_sfname = NULL;
3140 /* Allocate ffname and expand into full path. Also resolves .lnk
3141 * files on Win32. */
3142 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003143 buf->b_fname = buf->b_sfname;
3144 }
3145}
3146
3147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 * Take care of what needs to be done when the name of buffer "buf" has
3149 * changed.
3150 */
3151 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003152buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153{
3154 /*
3155 * If the file name changed, also change the name of the swapfile
3156 */
3157 if (buf->b_ml.ml_mfp != NULL)
3158 ml_setname(buf);
3159
3160 if (curwin->w_buffer == buf)
3161 check_arg_idx(curwin); /* check file name for arg list */
3162#ifdef FEAT_TITLE
3163 maketitle(); /* set window title */
3164#endif
3165#ifdef FEAT_WINDOWS
3166 status_redraw_all(); /* status lines need to be redrawn */
3167#endif
3168 fmarks_check_names(buf); /* check named file marks */
3169 ml_timestamp(buf); /* reset timestamp */
3170}
3171
3172/*
3173 * set alternate file name for current window
3174 *
3175 * Used by do_one_cmd(), do_write() and do_ecmd().
3176 * Return the buffer.
3177 */
3178 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179setaltfname(
3180 char_u *ffname,
3181 char_u *sfname,
3182 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183{
3184 buf_T *buf;
3185
3186 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3187 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003188 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 curwin->w_alt_fnum = buf->b_fnum;
3190 return buf;
3191}
3192
3193/*
3194 * Get alternate file name for current window.
3195 * Return NULL if there isn't any, and give error message if requested.
3196 */
3197 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198getaltfname(
3199 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200{
3201 char_u *fname;
3202 linenr_T dummy;
3203
3204 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3205 {
3206 if (errmsg)
3207 EMSG(_(e_noalt));
3208 return NULL;
3209 }
3210 return fname;
3211}
3212
3213/*
3214 * Add a file name to the buflist and return its number.
3215 * Uses same flags as buflist_new(), except BLN_DUMMY.
3216 *
3217 * used by qf_init(), main() and doarglist()
3218 */
3219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221{
3222 buf_T *buf;
3223
3224 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3225 if (buf != NULL)
3226 return buf->b_fnum;
3227 return 0;
3228}
3229
3230#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3231/*
3232 * Adjust slashes in file names. Called after 'shellslash' was set.
3233 */
3234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003235buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236{
3237 buf_T *bp;
3238
Bram Moolenaar29323592016-07-24 22:04:11 +02003239 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 {
3241 if (bp->b_ffname != NULL)
3242 slash_adjust(bp->b_ffname);
3243 if (bp->b_sfname != NULL)
3244 slash_adjust(bp->b_sfname);
3245 }
3246}
3247#endif
3248
3249/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003250 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 * Also save the local window option values.
3252 */
3253 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003254buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003256 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257}
3258
3259/*
3260 * Return TRUE if 'ffname' is not the same file as current file.
3261 * Fname must have a full path (expanded by mch_FullName()).
3262 */
3263 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003264otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265{
3266 return otherfile_buf(curbuf, ffname
3267#ifdef UNIX
3268 , NULL
3269#endif
3270 );
3271}
3272
3273 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003274otherfile_buf(
3275 buf_T *buf,
3276 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003278 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003280 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281{
3282 /* no name is different */
3283 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3284 return TRUE;
3285 if (fnamecmp(ffname, buf->b_ffname) == 0)
3286 return FALSE;
3287#ifdef UNIX
3288 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003289 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
Bram Moolenaar8767f522016-07-01 17:17:39 +02003291 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (stp == NULL)
3293 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003294 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 st.st_dev = (dev_T)-1;
3296 stp = &st;
3297 }
3298 /* Use dev/ino to check if the files are the same, even when the names
3299 * are different (possible with links). Still need to compare the
3300 * name above, for when the file doesn't exist yet.
3301 * Problem: The dev/ino changes when a file is deleted (and created
3302 * again) and remains the same when renamed/moved. We don't want to
3303 * mch_stat() each buffer each time, that would be too slow. Get the
3304 * dev/ino again when they appear to match, but not when they appear
3305 * to be different: Could skip a buffer when it's actually the same
3306 * file. */
3307 if (buf_same_ino(buf, stp))
3308 {
3309 buf_setino(buf);
3310 if (buf_same_ino(buf, stp))
3311 return FALSE;
3312 }
3313 }
3314#endif
3315 return TRUE;
3316}
3317
3318#if defined(UNIX) || defined(PROTO)
3319/*
3320 * Set inode and device number for a buffer.
3321 * Must always be called when b_fname is changed!.
3322 */
3323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003324buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003326 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
3328 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3329 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003330 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 buf->b_dev = st.st_dev;
3332 buf->b_ino = st.st_ino;
3333 }
3334 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003335 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336}
3337
3338/*
3339 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3340 */
3341 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342buf_same_ino(
3343 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003344 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003346 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 && stp->st_dev == buf->b_dev
3348 && stp->st_ino == buf->b_ino);
3349}
3350#endif
3351
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003352/*
3353 * Print info about the current buffer.
3354 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003356fileinfo(
3357 int fullname, /* when non-zero print full path */
3358 int shorthelp,
3359 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
3361 char_u *name;
3362 int n;
3363 char_u *p;
3364 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003365 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
3367 buffer = alloc(IOSIZE);
3368 if (buffer == NULL)
3369 return;
3370
3371 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3372 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003373 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 p = buffer + STRLEN(buffer);
3375 }
3376 else
3377 p = buffer;
3378
3379 *p++ = '"';
3380 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003381 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 else
3383 {
3384 if (!fullname && curbuf->b_fname != NULL)
3385 name = curbuf->b_fname;
3386 else
3387 name = curbuf->b_ffname;
3388 home_replace(shorthelp ? curbuf : NULL, name, p,
3389 (int)(IOSIZE - (p - buffer)), TRUE);
3390 }
3391
Bram Moolenaara800b422010-06-27 01:15:55 +02003392 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 curbufIsChanged() ? (shortmess(SHM_MOD)
3394 ? " [+]" : _(" [Modified]")) : " ",
3395 (curbuf->b_flags & BF_NOTEDITED)
3396#ifdef FEAT_QUICKFIX
3397 && !bt_dontwrite(curbuf)
3398#endif
3399 ? _("[Not edited]") : "",
3400 (curbuf->b_flags & BF_NEW)
3401#ifdef FEAT_QUICKFIX
3402 && !bt_dontwrite(curbuf)
3403#endif
3404 ? _("[New file]") : "",
3405 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003406 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 : _("[readonly]")) : "",
3408 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3409 || curbuf->b_p_ro) ?
3410 " " : "");
3411 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3412 * causes an overflow, thus for large numbers divide instead. */
3413 if (curwin->w_cursor.lnum > 1000000L)
3414 n = (int)(((long)curwin->w_cursor.lnum) /
3415 ((long)curbuf->b_ml.ml_line_count / 100L));
3416 else
3417 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3418 (long)curbuf->b_ml.ml_line_count);
3419 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3420 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003421 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 }
3423#ifdef FEAT_CMDL_INFO
3424 else if (p_ru)
3425 {
3426 /* Current line and column are already on the screen -- webb */
3427 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003428 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003430 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 (long)curbuf->b_ml.ml_line_count, n);
3432 }
3433#endif
3434 else
3435 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003436 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 _("line %ld of %ld --%d%%-- col "),
3438 (long)curwin->w_cursor.lnum,
3439 (long)curbuf->b_ml.ml_line_count,
3440 n);
3441 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003442 len = STRLEN(buffer);
3443 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3445 }
3446
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003447 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 if (dont_truncate)
3450 {
3451 /* Temporarily set msg_scroll to avoid the message being truncated.
3452 * First call msg_start() to get the message in the right place. */
3453 msg_start();
3454 n = msg_scroll;
3455 msg_scroll = TRUE;
3456 msg(buffer);
3457 msg_scroll = n;
3458 }
3459 else
3460 {
3461 p = msg_trunc_attr(buffer, FALSE, 0);
3462 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 /* Need to repeat the message after redrawing when:
3464 * - When restart_edit is set (otherwise there will be a delay
3465 * before redrawing).
3466 * - When the screen was scrolled but there is no wait-return
3467 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003468 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469 }
3470
3471 vim_free(buffer);
3472}
3473
3474 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003475col_print(
3476 char_u *buf,
3477 size_t buflen,
3478 int col,
3479 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480{
3481 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003482 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003484 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485}
3486
3487#if defined(FEAT_TITLE) || defined(PROTO)
3488/*
3489 * put file name in title bar of window and in icon title
3490 */
3491
3492static char_u *lasttitle = NULL;
3493static char_u *lasticon = NULL;
3494
3495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003496maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497{
3498 char_u *p;
3499 char_u *t_str = NULL;
3500 char_u *i_name;
3501 char_u *i_str = NULL;
3502 int maxlen = 0;
3503 int len;
3504 int mustset;
3505 char_u buf[IOSIZE];
3506 int off;
3507
3508 if (!redrawing())
3509 {
3510 /* Postpone updating the title when 'lazyredraw' is set. */
3511 need_maketitle = TRUE;
3512 return;
3513 }
3514
3515 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003516 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 return;
3518
3519 if (p_title)
3520 {
3521 if (p_titlelen > 0)
3522 {
3523 maxlen = p_titlelen * Columns / 100;
3524 if (maxlen < 10)
3525 maxlen = 10;
3526 }
3527
3528 t_str = buf;
3529 if (*p_titlestring != NUL)
3530 {
3531#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003532 if (stl_syntax & STL_IN_TITLE)
3533 {
3534 int use_sandbox = FALSE;
3535 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003536
3537# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003538 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003539# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003540 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003542 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003543 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003544 if (called_emsg)
3545 set_string_option_direct((char_u *)"titlestring", -1,
3546 (char_u *)"", OPT_FREE, SID_ERROR);
3547 called_emsg |= save_called_emsg;
3548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 else
3550#endif
3551 t_str = p_titlestring;
3552 }
3553 else
3554 {
3555 /* format: "fname + (path) (1 of 2) - VIM" */
3556
Bram Moolenaar2c666692012-09-05 13:30:40 +02003557#define SPACE_FOR_FNAME (IOSIZE - 100)
3558#define SPACE_FOR_DIR (IOSIZE - 20)
3559#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003561 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
3563 {
3564 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003565 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 }
3568
3569 switch (bufIsChanged(curbuf)
3570 + (curbuf->b_p_ro * 2)
3571 + (!curbuf->b_p_ma * 4))
3572 {
3573 case 1: STRCAT(buf, " +"); break;
3574 case 2: STRCAT(buf, " ="); break;
3575 case 3: STRCAT(buf, " =+"); break;
3576 case 4:
3577 case 6: STRCAT(buf, " -"); break;
3578 case 5:
3579 case 7: STRCAT(buf, " -+"); break;
3580 }
3581
3582 if (curbuf->b_fname != NULL)
3583 {
3584 /* Get path of file, replace home dir with ~ */
3585 off = (int)STRLEN(buf);
3586 buf[off++] = ' ';
3587 buf[off++] = '(';
3588 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003589 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590#ifdef BACKSLASH_IN_FILENAME
3591 /* avoid "c:/name" to be reduced to "c" */
3592 if (isalpha(buf[off]) && buf[off + 1] == ':')
3593 off += 2;
3594#endif
3595 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003596 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003599 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003600 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003603
Bram Moolenaar2c666692012-09-05 13:30:40 +02003604 /* Translate unprintable chars and concatenate. Keep some
3605 * room for the server name. When there is no room (very long
3606 * file name) use (...). */
3607 if (off < SPACE_FOR_DIR)
3608 {
3609 p = transstr(buf + off);
3610 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3611 vim_free(p);
3612 }
3613 else
3614 {
3615 vim_strncpy(buf + off, (char_u *)"...",
3616 (size_t)(SPACE_FOR_ARGNR - off));
3617 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 STRCAT(buf, ")");
3619 }
3620
Bram Moolenaar2c666692012-09-05 13:30:40 +02003621 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622
3623#if defined(FEAT_CLIENTSERVER)
3624 if (serverName != NULL)
3625 {
3626 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003627 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 else
3630#endif
3631 STRCAT(buf, " - VIM");
3632
3633 if (maxlen > 0)
3634 {
3635 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003636 if (vim_strsize(buf) > maxlen)
3637 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 }
3639 }
3640 }
3641 mustset = ti_change(t_str, &lasttitle);
3642
3643 if (p_icon)
3644 {
3645 i_str = buf;
3646 if (*p_iconstring != NUL)
3647 {
3648#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003649 if (stl_syntax & STL_IN_ICON)
3650 {
3651 int use_sandbox = FALSE;
3652 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003653
3654# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003655 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003656# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003657 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003659 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003660 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003661 if (called_emsg)
3662 set_string_option_direct((char_u *)"iconstring", -1,
3663 (char_u *)"", OPT_FREE, SID_ERROR);
3664 called_emsg |= save_called_emsg;
3665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 else
3667#endif
3668 i_str = p_iconstring;
3669 }
3670 else
3671 {
3672 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003673 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 else /* use file name only in icon */
3675 i_name = gettail(curbuf->b_ffname);
3676 *i_str = NUL;
3677 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003678 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 if (len > 100)
3680 {
3681 len -= 100;
3682#ifdef FEAT_MBYTE
3683 if (has_mbyte)
3684 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3685#endif
3686 i_name += len;
3687 }
3688 STRCPY(i_str, i_name);
3689 trans_characters(i_str, IOSIZE);
3690 }
3691 }
3692
3693 mustset |= ti_change(i_str, &lasticon);
3694
3695 if (mustset)
3696 resettitle();
3697}
3698
3699/*
3700 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3701 * from "str" if it does.
3702 * Return TRUE when "*last" changed.
3703 */
3704 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003705ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
3707 if ((str == NULL) != (*last == NULL)
3708 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3709 {
3710 vim_free(*last);
3711 if (str == NULL)
3712 *last = NULL;
3713 else
3714 *last = vim_strsave(str);
3715 return TRUE;
3716 }
3717 return FALSE;
3718}
3719
3720/*
3721 * Put current window title back (used after calling a shell)
3722 */
3723 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003724resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725{
3726 mch_settitle(lasttitle, lasticon);
3727}
Bram Moolenaarea408852005-06-25 22:49:46 +00003728
3729# if defined(EXITFREE) || defined(PROTO)
3730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003731free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003732{
3733 vim_free(lasttitle);
3734 vim_free(lasticon);
3735}
3736# endif
3737
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738#endif /* FEAT_TITLE */
3739
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003740#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003742 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 * Return length of string in screen cells.
3744 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003745 * Normally works for window "wp", except when working for 'tabline' then it
3746 * is "curwin".
3747 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 * Items are drawn interspersed with the text that surrounds it
3749 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3750 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3751 *
3752 * If maxwidth is not zero, the string will be filled at any middle marker
3753 * or truncated if too long, fillchar is used for all whitespace.
3754 */
3755 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003756build_stl_str_hl(
3757 win_T *wp,
3758 char_u *out, /* buffer to write into != NameBuff */
3759 size_t outlen, /* length of out[] */
3760 char_u *fmt,
3761 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3762 int fillchar,
3763 int maxwidth,
3764 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3765 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766{
3767 char_u *p;
3768 char_u *s;
3769 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003770 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771#ifdef FEAT_EVAL
3772 win_T *o_curwin;
3773 buf_T *o_curbuf;
3774#endif
3775 int empty_line;
3776 colnr_T virtcol;
3777 long l;
3778 long n;
3779 int prevchar_isflag;
3780 int prevchar_isitem;
3781 int itemisflag;
3782 int fillable;
3783 char_u *str;
3784 long num;
3785 int width;
3786 int itemcnt;
3787 int curitem;
3788 int groupitem[STL_MAX_ITEM];
3789 int groupdepth;
3790 struct stl_item
3791 {
3792 char_u *start;
3793 int minwid;
3794 int maxwid;
3795 enum
3796 {
3797 Normal,
3798 Empty,
3799 Group,
3800 Middle,
3801 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003802 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 Trunc
3804 } type;
3805 } item[STL_MAX_ITEM];
3806 int minwid;
3807 int maxwid;
3808 int zeropad;
3809 char_u base;
3810 char_u opt;
3811#define TMPLEN 70
3812 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003813 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003814 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003815
3816#ifdef FEAT_EVAL
3817 /*
3818 * When the format starts with "%!" then evaluate it as an expression and
3819 * use the result as the actual format string.
3820 */
3821 if (fmt[0] == '%' && fmt[1] == '!')
3822 {
3823 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3824 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003825 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003826 }
3827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828
3829 if (fillchar == 0)
3830 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003831#ifdef FEAT_MBYTE
3832 /* Can't handle a multi-byte fill character yet. */
3833 else if (mb_char2len(fillchar) > 1)
3834 fillchar = '-';
3835#endif
3836
Bram Moolenaar567199b2013-04-24 16:52:36 +02003837 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3838 * p will become invalid when getting another buffer line. */
3839 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3840 empty_line = (*p == NUL);
3841
3842 /* Get the byte value now, in case we need it below. This is more
3843 * efficient than making a copy of the line. */
3844 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3845 byteval = 0;
3846 else
3847#ifdef FEAT_MBYTE
3848 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3849#else
3850 byteval = p[wp->w_cursor.col];
3851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852
3853 groupdepth = 0;
3854 p = out;
3855 curitem = 0;
3856 prevchar_isflag = TRUE;
3857 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003858 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003860 if (curitem == STL_MAX_ITEM)
3861 {
3862 /* There are too many items. Add the error code to the statusline
3863 * to give the user a hint about what went wrong. */
3864 if (p + 6 < out + outlen)
3865 {
3866 mch_memmove(p, " E541", (size_t)5);
3867 p += 5;
3868 }
3869 break;
3870 }
3871
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 if (*s != NUL && *s != '%')
3873 prevchar_isflag = prevchar_isitem = FALSE;
3874
3875 /*
3876 * Handle up to the next '%' or the end.
3877 */
3878 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3879 *p++ = *s++;
3880 if (*s == NUL || p + 1 >= out + outlen)
3881 break;
3882
3883 /*
3884 * Handle one '%' item.
3885 */
3886 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003887 if (*s == NUL) /* ignore trailing % */
3888 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 if (*s == '%')
3890 {
3891 if (p + 1 >= out + outlen)
3892 break;
3893 *p++ = *s++;
3894 prevchar_isflag = prevchar_isitem = FALSE;
3895 continue;
3896 }
3897 if (*s == STL_MIDDLEMARK)
3898 {
3899 s++;
3900 if (groupdepth > 0)
3901 continue;
3902 item[curitem].type = Middle;
3903 item[curitem++].start = p;
3904 continue;
3905 }
3906 if (*s == STL_TRUNCMARK)
3907 {
3908 s++;
3909 item[curitem].type = Trunc;
3910 item[curitem++].start = p;
3911 continue;
3912 }
3913 if (*s == ')')
3914 {
3915 s++;
3916 if (groupdepth < 1)
3917 continue;
3918 groupdepth--;
3919
3920 t = item[groupitem[groupdepth]].start;
3921 *p = NUL;
3922 l = vim_strsize(t);
3923 if (curitem > groupitem[groupdepth] + 1
3924 && item[groupitem[groupdepth]].minwid == 0)
3925 {
3926 /* remove group if all items are empty */
3927 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003928 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 break;
3930 if (n == curitem)
3931 {
3932 p = t;
3933 l = 0;
3934 }
3935 }
3936 if (l > item[groupitem[groupdepth]].maxwid)
3937 {
3938 /* truncate, remove n bytes of text at the start */
3939#ifdef FEAT_MBYTE
3940 if (has_mbyte)
3941 {
3942 /* Find the first character that should be included. */
3943 n = 0;
3944 while (l >= item[groupitem[groupdepth]].maxwid)
3945 {
3946 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003947 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949 }
3950 else
3951#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003952 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953
3954 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003955 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 p = p - n + 1;
3957#ifdef FEAT_MBYTE
3958 /* Fill up space left over by half a double-wide char. */
3959 while (++l < item[groupitem[groupdepth]].minwid)
3960 *p++ = fillchar;
3961#endif
3962
3963 /* correct the start of the items for the truncation */
3964 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3965 {
3966 item[l].start -= n;
3967 if (item[l].start < t)
3968 item[l].start = t;
3969 }
3970 }
3971 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3972 {
3973 /* fill */
3974 n = item[groupitem[groupdepth]].minwid;
3975 if (n < 0)
3976 {
3977 /* fill by appending characters */
3978 n = 0 - n;
3979 while (l++ < n && p + 1 < out + outlen)
3980 *p++ = fillchar;
3981 }
3982 else
3983 {
3984 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003985 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 l = n - l;
3987 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003988 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 p += l;
3990 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3991 item[n].start += l;
3992 for ( ; l > 0; l--)
3993 *t++ = fillchar;
3994 }
3995 }
3996 continue;
3997 }
3998 minwid = 0;
3999 maxwid = 9999;
4000 zeropad = FALSE;
4001 l = 1;
4002 if (*s == '0')
4003 {
4004 s++;
4005 zeropad = TRUE;
4006 }
4007 if (*s == '-')
4008 {
4009 s++;
4010 l = -1;
4011 }
4012 if (VIM_ISDIGIT(*s))
4013 {
4014 minwid = (int)getdigits(&s);
4015 if (minwid < 0) /* overflow */
4016 minwid = 0;
4017 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004018 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 {
4020 item[curitem].type = Highlight;
4021 item[curitem].start = p;
4022 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4023 s++;
4024 curitem++;
4025 continue;
4026 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004027 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4028 {
4029 if (*s == STL_TABCLOSENR)
4030 {
4031 if (minwid == 0)
4032 {
4033 /* %X ends the close label, go back to the previously
4034 * define tab label nr. */
4035 for (n = curitem - 1; n >= 0; --n)
4036 if (item[n].type == TabPage && item[n].minwid >= 0)
4037 {
4038 minwid = item[n].minwid;
4039 break;
4040 }
4041 }
4042 else
4043 /* close nrs are stored as negative values */
4044 minwid = - minwid;
4045 }
4046 item[curitem].type = TabPage;
4047 item[curitem].start = p;
4048 item[curitem].minwid = minwid;
4049 s++;
4050 curitem++;
4051 continue;
4052 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 if (*s == '.')
4054 {
4055 s++;
4056 if (VIM_ISDIGIT(*s))
4057 {
4058 maxwid = (int)getdigits(&s);
4059 if (maxwid <= 0) /* overflow */
4060 maxwid = 50;
4061 }
4062 }
4063 minwid = (minwid > 50 ? 50 : minwid) * l;
4064 if (*s == '(')
4065 {
4066 groupitem[groupdepth++] = curitem;
4067 item[curitem].type = Group;
4068 item[curitem].start = p;
4069 item[curitem].minwid = minwid;
4070 item[curitem].maxwid = maxwid;
4071 s++;
4072 curitem++;
4073 continue;
4074 }
4075 if (vim_strchr(STL_ALL, *s) == NULL)
4076 {
4077 s++;
4078 continue;
4079 }
4080 opt = *s++;
4081
4082 /* OK - now for the real work */
4083 base = 'D';
4084 itemisflag = FALSE;
4085 fillable = TRUE;
4086 num = -1;
4087 str = NULL;
4088 switch (opt)
4089 {
4090 case STL_FILEPATH:
4091 case STL_FULLPATH:
4092 case STL_FILENAME:
4093 fillable = FALSE; /* don't change ' ' to fillchar */
4094 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004095 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 else
4097 {
4098 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004099 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4101 }
4102 trans_characters(NameBuff, MAXPATHL);
4103 if (opt != STL_FILENAME)
4104 str = NameBuff;
4105 else
4106 str = gettail(NameBuff);
4107 break;
4108
4109 case STL_VIM_EXPR: /* '{' */
4110 itemisflag = TRUE;
4111 t = p;
4112 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4113 *p++ = *s++;
4114 if (*s != '}') /* missing '}' or out of space */
4115 break;
4116 s++;
4117 *p = 0;
4118 p = t;
4119
4120#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004121 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4123
4124 o_curbuf = curbuf;
4125 o_curwin = curwin;
4126 curwin = wp;
4127 curbuf = wp->w_buffer;
4128
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004129 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130
4131 curwin = o_curwin;
4132 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004133 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134
4135 if (str != NULL && *str != 0)
4136 {
4137 if (*skipdigits(str) == NUL)
4138 {
4139 num = atoi((char *)str);
4140 vim_free(str);
4141 str = NULL;
4142 itemisflag = FALSE;
4143 }
4144 }
4145#endif
4146 break;
4147
4148 case STL_LINE:
4149 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4150 ? 0L : (long)(wp->w_cursor.lnum);
4151 break;
4152
4153 case STL_NUMLINES:
4154 num = wp->w_buffer->b_ml.ml_line_count;
4155 break;
4156
4157 case STL_COLUMN:
4158 num = !(State & INSERT) && empty_line
4159 ? 0 : (int)wp->w_cursor.col + 1;
4160 break;
4161
4162 case STL_VIRTCOL:
4163 case STL_VIRTCOL_ALT:
4164 /* In list mode virtcol needs to be recomputed */
4165 virtcol = wp->w_virtcol;
4166 if (wp->w_p_list && lcs_tab1 == NUL)
4167 {
4168 wp->w_p_list = FALSE;
4169 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4170 wp->w_p_list = TRUE;
4171 }
4172 ++virtcol;
4173 /* Don't display %V if it's the same as %c. */
4174 if (opt == STL_VIRTCOL_ALT
4175 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4176 ? 0 : (int)wp->w_cursor.col + 1)))
4177 break;
4178 num = (long)virtcol;
4179 break;
4180
4181 case STL_PERCENTAGE:
4182 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4183 (long)wp->w_buffer->b_ml.ml_line_count);
4184 break;
4185
4186 case STL_ALTPERCENT:
4187 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004188 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 break;
4190
4191 case STL_ARGLISTSTAT:
4192 fillable = FALSE;
4193 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004194 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 str = tmp;
4196 break;
4197
4198 case STL_KEYMAP:
4199 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004200 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 str = tmp;
4202 break;
4203 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004204#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004205 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206#else
4207 num = 0;
4208#endif
4209 break;
4210
4211 case STL_BUFNO:
4212 num = wp->w_buffer->b_fnum;
4213 break;
4214
4215 case STL_OFFSET_X:
4216 base = 'X';
4217 case STL_OFFSET:
4218#ifdef FEAT_BYTEOFF
4219 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4220 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4221 0L : l + 1 + (!(State & INSERT) && empty_line ?
4222 0 : (int)wp->w_cursor.col);
4223#endif
4224 break;
4225
4226 case STL_BYTEVAL_X:
4227 base = 'X';
4228 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004229 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 if (num == NL)
4231 num = 0;
4232 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4233 num = NL;
4234 break;
4235
4236 case STL_ROFLAG:
4237 case STL_ROFLAG_ALT:
4238 itemisflag = TRUE;
4239 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004240 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 break;
4242
4243 case STL_HELPFLAG:
4244 case STL_HELPFLAG_ALT:
4245 itemisflag = TRUE;
4246 if (wp->w_buffer->b_help)
4247 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004248 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 break;
4250
4251#ifdef FEAT_AUTOCMD
4252 case STL_FILETYPE:
4253 if (*wp->w_buffer->b_p_ft != NUL
4254 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4255 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004256 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4257 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 str = tmp;
4259 }
4260 break;
4261
4262 case STL_FILETYPE_ALT:
4263 itemisflag = TRUE;
4264 if (*wp->w_buffer->b_p_ft != NUL
4265 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4266 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004267 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4268 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 for (t = tmp; *t != 0; t++)
4270 *t = TOUPPER_LOC(*t);
4271 str = tmp;
4272 }
4273 break;
4274#endif
4275
4276#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4277 case STL_PREVIEWFLAG:
4278 case STL_PREVIEWFLAG_ALT:
4279 itemisflag = TRUE;
4280 if (wp->w_p_pvw)
4281 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4282 : _("[Preview]"));
4283 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004284
4285 case STL_QUICKFIX:
4286 if (bt_quickfix(wp->w_buffer))
4287 str = (char_u *)(wp->w_llist_ref
4288 ? _(msg_loclist)
4289 : _(msg_qflist));
4290 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291#endif
4292
4293 case STL_MODIFIED:
4294 case STL_MODIFIED_ALT:
4295 itemisflag = TRUE;
4296 switch ((opt == STL_MODIFIED_ALT)
4297 + bufIsChanged(wp->w_buffer) * 2
4298 + (!wp->w_buffer->b_p_ma) * 4)
4299 {
4300 case 2: str = (char_u *)"[+]"; break;
4301 case 3: str = (char_u *)",+"; break;
4302 case 4: str = (char_u *)"[-]"; break;
4303 case 5: str = (char_u *)",-"; break;
4304 case 6: str = (char_u *)"[+-]"; break;
4305 case 7: str = (char_u *)",+-"; break;
4306 }
4307 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004308
4309 case STL_HIGHLIGHT:
4310 t = s;
4311 while (*s != '#' && *s != NUL)
4312 ++s;
4313 if (*s == '#')
4314 {
4315 item[curitem].type = Highlight;
4316 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004317 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004318 curitem++;
4319 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004320 if (*s != NUL)
4321 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004322 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324
4325 item[curitem].start = p;
4326 item[curitem].type = Normal;
4327 if (str != NULL && *str)
4328 {
4329 t = str;
4330 if (itemisflag)
4331 {
4332 if ((t[0] && t[1])
4333 && ((!prevchar_isitem && *t == ',')
4334 || (prevchar_isflag && *t == ' ')))
4335 t++;
4336 prevchar_isflag = TRUE;
4337 }
4338 l = vim_strsize(t);
4339 if (l > 0)
4340 prevchar_isitem = TRUE;
4341 if (l > maxwid)
4342 {
4343 while (l >= maxwid)
4344#ifdef FEAT_MBYTE
4345 if (has_mbyte)
4346 {
4347 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004348 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 else
4351#endif
4352 l -= byte2cells(*t++);
4353 if (p + 1 >= out + outlen)
4354 break;
4355 *p++ = '<';
4356 }
4357 if (minwid > 0)
4358 {
4359 for (; l < minwid && p + 1 < out + outlen; l++)
4360 {
4361 /* Don't put a "-" in front of a digit. */
4362 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4363 *p++ = ' ';
4364 else
4365 *p++ = fillchar;
4366 }
4367 minwid = 0;
4368 }
4369 else
4370 minwid *= -1;
4371 while (*t && p + 1 < out + outlen)
4372 {
4373 *p++ = *t++;
4374 /* Change a space by fillchar, unless fillchar is '-' and a
4375 * digit follows. */
4376 if (fillable && p[-1] == ' '
4377 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4378 p[-1] = fillchar;
4379 }
4380 for (; l < minwid && p + 1 < out + outlen; l++)
4381 *p++ = fillchar;
4382 }
4383 else if (num >= 0)
4384 {
4385 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4386 char_u nstr[20];
4387
4388 if (p + 20 >= out + outlen)
4389 break; /* not sufficient space */
4390 prevchar_isitem = TRUE;
4391 t = nstr;
4392 if (opt == STL_VIRTCOL_ALT)
4393 {
4394 *t++ = '-';
4395 minwid--;
4396 }
4397 *t++ = '%';
4398 if (zeropad)
4399 *t++ = '0';
4400 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004401 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 *t = 0;
4403
4404 for (n = num, l = 1; n >= nbase; n /= nbase)
4405 l++;
4406 if (opt == STL_VIRTCOL_ALT)
4407 l++;
4408 if (l > maxwid)
4409 {
4410 l += 2;
4411 n = l - maxwid;
4412 while (l-- > maxwid)
4413 num /= nbase;
4414 *t++ = '>';
4415 *t++ = '%';
4416 *t = t[-3];
4417 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004418 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4419 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 }
4421 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004422 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4423 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 p += STRLEN(p);
4425 }
4426 else
4427 item[curitem].type = Empty;
4428
4429 if (opt == STL_VIM_EXPR)
4430 vim_free(str);
4431
4432 if (num >= 0 || (!itemisflag && str && *str))
4433 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4434 curitem++;
4435 }
4436 *p = NUL;
4437 itemcnt = curitem;
4438
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004439#ifdef FEAT_EVAL
4440 if (usefmt != fmt)
4441 vim_free(usefmt);
4442#endif
4443
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 width = vim_strsize(out);
4445 if (maxwidth > 0 && width > maxwidth)
4446 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004447 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 l = 0;
4449 if (itemcnt == 0)
4450 s = out;
4451 else
4452 {
4453 for ( ; l < itemcnt; l++)
4454 if (item[l].type == Trunc)
4455 {
4456 /* Truncate at %< item. */
4457 s = item[l].start;
4458 break;
4459 }
4460 if (l == itemcnt)
4461 {
4462 /* No %< item, truncate first item. */
4463 s = item[0].start;
4464 l = 0;
4465 }
4466 }
4467
4468 if (width - vim_strsize(s) >= maxwidth)
4469 {
4470 /* Truncation mark is beyond max length */
4471#ifdef FEAT_MBYTE
4472 if (has_mbyte)
4473 {
4474 s = out;
4475 width = 0;
4476 for (;;)
4477 {
4478 width += ptr2cells(s);
4479 if (width >= maxwidth)
4480 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004481 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 /* Fill up for half a double-wide character. */
4484 while (++width < maxwidth)
4485 *s++ = fillchar;
4486 }
4487 else
4488#endif
4489 s = out + maxwidth - 1;
4490 for (l = 0; l < itemcnt; l++)
4491 if (item[l].start > s)
4492 break;
4493 itemcnt = l;
4494 *s++ = '>';
4495 *s = 0;
4496 }
4497 else
4498 {
4499#ifdef FEAT_MBYTE
4500 if (has_mbyte)
4501 {
4502 n = 0;
4503 while (width >= maxwidth)
4504 {
4505 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004506 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 }
4508 }
4509 else
4510#endif
4511 n = width - maxwidth + 1;
4512 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004513 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 *s = '<';
4515
4516 /* Fill up for half a double-wide character. */
4517 while (++width < maxwidth)
4518 {
4519 s = s + STRLEN(s);
4520 *s++ = fillchar;
4521 *s = NUL;
4522 }
4523
4524 --n; /* count the '<' */
4525 for (; l < itemcnt; l++)
4526 {
4527 if (item[l].start - n >= s)
4528 item[l].start -= n;
4529 else
4530 item[l].start = s;
4531 }
4532 }
4533 width = maxwidth;
4534 }
4535 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4536 {
4537 /* Apply STL_MIDDLE if any */
4538 for (l = 0; l < itemcnt; l++)
4539 if (item[l].type == Middle)
4540 break;
4541 if (l < itemcnt)
4542 {
4543 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004544 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 for (s = item[l].start; s < p; s++)
4546 *s = fillchar;
4547 for (l++; l < itemcnt; l++)
4548 item[l].start += maxwidth - width;
4549 width = maxwidth;
4550 }
4551 }
4552
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004553 /* Store the info about highlighting. */
4554 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004556 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 for (l = 0; l < itemcnt; l++)
4558 {
4559 if (item[l].type == Highlight)
4560 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004561 sp->start = item[l].start;
4562 sp->userhl = item[l].minwid;
4563 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
4565 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004566 sp->start = NULL;
4567 sp->userhl = 0;
4568 }
4569
4570 /* Store the info about tab pages labels. */
4571 if (tabtab != NULL)
4572 {
4573 sp = tabtab;
4574 for (l = 0; l < itemcnt; l++)
4575 {
4576 if (item[l].type == TabPage)
4577 {
4578 sp->start = item[l].start;
4579 sp->userhl = item[l].minwid;
4580 sp++;
4581 }
4582 }
4583 sp->start = NULL;
4584 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586
4587 return width;
4588}
4589#endif /* FEAT_STL_OPT */
4590
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004591#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4592 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004594 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4595 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 */
4597 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004598get_rel_pos(
4599 win_T *wp,
4600 char_u *buf,
4601 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602{
4603 long above; /* number of lines above window */
4604 long below; /* number of lines below window */
4605
Bram Moolenaar0027c212015-01-07 13:31:52 +01004606 if (buflen < 3) /* need at least 3 chars for writing */
4607 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 above = wp->w_topline - 1;
4609#ifdef FEAT_DIFF
4610 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004611 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4612 above = 0; /* All buffer lines are displayed and there is an
4613 * indication of filler lines, that can be considered
4614 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615#endif
4616 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4617 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004618 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4619 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004621 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004623 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 ? (int)(above / ((above + below) / 100L))
4625 : (int)(above * 100L / (above + below)));
4626}
4627#endif
4628
4629/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004630 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 * Return TRUE if it was appended.
4632 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004633 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004634append_arg_number(
4635 win_T *wp,
4636 char_u *buf,
4637 int buflen,
4638 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639{
4640 char_u *p;
4641
4642 if (ARGCOUNT <= 1) /* nothing to do */
4643 return FALSE;
4644
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004645 p = buf + STRLEN(buf); /* go to the end of the buffer */
4646 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 return FALSE;
4648 *p++ = ' ';
4649 *p++ = '(';
4650 if (add_file)
4651 {
4652 STRCPY(p, "file ");
4653 p += 5;
4654 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004655 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4656 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4658 return TRUE;
4659}
4660
4661/*
4662 * If fname is not a full path, make it a full path.
4663 * Returns pointer to allocated memory (NULL for failure).
4664 */
4665 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004666fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667{
4668 /*
4669 * Force expanding the path always for Unix, because symbolic links may
4670 * mess up the full path name, even though it starts with a '/'.
4671 * Also expand when there is ".." in the file name, try to remove it,
4672 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004673 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 * For MS-Windows also expand names like "longna~1" to "longname".
4675 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004676#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 return FullName_save(fname, TRUE);
4678#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004679 if (!vim_isAbsName(fname)
4680 || strstr((char *)fname, "..") != NULL
4681 || strstr((char *)fname, "//") != NULL
4682# ifdef BACKSLASH_IN_FILENAME
4683 || strstr((char *)fname, "\\\\") != NULL
4684# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004685# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004687# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 )
4689 return FullName_save(fname, FALSE);
4690
4691 fname = vim_strsave(fname);
4692
Bram Moolenaar9b942202007-10-03 12:31:33 +00004693# ifdef USE_FNAME_CASE
4694# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004696# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698 if (fname != NULL)
4699 fname_case(fname, 0); /* set correct case for file name */
4700 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004701# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702
4703 return fname;
4704#endif
4705}
4706
4707/*
4708 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4709 * "ffname" becomes a pointer to allocated memory (or NULL).
4710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004712fname_expand(
4713 buf_T *buf UNUSED,
4714 char_u **ffname,
4715 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
4717 if (*ffname == NULL) /* if no file name given, nothing to do */
4718 return;
4719 if (*sfname == NULL) /* if no short file name given, use ffname */
4720 *sfname = *ffname;
4721 *ffname = fix_fname(*ffname); /* expand to full path */
4722
4723#ifdef FEAT_SHORTCUT
4724 if (!buf->b_p_bin)
4725 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004726 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727
4728 /* If the file name is a shortcut file, use the file it links to. */
4729 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004730 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
4732 vim_free(*ffname);
4733 *ffname = rfname;
4734 *sfname = rfname;
4735 }
4736 }
4737#endif
4738}
4739
4740/*
4741 * Get the file name for an argument list entry.
4742 */
4743 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004744alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745{
4746 buf_T *bp;
4747
4748 /* Use the name from the associated buffer if it exists. */
4749 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004750 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return aep->ae_fname;
4752 return bp->b_fname;
4753}
4754
4755#if defined(FEAT_WINDOWS) || defined(PROTO)
4756/*
4757 * do_arg_all(): Open up to 'count' windows, one for each argument.
4758 */
4759 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004760do_arg_all(
4761 int count,
4762 int forceit, /* hide buffers in current windows */
4763 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764{
4765 int i;
4766 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004767 char_u *opened; /* Array of weight for which args are open:
4768 * 0: not opened
4769 * 1: opened in other tab
4770 * 2: opened in curtab
4771 * 3: opened in curtab and curwin
4772 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004773 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 int use_firstwin = FALSE; /* use first window for arglist */
4775 int split_ret = OK;
4776 int p_ea_save;
4777 alist_T *alist; /* argument list to be used */
4778 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004779 tabpage_T *tpnext;
4780 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004781 win_T *old_curwin, *last_curwin;
4782 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004783 win_T *new_curwin = NULL;
4784 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785
4786 if (ARGCOUNT <= 0)
4787 {
4788 /* Don't give an error message. We don't want it when the ":all"
4789 * command is in the .vimrc. */
4790 return;
4791 }
4792 setpcmark();
4793
4794 opened_len = ARGCOUNT;
4795 opened = alloc_clear((unsigned)opened_len);
4796 if (opened == NULL)
4797 return;
4798
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004799 /* Autocommands may do anything to the argument list. Make sure it's not
4800 * freed while we are working here by "locking" it. We still have to
4801 * watch out for its size to be changed. */
4802 alist = curwin->w_alist;
4803 ++alist->al_refcount;
4804
4805 old_curwin = curwin;
4806 old_curtab = curtab;
4807
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004808# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 /*
4813 * Try closing all windows that are not in the argument list.
4814 * Also close windows that are not full width;
4815 * When 'hidden' or "forceit" set the buffer becomes hidden.
4816 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004817 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004819 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004820 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004821 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004823 tpnext = curtab->tp_next;
4824 for (wp = firstwin; wp != NULL; wp = wpnext)
4825 {
4826 wpnext = wp->w_next;
4827 buf = wp->w_buffer;
4828 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004829 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004830 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004831 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004832 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004834 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004835 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004837 if (i < alist->al_ga.ga_len
4838 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4839 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4840 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004842 int weight = 1;
4843
4844 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004845 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004846 ++weight;
4847 if (old_curwin == wp)
4848 ++weight;
4849 }
4850
4851 if (weight > (int)opened[i])
4852 {
4853 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004854 if (i == 0)
4855 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004856 if (new_curwin != NULL)
4857 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004858 new_curwin = wp;
4859 new_curtab = curtab;
4860 }
4861 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004862 else if (keep_tabs)
4863 i = opened_len;
4864
4865 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004866 {
4867 /* Use the current argument list for all windows
4868 * containing a file from it. */
4869 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004870 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004871 ++wp->w_alist->al_refcount;
4872 }
4873 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004877 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004879 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004881 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004882 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004884 /* If the buffer was changed, and we would like to hide it,
4885 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004886 if (!P_HID(buf) && buf->b_nwindows <= 1
4887 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004889#ifdef FEAT_AUTOCMD
4890 bufref_T bufref;
4891
4892 set_bufref(&bufref, buf);
4893#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004894 (void)autowrite(buf, FALSE);
4895#ifdef FEAT_AUTOCMD
4896 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004897 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004898 {
4899 wpnext = firstwin; /* start all over... */
4900 continue;
4901 }
4902#endif
4903 }
4904#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004905 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004906 if (firstwin == lastwin
4907 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004908#endif
4909 use_firstwin = TRUE;
4910#ifdef FEAT_WINDOWS
4911 else
4912 {
4913 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4914# ifdef FEAT_AUTOCMD
4915 /* check if autocommands removed the next window */
4916 if (!win_valid(wpnext))
4917 wpnext = firstwin; /* start all over... */
4918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 }
4920#endif
4921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 }
4923 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004924
4925 /* Without the ":tab" modifier only do the current tab page. */
4926 if (had_tab == 0 || tpnext == NULL)
4927 break;
4928
4929# ifdef FEAT_AUTOCMD
4930 /* check if autocommands removed the next tab page */
4931 if (!valid_tabpage(tpnext))
4932 tpnext = first_tabpage; /* start all over...*/
4933# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004934 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936
4937 /*
4938 * Open a window for files in the argument list that don't have one.
4939 * ARGCOUNT may change while doing this, because of autocommands.
4940 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004941 if (count > opened_len || count <= 0)
4942 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944#ifdef FEAT_AUTOCMD
4945 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4946 ++autocmd_no_enter;
4947 ++autocmd_no_leave;
4948#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004949 last_curwin = curwin;
4950 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004952#ifdef FEAT_WINDOWS
4953 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4954 * leaving an empty tab page when executed locally. */
4955 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4956 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4957 use_firstwin = TRUE;
4958#endif
4959
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004960 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 {
4962 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4963 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004964 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 /* Move the already present window to below the current window */
4967 if (curwin->w_arg_idx != i)
4968 {
4969 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4970 {
4971 if (wpnext->w_arg_idx == i)
4972 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004973 if (keep_tabs)
4974 {
4975 new_curwin = wpnext;
4976 new_curtab = curtab;
4977 }
4978 else
4979 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 break;
4981 }
4982 }
4983 }
4984 }
4985 else if (split_ret == OK)
4986 {
4987 if (!use_firstwin) /* split current window */
4988 {
4989 p_ea_save = p_ea;
4990 p_ea = TRUE; /* use space from all windows */
4991 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4992 p_ea = p_ea_save;
4993 if (split_ret == FAIL)
4994 continue;
4995 }
4996#ifdef FEAT_AUTOCMD
4997 else /* first window: do autocmd for leaving this buffer */
4998 --autocmd_no_leave;
4999#endif
5000
5001 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005002 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 */
5004 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005005 if (i == 0)
5006 {
5007 new_curwin = curwin;
5008 new_curtab = curtab;
5009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5011 ECMD_ONE,
5012 ((P_HID(curwin->w_buffer)
5013 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005014 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015#ifdef FEAT_AUTOCMD
5016 if (use_firstwin)
5017 ++autocmd_no_leave;
5018#endif
5019 use_firstwin = FALSE;
5020 }
5021 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005022
5023 /* When ":tab" was used open a new tab for a new window repeatedly. */
5024 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5025 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027
5028 /* Remove the "lock" on the argument list. */
5029 alist_unlink(alist);
5030
5031#ifdef FEAT_AUTOCMD
5032 --autocmd_no_enter;
5033#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005034 /* restore last referenced tabpage's curwin */
5035 if (last_curtab != new_curtab)
5036 {
5037 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005038 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005039 if (win_valid(last_curwin))
5040 win_enter(last_curwin, FALSE);
5041 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005042 /* to window with first arg */
5043 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005044 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005045 if (win_valid(new_curwin))
5046 win_enter(new_curwin, FALSE);
5047
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048#ifdef FEAT_AUTOCMD
5049 --autocmd_no_leave;
5050#endif
5051 vim_free(opened);
5052}
5053
5054# if defined(FEAT_LISTCMDS) || defined(PROTO)
5055/*
5056 * Open a window for a number of buffers.
5057 */
5058 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005059ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060{
5061 buf_T *buf;
5062 win_T *wp, *wpnext;
5063 int split_ret = OK;
5064 int p_ea_save;
5065 int open_wins = 0;
5066 int r;
5067 int count; /* Maximum number of windows to open. */
5068 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005069#ifdef FEAT_WINDOWS
5070 int had_tab = cmdmod.tab;
5071 tabpage_T *tpnext;
5072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 if (eap->addr_count == 0) /* make as many windows as possible */
5075 count = 9999;
5076 else
5077 count = eap->line2; /* make as many windows as specified */
5078 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5079 all = FALSE;
5080 else
5081 all = TRUE;
5082
5083 setpcmark();
5084
5085#ifdef FEAT_GUI
5086 need_mouse_correct = TRUE;
5087#endif
5088
5089 /*
5090 * Close superfluous windows (two windows for the same buffer).
5091 * Also close windows that are not full-width.
5092 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005093#ifdef FEAT_WINDOWS
5094 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005095 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005096 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005099 tpnext = curtab->tp_next;
5100 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005102 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005103 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005104#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005105 || ((cmdmod.split & WSP_VERT)
5106 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005107 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005108 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005109 || (had_tab > 0 && wp != firstwin)
5110#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02005111 ) && firstwin != lastwin
5112#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005113 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005114#endif
5115 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005116 {
5117 win_close(wp, FALSE);
5118#ifdef FEAT_AUTOCMD
5119 wpnext = firstwin; /* just in case an autocommand does
5120 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005121 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005122 open_wins = 0;
5123#endif
5124 }
5125 else
5126 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005128
5129#ifdef FEAT_WINDOWS
5130 /* Without the ":tab" modifier only do the current tab page. */
5131 if (had_tab == 0 || tpnext == NULL)
5132 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005133 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136
5137 /*
5138 * Go through the buffer list. When a buffer doesn't have a window yet,
5139 * open one. Otherwise move the window to the right position.
5140 * Watch out for autocommands that delete buffers or windows!
5141 */
5142#ifdef FEAT_AUTOCMD
5143 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5144 ++autocmd_no_enter;
5145#endif
5146 win_enter(lastwin, FALSE);
5147#ifdef FEAT_AUTOCMD
5148 ++autocmd_no_leave;
5149#endif
5150 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5151 {
5152 /* Check if this buffer needs a window */
5153 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5154 continue;
5155
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005156#ifdef FEAT_WINDOWS
5157 if (had_tab != 0)
5158 {
5159 /* With the ":tab" modifier don't move the window. */
5160 if (buf->b_nwindows > 0)
5161 wp = lastwin; /* buffer has a window, skip it */
5162 else
5163 wp = NULL;
5164 }
5165 else
5166#endif
5167 {
5168 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005169 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005170 if (wp->w_buffer == buf)
5171 break;
5172 /* If the buffer already has a window, move it */
5173 if (wp != NULL)
5174 win_move_after(wp, curwin);
5175 }
5176
5177 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005179#ifdef FEAT_AUTOCMD
5180 bufref_T bufref;
5181
5182 set_bufref(&bufref, buf);
5183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 /* Split the window and put the buffer in it */
5185 p_ea_save = p_ea;
5186 p_ea = TRUE; /* use space from all windows */
5187 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5188 ++open_wins;
5189 p_ea = p_ea_save;
5190 if (split_ret == FAIL)
5191 continue;
5192
5193 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005194#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 swap_exists_action = SEA_DIALOG;
5196#endif
5197 set_curbuf(buf, DOBUF_GOTO);
5198#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005199 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005201 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005202#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005204# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 break;
5206 }
5207#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005208#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (swap_exists_action == SEA_QUIT)
5210 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005211# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5212 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005213
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005214 /* Reset the error/interrupt/exception state here so that
5215 * aborting() returns FALSE when closing a window. */
5216 enter_cleanup(&cs);
5217# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005218
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005219 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 win_close(curwin, TRUE);
5221 --open_wins;
5222 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005223 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005224
5225# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5226 /* Restore the error/interrupt/exception state if not
5227 * discarded by a new aborting error, interrupt, or uncaught
5228 * exception. */
5229 leave_cleanup(&cs);
5230# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
5232 else
5233 handle_swap_exists(NULL);
5234#endif
5235 }
5236
5237 ui_breakcheck();
5238 if (got_int)
5239 {
5240 (void)vgetc(); /* only break the file loading, not the rest */
5241 break;
5242 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005243#ifdef FEAT_EVAL
5244 /* Autocommands deleted the buffer or aborted script processing!!! */
5245 if (aborting())
5246 break;
5247#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005248#ifdef FEAT_WINDOWS
5249 /* When ":tab" was used open a new tab for a new window repeatedly. */
5250 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5251 cmdmod.tab = 9999;
5252#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 }
5254#ifdef FEAT_AUTOCMD
5255 --autocmd_no_enter;
5256#endif
5257 win_enter(firstwin, FALSE); /* back to first window */
5258#ifdef FEAT_AUTOCMD
5259 --autocmd_no_leave;
5260#endif
5261
5262 /*
5263 * Close superfluous windows.
5264 */
5265 for (wp = lastwin; open_wins > count; )
5266 {
5267 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5268 || autowrite(wp->w_buffer, FALSE) == OK);
5269#ifdef FEAT_AUTOCMD
5270 if (!win_valid(wp))
5271 {
5272 /* BufWrite Autocommands made the window invalid, start over */
5273 wp = lastwin;
5274 }
5275 else
5276#endif
5277 if (r)
5278 {
5279 win_close(wp, !P_HID(wp->w_buffer));
5280 --open_wins;
5281 wp = lastwin;
5282 }
5283 else
5284 {
5285 wp = wp->w_prev;
5286 if (wp == NULL)
5287 break;
5288 }
5289 }
5290}
5291# endif /* FEAT_LISTCMDS */
5292
5293#endif /* FEAT_WINDOWS */
5294
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005295static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297/*
5298 * do_modelines() - process mode lines for the current file
5299 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005300 * "flags" can be:
5301 * OPT_WINONLY only set options local to window
5302 * OPT_NOWIN don't set options local to window
5303 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 * Returns immediately if the "ml" option isn't set.
5305 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005307do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005309 linenr_T lnum;
5310 int nmlines;
5311 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
5313 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5314 return;
5315
5316 /* Disallow recursive entry here. Can happen when executing a modeline
5317 * triggers an autocommand, which reloads modelines with a ":do". */
5318 if (entered)
5319 return;
5320
5321 ++entered;
5322 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5323 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005324 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325 nmlines = 0;
5326
5327 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5328 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005329 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 nmlines = 0;
5331 --entered;
5332}
5333
5334#include "version.h" /* for version number */
5335
5336/*
5337 * chk_modeline() - check a single line for a mode string
5338 * Return FAIL if an error encountered.
5339 */
5340 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005341chk_modeline(
5342 linenr_T lnum,
5343 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
5345 char_u *s;
5346 char_u *e;
5347 char_u *linecopy; /* local copy of any modeline found */
5348 int prev;
5349 int vers;
5350 int end;
5351 int retval = OK;
5352 char_u *save_sourcing_name;
5353 linenr_T save_sourcing_lnum;
5354#ifdef FEAT_EVAL
5355 scid_T save_SID;
5356#endif
5357
5358 prev = -1;
5359 for (s = ml_get(lnum); *s != NUL; ++s)
5360 {
5361 if (prev == -1 || vim_isspace(prev))
5362 {
5363 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5364 || STRNCMP(s, "vi:", (size_t)3) == 0)
5365 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005366 /* Accept both "vim" and "Vim". */
5367 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 {
5369 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5370 e = s + 4;
5371 else
5372 e = s + 3;
5373 vers = getdigits(&e);
5374 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005375 && (s[0] != 'V'
5376 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377 && (s[3] == ':'
5378 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5379 || (VIM_VERSION_100 < vers && s[3] == '<')
5380 || (VIM_VERSION_100 > vers && s[3] == '>')
5381 || (VIM_VERSION_100 == vers && s[3] == '=')))
5382 break;
5383 }
5384 }
5385 prev = *s;
5386 }
5387
5388 if (*s)
5389 {
5390 do /* skip over "ex:", "vi:" or "vim:" */
5391 ++s;
5392 while (s[-1] != ':');
5393
5394 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5395 if (linecopy == NULL)
5396 return FAIL;
5397
5398 save_sourcing_lnum = sourcing_lnum;
5399 save_sourcing_name = sourcing_name;
5400 sourcing_lnum = lnum; /* prepare for emsg() */
5401 sourcing_name = (char_u *)"modelines";
5402
5403 end = FALSE;
5404 while (end == FALSE)
5405 {
5406 s = skipwhite(s);
5407 if (*s == NUL)
5408 break;
5409
5410 /*
5411 * Find end of set command: ':' or end of line.
5412 * Skip over "\:", replacing it with ":".
5413 */
5414 for (e = s; *e != ':' && *e != NUL; ++e)
5415 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005416 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 if (*e == NUL)
5418 end = TRUE;
5419
5420 /*
5421 * If there is a "set" command, require a terminating ':' and
5422 * ignore the stuff after the ':'.
5423 * "vi:set opt opt opt: foo" -- foo not interpreted
5424 * "vi:opt opt opt: foo" -- foo interpreted
5425 * Accept "se" for compatibility with Elvis.
5426 */
5427 if (STRNCMP(s, "set ", (size_t)4) == 0
5428 || STRNCMP(s, "se ", (size_t)3) == 0)
5429 {
5430 if (*e != ':') /* no terminating ':'? */
5431 break;
5432 end = TRUE;
5433 s = vim_strchr(s, ' ') + 1;
5434 }
5435 *e = NUL; /* truncate the set command */
5436
5437 if (*s != NUL) /* skip over an empty "::" */
5438 {
5439#ifdef FEAT_EVAL
5440 save_SID = current_SID;
5441 current_SID = SID_MODELINE;
5442#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005443 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444#ifdef FEAT_EVAL
5445 current_SID = save_SID;
5446#endif
5447 if (retval == FAIL) /* stop if error found */
5448 break;
5449 }
5450 s = e + 1; /* advance to next part */
5451 }
5452
5453 sourcing_lnum = save_sourcing_lnum;
5454 sourcing_name = save_sourcing_name;
5455
5456 vim_free(linecopy);
5457 }
5458 return retval;
5459}
5460
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005461#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005463read_viminfo_bufferlist(
5464 vir_T *virp,
5465 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466{
5467 char_u *tab;
5468 linenr_T lnum;
5469 colnr_T col;
5470 buf_T *buf;
5471 char_u *sfname;
5472 char_u *xline;
5473
5474 /* Handle long line and escaped characters. */
5475 xline = viminfo_readstring(virp, 1, FALSE);
5476
5477 /* don't read in if there are files on the command-line or if writing: */
5478 if (xline != NULL && !writing && ARGCOUNT == 0
5479 && find_viminfo_parameter('%') != NULL)
5480 {
5481 /* Format is: <fname> Tab <lnum> Tab <col>.
5482 * Watch out for a Tab in the file name, work from the end. */
5483 lnum = 0;
5484 col = 0;
5485 tab = vim_strrchr(xline, '\t');
5486 if (tab != NULL)
5487 {
5488 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005489 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 tab = vim_strrchr(xline, '\t');
5491 if (tab != NULL)
5492 {
5493 *tab++ = '\0';
5494 lnum = atol((char *)tab);
5495 }
5496 }
5497
5498 /* Expand "~/" in the file name at "line + 1" to a full path.
5499 * Then try shortening it by comparing with the current directory */
5500 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005501 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
5503 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5504 if (buf != NULL) /* just in case... */
5505 {
5506 buf->b_last_cursor.lnum = lnum;
5507 buf->b_last_cursor.col = col;
5508 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5509 }
5510 }
5511 vim_free(xline);
5512
5513 return viminfo_readline(virp);
5514}
5515
5516 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005517write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518{
5519 buf_T *buf;
5520#ifdef FEAT_WINDOWS
5521 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005522 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523#endif
5524 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005525 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
5527 if (find_viminfo_parameter('%') == NULL)
5528 return;
5529
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005530 /* Without a number -1 is returned: do all buffers. */
5531 max_buffers = get_viminfo_parameter('%');
5532
Bram Moolenaar071d4272004-06-13 20:20:40 +00005533 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005534#define LINE_BUF_LEN (MAXPATHL + 40)
5535 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 if (line == NULL)
5537 return;
5538
5539#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005540 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 set_last_cursor(win);
5542#else
5543 set_last_cursor(curwin);
5544#endif
5545
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005546 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005547 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 {
5549 if (buf->b_fname == NULL
5550 || !buf->b_p_bl
5551#ifdef FEAT_QUICKFIX
5552 || bt_quickfix(buf)
5553#endif
5554 || removable(buf->b_ffname))
5555 continue;
5556
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005557 if (max_buffers-- == 0)
5558 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 putc('%', fp);
5560 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005561 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 (long)buf->b_last_cursor.lnum,
5563 buf->b_last_cursor.col);
5564 viminfo_writestring(fp, line);
5565 }
5566 vim_free(line);
5567}
5568#endif
5569
5570
5571/*
5572 * Return special buffer name.
5573 * Returns NULL when the buffer has a normal file name.
5574 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005575 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005576buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577{
5578#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5579 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005580 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005581 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005582 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005583
5584 /*
5585 * For location list window, w_llist_ref points to the location list.
5586 * For quickfix window, w_llist_ref is NULL.
5587 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005588 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005589 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005590 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005591 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593#endif
5594#ifdef FEAT_QUICKFIX
5595 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5596 * contains the name as specified by the user */
5597 if (bt_nofile(buf))
5598 {
5599 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005600 return buf->b_sfname;
5601 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 }
5603#endif
5604 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005605 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 return NULL;
5607}
5608
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005609#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5610 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5611 || defined(PROTO)
5612/*
5613 * Find a window for buffer "buf".
5614 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5615 * If not found FAIL is returned.
5616 */
5617 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005618find_win_for_buf(
5619 buf_T *buf,
5620 win_T **wp,
5621 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005622{
5623 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5624 if ((*wp)->w_buffer == buf)
5625 goto win_found;
5626 return FAIL;
5627win_found:
5628 return OK;
5629}
5630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631
5632#if defined(FEAT_SIGNS) || defined(PROTO)
5633/*
5634 * Insert the sign into the signlist.
5635 */
5636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005637insert_sign(
5638 buf_T *buf, /* buffer to store sign in */
5639 signlist_T *prev, /* previous sign entry */
5640 signlist_T *next, /* next sign entry */
5641 int id, /* sign ID */
5642 linenr_T lnum, /* line number which gets the mark */
5643 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 signlist_T *newsign;
5646
5647 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5648 if (newsign != NULL)
5649 {
5650 newsign->id = id;
5651 newsign->lnum = lnum;
5652 newsign->typenr = typenr;
5653 newsign->next = next;
5654#ifdef FEAT_NETBEANS_INTG
5655 newsign->prev = prev;
5656 if (next != NULL)
5657 next->prev = newsign;
5658#endif
5659
5660 if (prev == NULL)
5661 {
5662 /* When adding first sign need to redraw the windows to create the
5663 * column for signs. */
5664 if (buf->b_signlist == NULL)
5665 {
5666 redraw_buf_later(buf, NOT_VALID);
5667 changed_cline_bef_curs();
5668 }
5669
5670 /* first sign in signlist */
5671 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005672#ifdef FEAT_NETBEANS_INTG
5673 if (netbeans_active())
5674 buf->b_has_sign_column = TRUE;
5675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 }
5677 else
5678 prev->next = newsign;
5679 }
5680}
5681
5682/*
5683 * Add the sign into the signlist. Find the right spot to do it though.
5684 */
5685 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005686buf_addsign(
5687 buf_T *buf, /* buffer to store sign in */
5688 int id, /* sign ID */
5689 linenr_T lnum, /* line number which gets the mark */
5690 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691{
5692 signlist_T *sign; /* a sign in the signlist */
5693 signlist_T *prev; /* the previous sign */
5694
5695 prev = NULL;
5696 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5697 {
5698 if (lnum == sign->lnum && id == sign->id)
5699 {
5700 sign->typenr = typenr;
5701 return;
5702 }
5703 else if (
5704#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5705 id < 0 &&
5706#endif
5707 lnum < sign->lnum)
5708 {
5709#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5710 /* XXX - GRP: Is this because of sign slide problem? Or is it
5711 * really needed? Or is it because we allow multiple signs per
5712 * line? If so, should I add that feature to FEAT_SIGNS?
5713 */
5714 while (prev != NULL && prev->lnum == lnum)
5715 prev = prev->prev;
5716 if (prev == NULL)
5717 sign = buf->b_signlist;
5718 else
5719 sign = prev->next;
5720#endif
5721 insert_sign(buf, prev, sign, id, lnum, typenr);
5722 return;
5723 }
5724 prev = sign;
5725 }
5726#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5727 /* XXX - GRP: See previous comment */
5728 while (prev != NULL && prev->lnum == lnum)
5729 prev = prev->prev;
5730 if (prev == NULL)
5731 sign = buf->b_signlist;
5732 else
5733 sign = prev->next;
5734#endif
5735 insert_sign(buf, prev, sign, id, lnum, typenr);
5736
5737 return;
5738}
5739
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005740/*
5741 * For an existing, placed sign "markId" change the type to "typenr".
5742 * Returns the line number of the sign, or zero if the sign is not found.
5743 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005744 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005745buf_change_sign_type(
5746 buf_T *buf, /* buffer to store sign in */
5747 int markId, /* sign ID */
5748 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 signlist_T *sign; /* a sign in the signlist */
5751
5752 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5753 {
5754 if (sign->id == markId)
5755 {
5756 sign->typenr = typenr;
5757 return sign->lnum;
5758 }
5759 }
5760
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005761 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762}
5763
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005764 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005765buf_getsigntype(
5766 buf_T *buf,
5767 linenr_T lnum,
5768 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 signlist_T *sign; /* a sign in a b_signlist */
5771
5772 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5773 if (sign->lnum == lnum
5774 && (type == SIGN_ANY
5775# ifdef FEAT_SIGN_ICONS
5776 || (type == SIGN_ICON
5777 && sign_get_image(sign->typenr) != NULL)
5778# endif
5779 || (type == SIGN_TEXT
5780 && sign_get_text(sign->typenr) != NULL)
5781 || (type == SIGN_LINEHL
5782 && sign_get_attr(sign->typenr, TRUE) != 0)))
5783 return sign->typenr;
5784 return 0;
5785}
5786
5787
5788 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005789buf_delsign(
5790 buf_T *buf, /* buffer sign is stored in */
5791 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792{
5793 signlist_T **lastp; /* pointer to pointer to current sign */
5794 signlist_T *sign; /* a sign in a b_signlist */
5795 signlist_T *next; /* the next sign in a b_signlist */
5796 linenr_T lnum; /* line number whose sign was deleted */
5797
5798 lastp = &buf->b_signlist;
5799 lnum = 0;
5800 for (sign = buf->b_signlist; sign != NULL; sign = next)
5801 {
5802 next = sign->next;
5803 if (sign->id == id)
5804 {
5805 *lastp = next;
5806#ifdef FEAT_NETBEANS_INTG
5807 if (next != NULL)
5808 next->prev = sign->prev;
5809#endif
5810 lnum = sign->lnum;
5811 vim_free(sign);
5812 break;
5813 }
5814 else
5815 lastp = &sign->next;
5816 }
5817
5818 /* When deleted the last sign need to redraw the windows to remove the
5819 * sign column. */
5820 if (buf->b_signlist == NULL)
5821 {
5822 redraw_buf_later(buf, NOT_VALID);
5823 changed_cline_bef_curs();
5824 }
5825
5826 return lnum;
5827}
5828
5829
5830/*
5831 * Find the line number of the sign with the requested id. If the sign does
5832 * not exist, return 0 as the line number. This will still let the correct file
5833 * get loaded.
5834 */
5835 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005836buf_findsign(
5837 buf_T *buf, /* buffer to store sign in */
5838 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839{
5840 signlist_T *sign; /* a sign in the signlist */
5841
5842 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5843 if (sign->id == id)
5844 return sign->lnum;
5845
5846 return 0;
5847}
5848
5849 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005850buf_findsign_id(
5851 buf_T *buf, /* buffer whose sign we are searching for */
5852 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853{
5854 signlist_T *sign; /* a sign in the signlist */
5855
5856 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5857 if (sign->lnum == lnum)
5858 return sign->id;
5859
5860 return 0;
5861}
5862
5863
5864# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5865/* see if a given type of sign exists on a specific line */
5866 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005867buf_findsigntype_id(
5868 buf_T *buf, /* buffer whose sign we are searching for */
5869 linenr_T lnum, /* line number of sign */
5870 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
5872 signlist_T *sign; /* a sign in the signlist */
5873
5874 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5875 if (sign->lnum == lnum && sign->typenr == typenr)
5876 return sign->id;
5877
5878 return 0;
5879}
5880
5881
5882# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5883/* return the number of icons on the given line */
5884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005885buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886{
5887 signlist_T *sign; /* a sign in the signlist */
5888 int count = 0;
5889
5890 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5891 if (sign->lnum == lnum)
5892 if (sign_get_image(sign->typenr) != NULL)
5893 count++;
5894
5895 return count;
5896}
5897# endif /* FEAT_SIGN_ICONS */
5898# endif /* FEAT_NETBEANS_INTG */
5899
5900
5901/*
5902 * Delete signs in buffer "buf".
5903 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005905buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005906{
5907 signlist_T *next;
5908
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005909 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005910 * sign column. Not when curwin is NULL (this means we're exiting). */
5911 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005912 {
5913 redraw_buf_later(buf, NOT_VALID);
5914 changed_cline_bef_curs();
5915 }
5916
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 while (buf->b_signlist != NULL)
5918 {
5919 next = buf->b_signlist->next;
5920 vim_free(buf->b_signlist);
5921 buf->b_signlist = next;
5922 }
5923}
5924
5925/*
5926 * Delete all signs in all buffers.
5927 */
5928 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005929buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005930{
5931 buf_T *buf; /* buffer we are checking for signs */
5932
Bram Moolenaar29323592016-07-24 22:04:11 +02005933 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936}
5937
5938/*
5939 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5940 */
5941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943{
5944 buf_T *buf;
5945 signlist_T *p;
5946 char lbuf[BUFSIZ];
5947
5948 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5949 msg_putchar('\n');
5950 if (rbuf == NULL)
5951 buf = firstbuf;
5952 else
5953 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005954 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 {
5956 if (buf->b_signlist != NULL)
5957 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005958 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5960 msg_putchar('\n');
5961 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005962 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005964 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5966 MSG_PUTS(lbuf);
5967 msg_putchar('\n');
5968 }
5969 if (rbuf != NULL)
5970 break;
5971 buf = buf->b_next;
5972 }
5973}
5974
5975/*
5976 * Adjust a placed sign for inserted/deleted lines.
5977 */
5978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005979sign_mark_adjust(
5980 linenr_T line1,
5981 linenr_T line2,
5982 long amount,
5983 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005984{
5985 signlist_T *sign; /* a sign in a b_signlist */
5986
5987 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5988 {
5989 if (sign->lnum >= line1 && sign->lnum <= line2)
5990 {
5991 if (amount == MAXLNUM)
5992 sign->lnum = line1;
5993 else
5994 sign->lnum += amount;
5995 }
5996 else if (sign->lnum > line2)
5997 sign->lnum += amount_after;
5998 }
5999}
6000#endif /* FEAT_SIGNS */
6001
6002/*
6003 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6004 */
6005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006006set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007{
6008 if (on != curbuf->b_p_bl)
6009 {
6010 curbuf->b_p_bl = on;
6011#ifdef FEAT_AUTOCMD
6012 if (on)
6013 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6014 else
6015 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6016#endif
6017 }
6018}
6019
6020/*
6021 * Read the file for "buf" again and check if the contents changed.
6022 * Return TRUE if it changed or this could not be checked.
6023 */
6024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006025buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006026{
6027 buf_T *newbuf;
6028 int differ = TRUE;
6029 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006031 exarg_T ea;
6032
6033 /* Allocate a buffer without putting it in the buffer list. */
6034 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6035 if (newbuf == NULL)
6036 return TRUE;
6037
6038 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6039 if (prep_exarg(&ea, buf) == FAIL)
6040 {
6041 wipe_buffer(newbuf, FALSE);
6042 return TRUE;
6043 }
6044
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 /* set curwin/curbuf to buf and save a few things */
6046 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047
Bram Moolenaar4770d092006-01-12 23:22:24 +00006048 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 && readfile(buf->b_ffname, buf->b_fname,
6050 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6051 &ea, READ_NEW | READ_DUMMY) == OK)
6052 {
6053 /* compare the two files line by line */
6054 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6055 {
6056 differ = FALSE;
6057 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6058 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6059 {
6060 differ = TRUE;
6061 break;
6062 }
6063 }
6064 }
6065 vim_free(ea.cmd);
6066
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 /* restore curwin/curbuf and a few other things */
6068 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069
6070 if (curbuf != newbuf) /* safety check */
6071 wipe_buffer(newbuf, FALSE);
6072
6073 return differ;
6074}
6075
6076/*
6077 * Wipe out a buffer and decrement the last buffer number if it was used for
6078 * this buffer. Call this to wipe out a temp buffer that does not contain any
6079 * marks.
6080 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006082wipe_buffer(
6083 buf_T *buf,
6084 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085{
6086 if (buf->b_fnum == top_file_num - 1)
6087 --top_file_num;
6088
6089#ifdef FEAT_AUTOCMD
6090 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006091 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006092#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006093 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094#ifdef FEAT_AUTOCMD
6095 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006096 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097#endif
6098}