blob: 3bfd9752206b5fe2d23801edf5ffa222d92b1193 [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
Bram Moolenaarc4a908e2016-09-08 23:35:30 +0200581 /* When closing the current buffer stop Visual mode before freeing
582 * anything. */
583 if (buf == curbuf)
584 end_visual_mode();
585
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 /*
587 * Free all things allocated for this buffer.
588 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
589 */
590#ifdef FEAT_AUTOCMD
591 /* Remember if we are closing the current buffer. Restore the number of
592 * windows, so that autocommands in buf_freeall() don't get confused. */
593 is_curbuf = (buf == curbuf);
594 buf->b_nwindows = nwindows;
595#endif
596
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200597 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598
599#ifdef FEAT_AUTOCMD
600 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200601 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602 return;
603# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000604 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 return;
606# endif
607
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 /*
609 * It's possible that autocommands change curbuf to the one being deleted.
610 * This might cause the previous curbuf to be deleted unexpectedly. But
611 * in some cases it's OK to delete the curbuf, because a new one is
612 * obtained anyway. Therefore only return if curbuf changed to the
613 * deleted buffer.
614 */
615 if (buf == curbuf && !is_curbuf)
616 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200617
618 if (
619#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200620 win_valid_any_tab(win) &&
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200621#else
622 win != NULL &&
623#endif
624 win->w_buffer == buf)
625 win->w_buffer = NULL; /* make sure we don't use the buffer now */
626
627 /* Autocommands may have opened or closed windows for this buffer.
628 * Decrement the count for the close we do here. */
629 if (buf->b_nwindows > 0)
630 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#endif
632
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000633 /* Change directories when the 'acd' option is set. */
634 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635
636 /*
637 * Remove the buffer from the list.
638 */
639 if (wipe_buf)
640 {
641#ifdef FEAT_SUN_WORKSHOP
642 if (usingSunWorkShop)
643 workshop_file_closed_lineno((char *)buf->b_ffname,
644 (int)buf->b_last_cursor.lnum);
645#endif
646 vim_free(buf->b_ffname);
647 vim_free(buf->b_sfname);
648 if (buf->b_prev == NULL)
649 firstbuf = buf->b_next;
650 else
651 buf->b_prev->b_next = buf->b_next;
652 if (buf->b_next == NULL)
653 lastbuf = buf->b_prev;
654 else
655 buf->b_next->b_prev = buf->b_prev;
656 free_buffer(buf);
657 }
658 else
659 {
660 if (del_buf)
661 {
662 /* Free all internal variables and reset option values, to make
663 * ":bdel" compatible with Vim 5.7. */
664 free_buffer_stuff(buf, TRUE);
665
666 /* Make it look like a new buffer. */
667 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
668
669 /* Init the options when loaded again. */
670 buf->b_p_initialized = FALSE;
671 }
672 buf_clear_file(buf);
673 if (del_buf)
674 buf->b_p_bl = FALSE;
675 }
676}
677
678/*
679 * Make buffer not contain a file.
680 */
681 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100682buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683{
684 buf->b_ml.ml_line_count = 1;
685 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 buf->b_p_eol = TRUE;
688 buf->b_start_eol = TRUE;
689#ifdef FEAT_MBYTE
690 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000691 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000692#endif
693 buf->b_ml.ml_mfp = NULL;
694 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
695#ifdef FEAT_NETBEANS_INTG
696 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#endif
698}
699
700/*
701 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200702 * the file. Careful: get here with "curwin" NULL when exiting.
703 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200704 * BFA_DEL buffer is going to be deleted
705 * BFA_WIPE buffer is going to be wiped out
706 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000708 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100709buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710{
711#ifdef FEAT_AUTOCMD
712 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200713 bufref_T bufref;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200714# ifdef FEAT_WINDOWS
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200715 int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
Bram Moolenaar5a497892016-09-03 16:29:04 +0200716 win_T *the_curwin = curwin;
717 tabpage_T *the_curtab = curtab;
718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaar5a497892016-09-03 16:29:04 +0200720 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200721 ++buf->b_locked;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200722 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200723 if (buf->b_ml.ml_mfp != NULL)
724 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200725 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
726 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200727 && !bufref_valid(&bufref))
728 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200729 return;
730 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200731 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200733 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
734 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200735 && !bufref_valid(&bufref))
736 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 return;
738 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200739 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200741 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
742 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200743 && !bufref_valid(&bufref))
744 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 return;
746 }
Bram Moolenaare0ab94e2016-09-04 19:50:54 +0200747 --buf->b_locked;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200748
749# ifdef FEAT_WINDOWS
750 /* If the buffer was in curwin and the window has changed, go back to that
751 * window, if it still exists. This avoids that ":edit x" triggering a
752 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
753 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
754 {
755 block_autocmds();
756 goto_tabpage_win(the_curtab, the_curwin);
757 unblock_autocmds();
758 }
759# endif
760
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000762 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000763 return;
764# endif
765
766 /*
767 * It's possible that autocommands change curbuf to the one being deleted.
768 * This might cause curbuf to be deleted unexpectedly. But in some cases
769 * it's OK to delete the curbuf, because a new one is obtained anyway.
770 * Therefore only return if curbuf changed to the deleted buffer.
771 */
772 if (buf == curbuf && !is_curbuf)
773 return;
774#endif
775#ifdef FEAT_DIFF
776 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
777#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200778#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100779 /* Remove any ownsyntax, unless exiting. */
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200780 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaar89c71222011-11-30 15:40:56 +0100781 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200782#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000783
784#ifdef FEAT_FOLDING
785 /* No folds in an empty buffer. */
786# ifdef FEAT_WINDOWS
787 {
788 win_T *win;
789 tabpage_T *tp;
790
791 FOR_ALL_TAB_WINDOWS(tp, win)
792 if (win->w_buffer == buf)
793 clearFolding(win);
794 }
795# else
Bram Moolenaar030cddc2016-09-04 23:41:42 +0200796 if (curwin != NULL && curwin->w_buffer == buf)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000797 clearFolding(curwin);
798# endif
799#endif
800
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801#ifdef FEAT_TCL
802 tcl_buffer_free(buf);
803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 ml_close(buf, TRUE); /* close and delete the memline/memfile */
805 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200806 if ((flags & BFA_KEEP_UNDO) == 0)
807 {
808 u_blockfree(buf); /* free the memory allocated for undo */
809 u_clearall(buf); /* reset all undo information */
810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200812 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000814 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815}
816
817/*
818 * Free a buffer structure and the things it contains related to the buffer
819 * itself (not the file, that must have been done already).
820 */
821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100822free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200824 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200826#ifdef FEAT_EVAL
827 unref_var_dict(buf->b_vars);
828#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200829#ifdef FEAT_LUA
830 lua_buffer_free(buf);
831#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000832#ifdef FEAT_MZSCHEME
833 mzscheme_buffer_free(buf);
834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835#ifdef FEAT_PERL
836 perl_buf_free(buf);
837#endif
838#ifdef FEAT_PYTHON
839 python_buffer_free(buf);
840#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200841#ifdef FEAT_PYTHON3
842 python3_buffer_free(buf);
843#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844#ifdef FEAT_RUBY
845 ruby_buffer_free(buf);
846#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200847#ifdef FEAT_JOB_CHANNEL
848 channel_buffer_free(buf);
849#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200850
851 buf_hashtab_remove(buf);
852
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200853#ifdef FEAT_AUTOCMD
854 aubuflocal_remove(buf);
855
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200856 if (autocmd_busy)
857 {
858 /* Do not free the buffer structure while autocommands are executing,
859 * it's still needed. Free it when autocmd_busy is reset. */
860 buf->b_next = au_pending_free_buf;
861 au_pending_free_buf = buf;
862 }
863 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000864#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200865 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000866}
867
868/*
869 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
870 */
871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100872free_buffer_stuff(
873 buf_T *buf,
874 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875{
876 if (free_options)
877 {
878 clear_wininfo(buf); /* including window-local options */
879 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200880#ifdef FEAT_SPELL
881 ga_clear(&buf->b_s.b_langp);
882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883 }
884#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200885 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
886 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887#endif
888#ifdef FEAT_USR_CMDS
889 uc_clear(&buf->b_ucmds); /* clear local user commands */
890#endif
891#ifdef FEAT_SIGNS
892 buf_delete_signs(buf); /* delete any signs */
893#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000894#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200895 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897#ifdef FEAT_LOCALMAP
898 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
899 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
900#endif
901#ifdef FEAT_MBYTE
902 vim_free(buf->b_start_fenc);
903 buf->b_start_fenc = NULL;
904#endif
905}
906
907/*
908 * Free the b_wininfo list for buffer "buf".
909 */
910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100911clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912{
913 wininfo_T *wip;
914
915 while (buf->b_wininfo != NULL)
916 {
917 wip = buf->b_wininfo;
918 buf->b_wininfo = wip->wi_next;
919 if (wip->wi_optset)
920 {
921 clear_winopt(&wip->wi_opt);
922#ifdef FEAT_FOLDING
923 deleteFoldRecurse(&wip->wi_folds);
924#endif
925 }
926 vim_free(wip);
927 }
928}
929
930#if defined(FEAT_LISTCMDS) || defined(PROTO)
931/*
932 * Go to another buffer. Handles the result of the ATTENTION dialog.
933 */
934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100935goto_buffer(
936 exarg_T *eap,
937 int start,
938 int dir,
939 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000941# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200942 bufref_T old_curbuf;
943
944 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945
946 swap_exists_action = SEA_DIALOG;
947# endif
948 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
949 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000950# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
952 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000953# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
954 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000955
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000956 /* Reset the error/interrupt/exception state here so that
957 * aborting() returns FALSE when closing a window. */
958 enter_cleanup(&cs);
959# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000960
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000961 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 win_close(curwin, TRUE);
963 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000964 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000965
966# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
967 /* Restore the error/interrupt/exception state if not discarded by a
968 * new aborting error, interrupt, or uncaught exception. */
969 leave_cleanup(&cs);
970# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 }
972 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200973 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974# endif
975}
976#endif
977
Bram Moolenaare64ac772005-12-07 20:54:59 +0000978#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979/*
980 * Handle the situation of swap_exists_action being set.
981 * It is allowed for "old_curbuf" to be NULL or invalid.
982 */
983 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200984handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000986# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
987 cleanup_T cs;
988# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100989#ifdef FEAT_SYN_HL
990 long old_tw = curbuf->b_p_tw;
991#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200992 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000993
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (swap_exists_action == SEA_QUIT)
995 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000996# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
997 /* Reset the error/interrupt/exception state here so that
998 * aborting() returns FALSE when closing a buffer. */
999 enter_cleanup(&cs);
1000# endif
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 /* User selected Quit at ATTENTION prompt. Go back to previous
1003 * buffer. If that buffer is gone or the same as the current one,
1004 * open a new, empty buffer. */
1005 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +00001006 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001007 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001008 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
1009 || old_curbuf->br_buf == curbuf)
1010 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
1011 else
1012 buf = old_curbuf->br_buf;
1013 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001014 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001015 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001016#ifdef FEAT_SYN_HL
1017 if (old_tw != curbuf->b_p_tw)
1018 check_colorcolumn(curwin);
1019#endif
1020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001022
1023# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1024 /* Restore the error/interrupt/exception state if not discarded by a
1025 * new aborting error, interrupt, or uncaught exception. */
1026 leave_cleanup(&cs);
1027# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 }
1029 else if (swap_exists_action == SEA_RECOVER)
1030 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001031# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1032 /* Reset the error/interrupt/exception state here so that
1033 * aborting() returns FALSE when closing a buffer. */
1034 enter_cleanup(&cs);
1035# endif
1036
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 /* User selected Recover at ATTENTION prompt. */
1038 msg_scroll = TRUE;
1039 ml_recover();
1040 MSG_PUTS("\n"); /* don't overwrite the last message */
1041 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001042 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001043
1044# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1045 /* Restore the error/interrupt/exception state if not discarded by a
1046 * new aborting error, interrupt, or uncaught exception. */
1047 leave_cleanup(&cs);
1048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049 }
1050 swap_exists_action = SEA_NONE;
1051}
1052#endif
1053
1054#if defined(FEAT_LISTCMDS) || defined(PROTO)
1055/*
1056 * do_bufdel() - delete or unload buffer(s)
1057 *
1058 * addr_count == 0: ":bdel" - delete current buffer
1059 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1060 * buffer "end_bnr", then any other arguments.
1061 * addr_count == 2: ":N,N bdel" - delete buffers in range
1062 *
1063 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1064 * DOBUF_DEL (":bdel")
1065 *
1066 * Returns error message or NULL
1067 */
1068 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001069do_bufdel(
1070 int command,
1071 char_u *arg, /* pointer to extra arguments */
1072 int addr_count,
1073 int start_bnr, /* first buffer number in a range */
1074 int end_bnr, /* buffer nr or last buffer nr in a range */
1075 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 int do_current = 0; /* delete current buffer? */
1078 int deleted = 0; /* number of buffers deleted */
1079 char_u *errormsg = NULL; /* return value */
1080 int bnr; /* buffer number */
1081 char_u *p;
1082
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 if (addr_count == 0)
1084 {
1085 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1086 }
1087 else
1088 {
1089 if (addr_count == 2)
1090 {
1091 if (*arg) /* both range and argument is not allowed */
1092 return (char_u *)_(e_trailing);
1093 bnr = start_bnr;
1094 }
1095 else /* addr_count == 1 */
1096 bnr = end_bnr;
1097
1098 for ( ;!got_int; ui_breakcheck())
1099 {
1100 /*
1101 * delete the current buffer last, otherwise when the
1102 * current buffer is deleted, the next buffer becomes
1103 * the current one and will be loaded, which may then
1104 * also be deleted, etc.
1105 */
1106 if (bnr == curbuf->b_fnum)
1107 do_current = bnr;
1108 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1109 forceit) == OK)
1110 ++deleted;
1111
1112 /*
1113 * find next buffer number to delete/unload
1114 */
1115 if (addr_count == 2)
1116 {
1117 if (++bnr > end_bnr)
1118 break;
1119 }
1120 else /* addr_count == 1 */
1121 {
1122 arg = skipwhite(arg);
1123 if (*arg == NUL)
1124 break;
1125 if (!VIM_ISDIGIT(*arg))
1126 {
1127 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001128 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1129 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 if (bnr < 0) /* failed */
1131 break;
1132 arg = p;
1133 }
1134 else
1135 bnr = getdigits(&arg);
1136 }
1137 }
1138 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1139 FORWARD, do_current, forceit) == OK)
1140 ++deleted;
1141
1142 if (deleted == 0)
1143 {
1144 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001145 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001147 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001149 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 errormsg = IObuff;
1151 }
1152 else if (deleted >= p_report)
1153 {
1154 if (command == DOBUF_UNLOAD)
1155 {
1156 if (deleted == 1)
1157 MSG(_("1 buffer unloaded"));
1158 else
1159 smsg((char_u *)_("%d buffers unloaded"), deleted);
1160 }
1161 else if (command == DOBUF_DEL)
1162 {
1163 if (deleted == 1)
1164 MSG(_("1 buffer deleted"));
1165 else
1166 smsg((char_u *)_("%d buffers deleted"), deleted);
1167 }
1168 else
1169 {
1170 if (deleted == 1)
1171 MSG(_("1 buffer wiped out"));
1172 else
1173 smsg((char_u *)_("%d buffers wiped out"), deleted);
1174 }
1175 }
1176 }
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178
1179 return errormsg;
1180}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001181#endif /* FEAT_LISTCMDS */
1182
1183#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1184 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001186static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001187
1188/*
1189 * Make the current buffer empty.
1190 * Used when it is wiped out and it's the last buffer.
1191 */
1192 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001193empty_curbuf(
1194 int close_others,
1195 int forceit,
1196 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001197{
1198 int retval;
1199 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001200 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001201
1202 if (action == DOBUF_UNLOAD)
1203 {
1204 EMSG(_("E90: Cannot unload last buffer"));
1205 return FAIL;
1206 }
1207
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001208 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001209#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001210 if (close_others)
1211 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001212 close_windows(buf, TRUE);
1213#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001214
1215 setpcmark();
1216 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1217 forceit ? ECMD_FORCEIT : 0, curwin);
1218
1219 /*
1220 * do_ecmd() may create a new buffer, then we have to delete
1221 * the old one. But do_ecmd() may have done that already, check
1222 * if the buffer still exists.
1223 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001224 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001225 close_buffer(NULL, buf, action, FALSE);
1226 if (!close_others)
1227 need_fileinfo = FALSE;
1228 return retval;
1229}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230/*
1231 * Implementation of the commands for the buffer list.
1232 *
1233 * action == DOBUF_GOTO go to specified buffer
1234 * action == DOBUF_SPLIT split window and go to specified buffer
1235 * action == DOBUF_UNLOAD unload specified buffer(s)
1236 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1237 * action == DOBUF_WIPE delete specified buffer(s) really
1238 *
1239 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1240 * start == DOBUF_FIRST go to "count" buffer from first buffer
1241 * start == DOBUF_LAST go to "count" buffer from last buffer
1242 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1243 *
1244 * Return FAIL or OK.
1245 */
1246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001247do_buffer(
1248 int action,
1249 int start,
1250 int dir, /* FORWARD or BACKWARD */
1251 int count, /* buffer number or number of buffers */
1252 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253{
1254 buf_T *buf;
1255 buf_T *bp;
1256 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1257 || action == DOBUF_WIPE);
1258
1259 switch (start)
1260 {
1261 case DOBUF_FIRST: buf = firstbuf; break;
1262 case DOBUF_LAST: buf = lastbuf; break;
1263 default: buf = curbuf; break;
1264 }
1265 if (start == DOBUF_MOD) /* find next modified buffer */
1266 {
1267 while (count-- > 0)
1268 {
1269 do
1270 {
1271 buf = buf->b_next;
1272 if (buf == NULL)
1273 buf = firstbuf;
1274 }
1275 while (buf != curbuf && !bufIsChanged(buf));
1276 }
1277 if (!bufIsChanged(buf))
1278 {
1279 EMSG(_("E84: No modified buffer found"));
1280 return FAIL;
1281 }
1282 }
1283 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1284 {
1285 while (buf != NULL && buf->b_fnum != count)
1286 buf = buf->b_next;
1287 }
1288 else
1289 {
1290 bp = NULL;
1291 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1292 {
1293 /* remember the buffer where we start, we come back there when all
1294 * buffers are unlisted. */
1295 if (bp == NULL)
1296 bp = buf;
1297 if (dir == FORWARD)
1298 {
1299 buf = buf->b_next;
1300 if (buf == NULL)
1301 buf = firstbuf;
1302 }
1303 else
1304 {
1305 buf = buf->b_prev;
1306 if (buf == NULL)
1307 buf = lastbuf;
1308 }
1309 /* don't count unlisted buffers */
1310 if (unload || buf->b_p_bl)
1311 {
1312 --count;
1313 bp = NULL; /* use this buffer as new starting point */
1314 }
1315 if (bp == buf)
1316 {
1317 /* back where we started, didn't find anything. */
1318 EMSG(_("E85: There is no listed buffer"));
1319 return FAIL;
1320 }
1321 }
1322 }
1323
1324 if (buf == NULL) /* could not find it */
1325 {
1326 if (start == DOBUF_FIRST)
1327 {
1328 /* don't warn when deleting */
1329 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001330 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 }
1332 else if (dir == FORWARD)
1333 EMSG(_("E87: Cannot go beyond last buffer"));
1334 else
1335 EMSG(_("E88: Cannot go before first buffer"));
1336 return FAIL;
1337 }
1338
1339#ifdef FEAT_GUI
1340 need_mouse_correct = TRUE;
1341#endif
1342
1343#ifdef FEAT_LISTCMDS
1344 /*
1345 * delete buffer buf from memory and/or the list
1346 */
1347 if (unload)
1348 {
1349 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001350# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1351 bufref_T bufref;
1352
1353 set_bufref(&bufref, buf);
1354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355
1356 /* When unloading or deleting a buffer that's already unloaded and
1357 * unlisted: fail silently. */
1358 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1359 return FAIL;
1360
1361 if (!forceit && bufIsChanged(buf))
1362 {
1363#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1364 if ((p_confirm || cmdmod.confirm) && p_write)
1365 {
1366 dialog_changed(buf, FALSE);
1367# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001368 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 /* Autocommand deleted buffer, oops! It's not changed
1370 * now. */
1371 return FAIL;
1372# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001373 /* If it's still changed fail silently, the dialog already
1374 * mentioned why it fails. */
1375 if (bufIsChanged(buf))
1376 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001378 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379#endif
1380 {
1381 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1382 buf->b_fnum);
1383 return FAIL;
1384 }
1385 }
1386
Bram Moolenaarc4a908e2016-09-08 23:35:30 +02001387 /* When closing the current buffer stop Visual mode. */
1388 if (buf == curbuf)
1389 end_visual_mode();
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 /*
1392 * If deleting the last (listed) buffer, make it empty.
1393 * The last (listed) buffer cannot be unloaded.
1394 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001395 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 if (bp->b_p_bl && bp != buf)
1397 break;
1398 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001399 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400
1401#ifdef FEAT_WINDOWS
1402 /*
1403 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001404 * (unless it's the only window). Repeat this so long as we end up in
1405 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001407 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001408# ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02001409 && !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02001410# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001411 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001412 {
1413 if (win_close(curwin, FALSE) == FAIL)
1414 break;
1415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416#endif
1417
1418 /*
1419 * If the buffer to be deleted is not the current one, delete it here.
1420 */
1421 if (buf != curbuf)
1422 {
1423#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001424 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001425 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001427 if (buf->b_nwindows <= 0)
1428 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 return OK;
1430 }
1431
1432 /*
1433 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001434 * There should be another, otherwise it would have been handled
1435 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001436 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 * Then prefer the buffer we most recently visited.
1438 * Else try to find one that is loaded, after the current buffer,
1439 * then before the current buffer.
1440 * Finally use any buffer.
1441 */
1442 buf = NULL; /* selected buffer */
1443 bp = NULL; /* used when no loaded buffer found */
1444#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001445 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1446 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447# ifdef FEAT_JUMPLIST
1448 else
1449# endif
1450#endif
1451#ifdef FEAT_JUMPLIST
1452 if (curwin->w_jumplistlen > 0)
1453 {
1454 int jumpidx;
1455
1456 jumpidx = curwin->w_jumplistidx - 1;
1457 if (jumpidx < 0)
1458 jumpidx = curwin->w_jumplistlen - 1;
1459
1460 forward = jumpidx;
1461 while (jumpidx != curwin->w_jumplistidx)
1462 {
1463 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1464 if (buf != NULL)
1465 {
1466 if (buf == curbuf || !buf->b_p_bl)
1467 buf = NULL; /* skip current and unlisted bufs */
1468 else if (buf->b_ml.ml_mfp == NULL)
1469 {
1470 /* skip unloaded buf, but may keep it for later */
1471 if (bp == NULL)
1472 bp = buf;
1473 buf = NULL;
1474 }
1475 }
1476 if (buf != NULL) /* found a valid buffer: stop searching */
1477 break;
1478 /* advance to older entry in jump list */
1479 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1480 break;
1481 if (--jumpidx < 0)
1482 jumpidx = curwin->w_jumplistlen - 1;
1483 if (jumpidx == forward) /* List exhausted for sure */
1484 break;
1485 }
1486 }
1487#endif
1488
1489 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1490 {
1491 forward = TRUE;
1492 buf = curbuf->b_next;
1493 for (;;)
1494 {
1495 if (buf == NULL)
1496 {
1497 if (!forward) /* tried both directions */
1498 break;
1499 buf = curbuf->b_prev;
1500 forward = FALSE;
1501 continue;
1502 }
1503 /* in non-help buffer, try to skip help buffers, and vv */
1504 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1505 {
1506 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1507 break;
1508 if (bp == NULL) /* remember unloaded buf for later */
1509 bp = buf;
1510 }
1511 if (forward)
1512 buf = buf->b_next;
1513 else
1514 buf = buf->b_prev;
1515 }
1516 }
1517 if (buf == NULL) /* No loaded buffer, use unloaded one */
1518 buf = bp;
1519 if (buf == NULL) /* No loaded buffer, find listed one */
1520 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001521 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 if (buf->b_p_bl && buf != curbuf)
1523 break;
1524 }
1525 if (buf == NULL) /* Still no buffer, just take one */
1526 {
1527 if (curbuf->b_next != NULL)
1528 buf = curbuf->b_next;
1529 else
1530 buf = curbuf->b_prev;
1531 }
1532 }
1533
Bram Moolenaara02471e2014-01-10 16:43:14 +01001534 if (buf == NULL)
1535 {
1536 /* Autocommands must have wiped out all other buffers. Only option
1537 * now is to make the current buffer empty. */
1538 return empty_curbuf(FALSE, forceit, action);
1539 }
1540
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 /*
1542 * make buf current buffer
1543 */
1544 if (action == DOBUF_SPLIT) /* split window first */
1545 {
1546# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001547 /* If 'switchbuf' contains "useopen": jump to first window containing
1548 * "buf" if one exists */
1549 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001550 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001551 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001552 * page containing "buf" if one exists */
1553 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 return OK;
1555 if (win_split(0, 0) == FAIL)
1556# endif
1557 return FAIL;
1558 }
1559#endif
1560
1561 /* go to current buffer - nothing to do */
1562 if (buf == curbuf)
1563 return OK;
1564
1565 /*
1566 * Check if the current buffer may be abandoned.
1567 */
1568 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1569 {
1570#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1571 if ((p_confirm || cmdmod.confirm) && p_write)
1572 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001573# ifdef FEAT_AUTOCMD
1574 bufref_T bufref;
1575
1576 set_bufref(&bufref, buf);
1577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 dialog_changed(curbuf, FALSE);
1579# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001580 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 /* Autocommand deleted buffer, oops! */
1582 return FAIL;
1583# endif
1584 }
1585 if (bufIsChanged(curbuf))
1586#endif
1587 {
1588 EMSG(_(e_nowrtmsg));
1589 return FAIL;
1590 }
1591 }
1592
1593 /* Go to the other buffer. */
1594 set_curbuf(buf, action);
1595
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001596#if defined(FEAT_LISTCMDS) \
1597 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001599 {
1600 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602#endif
1603
1604#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1605 if (aborting()) /* autocmds may abort script processing */
1606 return FAIL;
1607#endif
1608
1609 return OK;
1610}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613/*
1614 * Set current buffer to "buf". Executes autocommands and closes current
1615 * buffer. "action" tells how to close the current buffer:
1616 * DOBUF_GOTO free or hide it
1617 * DOBUF_SPLIT nothing
1618 * DOBUF_UNLOAD unload it
1619 * DOBUF_DEL delete it
1620 * DOBUF_WIPE wipe it out
1621 */
1622 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625 buf_T *prevbuf;
1626 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1627 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001628#ifdef FEAT_SYN_HL
1629 long old_tw = curbuf->b_p_tw;
1630#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001631 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632
1633 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001634 if (!cmdmod.keepalt)
1635 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001636 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 /* Don't restart Select mode after switching to another buffer. */
1639 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640
1641 /* close_windows() or apply_autocmds() may change curbuf */
1642 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001643 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
1645#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001646 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001648 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001650 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651# endif
1652#endif
1653 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001654#ifdef FEAT_SYN_HL
1655 if (prevbuf == curwin->w_buffer)
1656 reset_synblock(curwin);
1657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658#ifdef FEAT_WINDOWS
1659 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001660 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661#endif
1662#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001663 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001665 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001667 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001668#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001669 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001670#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001671 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001672 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1674 unload ? action : (action == DOBUF_GOTO
1675 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001676 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001677#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001678 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001679 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001680 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001681#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 }
1684#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001685 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001686 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001687 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1688 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001689# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001690 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001691# endif
1692# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001693 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001694# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001695 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001699#ifdef FEAT_SYN_HL
1700 if (old_tw != curbuf->b_p_tw)
1701 check_colorcolumn(curwin);
1702#endif
1703 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704}
1705
1706/*
1707 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001708 * Old curbuf must have been abandoned already! This also means "curbuf" may
1709 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 */
1711 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001712enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713{
1714 /* Copy buffer and window local option values. Not for a help buffer. */
1715 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1716 if (!buf->b_help)
1717 get_winopts(buf);
1718#ifdef FEAT_FOLDING
1719 else
1720 /* Remove all folds in the window. */
1721 clearFolding(curwin);
1722 foldUpdateAll(curwin); /* update folds (later). */
1723#endif
1724
1725 /* Get the buffer in the current window. */
1726 curwin->w_buffer = buf;
1727 curbuf = buf;
1728 ++curbuf->b_nwindows;
1729
1730#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001731 if (curwin->w_p_diff)
1732 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733#endif
1734
Bram Moolenaara971b822011-09-14 14:43:25 +02001735#ifdef FEAT_SYN_HL
1736 curwin->w_s = &(buf->b_s);
1737#endif
1738
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 /* Cursor on first line by default. */
1740 curwin->w_cursor.lnum = 1;
1741 curwin->w_cursor.col = 0;
1742#ifdef FEAT_VIRTUALEDIT
1743 curwin->w_cursor.coladd = 0;
1744#endif
1745 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001746#ifdef FEAT_AUTOCMD
1747 curwin->w_topline_was_set = FALSE;
1748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001750 /* mark cursor position as being invalid */
1751 curwin->w_valid = 0;
1752
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 /* Make sure the buffer is loaded. */
1754 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001755 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001756#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001757 /* If there is no filetype, allow for detecting one. Esp. useful for
1758 * ":ball" used in a autocommand. If there already is a filetype we
1759 * might prefer to keep it. */
1760 if (*curbuf->b_p_ft == NUL)
1761 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001762#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001763
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001764 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 else
1767 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001768 if (!msg_silent)
1769 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1771#ifdef FEAT_AUTOCMD
1772 curwin->w_topline = 1;
1773# ifdef FEAT_DIFF
1774 curwin->w_topfill = 0;
1775# endif
1776 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1777 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1778#endif
1779 }
1780
1781 /* If autocommands did not change the cursor position, restore cursor lnum
1782 * and possibly cursor col. */
1783 if (curwin->w_cursor.lnum == 1 && inindent(0))
1784 buflist_getfpos();
1785
1786 check_arg_idx(curwin); /* check for valid arg_idx */
1787#ifdef FEAT_TITLE
1788 maketitle();
1789#endif
1790#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001791 /* when autocmds didn't change it */
1792 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#endif
1794 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1795
Bram Moolenaar009b2592004-10-24 19:18:58 +00001796#ifdef FEAT_NETBEANS_INTG
1797 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001798 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001799#endif
1800
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001801 /* Change directories when the 'acd' option is set. */
1802 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803
1804#ifdef FEAT_KEYMAP
1805 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001806 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001808#ifdef FEAT_SPELL
1809 /* May need to set the spell language. Can only do this after the buffer
1810 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001811 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1812 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001813#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001814#ifdef FEAT_VIMINFO
1815 curbuf->b_last_used = vim_time();
1816#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001817
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 redraw_later(NOT_VALID);
1819}
1820
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001821#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1822/*
1823 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001824 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001825 */
1826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001827do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001828{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001829 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001830 && curbuf->b_ffname != NULL
1831 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001832 shorten_fnames(TRUE);
1833}
1834#endif
1835
Bram Moolenaar071d4272004-06-13 20:20:40 +00001836/*
1837 * functions for dealing with the buffer list
1838 */
1839
Bram Moolenaar480778b2016-07-14 22:09:39 +02001840static int top_file_num = 1; /* highest file number */
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842/*
1843 * Add a file name to the buffer list. Return a pointer to the buffer.
1844 * If the same file name already exists return a pointer to that buffer.
1845 * If it does not exist, or if fname == NULL, a new entry is created.
1846 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1847 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1848 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001849 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001850 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1851 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 * This is the ONLY way to create a new buffer.
1853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001855buflist_new(
1856 char_u *ffname, /* full path of fname or relative */
1857 char_u *sfname, /* short fname or NULL */
1858 linenr_T lnum, /* preferred cursor line */
1859 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860{
1861 buf_T *buf;
1862#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001863 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864#endif
1865
Bram Moolenaar480778b2016-07-14 22:09:39 +02001866 if (top_file_num == 1)
1867 hash_init(&buf_hashtab);
1868
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1870
1871 /*
1872 * If file name already exists in the list, update the entry.
1873 */
1874#ifdef UNIX
1875 /* On Unix we can use inode numbers when the file exists. Works better
1876 * for hard links. */
1877 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1878 st.st_dev = (dev_T)-1;
1879#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001880 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881#ifdef UNIX
1882 buflist_findname_stat(ffname, &st)
1883#else
1884 buflist_findname(ffname)
1885#endif
1886 ) != NULL)
1887 {
1888 vim_free(ffname);
1889 if (lnum != 0)
1890 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001891
1892 if ((flags & BLN_NOOPT) == 0)
1893 /* copy the options now, if 'cpo' doesn't have 's' and not done
1894 * already */
1895 buf_copy_options(buf, 0);
1896
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1898 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001899#ifdef FEAT_AUTOCMD
1900 bufref_T bufref;
1901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 buf->b_p_bl = TRUE;
1903#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001904 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001906 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001907 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001908 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001909 return NULL;
1910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911#endif
1912 }
1913 return buf;
1914 }
1915
1916 /*
1917 * If the current buffer has no name and no contents, use the current
1918 * buffer. Otherwise: Need to allocate a new buffer structure.
1919 *
1920 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001921 * (A spell file buffer is allocated in spell.c, but that's not a normal
1922 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 */
1924 buf = NULL;
1925 if ((flags & BLN_CURBUF)
1926 && curbuf != NULL
1927 && curbuf->b_ffname == NULL
1928 && curbuf->b_nwindows <= 1
1929 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1930 {
1931 buf = curbuf;
1932#ifdef FEAT_AUTOCMD
1933 /* It's like this buffer is deleted. Watch out for autocommands that
1934 * change curbuf! If that happens, allocate a new buffer anyway. */
1935 if (curbuf->b_p_bl)
1936 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1937 if (buf == curbuf)
1938 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1939# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001940 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 return NULL;
1942# endif
1943#endif
1944#ifdef FEAT_QUICKFIX
1945# ifdef FEAT_AUTOCMD
1946 if (buf == curbuf)
1947# endif
1948 {
1949 /* Make sure 'bufhidden' and 'buftype' are empty */
1950 clear_string_option(&buf->b_p_bh);
1951 clear_string_option(&buf->b_p_bt);
1952 }
1953#endif
1954 }
1955 if (buf != curbuf || curbuf == NULL)
1956 {
1957 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1958 if (buf == NULL)
1959 {
1960 vim_free(ffname);
1961 return NULL;
1962 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001963#ifdef FEAT_EVAL
1964 /* init b: variables */
1965 buf->b_vars = dict_alloc();
1966 if (buf->b_vars == NULL)
1967 {
1968 vim_free(ffname);
1969 vim_free(buf);
1970 return NULL;
1971 }
1972 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 }
1975
1976 if (ffname != NULL)
1977 {
1978 buf->b_ffname = ffname;
1979 buf->b_sfname = vim_strsave(sfname);
1980 }
1981
1982 clear_wininfo(buf);
1983 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1984
1985 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1986 || buf->b_wininfo == NULL)
1987 {
1988 vim_free(buf->b_ffname);
1989 buf->b_ffname = NULL;
1990 vim_free(buf->b_sfname);
1991 buf->b_sfname = NULL;
1992 if (buf != curbuf)
1993 free_buffer(buf);
1994 return NULL;
1995 }
1996
1997 if (buf == curbuf)
1998 {
1999 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02002000 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (buf != curbuf) /* autocommands deleted the buffer! */
2002 return NULL;
2003#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002004 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 return NULL;
2006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01002008
2009 /* Init the options. */
2010 buf->b_p_initialized = FALSE;
2011 buf_copy_options(buf, BCO_ENTER);
2012
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013#ifdef FEAT_KEYMAP
2014 /* need to reload lmaps and set b:keymap_name */
2015 curbuf->b_kmap_state |= KEYMAP_INIT;
2016#endif
2017 }
2018 else
2019 {
2020 /*
2021 * put new buffer at the end of the buffer list
2022 */
2023 buf->b_next = NULL;
2024 if (firstbuf == NULL) /* buffer list is empty */
2025 {
2026 buf->b_prev = NULL;
2027 firstbuf = buf;
2028 }
2029 else /* append new buffer at end of list */
2030 {
2031 lastbuf->b_next = buf;
2032 buf->b_prev = lastbuf;
2033 }
2034 lastbuf = buf;
2035
2036 buf->b_fnum = top_file_num++;
2037 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2038 {
2039 EMSG(_("W14: Warning: List of file names overflow"));
2040 if (emsg_silent == 0)
2041 {
2042 out_flush();
2043 ui_delay(3000L, TRUE); /* make sure it is noticed */
2044 }
2045 top_file_num = 1;
2046 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002047 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048
2049 /*
2050 * Always copy the options from the current buffer.
2051 */
2052 buf_copy_options(buf, BCO_ALWAYS);
2053 }
2054
2055 buf->b_wininfo->wi_fpos.lnum = lnum;
2056 buf->b_wininfo->wi_win = curwin;
2057
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002058#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002059 hash_init(&buf->b_s.b_keywtab);
2060 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002061#endif
2062
2063 buf->b_fname = buf->b_sfname;
2064#ifdef UNIX
2065 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002066 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 else
2068 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002069 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 buf->b_dev = st.st_dev;
2071 buf->b_ino = st.st_ino;
2072 }
2073#endif
2074 buf->b_u_synced = TRUE;
2075 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002076 if (flags & BLN_DUMMY)
2077 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 buf_clear_file(buf);
2079 clrallmarks(buf); /* clear marks */
2080 fmarks_check_names(buf); /* check file marks for this file */
2081 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2082#ifdef FEAT_AUTOCMD
2083 if (!(flags & BLN_DUMMY))
2084 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002085 bufref_T bufref;
2086
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002087 /* Tricky: these autocommands may change the buffer list. They could
2088 * also split the window with re-using the one empty buffer. This may
2089 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002090 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002091 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002092 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002093 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002095 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002096 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002097 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002098 return NULL;
2099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002101 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 return NULL;
2103# endif
2104 }
2105#endif
2106
2107 return buf;
2108}
2109
2110/*
2111 * Free the memory for the options of a buffer.
2112 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2113 * 'fileencoding'.
2114 */
2115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002116free_buf_options(
2117 buf_T *buf,
2118 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119{
2120 if (free_p_ff)
2121 {
2122#ifdef FEAT_MBYTE
2123 clear_string_option(&buf->b_p_fenc);
2124#endif
2125 clear_string_option(&buf->b_p_ff);
2126#ifdef FEAT_QUICKFIX
2127 clear_string_option(&buf->b_p_bh);
2128 clear_string_option(&buf->b_p_bt);
2129#endif
2130 }
2131#ifdef FEAT_FIND_ID
2132 clear_string_option(&buf->b_p_def);
2133 clear_string_option(&buf->b_p_inc);
2134# ifdef FEAT_EVAL
2135 clear_string_option(&buf->b_p_inex);
2136# endif
2137#endif
2138#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2139 clear_string_option(&buf->b_p_inde);
2140 clear_string_option(&buf->b_p_indk);
2141#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002142#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2143 clear_string_option(&buf->b_p_bexpr);
2144#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002145#if defined(FEAT_CRYPT)
2146 clear_string_option(&buf->b_p_cm);
2147#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002148#if defined(FEAT_EVAL)
2149 clear_string_option(&buf->b_p_fex);
2150#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151#ifdef FEAT_CRYPT
2152 clear_string_option(&buf->b_p_key);
2153#endif
2154 clear_string_option(&buf->b_p_kp);
2155 clear_string_option(&buf->b_p_mps);
2156 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002157 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 clear_string_option(&buf->b_p_isk);
2159#ifdef FEAT_KEYMAP
2160 clear_string_option(&buf->b_p_keymap);
2161 ga_clear(&buf->b_kmap_ga);
2162#endif
2163#ifdef FEAT_COMMENTS
2164 clear_string_option(&buf->b_p_com);
2165#endif
2166#ifdef FEAT_FOLDING
2167 clear_string_option(&buf->b_p_cms);
2168#endif
2169 clear_string_option(&buf->b_p_nf);
2170#ifdef FEAT_SYN_HL
2171 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002172 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002173#endif
2174#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002175 clear_string_option(&buf->b_s.b_p_spc);
2176 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002177 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002178 buf->b_s.b_cap_prog = NULL;
2179 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180#endif
2181#ifdef FEAT_SEARCHPATH
2182 clear_string_option(&buf->b_p_sua);
2183#endif
2184#ifdef FEAT_AUTOCMD
2185 clear_string_option(&buf->b_p_ft);
2186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187#ifdef FEAT_CINDENT
2188 clear_string_option(&buf->b_p_cink);
2189 clear_string_option(&buf->b_p_cino);
2190#endif
2191#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2192 clear_string_option(&buf->b_p_cinw);
2193#endif
2194#ifdef FEAT_INS_EXPAND
2195 clear_string_option(&buf->b_p_cpt);
2196#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002197#ifdef FEAT_COMPL_FUNC
2198 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002199 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#ifdef FEAT_QUICKFIX
2202 clear_string_option(&buf->b_p_gp);
2203 clear_string_option(&buf->b_p_mp);
2204 clear_string_option(&buf->b_p_efm);
2205#endif
2206 clear_string_option(&buf->b_p_ep);
2207 clear_string_option(&buf->b_p_path);
2208 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002209 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210#ifdef FEAT_INS_EXPAND
2211 clear_string_option(&buf->b_p_dict);
2212 clear_string_option(&buf->b_p_tsr);
2213#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002214#ifdef FEAT_TEXTOBJ
2215 clear_string_option(&buf->b_p_qe);
2216#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002218 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002219#ifdef FEAT_LISP
2220 clear_string_option(&buf->b_p_lw);
2221#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002222 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223}
2224
2225/*
2226 * get alternate file n
2227 * set linenr to lnum or altfpos.lnum if lnum == 0
2228 * also set cursor column to altfpos.col if 'startofline' is not set.
2229 * if (options & GETF_SETMARK) call setpcmark()
2230 * if (options & GETF_ALT) we are jumping to an alternate file.
2231 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2232 *
2233 * return FAIL for failure, OK for success
2234 */
2235 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236buflist_getfile(
2237 int n,
2238 linenr_T lnum,
2239 int options,
2240 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241{
2242 buf_T *buf;
2243#ifdef FEAT_WINDOWS
2244 win_T *wp = NULL;
2245#endif
2246 pos_T *fpos;
2247 colnr_T col;
2248
2249 buf = buflist_findnr(n);
2250 if (buf == NULL)
2251 {
2252 if ((options & GETF_ALT) && n == 0)
2253 EMSG(_(e_noalt));
2254 else
2255 EMSGN(_("E92: Buffer %ld not found"), n);
2256 return FAIL;
2257 }
2258
2259 /* if alternate file is the current buffer, nothing to do */
2260 if (buf == curbuf)
2261 return OK;
2262
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002263 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002264 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002265 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002267 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002268#ifdef FEAT_AUTOCMD
2269 if (curbuf_locked())
2270 return FAIL;
2271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
2273 /* altfpos may be changed by getfile(), get it now */
2274 if (lnum == 0)
2275 {
2276 fpos = buflist_findfpos(buf);
2277 lnum = fpos->lnum;
2278 col = fpos->col;
2279 }
2280 else
2281 col = 0;
2282
2283#ifdef FEAT_WINDOWS
2284 if (options & GETF_SWITCH)
2285 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002286 /* If 'switchbuf' contains "useopen": jump to first window containing
2287 * "buf" if one exists */
2288 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002290
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002291 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002292 * page containing "buf" if one exists */
2293 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002294 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002295
2296 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2297 * current buffer isn't empty: open new tab or window */
2298 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2299 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002301 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002302 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002303 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2304 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002306 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 }
2308 }
2309#endif
2310
2311 ++RedrawingDisabled;
2312 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2313 lnum, forceit) <= 0)
2314 {
2315 --RedrawingDisabled;
2316
2317 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2318 if (!p_sol && col != 0)
2319 {
2320 curwin->w_cursor.col = col;
2321 check_cursor_col();
2322#ifdef FEAT_VIRTUALEDIT
2323 curwin->w_cursor.coladd = 0;
2324#endif
2325 curwin->w_set_curswant = TRUE;
2326 }
2327 return OK;
2328 }
2329 --RedrawingDisabled;
2330 return FAIL;
2331}
2332
2333/*
2334 * go to the last know line number for the current buffer
2335 */
2336 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002337buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338{
2339 pos_T *fpos;
2340
2341 fpos = buflist_findfpos(curbuf);
2342
2343 curwin->w_cursor.lnum = fpos->lnum;
2344 check_cursor_lnum();
2345
2346 if (p_sol)
2347 curwin->w_cursor.col = 0;
2348 else
2349 {
2350 curwin->w_cursor.col = fpos->col;
2351 check_cursor_col();
2352#ifdef FEAT_VIRTUALEDIT
2353 curwin->w_cursor.coladd = 0;
2354#endif
2355 curwin->w_set_curswant = TRUE;
2356 }
2357}
2358
Bram Moolenaar81695252004-12-29 20:58:21 +00002359#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2360/*
2361 * Find file in buffer list by name (it has to be for the current window).
2362 * Returns NULL if not found.
2363 */
2364 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002365buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002366{
2367 char_u *ffname;
2368 buf_T *buf = NULL;
2369
2370 /* First make the name into a full path name */
2371 ffname = FullName_save(fname,
2372#ifdef UNIX
2373 TRUE /* force expansion, get rid of symbolic links */
2374#else
2375 FALSE
2376#endif
2377 );
2378 if (ffname != NULL)
2379 {
2380 buf = buflist_findname(ffname);
2381 vim_free(ffname);
2382 }
2383 return buf;
2384}
2385#endif
2386
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387/*
2388 * Find file in buffer list by name (it has to be for the current window).
2389 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002390 * Skips dummy buffers.
2391 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 */
2393 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002394buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395{
2396#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002397 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 if (mch_stat((char *)ffname, &st) < 0)
2400 st.st_dev = (dev_T)-1;
2401 return buflist_findname_stat(ffname, &st);
2402}
2403
2404/*
2405 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2406 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002407 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 */
2409 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002410buflist_findname_stat(
2411 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002412 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413{
2414#endif
2415 buf_T *buf;
2416
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002417 /* Start at the last buffer, expect to find a match sooner. */
2418 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002419 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#ifdef UNIX
2421 , stp
2422#endif
2423 ))
2424 return buf;
2425 return NULL;
2426}
2427
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002428#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2429 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002430/*
2431 * Find file in buffer list by a regexp pattern.
2432 * Return fnum of the found buffer.
2433 * Return < 0 for error.
2434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002436buflist_findpat(
2437 char_u *pattern,
2438 char_u *pattern_end, /* pointer to first char after pattern */
2439 int unlisted, /* find unlisted buffers */
2440 int diffmode UNUSED, /* find diff-mode buffers only */
2441 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442{
2443 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 int match = -1;
2445 int find_listed;
2446 char_u *pat;
2447 char_u *patend;
2448 int attempt;
2449 char_u *p;
2450 int toggledollar;
2451
2452 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2453 {
2454 if (*pattern == '%')
2455 match = curbuf->b_fnum;
2456 else
2457 match = curwin->w_alt_fnum;
2458#ifdef FEAT_DIFF
2459 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2460 match = -1;
2461#endif
2462 }
2463
2464 /*
2465 * Try four ways of matching a listed buffer:
2466 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002467 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 * attempt == 2: with '$' at end (only match at end)
2469 * attempt == 3: with '^' at start and '$' at end (only full match)
2470 * Repeat this for finding an unlisted buffer if there was no matching
2471 * listed buffer.
2472 */
2473 else
2474 {
2475 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2476 if (pat == NULL)
2477 return -1;
2478 patend = pat + STRLEN(pat) - 1;
2479 toggledollar = (patend > pat && *patend == '$');
2480
2481 /* First try finding a listed buffer. If not found and "unlisted"
2482 * is TRUE, try finding an unlisted buffer. */
2483 find_listed = TRUE;
2484 for (;;)
2485 {
2486 for (attempt = 0; attempt <= 3; ++attempt)
2487 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002488 regmatch_T regmatch;
2489
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 /* may add '^' and '$' */
2491 if (toggledollar)
2492 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2493 p = pat;
2494 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2495 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002496 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2497 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 vim_free(pat);
2500 return -1;
2501 }
2502
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002503 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 if (buf->b_p_bl == find_listed
2505#ifdef FEAT_DIFF
2506 && (!diffmode || diff_mode_buf(buf))
2507#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002508 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002510 if (curtab_only)
2511 {
2512 /* Ignore the match if the buffer is not open in
2513 * the current tab. */
2514#ifdef FEAT_WINDOWS
2515 win_T *wp;
2516
Bram Moolenaar29323592016-07-24 22:04:11 +02002517 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002518 if (wp->w_buffer == buf)
2519 break;
2520 if (wp == NULL)
2521 continue;
2522#else
2523 if (curwin->w_buffer != buf)
2524 continue;
2525#endif
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (match >= 0) /* already found a match */
2528 {
2529 match = -2;
2530 break;
2531 }
2532 match = buf->b_fnum; /* remember first match */
2533 }
2534
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002535 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (match >= 0) /* found one match */
2537 break;
2538 }
2539
2540 /* Only search for unlisted buffers if there was no match with
2541 * a listed buffer. */
2542 if (!unlisted || !find_listed || match != -1)
2543 break;
2544 find_listed = FALSE;
2545 }
2546
2547 vim_free(pat);
2548 }
2549
2550 if (match == -2)
2551 EMSG2(_("E93: More than one match for %s"), pattern);
2552 else if (match < 0)
2553 EMSG2(_("E94: No matching buffer for %s"), pattern);
2554 return match;
2555}
2556#endif
2557
2558#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2559
2560/*
2561 * Find all buffer names that match.
2562 * For command line expansion of ":buf" and ":sbuf".
2563 * Return OK if matches found, FAIL otherwise.
2564 */
2565 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002566ExpandBufnames(
2567 char_u *pat,
2568 int *num_file,
2569 char_u ***file,
2570 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571{
2572 int count = 0;
2573 buf_T *buf;
2574 int round;
2575 char_u *p;
2576 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002577 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578
2579 *num_file = 0; /* return values in case of FAIL */
2580 *file = NULL;
2581
Bram Moolenaar05159a02005-02-26 23:04:13 +00002582 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2583 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002585 patc = alloc((unsigned)STRLEN(pat) + 11);
2586 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002588 STRCPY(patc, "\\(^\\|[\\/]\\)");
2589 STRCPY(patc + 11, pat + 1);
2590 }
2591 else
2592 patc = pat;
2593
2594 /*
2595 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002596 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002597 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002598 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002599 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002600 regmatch_T regmatch;
2601
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002602 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002603 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002604 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2605 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002606 {
2607 if (patc != pat)
2608 vim_free(patc);
2609 return FAIL;
2610 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
2612 /*
2613 * round == 1: Count the matches.
2614 * round == 2: Build the array to keep the matches.
2615 */
2616 for (round = 1; round <= 2; ++round)
2617 {
2618 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002619 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 {
2621 if (!buf->b_p_bl) /* skip unlisted buffers */
2622 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002623 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 if (p != NULL)
2625 {
2626 if (round == 1)
2627 ++count;
2628 else
2629 {
2630 if (options & WILD_HOME_REPLACE)
2631 p = home_replace_save(buf, p);
2632 else
2633 p = vim_strsave(p);
2634 (*file)[count++] = p;
2635 }
2636 }
2637 }
2638 if (count == 0) /* no match found, break here */
2639 break;
2640 if (round == 1)
2641 {
2642 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2643 if (*file == NULL)
2644 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002645 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002646 if (patc != pat)
2647 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 return FAIL;
2649 }
2650 }
2651 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002652 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002653 if (count) /* match(es) found, break here */
2654 break;
2655 }
2656
Bram Moolenaar05159a02005-02-26 23:04:13 +00002657 if (patc != pat)
2658 vim_free(patc);
2659
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 *num_file = count;
2661 return (count == 0 ? FAIL : OK);
2662}
2663
2664#endif /* FEAT_CMDL_COMPL */
2665
2666#ifdef HAVE_BUFLIST_MATCH
2667/*
2668 * Check for a match on the file name for buffer "buf" with regprog "prog".
2669 */
2670 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002671buflist_match(
2672 regmatch_T *rmp,
2673 buf_T *buf,
2674 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675{
2676 char_u *match;
2677
2678 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002679 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002680 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002681 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002682
2683 return match;
2684}
2685
2686/*
2687 * Try matching the regexp in "prog" with file name "name".
2688 * Return "name" when there is a match, NULL when not.
2689 */
2690 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002691fname_match(
2692 regmatch_T *rmp,
2693 char_u *name,
2694 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695{
2696 char_u *match = NULL;
2697 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
2699 if (name != NULL)
2700 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002701 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002702 rmp->rm_ic = p_fic || ignore_case;
2703 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 match = name;
2705 else
2706 {
2707 /* Replace $(HOME) with '~' and try matching again. */
2708 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002709 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 match = name;
2711 vim_free(p);
2712 }
2713 }
2714
2715 return match;
2716}
2717#endif
2718
2719/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002720 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 */
2722 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002723buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002725 char_u key[VIM_SIZEOF_INT * 2 + 1];
2726 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
2728 if (nr == 0)
2729 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002730 sprintf((char *)key, "%x", nr);
2731 hi = hash_find(&buf_hashtab, key);
2732
2733 if (!HASHITEM_EMPTY(hi))
2734 return (buf_T *)(hi->hi_key
2735 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 return NULL;
2737}
2738
2739/*
2740 * Get name of file 'n' in the buffer list.
2741 * When the file has no name an empty string is returned.
2742 * home_replace() is used to shorten the file name (used for marks).
2743 * Returns a pointer to allocated memory, of NULL when failed.
2744 */
2745 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002746buflist_nr2name(
2747 int n,
2748 int fullname,
2749 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750{
2751 buf_T *buf;
2752
2753 buf = buflist_findnr(n);
2754 if (buf == NULL)
2755 return NULL;
2756 return home_replace_save(helptail ? buf : NULL,
2757 fullname ? buf->b_ffname : buf->b_fname);
2758}
2759
2760/*
2761 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2762 * When "copy_options" is TRUE save the local window option values.
2763 * When "lnum" is 0 only do the options.
2764 */
2765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002766buflist_setfpos(
2767 buf_T *buf,
2768 win_T *win,
2769 linenr_T lnum,
2770 colnr_T col,
2771 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
2773 wininfo_T *wip;
2774
2775 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2776 if (wip->wi_win == win)
2777 break;
2778 if (wip == NULL)
2779 {
2780 /* allocate a new entry */
2781 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2782 if (wip == NULL)
2783 return;
2784 wip->wi_win = win;
2785 if (lnum == 0) /* set lnum even when it's 0 */
2786 lnum = 1;
2787 }
2788 else
2789 {
2790 /* remove the entry from the list */
2791 if (wip->wi_prev)
2792 wip->wi_prev->wi_next = wip->wi_next;
2793 else
2794 buf->b_wininfo = wip->wi_next;
2795 if (wip->wi_next)
2796 wip->wi_next->wi_prev = wip->wi_prev;
2797 if (copy_options && wip->wi_optset)
2798 {
2799 clear_winopt(&wip->wi_opt);
2800#ifdef FEAT_FOLDING
2801 deleteFoldRecurse(&wip->wi_folds);
2802#endif
2803 }
2804 }
2805 if (lnum != 0)
2806 {
2807 wip->wi_fpos.lnum = lnum;
2808 wip->wi_fpos.col = col;
2809 }
2810 if (copy_options)
2811 {
2812 /* Save the window-specific option values. */
2813 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2814#ifdef FEAT_FOLDING
2815 wip->wi_fold_manual = win->w_fold_manual;
2816 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2817#endif
2818 wip->wi_optset = TRUE;
2819 }
2820
2821 /* insert the entry in front of the list */
2822 wip->wi_next = buf->b_wininfo;
2823 buf->b_wininfo = wip;
2824 wip->wi_prev = NULL;
2825 if (wip->wi_next)
2826 wip->wi_next->wi_prev = wip;
2827
2828 return;
2829}
2830
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002831#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002832static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002833
2834/*
2835 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2836 * page. That's because a diff is local to a tab page.
2837 */
2838 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002839wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002840{
2841 win_T *wp;
2842
2843 if (wip->wi_opt.wo_diff)
2844 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002845 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002846 /* return FALSE when it's a window in the current tab page, thus
2847 * the buffer was in diff mode here */
2848 if (wip->wi_win == wp)
2849 return FALSE;
2850 return TRUE;
2851 }
2852 return FALSE;
2853}
2854#endif
2855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856/*
2857 * Find info for the current window in buffer "buf".
2858 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002859 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2860 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 * Returns NULL when there isn't any info.
2862 */
2863 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002864find_wininfo(
2865 buf_T *buf,
2866 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 wininfo_T *wip;
2869
2870 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002871 if (wip->wi_win == curwin
2872#ifdef FEAT_DIFF
2873 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2874#endif
2875 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002877
2878 /* If no wininfo for curwin, use the first in the list (that doesn't have
2879 * 'diff' set and is in another tab page). */
2880 if (wip == NULL)
2881 {
2882#ifdef FEAT_DIFF
2883 if (skip_diff_buffer)
2884 {
2885 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2886 if (!wininfo_other_tab_diff(wip))
2887 break;
2888 }
2889 else
2890#endif
2891 wip = buf->b_wininfo;
2892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 return wip;
2894}
2895
2896/*
2897 * Reset the local window options to the values last used in this window.
2898 * If the buffer wasn't used in this window before, use the values from
2899 * the most recently used window. If the values were never set, use the
2900 * global values for the window.
2901 */
2902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002903get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904{
2905 wininfo_T *wip;
2906
2907 clear_winopt(&curwin->w_onebuf_opt);
2908#ifdef FEAT_FOLDING
2909 clearFolding(curwin);
2910#endif
2911
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002912 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 if (wip != NULL && wip->wi_optset)
2914 {
2915 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2916#ifdef FEAT_FOLDING
2917 curwin->w_fold_manual = wip->wi_fold_manual;
2918 curwin->w_foldinvalid = TRUE;
2919 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2920#endif
2921 }
2922 else
2923 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2924
2925#ifdef FEAT_FOLDING
2926 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2927 if (p_fdls >= 0)
2928 curwin->w_p_fdl = p_fdls;
2929#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002930#ifdef FEAT_SYN_HL
2931 check_colorcolumn(curwin);
2932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933}
2934
2935/*
2936 * Find the position (lnum and col) for the buffer 'buf' for the current
2937 * window.
2938 * Returns a pointer to no_position if no position is found.
2939 */
2940 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002941buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002942{
2943 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002944 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002946 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002947 if (wip != NULL)
2948 return &(wip->wi_fpos);
2949 else
2950 return &no_position;
2951}
2952
2953/*
2954 * Find the lnum for the buffer 'buf' for the current window.
2955 */
2956 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002957buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
2959 return buflist_findfpos(buf)->lnum;
2960}
2961
2962#if defined(FEAT_LISTCMDS) || defined(PROTO)
2963/*
2964 * List all know file names (for :files and :buffers command).
2965 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002967buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968{
2969 buf_T *buf;
2970 int len;
2971 int i;
2972
2973 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2974 {
2975 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002976 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2977 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2978 || (vim_strchr(eap->arg, '+')
2979 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2980 || (vim_strchr(eap->arg, 'a')
2981 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2982 || (vim_strchr(eap->arg, 'h')
2983 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2984 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2985 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2986 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2987 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2988 || (vim_strchr(eap->arg, '#')
2989 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002992 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 else
2994 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002995 if (message_filtered(NameBuff))
2996 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002998 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00002999 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000 buf->b_fnum,
3001 buf->b_p_bl ? ' ' : 'u',
3002 buf == curbuf ? '%' :
3003 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
3004 buf->b_ml.ml_mfp == NULL ? ' ' :
3005 (buf->b_nwindows == 0 ? 'h' : 'a'),
3006 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
3007 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00003008 : (bufIsChanged(buf) ? '+' : ' '),
3009 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01003010 if (len > IOSIZE - 20)
3011 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012
3013 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 i = 40 - vim_strsize(IObuff);
3015 do
3016 {
3017 IObuff[len++] = ' ';
3018 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003019 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
3020 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003021 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 msg_outtrans(IObuff);
3023 out_flush(); /* output one line at a time */
3024 ui_breakcheck();
3025 }
3026}
3027#endif
3028
3029/*
3030 * Get file name and line number for file 'fnum'.
3031 * Used by DoOneCmd() for translating '%' and '#'.
3032 * Used by insert_reg() and cmdline_paste() for '#' register.
3033 * Return FAIL if not found, OK for success.
3034 */
3035 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003036buflist_name_nr(
3037 int fnum,
3038 char_u **fname,
3039 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040{
3041 buf_T *buf;
3042
3043 buf = buflist_findnr(fnum);
3044 if (buf == NULL || buf->b_fname == NULL)
3045 return FAIL;
3046
3047 *fname = buf->b_fname;
3048 *lnum = buflist_findlnum(buf);
3049
3050 return OK;
3051}
3052
3053/*
3054 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3055 * The file name with the full path is also remembered, for when :cd is used.
3056 * Returns FAIL for failure (file name already in use by other buffer)
3057 * OK otherwise.
3058 */
3059 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003060setfname(
3061 buf_T *buf,
3062 char_u *ffname,
3063 char_u *sfname,
3064 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065{
Bram Moolenaar81695252004-12-29 20:58:21 +00003066 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003068 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069#endif
3070
3071 if (ffname == NULL || *ffname == NUL)
3072 {
3073 /* Removing the name. */
3074 vim_free(buf->b_ffname);
3075 vim_free(buf->b_sfname);
3076 buf->b_ffname = NULL;
3077 buf->b_sfname = NULL;
3078#ifdef UNIX
3079 st.st_dev = (dev_T)-1;
3080#endif
3081 }
3082 else
3083 {
3084 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3085 if (ffname == NULL) /* out of memory */
3086 return FAIL;
3087
3088 /*
3089 * if the file name is already used in another buffer:
3090 * - if the buffer is loaded, fail
3091 * - if the buffer is not loaded, delete it from the list
3092 */
3093#ifdef UNIX
3094 if (mch_stat((char *)ffname, &st) < 0)
3095 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003096#endif
3097 if (!(buf->b_flags & BF_DUMMY))
3098#ifdef UNIX
3099 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003101 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102#endif
3103 if (obuf != NULL && obuf != buf)
3104 {
3105 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3106 {
3107 if (message)
3108 EMSG(_("E95: Buffer with this name already exists"));
3109 vim_free(ffname);
3110 return FAIL;
3111 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003112 /* delete from the list */
3113 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 }
3115 sfname = vim_strsave(sfname);
3116 if (ffname == NULL || sfname == NULL)
3117 {
3118 vim_free(sfname);
3119 vim_free(ffname);
3120 return FAIL;
3121 }
3122#ifdef USE_FNAME_CASE
3123# ifdef USE_LONG_FNAME
3124 if (USE_LONG_FNAME)
3125# endif
3126 fname_case(sfname, 0); /* set correct case for short file name */
3127#endif
3128 vim_free(buf->b_ffname);
3129 vim_free(buf->b_sfname);
3130 buf->b_ffname = ffname;
3131 buf->b_sfname = sfname;
3132 }
3133 buf->b_fname = buf->b_sfname;
3134#ifdef UNIX
3135 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003136 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 else
3138 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003139 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 buf->b_dev = st.st_dev;
3141 buf->b_ino = st.st_ino;
3142 }
3143#endif
3144
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146
3147 buf_name_changed(buf);
3148 return OK;
3149}
3150
3151/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003152 * Crude way of changing the name of a buffer. Use with care!
3153 * The name should be relative to the current directory.
3154 */
3155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003156buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003157{
3158 buf_T *buf;
3159
3160 buf = buflist_findnr(fnum);
3161 if (buf != NULL)
3162 {
3163 vim_free(buf->b_sfname);
3164 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003165 buf->b_ffname = vim_strsave(name);
3166 buf->b_sfname = NULL;
3167 /* Allocate ffname and expand into full path. Also resolves .lnk
3168 * files on Win32. */
3169 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003170 buf->b_fname = buf->b_sfname;
3171 }
3172}
3173
3174/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003175 * Take care of what needs to be done when the name of buffer "buf" has
3176 * changed.
3177 */
3178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003179buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180{
3181 /*
3182 * If the file name changed, also change the name of the swapfile
3183 */
3184 if (buf->b_ml.ml_mfp != NULL)
3185 ml_setname(buf);
3186
3187 if (curwin->w_buffer == buf)
3188 check_arg_idx(curwin); /* check file name for arg list */
3189#ifdef FEAT_TITLE
3190 maketitle(); /* set window title */
3191#endif
3192#ifdef FEAT_WINDOWS
3193 status_redraw_all(); /* status lines need to be redrawn */
3194#endif
3195 fmarks_check_names(buf); /* check named file marks */
3196 ml_timestamp(buf); /* reset timestamp */
3197}
3198
3199/*
3200 * set alternate file name for current window
3201 *
3202 * Used by do_one_cmd(), do_write() and do_ecmd().
3203 * Return the buffer.
3204 */
3205 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003206setaltfname(
3207 char_u *ffname,
3208 char_u *sfname,
3209 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210{
3211 buf_T *buf;
3212
3213 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3214 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003215 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 curwin->w_alt_fnum = buf->b_fnum;
3217 return buf;
3218}
3219
3220/*
3221 * Get alternate file name for current window.
3222 * Return NULL if there isn't any, and give error message if requested.
3223 */
3224 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225getaltfname(
3226 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227{
3228 char_u *fname;
3229 linenr_T dummy;
3230
3231 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3232 {
3233 if (errmsg)
3234 EMSG(_(e_noalt));
3235 return NULL;
3236 }
3237 return fname;
3238}
3239
3240/*
3241 * Add a file name to the buflist and return its number.
3242 * Uses same flags as buflist_new(), except BLN_DUMMY.
3243 *
3244 * used by qf_init(), main() and doarglist()
3245 */
3246 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003247buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248{
3249 buf_T *buf;
3250
3251 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3252 if (buf != NULL)
3253 return buf->b_fnum;
3254 return 0;
3255}
3256
3257#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3258/*
3259 * Adjust slashes in file names. Called after 'shellslash' was set.
3260 */
3261 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003262buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263{
3264 buf_T *bp;
3265
Bram Moolenaar29323592016-07-24 22:04:11 +02003266 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 {
3268 if (bp->b_ffname != NULL)
3269 slash_adjust(bp->b_ffname);
3270 if (bp->b_sfname != NULL)
3271 slash_adjust(bp->b_sfname);
3272 }
3273}
3274#endif
3275
3276/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003277 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 * Also save the local window option values.
3279 */
3280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003281buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003283 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284}
3285
3286/*
3287 * Return TRUE if 'ffname' is not the same file as current file.
3288 * Fname must have a full path (expanded by mch_FullName()).
3289 */
3290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003291otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292{
3293 return otherfile_buf(curbuf, ffname
3294#ifdef UNIX
3295 , NULL
3296#endif
3297 );
3298}
3299
3300 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003301otherfile_buf(
3302 buf_T *buf,
3303 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003305 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003307 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309 /* no name is different */
3310 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3311 return TRUE;
3312 if (fnamecmp(ffname, buf->b_ffname) == 0)
3313 return FALSE;
3314#ifdef UNIX
3315 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003316 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
Bram Moolenaar8767f522016-07-01 17:17:39 +02003318 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 if (stp == NULL)
3320 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003321 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 st.st_dev = (dev_T)-1;
3323 stp = &st;
3324 }
3325 /* Use dev/ino to check if the files are the same, even when the names
3326 * are different (possible with links). Still need to compare the
3327 * name above, for when the file doesn't exist yet.
3328 * Problem: The dev/ino changes when a file is deleted (and created
3329 * again) and remains the same when renamed/moved. We don't want to
3330 * mch_stat() each buffer each time, that would be too slow. Get the
3331 * dev/ino again when they appear to match, but not when they appear
3332 * to be different: Could skip a buffer when it's actually the same
3333 * file. */
3334 if (buf_same_ino(buf, stp))
3335 {
3336 buf_setino(buf);
3337 if (buf_same_ino(buf, stp))
3338 return FALSE;
3339 }
3340 }
3341#endif
3342 return TRUE;
3343}
3344
3345#if defined(UNIX) || defined(PROTO)
3346/*
3347 * Set inode and device number for a buffer.
3348 * Must always be called when b_fname is changed!.
3349 */
3350 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003351buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003353 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354
3355 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3356 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003357 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 buf->b_dev = st.st_dev;
3359 buf->b_ino = st.st_ino;
3360 }
3361 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003362 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363}
3364
3365/*
3366 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3367 */
3368 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003369buf_same_ino(
3370 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003371 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003373 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 && stp->st_dev == buf->b_dev
3375 && stp->st_ino == buf->b_ino);
3376}
3377#endif
3378
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003379/*
3380 * Print info about the current buffer.
3381 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003383fileinfo(
3384 int fullname, /* when non-zero print full path */
3385 int shorthelp,
3386 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387{
3388 char_u *name;
3389 int n;
3390 char_u *p;
3391 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003392 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
3394 buffer = alloc(IOSIZE);
3395 if (buffer == NULL)
3396 return;
3397
3398 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3399 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003400 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 p = buffer + STRLEN(buffer);
3402 }
3403 else
3404 p = buffer;
3405
3406 *p++ = '"';
3407 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003408 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 else
3410 {
3411 if (!fullname && curbuf->b_fname != NULL)
3412 name = curbuf->b_fname;
3413 else
3414 name = curbuf->b_ffname;
3415 home_replace(shorthelp ? curbuf : NULL, name, p,
3416 (int)(IOSIZE - (p - buffer)), TRUE);
3417 }
3418
Bram Moolenaara800b422010-06-27 01:15:55 +02003419 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 curbufIsChanged() ? (shortmess(SHM_MOD)
3421 ? " [+]" : _(" [Modified]")) : " ",
3422 (curbuf->b_flags & BF_NOTEDITED)
3423#ifdef FEAT_QUICKFIX
3424 && !bt_dontwrite(curbuf)
3425#endif
3426 ? _("[Not edited]") : "",
3427 (curbuf->b_flags & BF_NEW)
3428#ifdef FEAT_QUICKFIX
3429 && !bt_dontwrite(curbuf)
3430#endif
3431 ? _("[New file]") : "",
3432 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003433 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 : _("[readonly]")) : "",
3435 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3436 || curbuf->b_p_ro) ?
3437 " " : "");
3438 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3439 * causes an overflow, thus for large numbers divide instead. */
3440 if (curwin->w_cursor.lnum > 1000000L)
3441 n = (int)(((long)curwin->w_cursor.lnum) /
3442 ((long)curbuf->b_ml.ml_line_count / 100L));
3443 else
3444 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3445 (long)curbuf->b_ml.ml_line_count);
3446 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3447 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003448 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 }
3450#ifdef FEAT_CMDL_INFO
3451 else if (p_ru)
3452 {
3453 /* Current line and column are already on the screen -- webb */
3454 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003455 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003457 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 (long)curbuf->b_ml.ml_line_count, n);
3459 }
3460#endif
3461 else
3462 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003463 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 _("line %ld of %ld --%d%%-- col "),
3465 (long)curwin->w_cursor.lnum,
3466 (long)curbuf->b_ml.ml_line_count,
3467 n);
3468 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003469 len = STRLEN(buffer);
3470 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3472 }
3473
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003474 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
3476 if (dont_truncate)
3477 {
3478 /* Temporarily set msg_scroll to avoid the message being truncated.
3479 * First call msg_start() to get the message in the right place. */
3480 msg_start();
3481 n = msg_scroll;
3482 msg_scroll = TRUE;
3483 msg(buffer);
3484 msg_scroll = n;
3485 }
3486 else
3487 {
3488 p = msg_trunc_attr(buffer, FALSE, 0);
3489 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 /* Need to repeat the message after redrawing when:
3491 * - When restart_edit is set (otherwise there will be a delay
3492 * before redrawing).
3493 * - When the screen was scrolled but there is no wait-return
3494 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003495 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 }
3497
3498 vim_free(buffer);
3499}
3500
3501 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003502col_print(
3503 char_u *buf,
3504 size_t buflen,
3505 int col,
3506 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507{
3508 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003509 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003511 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512}
3513
3514#if defined(FEAT_TITLE) || defined(PROTO)
3515/*
3516 * put file name in title bar of window and in icon title
3517 */
3518
3519static char_u *lasttitle = NULL;
3520static char_u *lasticon = NULL;
3521
3522 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003523maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524{
3525 char_u *p;
3526 char_u *t_str = NULL;
3527 char_u *i_name;
3528 char_u *i_str = NULL;
3529 int maxlen = 0;
3530 int len;
3531 int mustset;
3532 char_u buf[IOSIZE];
3533 int off;
3534
3535 if (!redrawing())
3536 {
3537 /* Postpone updating the title when 'lazyredraw' is set. */
3538 need_maketitle = TRUE;
3539 return;
3540 }
3541
3542 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003543 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 return;
3545
3546 if (p_title)
3547 {
3548 if (p_titlelen > 0)
3549 {
3550 maxlen = p_titlelen * Columns / 100;
3551 if (maxlen < 10)
3552 maxlen = 10;
3553 }
3554
3555 t_str = buf;
3556 if (*p_titlestring != NUL)
3557 {
3558#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003559 if (stl_syntax & STL_IN_TITLE)
3560 {
3561 int use_sandbox = FALSE;
3562 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003563
3564# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003565 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003566# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003567 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003569 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003570 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003571 if (called_emsg)
3572 set_string_option_direct((char_u *)"titlestring", -1,
3573 (char_u *)"", OPT_FREE, SID_ERROR);
3574 called_emsg |= save_called_emsg;
3575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 else
3577#endif
3578 t_str = p_titlestring;
3579 }
3580 else
3581 {
3582 /* format: "fname + (path) (1 of 2) - VIM" */
3583
Bram Moolenaar2c666692012-09-05 13:30:40 +02003584#define SPACE_FOR_FNAME (IOSIZE - 100)
3585#define SPACE_FOR_DIR (IOSIZE - 20)
3586#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003588 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 else
3590 {
3591 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003592 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 }
3595
3596 switch (bufIsChanged(curbuf)
3597 + (curbuf->b_p_ro * 2)
3598 + (!curbuf->b_p_ma * 4))
3599 {
3600 case 1: STRCAT(buf, " +"); break;
3601 case 2: STRCAT(buf, " ="); break;
3602 case 3: STRCAT(buf, " =+"); break;
3603 case 4:
3604 case 6: STRCAT(buf, " -"); break;
3605 case 5:
3606 case 7: STRCAT(buf, " -+"); break;
3607 }
3608
3609 if (curbuf->b_fname != NULL)
3610 {
3611 /* Get path of file, replace home dir with ~ */
3612 off = (int)STRLEN(buf);
3613 buf[off++] = ' ';
3614 buf[off++] = '(';
3615 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003616 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617#ifdef BACKSLASH_IN_FILENAME
3618 /* avoid "c:/name" to be reduced to "c" */
3619 if (isalpha(buf[off]) && buf[off + 1] == ':')
3620 off += 2;
3621#endif
3622 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003623 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003626 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003627 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003630
Bram Moolenaar2c666692012-09-05 13:30:40 +02003631 /* Translate unprintable chars and concatenate. Keep some
3632 * room for the server name. When there is no room (very long
3633 * file name) use (...). */
3634 if (off < SPACE_FOR_DIR)
3635 {
3636 p = transstr(buf + off);
3637 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3638 vim_free(p);
3639 }
3640 else
3641 {
3642 vim_strncpy(buf + off, (char_u *)"...",
3643 (size_t)(SPACE_FOR_ARGNR - off));
3644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645 STRCAT(buf, ")");
3646 }
3647
Bram Moolenaar2c666692012-09-05 13:30:40 +02003648 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649
3650#if defined(FEAT_CLIENTSERVER)
3651 if (serverName != NULL)
3652 {
3653 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003654 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 }
3656 else
3657#endif
3658 STRCAT(buf, " - VIM");
3659
3660 if (maxlen > 0)
3661 {
3662 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003663 if (vim_strsize(buf) > maxlen)
3664 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 }
3666 }
3667 }
3668 mustset = ti_change(t_str, &lasttitle);
3669
3670 if (p_icon)
3671 {
3672 i_str = buf;
3673 if (*p_iconstring != NUL)
3674 {
3675#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003676 if (stl_syntax & STL_IN_ICON)
3677 {
3678 int use_sandbox = FALSE;
3679 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003680
3681# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003682 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003683# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003684 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003686 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003687 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003688 if (called_emsg)
3689 set_string_option_direct((char_u *)"iconstring", -1,
3690 (char_u *)"", OPT_FREE, SID_ERROR);
3691 called_emsg |= save_called_emsg;
3692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 else
3694#endif
3695 i_str = p_iconstring;
3696 }
3697 else
3698 {
3699 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003700 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 else /* use file name only in icon */
3702 i_name = gettail(curbuf->b_ffname);
3703 *i_str = NUL;
3704 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003705 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 if (len > 100)
3707 {
3708 len -= 100;
3709#ifdef FEAT_MBYTE
3710 if (has_mbyte)
3711 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3712#endif
3713 i_name += len;
3714 }
3715 STRCPY(i_str, i_name);
3716 trans_characters(i_str, IOSIZE);
3717 }
3718 }
3719
3720 mustset |= ti_change(i_str, &lasticon);
3721
3722 if (mustset)
3723 resettitle();
3724}
3725
3726/*
3727 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3728 * from "str" if it does.
3729 * Return TRUE when "*last" changed.
3730 */
3731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003732ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733{
3734 if ((str == NULL) != (*last == NULL)
3735 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3736 {
3737 vim_free(*last);
3738 if (str == NULL)
3739 *last = NULL;
3740 else
3741 *last = vim_strsave(str);
3742 return TRUE;
3743 }
3744 return FALSE;
3745}
3746
3747/*
3748 * Put current window title back (used after calling a shell)
3749 */
3750 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003751resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752{
3753 mch_settitle(lasttitle, lasticon);
3754}
Bram Moolenaarea408852005-06-25 22:49:46 +00003755
3756# if defined(EXITFREE) || defined(PROTO)
3757 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003758free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003759{
3760 vim_free(lasttitle);
3761 vim_free(lasticon);
3762}
3763# endif
3764
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765#endif /* FEAT_TITLE */
3766
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003767#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003769 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 * Return length of string in screen cells.
3771 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003772 * Normally works for window "wp", except when working for 'tabline' then it
3773 * is "curwin".
3774 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 * Items are drawn interspersed with the text that surrounds it
3776 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3777 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3778 *
3779 * If maxwidth is not zero, the string will be filled at any middle marker
3780 * or truncated if too long, fillchar is used for all whitespace.
3781 */
3782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003783build_stl_str_hl(
3784 win_T *wp,
3785 char_u *out, /* buffer to write into != NameBuff */
3786 size_t outlen, /* length of out[] */
3787 char_u *fmt,
3788 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3789 int fillchar,
3790 int maxwidth,
3791 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3792 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793{
3794 char_u *p;
3795 char_u *s;
3796 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003797 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798#ifdef FEAT_EVAL
3799 win_T *o_curwin;
3800 buf_T *o_curbuf;
3801#endif
3802 int empty_line;
3803 colnr_T virtcol;
3804 long l;
3805 long n;
3806 int prevchar_isflag;
3807 int prevchar_isitem;
3808 int itemisflag;
3809 int fillable;
3810 char_u *str;
3811 long num;
3812 int width;
3813 int itemcnt;
3814 int curitem;
3815 int groupitem[STL_MAX_ITEM];
3816 int groupdepth;
3817 struct stl_item
3818 {
3819 char_u *start;
3820 int minwid;
3821 int maxwid;
3822 enum
3823 {
3824 Normal,
3825 Empty,
3826 Group,
3827 Middle,
3828 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003829 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 Trunc
3831 } type;
3832 } item[STL_MAX_ITEM];
3833 int minwid;
3834 int maxwid;
3835 int zeropad;
3836 char_u base;
3837 char_u opt;
3838#define TMPLEN 70
3839 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003840 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003841 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003842
3843#ifdef FEAT_EVAL
3844 /*
3845 * When the format starts with "%!" then evaluate it as an expression and
3846 * use the result as the actual format string.
3847 */
3848 if (fmt[0] == '%' && fmt[1] == '!')
3849 {
3850 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3851 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003852 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003853 }
3854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855
3856 if (fillchar == 0)
3857 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003858#ifdef FEAT_MBYTE
3859 /* Can't handle a multi-byte fill character yet. */
3860 else if (mb_char2len(fillchar) > 1)
3861 fillchar = '-';
3862#endif
3863
Bram Moolenaar567199b2013-04-24 16:52:36 +02003864 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3865 * p will become invalid when getting another buffer line. */
3866 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3867 empty_line = (*p == NUL);
3868
3869 /* Get the byte value now, in case we need it below. This is more
3870 * efficient than making a copy of the line. */
3871 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3872 byteval = 0;
3873 else
3874#ifdef FEAT_MBYTE
3875 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3876#else
3877 byteval = p[wp->w_cursor.col];
3878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879
3880 groupdepth = 0;
3881 p = out;
3882 curitem = 0;
3883 prevchar_isflag = TRUE;
3884 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003885 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003887 if (curitem == STL_MAX_ITEM)
3888 {
3889 /* There are too many items. Add the error code to the statusline
3890 * to give the user a hint about what went wrong. */
3891 if (p + 6 < out + outlen)
3892 {
3893 mch_memmove(p, " E541", (size_t)5);
3894 p += 5;
3895 }
3896 break;
3897 }
3898
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 if (*s != NUL && *s != '%')
3900 prevchar_isflag = prevchar_isitem = FALSE;
3901
3902 /*
3903 * Handle up to the next '%' or the end.
3904 */
3905 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3906 *p++ = *s++;
3907 if (*s == NUL || p + 1 >= out + outlen)
3908 break;
3909
3910 /*
3911 * Handle one '%' item.
3912 */
3913 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003914 if (*s == NUL) /* ignore trailing % */
3915 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 if (*s == '%')
3917 {
3918 if (p + 1 >= out + outlen)
3919 break;
3920 *p++ = *s++;
3921 prevchar_isflag = prevchar_isitem = FALSE;
3922 continue;
3923 }
3924 if (*s == STL_MIDDLEMARK)
3925 {
3926 s++;
3927 if (groupdepth > 0)
3928 continue;
3929 item[curitem].type = Middle;
3930 item[curitem++].start = p;
3931 continue;
3932 }
3933 if (*s == STL_TRUNCMARK)
3934 {
3935 s++;
3936 item[curitem].type = Trunc;
3937 item[curitem++].start = p;
3938 continue;
3939 }
3940 if (*s == ')')
3941 {
3942 s++;
3943 if (groupdepth < 1)
3944 continue;
3945 groupdepth--;
3946
3947 t = item[groupitem[groupdepth]].start;
3948 *p = NUL;
3949 l = vim_strsize(t);
3950 if (curitem > groupitem[groupdepth] + 1
3951 && item[groupitem[groupdepth]].minwid == 0)
3952 {
3953 /* remove group if all items are empty */
3954 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003955 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 break;
3957 if (n == curitem)
3958 {
3959 p = t;
3960 l = 0;
3961 }
3962 }
3963 if (l > item[groupitem[groupdepth]].maxwid)
3964 {
3965 /* truncate, remove n bytes of text at the start */
3966#ifdef FEAT_MBYTE
3967 if (has_mbyte)
3968 {
3969 /* Find the first character that should be included. */
3970 n = 0;
3971 while (l >= item[groupitem[groupdepth]].maxwid)
3972 {
3973 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003974 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 }
3976 }
3977 else
3978#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003979 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003982 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 p = p - n + 1;
3984#ifdef FEAT_MBYTE
3985 /* Fill up space left over by half a double-wide char. */
3986 while (++l < item[groupitem[groupdepth]].minwid)
3987 *p++ = fillchar;
3988#endif
3989
3990 /* correct the start of the items for the truncation */
3991 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3992 {
3993 item[l].start -= n;
3994 if (item[l].start < t)
3995 item[l].start = t;
3996 }
3997 }
3998 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3999 {
4000 /* fill */
4001 n = item[groupitem[groupdepth]].minwid;
4002 if (n < 0)
4003 {
4004 /* fill by appending characters */
4005 n = 0 - n;
4006 while (l++ < n && p + 1 < out + outlen)
4007 *p++ = fillchar;
4008 }
4009 else
4010 {
4011 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004012 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013 l = n - l;
4014 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004015 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 p += l;
4017 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
4018 item[n].start += l;
4019 for ( ; l > 0; l--)
4020 *t++ = fillchar;
4021 }
4022 }
4023 continue;
4024 }
4025 minwid = 0;
4026 maxwid = 9999;
4027 zeropad = FALSE;
4028 l = 1;
4029 if (*s == '0')
4030 {
4031 s++;
4032 zeropad = TRUE;
4033 }
4034 if (*s == '-')
4035 {
4036 s++;
4037 l = -1;
4038 }
4039 if (VIM_ISDIGIT(*s))
4040 {
4041 minwid = (int)getdigits(&s);
4042 if (minwid < 0) /* overflow */
4043 minwid = 0;
4044 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004045 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 {
4047 item[curitem].type = Highlight;
4048 item[curitem].start = p;
4049 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4050 s++;
4051 curitem++;
4052 continue;
4053 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004054 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4055 {
4056 if (*s == STL_TABCLOSENR)
4057 {
4058 if (minwid == 0)
4059 {
4060 /* %X ends the close label, go back to the previously
4061 * define tab label nr. */
4062 for (n = curitem - 1; n >= 0; --n)
4063 if (item[n].type == TabPage && item[n].minwid >= 0)
4064 {
4065 minwid = item[n].minwid;
4066 break;
4067 }
4068 }
4069 else
4070 /* close nrs are stored as negative values */
4071 minwid = - minwid;
4072 }
4073 item[curitem].type = TabPage;
4074 item[curitem].start = p;
4075 item[curitem].minwid = minwid;
4076 s++;
4077 curitem++;
4078 continue;
4079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 if (*s == '.')
4081 {
4082 s++;
4083 if (VIM_ISDIGIT(*s))
4084 {
4085 maxwid = (int)getdigits(&s);
4086 if (maxwid <= 0) /* overflow */
4087 maxwid = 50;
4088 }
4089 }
4090 minwid = (minwid > 50 ? 50 : minwid) * l;
4091 if (*s == '(')
4092 {
4093 groupitem[groupdepth++] = curitem;
4094 item[curitem].type = Group;
4095 item[curitem].start = p;
4096 item[curitem].minwid = minwid;
4097 item[curitem].maxwid = maxwid;
4098 s++;
4099 curitem++;
4100 continue;
4101 }
4102 if (vim_strchr(STL_ALL, *s) == NULL)
4103 {
4104 s++;
4105 continue;
4106 }
4107 opt = *s++;
4108
4109 /* OK - now for the real work */
4110 base = 'D';
4111 itemisflag = FALSE;
4112 fillable = TRUE;
4113 num = -1;
4114 str = NULL;
4115 switch (opt)
4116 {
4117 case STL_FILEPATH:
4118 case STL_FULLPATH:
4119 case STL_FILENAME:
4120 fillable = FALSE; /* don't change ' ' to fillchar */
4121 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004122 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 else
4124 {
4125 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004126 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4128 }
4129 trans_characters(NameBuff, MAXPATHL);
4130 if (opt != STL_FILENAME)
4131 str = NameBuff;
4132 else
4133 str = gettail(NameBuff);
4134 break;
4135
4136 case STL_VIM_EXPR: /* '{' */
4137 itemisflag = TRUE;
4138 t = p;
4139 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4140 *p++ = *s++;
4141 if (*s != '}') /* missing '}' or out of space */
4142 break;
4143 s++;
4144 *p = 0;
4145 p = t;
4146
4147#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004148 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4150
4151 o_curbuf = curbuf;
4152 o_curwin = curwin;
4153 curwin = wp;
4154 curbuf = wp->w_buffer;
4155
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004156 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157
4158 curwin = o_curwin;
4159 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004160 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161
4162 if (str != NULL && *str != 0)
4163 {
4164 if (*skipdigits(str) == NUL)
4165 {
4166 num = atoi((char *)str);
4167 vim_free(str);
4168 str = NULL;
4169 itemisflag = FALSE;
4170 }
4171 }
4172#endif
4173 break;
4174
4175 case STL_LINE:
4176 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4177 ? 0L : (long)(wp->w_cursor.lnum);
4178 break;
4179
4180 case STL_NUMLINES:
4181 num = wp->w_buffer->b_ml.ml_line_count;
4182 break;
4183
4184 case STL_COLUMN:
4185 num = !(State & INSERT) && empty_line
4186 ? 0 : (int)wp->w_cursor.col + 1;
4187 break;
4188
4189 case STL_VIRTCOL:
4190 case STL_VIRTCOL_ALT:
4191 /* In list mode virtcol needs to be recomputed */
4192 virtcol = wp->w_virtcol;
4193 if (wp->w_p_list && lcs_tab1 == NUL)
4194 {
4195 wp->w_p_list = FALSE;
4196 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4197 wp->w_p_list = TRUE;
4198 }
4199 ++virtcol;
4200 /* Don't display %V if it's the same as %c. */
4201 if (opt == STL_VIRTCOL_ALT
4202 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4203 ? 0 : (int)wp->w_cursor.col + 1)))
4204 break;
4205 num = (long)virtcol;
4206 break;
4207
4208 case STL_PERCENTAGE:
4209 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4210 (long)wp->w_buffer->b_ml.ml_line_count);
4211 break;
4212
4213 case STL_ALTPERCENT:
4214 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004215 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 break;
4217
4218 case STL_ARGLISTSTAT:
4219 fillable = FALSE;
4220 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004221 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 str = tmp;
4223 break;
4224
4225 case STL_KEYMAP:
4226 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004227 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 str = tmp;
4229 break;
4230 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004231#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004232 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233#else
4234 num = 0;
4235#endif
4236 break;
4237
4238 case STL_BUFNO:
4239 num = wp->w_buffer->b_fnum;
4240 break;
4241
4242 case STL_OFFSET_X:
4243 base = 'X';
4244 case STL_OFFSET:
4245#ifdef FEAT_BYTEOFF
4246 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4247 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4248 0L : l + 1 + (!(State & INSERT) && empty_line ?
4249 0 : (int)wp->w_cursor.col);
4250#endif
4251 break;
4252
4253 case STL_BYTEVAL_X:
4254 base = 'X';
4255 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004256 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 if (num == NL)
4258 num = 0;
4259 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4260 num = NL;
4261 break;
4262
4263 case STL_ROFLAG:
4264 case STL_ROFLAG_ALT:
4265 itemisflag = TRUE;
4266 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004267 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 break;
4269
4270 case STL_HELPFLAG:
4271 case STL_HELPFLAG_ALT:
4272 itemisflag = TRUE;
4273 if (wp->w_buffer->b_help)
4274 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004275 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 break;
4277
4278#ifdef FEAT_AUTOCMD
4279 case STL_FILETYPE:
4280 if (*wp->w_buffer->b_p_ft != NUL
4281 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4282 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004283 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4284 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 str = tmp;
4286 }
4287 break;
4288
4289 case STL_FILETYPE_ALT:
4290 itemisflag = TRUE;
4291 if (*wp->w_buffer->b_p_ft != NUL
4292 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4293 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004294 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4295 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 for (t = tmp; *t != 0; t++)
4297 *t = TOUPPER_LOC(*t);
4298 str = tmp;
4299 }
4300 break;
4301#endif
4302
4303#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4304 case STL_PREVIEWFLAG:
4305 case STL_PREVIEWFLAG_ALT:
4306 itemisflag = TRUE;
4307 if (wp->w_p_pvw)
4308 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4309 : _("[Preview]"));
4310 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004311
4312 case STL_QUICKFIX:
4313 if (bt_quickfix(wp->w_buffer))
4314 str = (char_u *)(wp->w_llist_ref
4315 ? _(msg_loclist)
4316 : _(msg_qflist));
4317 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318#endif
4319
4320 case STL_MODIFIED:
4321 case STL_MODIFIED_ALT:
4322 itemisflag = TRUE;
4323 switch ((opt == STL_MODIFIED_ALT)
4324 + bufIsChanged(wp->w_buffer) * 2
4325 + (!wp->w_buffer->b_p_ma) * 4)
4326 {
4327 case 2: str = (char_u *)"[+]"; break;
4328 case 3: str = (char_u *)",+"; break;
4329 case 4: str = (char_u *)"[-]"; break;
4330 case 5: str = (char_u *)",-"; break;
4331 case 6: str = (char_u *)"[+-]"; break;
4332 case 7: str = (char_u *)",+-"; break;
4333 }
4334 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004335
4336 case STL_HIGHLIGHT:
4337 t = s;
4338 while (*s != '#' && *s != NUL)
4339 ++s;
4340 if (*s == '#')
4341 {
4342 item[curitem].type = Highlight;
4343 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004344 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004345 curitem++;
4346 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004347 if (*s != NUL)
4348 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004349 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 }
4351
4352 item[curitem].start = p;
4353 item[curitem].type = Normal;
4354 if (str != NULL && *str)
4355 {
4356 t = str;
4357 if (itemisflag)
4358 {
4359 if ((t[0] && t[1])
4360 && ((!prevchar_isitem && *t == ',')
4361 || (prevchar_isflag && *t == ' ')))
4362 t++;
4363 prevchar_isflag = TRUE;
4364 }
4365 l = vim_strsize(t);
4366 if (l > 0)
4367 prevchar_isitem = TRUE;
4368 if (l > maxwid)
4369 {
4370 while (l >= maxwid)
4371#ifdef FEAT_MBYTE
4372 if (has_mbyte)
4373 {
4374 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004375 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 }
4377 else
4378#endif
4379 l -= byte2cells(*t++);
4380 if (p + 1 >= out + outlen)
4381 break;
4382 *p++ = '<';
4383 }
4384 if (minwid > 0)
4385 {
4386 for (; l < minwid && p + 1 < out + outlen; l++)
4387 {
4388 /* Don't put a "-" in front of a digit. */
4389 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4390 *p++ = ' ';
4391 else
4392 *p++ = fillchar;
4393 }
4394 minwid = 0;
4395 }
4396 else
4397 minwid *= -1;
4398 while (*t && p + 1 < out + outlen)
4399 {
4400 *p++ = *t++;
4401 /* Change a space by fillchar, unless fillchar is '-' and a
4402 * digit follows. */
4403 if (fillable && p[-1] == ' '
4404 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4405 p[-1] = fillchar;
4406 }
4407 for (; l < minwid && p + 1 < out + outlen; l++)
4408 *p++ = fillchar;
4409 }
4410 else if (num >= 0)
4411 {
4412 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4413 char_u nstr[20];
4414
4415 if (p + 20 >= out + outlen)
4416 break; /* not sufficient space */
4417 prevchar_isitem = TRUE;
4418 t = nstr;
4419 if (opt == STL_VIRTCOL_ALT)
4420 {
4421 *t++ = '-';
4422 minwid--;
4423 }
4424 *t++ = '%';
4425 if (zeropad)
4426 *t++ = '0';
4427 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004428 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 *t = 0;
4430
4431 for (n = num, l = 1; n >= nbase; n /= nbase)
4432 l++;
4433 if (opt == STL_VIRTCOL_ALT)
4434 l++;
4435 if (l > maxwid)
4436 {
4437 l += 2;
4438 n = l - maxwid;
4439 while (l-- > maxwid)
4440 num /= nbase;
4441 *t++ = '>';
4442 *t++ = '%';
4443 *t = t[-3];
4444 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004445 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4446 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004449 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4450 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 p += STRLEN(p);
4452 }
4453 else
4454 item[curitem].type = Empty;
4455
4456 if (opt == STL_VIM_EXPR)
4457 vim_free(str);
4458
4459 if (num >= 0 || (!itemisflag && str && *str))
4460 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4461 curitem++;
4462 }
4463 *p = NUL;
4464 itemcnt = curitem;
4465
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004466#ifdef FEAT_EVAL
4467 if (usefmt != fmt)
4468 vim_free(usefmt);
4469#endif
4470
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 width = vim_strsize(out);
4472 if (maxwidth > 0 && width > maxwidth)
4473 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004474 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 l = 0;
4476 if (itemcnt == 0)
4477 s = out;
4478 else
4479 {
4480 for ( ; l < itemcnt; l++)
4481 if (item[l].type == Trunc)
4482 {
4483 /* Truncate at %< item. */
4484 s = item[l].start;
4485 break;
4486 }
4487 if (l == itemcnt)
4488 {
4489 /* No %< item, truncate first item. */
4490 s = item[0].start;
4491 l = 0;
4492 }
4493 }
4494
4495 if (width - vim_strsize(s) >= maxwidth)
4496 {
4497 /* Truncation mark is beyond max length */
4498#ifdef FEAT_MBYTE
4499 if (has_mbyte)
4500 {
4501 s = out;
4502 width = 0;
4503 for (;;)
4504 {
4505 width += ptr2cells(s);
4506 if (width >= maxwidth)
4507 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004508 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 /* Fill up for half a double-wide character. */
4511 while (++width < maxwidth)
4512 *s++ = fillchar;
4513 }
4514 else
4515#endif
4516 s = out + maxwidth - 1;
4517 for (l = 0; l < itemcnt; l++)
4518 if (item[l].start > s)
4519 break;
4520 itemcnt = l;
4521 *s++ = '>';
4522 *s = 0;
4523 }
4524 else
4525 {
4526#ifdef FEAT_MBYTE
4527 if (has_mbyte)
4528 {
4529 n = 0;
4530 while (width >= maxwidth)
4531 {
4532 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004533 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 }
4535 }
4536 else
4537#endif
4538 n = width - maxwidth + 1;
4539 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004540 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 *s = '<';
4542
4543 /* Fill up for half a double-wide character. */
4544 while (++width < maxwidth)
4545 {
4546 s = s + STRLEN(s);
4547 *s++ = fillchar;
4548 *s = NUL;
4549 }
4550
4551 --n; /* count the '<' */
4552 for (; l < itemcnt; l++)
4553 {
4554 if (item[l].start - n >= s)
4555 item[l].start -= n;
4556 else
4557 item[l].start = s;
4558 }
4559 }
4560 width = maxwidth;
4561 }
4562 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4563 {
4564 /* Apply STL_MIDDLE if any */
4565 for (l = 0; l < itemcnt; l++)
4566 if (item[l].type == Middle)
4567 break;
4568 if (l < itemcnt)
4569 {
4570 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004571 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 for (s = item[l].start; s < p; s++)
4573 *s = fillchar;
4574 for (l++; l < itemcnt; l++)
4575 item[l].start += maxwidth - width;
4576 width = maxwidth;
4577 }
4578 }
4579
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004580 /* Store the info about highlighting. */
4581 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004583 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 for (l = 0; l < itemcnt; l++)
4585 {
4586 if (item[l].type == Highlight)
4587 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004588 sp->start = item[l].start;
4589 sp->userhl = item[l].minwid;
4590 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004593 sp->start = NULL;
4594 sp->userhl = 0;
4595 }
4596
4597 /* Store the info about tab pages labels. */
4598 if (tabtab != NULL)
4599 {
4600 sp = tabtab;
4601 for (l = 0; l < itemcnt; l++)
4602 {
4603 if (item[l].type == TabPage)
4604 {
4605 sp->start = item[l].start;
4606 sp->userhl = item[l].minwid;
4607 sp++;
4608 }
4609 }
4610 sp->start = NULL;
4611 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 }
4613
4614 return width;
4615}
4616#endif /* FEAT_STL_OPT */
4617
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004618#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4619 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004621 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4622 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 */
4624 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004625get_rel_pos(
4626 win_T *wp,
4627 char_u *buf,
4628 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 long above; /* number of lines above window */
4631 long below; /* number of lines below window */
4632
Bram Moolenaar0027c212015-01-07 13:31:52 +01004633 if (buflen < 3) /* need at least 3 chars for writing */
4634 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 above = wp->w_topline - 1;
4636#ifdef FEAT_DIFF
4637 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004638 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4639 above = 0; /* All buffer lines are displayed and there is an
4640 * indication of filler lines, that can be considered
4641 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642#endif
4643 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4644 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004645 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4646 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004648 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004650 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 ? (int)(above / ((above + below) / 100L))
4652 : (int)(above * 100L / (above + below)));
4653}
4654#endif
4655
4656/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004657 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 * Return TRUE if it was appended.
4659 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004661append_arg_number(
4662 win_T *wp,
4663 char_u *buf,
4664 int buflen,
4665 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666{
4667 char_u *p;
4668
4669 if (ARGCOUNT <= 1) /* nothing to do */
4670 return FALSE;
4671
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004672 p = buf + STRLEN(buf); /* go to the end of the buffer */
4673 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 return FALSE;
4675 *p++ = ' ';
4676 *p++ = '(';
4677 if (add_file)
4678 {
4679 STRCPY(p, "file ");
4680 p += 5;
4681 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004682 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4683 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4685 return TRUE;
4686}
4687
4688/*
4689 * If fname is not a full path, make it a full path.
4690 * Returns pointer to allocated memory (NULL for failure).
4691 */
4692 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004693fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694{
4695 /*
4696 * Force expanding the path always for Unix, because symbolic links may
4697 * mess up the full path name, even though it starts with a '/'.
4698 * Also expand when there is ".." in the file name, try to remove it,
4699 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004700 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 * For MS-Windows also expand names like "longna~1" to "longname".
4702 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004703#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 return FullName_save(fname, TRUE);
4705#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004706 if (!vim_isAbsName(fname)
4707 || strstr((char *)fname, "..") != NULL
4708 || strstr((char *)fname, "//") != NULL
4709# ifdef BACKSLASH_IN_FILENAME
4710 || strstr((char *)fname, "\\\\") != NULL
4711# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004712# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 )
4716 return FullName_save(fname, FALSE);
4717
4718 fname = vim_strsave(fname);
4719
Bram Moolenaar9b942202007-10-03 12:31:33 +00004720# ifdef USE_FNAME_CASE
4721# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004723# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 {
4725 if (fname != NULL)
4726 fname_case(fname, 0); /* set correct case for file name */
4727 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 return fname;
4731#endif
4732}
4733
4734/*
4735 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4736 * "ffname" becomes a pointer to allocated memory (or NULL).
4737 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004739fname_expand(
4740 buf_T *buf UNUSED,
4741 char_u **ffname,
4742 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743{
4744 if (*ffname == NULL) /* if no file name given, nothing to do */
4745 return;
4746 if (*sfname == NULL) /* if no short file name given, use ffname */
4747 *sfname = *ffname;
4748 *ffname = fix_fname(*ffname); /* expand to full path */
4749
4750#ifdef FEAT_SHORTCUT
4751 if (!buf->b_p_bin)
4752 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004753 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
4755 /* If the file name is a shortcut file, use the file it links to. */
4756 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004757 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
4759 vim_free(*ffname);
4760 *ffname = rfname;
4761 *sfname = rfname;
4762 }
4763 }
4764#endif
4765}
4766
4767/*
4768 * Get the file name for an argument list entry.
4769 */
4770 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004771alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772{
4773 buf_T *bp;
4774
4775 /* Use the name from the associated buffer if it exists. */
4776 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004777 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 return aep->ae_fname;
4779 return bp->b_fname;
4780}
4781
4782#if defined(FEAT_WINDOWS) || defined(PROTO)
4783/*
4784 * do_arg_all(): Open up to 'count' windows, one for each argument.
4785 */
4786 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004787do_arg_all(
4788 int count,
4789 int forceit, /* hide buffers in current windows */
4790 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791{
4792 int i;
4793 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004794 char_u *opened; /* Array of weight for which args are open:
4795 * 0: not opened
4796 * 1: opened in other tab
4797 * 2: opened in curtab
4798 * 3: opened in curtab and curwin
4799 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004800 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 int use_firstwin = FALSE; /* use first window for arglist */
4802 int split_ret = OK;
4803 int p_ea_save;
4804 alist_T *alist; /* argument list to be used */
4805 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004806 tabpage_T *tpnext;
4807 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004808 win_T *old_curwin, *last_curwin;
4809 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004810 win_T *new_curwin = NULL;
4811 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812
4813 if (ARGCOUNT <= 0)
4814 {
4815 /* Don't give an error message. We don't want it when the ":all"
4816 * command is in the .vimrc. */
4817 return;
4818 }
4819 setpcmark();
4820
4821 opened_len = ARGCOUNT;
4822 opened = alloc_clear((unsigned)opened_len);
4823 if (opened == NULL)
4824 return;
4825
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004826 /* Autocommands may do anything to the argument list. Make sure it's not
4827 * freed while we are working here by "locking" it. We still have to
4828 * watch out for its size to be changed. */
4829 alist = curwin->w_alist;
4830 ++alist->al_refcount;
4831
4832 old_curwin = curwin;
4833 old_curtab = curtab;
4834
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004835# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004837# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838
4839 /*
4840 * Try closing all windows that are not in the argument list.
4841 * Also close windows that are not full width;
4842 * When 'hidden' or "forceit" set the buffer becomes hidden.
4843 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004844 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004846 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004847 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004848 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004850 tpnext = curtab->tp_next;
4851 for (wp = firstwin; wp != NULL; wp = wpnext)
4852 {
4853 wpnext = wp->w_next;
4854 buf = wp->w_buffer;
4855 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004856 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004857 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004858 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004859 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004861 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004862 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004864 if (i < alist->al_ga.ga_len
4865 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4866 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4867 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004869 int weight = 1;
4870
4871 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004872 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004873 ++weight;
4874 if (old_curwin == wp)
4875 ++weight;
4876 }
4877
4878 if (weight > (int)opened[i])
4879 {
4880 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004881 if (i == 0)
4882 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004883 if (new_curwin != NULL)
4884 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004885 new_curwin = wp;
4886 new_curtab = curtab;
4887 }
4888 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004889 else if (keep_tabs)
4890 i = opened_len;
4891
4892 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004893 {
4894 /* Use the current argument list for all windows
4895 * containing a file from it. */
4896 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004897 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004898 ++wp->w_alist->al_refcount;
4899 }
4900 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 }
4903 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004904 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004906 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004908 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004909 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004911 /* If the buffer was changed, and we would like to hide it,
4912 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004913 if (!P_HID(buf) && buf->b_nwindows <= 1
4914 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004916#ifdef FEAT_AUTOCMD
4917 bufref_T bufref;
4918
4919 set_bufref(&bufref, buf);
4920#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004921 (void)autowrite(buf, FALSE);
4922#ifdef FEAT_AUTOCMD
4923 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004924 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004925 {
4926 wpnext = firstwin; /* start all over... */
4927 continue;
4928 }
4929#endif
4930 }
4931#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004932 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004933 if (firstwin == lastwin
4934 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004935#endif
4936 use_firstwin = TRUE;
4937#ifdef FEAT_WINDOWS
4938 else
4939 {
4940 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4941# ifdef FEAT_AUTOCMD
4942 /* check if autocommands removed the next window */
4943 if (!win_valid(wpnext))
4944 wpnext = firstwin; /* start all over... */
4945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
4947#endif
4948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 }
4950 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004951
4952 /* Without the ":tab" modifier only do the current tab page. */
4953 if (had_tab == 0 || tpnext == NULL)
4954 break;
4955
4956# ifdef FEAT_AUTOCMD
4957 /* check if autocommands removed the next tab page */
4958 if (!valid_tabpage(tpnext))
4959 tpnext = first_tabpage; /* start all over...*/
4960# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004961 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963
4964 /*
4965 * Open a window for files in the argument list that don't have one.
4966 * ARGCOUNT may change while doing this, because of autocommands.
4967 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004968 if (count > opened_len || count <= 0)
4969 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971#ifdef FEAT_AUTOCMD
4972 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4973 ++autocmd_no_enter;
4974 ++autocmd_no_leave;
4975#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004976 last_curwin = curwin;
4977 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004979#ifdef FEAT_WINDOWS
4980 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4981 * leaving an empty tab page when executed locally. */
4982 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4983 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4984 use_firstwin = TRUE;
4985#endif
4986
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004987 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 {
4989 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4990 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004991 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 {
4993 /* Move the already present window to below the current window */
4994 if (curwin->w_arg_idx != i)
4995 {
4996 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4997 {
4998 if (wpnext->w_arg_idx == i)
4999 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005000 if (keep_tabs)
5001 {
5002 new_curwin = wpnext;
5003 new_curtab = curtab;
5004 }
5005 else
5006 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 break;
5008 }
5009 }
5010 }
5011 }
5012 else if (split_ret == OK)
5013 {
5014 if (!use_firstwin) /* split current window */
5015 {
5016 p_ea_save = p_ea;
5017 p_ea = TRUE; /* use space from all windows */
5018 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5019 p_ea = p_ea_save;
5020 if (split_ret == FAIL)
5021 continue;
5022 }
5023#ifdef FEAT_AUTOCMD
5024 else /* first window: do autocmd for leaving this buffer */
5025 --autocmd_no_leave;
5026#endif
5027
5028 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005029 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 */
5031 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005032 if (i == 0)
5033 {
5034 new_curwin = curwin;
5035 new_curtab = curtab;
5036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5038 ECMD_ONE,
5039 ((P_HID(curwin->w_buffer)
5040 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005041 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#ifdef FEAT_AUTOCMD
5043 if (use_firstwin)
5044 ++autocmd_no_leave;
5045#endif
5046 use_firstwin = FALSE;
5047 }
5048 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005049
5050 /* When ":tab" was used open a new tab for a new window repeatedly. */
5051 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5052 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054
5055 /* Remove the "lock" on the argument list. */
5056 alist_unlink(alist);
5057
5058#ifdef FEAT_AUTOCMD
5059 --autocmd_no_enter;
5060#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005061 /* restore last referenced tabpage's curwin */
5062 if (last_curtab != new_curtab)
5063 {
5064 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005065 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005066 if (win_valid(last_curwin))
5067 win_enter(last_curwin, FALSE);
5068 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005069 /* to window with first arg */
5070 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005071 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005072 if (win_valid(new_curwin))
5073 win_enter(new_curwin, FALSE);
5074
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075#ifdef FEAT_AUTOCMD
5076 --autocmd_no_leave;
5077#endif
5078 vim_free(opened);
5079}
5080
5081# if defined(FEAT_LISTCMDS) || defined(PROTO)
5082/*
5083 * Open a window for a number of buffers.
5084 */
5085 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005086ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
5088 buf_T *buf;
5089 win_T *wp, *wpnext;
5090 int split_ret = OK;
5091 int p_ea_save;
5092 int open_wins = 0;
5093 int r;
5094 int count; /* Maximum number of windows to open. */
5095 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005096#ifdef FEAT_WINDOWS
5097 int had_tab = cmdmod.tab;
5098 tabpage_T *tpnext;
5099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100
5101 if (eap->addr_count == 0) /* make as many windows as possible */
5102 count = 9999;
5103 else
5104 count = eap->line2; /* make as many windows as specified */
5105 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5106 all = FALSE;
5107 else
5108 all = TRUE;
5109
5110 setpcmark();
5111
5112#ifdef FEAT_GUI
5113 need_mouse_correct = TRUE;
5114#endif
5115
5116 /*
5117 * Close superfluous windows (two windows for the same buffer).
5118 * Also close windows that are not full-width.
5119 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005120#ifdef FEAT_WINDOWS
5121 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005122 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005123 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005126 tpnext = curtab->tp_next;
5127 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005129 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005130 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005131#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005132 || ((cmdmod.split & WSP_VERT)
5133 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005134 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005135 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005136 || (had_tab > 0 && wp != firstwin)
5137#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02005138 ) && firstwin != lastwin
5139#ifdef FEAT_AUTOCMD
Bram Moolenaare0ab94e2016-09-04 19:50:54 +02005140 && !(wp->w_closing || wp->w_buffer->b_locked > 0)
Bram Moolenaar362ce482012-06-06 19:02:45 +02005141#endif
5142 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005143 {
5144 win_close(wp, FALSE);
5145#ifdef FEAT_AUTOCMD
5146 wpnext = firstwin; /* just in case an autocommand does
5147 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005148 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005149 open_wins = 0;
5150#endif
5151 }
5152 else
5153 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005155
5156#ifdef FEAT_WINDOWS
5157 /* Without the ":tab" modifier only do the current tab page. */
5158 if (had_tab == 0 || tpnext == NULL)
5159 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005160 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
5165 * Go through the buffer list. When a buffer doesn't have a window yet,
5166 * open one. Otherwise move the window to the right position.
5167 * Watch out for autocommands that delete buffers or windows!
5168 */
5169#ifdef FEAT_AUTOCMD
5170 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5171 ++autocmd_no_enter;
5172#endif
5173 win_enter(lastwin, FALSE);
5174#ifdef FEAT_AUTOCMD
5175 ++autocmd_no_leave;
5176#endif
5177 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5178 {
5179 /* Check if this buffer needs a window */
5180 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5181 continue;
5182
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005183#ifdef FEAT_WINDOWS
5184 if (had_tab != 0)
5185 {
5186 /* With the ":tab" modifier don't move the window. */
5187 if (buf->b_nwindows > 0)
5188 wp = lastwin; /* buffer has a window, skip it */
5189 else
5190 wp = NULL;
5191 }
5192 else
5193#endif
5194 {
5195 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005196 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005197 if (wp->w_buffer == buf)
5198 break;
5199 /* If the buffer already has a window, move it */
5200 if (wp != NULL)
5201 win_move_after(wp, curwin);
5202 }
5203
5204 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005206#ifdef FEAT_AUTOCMD
5207 bufref_T bufref;
5208
5209 set_bufref(&bufref, buf);
5210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 /* Split the window and put the buffer in it */
5212 p_ea_save = p_ea;
5213 p_ea = TRUE; /* use space from all windows */
5214 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5215 ++open_wins;
5216 p_ea = p_ea_save;
5217 if (split_ret == FAIL)
5218 continue;
5219
5220 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005221#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 swap_exists_action = SEA_DIALOG;
5223#endif
5224 set_curbuf(buf, DOBUF_GOTO);
5225#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005226 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005228 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005229#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005231# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 break;
5233 }
5234#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005235#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 if (swap_exists_action == SEA_QUIT)
5237 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005238# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5239 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005240
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005241 /* Reset the error/interrupt/exception state here so that
5242 * aborting() returns FALSE when closing a window. */
5243 enter_cleanup(&cs);
5244# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005245
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005246 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 win_close(curwin, TRUE);
5248 --open_wins;
5249 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005250 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005251
5252# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5253 /* Restore the error/interrupt/exception state if not
5254 * discarded by a new aborting error, interrupt, or uncaught
5255 * exception. */
5256 leave_cleanup(&cs);
5257# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 }
5259 else
5260 handle_swap_exists(NULL);
5261#endif
5262 }
5263
5264 ui_breakcheck();
5265 if (got_int)
5266 {
5267 (void)vgetc(); /* only break the file loading, not the rest */
5268 break;
5269 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005270#ifdef FEAT_EVAL
5271 /* Autocommands deleted the buffer or aborted script processing!!! */
5272 if (aborting())
5273 break;
5274#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005275#ifdef FEAT_WINDOWS
5276 /* When ":tab" was used open a new tab for a new window repeatedly. */
5277 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5278 cmdmod.tab = 9999;
5279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 }
5281#ifdef FEAT_AUTOCMD
5282 --autocmd_no_enter;
5283#endif
5284 win_enter(firstwin, FALSE); /* back to first window */
5285#ifdef FEAT_AUTOCMD
5286 --autocmd_no_leave;
5287#endif
5288
5289 /*
5290 * Close superfluous windows.
5291 */
5292 for (wp = lastwin; open_wins > count; )
5293 {
5294 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5295 || autowrite(wp->w_buffer, FALSE) == OK);
5296#ifdef FEAT_AUTOCMD
5297 if (!win_valid(wp))
5298 {
5299 /* BufWrite Autocommands made the window invalid, start over */
5300 wp = lastwin;
5301 }
5302 else
5303#endif
5304 if (r)
5305 {
5306 win_close(wp, !P_HID(wp->w_buffer));
5307 --open_wins;
5308 wp = lastwin;
5309 }
5310 else
5311 {
5312 wp = wp->w_prev;
5313 if (wp == NULL)
5314 break;
5315 }
5316 }
5317}
5318# endif /* FEAT_LISTCMDS */
5319
5320#endif /* FEAT_WINDOWS */
5321
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005322static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005323
Bram Moolenaar071d4272004-06-13 20:20:40 +00005324/*
5325 * do_modelines() - process mode lines for the current file
5326 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005327 * "flags" can be:
5328 * OPT_WINONLY only set options local to window
5329 * OPT_NOWIN don't set options local to window
5330 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 * Returns immediately if the "ml" option isn't set.
5332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005334do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005336 linenr_T lnum;
5337 int nmlines;
5338 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
5340 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5341 return;
5342
5343 /* Disallow recursive entry here. Can happen when executing a modeline
5344 * triggers an autocommand, which reloads modelines with a ":do". */
5345 if (entered)
5346 return;
5347
5348 ++entered;
5349 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5350 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005351 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352 nmlines = 0;
5353
5354 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5355 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005356 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 nmlines = 0;
5358 --entered;
5359}
5360
5361#include "version.h" /* for version number */
5362
5363/*
5364 * chk_modeline() - check a single line for a mode string
5365 * Return FAIL if an error encountered.
5366 */
5367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005368chk_modeline(
5369 linenr_T lnum,
5370 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371{
5372 char_u *s;
5373 char_u *e;
5374 char_u *linecopy; /* local copy of any modeline found */
5375 int prev;
5376 int vers;
5377 int end;
5378 int retval = OK;
5379 char_u *save_sourcing_name;
5380 linenr_T save_sourcing_lnum;
5381#ifdef FEAT_EVAL
5382 scid_T save_SID;
5383#endif
5384
5385 prev = -1;
5386 for (s = ml_get(lnum); *s != NUL; ++s)
5387 {
5388 if (prev == -1 || vim_isspace(prev))
5389 {
5390 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5391 || STRNCMP(s, "vi:", (size_t)3) == 0)
5392 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005393 /* Accept both "vim" and "Vim". */
5394 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005395 {
5396 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5397 e = s + 4;
5398 else
5399 e = s + 3;
5400 vers = getdigits(&e);
5401 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005402 && (s[0] != 'V'
5403 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 && (s[3] == ':'
5405 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5406 || (VIM_VERSION_100 < vers && s[3] == '<')
5407 || (VIM_VERSION_100 > vers && s[3] == '>')
5408 || (VIM_VERSION_100 == vers && s[3] == '=')))
5409 break;
5410 }
5411 }
5412 prev = *s;
5413 }
5414
5415 if (*s)
5416 {
5417 do /* skip over "ex:", "vi:" or "vim:" */
5418 ++s;
5419 while (s[-1] != ':');
5420
5421 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5422 if (linecopy == NULL)
5423 return FAIL;
5424
5425 save_sourcing_lnum = sourcing_lnum;
5426 save_sourcing_name = sourcing_name;
5427 sourcing_lnum = lnum; /* prepare for emsg() */
5428 sourcing_name = (char_u *)"modelines";
5429
5430 end = FALSE;
5431 while (end == FALSE)
5432 {
5433 s = skipwhite(s);
5434 if (*s == NUL)
5435 break;
5436
5437 /*
5438 * Find end of set command: ':' or end of line.
5439 * Skip over "\:", replacing it with ":".
5440 */
5441 for (e = s; *e != ':' && *e != NUL; ++e)
5442 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005443 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 if (*e == NUL)
5445 end = TRUE;
5446
5447 /*
5448 * If there is a "set" command, require a terminating ':' and
5449 * ignore the stuff after the ':'.
5450 * "vi:set opt opt opt: foo" -- foo not interpreted
5451 * "vi:opt opt opt: foo" -- foo interpreted
5452 * Accept "se" for compatibility with Elvis.
5453 */
5454 if (STRNCMP(s, "set ", (size_t)4) == 0
5455 || STRNCMP(s, "se ", (size_t)3) == 0)
5456 {
5457 if (*e != ':') /* no terminating ':'? */
5458 break;
5459 end = TRUE;
5460 s = vim_strchr(s, ' ') + 1;
5461 }
5462 *e = NUL; /* truncate the set command */
5463
5464 if (*s != NUL) /* skip over an empty "::" */
5465 {
5466#ifdef FEAT_EVAL
5467 save_SID = current_SID;
5468 current_SID = SID_MODELINE;
5469#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005470 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471#ifdef FEAT_EVAL
5472 current_SID = save_SID;
5473#endif
5474 if (retval == FAIL) /* stop if error found */
5475 break;
5476 }
5477 s = e + 1; /* advance to next part */
5478 }
5479
5480 sourcing_lnum = save_sourcing_lnum;
5481 sourcing_name = save_sourcing_name;
5482
5483 vim_free(linecopy);
5484 }
5485 return retval;
5486}
5487
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005488#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005490read_viminfo_bufferlist(
5491 vir_T *virp,
5492 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493{
5494 char_u *tab;
5495 linenr_T lnum;
5496 colnr_T col;
5497 buf_T *buf;
5498 char_u *sfname;
5499 char_u *xline;
5500
5501 /* Handle long line and escaped characters. */
5502 xline = viminfo_readstring(virp, 1, FALSE);
5503
5504 /* don't read in if there are files on the command-line or if writing: */
5505 if (xline != NULL && !writing && ARGCOUNT == 0
5506 && find_viminfo_parameter('%') != NULL)
5507 {
5508 /* Format is: <fname> Tab <lnum> Tab <col>.
5509 * Watch out for a Tab in the file name, work from the end. */
5510 lnum = 0;
5511 col = 0;
5512 tab = vim_strrchr(xline, '\t');
5513 if (tab != NULL)
5514 {
5515 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005516 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 tab = vim_strrchr(xline, '\t');
5518 if (tab != NULL)
5519 {
5520 *tab++ = '\0';
5521 lnum = atol((char *)tab);
5522 }
5523 }
5524
5525 /* Expand "~/" in the file name at "line + 1" to a full path.
5526 * Then try shortening it by comparing with the current directory */
5527 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005528 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529
5530 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5531 if (buf != NULL) /* just in case... */
5532 {
5533 buf->b_last_cursor.lnum = lnum;
5534 buf->b_last_cursor.col = col;
5535 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5536 }
5537 }
5538 vim_free(xline);
5539
5540 return viminfo_readline(virp);
5541}
5542
5543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005544write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545{
5546 buf_T *buf;
5547#ifdef FEAT_WINDOWS
5548 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005549 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550#endif
5551 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005552 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553
5554 if (find_viminfo_parameter('%') == NULL)
5555 return;
5556
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005557 /* Without a number -1 is returned: do all buffers. */
5558 max_buffers = get_viminfo_parameter('%');
5559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005561#define LINE_BUF_LEN (MAXPATHL + 40)
5562 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 if (line == NULL)
5564 return;
5565
5566#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005567 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 set_last_cursor(win);
5569#else
5570 set_last_cursor(curwin);
5571#endif
5572
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005573 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005574 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
5576 if (buf->b_fname == NULL
5577 || !buf->b_p_bl
5578#ifdef FEAT_QUICKFIX
5579 || bt_quickfix(buf)
5580#endif
5581 || removable(buf->b_ffname))
5582 continue;
5583
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005584 if (max_buffers-- == 0)
5585 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 putc('%', fp);
5587 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005588 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 (long)buf->b_last_cursor.lnum,
5590 buf->b_last_cursor.col);
5591 viminfo_writestring(fp, line);
5592 }
5593 vim_free(line);
5594}
5595#endif
5596
5597
5598/*
5599 * Return special buffer name.
5600 * Returns NULL when the buffer has a normal file name.
5601 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005602 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005603buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604{
5605#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5606 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005607 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005608 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005609 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005610
5611 /*
5612 * For location list window, w_llist_ref points to the location list.
5613 * For quickfix window, w_llist_ref is NULL.
5614 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005615 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005616 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005617 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005618 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620#endif
5621#ifdef FEAT_QUICKFIX
5622 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5623 * contains the name as specified by the user */
5624 if (bt_nofile(buf))
5625 {
5626 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005627 return buf->b_sfname;
5628 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
5630#endif
5631 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005632 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 return NULL;
5634}
5635
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005636#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5637 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5638 || defined(PROTO)
5639/*
5640 * Find a window for buffer "buf".
5641 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5642 * If not found FAIL is returned.
5643 */
5644 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005645find_win_for_buf(
5646 buf_T *buf,
5647 win_T **wp,
5648 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005649{
5650 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5651 if ((*wp)->w_buffer == buf)
5652 goto win_found;
5653 return FAIL;
5654win_found:
5655 return OK;
5656}
5657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658
5659#if defined(FEAT_SIGNS) || defined(PROTO)
5660/*
5661 * Insert the sign into the signlist.
5662 */
5663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005664insert_sign(
5665 buf_T *buf, /* buffer to store sign in */
5666 signlist_T *prev, /* previous sign entry */
5667 signlist_T *next, /* next sign entry */
5668 int id, /* sign ID */
5669 linenr_T lnum, /* line number which gets the mark */
5670 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671{
5672 signlist_T *newsign;
5673
5674 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5675 if (newsign != NULL)
5676 {
5677 newsign->id = id;
5678 newsign->lnum = lnum;
5679 newsign->typenr = typenr;
5680 newsign->next = next;
5681#ifdef FEAT_NETBEANS_INTG
5682 newsign->prev = prev;
5683 if (next != NULL)
5684 next->prev = newsign;
5685#endif
5686
5687 if (prev == NULL)
5688 {
5689 /* When adding first sign need to redraw the windows to create the
5690 * column for signs. */
5691 if (buf->b_signlist == NULL)
5692 {
5693 redraw_buf_later(buf, NOT_VALID);
5694 changed_cline_bef_curs();
5695 }
5696
5697 /* first sign in signlist */
5698 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005699#ifdef FEAT_NETBEANS_INTG
5700 if (netbeans_active())
5701 buf->b_has_sign_column = TRUE;
5702#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 else
5705 prev->next = newsign;
5706 }
5707}
5708
5709/*
5710 * Add the sign into the signlist. Find the right spot to do it though.
5711 */
5712 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005713buf_addsign(
5714 buf_T *buf, /* buffer to store sign in */
5715 int id, /* sign ID */
5716 linenr_T lnum, /* line number which gets the mark */
5717 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 signlist_T *sign; /* a sign in the signlist */
5720 signlist_T *prev; /* the previous sign */
5721
5722 prev = NULL;
5723 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5724 {
5725 if (lnum == sign->lnum && id == sign->id)
5726 {
5727 sign->typenr = typenr;
5728 return;
5729 }
5730 else if (
5731#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5732 id < 0 &&
5733#endif
5734 lnum < sign->lnum)
5735 {
5736#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5737 /* XXX - GRP: Is this because of sign slide problem? Or is it
5738 * really needed? Or is it because we allow multiple signs per
5739 * line? If so, should I add that feature to FEAT_SIGNS?
5740 */
5741 while (prev != NULL && prev->lnum == lnum)
5742 prev = prev->prev;
5743 if (prev == NULL)
5744 sign = buf->b_signlist;
5745 else
5746 sign = prev->next;
5747#endif
5748 insert_sign(buf, prev, sign, id, lnum, typenr);
5749 return;
5750 }
5751 prev = sign;
5752 }
5753#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5754 /* XXX - GRP: See previous comment */
5755 while (prev != NULL && prev->lnum == lnum)
5756 prev = prev->prev;
5757 if (prev == NULL)
5758 sign = buf->b_signlist;
5759 else
5760 sign = prev->next;
5761#endif
5762 insert_sign(buf, prev, sign, id, lnum, typenr);
5763
5764 return;
5765}
5766
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005767/*
5768 * For an existing, placed sign "markId" change the type to "typenr".
5769 * Returns the line number of the sign, or zero if the sign is not found.
5770 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005771 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005772buf_change_sign_type(
5773 buf_T *buf, /* buffer to store sign in */
5774 int markId, /* sign ID */
5775 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776{
5777 signlist_T *sign; /* a sign in the signlist */
5778
5779 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5780 {
5781 if (sign->id == markId)
5782 {
5783 sign->typenr = typenr;
5784 return sign->lnum;
5785 }
5786 }
5787
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005788 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789}
5790
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005791 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005792buf_getsigntype(
5793 buf_T *buf,
5794 linenr_T lnum,
5795 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796{
5797 signlist_T *sign; /* a sign in a b_signlist */
5798
5799 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5800 if (sign->lnum == lnum
5801 && (type == SIGN_ANY
5802# ifdef FEAT_SIGN_ICONS
5803 || (type == SIGN_ICON
5804 && sign_get_image(sign->typenr) != NULL)
5805# endif
5806 || (type == SIGN_TEXT
5807 && sign_get_text(sign->typenr) != NULL)
5808 || (type == SIGN_LINEHL
5809 && sign_get_attr(sign->typenr, TRUE) != 0)))
5810 return sign->typenr;
5811 return 0;
5812}
5813
5814
5815 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005816buf_delsign(
5817 buf_T *buf, /* buffer sign is stored in */
5818 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819{
5820 signlist_T **lastp; /* pointer to pointer to current sign */
5821 signlist_T *sign; /* a sign in a b_signlist */
5822 signlist_T *next; /* the next sign in a b_signlist */
5823 linenr_T lnum; /* line number whose sign was deleted */
5824
5825 lastp = &buf->b_signlist;
5826 lnum = 0;
5827 for (sign = buf->b_signlist; sign != NULL; sign = next)
5828 {
5829 next = sign->next;
5830 if (sign->id == id)
5831 {
5832 *lastp = next;
5833#ifdef FEAT_NETBEANS_INTG
5834 if (next != NULL)
5835 next->prev = sign->prev;
5836#endif
5837 lnum = sign->lnum;
5838 vim_free(sign);
5839 break;
5840 }
5841 else
5842 lastp = &sign->next;
5843 }
5844
5845 /* When deleted the last sign need to redraw the windows to remove the
5846 * sign column. */
5847 if (buf->b_signlist == NULL)
5848 {
5849 redraw_buf_later(buf, NOT_VALID);
5850 changed_cline_bef_curs();
5851 }
5852
5853 return lnum;
5854}
5855
5856
5857/*
5858 * Find the line number of the sign with the requested id. If the sign does
5859 * not exist, return 0 as the line number. This will still let the correct file
5860 * get loaded.
5861 */
5862 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005863buf_findsign(
5864 buf_T *buf, /* buffer to store sign in */
5865 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866{
5867 signlist_T *sign; /* a sign in the signlist */
5868
5869 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5870 if (sign->id == id)
5871 return sign->lnum;
5872
5873 return 0;
5874}
5875
5876 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005877buf_findsign_id(
5878 buf_T *buf, /* buffer whose sign we are searching for */
5879 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880{
5881 signlist_T *sign; /* a sign in the signlist */
5882
5883 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5884 if (sign->lnum == lnum)
5885 return sign->id;
5886
5887 return 0;
5888}
5889
5890
5891# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5892/* see if a given type of sign exists on a specific line */
5893 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005894buf_findsigntype_id(
5895 buf_T *buf, /* buffer whose sign we are searching for */
5896 linenr_T lnum, /* line number of sign */
5897 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898{
5899 signlist_T *sign; /* a sign in the signlist */
5900
5901 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5902 if (sign->lnum == lnum && sign->typenr == typenr)
5903 return sign->id;
5904
5905 return 0;
5906}
5907
5908
5909# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5910/* return the number of icons on the given line */
5911 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005912buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913{
5914 signlist_T *sign; /* a sign in the signlist */
5915 int count = 0;
5916
5917 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5918 if (sign->lnum == lnum)
5919 if (sign_get_image(sign->typenr) != NULL)
5920 count++;
5921
5922 return count;
5923}
5924# endif /* FEAT_SIGN_ICONS */
5925# endif /* FEAT_NETBEANS_INTG */
5926
5927
5928/*
5929 * Delete signs in buffer "buf".
5930 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005932buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933{
5934 signlist_T *next;
5935
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005936 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005937 * sign column. Not when curwin is NULL (this means we're exiting). */
5938 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005939 {
5940 redraw_buf_later(buf, NOT_VALID);
5941 changed_cline_bef_curs();
5942 }
5943
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 while (buf->b_signlist != NULL)
5945 {
5946 next = buf->b_signlist->next;
5947 vim_free(buf->b_signlist);
5948 buf->b_signlist = next;
5949 }
5950}
5951
5952/*
5953 * Delete all signs in all buffers.
5954 */
5955 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005956buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957{
5958 buf_T *buf; /* buffer we are checking for signs */
5959
Bram Moolenaar29323592016-07-24 22:04:11 +02005960 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005961 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005962 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963}
5964
5965/*
5966 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5967 */
5968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970{
5971 buf_T *buf;
5972 signlist_T *p;
5973 char lbuf[BUFSIZ];
5974
5975 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5976 msg_putchar('\n');
5977 if (rbuf == NULL)
5978 buf = firstbuf;
5979 else
5980 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005981 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 {
5983 if (buf->b_signlist != NULL)
5984 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005985 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5987 msg_putchar('\n');
5988 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005989 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005991 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5993 MSG_PUTS(lbuf);
5994 msg_putchar('\n');
5995 }
5996 if (rbuf != NULL)
5997 break;
5998 buf = buf->b_next;
5999 }
6000}
6001
6002/*
6003 * Adjust a placed sign for inserted/deleted lines.
6004 */
6005 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006006sign_mark_adjust(
6007 linenr_T line1,
6008 linenr_T line2,
6009 long amount,
6010 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006011{
6012 signlist_T *sign; /* a sign in a b_signlist */
6013
6014 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
6015 {
6016 if (sign->lnum >= line1 && sign->lnum <= line2)
6017 {
6018 if (amount == MAXLNUM)
6019 sign->lnum = line1;
6020 else
6021 sign->lnum += amount;
6022 }
6023 else if (sign->lnum > line2)
6024 sign->lnum += amount_after;
6025 }
6026}
6027#endif /* FEAT_SIGNS */
6028
6029/*
6030 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
6031 */
6032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006034{
6035 if (on != curbuf->b_p_bl)
6036 {
6037 curbuf->b_p_bl = on;
6038#ifdef FEAT_AUTOCMD
6039 if (on)
6040 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6041 else
6042 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6043#endif
6044 }
6045}
6046
6047/*
6048 * Read the file for "buf" again and check if the contents changed.
6049 * Return TRUE if it changed or this could not be checked.
6050 */
6051 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006052buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053{
6054 buf_T *newbuf;
6055 int differ = TRUE;
6056 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006058 exarg_T ea;
6059
6060 /* Allocate a buffer without putting it in the buffer list. */
6061 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6062 if (newbuf == NULL)
6063 return TRUE;
6064
6065 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6066 if (prep_exarg(&ea, buf) == FAIL)
6067 {
6068 wipe_buffer(newbuf, FALSE);
6069 return TRUE;
6070 }
6071
Bram Moolenaar071d4272004-06-13 20:20:40 +00006072 /* set curwin/curbuf to buf and save a few things */
6073 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074
Bram Moolenaar4770d092006-01-12 23:22:24 +00006075 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 && readfile(buf->b_ffname, buf->b_fname,
6077 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6078 &ea, READ_NEW | READ_DUMMY) == OK)
6079 {
6080 /* compare the two files line by line */
6081 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6082 {
6083 differ = FALSE;
6084 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6085 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6086 {
6087 differ = TRUE;
6088 break;
6089 }
6090 }
6091 }
6092 vim_free(ea.cmd);
6093
Bram Moolenaar071d4272004-06-13 20:20:40 +00006094 /* restore curwin/curbuf and a few other things */
6095 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096
6097 if (curbuf != newbuf) /* safety check */
6098 wipe_buffer(newbuf, FALSE);
6099
6100 return differ;
6101}
6102
6103/*
6104 * Wipe out a buffer and decrement the last buffer number if it was used for
6105 * this buffer. Call this to wipe out a temp buffer that does not contain any
6106 * marks.
6107 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006109wipe_buffer(
6110 buf_T *buf,
6111 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112{
6113 if (buf->b_fnum == top_file_num - 1)
6114 --top_file_num;
6115
6116#ifdef FEAT_AUTOCMD
6117 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006118 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006120 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121#ifdef FEAT_AUTOCMD
6122 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006123 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124#endif
6125}