blob: ac37685aa62db1fd4be76d33b5c92dc9e5c326ba [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 Moolenaarf9e687e2016-09-04 21:33:09 +0200453# ifdef FEAT_WINDOWS
454 int is_curwin = (curwin!= NULL && curwin->w_buffer == buf);
455 win_T *the_curwin = curwin;
456 tabpage_T *the_curtab = curtab;
457# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458#endif
459 int unload_buf = (action != 0);
460 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
461 int wipe_buf = (action == DOBUF_WIPE);
462
463#ifdef FEAT_QUICKFIX
464 /*
465 * Force unloading or deleting when 'bufhidden' says so.
466 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
467 * "hide" (otherwise we could never free or delete a buffer).
468 */
469 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
470 {
471 del_buf = TRUE;
472 unload_buf = TRUE;
473 }
474 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
475 {
476 del_buf = TRUE;
477 unload_buf = TRUE;
478 wipe_buf = TRUE;
479 }
480 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
481 unload_buf = TRUE;
482#endif
483
Bram Moolenaar30180b82016-09-04 19:57:56 +0200484#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200485 /* Disallow deleting the buffer when it is locked (already being closed or
486 * halfway a command that relies on it). Unloading is allowed. */
487 if (buf->b_locked > 0 && (del_buf || wipe_buf))
488 {
489 EMSG(_("E937: Attempt to delete a buffer that is in use"));
490 return;
491 }
Bram Moolenaar30180b82016-09-04 19:57:56 +0200492#endif
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200493
Bram Moolenaar3be85852014-06-12 14:01:31 +0200494 if (win != NULL
495#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200496 && win_valid_any_tab(win) /* in case autocommands closed the window */
Bram Moolenaar3be85852014-06-12 14:01:31 +0200497#endif
498 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 {
500 /* Set b_last_cursor when closing the last window for the buffer.
501 * Remember the last cursor position and window options of the buffer.
502 * This used to be only for the current window, but then options like
503 * 'foldmethod' may be lost with a ":only" command. */
504 if (buf->b_nwindows == 1)
505 set_last_cursor(win);
506 buflist_setfpos(buf, win,
507 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
508 win->w_cursor.col, TRUE);
509 }
510
511#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200512 set_bufref(&bufref, buf);
513
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514 /* When the buffer is no longer in a window, trigger BufWinLeave */
515 if (buf->b_nwindows == 1)
516 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200517 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200518 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
519 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200520 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100521 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200522 /* Autocommands deleted the buffer. */
523aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100524 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100526 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200527 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200528 if (abort_if_last && one_window())
529 /* Autocommands made this the only window. */
530 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000531
532 /* When the buffer becomes hidden, but is not unloaded, trigger
533 * BufHidden */
534 if (!unload_buf)
535 {
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200536 ++buf->b_locked;
Bram Moolenaar82404332016-07-10 17:00:38 +0200537 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
538 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200539 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200540 /* Autocommands deleted the buffer. */
541 goto aucmd_abort;
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200542 --buf->b_locked;
Bram Moolenaar362ce482012-06-06 19:02:45 +0200543 if (abort_if_last && one_window())
544 /* Autocommands made this the only window. */
545 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 }
547# ifdef FEAT_EVAL
548 if (aborting()) /* autocmds may abort script processing */
549 return;
550# endif
551 }
Bram Moolenaarf9e687e2016-09-04 21:33:09 +0200552
553# ifdef FEAT_WINDOWS
554 /* If the buffer was in curwin and the window has changed, go back to that
555 * window, if it still exists. This avoids that ":edit x" triggering a
556 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
557 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
558 {
559 block_autocmds();
560 goto_tabpage_win(the_curtab, the_curwin);
561 unblock_autocmds();
562 }
563# endif
564
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 nwindows = buf->b_nwindows;
566#endif
567
568 /* decrease the link count from windows (unless not in any window) */
569 if (buf->b_nwindows > 0)
570 --buf->b_nwindows;
571
572 /* Return when a window is displaying the buffer or when it's not
573 * unloaded. */
574 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576
577 /* Always remove the buffer when there is no file name. */
578 if (buf->b_ffname == NULL)
579 del_buf = TRUE;
580
581 /*
582 * Free all things allocated for this buffer.
583 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
584 */
585#ifdef FEAT_AUTOCMD
586 /* Remember if we are closing the current buffer. Restore the number of
587 * windows, so that autocommands in buf_freeall() don't get confused. */
588 is_curbuf = (buf == curbuf);
589 buf->b_nwindows = nwindows;
590#endif
591
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200592 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593
594#ifdef FEAT_AUTOCMD
595 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200596 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 return;
598# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000599 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 return;
601# endif
602
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 /*
604 * It's possible that autocommands change curbuf to the one being deleted.
605 * This might cause the previous curbuf to be deleted unexpectedly. But
606 * in some cases it's OK to delete the curbuf, because a new one is
607 * obtained anyway. Therefore only return if curbuf changed to the
608 * deleted buffer.
609 */
610 if (buf == curbuf && !is_curbuf)
611 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200612
613 if (
614#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200615 win_valid_any_tab(win) &&
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200616#else
617 win != NULL &&
618#endif
619 win->w_buffer == buf)
620 win->w_buffer = NULL; /* make sure we don't use the buffer now */
621
622 /* Autocommands may have opened or closed windows for this buffer.
623 * Decrement the count for the close we do here. */
624 if (buf->b_nwindows > 0)
625 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626#endif
627
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000628 /* Change directories when the 'acd' option is set. */
629 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630
631 /*
632 * Remove the buffer from the list.
633 */
634 if (wipe_buf)
635 {
636#ifdef FEAT_SUN_WORKSHOP
637 if (usingSunWorkShop)
638 workshop_file_closed_lineno((char *)buf->b_ffname,
639 (int)buf->b_last_cursor.lnum);
640#endif
641 vim_free(buf->b_ffname);
642 vim_free(buf->b_sfname);
643 if (buf->b_prev == NULL)
644 firstbuf = buf->b_next;
645 else
646 buf->b_prev->b_next = buf->b_next;
647 if (buf->b_next == NULL)
648 lastbuf = buf->b_prev;
649 else
650 buf->b_next->b_prev = buf->b_prev;
651 free_buffer(buf);
652 }
653 else
654 {
655 if (del_buf)
656 {
657 /* Free all internal variables and reset option values, to make
658 * ":bdel" compatible with Vim 5.7. */
659 free_buffer_stuff(buf, TRUE);
660
661 /* Make it look like a new buffer. */
662 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
663
664 /* Init the options when loaded again. */
665 buf->b_p_initialized = FALSE;
666 }
667 buf_clear_file(buf);
668 if (del_buf)
669 buf->b_p_bl = FALSE;
670 }
671}
672
673/*
674 * Make buffer not contain a file.
675 */
676 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100677buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678{
679 buf->b_ml.ml_line_count = 1;
680 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 buf->b_p_eol = TRUE;
683 buf->b_start_eol = TRUE;
684#ifdef FEAT_MBYTE
685 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000686 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687#endif
688 buf->b_ml.ml_mfp = NULL;
689 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
690#ifdef FEAT_NETBEANS_INTG
691 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#endif
693}
694
695/*
696 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200697 * the file. Careful: get here with "curwin" NULL when exiting.
698 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200699 * BFA_DEL buffer is going to be deleted
700 * BFA_WIPE buffer is going to be wiped out
701 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100704buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705{
706#ifdef FEAT_AUTOCMD
707 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200708 bufref_T bufref;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200709# ifdef FEAT_WINDOWS
710 int is_curwin = (curwin!= NULL && curwin->w_buffer == buf);
711 win_T *the_curwin = curwin;
712 tabpage_T *the_curtab = curtab;
713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714
Bram Moolenaar5a497892016-09-03 16:29:04 +0200715 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200716 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200717 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200718 if (buf->b_ml.ml_mfp != NULL)
719 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200720 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
721 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200722 && !bufref_valid(&bufref))
723 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200724 return;
725 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200726 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000727 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200728 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
729 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200730 && !bufref_valid(&bufref))
731 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 return;
733 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200734 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200736 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
737 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200738 && !bufref_valid(&bufref))
739 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 return;
741 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200742 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200743
744# ifdef FEAT_WINDOWS
745 /* If the buffer was in curwin and the window has changed, go back to that
746 * window, if it still exists. This avoids that ":edit x" triggering a
747 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
748 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
749 {
750 block_autocmds();
751 goto_tabpage_win(the_curtab, the_curwin);
752 unblock_autocmds();
753 }
754# endif
755
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000757 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 return;
759# endif
760
761 /*
762 * It's possible that autocommands change curbuf to the one being deleted.
763 * This might cause curbuf to be deleted unexpectedly. But in some cases
764 * it's OK to delete the curbuf, because a new one is obtained anyway.
765 * Therefore only return if curbuf changed to the deleted buffer.
766 */
767 if (buf == curbuf && !is_curbuf)
768 return;
769#endif
770#ifdef FEAT_DIFF
771 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
772#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200773#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100774 /* Remove any ownsyntax, unless exiting. */
775 if (firstwin != NULL && curwin->w_buffer == buf)
776 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200777#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000778
779#ifdef FEAT_FOLDING
780 /* No folds in an empty buffer. */
781# ifdef FEAT_WINDOWS
782 {
783 win_T *win;
784 tabpage_T *tp;
785
786 FOR_ALL_TAB_WINDOWS(tp, win)
787 if (win->w_buffer == buf)
788 clearFolding(win);
789 }
790# else
791 if (curwin->w_buffer == buf)
792 clearFolding(curwin);
793# endif
794#endif
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796#ifdef FEAT_TCL
797 tcl_buffer_free(buf);
798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 ml_close(buf, TRUE); /* close and delete the memline/memfile */
800 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200801 if ((flags & BFA_KEEP_UNDO) == 0)
802 {
803 u_blockfree(buf); /* free the memory allocated for undo */
804 u_clearall(buf); /* reset all undo information */
805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200807 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000809 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810}
811
812/*
813 * Free a buffer structure and the things it contains related to the buffer
814 * itself (not the file, that must have been done already).
815 */
816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100817free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200819 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200821#ifdef FEAT_EVAL
822 unref_var_dict(buf->b_vars);
823#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200824#ifdef FEAT_LUA
825 lua_buffer_free(buf);
826#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000827#ifdef FEAT_MZSCHEME
828 mzscheme_buffer_free(buf);
829#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830#ifdef FEAT_PERL
831 perl_buf_free(buf);
832#endif
833#ifdef FEAT_PYTHON
834 python_buffer_free(buf);
835#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200836#ifdef FEAT_PYTHON3
837 python3_buffer_free(buf);
838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839#ifdef FEAT_RUBY
840 ruby_buffer_free(buf);
841#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200842#ifdef FEAT_JOB_CHANNEL
843 channel_buffer_free(buf);
844#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200845
846 buf_hashtab_remove(buf);
847
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200848#ifdef FEAT_AUTOCMD
849 aubuflocal_remove(buf);
850
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200851 if (autocmd_busy)
852 {
853 /* Do not free the buffer structure while autocommands are executing,
854 * it's still needed. Free it when autocmd_busy is reset. */
855 buf->b_next = au_pending_free_buf;
856 au_pending_free_buf = buf;
857 }
858 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000859#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200860 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861}
862
863/*
864 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
865 */
866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100867free_buffer_stuff(
868 buf_T *buf,
869 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870{
871 if (free_options)
872 {
873 clear_wininfo(buf); /* including window-local options */
874 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200875#ifdef FEAT_SPELL
876 ga_clear(&buf->b_s.b_langp);
877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 }
879#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200880 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
881 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882#endif
883#ifdef FEAT_USR_CMDS
884 uc_clear(&buf->b_ucmds); /* clear local user commands */
885#endif
886#ifdef FEAT_SIGNS
887 buf_delete_signs(buf); /* delete any signs */
888#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000889#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200890 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892#ifdef FEAT_LOCALMAP
893 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
894 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
895#endif
896#ifdef FEAT_MBYTE
897 vim_free(buf->b_start_fenc);
898 buf->b_start_fenc = NULL;
899#endif
900}
901
902/*
903 * Free the b_wininfo list for buffer "buf".
904 */
905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100906clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
908 wininfo_T *wip;
909
910 while (buf->b_wininfo != NULL)
911 {
912 wip = buf->b_wininfo;
913 buf->b_wininfo = wip->wi_next;
914 if (wip->wi_optset)
915 {
916 clear_winopt(&wip->wi_opt);
917#ifdef FEAT_FOLDING
918 deleteFoldRecurse(&wip->wi_folds);
919#endif
920 }
921 vim_free(wip);
922 }
923}
924
925#if defined(FEAT_LISTCMDS) || defined(PROTO)
926/*
927 * Go to another buffer. Handles the result of the ATTENTION dialog.
928 */
929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100930goto_buffer(
931 exarg_T *eap,
932 int start,
933 int dir,
934 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000936# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200937 bufref_T old_curbuf;
938
939 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 swap_exists_action = SEA_DIALOG;
942# endif
943 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
944 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000945# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
947 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000948# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
949 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000950
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000951 /* Reset the error/interrupt/exception state here so that
952 * aborting() returns FALSE when closing a window. */
953 enter_cleanup(&cs);
954# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000955
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000956 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 win_close(curwin, TRUE);
958 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000959 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000960
961# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
962 /* Restore the error/interrupt/exception state if not discarded by a
963 * new aborting error, interrupt, or uncaught exception. */
964 leave_cleanup(&cs);
965# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 }
967 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200968 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969# endif
970}
971#endif
972
Bram Moolenaare64ac772005-12-07 20:54:59 +0000973#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974/*
975 * Handle the situation of swap_exists_action being set.
976 * It is allowed for "old_curbuf" to be NULL or invalid.
977 */
978 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200979handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000981# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
982 cleanup_T cs;
983# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100984#ifdef FEAT_SYN_HL
985 long old_tw = curbuf->b_p_tw;
986#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200987 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 if (swap_exists_action == SEA_QUIT)
990 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000991# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
992 /* Reset the error/interrupt/exception state here so that
993 * aborting() returns FALSE when closing a buffer. */
994 enter_cleanup(&cs);
995# endif
996
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 /* User selected Quit at ATTENTION prompt. Go back to previous
998 * buffer. If that buffer is gone or the same as the current one,
999 * open a new, empty buffer. */
1000 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001001 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001002 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001003 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1004 || old_curbuf->br_buf == curbuf)
1005 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1006 else
1007 buf = old_curbuf->br_buf;
1008 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001009 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001010 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001011#ifdef FEAT_SYN_HL
1012 if (old_tw != curbuf->b_p_tw)
1013 check_colorcolumn(curwin);
1014#endif
1015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001017
1018# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1019 /* Restore the error/interrupt/exception state if not discarded by a
1020 * new aborting error, interrupt, or uncaught exception. */
1021 leave_cleanup(&cs);
1022# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 }
1024 else if (swap_exists_action == SEA_RECOVER)
1025 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001026# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1027 /* Reset the error/interrupt/exception state here so that
1028 * aborting() returns FALSE when closing a buffer. */
1029 enter_cleanup(&cs);
1030# endif
1031
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 /* User selected Recover at ATTENTION prompt. */
1033 msg_scroll = TRUE;
1034 ml_recover();
1035 MSG_PUTS("\n"); /* don't overwrite the last message */
1036 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001037 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001038
1039# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1040 /* Restore the error/interrupt/exception state if not discarded by a
1041 * new aborting error, interrupt, or uncaught exception. */
1042 leave_cleanup(&cs);
1043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045 swap_exists_action = SEA_NONE;
1046}
1047#endif
1048
1049#if defined(FEAT_LISTCMDS) || defined(PROTO)
1050/*
1051 * do_bufdel() - delete or unload buffer(s)
1052 *
1053 * addr_count == 0: ":bdel" - delete current buffer
1054 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1055 * buffer "end_bnr", then any other arguments.
1056 * addr_count == 2: ":N,N bdel" - delete buffers in range
1057 *
1058 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1059 * DOBUF_DEL (":bdel")
1060 *
1061 * Returns error message or NULL
1062 */
1063 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001064do_bufdel(
1065 int command,
1066 char_u *arg, /* pointer to extra arguments */
1067 int addr_count,
1068 int start_bnr, /* first buffer number in a range */
1069 int end_bnr, /* buffer nr or last buffer nr in a range */
1070 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 int do_current = 0; /* delete current buffer? */
1073 int deleted = 0; /* number of buffers deleted */
1074 char_u *errormsg = NULL; /* return value */
1075 int bnr; /* buffer number */
1076 char_u *p;
1077
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 if (addr_count == 0)
1079 {
1080 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1081 }
1082 else
1083 {
1084 if (addr_count == 2)
1085 {
1086 if (*arg) /* both range and argument is not allowed */
1087 return (char_u *)_(e_trailing);
1088 bnr = start_bnr;
1089 }
1090 else /* addr_count == 1 */
1091 bnr = end_bnr;
1092
1093 for ( ;!got_int; ui_breakcheck())
1094 {
1095 /*
1096 * delete the current buffer last, otherwise when the
1097 * current buffer is deleted, the next buffer becomes
1098 * the current one and will be loaded, which may then
1099 * also be deleted, etc.
1100 */
1101 if (bnr == curbuf->b_fnum)
1102 do_current = bnr;
1103 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1104 forceit) == OK)
1105 ++deleted;
1106
1107 /*
1108 * find next buffer number to delete/unload
1109 */
1110 if (addr_count == 2)
1111 {
1112 if (++bnr > end_bnr)
1113 break;
1114 }
1115 else /* addr_count == 1 */
1116 {
1117 arg = skipwhite(arg);
1118 if (*arg == NUL)
1119 break;
1120 if (!VIM_ISDIGIT(*arg))
1121 {
1122 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001123 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1124 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 if (bnr < 0) /* failed */
1126 break;
1127 arg = p;
1128 }
1129 else
1130 bnr = getdigits(&arg);
1131 }
1132 }
1133 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1134 FORWARD, do_current, forceit) == OK)
1135 ++deleted;
1136
1137 if (deleted == 0)
1138 {
1139 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001140 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001142 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001144 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 errormsg = IObuff;
1146 }
1147 else if (deleted >= p_report)
1148 {
1149 if (command == DOBUF_UNLOAD)
1150 {
1151 if (deleted == 1)
1152 MSG(_("1 buffer unloaded"));
1153 else
1154 smsg((char_u *)_("%d buffers unloaded"), deleted);
1155 }
1156 else if (command == DOBUF_DEL)
1157 {
1158 if (deleted == 1)
1159 MSG(_("1 buffer deleted"));
1160 else
1161 smsg((char_u *)_("%d buffers deleted"), deleted);
1162 }
1163 else
1164 {
1165 if (deleted == 1)
1166 MSG(_("1 buffer wiped out"));
1167 else
1168 smsg((char_u *)_("%d buffers wiped out"), deleted);
1169 }
1170 }
1171 }
1172
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 return errormsg;
1175}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001176#endif /* FEAT_LISTCMDS */
1177
1178#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1179 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001181static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001182
1183/*
1184 * Make the current buffer empty.
1185 * Used when it is wiped out and it's the last buffer.
1186 */
1187 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001188empty_curbuf(
1189 int close_others,
1190 int forceit,
1191 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001192{
1193 int retval;
1194 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001195 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001196
1197 if (action == DOBUF_UNLOAD)
1198 {
1199 EMSG(_("E90: Cannot unload last buffer"));
1200 return FAIL;
1201 }
1202
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001203 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001204#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001205 if (close_others)
1206 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001207 close_windows(buf, TRUE);
1208#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001209
1210 setpcmark();
1211 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1212 forceit ? ECMD_FORCEIT : 0, curwin);
1213
1214 /*
1215 * do_ecmd() may create a new buffer, then we have to delete
1216 * the old one. But do_ecmd() may have done that already, check
1217 * if the buffer still exists.
1218 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001219 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001220 close_buffer(NULL, buf, action, FALSE);
1221 if (!close_others)
1222 need_fileinfo = FALSE;
1223 return retval;
1224}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225/*
1226 * Implementation of the commands for the buffer list.
1227 *
1228 * action == DOBUF_GOTO go to specified buffer
1229 * action == DOBUF_SPLIT split window and go to specified buffer
1230 * action == DOBUF_UNLOAD unload specified buffer(s)
1231 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1232 * action == DOBUF_WIPE delete specified buffer(s) really
1233 *
1234 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1235 * start == DOBUF_FIRST go to "count" buffer from first buffer
1236 * start == DOBUF_LAST go to "count" buffer from last buffer
1237 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1238 *
1239 * Return FAIL or OK.
1240 */
1241 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001242do_buffer(
1243 int action,
1244 int start,
1245 int dir, /* FORWARD or BACKWARD */
1246 int count, /* buffer number or number of buffers */
1247 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 buf_T *buf;
1250 buf_T *bp;
1251 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1252 || action == DOBUF_WIPE);
1253
1254 switch (start)
1255 {
1256 case DOBUF_FIRST: buf = firstbuf; break;
1257 case DOBUF_LAST: buf = lastbuf; break;
1258 default: buf = curbuf; break;
1259 }
1260 if (start == DOBUF_MOD) /* find next modified buffer */
1261 {
1262 while (count-- > 0)
1263 {
1264 do
1265 {
1266 buf = buf->b_next;
1267 if (buf == NULL)
1268 buf = firstbuf;
1269 }
1270 while (buf != curbuf && !bufIsChanged(buf));
1271 }
1272 if (!bufIsChanged(buf))
1273 {
1274 EMSG(_("E84: No modified buffer found"));
1275 return FAIL;
1276 }
1277 }
1278 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1279 {
1280 while (buf != NULL && buf->b_fnum != count)
1281 buf = buf->b_next;
1282 }
1283 else
1284 {
1285 bp = NULL;
1286 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1287 {
1288 /* remember the buffer where we start, we come back there when all
1289 * buffers are unlisted. */
1290 if (bp == NULL)
1291 bp = buf;
1292 if (dir == FORWARD)
1293 {
1294 buf = buf->b_next;
1295 if (buf == NULL)
1296 buf = firstbuf;
1297 }
1298 else
1299 {
1300 buf = buf->b_prev;
1301 if (buf == NULL)
1302 buf = lastbuf;
1303 }
1304 /* don't count unlisted buffers */
1305 if (unload || buf->b_p_bl)
1306 {
1307 --count;
1308 bp = NULL; /* use this buffer as new starting point */
1309 }
1310 if (bp == buf)
1311 {
1312 /* back where we started, didn't find anything. */
1313 EMSG(_("E85: There is no listed buffer"));
1314 return FAIL;
1315 }
1316 }
1317 }
1318
1319 if (buf == NULL) /* could not find it */
1320 {
1321 if (start == DOBUF_FIRST)
1322 {
1323 /* don't warn when deleting */
1324 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001325 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326 }
1327 else if (dir == FORWARD)
1328 EMSG(_("E87: Cannot go beyond last buffer"));
1329 else
1330 EMSG(_("E88: Cannot go before first buffer"));
1331 return FAIL;
1332 }
1333
1334#ifdef FEAT_GUI
1335 need_mouse_correct = TRUE;
1336#endif
1337
1338#ifdef FEAT_LISTCMDS
1339 /*
1340 * delete buffer buf from memory and/or the list
1341 */
1342 if (unload)
1343 {
1344 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001345# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1346 bufref_T bufref;
1347
1348 set_bufref(&bufref, buf);
1349# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
1351 /* When unloading or deleting a buffer that's already unloaded and
1352 * unlisted: fail silently. */
1353 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1354 return FAIL;
1355
1356 if (!forceit && bufIsChanged(buf))
1357 {
1358#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1359 if ((p_confirm || cmdmod.confirm) && p_write)
1360 {
1361 dialog_changed(buf, FALSE);
1362# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001363 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 /* Autocommand deleted buffer, oops! It's not changed
1365 * now. */
1366 return FAIL;
1367# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001368 /* If it's still changed fail silently, the dialog already
1369 * mentioned why it fails. */
1370 if (bufIsChanged(buf))
1371 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001373 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374#endif
1375 {
1376 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1377 buf->b_fnum);
1378 return FAIL;
1379 }
1380 }
1381
1382 /*
1383 * If deleting the last (listed) buffer, make it empty.
1384 * The last (listed) buffer cannot be unloaded.
1385 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001386 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (bp->b_p_bl && bp != buf)
1388 break;
1389 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001390 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391
1392#ifdef FEAT_WINDOWS
1393 /*
1394 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001395 * (unless it's the only window). Repeat this so long as we end up in
1396 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001398 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001399# ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001400 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02001401# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001402 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001403 {
1404 if (win_close(curwin, FALSE) == FAIL)
1405 break;
1406 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407#endif
1408
1409 /*
1410 * If the buffer to be deleted is not the current one, delete it here.
1411 */
1412 if (buf != curbuf)
1413 {
1414#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001415 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001416 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001418 if (buf->b_nwindows <= 0)
1419 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 return OK;
1421 }
1422
1423 /*
1424 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001425 * There should be another, otherwise it would have been handled
1426 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001427 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 * Then prefer the buffer we most recently visited.
1429 * Else try to find one that is loaded, after the current buffer,
1430 * then before the current buffer.
1431 * Finally use any buffer.
1432 */
1433 buf = NULL; /* selected buffer */
1434 bp = NULL; /* used when no loaded buffer found */
1435#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001436 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1437 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438# ifdef FEAT_JUMPLIST
1439 else
1440# endif
1441#endif
1442#ifdef FEAT_JUMPLIST
1443 if (curwin->w_jumplistlen > 0)
1444 {
1445 int jumpidx;
1446
1447 jumpidx = curwin->w_jumplistidx - 1;
1448 if (jumpidx < 0)
1449 jumpidx = curwin->w_jumplistlen - 1;
1450
1451 forward = jumpidx;
1452 while (jumpidx != curwin->w_jumplistidx)
1453 {
1454 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1455 if (buf != NULL)
1456 {
1457 if (buf == curbuf || !buf->b_p_bl)
1458 buf = NULL; /* skip current and unlisted bufs */
1459 else if (buf->b_ml.ml_mfp == NULL)
1460 {
1461 /* skip unloaded buf, but may keep it for later */
1462 if (bp == NULL)
1463 bp = buf;
1464 buf = NULL;
1465 }
1466 }
1467 if (buf != NULL) /* found a valid buffer: stop searching */
1468 break;
1469 /* advance to older entry in jump list */
1470 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1471 break;
1472 if (--jumpidx < 0)
1473 jumpidx = curwin->w_jumplistlen - 1;
1474 if (jumpidx == forward) /* List exhausted for sure */
1475 break;
1476 }
1477 }
1478#endif
1479
1480 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1481 {
1482 forward = TRUE;
1483 buf = curbuf->b_next;
1484 for (;;)
1485 {
1486 if (buf == NULL)
1487 {
1488 if (!forward) /* tried both directions */
1489 break;
1490 buf = curbuf->b_prev;
1491 forward = FALSE;
1492 continue;
1493 }
1494 /* in non-help buffer, try to skip help buffers, and vv */
1495 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1496 {
1497 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1498 break;
1499 if (bp == NULL) /* remember unloaded buf for later */
1500 bp = buf;
1501 }
1502 if (forward)
1503 buf = buf->b_next;
1504 else
1505 buf = buf->b_prev;
1506 }
1507 }
1508 if (buf == NULL) /* No loaded buffer, use unloaded one */
1509 buf = bp;
1510 if (buf == NULL) /* No loaded buffer, find listed one */
1511 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001512 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513 if (buf->b_p_bl && buf != curbuf)
1514 break;
1515 }
1516 if (buf == NULL) /* Still no buffer, just take one */
1517 {
1518 if (curbuf->b_next != NULL)
1519 buf = curbuf->b_next;
1520 else
1521 buf = curbuf->b_prev;
1522 }
1523 }
1524
Bram Moolenaara02471e2014-01-10 16:43:14 +01001525 if (buf == NULL)
1526 {
1527 /* Autocommands must have wiped out all other buffers. Only option
1528 * now is to make the current buffer empty. */
1529 return empty_curbuf(FALSE, forceit, action);
1530 }
1531
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 /*
1533 * make buf current buffer
1534 */
1535 if (action == DOBUF_SPLIT) /* split window first */
1536 {
1537# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001538 /* If 'switchbuf' contains "useopen": jump to first window containing
1539 * "buf" if one exists */
1540 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001541 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001542 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001543 * page containing "buf" if one exists */
1544 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001545 return OK;
1546 if (win_split(0, 0) == FAIL)
1547# endif
1548 return FAIL;
1549 }
1550#endif
1551
1552 /* go to current buffer - nothing to do */
1553 if (buf == curbuf)
1554 return OK;
1555
1556 /*
1557 * Check if the current buffer may be abandoned.
1558 */
1559 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1560 {
1561#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1562 if ((p_confirm || cmdmod.confirm) && p_write)
1563 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001564# ifdef FEAT_AUTOCMD
1565 bufref_T bufref;
1566
1567 set_bufref(&bufref, buf);
1568# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 dialog_changed(curbuf, FALSE);
1570# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001571 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 /* Autocommand deleted buffer, oops! */
1573 return FAIL;
1574# endif
1575 }
1576 if (bufIsChanged(curbuf))
1577#endif
1578 {
1579 EMSG(_(e_nowrtmsg));
1580 return FAIL;
1581 }
1582 }
1583
1584 /* Go to the other buffer. */
1585 set_curbuf(buf, action);
1586
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001587#if defined(FEAT_LISTCMDS) \
1588 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001590 {
1591 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1592 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#endif
1594
1595#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1596 if (aborting()) /* autocmds may abort script processing */
1597 return FAIL;
1598#endif
1599
1600 return OK;
1601}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604/*
1605 * Set current buffer to "buf". Executes autocommands and closes current
1606 * buffer. "action" tells how to close the current buffer:
1607 * DOBUF_GOTO free or hide it
1608 * DOBUF_SPLIT nothing
1609 * DOBUF_UNLOAD unload it
1610 * DOBUF_DEL delete it
1611 * DOBUF_WIPE wipe it out
1612 */
1613 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615{
1616 buf_T *prevbuf;
1617 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1618 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001619#ifdef FEAT_SYN_HL
1620 long old_tw = curbuf->b_p_tw;
1621#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001622 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
1624 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001625 if (!cmdmod.keepalt)
1626 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001627 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 /* Don't restart Select mode after switching to another buffer. */
1630 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631
1632 /* close_windows() or apply_autocmds() may change curbuf */
1633 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001634 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635
1636#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001637 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001639 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001641 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642# endif
1643#endif
1644 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001645#ifdef FEAT_SYN_HL
1646 if (prevbuf == curwin->w_buffer)
1647 reset_synblock(curwin);
1648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649#ifdef FEAT_WINDOWS
1650 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001651 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652#endif
1653#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001654 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001656 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001658 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001659#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001660 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001661#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001662 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001663 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1665 unload ? action : (action == DOBUF_GOTO
1666 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001667 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001668#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001669 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001670 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001671 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001672#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
1675#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001676 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001677 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001678 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1679 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001680# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001681 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001682# endif
1683# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001684 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001685# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001686 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001688 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001690#ifdef FEAT_SYN_HL
1691 if (old_tw != curbuf->b_p_tw)
1692 check_colorcolumn(curwin);
1693#endif
1694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695}
1696
1697/*
1698 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001699 * Old curbuf must have been abandoned already! This also means "curbuf" may
1700 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 */
1702 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 /* Copy buffer and window local option values. Not for a help buffer. */
1706 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1707 if (!buf->b_help)
1708 get_winopts(buf);
1709#ifdef FEAT_FOLDING
1710 else
1711 /* Remove all folds in the window. */
1712 clearFolding(curwin);
1713 foldUpdateAll(curwin); /* update folds (later). */
1714#endif
1715
1716 /* Get the buffer in the current window. */
1717 curwin->w_buffer = buf;
1718 curbuf = buf;
1719 ++curbuf->b_nwindows;
1720
1721#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001722 if (curwin->w_p_diff)
1723 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
1725
Bram Moolenaara971b822011-09-14 14:43:25 +02001726#ifdef FEAT_SYN_HL
1727 curwin->w_s = &(buf->b_s);
1728#endif
1729
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 /* Cursor on first line by default. */
1731 curwin->w_cursor.lnum = 1;
1732 curwin->w_cursor.col = 0;
1733#ifdef FEAT_VIRTUALEDIT
1734 curwin->w_cursor.coladd = 0;
1735#endif
1736 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001737#ifdef FEAT_AUTOCMD
1738 curwin->w_topline_was_set = FALSE;
1739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001741 /* mark cursor position as being invalid */
1742 curwin->w_valid = 0;
1743
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 /* Make sure the buffer is loaded. */
1745 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001746 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001747#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001748 /* If there is no filetype, allow for detecting one. Esp. useful for
1749 * ":ball" used in a autocommand. If there already is a filetype we
1750 * might prefer to keep it. */
1751 if (*curbuf->b_p_ft == NUL)
1752 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001753#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001754
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001755 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001756 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 else
1758 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001759 if (!msg_silent)
1760 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1762#ifdef FEAT_AUTOCMD
1763 curwin->w_topline = 1;
1764# ifdef FEAT_DIFF
1765 curwin->w_topfill = 0;
1766# endif
1767 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1768 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1769#endif
1770 }
1771
1772 /* If autocommands did not change the cursor position, restore cursor lnum
1773 * and possibly cursor col. */
1774 if (curwin->w_cursor.lnum == 1 && inindent(0))
1775 buflist_getfpos();
1776
1777 check_arg_idx(curwin); /* check for valid arg_idx */
1778#ifdef FEAT_TITLE
1779 maketitle();
1780#endif
1781#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001782 /* when autocmds didn't change it */
1783 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784#endif
1785 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1786
Bram Moolenaar009b2592004-10-24 19:18:58 +00001787#ifdef FEAT_NETBEANS_INTG
1788 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001789 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001790#endif
1791
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001792 /* Change directories when the 'acd' option is set. */
1793 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794
1795#ifdef FEAT_KEYMAP
1796 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001797 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001799#ifdef FEAT_SPELL
1800 /* May need to set the spell language. Can only do this after the buffer
1801 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001802 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1803 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001804#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001805#ifdef FEAT_VIMINFO
1806 curbuf->b_last_used = vim_time();
1807#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001808
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 redraw_later(NOT_VALID);
1810}
1811
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001812#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1813/*
1814 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001815 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001816 */
1817 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001819{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001820 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001821 && curbuf->b_ffname != NULL
1822 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001823 shorten_fnames(TRUE);
1824}
1825#endif
1826
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827/*
1828 * functions for dealing with the buffer list
1829 */
1830
Bram Moolenaar480778b2016-07-14 22:09:39 +02001831static int top_file_num = 1; /* highest file number */
1832
Bram Moolenaar071d4272004-06-13 20:20:40 +00001833/*
1834 * Add a file name to the buffer list. Return a pointer to the buffer.
1835 * If the same file name already exists return a pointer to that buffer.
1836 * If it does not exist, or if fname == NULL, a new entry is created.
1837 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1838 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1839 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001840 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001841 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1842 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 * This is the ONLY way to create a new buffer.
1844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001846buflist_new(
1847 char_u *ffname, /* full path of fname or relative */
1848 char_u *sfname, /* short fname or NULL */
1849 linenr_T lnum, /* preferred cursor line */
1850 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851{
1852 buf_T *buf;
1853#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001854 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855#endif
1856
Bram Moolenaar480778b2016-07-14 22:09:39 +02001857 if (top_file_num == 1)
1858 hash_init(&buf_hashtab);
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1861
1862 /*
1863 * If file name already exists in the list, update the entry.
1864 */
1865#ifdef UNIX
1866 /* On Unix we can use inode numbers when the file exists. Works better
1867 * for hard links. */
1868 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1869 st.st_dev = (dev_T)-1;
1870#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001871 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#ifdef UNIX
1873 buflist_findname_stat(ffname, &st)
1874#else
1875 buflist_findname(ffname)
1876#endif
1877 ) != NULL)
1878 {
1879 vim_free(ffname);
1880 if (lnum != 0)
1881 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001882
1883 if ((flags & BLN_NOOPT) == 0)
1884 /* copy the options now, if 'cpo' doesn't have 's' and not done
1885 * already */
1886 buf_copy_options(buf, 0);
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1889 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001890#ifdef FEAT_AUTOCMD
1891 bufref_T bufref;
1892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 buf->b_p_bl = TRUE;
1894#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001895 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001897 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001898 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001899 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001900 return NULL;
1901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902#endif
1903 }
1904 return buf;
1905 }
1906
1907 /*
1908 * If the current buffer has no name and no contents, use the current
1909 * buffer. Otherwise: Need to allocate a new buffer structure.
1910 *
1911 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001912 * (A spell file buffer is allocated in spell.c, but that's not a normal
1913 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 */
1915 buf = NULL;
1916 if ((flags & BLN_CURBUF)
1917 && curbuf != NULL
1918 && curbuf->b_ffname == NULL
1919 && curbuf->b_nwindows <= 1
1920 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1921 {
1922 buf = curbuf;
1923#ifdef FEAT_AUTOCMD
1924 /* It's like this buffer is deleted. Watch out for autocommands that
1925 * change curbuf! If that happens, allocate a new buffer anyway. */
1926 if (curbuf->b_p_bl)
1927 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1928 if (buf == curbuf)
1929 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1930# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001931 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 return NULL;
1933# endif
1934#endif
1935#ifdef FEAT_QUICKFIX
1936# ifdef FEAT_AUTOCMD
1937 if (buf == curbuf)
1938# endif
1939 {
1940 /* Make sure 'bufhidden' and 'buftype' are empty */
1941 clear_string_option(&buf->b_p_bh);
1942 clear_string_option(&buf->b_p_bt);
1943 }
1944#endif
1945 }
1946 if (buf != curbuf || curbuf == NULL)
1947 {
1948 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1949 if (buf == NULL)
1950 {
1951 vim_free(ffname);
1952 return NULL;
1953 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001954#ifdef FEAT_EVAL
1955 /* init b: variables */
1956 buf->b_vars = dict_alloc();
1957 if (buf->b_vars == NULL)
1958 {
1959 vim_free(ffname);
1960 vim_free(buf);
1961 return NULL;
1962 }
1963 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1964#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 }
1966
1967 if (ffname != NULL)
1968 {
1969 buf->b_ffname = ffname;
1970 buf->b_sfname = vim_strsave(sfname);
1971 }
1972
1973 clear_wininfo(buf);
1974 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1975
1976 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1977 || buf->b_wininfo == NULL)
1978 {
1979 vim_free(buf->b_ffname);
1980 buf->b_ffname = NULL;
1981 vim_free(buf->b_sfname);
1982 buf->b_sfname = NULL;
1983 if (buf != curbuf)
1984 free_buffer(buf);
1985 return NULL;
1986 }
1987
1988 if (buf == curbuf)
1989 {
1990 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001991 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 if (buf != curbuf) /* autocommands deleted the buffer! */
1993 return NULL;
1994#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001995 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 return NULL;
1997#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001998 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001999
2000 /* Init the options. */
2001 buf->b_p_initialized = FALSE;
2002 buf_copy_options(buf, BCO_ENTER);
2003
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004#ifdef FEAT_KEYMAP
2005 /* need to reload lmaps and set b:keymap_name */
2006 curbuf->b_kmap_state |= KEYMAP_INIT;
2007#endif
2008 }
2009 else
2010 {
2011 /*
2012 * put new buffer at the end of the buffer list
2013 */
2014 buf->b_next = NULL;
2015 if (firstbuf == NULL) /* buffer list is empty */
2016 {
2017 buf->b_prev = NULL;
2018 firstbuf = buf;
2019 }
2020 else /* append new buffer at end of list */
2021 {
2022 lastbuf->b_next = buf;
2023 buf->b_prev = lastbuf;
2024 }
2025 lastbuf = buf;
2026
2027 buf->b_fnum = top_file_num++;
2028 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2029 {
2030 EMSG(_("W14: Warning: List of file names overflow"));
2031 if (emsg_silent == 0)
2032 {
2033 out_flush();
2034 ui_delay(3000L, TRUE); /* make sure it is noticed */
2035 }
2036 top_file_num = 1;
2037 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002038 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039
2040 /*
2041 * Always copy the options from the current buffer.
2042 */
2043 buf_copy_options(buf, BCO_ALWAYS);
2044 }
2045
2046 buf->b_wininfo->wi_fpos.lnum = lnum;
2047 buf->b_wininfo->wi_win = curwin;
2048
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002049#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002050 hash_init(&buf->b_s.b_keywtab);
2051 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#endif
2053
2054 buf->b_fname = buf->b_sfname;
2055#ifdef UNIX
2056 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002057 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 else
2059 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002060 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061 buf->b_dev = st.st_dev;
2062 buf->b_ino = st.st_ino;
2063 }
2064#endif
2065 buf->b_u_synced = TRUE;
2066 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002067 if (flags & BLN_DUMMY)
2068 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 buf_clear_file(buf);
2070 clrallmarks(buf); /* clear marks */
2071 fmarks_check_names(buf); /* check file marks for this file */
2072 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2073#ifdef FEAT_AUTOCMD
2074 if (!(flags & BLN_DUMMY))
2075 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002076 bufref_T bufref;
2077
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002078 /* Tricky: these autocommands may change the buffer list. They could
2079 * also split the window with re-using the one empty buffer. This may
2080 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002081 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002082 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002083 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002084 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002086 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002087 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002088 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002089 return NULL;
2090 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002092 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 return NULL;
2094# endif
2095 }
2096#endif
2097
2098 return buf;
2099}
2100
2101/*
2102 * Free the memory for the options of a buffer.
2103 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2104 * 'fileencoding'.
2105 */
2106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002107free_buf_options(
2108 buf_T *buf,
2109 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 if (free_p_ff)
2112 {
2113#ifdef FEAT_MBYTE
2114 clear_string_option(&buf->b_p_fenc);
2115#endif
2116 clear_string_option(&buf->b_p_ff);
2117#ifdef FEAT_QUICKFIX
2118 clear_string_option(&buf->b_p_bh);
2119 clear_string_option(&buf->b_p_bt);
2120#endif
2121 }
2122#ifdef FEAT_FIND_ID
2123 clear_string_option(&buf->b_p_def);
2124 clear_string_option(&buf->b_p_inc);
2125# ifdef FEAT_EVAL
2126 clear_string_option(&buf->b_p_inex);
2127# endif
2128#endif
2129#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2130 clear_string_option(&buf->b_p_inde);
2131 clear_string_option(&buf->b_p_indk);
2132#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002133#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2134 clear_string_option(&buf->b_p_bexpr);
2135#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002136#if defined(FEAT_CRYPT)
2137 clear_string_option(&buf->b_p_cm);
2138#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002139#if defined(FEAT_EVAL)
2140 clear_string_option(&buf->b_p_fex);
2141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142#ifdef FEAT_CRYPT
2143 clear_string_option(&buf->b_p_key);
2144#endif
2145 clear_string_option(&buf->b_p_kp);
2146 clear_string_option(&buf->b_p_mps);
2147 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002148 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 clear_string_option(&buf->b_p_isk);
2150#ifdef FEAT_KEYMAP
2151 clear_string_option(&buf->b_p_keymap);
2152 ga_clear(&buf->b_kmap_ga);
2153#endif
2154#ifdef FEAT_COMMENTS
2155 clear_string_option(&buf->b_p_com);
2156#endif
2157#ifdef FEAT_FOLDING
2158 clear_string_option(&buf->b_p_cms);
2159#endif
2160 clear_string_option(&buf->b_p_nf);
2161#ifdef FEAT_SYN_HL
2162 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002163 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002164#endif
2165#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002166 clear_string_option(&buf->b_s.b_p_spc);
2167 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002168 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002169 buf->b_s.b_cap_prog = NULL;
2170 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171#endif
2172#ifdef FEAT_SEARCHPATH
2173 clear_string_option(&buf->b_p_sua);
2174#endif
2175#ifdef FEAT_AUTOCMD
2176 clear_string_option(&buf->b_p_ft);
2177#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178#ifdef FEAT_CINDENT
2179 clear_string_option(&buf->b_p_cink);
2180 clear_string_option(&buf->b_p_cino);
2181#endif
2182#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2183 clear_string_option(&buf->b_p_cinw);
2184#endif
2185#ifdef FEAT_INS_EXPAND
2186 clear_string_option(&buf->b_p_cpt);
2187#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002188#ifdef FEAT_COMPL_FUNC
2189 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002190 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192#ifdef FEAT_QUICKFIX
2193 clear_string_option(&buf->b_p_gp);
2194 clear_string_option(&buf->b_p_mp);
2195 clear_string_option(&buf->b_p_efm);
2196#endif
2197 clear_string_option(&buf->b_p_ep);
2198 clear_string_option(&buf->b_p_path);
2199 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002200 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#ifdef FEAT_INS_EXPAND
2202 clear_string_option(&buf->b_p_dict);
2203 clear_string_option(&buf->b_p_tsr);
2204#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002205#ifdef FEAT_TEXTOBJ
2206 clear_string_option(&buf->b_p_qe);
2207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002209 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002210#ifdef FEAT_LISP
2211 clear_string_option(&buf->b_p_lw);
2212#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002213 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214}
2215
2216/*
2217 * get alternate file n
2218 * set linenr to lnum or altfpos.lnum if lnum == 0
2219 * also set cursor column to altfpos.col if 'startofline' is not set.
2220 * if (options & GETF_SETMARK) call setpcmark()
2221 * if (options & GETF_ALT) we are jumping to an alternate file.
2222 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2223 *
2224 * return FAIL for failure, OK for success
2225 */
2226 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227buflist_getfile(
2228 int n,
2229 linenr_T lnum,
2230 int options,
2231 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232{
2233 buf_T *buf;
2234#ifdef FEAT_WINDOWS
2235 win_T *wp = NULL;
2236#endif
2237 pos_T *fpos;
2238 colnr_T col;
2239
2240 buf = buflist_findnr(n);
2241 if (buf == NULL)
2242 {
2243 if ((options & GETF_ALT) && n == 0)
2244 EMSG(_(e_noalt));
2245 else
2246 EMSGN(_("E92: Buffer %ld not found"), n);
2247 return FAIL;
2248 }
2249
2250 /* if alternate file is the current buffer, nothing to do */
2251 if (buf == curbuf)
2252 return OK;
2253
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002254 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002255 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002256 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002258 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259#ifdef FEAT_AUTOCMD
2260 if (curbuf_locked())
2261 return FAIL;
2262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263
2264 /* altfpos may be changed by getfile(), get it now */
2265 if (lnum == 0)
2266 {
2267 fpos = buflist_findfpos(buf);
2268 lnum = fpos->lnum;
2269 col = fpos->col;
2270 }
2271 else
2272 col = 0;
2273
2274#ifdef FEAT_WINDOWS
2275 if (options & GETF_SWITCH)
2276 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002277 /* If 'switchbuf' contains "useopen": jump to first window containing
2278 * "buf" if one exists */
2279 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002281
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002282 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002283 * page containing "buf" if one exists */
2284 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002285 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002286
2287 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2288 * current buffer isn't empty: open new tab or window */
2289 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2290 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002292 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002293 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002294 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2295 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002297 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 }
2299 }
2300#endif
2301
2302 ++RedrawingDisabled;
2303 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2304 lnum, forceit) <= 0)
2305 {
2306 --RedrawingDisabled;
2307
2308 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2309 if (!p_sol && col != 0)
2310 {
2311 curwin->w_cursor.col = col;
2312 check_cursor_col();
2313#ifdef FEAT_VIRTUALEDIT
2314 curwin->w_cursor.coladd = 0;
2315#endif
2316 curwin->w_set_curswant = TRUE;
2317 }
2318 return OK;
2319 }
2320 --RedrawingDisabled;
2321 return FAIL;
2322}
2323
2324/*
2325 * go to the last know line number for the current buffer
2326 */
2327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329{
2330 pos_T *fpos;
2331
2332 fpos = buflist_findfpos(curbuf);
2333
2334 curwin->w_cursor.lnum = fpos->lnum;
2335 check_cursor_lnum();
2336
2337 if (p_sol)
2338 curwin->w_cursor.col = 0;
2339 else
2340 {
2341 curwin->w_cursor.col = fpos->col;
2342 check_cursor_col();
2343#ifdef FEAT_VIRTUALEDIT
2344 curwin->w_cursor.coladd = 0;
2345#endif
2346 curwin->w_set_curswant = TRUE;
2347 }
2348}
2349
Bram Moolenaar81695252004-12-29 20:58:21 +00002350#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2351/*
2352 * Find file in buffer list by name (it has to be for the current window).
2353 * Returns NULL if not found.
2354 */
2355 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002356buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002357{
2358 char_u *ffname;
2359 buf_T *buf = NULL;
2360
2361 /* First make the name into a full path name */
2362 ffname = FullName_save(fname,
2363#ifdef UNIX
2364 TRUE /* force expansion, get rid of symbolic links */
2365#else
2366 FALSE
2367#endif
2368 );
2369 if (ffname != NULL)
2370 {
2371 buf = buflist_findname(ffname);
2372 vim_free(ffname);
2373 }
2374 return buf;
2375}
2376#endif
2377
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378/*
2379 * Find file in buffer list by name (it has to be for the current window).
2380 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002381 * Skips dummy buffers.
2382 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 */
2384 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002385buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386{
2387#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002388 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
2390 if (mch_stat((char *)ffname, &st) < 0)
2391 st.st_dev = (dev_T)-1;
2392 return buflist_findname_stat(ffname, &st);
2393}
2394
2395/*
2396 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2397 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002398 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 */
2400 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002401buflist_findname_stat(
2402 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002403 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404{
2405#endif
2406 buf_T *buf;
2407
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002408 /* Start at the last buffer, expect to find a match sooner. */
2409 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002410 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411#ifdef UNIX
2412 , stp
2413#endif
2414 ))
2415 return buf;
2416 return NULL;
2417}
2418
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002419#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2420 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421/*
2422 * Find file in buffer list by a regexp pattern.
2423 * Return fnum of the found buffer.
2424 * Return < 0 for error.
2425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002427buflist_findpat(
2428 char_u *pattern,
2429 char_u *pattern_end, /* pointer to first char after pattern */
2430 int unlisted, /* find unlisted buffers */
2431 int diffmode UNUSED, /* find diff-mode buffers only */
2432 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int match = -1;
2436 int find_listed;
2437 char_u *pat;
2438 char_u *patend;
2439 int attempt;
2440 char_u *p;
2441 int toggledollar;
2442
2443 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2444 {
2445 if (*pattern == '%')
2446 match = curbuf->b_fnum;
2447 else
2448 match = curwin->w_alt_fnum;
2449#ifdef FEAT_DIFF
2450 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2451 match = -1;
2452#endif
2453 }
2454
2455 /*
2456 * Try four ways of matching a listed buffer:
2457 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002458 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 * attempt == 2: with '$' at end (only match at end)
2460 * attempt == 3: with '^' at start and '$' at end (only full match)
2461 * Repeat this for finding an unlisted buffer if there was no matching
2462 * listed buffer.
2463 */
2464 else
2465 {
2466 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2467 if (pat == NULL)
2468 return -1;
2469 patend = pat + STRLEN(pat) - 1;
2470 toggledollar = (patend > pat && *patend == '$');
2471
2472 /* First try finding a listed buffer. If not found and "unlisted"
2473 * is TRUE, try finding an unlisted buffer. */
2474 find_listed = TRUE;
2475 for (;;)
2476 {
2477 for (attempt = 0; attempt <= 3; ++attempt)
2478 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002479 regmatch_T regmatch;
2480
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 /* may add '^' and '$' */
2482 if (toggledollar)
2483 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2484 p = pat;
2485 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2486 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002487 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2488 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 {
2490 vim_free(pat);
2491 return -1;
2492 }
2493
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002494 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 if (buf->b_p_bl == find_listed
2496#ifdef FEAT_DIFF
2497 && (!diffmode || diff_mode_buf(buf))
2498#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002499 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002501 if (curtab_only)
2502 {
2503 /* Ignore the match if the buffer is not open in
2504 * the current tab. */
2505#ifdef FEAT_WINDOWS
2506 win_T *wp;
2507
Bram Moolenaar29323592016-07-24 22:04:11 +02002508 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002509 if (wp->w_buffer == buf)
2510 break;
2511 if (wp == NULL)
2512 continue;
2513#else
2514 if (curwin->w_buffer != buf)
2515 continue;
2516#endif
2517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 if (match >= 0) /* already found a match */
2519 {
2520 match = -2;
2521 break;
2522 }
2523 match = buf->b_fnum; /* remember first match */
2524 }
2525
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002526 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (match >= 0) /* found one match */
2528 break;
2529 }
2530
2531 /* Only search for unlisted buffers if there was no match with
2532 * a listed buffer. */
2533 if (!unlisted || !find_listed || match != -1)
2534 break;
2535 find_listed = FALSE;
2536 }
2537
2538 vim_free(pat);
2539 }
2540
2541 if (match == -2)
2542 EMSG2(_("E93: More than one match for %s"), pattern);
2543 else if (match < 0)
2544 EMSG2(_("E94: No matching buffer for %s"), pattern);
2545 return match;
2546}
2547#endif
2548
2549#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2550
2551/*
2552 * Find all buffer names that match.
2553 * For command line expansion of ":buf" and ":sbuf".
2554 * Return OK if matches found, FAIL otherwise.
2555 */
2556 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002557ExpandBufnames(
2558 char_u *pat,
2559 int *num_file,
2560 char_u ***file,
2561 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562{
2563 int count = 0;
2564 buf_T *buf;
2565 int round;
2566 char_u *p;
2567 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002568 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 *num_file = 0; /* return values in case of FAIL */
2571 *file = NULL;
2572
Bram Moolenaar05159a02005-02-26 23:04:13 +00002573 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2574 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002576 patc = alloc((unsigned)STRLEN(pat) + 11);
2577 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002579 STRCPY(patc, "\\(^\\|[\\/]\\)");
2580 STRCPY(patc + 11, pat + 1);
2581 }
2582 else
2583 patc = pat;
2584
2585 /*
2586 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002587 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002588 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002589 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002590 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002591 regmatch_T regmatch;
2592
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002593 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002594 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002595 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2596 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002597 {
2598 if (patc != pat)
2599 vim_free(patc);
2600 return FAIL;
2601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602
2603 /*
2604 * round == 1: Count the matches.
2605 * round == 2: Build the array to keep the matches.
2606 */
2607 for (round = 1; round <= 2; ++round)
2608 {
2609 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002610 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
2612 if (!buf->b_p_bl) /* skip unlisted buffers */
2613 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002614 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 if (p != NULL)
2616 {
2617 if (round == 1)
2618 ++count;
2619 else
2620 {
2621 if (options & WILD_HOME_REPLACE)
2622 p = home_replace_save(buf, p);
2623 else
2624 p = vim_strsave(p);
2625 (*file)[count++] = p;
2626 }
2627 }
2628 }
2629 if (count == 0) /* no match found, break here */
2630 break;
2631 if (round == 1)
2632 {
2633 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2634 if (*file == NULL)
2635 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002636 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002637 if (patc != pat)
2638 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 return FAIL;
2640 }
2641 }
2642 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002643 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 if (count) /* match(es) found, break here */
2645 break;
2646 }
2647
Bram Moolenaar05159a02005-02-26 23:04:13 +00002648 if (patc != pat)
2649 vim_free(patc);
2650
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 *num_file = count;
2652 return (count == 0 ? FAIL : OK);
2653}
2654
2655#endif /* FEAT_CMDL_COMPL */
2656
2657#ifdef HAVE_BUFLIST_MATCH
2658/*
2659 * Check for a match on the file name for buffer "buf" with regprog "prog".
2660 */
2661 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002662buflist_match(
2663 regmatch_T *rmp,
2664 buf_T *buf,
2665 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666{
2667 char_u *match;
2668
2669 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002670 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002672 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
2674 return match;
2675}
2676
2677/*
2678 * Try matching the regexp in "prog" with file name "name".
2679 * Return "name" when there is a match, NULL when not.
2680 */
2681 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002682fname_match(
2683 regmatch_T *rmp,
2684 char_u *name,
2685 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686{
2687 char_u *match = NULL;
2688 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689
2690 if (name != NULL)
2691 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002692 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002693 rmp->rm_ic = p_fic || ignore_case;
2694 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 match = name;
2696 else
2697 {
2698 /* Replace $(HOME) with '~' and try matching again. */
2699 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002700 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 match = name;
2702 vim_free(p);
2703 }
2704 }
2705
2706 return match;
2707}
2708#endif
2709
2710/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002711 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 */
2713 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002714buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002716 char_u key[VIM_SIZEOF_INT * 2 + 1];
2717 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718
2719 if (nr == 0)
2720 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002721 sprintf((char *)key, "%x", nr);
2722 hi = hash_find(&buf_hashtab, key);
2723
2724 if (!HASHITEM_EMPTY(hi))
2725 return (buf_T *)(hi->hi_key
2726 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 return NULL;
2728}
2729
2730/*
2731 * Get name of file 'n' in the buffer list.
2732 * When the file has no name an empty string is returned.
2733 * home_replace() is used to shorten the file name (used for marks).
2734 * Returns a pointer to allocated memory, of NULL when failed.
2735 */
2736 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002737buflist_nr2name(
2738 int n,
2739 int fullname,
2740 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
2742 buf_T *buf;
2743
2744 buf = buflist_findnr(n);
2745 if (buf == NULL)
2746 return NULL;
2747 return home_replace_save(helptail ? buf : NULL,
2748 fullname ? buf->b_ffname : buf->b_fname);
2749}
2750
2751/*
2752 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2753 * When "copy_options" is TRUE save the local window option values.
2754 * When "lnum" is 0 only do the options.
2755 */
2756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002757buflist_setfpos(
2758 buf_T *buf,
2759 win_T *win,
2760 linenr_T lnum,
2761 colnr_T col,
2762 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
2764 wininfo_T *wip;
2765
2766 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2767 if (wip->wi_win == win)
2768 break;
2769 if (wip == NULL)
2770 {
2771 /* allocate a new entry */
2772 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2773 if (wip == NULL)
2774 return;
2775 wip->wi_win = win;
2776 if (lnum == 0) /* set lnum even when it's 0 */
2777 lnum = 1;
2778 }
2779 else
2780 {
2781 /* remove the entry from the list */
2782 if (wip->wi_prev)
2783 wip->wi_prev->wi_next = wip->wi_next;
2784 else
2785 buf->b_wininfo = wip->wi_next;
2786 if (wip->wi_next)
2787 wip->wi_next->wi_prev = wip->wi_prev;
2788 if (copy_options && wip->wi_optset)
2789 {
2790 clear_winopt(&wip->wi_opt);
2791#ifdef FEAT_FOLDING
2792 deleteFoldRecurse(&wip->wi_folds);
2793#endif
2794 }
2795 }
2796 if (lnum != 0)
2797 {
2798 wip->wi_fpos.lnum = lnum;
2799 wip->wi_fpos.col = col;
2800 }
2801 if (copy_options)
2802 {
2803 /* Save the window-specific option values. */
2804 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2805#ifdef FEAT_FOLDING
2806 wip->wi_fold_manual = win->w_fold_manual;
2807 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2808#endif
2809 wip->wi_optset = TRUE;
2810 }
2811
2812 /* insert the entry in front of the list */
2813 wip->wi_next = buf->b_wininfo;
2814 buf->b_wininfo = wip;
2815 wip->wi_prev = NULL;
2816 if (wip->wi_next)
2817 wip->wi_next->wi_prev = wip;
2818
2819 return;
2820}
2821
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002822#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002823static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002824
2825/*
2826 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2827 * page. That's because a diff is local to a tab page.
2828 */
2829 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002830wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002831{
2832 win_T *wp;
2833
2834 if (wip->wi_opt.wo_diff)
2835 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002836 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002837 /* return FALSE when it's a window in the current tab page, thus
2838 * the buffer was in diff mode here */
2839 if (wip->wi_win == wp)
2840 return FALSE;
2841 return TRUE;
2842 }
2843 return FALSE;
2844}
2845#endif
2846
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847/*
2848 * Find info for the current window in buffer "buf".
2849 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002850 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2851 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * Returns NULL when there isn't any info.
2853 */
2854 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002855find_wininfo(
2856 buf_T *buf,
2857 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858{
2859 wininfo_T *wip;
2860
2861 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002862 if (wip->wi_win == curwin
2863#ifdef FEAT_DIFF
2864 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2865#endif
2866 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002868
2869 /* If no wininfo for curwin, use the first in the list (that doesn't have
2870 * 'diff' set and is in another tab page). */
2871 if (wip == NULL)
2872 {
2873#ifdef FEAT_DIFF
2874 if (skip_diff_buffer)
2875 {
2876 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2877 if (!wininfo_other_tab_diff(wip))
2878 break;
2879 }
2880 else
2881#endif
2882 wip = buf->b_wininfo;
2883 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 return wip;
2885}
2886
2887/*
2888 * Reset the local window options to the values last used in this window.
2889 * If the buffer wasn't used in this window before, use the values from
2890 * the most recently used window. If the values were never set, use the
2891 * global values for the window.
2892 */
2893 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895{
2896 wininfo_T *wip;
2897
2898 clear_winopt(&curwin->w_onebuf_opt);
2899#ifdef FEAT_FOLDING
2900 clearFolding(curwin);
2901#endif
2902
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002903 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 if (wip != NULL && wip->wi_optset)
2905 {
2906 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2907#ifdef FEAT_FOLDING
2908 curwin->w_fold_manual = wip->wi_fold_manual;
2909 curwin->w_foldinvalid = TRUE;
2910 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2911#endif
2912 }
2913 else
2914 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2915
2916#ifdef FEAT_FOLDING
2917 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2918 if (p_fdls >= 0)
2919 curwin->w_p_fdl = p_fdls;
2920#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002921#ifdef FEAT_SYN_HL
2922 check_colorcolumn(curwin);
2923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924}
2925
2926/*
2927 * Find the position (lnum and col) for the buffer 'buf' for the current
2928 * window.
2929 * Returns a pointer to no_position if no position is found.
2930 */
2931 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
2934 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002935 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002937 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 if (wip != NULL)
2939 return &(wip->wi_fpos);
2940 else
2941 return &no_position;
2942}
2943
2944/*
2945 * Find the lnum for the buffer 'buf' for the current window.
2946 */
2947 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002948buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949{
2950 return buflist_findfpos(buf)->lnum;
2951}
2952
2953#if defined(FEAT_LISTCMDS) || defined(PROTO)
2954/*
2955 * List all know file names (for :files and :buffers command).
2956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002958buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959{
2960 buf_T *buf;
2961 int len;
2962 int i;
2963
2964 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2965 {
2966 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002967 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2968 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2969 || (vim_strchr(eap->arg, '+')
2970 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2971 || (vim_strchr(eap->arg, 'a')
2972 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2973 || (vim_strchr(eap->arg, 'h')
2974 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2975 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2976 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2977 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2978 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2979 || (vim_strchr(eap->arg, '#')
2980 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002983 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002984 else
2985 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002986 if (message_filtered(NameBuff))
2987 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002989 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00002990 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 buf->b_fnum,
2992 buf->b_p_bl ? ' ' : 'u',
2993 buf == curbuf ? '%' :
2994 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2995 buf->b_ml.ml_mfp == NULL ? ' ' :
2996 (buf->b_nwindows == 0 ? 'h' : 'a'),
2997 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2998 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002999 : (bufIsChanged(buf) ? '+' : ' '),
3000 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003001 if (len > IOSIZE - 20)
3002 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 i = 40 - vim_strsize(IObuff);
3006 do
3007 {
3008 IObuff[len++] = ' ';
3009 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003010 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3011 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003012 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 msg_outtrans(IObuff);
3014 out_flush(); /* output one line at a time */
3015 ui_breakcheck();
3016 }
3017}
3018#endif
3019
3020/*
3021 * Get file name and line number for file 'fnum'.
3022 * Used by DoOneCmd() for translating '%' and '#'.
3023 * Used by insert_reg() and cmdline_paste() for '#' register.
3024 * Return FAIL if not found, OK for success.
3025 */
3026 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003027buflist_name_nr(
3028 int fnum,
3029 char_u **fname,
3030 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 buf_T *buf;
3033
3034 buf = buflist_findnr(fnum);
3035 if (buf == NULL || buf->b_fname == NULL)
3036 return FAIL;
3037
3038 *fname = buf->b_fname;
3039 *lnum = buflist_findlnum(buf);
3040
3041 return OK;
3042}
3043
3044/*
3045 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3046 * The file name with the full path is also remembered, for when :cd is used.
3047 * Returns FAIL for failure (file name already in use by other buffer)
3048 * OK otherwise.
3049 */
3050 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003051setfname(
3052 buf_T *buf,
3053 char_u *ffname,
3054 char_u *sfname,
3055 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
Bram Moolenaar81695252004-12-29 20:58:21 +00003057 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003059 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060#endif
3061
3062 if (ffname == NULL || *ffname == NUL)
3063 {
3064 /* Removing the name. */
3065 vim_free(buf->b_ffname);
3066 vim_free(buf->b_sfname);
3067 buf->b_ffname = NULL;
3068 buf->b_sfname = NULL;
3069#ifdef UNIX
3070 st.st_dev = (dev_T)-1;
3071#endif
3072 }
3073 else
3074 {
3075 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3076 if (ffname == NULL) /* out of memory */
3077 return FAIL;
3078
3079 /*
3080 * if the file name is already used in another buffer:
3081 * - if the buffer is loaded, fail
3082 * - if the buffer is not loaded, delete it from the list
3083 */
3084#ifdef UNIX
3085 if (mch_stat((char *)ffname, &st) < 0)
3086 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003087#endif
3088 if (!(buf->b_flags & BF_DUMMY))
3089#ifdef UNIX
3090 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003092 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093#endif
3094 if (obuf != NULL && obuf != buf)
3095 {
3096 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3097 {
3098 if (message)
3099 EMSG(_("E95: Buffer with this name already exists"));
3100 vim_free(ffname);
3101 return FAIL;
3102 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003103 /* delete from the list */
3104 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106 sfname = vim_strsave(sfname);
3107 if (ffname == NULL || sfname == NULL)
3108 {
3109 vim_free(sfname);
3110 vim_free(ffname);
3111 return FAIL;
3112 }
3113#ifdef USE_FNAME_CASE
3114# ifdef USE_LONG_FNAME
3115 if (USE_LONG_FNAME)
3116# endif
3117 fname_case(sfname, 0); /* set correct case for short file name */
3118#endif
3119 vim_free(buf->b_ffname);
3120 vim_free(buf->b_sfname);
3121 buf->b_ffname = ffname;
3122 buf->b_sfname = sfname;
3123 }
3124 buf->b_fname = buf->b_sfname;
3125#ifdef UNIX
3126 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003127 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 else
3129 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003130 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 buf->b_dev = st.st_dev;
3132 buf->b_ino = st.st_ino;
3133 }
3134#endif
3135
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
3138 buf_name_changed(buf);
3139 return OK;
3140}
3141
3142/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003143 * Crude way of changing the name of a buffer. Use with care!
3144 * The name should be relative to the current directory.
3145 */
3146 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003147buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003148{
3149 buf_T *buf;
3150
3151 buf = buflist_findnr(fnum);
3152 if (buf != NULL)
3153 {
3154 vim_free(buf->b_sfname);
3155 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003156 buf->b_ffname = vim_strsave(name);
3157 buf->b_sfname = NULL;
3158 /* Allocate ffname and expand into full path. Also resolves .lnk
3159 * files on Win32. */
3160 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003161 buf->b_fname = buf->b_sfname;
3162 }
3163}
3164
3165/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 * Take care of what needs to be done when the name of buffer "buf" has
3167 * changed.
3168 */
3169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003170buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171{
3172 /*
3173 * If the file name changed, also change the name of the swapfile
3174 */
3175 if (buf->b_ml.ml_mfp != NULL)
3176 ml_setname(buf);
3177
3178 if (curwin->w_buffer == buf)
3179 check_arg_idx(curwin); /* check file name for arg list */
3180#ifdef FEAT_TITLE
3181 maketitle(); /* set window title */
3182#endif
3183#ifdef FEAT_WINDOWS
3184 status_redraw_all(); /* status lines need to be redrawn */
3185#endif
3186 fmarks_check_names(buf); /* check named file marks */
3187 ml_timestamp(buf); /* reset timestamp */
3188}
3189
3190/*
3191 * set alternate file name for current window
3192 *
3193 * Used by do_one_cmd(), do_write() and do_ecmd().
3194 * Return the buffer.
3195 */
3196 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003197setaltfname(
3198 char_u *ffname,
3199 char_u *sfname,
3200 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201{
3202 buf_T *buf;
3203
3204 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3205 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003206 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 curwin->w_alt_fnum = buf->b_fnum;
3208 return buf;
3209}
3210
3211/*
3212 * Get alternate file name for current window.
3213 * Return NULL if there isn't any, and give error message if requested.
3214 */
3215 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003216getaltfname(
3217 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218{
3219 char_u *fname;
3220 linenr_T dummy;
3221
3222 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3223 {
3224 if (errmsg)
3225 EMSG(_(e_noalt));
3226 return NULL;
3227 }
3228 return fname;
3229}
3230
3231/*
3232 * Add a file name to the buflist and return its number.
3233 * Uses same flags as buflist_new(), except BLN_DUMMY.
3234 *
3235 * used by qf_init(), main() and doarglist()
3236 */
3237 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239{
3240 buf_T *buf;
3241
3242 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3243 if (buf != NULL)
3244 return buf->b_fnum;
3245 return 0;
3246}
3247
3248#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3249/*
3250 * Adjust slashes in file names. Called after 'shellslash' was set.
3251 */
3252 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003253buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254{
3255 buf_T *bp;
3256
Bram Moolenaar29323592016-07-24 22:04:11 +02003257 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 {
3259 if (bp->b_ffname != NULL)
3260 slash_adjust(bp->b_ffname);
3261 if (bp->b_sfname != NULL)
3262 slash_adjust(bp->b_sfname);
3263 }
3264}
3265#endif
3266
3267/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003268 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 * Also save the local window option values.
3270 */
3271 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003272buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003274 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275}
3276
3277/*
3278 * Return TRUE if 'ffname' is not the same file as current file.
3279 * Fname must have a full path (expanded by mch_FullName()).
3280 */
3281 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003282otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283{
3284 return otherfile_buf(curbuf, ffname
3285#ifdef UNIX
3286 , NULL
3287#endif
3288 );
3289}
3290
3291 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003292otherfile_buf(
3293 buf_T *buf,
3294 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003296 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003299{
3300 /* no name is different */
3301 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3302 return TRUE;
3303 if (fnamecmp(ffname, buf->b_ffname) == 0)
3304 return FALSE;
3305#ifdef UNIX
3306 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003307 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308
Bram Moolenaar8767f522016-07-01 17:17:39 +02003309 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 if (stp == NULL)
3311 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003312 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 st.st_dev = (dev_T)-1;
3314 stp = &st;
3315 }
3316 /* Use dev/ino to check if the files are the same, even when the names
3317 * are different (possible with links). Still need to compare the
3318 * name above, for when the file doesn't exist yet.
3319 * Problem: The dev/ino changes when a file is deleted (and created
3320 * again) and remains the same when renamed/moved. We don't want to
3321 * mch_stat() each buffer each time, that would be too slow. Get the
3322 * dev/ino again when they appear to match, but not when they appear
3323 * to be different: Could skip a buffer when it's actually the same
3324 * file. */
3325 if (buf_same_ino(buf, stp))
3326 {
3327 buf_setino(buf);
3328 if (buf_same_ino(buf, stp))
3329 return FALSE;
3330 }
3331 }
3332#endif
3333 return TRUE;
3334}
3335
3336#if defined(UNIX) || defined(PROTO)
3337/*
3338 * Set inode and device number for a buffer.
3339 * Must always be called when b_fname is changed!.
3340 */
3341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003344 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345
3346 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3347 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003348 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 buf->b_dev = st.st_dev;
3350 buf->b_ino = st.st_ino;
3351 }
3352 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003353 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354}
3355
3356/*
3357 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3358 */
3359 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003360buf_same_ino(
3361 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003362 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003364 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 && stp->st_dev == buf->b_dev
3366 && stp->st_ino == buf->b_ino);
3367}
3368#endif
3369
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003370/*
3371 * Print info about the current buffer.
3372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374fileinfo(
3375 int fullname, /* when non-zero print full path */
3376 int shorthelp,
3377 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378{
3379 char_u *name;
3380 int n;
3381 char_u *p;
3382 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003383 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384
3385 buffer = alloc(IOSIZE);
3386 if (buffer == NULL)
3387 return;
3388
3389 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3390 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003391 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 p = buffer + STRLEN(buffer);
3393 }
3394 else
3395 p = buffer;
3396
3397 *p++ = '"';
3398 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003399 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 else
3401 {
3402 if (!fullname && curbuf->b_fname != NULL)
3403 name = curbuf->b_fname;
3404 else
3405 name = curbuf->b_ffname;
3406 home_replace(shorthelp ? curbuf : NULL, name, p,
3407 (int)(IOSIZE - (p - buffer)), TRUE);
3408 }
3409
Bram Moolenaara800b422010-06-27 01:15:55 +02003410 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 curbufIsChanged() ? (shortmess(SHM_MOD)
3412 ? " [+]" : _(" [Modified]")) : " ",
3413 (curbuf->b_flags & BF_NOTEDITED)
3414#ifdef FEAT_QUICKFIX
3415 && !bt_dontwrite(curbuf)
3416#endif
3417 ? _("[Not edited]") : "",
3418 (curbuf->b_flags & BF_NEW)
3419#ifdef FEAT_QUICKFIX
3420 && !bt_dontwrite(curbuf)
3421#endif
3422 ? _("[New file]") : "",
3423 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003424 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 : _("[readonly]")) : "",
3426 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3427 || curbuf->b_p_ro) ?
3428 " " : "");
3429 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3430 * causes an overflow, thus for large numbers divide instead. */
3431 if (curwin->w_cursor.lnum > 1000000L)
3432 n = (int)(((long)curwin->w_cursor.lnum) /
3433 ((long)curbuf->b_ml.ml_line_count / 100L));
3434 else
3435 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3436 (long)curbuf->b_ml.ml_line_count);
3437 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3438 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003439 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 }
3441#ifdef FEAT_CMDL_INFO
3442 else if (p_ru)
3443 {
3444 /* Current line and column are already on the screen -- webb */
3445 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003446 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003448 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 (long)curbuf->b_ml.ml_line_count, n);
3450 }
3451#endif
3452 else
3453 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003454 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 _("line %ld of %ld --%d%%-- col "),
3456 (long)curwin->w_cursor.lnum,
3457 (long)curbuf->b_ml.ml_line_count,
3458 n);
3459 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003460 len = STRLEN(buffer);
3461 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3463 }
3464
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003465 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
3467 if (dont_truncate)
3468 {
3469 /* Temporarily set msg_scroll to avoid the message being truncated.
3470 * First call msg_start() to get the message in the right place. */
3471 msg_start();
3472 n = msg_scroll;
3473 msg_scroll = TRUE;
3474 msg(buffer);
3475 msg_scroll = n;
3476 }
3477 else
3478 {
3479 p = msg_trunc_attr(buffer, FALSE, 0);
3480 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 /* Need to repeat the message after redrawing when:
3482 * - When restart_edit is set (otherwise there will be a delay
3483 * before redrawing).
3484 * - When the screen was scrolled but there is no wait-return
3485 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003486 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 }
3488
3489 vim_free(buffer);
3490}
3491
3492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003493col_print(
3494 char_u *buf,
3495 size_t buflen,
3496 int col,
3497 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498{
3499 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003500 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003502 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503}
3504
3505#if defined(FEAT_TITLE) || defined(PROTO)
3506/*
3507 * put file name in title bar of window and in icon title
3508 */
3509
3510static char_u *lasttitle = NULL;
3511static char_u *lasticon = NULL;
3512
3513 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003514maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515{
3516 char_u *p;
3517 char_u *t_str = NULL;
3518 char_u *i_name;
3519 char_u *i_str = NULL;
3520 int maxlen = 0;
3521 int len;
3522 int mustset;
3523 char_u buf[IOSIZE];
3524 int off;
3525
3526 if (!redrawing())
3527 {
3528 /* Postpone updating the title when 'lazyredraw' is set. */
3529 need_maketitle = TRUE;
3530 return;
3531 }
3532
3533 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003534 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 return;
3536
3537 if (p_title)
3538 {
3539 if (p_titlelen > 0)
3540 {
3541 maxlen = p_titlelen * Columns / 100;
3542 if (maxlen < 10)
3543 maxlen = 10;
3544 }
3545
3546 t_str = buf;
3547 if (*p_titlestring != NUL)
3548 {
3549#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003550 if (stl_syntax & STL_IN_TITLE)
3551 {
3552 int use_sandbox = FALSE;
3553 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003554
3555# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003556 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003557# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003558 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003560 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003561 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003562 if (called_emsg)
3563 set_string_option_direct((char_u *)"titlestring", -1,
3564 (char_u *)"", OPT_FREE, SID_ERROR);
3565 called_emsg |= save_called_emsg;
3566 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 else
3568#endif
3569 t_str = p_titlestring;
3570 }
3571 else
3572 {
3573 /* format: "fname + (path) (1 of 2) - VIM" */
3574
Bram Moolenaar2c666692012-09-05 13:30:40 +02003575#define SPACE_FOR_FNAME (IOSIZE - 100)
3576#define SPACE_FOR_DIR (IOSIZE - 20)
3577#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003579 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580 else
3581 {
3582 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003583 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 }
3586
3587 switch (bufIsChanged(curbuf)
3588 + (curbuf->b_p_ro * 2)
3589 + (!curbuf->b_p_ma * 4))
3590 {
3591 case 1: STRCAT(buf, " +"); break;
3592 case 2: STRCAT(buf, " ="); break;
3593 case 3: STRCAT(buf, " =+"); break;
3594 case 4:
3595 case 6: STRCAT(buf, " -"); break;
3596 case 5:
3597 case 7: STRCAT(buf, " -+"); break;
3598 }
3599
3600 if (curbuf->b_fname != NULL)
3601 {
3602 /* Get path of file, replace home dir with ~ */
3603 off = (int)STRLEN(buf);
3604 buf[off++] = ' ';
3605 buf[off++] = '(';
3606 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003607 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608#ifdef BACKSLASH_IN_FILENAME
3609 /* avoid "c:/name" to be reduced to "c" */
3610 if (isalpha(buf[off]) && buf[off + 1] == ':')
3611 off += 2;
3612#endif
3613 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003614 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003617 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003618 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003621
Bram Moolenaar2c666692012-09-05 13:30:40 +02003622 /* Translate unprintable chars and concatenate. Keep some
3623 * room for the server name. When there is no room (very long
3624 * file name) use (...). */
3625 if (off < SPACE_FOR_DIR)
3626 {
3627 p = transstr(buf + off);
3628 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3629 vim_free(p);
3630 }
3631 else
3632 {
3633 vim_strncpy(buf + off, (char_u *)"...",
3634 (size_t)(SPACE_FOR_ARGNR - off));
3635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 STRCAT(buf, ")");
3637 }
3638
Bram Moolenaar2c666692012-09-05 13:30:40 +02003639 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
3641#if defined(FEAT_CLIENTSERVER)
3642 if (serverName != NULL)
3643 {
3644 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003645 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 }
3647 else
3648#endif
3649 STRCAT(buf, " - VIM");
3650
3651 if (maxlen > 0)
3652 {
3653 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003654 if (vim_strsize(buf) > maxlen)
3655 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 }
3657 }
3658 }
3659 mustset = ti_change(t_str, &lasttitle);
3660
3661 if (p_icon)
3662 {
3663 i_str = buf;
3664 if (*p_iconstring != NUL)
3665 {
3666#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003667 if (stl_syntax & STL_IN_ICON)
3668 {
3669 int use_sandbox = FALSE;
3670 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003671
3672# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003673 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003674# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003675 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003677 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003678 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003679 if (called_emsg)
3680 set_string_option_direct((char_u *)"iconstring", -1,
3681 (char_u *)"", OPT_FREE, SID_ERROR);
3682 called_emsg |= save_called_emsg;
3683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 else
3685#endif
3686 i_str = p_iconstring;
3687 }
3688 else
3689 {
3690 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003691 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 else /* use file name only in icon */
3693 i_name = gettail(curbuf->b_ffname);
3694 *i_str = NUL;
3695 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003696 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (len > 100)
3698 {
3699 len -= 100;
3700#ifdef FEAT_MBYTE
3701 if (has_mbyte)
3702 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3703#endif
3704 i_name += len;
3705 }
3706 STRCPY(i_str, i_name);
3707 trans_characters(i_str, IOSIZE);
3708 }
3709 }
3710
3711 mustset |= ti_change(i_str, &lasticon);
3712
3713 if (mustset)
3714 resettitle();
3715}
3716
3717/*
3718 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3719 * from "str" if it does.
3720 * Return TRUE when "*last" changed.
3721 */
3722 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003723ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724{
3725 if ((str == NULL) != (*last == NULL)
3726 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3727 {
3728 vim_free(*last);
3729 if (str == NULL)
3730 *last = NULL;
3731 else
3732 *last = vim_strsave(str);
3733 return TRUE;
3734 }
3735 return FALSE;
3736}
3737
3738/*
3739 * Put current window title back (used after calling a shell)
3740 */
3741 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003742resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
3744 mch_settitle(lasttitle, lasticon);
3745}
Bram Moolenaarea408852005-06-25 22:49:46 +00003746
3747# if defined(EXITFREE) || defined(PROTO)
3748 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003749free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003750{
3751 vim_free(lasttitle);
3752 vim_free(lasticon);
3753}
3754# endif
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#endif /* FEAT_TITLE */
3757
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003758#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003760 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 * Return length of string in screen cells.
3762 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003763 * Normally works for window "wp", except when working for 'tabline' then it
3764 * is "curwin".
3765 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 * Items are drawn interspersed with the text that surrounds it
3767 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3768 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3769 *
3770 * If maxwidth is not zero, the string will be filled at any middle marker
3771 * or truncated if too long, fillchar is used for all whitespace.
3772 */
3773 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003774build_stl_str_hl(
3775 win_T *wp,
3776 char_u *out, /* buffer to write into != NameBuff */
3777 size_t outlen, /* length of out[] */
3778 char_u *fmt,
3779 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3780 int fillchar,
3781 int maxwidth,
3782 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3783 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784{
3785 char_u *p;
3786 char_u *s;
3787 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003788 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789#ifdef FEAT_EVAL
3790 win_T *o_curwin;
3791 buf_T *o_curbuf;
3792#endif
3793 int empty_line;
3794 colnr_T virtcol;
3795 long l;
3796 long n;
3797 int prevchar_isflag;
3798 int prevchar_isitem;
3799 int itemisflag;
3800 int fillable;
3801 char_u *str;
3802 long num;
3803 int width;
3804 int itemcnt;
3805 int curitem;
3806 int groupitem[STL_MAX_ITEM];
3807 int groupdepth;
3808 struct stl_item
3809 {
3810 char_u *start;
3811 int minwid;
3812 int maxwid;
3813 enum
3814 {
3815 Normal,
3816 Empty,
3817 Group,
3818 Middle,
3819 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003820 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 Trunc
3822 } type;
3823 } item[STL_MAX_ITEM];
3824 int minwid;
3825 int maxwid;
3826 int zeropad;
3827 char_u base;
3828 char_u opt;
3829#define TMPLEN 70
3830 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003831 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003832 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003833
3834#ifdef FEAT_EVAL
3835 /*
3836 * When the format starts with "%!" then evaluate it as an expression and
3837 * use the result as the actual format string.
3838 */
3839 if (fmt[0] == '%' && fmt[1] == '!')
3840 {
3841 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3842 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003843 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003844 }
3845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846
3847 if (fillchar == 0)
3848 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003849#ifdef FEAT_MBYTE
3850 /* Can't handle a multi-byte fill character yet. */
3851 else if (mb_char2len(fillchar) > 1)
3852 fillchar = '-';
3853#endif
3854
Bram Moolenaar567199b2013-04-24 16:52:36 +02003855 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3856 * p will become invalid when getting another buffer line. */
3857 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3858 empty_line = (*p == NUL);
3859
3860 /* Get the byte value now, in case we need it below. This is more
3861 * efficient than making a copy of the line. */
3862 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3863 byteval = 0;
3864 else
3865#ifdef FEAT_MBYTE
3866 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3867#else
3868 byteval = p[wp->w_cursor.col];
3869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870
3871 groupdepth = 0;
3872 p = out;
3873 curitem = 0;
3874 prevchar_isflag = TRUE;
3875 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003876 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003878 if (curitem == STL_MAX_ITEM)
3879 {
3880 /* There are too many items. Add the error code to the statusline
3881 * to give the user a hint about what went wrong. */
3882 if (p + 6 < out + outlen)
3883 {
3884 mch_memmove(p, " E541", (size_t)5);
3885 p += 5;
3886 }
3887 break;
3888 }
3889
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (*s != NUL && *s != '%')
3891 prevchar_isflag = prevchar_isitem = FALSE;
3892
3893 /*
3894 * Handle up to the next '%' or the end.
3895 */
3896 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3897 *p++ = *s++;
3898 if (*s == NUL || p + 1 >= out + outlen)
3899 break;
3900
3901 /*
3902 * Handle one '%' item.
3903 */
3904 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003905 if (*s == NUL) /* ignore trailing % */
3906 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (*s == '%')
3908 {
3909 if (p + 1 >= out + outlen)
3910 break;
3911 *p++ = *s++;
3912 prevchar_isflag = prevchar_isitem = FALSE;
3913 continue;
3914 }
3915 if (*s == STL_MIDDLEMARK)
3916 {
3917 s++;
3918 if (groupdepth > 0)
3919 continue;
3920 item[curitem].type = Middle;
3921 item[curitem++].start = p;
3922 continue;
3923 }
3924 if (*s == STL_TRUNCMARK)
3925 {
3926 s++;
3927 item[curitem].type = Trunc;
3928 item[curitem++].start = p;
3929 continue;
3930 }
3931 if (*s == ')')
3932 {
3933 s++;
3934 if (groupdepth < 1)
3935 continue;
3936 groupdepth--;
3937
3938 t = item[groupitem[groupdepth]].start;
3939 *p = NUL;
3940 l = vim_strsize(t);
3941 if (curitem > groupitem[groupdepth] + 1
3942 && item[groupitem[groupdepth]].minwid == 0)
3943 {
3944 /* remove group if all items are empty */
3945 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003946 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 break;
3948 if (n == curitem)
3949 {
3950 p = t;
3951 l = 0;
3952 }
3953 }
3954 if (l > item[groupitem[groupdepth]].maxwid)
3955 {
3956 /* truncate, remove n bytes of text at the start */
3957#ifdef FEAT_MBYTE
3958 if (has_mbyte)
3959 {
3960 /* Find the first character that should be included. */
3961 n = 0;
3962 while (l >= item[groupitem[groupdepth]].maxwid)
3963 {
3964 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003965 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 }
3967 }
3968 else
3969#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003970 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
3972 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003973 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 p = p - n + 1;
3975#ifdef FEAT_MBYTE
3976 /* Fill up space left over by half a double-wide char. */
3977 while (++l < item[groupitem[groupdepth]].minwid)
3978 *p++ = fillchar;
3979#endif
3980
3981 /* correct the start of the items for the truncation */
3982 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3983 {
3984 item[l].start -= n;
3985 if (item[l].start < t)
3986 item[l].start = t;
3987 }
3988 }
3989 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3990 {
3991 /* fill */
3992 n = item[groupitem[groupdepth]].minwid;
3993 if (n < 0)
3994 {
3995 /* fill by appending characters */
3996 n = 0 - n;
3997 while (l++ < n && p + 1 < out + outlen)
3998 *p++ = fillchar;
3999 }
4000 else
4001 {
4002 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004003 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 l = n - l;
4005 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004006 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 p += l;
4008 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4009 item[n].start += l;
4010 for ( ; l > 0; l--)
4011 *t++ = fillchar;
4012 }
4013 }
4014 continue;
4015 }
4016 minwid = 0;
4017 maxwid = 9999;
4018 zeropad = FALSE;
4019 l = 1;
4020 if (*s == '0')
4021 {
4022 s++;
4023 zeropad = TRUE;
4024 }
4025 if (*s == '-')
4026 {
4027 s++;
4028 l = -1;
4029 }
4030 if (VIM_ISDIGIT(*s))
4031 {
4032 minwid = (int)getdigits(&s);
4033 if (minwid < 0) /* overflow */
4034 minwid = 0;
4035 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004036 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 {
4038 item[curitem].type = Highlight;
4039 item[curitem].start = p;
4040 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4041 s++;
4042 curitem++;
4043 continue;
4044 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004045 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4046 {
4047 if (*s == STL_TABCLOSENR)
4048 {
4049 if (minwid == 0)
4050 {
4051 /* %X ends the close label, go back to the previously
4052 * define tab label nr. */
4053 for (n = curitem - 1; n >= 0; --n)
4054 if (item[n].type == TabPage && item[n].minwid >= 0)
4055 {
4056 minwid = item[n].minwid;
4057 break;
4058 }
4059 }
4060 else
4061 /* close nrs are stored as negative values */
4062 minwid = - minwid;
4063 }
4064 item[curitem].type = TabPage;
4065 item[curitem].start = p;
4066 item[curitem].minwid = minwid;
4067 s++;
4068 curitem++;
4069 continue;
4070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 if (*s == '.')
4072 {
4073 s++;
4074 if (VIM_ISDIGIT(*s))
4075 {
4076 maxwid = (int)getdigits(&s);
4077 if (maxwid <= 0) /* overflow */
4078 maxwid = 50;
4079 }
4080 }
4081 minwid = (minwid > 50 ? 50 : minwid) * l;
4082 if (*s == '(')
4083 {
4084 groupitem[groupdepth++] = curitem;
4085 item[curitem].type = Group;
4086 item[curitem].start = p;
4087 item[curitem].minwid = minwid;
4088 item[curitem].maxwid = maxwid;
4089 s++;
4090 curitem++;
4091 continue;
4092 }
4093 if (vim_strchr(STL_ALL, *s) == NULL)
4094 {
4095 s++;
4096 continue;
4097 }
4098 opt = *s++;
4099
4100 /* OK - now for the real work */
4101 base = 'D';
4102 itemisflag = FALSE;
4103 fillable = TRUE;
4104 num = -1;
4105 str = NULL;
4106 switch (opt)
4107 {
4108 case STL_FILEPATH:
4109 case STL_FULLPATH:
4110 case STL_FILENAME:
4111 fillable = FALSE; /* don't change ' ' to fillchar */
4112 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004113 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 else
4115 {
4116 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004117 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4119 }
4120 trans_characters(NameBuff, MAXPATHL);
4121 if (opt != STL_FILENAME)
4122 str = NameBuff;
4123 else
4124 str = gettail(NameBuff);
4125 break;
4126
4127 case STL_VIM_EXPR: /* '{' */
4128 itemisflag = TRUE;
4129 t = p;
4130 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4131 *p++ = *s++;
4132 if (*s != '}') /* missing '}' or out of space */
4133 break;
4134 s++;
4135 *p = 0;
4136 p = t;
4137
4138#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004139 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4141
4142 o_curbuf = curbuf;
4143 o_curwin = curwin;
4144 curwin = wp;
4145 curbuf = wp->w_buffer;
4146
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004147 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148
4149 curwin = o_curwin;
4150 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004151 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 if (str != NULL && *str != 0)
4154 {
4155 if (*skipdigits(str) == NUL)
4156 {
4157 num = atoi((char *)str);
4158 vim_free(str);
4159 str = NULL;
4160 itemisflag = FALSE;
4161 }
4162 }
4163#endif
4164 break;
4165
4166 case STL_LINE:
4167 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4168 ? 0L : (long)(wp->w_cursor.lnum);
4169 break;
4170
4171 case STL_NUMLINES:
4172 num = wp->w_buffer->b_ml.ml_line_count;
4173 break;
4174
4175 case STL_COLUMN:
4176 num = !(State & INSERT) && empty_line
4177 ? 0 : (int)wp->w_cursor.col + 1;
4178 break;
4179
4180 case STL_VIRTCOL:
4181 case STL_VIRTCOL_ALT:
4182 /* In list mode virtcol needs to be recomputed */
4183 virtcol = wp->w_virtcol;
4184 if (wp->w_p_list && lcs_tab1 == NUL)
4185 {
4186 wp->w_p_list = FALSE;
4187 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4188 wp->w_p_list = TRUE;
4189 }
4190 ++virtcol;
4191 /* Don't display %V if it's the same as %c. */
4192 if (opt == STL_VIRTCOL_ALT
4193 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4194 ? 0 : (int)wp->w_cursor.col + 1)))
4195 break;
4196 num = (long)virtcol;
4197 break;
4198
4199 case STL_PERCENTAGE:
4200 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4201 (long)wp->w_buffer->b_ml.ml_line_count);
4202 break;
4203
4204 case STL_ALTPERCENT:
4205 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004206 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 break;
4208
4209 case STL_ARGLISTSTAT:
4210 fillable = FALSE;
4211 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004212 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 str = tmp;
4214 break;
4215
4216 case STL_KEYMAP:
4217 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004218 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 str = tmp;
4220 break;
4221 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004222#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004223 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224#else
4225 num = 0;
4226#endif
4227 break;
4228
4229 case STL_BUFNO:
4230 num = wp->w_buffer->b_fnum;
4231 break;
4232
4233 case STL_OFFSET_X:
4234 base = 'X';
4235 case STL_OFFSET:
4236#ifdef FEAT_BYTEOFF
4237 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4238 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4239 0L : l + 1 + (!(State & INSERT) && empty_line ?
4240 0 : (int)wp->w_cursor.col);
4241#endif
4242 break;
4243
4244 case STL_BYTEVAL_X:
4245 base = 'X';
4246 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004247 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 if (num == NL)
4249 num = 0;
4250 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4251 num = NL;
4252 break;
4253
4254 case STL_ROFLAG:
4255 case STL_ROFLAG_ALT:
4256 itemisflag = TRUE;
4257 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004258 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 break;
4260
4261 case STL_HELPFLAG:
4262 case STL_HELPFLAG_ALT:
4263 itemisflag = TRUE;
4264 if (wp->w_buffer->b_help)
4265 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004266 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 break;
4268
4269#ifdef FEAT_AUTOCMD
4270 case STL_FILETYPE:
4271 if (*wp->w_buffer->b_p_ft != NUL
4272 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4273 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004274 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4275 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 str = tmp;
4277 }
4278 break;
4279
4280 case STL_FILETYPE_ALT:
4281 itemisflag = TRUE;
4282 if (*wp->w_buffer->b_p_ft != NUL
4283 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4284 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004285 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4286 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 for (t = tmp; *t != 0; t++)
4288 *t = TOUPPER_LOC(*t);
4289 str = tmp;
4290 }
4291 break;
4292#endif
4293
4294#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4295 case STL_PREVIEWFLAG:
4296 case STL_PREVIEWFLAG_ALT:
4297 itemisflag = TRUE;
4298 if (wp->w_p_pvw)
4299 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4300 : _("[Preview]"));
4301 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004302
4303 case STL_QUICKFIX:
4304 if (bt_quickfix(wp->w_buffer))
4305 str = (char_u *)(wp->w_llist_ref
4306 ? _(msg_loclist)
4307 : _(msg_qflist));
4308 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309#endif
4310
4311 case STL_MODIFIED:
4312 case STL_MODIFIED_ALT:
4313 itemisflag = TRUE;
4314 switch ((opt == STL_MODIFIED_ALT)
4315 + bufIsChanged(wp->w_buffer) * 2
4316 + (!wp->w_buffer->b_p_ma) * 4)
4317 {
4318 case 2: str = (char_u *)"[+]"; break;
4319 case 3: str = (char_u *)",+"; break;
4320 case 4: str = (char_u *)"[-]"; break;
4321 case 5: str = (char_u *)",-"; break;
4322 case 6: str = (char_u *)"[+-]"; break;
4323 case 7: str = (char_u *)",+-"; break;
4324 }
4325 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004326
4327 case STL_HIGHLIGHT:
4328 t = s;
4329 while (*s != '#' && *s != NUL)
4330 ++s;
4331 if (*s == '#')
4332 {
4333 item[curitem].type = Highlight;
4334 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004335 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004336 curitem++;
4337 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004338 if (*s != NUL)
4339 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004340 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 }
4342
4343 item[curitem].start = p;
4344 item[curitem].type = Normal;
4345 if (str != NULL && *str)
4346 {
4347 t = str;
4348 if (itemisflag)
4349 {
4350 if ((t[0] && t[1])
4351 && ((!prevchar_isitem && *t == ',')
4352 || (prevchar_isflag && *t == ' ')))
4353 t++;
4354 prevchar_isflag = TRUE;
4355 }
4356 l = vim_strsize(t);
4357 if (l > 0)
4358 prevchar_isitem = TRUE;
4359 if (l > maxwid)
4360 {
4361 while (l >= maxwid)
4362#ifdef FEAT_MBYTE
4363 if (has_mbyte)
4364 {
4365 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004366 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 }
4368 else
4369#endif
4370 l -= byte2cells(*t++);
4371 if (p + 1 >= out + outlen)
4372 break;
4373 *p++ = '<';
4374 }
4375 if (minwid > 0)
4376 {
4377 for (; l < minwid && p + 1 < out + outlen; l++)
4378 {
4379 /* Don't put a "-" in front of a digit. */
4380 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4381 *p++ = ' ';
4382 else
4383 *p++ = fillchar;
4384 }
4385 minwid = 0;
4386 }
4387 else
4388 minwid *= -1;
4389 while (*t && p + 1 < out + outlen)
4390 {
4391 *p++ = *t++;
4392 /* Change a space by fillchar, unless fillchar is '-' and a
4393 * digit follows. */
4394 if (fillable && p[-1] == ' '
4395 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4396 p[-1] = fillchar;
4397 }
4398 for (; l < minwid && p + 1 < out + outlen; l++)
4399 *p++ = fillchar;
4400 }
4401 else if (num >= 0)
4402 {
4403 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4404 char_u nstr[20];
4405
4406 if (p + 20 >= out + outlen)
4407 break; /* not sufficient space */
4408 prevchar_isitem = TRUE;
4409 t = nstr;
4410 if (opt == STL_VIRTCOL_ALT)
4411 {
4412 *t++ = '-';
4413 minwid--;
4414 }
4415 *t++ = '%';
4416 if (zeropad)
4417 *t++ = '0';
4418 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004419 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 *t = 0;
4421
4422 for (n = num, l = 1; n >= nbase; n /= nbase)
4423 l++;
4424 if (opt == STL_VIRTCOL_ALT)
4425 l++;
4426 if (l > maxwid)
4427 {
4428 l += 2;
4429 n = l - maxwid;
4430 while (l-- > maxwid)
4431 num /= nbase;
4432 *t++ = '>';
4433 *t++ = '%';
4434 *t = t[-3];
4435 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004436 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4437 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 }
4439 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004440 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4441 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 p += STRLEN(p);
4443 }
4444 else
4445 item[curitem].type = Empty;
4446
4447 if (opt == STL_VIM_EXPR)
4448 vim_free(str);
4449
4450 if (num >= 0 || (!itemisflag && str && *str))
4451 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4452 curitem++;
4453 }
4454 *p = NUL;
4455 itemcnt = curitem;
4456
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004457#ifdef FEAT_EVAL
4458 if (usefmt != fmt)
4459 vim_free(usefmt);
4460#endif
4461
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 width = vim_strsize(out);
4463 if (maxwidth > 0 && width > maxwidth)
4464 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004465 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 l = 0;
4467 if (itemcnt == 0)
4468 s = out;
4469 else
4470 {
4471 for ( ; l < itemcnt; l++)
4472 if (item[l].type == Trunc)
4473 {
4474 /* Truncate at %< item. */
4475 s = item[l].start;
4476 break;
4477 }
4478 if (l == itemcnt)
4479 {
4480 /* No %< item, truncate first item. */
4481 s = item[0].start;
4482 l = 0;
4483 }
4484 }
4485
4486 if (width - vim_strsize(s) >= maxwidth)
4487 {
4488 /* Truncation mark is beyond max length */
4489#ifdef FEAT_MBYTE
4490 if (has_mbyte)
4491 {
4492 s = out;
4493 width = 0;
4494 for (;;)
4495 {
4496 width += ptr2cells(s);
4497 if (width >= maxwidth)
4498 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004499 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 /* Fill up for half a double-wide character. */
4502 while (++width < maxwidth)
4503 *s++ = fillchar;
4504 }
4505 else
4506#endif
4507 s = out + maxwidth - 1;
4508 for (l = 0; l < itemcnt; l++)
4509 if (item[l].start > s)
4510 break;
4511 itemcnt = l;
4512 *s++ = '>';
4513 *s = 0;
4514 }
4515 else
4516 {
4517#ifdef FEAT_MBYTE
4518 if (has_mbyte)
4519 {
4520 n = 0;
4521 while (width >= maxwidth)
4522 {
4523 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004524 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526 }
4527 else
4528#endif
4529 n = width - maxwidth + 1;
4530 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004531 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 *s = '<';
4533
4534 /* Fill up for half a double-wide character. */
4535 while (++width < maxwidth)
4536 {
4537 s = s + STRLEN(s);
4538 *s++ = fillchar;
4539 *s = NUL;
4540 }
4541
4542 --n; /* count the '<' */
4543 for (; l < itemcnt; l++)
4544 {
4545 if (item[l].start - n >= s)
4546 item[l].start -= n;
4547 else
4548 item[l].start = s;
4549 }
4550 }
4551 width = maxwidth;
4552 }
4553 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4554 {
4555 /* Apply STL_MIDDLE if any */
4556 for (l = 0; l < itemcnt; l++)
4557 if (item[l].type == Middle)
4558 break;
4559 if (l < itemcnt)
4560 {
4561 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004562 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 for (s = item[l].start; s < p; s++)
4564 *s = fillchar;
4565 for (l++; l < itemcnt; l++)
4566 item[l].start += maxwidth - width;
4567 width = maxwidth;
4568 }
4569 }
4570
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004571 /* Store the info about highlighting. */
4572 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004574 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 for (l = 0; l < itemcnt; l++)
4576 {
4577 if (item[l].type == Highlight)
4578 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004579 sp->start = item[l].start;
4580 sp->userhl = item[l].minwid;
4581 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004584 sp->start = NULL;
4585 sp->userhl = 0;
4586 }
4587
4588 /* Store the info about tab pages labels. */
4589 if (tabtab != NULL)
4590 {
4591 sp = tabtab;
4592 for (l = 0; l < itemcnt; l++)
4593 {
4594 if (item[l].type == TabPage)
4595 {
4596 sp->start = item[l].start;
4597 sp->userhl = item[l].minwid;
4598 sp++;
4599 }
4600 }
4601 sp->start = NULL;
4602 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
4604
4605 return width;
4606}
4607#endif /* FEAT_STL_OPT */
4608
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004609#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4610 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004612 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4613 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 */
4615 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004616get_rel_pos(
4617 win_T *wp,
4618 char_u *buf,
4619 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620{
4621 long above; /* number of lines above window */
4622 long below; /* number of lines below window */
4623
Bram Moolenaar0027c212015-01-07 13:31:52 +01004624 if (buflen < 3) /* need at least 3 chars for writing */
4625 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 above = wp->w_topline - 1;
4627#ifdef FEAT_DIFF
4628 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004629 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4630 above = 0; /* All buffer lines are displayed and there is an
4631 * indication of filler lines, that can be considered
4632 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633#endif
4634 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4635 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004636 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4637 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004639 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004641 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 ? (int)(above / ((above + below) / 100L))
4643 : (int)(above * 100L / (above + below)));
4644}
4645#endif
4646
4647/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004648 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 * Return TRUE if it was appended.
4650 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004652append_arg_number(
4653 win_T *wp,
4654 char_u *buf,
4655 int buflen,
4656 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658 char_u *p;
4659
4660 if (ARGCOUNT <= 1) /* nothing to do */
4661 return FALSE;
4662
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004663 p = buf + STRLEN(buf); /* go to the end of the buffer */
4664 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 return FALSE;
4666 *p++ = ' ';
4667 *p++ = '(';
4668 if (add_file)
4669 {
4670 STRCPY(p, "file ");
4671 p += 5;
4672 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004673 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4674 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4676 return TRUE;
4677}
4678
4679/*
4680 * If fname is not a full path, make it a full path.
4681 * Returns pointer to allocated memory (NULL for failure).
4682 */
4683 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004684fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685{
4686 /*
4687 * Force expanding the path always for Unix, because symbolic links may
4688 * mess up the full path name, even though it starts with a '/'.
4689 * Also expand when there is ".." in the file name, try to remove it,
4690 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004691 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 * For MS-Windows also expand names like "longna~1" to "longname".
4693 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004694#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 return FullName_save(fname, TRUE);
4696#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004697 if (!vim_isAbsName(fname)
4698 || strstr((char *)fname, "..") != NULL
4699 || strstr((char *)fname, "//") != NULL
4700# ifdef BACKSLASH_IN_FILENAME
4701 || strstr((char *)fname, "\\\\") != NULL
4702# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004703# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004705# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 )
4707 return FullName_save(fname, FALSE);
4708
4709 fname = vim_strsave(fname);
4710
Bram Moolenaar9b942202007-10-03 12:31:33 +00004711# ifdef USE_FNAME_CASE
4712# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 {
4716 if (fname != NULL)
4717 fname_case(fname, 0); /* set correct case for file name */
4718 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004719# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
4721 return fname;
4722#endif
4723}
4724
4725/*
4726 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4727 * "ffname" becomes a pointer to allocated memory (or NULL).
4728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004730fname_expand(
4731 buf_T *buf UNUSED,
4732 char_u **ffname,
4733 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734{
4735 if (*ffname == NULL) /* if no file name given, nothing to do */
4736 return;
4737 if (*sfname == NULL) /* if no short file name given, use ffname */
4738 *sfname = *ffname;
4739 *ffname = fix_fname(*ffname); /* expand to full path */
4740
4741#ifdef FEAT_SHORTCUT
4742 if (!buf->b_p_bin)
4743 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004744 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745
4746 /* If the file name is a shortcut file, use the file it links to. */
4747 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004748 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
4750 vim_free(*ffname);
4751 *ffname = rfname;
4752 *sfname = rfname;
4753 }
4754 }
4755#endif
4756}
4757
4758/*
4759 * Get the file name for an argument list entry.
4760 */
4761 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004762alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763{
4764 buf_T *bp;
4765
4766 /* Use the name from the associated buffer if it exists. */
4767 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004768 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 return aep->ae_fname;
4770 return bp->b_fname;
4771}
4772
4773#if defined(FEAT_WINDOWS) || defined(PROTO)
4774/*
4775 * do_arg_all(): Open up to 'count' windows, one for each argument.
4776 */
4777 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004778do_arg_all(
4779 int count,
4780 int forceit, /* hide buffers in current windows */
4781 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782{
4783 int i;
4784 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004785 char_u *opened; /* Array of weight for which args are open:
4786 * 0: not opened
4787 * 1: opened in other tab
4788 * 2: opened in curtab
4789 * 3: opened in curtab and curwin
4790 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004791 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 int use_firstwin = FALSE; /* use first window for arglist */
4793 int split_ret = OK;
4794 int p_ea_save;
4795 alist_T *alist; /* argument list to be used */
4796 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004797 tabpage_T *tpnext;
4798 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004799 win_T *old_curwin, *last_curwin;
4800 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004801 win_T *new_curwin = NULL;
4802 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803
4804 if (ARGCOUNT <= 0)
4805 {
4806 /* Don't give an error message. We don't want it when the ":all"
4807 * command is in the .vimrc. */
4808 return;
4809 }
4810 setpcmark();
4811
4812 opened_len = ARGCOUNT;
4813 opened = alloc_clear((unsigned)opened_len);
4814 if (opened == NULL)
4815 return;
4816
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004817 /* Autocommands may do anything to the argument list. Make sure it's not
4818 * freed while we are working here by "locking" it. We still have to
4819 * watch out for its size to be changed. */
4820 alist = curwin->w_alist;
4821 ++alist->al_refcount;
4822
4823 old_curwin = curwin;
4824 old_curtab = curtab;
4825
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004826# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004828# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829
4830 /*
4831 * Try closing all windows that are not in the argument list.
4832 * Also close windows that are not full width;
4833 * When 'hidden' or "forceit" set the buffer becomes hidden.
4834 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004835 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004837 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004838 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004839 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004841 tpnext = curtab->tp_next;
4842 for (wp = firstwin; wp != NULL; wp = wpnext)
4843 {
4844 wpnext = wp->w_next;
4845 buf = wp->w_buffer;
4846 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004847 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004848 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004849 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004850 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004852 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004853 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004855 if (i < alist->al_ga.ga_len
4856 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4857 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4858 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004860 int weight = 1;
4861
4862 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004863 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004864 ++weight;
4865 if (old_curwin == wp)
4866 ++weight;
4867 }
4868
4869 if (weight > (int)opened[i])
4870 {
4871 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004872 if (i == 0)
4873 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004874 if (new_curwin != NULL)
4875 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004876 new_curwin = wp;
4877 new_curtab = curtab;
4878 }
4879 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004880 else if (keep_tabs)
4881 i = opened_len;
4882
4883 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004884 {
4885 /* Use the current argument list for all windows
4886 * containing a file from it. */
4887 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004888 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004889 ++wp->w_alist->al_refcount;
4890 }
4891 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
4894 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004895 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004897 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004899 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004900 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004902 /* If the buffer was changed, and we would like to hide it,
4903 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004904 if (!P_HID(buf) && buf->b_nwindows <= 1
4905 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004907#ifdef FEAT_AUTOCMD
4908 bufref_T bufref;
4909
4910 set_bufref(&bufref, buf);
4911#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004912 (void)autowrite(buf, FALSE);
4913#ifdef FEAT_AUTOCMD
4914 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004915 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004916 {
4917 wpnext = firstwin; /* start all over... */
4918 continue;
4919 }
4920#endif
4921 }
4922#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004923 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004924 if (firstwin == lastwin
4925 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004926#endif
4927 use_firstwin = TRUE;
4928#ifdef FEAT_WINDOWS
4929 else
4930 {
4931 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4932# ifdef FEAT_AUTOCMD
4933 /* check if autocommands removed the next window */
4934 if (!win_valid(wpnext))
4935 wpnext = firstwin; /* start all over... */
4936# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938#endif
4939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
4941 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004942
4943 /* Without the ":tab" modifier only do the current tab page. */
4944 if (had_tab == 0 || tpnext == NULL)
4945 break;
4946
4947# ifdef FEAT_AUTOCMD
4948 /* check if autocommands removed the next tab page */
4949 if (!valid_tabpage(tpnext))
4950 tpnext = first_tabpage; /* start all over...*/
4951# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004952 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 }
4954
4955 /*
4956 * Open a window for files in the argument list that don't have one.
4957 * ARGCOUNT may change while doing this, because of autocommands.
4958 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004959 if (count > opened_len || count <= 0)
4960 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962#ifdef FEAT_AUTOCMD
4963 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4964 ++autocmd_no_enter;
4965 ++autocmd_no_leave;
4966#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004967 last_curwin = curwin;
4968 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004970#ifdef FEAT_WINDOWS
4971 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4972 * leaving an empty tab page when executed locally. */
4973 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4974 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4975 use_firstwin = TRUE;
4976#endif
4977
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004978 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
4980 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4981 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004982 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 /* Move the already present window to below the current window */
4985 if (curwin->w_arg_idx != i)
4986 {
4987 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4988 {
4989 if (wpnext->w_arg_idx == i)
4990 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004991 if (keep_tabs)
4992 {
4993 new_curwin = wpnext;
4994 new_curtab = curtab;
4995 }
4996 else
4997 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 break;
4999 }
5000 }
5001 }
5002 }
5003 else if (split_ret == OK)
5004 {
5005 if (!use_firstwin) /* split current window */
5006 {
5007 p_ea_save = p_ea;
5008 p_ea = TRUE; /* use space from all windows */
5009 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5010 p_ea = p_ea_save;
5011 if (split_ret == FAIL)
5012 continue;
5013 }
5014#ifdef FEAT_AUTOCMD
5015 else /* first window: do autocmd for leaving this buffer */
5016 --autocmd_no_leave;
5017#endif
5018
5019 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005020 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 */
5022 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005023 if (i == 0)
5024 {
5025 new_curwin = curwin;
5026 new_curtab = curtab;
5027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5029 ECMD_ONE,
5030 ((P_HID(curwin->w_buffer)
5031 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005032 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033#ifdef FEAT_AUTOCMD
5034 if (use_firstwin)
5035 ++autocmd_no_leave;
5036#endif
5037 use_firstwin = FALSE;
5038 }
5039 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005040
5041 /* When ":tab" was used open a new tab for a new window repeatedly. */
5042 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5043 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045
5046 /* Remove the "lock" on the argument list. */
5047 alist_unlink(alist);
5048
5049#ifdef FEAT_AUTOCMD
5050 --autocmd_no_enter;
5051#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005052 /* restore last referenced tabpage's curwin */
5053 if (last_curtab != new_curtab)
5054 {
5055 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005056 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005057 if (win_valid(last_curwin))
5058 win_enter(last_curwin, FALSE);
5059 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005060 /* to window with first arg */
5061 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005062 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005063 if (win_valid(new_curwin))
5064 win_enter(new_curwin, FALSE);
5065
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066#ifdef FEAT_AUTOCMD
5067 --autocmd_no_leave;
5068#endif
5069 vim_free(opened);
5070}
5071
5072# if defined(FEAT_LISTCMDS) || defined(PROTO)
5073/*
5074 * Open a window for a number of buffers.
5075 */
5076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005077ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
5079 buf_T *buf;
5080 win_T *wp, *wpnext;
5081 int split_ret = OK;
5082 int p_ea_save;
5083 int open_wins = 0;
5084 int r;
5085 int count; /* Maximum number of windows to open. */
5086 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005087#ifdef FEAT_WINDOWS
5088 int had_tab = cmdmod.tab;
5089 tabpage_T *tpnext;
5090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
5092 if (eap->addr_count == 0) /* make as many windows as possible */
5093 count = 9999;
5094 else
5095 count = eap->line2; /* make as many windows as specified */
5096 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5097 all = FALSE;
5098 else
5099 all = TRUE;
5100
5101 setpcmark();
5102
5103#ifdef FEAT_GUI
5104 need_mouse_correct = TRUE;
5105#endif
5106
5107 /*
5108 * Close superfluous windows (two windows for the same buffer).
5109 * Also close windows that are not full-width.
5110 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005111#ifdef FEAT_WINDOWS
5112 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005113 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005114 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005117 tpnext = curtab->tp_next;
5118 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005120 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005121 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005122#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005123 || ((cmdmod.split & WSP_VERT)
5124 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005125 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005126 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005127 || (had_tab > 0 && wp != firstwin)
5128#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02005129 ) && firstwin != lastwin
5130#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005131 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005132#endif
5133 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005134 {
5135 win_close(wp, FALSE);
5136#ifdef FEAT_AUTOCMD
5137 wpnext = firstwin; /* just in case an autocommand does
5138 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005139 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005140 open_wins = 0;
5141#endif
5142 }
5143 else
5144 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005146
5147#ifdef FEAT_WINDOWS
5148 /* Without the ":tab" modifier only do the current tab page. */
5149 if (had_tab == 0 || tpnext == NULL)
5150 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005151 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005153#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154
5155 /*
5156 * Go through the buffer list. When a buffer doesn't have a window yet,
5157 * open one. Otherwise move the window to the right position.
5158 * Watch out for autocommands that delete buffers or windows!
5159 */
5160#ifdef FEAT_AUTOCMD
5161 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5162 ++autocmd_no_enter;
5163#endif
5164 win_enter(lastwin, FALSE);
5165#ifdef FEAT_AUTOCMD
5166 ++autocmd_no_leave;
5167#endif
5168 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5169 {
5170 /* Check if this buffer needs a window */
5171 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5172 continue;
5173
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005174#ifdef FEAT_WINDOWS
5175 if (had_tab != 0)
5176 {
5177 /* With the ":tab" modifier don't move the window. */
5178 if (buf->b_nwindows > 0)
5179 wp = lastwin; /* buffer has a window, skip it */
5180 else
5181 wp = NULL;
5182 }
5183 else
5184#endif
5185 {
5186 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005187 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005188 if (wp->w_buffer == buf)
5189 break;
5190 /* If the buffer already has a window, move it */
5191 if (wp != NULL)
5192 win_move_after(wp, curwin);
5193 }
5194
5195 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005197#ifdef FEAT_AUTOCMD
5198 bufref_T bufref;
5199
5200 set_bufref(&bufref, buf);
5201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 /* Split the window and put the buffer in it */
5203 p_ea_save = p_ea;
5204 p_ea = TRUE; /* use space from all windows */
5205 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5206 ++open_wins;
5207 p_ea = p_ea_save;
5208 if (split_ret == FAIL)
5209 continue;
5210
5211 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005212#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 swap_exists_action = SEA_DIALOG;
5214#endif
5215 set_curbuf(buf, DOBUF_GOTO);
5216#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005217 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005219 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005220#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005222# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 break;
5224 }
5225#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005226#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 if (swap_exists_action == SEA_QUIT)
5228 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005229# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5230 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005231
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005232 /* Reset the error/interrupt/exception state here so that
5233 * aborting() returns FALSE when closing a window. */
5234 enter_cleanup(&cs);
5235# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005236
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005237 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 win_close(curwin, TRUE);
5239 --open_wins;
5240 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005241 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005242
5243# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5244 /* Restore the error/interrupt/exception state if not
5245 * discarded by a new aborting error, interrupt, or uncaught
5246 * exception. */
5247 leave_cleanup(&cs);
5248# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 else
5251 handle_swap_exists(NULL);
5252#endif
5253 }
5254
5255 ui_breakcheck();
5256 if (got_int)
5257 {
5258 (void)vgetc(); /* only break the file loading, not the rest */
5259 break;
5260 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005261#ifdef FEAT_EVAL
5262 /* Autocommands deleted the buffer or aborted script processing!!! */
5263 if (aborting())
5264 break;
5265#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005266#ifdef FEAT_WINDOWS
5267 /* When ":tab" was used open a new tab for a new window repeatedly. */
5268 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5269 cmdmod.tab = 9999;
5270#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
5272#ifdef FEAT_AUTOCMD
5273 --autocmd_no_enter;
5274#endif
5275 win_enter(firstwin, FALSE); /* back to first window */
5276#ifdef FEAT_AUTOCMD
5277 --autocmd_no_leave;
5278#endif
5279
5280 /*
5281 * Close superfluous windows.
5282 */
5283 for (wp = lastwin; open_wins > count; )
5284 {
5285 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5286 || autowrite(wp->w_buffer, FALSE) == OK);
5287#ifdef FEAT_AUTOCMD
5288 if (!win_valid(wp))
5289 {
5290 /* BufWrite Autocommands made the window invalid, start over */
5291 wp = lastwin;
5292 }
5293 else
5294#endif
5295 if (r)
5296 {
5297 win_close(wp, !P_HID(wp->w_buffer));
5298 --open_wins;
5299 wp = lastwin;
5300 }
5301 else
5302 {
5303 wp = wp->w_prev;
5304 if (wp == NULL)
5305 break;
5306 }
5307 }
5308}
5309# endif /* FEAT_LISTCMDS */
5310
5311#endif /* FEAT_WINDOWS */
5312
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005313static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005314
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315/*
5316 * do_modelines() - process mode lines for the current file
5317 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005318 * "flags" can be:
5319 * OPT_WINONLY only set options local to window
5320 * OPT_NOWIN don't set options local to window
5321 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 * Returns immediately if the "ml" option isn't set.
5323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005325do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005327 linenr_T lnum;
5328 int nmlines;
5329 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330
5331 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5332 return;
5333
5334 /* Disallow recursive entry here. Can happen when executing a modeline
5335 * triggers an autocommand, which reloads modelines with a ":do". */
5336 if (entered)
5337 return;
5338
5339 ++entered;
5340 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5341 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005342 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 nmlines = 0;
5344
5345 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5346 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005347 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 nmlines = 0;
5349 --entered;
5350}
5351
5352#include "version.h" /* for version number */
5353
5354/*
5355 * chk_modeline() - check a single line for a mode string
5356 * Return FAIL if an error encountered.
5357 */
5358 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005359chk_modeline(
5360 linenr_T lnum,
5361 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362{
5363 char_u *s;
5364 char_u *e;
5365 char_u *linecopy; /* local copy of any modeline found */
5366 int prev;
5367 int vers;
5368 int end;
5369 int retval = OK;
5370 char_u *save_sourcing_name;
5371 linenr_T save_sourcing_lnum;
5372#ifdef FEAT_EVAL
5373 scid_T save_SID;
5374#endif
5375
5376 prev = -1;
5377 for (s = ml_get(lnum); *s != NUL; ++s)
5378 {
5379 if (prev == -1 || vim_isspace(prev))
5380 {
5381 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5382 || STRNCMP(s, "vi:", (size_t)3) == 0)
5383 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005384 /* Accept both "vim" and "Vim". */
5385 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
5387 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5388 e = s + 4;
5389 else
5390 e = s + 3;
5391 vers = getdigits(&e);
5392 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005393 && (s[0] != 'V'
5394 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 && (s[3] == ':'
5396 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5397 || (VIM_VERSION_100 < vers && s[3] == '<')
5398 || (VIM_VERSION_100 > vers && s[3] == '>')
5399 || (VIM_VERSION_100 == vers && s[3] == '=')))
5400 break;
5401 }
5402 }
5403 prev = *s;
5404 }
5405
5406 if (*s)
5407 {
5408 do /* skip over "ex:", "vi:" or "vim:" */
5409 ++s;
5410 while (s[-1] != ':');
5411
5412 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5413 if (linecopy == NULL)
5414 return FAIL;
5415
5416 save_sourcing_lnum = sourcing_lnum;
5417 save_sourcing_name = sourcing_name;
5418 sourcing_lnum = lnum; /* prepare for emsg() */
5419 sourcing_name = (char_u *)"modelines";
5420
5421 end = FALSE;
5422 while (end == FALSE)
5423 {
5424 s = skipwhite(s);
5425 if (*s == NUL)
5426 break;
5427
5428 /*
5429 * Find end of set command: ':' or end of line.
5430 * Skip over "\:", replacing it with ":".
5431 */
5432 for (e = s; *e != ':' && *e != NUL; ++e)
5433 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005434 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 if (*e == NUL)
5436 end = TRUE;
5437
5438 /*
5439 * If there is a "set" command, require a terminating ':' and
5440 * ignore the stuff after the ':'.
5441 * "vi:set opt opt opt: foo" -- foo not interpreted
5442 * "vi:opt opt opt: foo" -- foo interpreted
5443 * Accept "se" for compatibility with Elvis.
5444 */
5445 if (STRNCMP(s, "set ", (size_t)4) == 0
5446 || STRNCMP(s, "se ", (size_t)3) == 0)
5447 {
5448 if (*e != ':') /* no terminating ':'? */
5449 break;
5450 end = TRUE;
5451 s = vim_strchr(s, ' ') + 1;
5452 }
5453 *e = NUL; /* truncate the set command */
5454
5455 if (*s != NUL) /* skip over an empty "::" */
5456 {
5457#ifdef FEAT_EVAL
5458 save_SID = current_SID;
5459 current_SID = SID_MODELINE;
5460#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005461 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462#ifdef FEAT_EVAL
5463 current_SID = save_SID;
5464#endif
5465 if (retval == FAIL) /* stop if error found */
5466 break;
5467 }
5468 s = e + 1; /* advance to next part */
5469 }
5470
5471 sourcing_lnum = save_sourcing_lnum;
5472 sourcing_name = save_sourcing_name;
5473
5474 vim_free(linecopy);
5475 }
5476 return retval;
5477}
5478
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005479#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005481read_viminfo_bufferlist(
5482 vir_T *virp,
5483 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484{
5485 char_u *tab;
5486 linenr_T lnum;
5487 colnr_T col;
5488 buf_T *buf;
5489 char_u *sfname;
5490 char_u *xline;
5491
5492 /* Handle long line and escaped characters. */
5493 xline = viminfo_readstring(virp, 1, FALSE);
5494
5495 /* don't read in if there are files on the command-line or if writing: */
5496 if (xline != NULL && !writing && ARGCOUNT == 0
5497 && find_viminfo_parameter('%') != NULL)
5498 {
5499 /* Format is: <fname> Tab <lnum> Tab <col>.
5500 * Watch out for a Tab in the file name, work from the end. */
5501 lnum = 0;
5502 col = 0;
5503 tab = vim_strrchr(xline, '\t');
5504 if (tab != NULL)
5505 {
5506 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005507 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 tab = vim_strrchr(xline, '\t');
5509 if (tab != NULL)
5510 {
5511 *tab++ = '\0';
5512 lnum = atol((char *)tab);
5513 }
5514 }
5515
5516 /* Expand "~/" in the file name at "line + 1" to a full path.
5517 * Then try shortening it by comparing with the current directory */
5518 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005519 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
5521 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5522 if (buf != NULL) /* just in case... */
5523 {
5524 buf->b_last_cursor.lnum = lnum;
5525 buf->b_last_cursor.col = col;
5526 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5527 }
5528 }
5529 vim_free(xline);
5530
5531 return viminfo_readline(virp);
5532}
5533
5534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005535write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536{
5537 buf_T *buf;
5538#ifdef FEAT_WINDOWS
5539 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005540 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541#endif
5542 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005543 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544
5545 if (find_viminfo_parameter('%') == NULL)
5546 return;
5547
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005548 /* Without a number -1 is returned: do all buffers. */
5549 max_buffers = get_viminfo_parameter('%');
5550
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005552#define LINE_BUF_LEN (MAXPATHL + 40)
5553 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 if (line == NULL)
5555 return;
5556
5557#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005558 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 set_last_cursor(win);
5560#else
5561 set_last_cursor(curwin);
5562#endif
5563
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005564 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005565 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 {
5567 if (buf->b_fname == NULL
5568 || !buf->b_p_bl
5569#ifdef FEAT_QUICKFIX
5570 || bt_quickfix(buf)
5571#endif
5572 || removable(buf->b_ffname))
5573 continue;
5574
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005575 if (max_buffers-- == 0)
5576 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 putc('%', fp);
5578 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005579 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 (long)buf->b_last_cursor.lnum,
5581 buf->b_last_cursor.col);
5582 viminfo_writestring(fp, line);
5583 }
5584 vim_free(line);
5585}
5586#endif
5587
5588
5589/*
5590 * Return special buffer name.
5591 * Returns NULL when the buffer has a normal file name.
5592 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005593 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005594buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595{
5596#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5597 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005598 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005599 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005600 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005601
5602 /*
5603 * For location list window, w_llist_ref points to the location list.
5604 * For quickfix window, w_llist_ref is NULL.
5605 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005606 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005607 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005608 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005609 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611#endif
5612#ifdef FEAT_QUICKFIX
5613 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5614 * contains the name as specified by the user */
5615 if (bt_nofile(buf))
5616 {
5617 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005618 return buf->b_sfname;
5619 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 }
5621#endif
5622 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005623 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 return NULL;
5625}
5626
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005627#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5628 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5629 || defined(PROTO)
5630/*
5631 * Find a window for buffer "buf".
5632 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5633 * If not found FAIL is returned.
5634 */
5635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005636find_win_for_buf(
5637 buf_T *buf,
5638 win_T **wp,
5639 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005640{
5641 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5642 if ((*wp)->w_buffer == buf)
5643 goto win_found;
5644 return FAIL;
5645win_found:
5646 return OK;
5647}
5648#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
5650#if defined(FEAT_SIGNS) || defined(PROTO)
5651/*
5652 * Insert the sign into the signlist.
5653 */
5654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005655insert_sign(
5656 buf_T *buf, /* buffer to store sign in */
5657 signlist_T *prev, /* previous sign entry */
5658 signlist_T *next, /* next sign entry */
5659 int id, /* sign ID */
5660 linenr_T lnum, /* line number which gets the mark */
5661 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 signlist_T *newsign;
5664
5665 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5666 if (newsign != NULL)
5667 {
5668 newsign->id = id;
5669 newsign->lnum = lnum;
5670 newsign->typenr = typenr;
5671 newsign->next = next;
5672#ifdef FEAT_NETBEANS_INTG
5673 newsign->prev = prev;
5674 if (next != NULL)
5675 next->prev = newsign;
5676#endif
5677
5678 if (prev == NULL)
5679 {
5680 /* When adding first sign need to redraw the windows to create the
5681 * column for signs. */
5682 if (buf->b_signlist == NULL)
5683 {
5684 redraw_buf_later(buf, NOT_VALID);
5685 changed_cline_bef_curs();
5686 }
5687
5688 /* first sign in signlist */
5689 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005690#ifdef FEAT_NETBEANS_INTG
5691 if (netbeans_active())
5692 buf->b_has_sign_column = TRUE;
5693#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 else
5696 prev->next = newsign;
5697 }
5698}
5699
5700/*
5701 * Add the sign into the signlist. Find the right spot to do it though.
5702 */
5703 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005704buf_addsign(
5705 buf_T *buf, /* buffer to store sign in */
5706 int id, /* sign ID */
5707 linenr_T lnum, /* line number which gets the mark */
5708 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 signlist_T *sign; /* a sign in the signlist */
5711 signlist_T *prev; /* the previous sign */
5712
5713 prev = NULL;
5714 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5715 {
5716 if (lnum == sign->lnum && id == sign->id)
5717 {
5718 sign->typenr = typenr;
5719 return;
5720 }
5721 else if (
5722#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5723 id < 0 &&
5724#endif
5725 lnum < sign->lnum)
5726 {
5727#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5728 /* XXX - GRP: Is this because of sign slide problem? Or is it
5729 * really needed? Or is it because we allow multiple signs per
5730 * line? If so, should I add that feature to FEAT_SIGNS?
5731 */
5732 while (prev != NULL && prev->lnum == lnum)
5733 prev = prev->prev;
5734 if (prev == NULL)
5735 sign = buf->b_signlist;
5736 else
5737 sign = prev->next;
5738#endif
5739 insert_sign(buf, prev, sign, id, lnum, typenr);
5740 return;
5741 }
5742 prev = sign;
5743 }
5744#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5745 /* XXX - GRP: See previous comment */
5746 while (prev != NULL && prev->lnum == lnum)
5747 prev = prev->prev;
5748 if (prev == NULL)
5749 sign = buf->b_signlist;
5750 else
5751 sign = prev->next;
5752#endif
5753 insert_sign(buf, prev, sign, id, lnum, typenr);
5754
5755 return;
5756}
5757
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005758/*
5759 * For an existing, placed sign "markId" change the type to "typenr".
5760 * Returns the line number of the sign, or zero if the sign is not found.
5761 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005762 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763buf_change_sign_type(
5764 buf_T *buf, /* buffer to store sign in */
5765 int markId, /* sign ID */
5766 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767{
5768 signlist_T *sign; /* a sign in the signlist */
5769
5770 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5771 {
5772 if (sign->id == markId)
5773 {
5774 sign->typenr = typenr;
5775 return sign->lnum;
5776 }
5777 }
5778
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005779 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780}
5781
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005783buf_getsigntype(
5784 buf_T *buf,
5785 linenr_T lnum,
5786 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787{
5788 signlist_T *sign; /* a sign in a b_signlist */
5789
5790 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5791 if (sign->lnum == lnum
5792 && (type == SIGN_ANY
5793# ifdef FEAT_SIGN_ICONS
5794 || (type == SIGN_ICON
5795 && sign_get_image(sign->typenr) != NULL)
5796# endif
5797 || (type == SIGN_TEXT
5798 && sign_get_text(sign->typenr) != NULL)
5799 || (type == SIGN_LINEHL
5800 && sign_get_attr(sign->typenr, TRUE) != 0)))
5801 return sign->typenr;
5802 return 0;
5803}
5804
5805
5806 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005807buf_delsign(
5808 buf_T *buf, /* buffer sign is stored in */
5809 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005810{
5811 signlist_T **lastp; /* pointer to pointer to current sign */
5812 signlist_T *sign; /* a sign in a b_signlist */
5813 signlist_T *next; /* the next sign in a b_signlist */
5814 linenr_T lnum; /* line number whose sign was deleted */
5815
5816 lastp = &buf->b_signlist;
5817 lnum = 0;
5818 for (sign = buf->b_signlist; sign != NULL; sign = next)
5819 {
5820 next = sign->next;
5821 if (sign->id == id)
5822 {
5823 *lastp = next;
5824#ifdef FEAT_NETBEANS_INTG
5825 if (next != NULL)
5826 next->prev = sign->prev;
5827#endif
5828 lnum = sign->lnum;
5829 vim_free(sign);
5830 break;
5831 }
5832 else
5833 lastp = &sign->next;
5834 }
5835
5836 /* When deleted the last sign need to redraw the windows to remove the
5837 * sign column. */
5838 if (buf->b_signlist == NULL)
5839 {
5840 redraw_buf_later(buf, NOT_VALID);
5841 changed_cline_bef_curs();
5842 }
5843
5844 return lnum;
5845}
5846
5847
5848/*
5849 * Find the line number of the sign with the requested id. If the sign does
5850 * not exist, return 0 as the line number. This will still let the correct file
5851 * get loaded.
5852 */
5853 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005854buf_findsign(
5855 buf_T *buf, /* buffer to store sign in */
5856 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857{
5858 signlist_T *sign; /* a sign in the signlist */
5859
5860 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5861 if (sign->id == id)
5862 return sign->lnum;
5863
5864 return 0;
5865}
5866
5867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005868buf_findsign_id(
5869 buf_T *buf, /* buffer whose sign we are searching for */
5870 linenr_T lnum) /* line number of sign */
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)
5876 return sign->id;
5877
5878 return 0;
5879}
5880
5881
5882# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5883/* see if a given type of sign exists on a specific line */
5884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005885buf_findsigntype_id(
5886 buf_T *buf, /* buffer whose sign we are searching for */
5887 linenr_T lnum, /* line number of sign */
5888 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889{
5890 signlist_T *sign; /* a sign in the signlist */
5891
5892 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5893 if (sign->lnum == lnum && sign->typenr == typenr)
5894 return sign->id;
5895
5896 return 0;
5897}
5898
5899
5900# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5901/* return the number of icons on the given line */
5902 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005903buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 signlist_T *sign; /* a sign in the signlist */
5906 int count = 0;
5907
5908 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5909 if (sign->lnum == lnum)
5910 if (sign_get_image(sign->typenr) != NULL)
5911 count++;
5912
5913 return count;
5914}
5915# endif /* FEAT_SIGN_ICONS */
5916# endif /* FEAT_NETBEANS_INTG */
5917
5918
5919/*
5920 * Delete signs in buffer "buf".
5921 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005923buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924{
5925 signlist_T *next;
5926
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005927 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005928 * sign column. Not when curwin is NULL (this means we're exiting). */
5929 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005930 {
5931 redraw_buf_later(buf, NOT_VALID);
5932 changed_cline_bef_curs();
5933 }
5934
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 while (buf->b_signlist != NULL)
5936 {
5937 next = buf->b_signlist->next;
5938 vim_free(buf->b_signlist);
5939 buf->b_signlist = next;
5940 }
5941}
5942
5943/*
5944 * Delete all signs in all buffers.
5945 */
5946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005947buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948{
5949 buf_T *buf; /* buffer we are checking for signs */
5950
Bram Moolenaar29323592016-07-24 22:04:11 +02005951 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954}
5955
5956/*
5957 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5958 */
5959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005960sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961{
5962 buf_T *buf;
5963 signlist_T *p;
5964 char lbuf[BUFSIZ];
5965
5966 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5967 msg_putchar('\n');
5968 if (rbuf == NULL)
5969 buf = firstbuf;
5970 else
5971 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005972 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005973 {
5974 if (buf->b_signlist != NULL)
5975 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005976 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005977 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5978 msg_putchar('\n');
5979 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005980 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005982 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005983 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5984 MSG_PUTS(lbuf);
5985 msg_putchar('\n');
5986 }
5987 if (rbuf != NULL)
5988 break;
5989 buf = buf->b_next;
5990 }
5991}
5992
5993/*
5994 * Adjust a placed sign for inserted/deleted lines.
5995 */
5996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005997sign_mark_adjust(
5998 linenr_T line1,
5999 linenr_T line2,
6000 long amount,
6001 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002{
6003 signlist_T *sign; /* a sign in a b_signlist */
6004
6005 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6006 {
6007 if (sign->lnum >= line1 && sign->lnum <= line2)
6008 {
6009 if (amount == MAXLNUM)
6010 sign->lnum = line1;
6011 else
6012 sign->lnum += amount;
6013 }
6014 else if (sign->lnum > line2)
6015 sign->lnum += amount_after;
6016 }
6017}
6018#endif /* FEAT_SIGNS */
6019
6020/*
6021 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6022 */
6023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006025{
6026 if (on != curbuf->b_p_bl)
6027 {
6028 curbuf->b_p_bl = on;
6029#ifdef FEAT_AUTOCMD
6030 if (on)
6031 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6032 else
6033 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6034#endif
6035 }
6036}
6037
6038/*
6039 * Read the file for "buf" again and check if the contents changed.
6040 * Return TRUE if it changed or this could not be checked.
6041 */
6042 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006043buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044{
6045 buf_T *newbuf;
6046 int differ = TRUE;
6047 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006049 exarg_T ea;
6050
6051 /* Allocate a buffer without putting it in the buffer list. */
6052 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6053 if (newbuf == NULL)
6054 return TRUE;
6055
6056 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6057 if (prep_exarg(&ea, buf) == FAIL)
6058 {
6059 wipe_buffer(newbuf, FALSE);
6060 return TRUE;
6061 }
6062
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 /* set curwin/curbuf to buf and save a few things */
6064 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065
Bram Moolenaar4770d092006-01-12 23:22:24 +00006066 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 && readfile(buf->b_ffname, buf->b_fname,
6068 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6069 &ea, READ_NEW | READ_DUMMY) == OK)
6070 {
6071 /* compare the two files line by line */
6072 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6073 {
6074 differ = FALSE;
6075 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6076 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6077 {
6078 differ = TRUE;
6079 break;
6080 }
6081 }
6082 }
6083 vim_free(ea.cmd);
6084
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 /* restore curwin/curbuf and a few other things */
6086 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087
6088 if (curbuf != newbuf) /* safety check */
6089 wipe_buffer(newbuf, FALSE);
6090
6091 return differ;
6092}
6093
6094/*
6095 * Wipe out a buffer and decrement the last buffer number if it was used for
6096 * this buffer. Call this to wipe out a temp buffer that does not contain any
6097 * marks.
6098 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100wipe_buffer(
6101 buf_T *buf,
6102 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103{
6104 if (buf->b_fnum == top_file_num - 1)
6105 --top_file_num;
6106
6107#ifdef FEAT_AUTOCMD
6108 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006109 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006111 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112#ifdef FEAT_AUTOCMD
6113 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006114 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115#endif
6116}