blob: 9270c39fc9f038fa36b13e8bcdc69283b2226980 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010031static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010033static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010035static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
36static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +020038static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
39static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
40static int buf_same_ino(buf_T *buf, stat_T *stp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000041#else
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010042static int otherfile_buf(buf_T *buf, char_u *ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#endif
44#ifdef FEAT_TITLE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010045static int ti_change(char_u *str, char_u **last);
Bram Moolenaar071d4272004-06-13 20:20:40 +000046#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010047static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
48static void free_buffer(buf_T *);
49static void free_buffer_stuff(buf_T *buf, int free_options);
50static void clear_wininfo(buf_T *buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010059static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000060#endif
61
Bram Moolenaar7fd73202010-07-25 16:58:46 +020062#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
63static char *msg_loclist = N_("[Location List]");
64static char *msg_qflist = N_("[Quickfix List]");
65#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010066#ifdef FEAT_AUTOCMD
67static char *e_auabort = N_("E855: Autocommands caused command to abort");
68#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020069
Bram Moolenaarb25f9a92016-07-10 18:21:50 +020070/* Number of times free_buffer() was called. */
71static int buf_free_count = 0;
72
Bram Moolenaarf71d7b92016-08-09 22:14:05 +020073/* Read data from buffer for retrying. */
74 static int
75read_buffer(
76 int read_stdin, /* read file from stdin, otherwise fifo */
77 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
78 int flags) /* extra flags for readfile() */
79{
80 int retval = OK;
81 linenr_T line_count;
82
83 /*
84 * Read from the buffer which the text is already filled in and append at
85 * the end. This makes it possible to retry when 'fileformat' or
86 * 'fileencoding' was guessed wrong.
87 */
88 line_count = curbuf->b_ml.ml_line_count;
89 retval = readfile(
90 read_stdin ? NULL : curbuf->b_ffname,
91 read_stdin ? NULL : curbuf->b_fname,
92 (linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
93 flags | READ_BUFFER);
94 if (retval == OK)
95 {
96 /* Delete the binary lines. */
97 while (--line_count >= 0)
98 ml_delete((linenr_T)1, FALSE);
99 }
100 else
101 {
102 /* Delete the converted lines. */
103 while (curbuf->b_ml.ml_line_count > line_count)
104 ml_delete(line_count, FALSE);
105 }
106 /* Put the cursor on the first line. */
107 curwin->w_cursor.lnum = 1;
108 curwin->w_cursor.col = 0;
109
110 if (read_stdin)
111 {
112 /* Set or reset 'modified' before executing autocommands, so that
113 * it can be changed there. */
114 if (!readonlymode && !bufempty())
115 changed();
116 else if (retval != FAIL)
117 unchanged(curbuf, FALSE);
118
119#ifdef FEAT_AUTOCMD
120# ifdef FEAT_EVAL
121 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
122 curbuf, &retval);
123# else
124 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
125# endif
126#endif
127 }
128 return retval;
129}
130
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +0200132 * Open current buffer, that is: open the memfile and read the file into
133 * memory.
134 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 */
136 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100137open_buffer(
138 int read_stdin, /* read file from stdin */
139 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
140 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141{
142 int retval = OK;
143#ifdef FEAT_AUTOCMD
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200144 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100146#ifdef FEAT_SYN_HL
147 long old_tw = curbuf->b_p_tw;
148#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200149 int read_fifo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151 /*
152 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
153 * When re-entering the same buffer, it should not change, because the
154 * user may have reset the flag by hand.
155 */
156 if (readonlymode && curbuf->b_ffname != NULL
157 && (curbuf->b_flags & BF_NEVERLOADED))
158 curbuf->b_p_ro = TRUE;
159
Bram Moolenaar4770d092006-01-12 23:22:24 +0000160 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 {
162 /*
163 * There MUST be a memfile, otherwise we can't do anything
164 * If we can't create one for the current buffer, take another buffer
165 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100166 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar29323592016-07-24 22:04:11 +0200167 FOR_ALL_BUFFERS(curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 if (curbuf->b_ml.ml_mfp != NULL)
169 break;
170 /*
171 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000172 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 */
174 if (curbuf == NULL)
175 {
176 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
177 getout(2);
178 }
179 EMSG(_("E83: Cannot allocate buffer, using other one..."));
180 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100181#ifdef FEAT_SYN_HL
182 if (old_tw != curbuf->b_p_tw)
183 check_colorcolumn(curwin);
184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 return FAIL;
186 }
187
188#ifdef FEAT_AUTOCMD
189 /* The autocommands in readfile() may change the buffer, but only AFTER
190 * reading the file. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200191 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 modified_was_set = FALSE;
193#endif
194
195 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100196 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197
198 if (curbuf->b_ffname != NULL
199#ifdef FEAT_NETBEANS_INTG
200 && netbeansReadFile
201#endif
202 )
203 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100204 int old_msg_silent = msg_silent;
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200205#ifdef UNIX
206 int save_bin = curbuf->b_p_bin;
207 int perm;
208#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#ifdef FEAT_NETBEANS_INTG
210 int oldFire = netbeansFireChanges;
211
212 netbeansFireChanges = 0;
213#endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200214#ifdef UNIX
215 perm = mch_getperm(curbuf->b_ffname);
216 if (perm >= 0 && (0
217# ifdef S_ISFIFO
218 || S_ISFIFO(perm)
219# endif
220# ifdef S_ISSOCK
221 || S_ISSOCK(perm)
222# endif
Bram Moolenaarf04507d2016-08-20 15:05:39 +0200223# ifdef OPEN_CHR_FILES
224 || (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
225# endif
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200226 ))
227 read_fifo = TRUE;
228 if (read_fifo)
229 curbuf->b_p_bin = TRUE;
230#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100231 if (shortmess(SHM_FILEINFO))
232 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200234 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200235 flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
236#ifdef UNIX
237 if (read_fifo)
238 {
239 curbuf->b_p_bin = save_bin;
240 if (retval == OK)
241 retval = read_buffer(FALSE, eap, flags);
242 }
243#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100244 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_NETBEANS_INTG
246 netbeansFireChanges = oldFire;
247#endif
248 /* Help buffer is filtered. */
249 if (curbuf->b_help)
250 fix_help_buffer();
251 }
252 else if (read_stdin)
253 {
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200254 int save_bin = curbuf->b_p_bin;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256 /*
257 * First read the text in binary mode into the buffer.
258 * Then read from that same buffer and append at the end. This makes
259 * it possible to retry when 'fileformat' or 'fileencoding' was
260 * guessed wrong.
261 */
262 curbuf->b_p_bin = TRUE;
263 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200264 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
265 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266 curbuf->b_p_bin = save_bin;
267 if (retval == OK)
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200268 retval = read_buffer(TRUE, eap, flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000269 }
270
271 /* if first time loading this buffer, init b_chartab[] */
272 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100275#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100276 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100277#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279
280 /*
281 * Set/reset the Changed flag first, autocmds may change the buffer.
282 * Apply the automatic commands, before processing the modelines.
283 * So the modelines have priority over auto commands.
284 */
285 /* When reading stdin, the buffer contents always needs writing, so set
286 * the changed flag. Unless in readonly mode: "ls | gview -".
287 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000288 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289#ifdef FEAT_AUTOCMD
290 || modified_was_set /* ":set modified" used in autocmd */
291# ifdef FEAT_EVAL
292 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
293# endif
294#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000295 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296 changed();
Bram Moolenaarf71d7b92016-08-09 22:14:05 +0200297 else if (retval != FAIL && !read_stdin && !read_fifo)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000298 unchanged(curbuf, FALSE);
299 save_file_ff(curbuf); /* keep this fileformat */
300
301 /* require "!" to overwrite the file, because it wasn't read completely */
302#ifdef FEAT_EVAL
303 if (aborting())
304#else
305 if (got_int)
306#endif
307 curbuf->b_flags |= BF_READERR;
308
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000309#ifdef FEAT_FOLDING
310 /* Need to update automatic folding. Do this before the autocommands,
311 * they may use the fold info. */
312 foldUpdateAll(curwin);
313#endif
314
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315#ifdef FEAT_AUTOCMD
316 /* need to set w_topline, unless some autocommand already did that. */
317 if (!(curwin->w_valid & VALID_TOPLINE))
318 {
319 curwin->w_topline = 1;
320# ifdef FEAT_DIFF
321 curwin->w_topfill = 0;
322# endif
323 }
324# ifdef FEAT_EVAL
325 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
326# else
327 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
328# endif
329#endif
330
331 if (retval != FAIL)
332 {
333#ifdef FEAT_AUTOCMD
334 /*
335 * The autocommands may have changed the current buffer. Apply the
336 * modelines to the correct buffer, if it still exists and is loaded.
337 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200338 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 {
340 aco_save_T aco;
341
342 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200343 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000345 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
347
348#ifdef FEAT_AUTOCMD
349# ifdef FEAT_EVAL
350 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
351 &retval);
352# else
353 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
354# endif
355
356 /* restore curwin/curbuf and a few other things */
357 aucmd_restbuf(&aco);
358 }
359#endif
360 }
361
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362 return retval;
363}
364
365/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200366 * Store "buf" in "bufref" and set the free count.
367 */
368 void
369set_bufref(bufref_T *bufref, buf_T *buf)
370{
371 bufref->br_buf = buf;
372 bufref->br_buf_free_count = buf_free_count;
373}
374
375/*
376 * Return TRUE if "bufref->br_buf" points to a valid buffer.
377 * Only goes through the buffer list if buf_free_count changed.
378 */
379 int
380bufref_valid(bufref_T *bufref)
381{
382 return bufref->br_buf_free_count == buf_free_count
383 ? TRUE : buf_valid(bufref->br_buf);
384}
385
386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200388 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 */
390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100391buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392{
393 buf_T *bp;
394
Bram Moolenaar82404332016-07-10 17:00:38 +0200395 /* Assume that we more often have a recent buffer, start with the last
396 * one. */
397 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000398 if (bp == buf)
399 return TRUE;
400 return FALSE;
401}
402
403/*
Bram Moolenaar480778b2016-07-14 22:09:39 +0200404 * A hash table used to quickly lookup a buffer by its number.
405 */
406static hashtab_T buf_hashtab;
407
408 static void
409buf_hashtab_add(buf_T *buf)
410{
411 sprintf((char *)buf->b_key, "%x", buf->b_fnum);
412 if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
413 EMSG(_("E931: Buffer cannot be registered"));
414}
415
416 static void
417buf_hashtab_remove(buf_T *buf)
418{
419 hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
420
421 if (!HASHITEM_EMPTY(hi))
422 hash_remove(&buf_hashtab, hi);
423}
424
425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 * Close the link to a buffer.
427 * "action" is used when there is no longer a window for the buffer.
428 * It can be:
429 * 0 buffer becomes hidden
430 * DOBUF_UNLOAD buffer is unloaded
431 * DOBUF_DELETE buffer is unloaded and removed from buffer list
432 * DOBUF_WIPE buffer is unloaded and really deleted
433 * When doing all but the first one on the current buffer, the caller should
434 * get a new buffer very soon!
435 *
436 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100437 *
438 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
439 * cause there to be only one window with this buffer. e.g. when ":quit" is
440 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 */
442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100443close_buffer(
444 win_T *win, /* if not NULL, set b_last_cursor */
445 buf_T *buf,
446 int action,
447 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448{
449#ifdef FEAT_AUTOCMD
450 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100451 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200452 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453#endif
454 int unload_buf = (action != 0);
455 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
456 int wipe_buf = (action == DOBUF_WIPE);
457
458#ifdef FEAT_QUICKFIX
459 /*
460 * Force unloading or deleting when 'bufhidden' says so.
461 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
462 * "hide" (otherwise we could never free or delete a buffer).
463 */
464 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
465 {
466 del_buf = TRUE;
467 unload_buf = TRUE;
468 }
469 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
470 {
471 del_buf = TRUE;
472 unload_buf = TRUE;
473 wipe_buf = TRUE;
474 }
475 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
476 unload_buf = TRUE;
477#endif
478
Bram Moolenaar3be85852014-06-12 14:01:31 +0200479 if (win != NULL
480#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200481 && win_valid_any_tab(win) /* in case autocommands closed the window */
Bram Moolenaar3be85852014-06-12 14:01:31 +0200482#endif
483 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 {
485 /* Set b_last_cursor when closing the last window for the buffer.
486 * Remember the last cursor position and window options of the buffer.
487 * This used to be only for the current window, but then options like
488 * 'foldmethod' may be lost with a ":only" command. */
489 if (buf->b_nwindows == 1)
490 set_last_cursor(win);
491 buflist_setfpos(buf, win,
492 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
493 win->w_cursor.col, TRUE);
494 }
495
496#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200497 set_bufref(&bufref, buf);
498
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 /* When the buffer is no longer in a window, trigger BufWinLeave */
500 if (buf->b_nwindows == 1)
501 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200502 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200503 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
504 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200505 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100506 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200507 /* Autocommands deleted the buffer. */
508aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100509 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100511 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200512 buf->b_closing = FALSE;
513 if (abort_if_last && one_window())
514 /* Autocommands made this the only window. */
515 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516
517 /* When the buffer becomes hidden, but is not unloaded, trigger
518 * BufHidden */
519 if (!unload_buf)
520 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200521 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200522 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
523 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200524 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200525 /* Autocommands deleted the buffer. */
526 goto aucmd_abort;
527 buf->b_closing = FALSE;
528 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# ifdef FEAT_EVAL
533 if (aborting()) /* autocmds may abort script processing */
534 return;
535# endif
536 }
537 nwindows = buf->b_nwindows;
538#endif
539
540 /* decrease the link count from windows (unless not in any window) */
541 if (buf->b_nwindows > 0)
542 --buf->b_nwindows;
543
544 /* Return when a window is displaying the buffer or when it's not
545 * unloaded. */
546 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000547 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548
549 /* Always remove the buffer when there is no file name. */
550 if (buf->b_ffname == NULL)
551 del_buf = TRUE;
552
553 /*
554 * Free all things allocated for this buffer.
555 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
556 */
557#ifdef FEAT_AUTOCMD
558 /* Remember if we are closing the current buffer. Restore the number of
559 * windows, so that autocommands in buf_freeall() don't get confused. */
560 is_curbuf = (buf == curbuf);
561 buf->b_nwindows = nwindows;
562#endif
563
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200564 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565
566#ifdef FEAT_AUTOCMD
567 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200568 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 return;
570# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000571 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 return;
573# endif
574
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 /*
576 * It's possible that autocommands change curbuf to the one being deleted.
577 * This might cause the previous curbuf to be deleted unexpectedly. But
578 * in some cases it's OK to delete the curbuf, because a new one is
579 * obtained anyway. Therefore only return if curbuf changed to the
580 * deleted buffer.
581 */
582 if (buf == curbuf && !is_curbuf)
583 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200584
585 if (
586#ifdef FEAT_WINDOWS
Bram Moolenaare59215c2016-08-14 19:08:45 +0200587 win_valid_any_tab(win) &&
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200588#else
589 win != NULL &&
590#endif
591 win->w_buffer == buf)
592 win->w_buffer = NULL; /* make sure we don't use the buffer now */
593
594 /* Autocommands may have opened or closed windows for this buffer.
595 * Decrement the count for the close we do here. */
596 if (buf->b_nwindows > 0)
597 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598#endif
599
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000600 /* Change directories when the 'acd' option is set. */
601 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000602
603 /*
604 * Remove the buffer from the list.
605 */
606 if (wipe_buf)
607 {
608#ifdef FEAT_SUN_WORKSHOP
609 if (usingSunWorkShop)
610 workshop_file_closed_lineno((char *)buf->b_ffname,
611 (int)buf->b_last_cursor.lnum);
612#endif
613 vim_free(buf->b_ffname);
614 vim_free(buf->b_sfname);
615 if (buf->b_prev == NULL)
616 firstbuf = buf->b_next;
617 else
618 buf->b_prev->b_next = buf->b_next;
619 if (buf->b_next == NULL)
620 lastbuf = buf->b_prev;
621 else
622 buf->b_next->b_prev = buf->b_prev;
623 free_buffer(buf);
624 }
625 else
626 {
627 if (del_buf)
628 {
629 /* Free all internal variables and reset option values, to make
630 * ":bdel" compatible with Vim 5.7. */
631 free_buffer_stuff(buf, TRUE);
632
633 /* Make it look like a new buffer. */
634 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
635
636 /* Init the options when loaded again. */
637 buf->b_p_initialized = FALSE;
638 }
639 buf_clear_file(buf);
640 if (del_buf)
641 buf->b_p_bl = FALSE;
642 }
643}
644
645/*
646 * Make buffer not contain a file.
647 */
648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650{
651 buf->b_ml.ml_line_count = 1;
652 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654 buf->b_p_eol = TRUE;
655 buf->b_start_eol = TRUE;
656#ifdef FEAT_MBYTE
657 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000658 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#endif
660 buf->b_ml.ml_mfp = NULL;
661 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
662#ifdef FEAT_NETBEANS_INTG
663 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000664#endif
665}
666
667/*
668 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar5a497892016-09-03 16:29:04 +0200669 * the file. Careful: get here with "curwin" NULL when exiting.
670 * flags:
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200671 * BFA_DEL buffer is going to be deleted
672 * BFA_WIPE buffer is going to be wiped out
673 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100676buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677{
678#ifdef FEAT_AUTOCMD
679 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200680 bufref_T bufref;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200681# ifdef FEAT_WINDOWS
682 int is_curwin = (curwin!= NULL && curwin->w_buffer == buf);
683 win_T *the_curwin = curwin;
684 tabpage_T *the_curtab = curtab;
685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686
Bram Moolenaar5a497892016-09-03 16:29:04 +0200687 /* Make sure the buffer isn't closed by autocommands. */
Bram Moolenaar362ce482012-06-06 19:02:45 +0200688 buf->b_closing = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200689 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200690 if (buf->b_ml.ml_mfp != NULL)
691 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200692 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
693 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200694 && !bufref_valid(&bufref))
695 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200696 return;
697 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200698 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200700 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
701 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200702 && !bufref_valid(&bufref))
703 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 return;
705 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200706 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000707 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200708 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
709 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200710 && !bufref_valid(&bufref))
711 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 return;
713 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200714 buf->b_closing = FALSE;
Bram Moolenaar5a497892016-09-03 16:29:04 +0200715
716# ifdef FEAT_WINDOWS
717 /* If the buffer was in curwin and the window has changed, go back to that
718 * window, if it still exists. This avoids that ":edit x" triggering a
719 * "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
720 if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
721 {
722 block_autocmds();
723 goto_tabpage_win(the_curtab, the_curwin);
724 unblock_autocmds();
725 }
726# endif
727
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000729 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 return;
731# endif
732
733 /*
734 * It's possible that autocommands change curbuf to the one being deleted.
735 * This might cause curbuf to be deleted unexpectedly. But in some cases
736 * it's OK to delete the curbuf, because a new one is obtained anyway.
737 * Therefore only return if curbuf changed to the deleted buffer.
738 */
739 if (buf == curbuf && !is_curbuf)
740 return;
741#endif
742#ifdef FEAT_DIFF
743 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
744#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200745#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100746 /* Remove any ownsyntax, unless exiting. */
747 if (firstwin != NULL && curwin->w_buffer == buf)
748 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200749#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000750
751#ifdef FEAT_FOLDING
752 /* No folds in an empty buffer. */
753# ifdef FEAT_WINDOWS
754 {
755 win_T *win;
756 tabpage_T *tp;
757
758 FOR_ALL_TAB_WINDOWS(tp, win)
759 if (win->w_buffer == buf)
760 clearFolding(win);
761 }
762# else
763 if (curwin->w_buffer == buf)
764 clearFolding(curwin);
765# endif
766#endif
767
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768#ifdef FEAT_TCL
769 tcl_buffer_free(buf);
770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 ml_close(buf, TRUE); /* close and delete the memline/memfile */
772 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200773 if ((flags & BFA_KEEP_UNDO) == 0)
774 {
775 u_blockfree(buf); /* free the memory allocated for undo */
776 u_clearall(buf); /* reset all undo information */
777 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200779 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000781 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782}
783
784/*
785 * Free a buffer structure and the things it contains related to the buffer
786 * itself (not the file, that must have been done already).
787 */
788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100789free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200791 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200793#ifdef FEAT_EVAL
794 unref_var_dict(buf->b_vars);
795#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200796#ifdef FEAT_LUA
797 lua_buffer_free(buf);
798#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000799#ifdef FEAT_MZSCHEME
800 mzscheme_buffer_free(buf);
801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#ifdef FEAT_PERL
803 perl_buf_free(buf);
804#endif
805#ifdef FEAT_PYTHON
806 python_buffer_free(buf);
807#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200808#ifdef FEAT_PYTHON3
809 python3_buffer_free(buf);
810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811#ifdef FEAT_RUBY
812 ruby_buffer_free(buf);
813#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200814#ifdef FEAT_JOB_CHANNEL
815 channel_buffer_free(buf);
816#endif
Bram Moolenaar480778b2016-07-14 22:09:39 +0200817
818 buf_hashtab_remove(buf);
819
Bram Moolenaar9280e3f2016-07-14 23:03:19 +0200820#ifdef FEAT_AUTOCMD
821 aubuflocal_remove(buf);
822
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200823 if (autocmd_busy)
824 {
825 /* Do not free the buffer structure while autocommands are executing,
826 * it's still needed. Free it when autocmd_busy is reset. */
827 buf->b_next = au_pending_free_buf;
828 au_pending_free_buf = buf;
829 }
830 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000831#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200832 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833}
834
835/*
836 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
837 */
838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100839free_buffer_stuff(
840 buf_T *buf,
841 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842{
843 if (free_options)
844 {
845 clear_wininfo(buf); /* including window-local options */
846 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200847#ifdef FEAT_SPELL
848 ga_clear(&buf->b_s.b_langp);
849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 }
851#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200852 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
853 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854#endif
855#ifdef FEAT_USR_CMDS
856 uc_clear(&buf->b_ucmds); /* clear local user commands */
857#endif
858#ifdef FEAT_SIGNS
859 buf_delete_signs(buf); /* delete any signs */
860#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000861#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200862 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864#ifdef FEAT_LOCALMAP
865 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
866 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
867#endif
868#ifdef FEAT_MBYTE
869 vim_free(buf->b_start_fenc);
870 buf->b_start_fenc = NULL;
871#endif
872}
873
874/*
875 * Free the b_wininfo list for buffer "buf".
876 */
877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100878clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879{
880 wininfo_T *wip;
881
882 while (buf->b_wininfo != NULL)
883 {
884 wip = buf->b_wininfo;
885 buf->b_wininfo = wip->wi_next;
886 if (wip->wi_optset)
887 {
888 clear_winopt(&wip->wi_opt);
889#ifdef FEAT_FOLDING
890 deleteFoldRecurse(&wip->wi_folds);
891#endif
892 }
893 vim_free(wip);
894 }
895}
896
897#if defined(FEAT_LISTCMDS) || defined(PROTO)
898/*
899 * Go to another buffer. Handles the result of the ATTENTION dialog.
900 */
901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100902goto_buffer(
903 exarg_T *eap,
904 int start,
905 int dir,
906 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000908# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200909 bufref_T old_curbuf;
910
911 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912
913 swap_exists_action = SEA_DIALOG;
914# endif
915 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
916 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000917# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
919 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000920# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
921 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000922
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000923 /* Reset the error/interrupt/exception state here so that
924 * aborting() returns FALSE when closing a window. */
925 enter_cleanup(&cs);
926# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000927
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000928 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 win_close(curwin, TRUE);
930 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000931 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000932
933# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
934 /* Restore the error/interrupt/exception state if not discarded by a
935 * new aborting error, interrupt, or uncaught exception. */
936 leave_cleanup(&cs);
937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000938 }
939 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200940 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941# endif
942}
943#endif
944
Bram Moolenaare64ac772005-12-07 20:54:59 +0000945#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946/*
947 * Handle the situation of swap_exists_action being set.
948 * It is allowed for "old_curbuf" to be NULL or invalid.
949 */
950 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200951handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000953# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
954 cleanup_T cs;
955# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100956#ifdef FEAT_SYN_HL
957 long old_tw = curbuf->b_p_tw;
958#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200959 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000960
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 if (swap_exists_action == SEA_QUIT)
962 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000963# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
964 /* Reset the error/interrupt/exception state here so that
965 * aborting() returns FALSE when closing a buffer. */
966 enter_cleanup(&cs);
967# endif
968
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 /* User selected Quit at ATTENTION prompt. Go back to previous
970 * buffer. If that buffer is gone or the same as the current one,
971 * open a new, empty buffer. */
972 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000973 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100974 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200975 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
976 || old_curbuf->br_buf == curbuf)
977 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
978 else
979 buf = old_curbuf->br_buf;
980 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100981 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200982 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100983#ifdef FEAT_SYN_HL
984 if (old_tw != curbuf->b_p_tw)
985 check_colorcolumn(curwin);
986#endif
987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000989
990# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
991 /* Restore the error/interrupt/exception state if not discarded by a
992 * new aborting error, interrupt, or uncaught exception. */
993 leave_cleanup(&cs);
994# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 }
996 else if (swap_exists_action == SEA_RECOVER)
997 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000998# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
999 /* Reset the error/interrupt/exception state here so that
1000 * aborting() returns FALSE when closing a buffer. */
1001 enter_cleanup(&cs);
1002# endif
1003
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 /* User selected Recover at ATTENTION prompt. */
1005 msg_scroll = TRUE;
1006 ml_recover();
1007 MSG_PUTS("\n"); /* don't overwrite the last message */
1008 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +00001009 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001010
1011# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1012 /* Restore the error/interrupt/exception state if not discarded by a
1013 * new aborting error, interrupt, or uncaught exception. */
1014 leave_cleanup(&cs);
1015# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017 swap_exists_action = SEA_NONE;
1018}
1019#endif
1020
1021#if defined(FEAT_LISTCMDS) || defined(PROTO)
1022/*
1023 * do_bufdel() - delete or unload buffer(s)
1024 *
1025 * addr_count == 0: ":bdel" - delete current buffer
1026 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
1027 * buffer "end_bnr", then any other arguments.
1028 * addr_count == 2: ":N,N bdel" - delete buffers in range
1029 *
1030 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
1031 * DOBUF_DEL (":bdel")
1032 *
1033 * Returns error message or NULL
1034 */
1035 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001036do_bufdel(
1037 int command,
1038 char_u *arg, /* pointer to extra arguments */
1039 int addr_count,
1040 int start_bnr, /* first buffer number in a range */
1041 int end_bnr, /* buffer nr or last buffer nr in a range */
1042 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
1044 int do_current = 0; /* delete current buffer? */
1045 int deleted = 0; /* number of buffers deleted */
1046 char_u *errormsg = NULL; /* return value */
1047 int bnr; /* buffer number */
1048 char_u *p;
1049
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 if (addr_count == 0)
1051 {
1052 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
1053 }
1054 else
1055 {
1056 if (addr_count == 2)
1057 {
1058 if (*arg) /* both range and argument is not allowed */
1059 return (char_u *)_(e_trailing);
1060 bnr = start_bnr;
1061 }
1062 else /* addr_count == 1 */
1063 bnr = end_bnr;
1064
1065 for ( ;!got_int; ui_breakcheck())
1066 {
1067 /*
1068 * delete the current buffer last, otherwise when the
1069 * current buffer is deleted, the next buffer becomes
1070 * the current one and will be loaded, which may then
1071 * also be deleted, etc.
1072 */
1073 if (bnr == curbuf->b_fnum)
1074 do_current = bnr;
1075 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
1076 forceit) == OK)
1077 ++deleted;
1078
1079 /*
1080 * find next buffer number to delete/unload
1081 */
1082 if (addr_count == 2)
1083 {
1084 if (++bnr > end_bnr)
1085 break;
1086 }
1087 else /* addr_count == 1 */
1088 {
1089 arg = skipwhite(arg);
1090 if (*arg == NUL)
1091 break;
1092 if (!VIM_ISDIGIT(*arg))
1093 {
1094 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01001095 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
1096 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 if (bnr < 0) /* failed */
1098 break;
1099 arg = p;
1100 }
1101 else
1102 bnr = getdigits(&arg);
1103 }
1104 }
1105 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1106 FORWARD, do_current, forceit) == OK)
1107 ++deleted;
1108
1109 if (deleted == 0)
1110 {
1111 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001112 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001114 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001116 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 errormsg = IObuff;
1118 }
1119 else if (deleted >= p_report)
1120 {
1121 if (command == DOBUF_UNLOAD)
1122 {
1123 if (deleted == 1)
1124 MSG(_("1 buffer unloaded"));
1125 else
1126 smsg((char_u *)_("%d buffers unloaded"), deleted);
1127 }
1128 else if (command == DOBUF_DEL)
1129 {
1130 if (deleted == 1)
1131 MSG(_("1 buffer deleted"));
1132 else
1133 smsg((char_u *)_("%d buffers deleted"), deleted);
1134 }
1135 else
1136 {
1137 if (deleted == 1)
1138 MSG(_("1 buffer wiped out"));
1139 else
1140 smsg((char_u *)_("%d buffers wiped out"), deleted);
1141 }
1142 }
1143 }
1144
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145
1146 return errormsg;
1147}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001148#endif /* FEAT_LISTCMDS */
1149
1150#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1151 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001153static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001154
1155/*
1156 * Make the current buffer empty.
1157 * Used when it is wiped out and it's the last buffer.
1158 */
1159 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001160empty_curbuf(
1161 int close_others,
1162 int forceit,
1163 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001164{
1165 int retval;
1166 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001167 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001168
1169 if (action == DOBUF_UNLOAD)
1170 {
1171 EMSG(_("E90: Cannot unload last buffer"));
1172 return FAIL;
1173 }
1174
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001175 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001176#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001177 if (close_others)
1178 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001179 close_windows(buf, TRUE);
1180#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001181
1182 setpcmark();
1183 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1184 forceit ? ECMD_FORCEIT : 0, curwin);
1185
1186 /*
1187 * do_ecmd() may create a new buffer, then we have to delete
1188 * the old one. But do_ecmd() may have done that already, check
1189 * if the buffer still exists.
1190 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001191 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001192 close_buffer(NULL, buf, action, FALSE);
1193 if (!close_others)
1194 need_fileinfo = FALSE;
1195 return retval;
1196}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197/*
1198 * Implementation of the commands for the buffer list.
1199 *
1200 * action == DOBUF_GOTO go to specified buffer
1201 * action == DOBUF_SPLIT split window and go to specified buffer
1202 * action == DOBUF_UNLOAD unload specified buffer(s)
1203 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1204 * action == DOBUF_WIPE delete specified buffer(s) really
1205 *
1206 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1207 * start == DOBUF_FIRST go to "count" buffer from first buffer
1208 * start == DOBUF_LAST go to "count" buffer from last buffer
1209 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1210 *
1211 * Return FAIL or OK.
1212 */
1213 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001214do_buffer(
1215 int action,
1216 int start,
1217 int dir, /* FORWARD or BACKWARD */
1218 int count, /* buffer number or number of buffers */
1219 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220{
1221 buf_T *buf;
1222 buf_T *bp;
1223 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1224 || action == DOBUF_WIPE);
1225
1226 switch (start)
1227 {
1228 case DOBUF_FIRST: buf = firstbuf; break;
1229 case DOBUF_LAST: buf = lastbuf; break;
1230 default: buf = curbuf; break;
1231 }
1232 if (start == DOBUF_MOD) /* find next modified buffer */
1233 {
1234 while (count-- > 0)
1235 {
1236 do
1237 {
1238 buf = buf->b_next;
1239 if (buf == NULL)
1240 buf = firstbuf;
1241 }
1242 while (buf != curbuf && !bufIsChanged(buf));
1243 }
1244 if (!bufIsChanged(buf))
1245 {
1246 EMSG(_("E84: No modified buffer found"));
1247 return FAIL;
1248 }
1249 }
1250 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1251 {
1252 while (buf != NULL && buf->b_fnum != count)
1253 buf = buf->b_next;
1254 }
1255 else
1256 {
1257 bp = NULL;
1258 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1259 {
1260 /* remember the buffer where we start, we come back there when all
1261 * buffers are unlisted. */
1262 if (bp == NULL)
1263 bp = buf;
1264 if (dir == FORWARD)
1265 {
1266 buf = buf->b_next;
1267 if (buf == NULL)
1268 buf = firstbuf;
1269 }
1270 else
1271 {
1272 buf = buf->b_prev;
1273 if (buf == NULL)
1274 buf = lastbuf;
1275 }
1276 /* don't count unlisted buffers */
1277 if (unload || buf->b_p_bl)
1278 {
1279 --count;
1280 bp = NULL; /* use this buffer as new starting point */
1281 }
1282 if (bp == buf)
1283 {
1284 /* back where we started, didn't find anything. */
1285 EMSG(_("E85: There is no listed buffer"));
1286 return FAIL;
1287 }
1288 }
1289 }
1290
1291 if (buf == NULL) /* could not find it */
1292 {
1293 if (start == DOBUF_FIRST)
1294 {
1295 /* don't warn when deleting */
1296 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001297 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 }
1299 else if (dir == FORWARD)
1300 EMSG(_("E87: Cannot go beyond last buffer"));
1301 else
1302 EMSG(_("E88: Cannot go before first buffer"));
1303 return FAIL;
1304 }
1305
1306#ifdef FEAT_GUI
1307 need_mouse_correct = TRUE;
1308#endif
1309
1310#ifdef FEAT_LISTCMDS
1311 /*
1312 * delete buffer buf from memory and/or the list
1313 */
1314 if (unload)
1315 {
1316 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001317# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1318 bufref_T bufref;
1319
1320 set_bufref(&bufref, buf);
1321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322
1323 /* When unloading or deleting a buffer that's already unloaded and
1324 * unlisted: fail silently. */
1325 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1326 return FAIL;
1327
1328 if (!forceit && bufIsChanged(buf))
1329 {
1330#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1331 if ((p_confirm || cmdmod.confirm) && p_write)
1332 {
1333 dialog_changed(buf, FALSE);
1334# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001335 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 /* Autocommand deleted buffer, oops! It's not changed
1337 * now. */
1338 return FAIL;
1339# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001340 /* If it's still changed fail silently, the dialog already
1341 * mentioned why it fails. */
1342 if (bufIsChanged(buf))
1343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001345 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#endif
1347 {
1348 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1349 buf->b_fnum);
1350 return FAIL;
1351 }
1352 }
1353
1354 /*
1355 * If deleting the last (listed) buffer, make it empty.
1356 * The last (listed) buffer cannot be unloaded.
1357 */
Bram Moolenaar29323592016-07-24 22:04:11 +02001358 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 if (bp->b_p_bl && bp != buf)
1360 break;
1361 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001362 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363
1364#ifdef FEAT_WINDOWS
1365 /*
1366 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001367 * (unless it's the only window). Repeat this so long as we end up in
1368 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001370 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001371# ifdef FEAT_AUTOCMD
1372 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1373# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001374 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001375 {
1376 if (win_close(curwin, FALSE) == FAIL)
1377 break;
1378 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379#endif
1380
1381 /*
1382 * If the buffer to be deleted is not the current one, delete it here.
1383 */
1384 if (buf != curbuf)
1385 {
1386#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001387 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001388 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001390 if (buf->b_nwindows <= 0)
1391 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 return OK;
1393 }
1394
1395 /*
1396 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001397 * There should be another, otherwise it would have been handled
1398 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001399 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 * Then prefer the buffer we most recently visited.
1401 * Else try to find one that is loaded, after the current buffer,
1402 * then before the current buffer.
1403 * Finally use any buffer.
1404 */
1405 buf = NULL; /* selected buffer */
1406 bp = NULL; /* used when no loaded buffer found */
1407#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001408 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1409 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410# ifdef FEAT_JUMPLIST
1411 else
1412# endif
1413#endif
1414#ifdef FEAT_JUMPLIST
1415 if (curwin->w_jumplistlen > 0)
1416 {
1417 int jumpidx;
1418
1419 jumpidx = curwin->w_jumplistidx - 1;
1420 if (jumpidx < 0)
1421 jumpidx = curwin->w_jumplistlen - 1;
1422
1423 forward = jumpidx;
1424 while (jumpidx != curwin->w_jumplistidx)
1425 {
1426 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1427 if (buf != NULL)
1428 {
1429 if (buf == curbuf || !buf->b_p_bl)
1430 buf = NULL; /* skip current and unlisted bufs */
1431 else if (buf->b_ml.ml_mfp == NULL)
1432 {
1433 /* skip unloaded buf, but may keep it for later */
1434 if (bp == NULL)
1435 bp = buf;
1436 buf = NULL;
1437 }
1438 }
1439 if (buf != NULL) /* found a valid buffer: stop searching */
1440 break;
1441 /* advance to older entry in jump list */
1442 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1443 break;
1444 if (--jumpidx < 0)
1445 jumpidx = curwin->w_jumplistlen - 1;
1446 if (jumpidx == forward) /* List exhausted for sure */
1447 break;
1448 }
1449 }
1450#endif
1451
1452 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1453 {
1454 forward = TRUE;
1455 buf = curbuf->b_next;
1456 for (;;)
1457 {
1458 if (buf == NULL)
1459 {
1460 if (!forward) /* tried both directions */
1461 break;
1462 buf = curbuf->b_prev;
1463 forward = FALSE;
1464 continue;
1465 }
1466 /* in non-help buffer, try to skip help buffers, and vv */
1467 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1468 {
1469 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1470 break;
1471 if (bp == NULL) /* remember unloaded buf for later */
1472 bp = buf;
1473 }
1474 if (forward)
1475 buf = buf->b_next;
1476 else
1477 buf = buf->b_prev;
1478 }
1479 }
1480 if (buf == NULL) /* No loaded buffer, use unloaded one */
1481 buf = bp;
1482 if (buf == NULL) /* No loaded buffer, find listed one */
1483 {
Bram Moolenaar29323592016-07-24 22:04:11 +02001484 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 if (buf->b_p_bl && buf != curbuf)
1486 break;
1487 }
1488 if (buf == NULL) /* Still no buffer, just take one */
1489 {
1490 if (curbuf->b_next != NULL)
1491 buf = curbuf->b_next;
1492 else
1493 buf = curbuf->b_prev;
1494 }
1495 }
1496
Bram Moolenaara02471e2014-01-10 16:43:14 +01001497 if (buf == NULL)
1498 {
1499 /* Autocommands must have wiped out all other buffers. Only option
1500 * now is to make the current buffer empty. */
1501 return empty_curbuf(FALSE, forceit, action);
1502 }
1503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 /*
1505 * make buf current buffer
1506 */
1507 if (action == DOBUF_SPLIT) /* split window first */
1508 {
1509# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001510 /* If 'switchbuf' contains "useopen": jump to first window containing
1511 * "buf" if one exists */
1512 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001513 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001514 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001515 * page containing "buf" if one exists */
1516 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 return OK;
1518 if (win_split(0, 0) == FAIL)
1519# endif
1520 return FAIL;
1521 }
1522#endif
1523
1524 /* go to current buffer - nothing to do */
1525 if (buf == curbuf)
1526 return OK;
1527
1528 /*
1529 * Check if the current buffer may be abandoned.
1530 */
1531 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1532 {
1533#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1534 if ((p_confirm || cmdmod.confirm) && p_write)
1535 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001536# ifdef FEAT_AUTOCMD
1537 bufref_T bufref;
1538
1539 set_bufref(&bufref, buf);
1540# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 dialog_changed(curbuf, FALSE);
1542# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001543 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544 /* Autocommand deleted buffer, oops! */
1545 return FAIL;
1546# endif
1547 }
1548 if (bufIsChanged(curbuf))
1549#endif
1550 {
1551 EMSG(_(e_nowrtmsg));
1552 return FAIL;
1553 }
1554 }
1555
1556 /* Go to the other buffer. */
1557 set_curbuf(buf, action);
1558
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001559#if defined(FEAT_LISTCMDS) \
1560 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001562 {
1563 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565#endif
1566
1567#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1568 if (aborting()) /* autocmds may abort script processing */
1569 return FAIL;
1570#endif
1571
1572 return OK;
1573}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575
1576/*
1577 * Set current buffer to "buf". Executes autocommands and closes current
1578 * buffer. "action" tells how to close the current buffer:
1579 * DOBUF_GOTO free or hide it
1580 * DOBUF_SPLIT nothing
1581 * DOBUF_UNLOAD unload it
1582 * DOBUF_DEL delete it
1583 * DOBUF_WIPE wipe it out
1584 */
1585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001586set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587{
1588 buf_T *prevbuf;
1589 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1590 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001591#ifdef FEAT_SYN_HL
1592 long old_tw = curbuf->b_p_tw;
1593#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001594 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595
1596 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001597 if (!cmdmod.keepalt)
1598 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001599 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 /* Don't restart Select mode after switching to another buffer. */
1602 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603
1604 /* close_windows() or apply_autocmds() may change curbuf */
1605 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001606 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607
1608#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001609 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001611 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001613 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614# endif
1615#endif
1616 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001617#ifdef FEAT_SYN_HL
1618 if (prevbuf == curwin->w_buffer)
1619 reset_synblock(curwin);
1620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621#ifdef FEAT_WINDOWS
1622 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001623 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624#endif
1625#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001626 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001628 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001630 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001631#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001632 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001633#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001634 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001635 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1637 unload ? action : (action == DOBUF_GOTO
1638 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001639 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001640#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001641 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001642 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001643 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001644#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 }
1647#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001648 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001649 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001650 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1651 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001652# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001653 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001654# endif
1655# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001656 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001657# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001658 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001662#ifdef FEAT_SYN_HL
1663 if (old_tw != curbuf->b_p_tw)
1664 check_colorcolumn(curwin);
1665#endif
1666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667}
1668
1669/*
1670 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001671 * Old curbuf must have been abandoned already! This also means "curbuf" may
1672 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 */
1674 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001675enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 /* Copy buffer and window local option values. Not for a help buffer. */
1678 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1679 if (!buf->b_help)
1680 get_winopts(buf);
1681#ifdef FEAT_FOLDING
1682 else
1683 /* Remove all folds in the window. */
1684 clearFolding(curwin);
1685 foldUpdateAll(curwin); /* update folds (later). */
1686#endif
1687
1688 /* Get the buffer in the current window. */
1689 curwin->w_buffer = buf;
1690 curbuf = buf;
1691 ++curbuf->b_nwindows;
1692
1693#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001694 if (curwin->w_p_diff)
1695 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696#endif
1697
Bram Moolenaara971b822011-09-14 14:43:25 +02001698#ifdef FEAT_SYN_HL
1699 curwin->w_s = &(buf->b_s);
1700#endif
1701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702 /* Cursor on first line by default. */
1703 curwin->w_cursor.lnum = 1;
1704 curwin->w_cursor.col = 0;
1705#ifdef FEAT_VIRTUALEDIT
1706 curwin->w_cursor.coladd = 0;
1707#endif
1708 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001709#ifdef FEAT_AUTOCMD
1710 curwin->w_topline_was_set = FALSE;
1711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001713 /* mark cursor position as being invalid */
1714 curwin->w_valid = 0;
1715
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 /* Make sure the buffer is loaded. */
1717 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001718 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001719#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001720 /* If there is no filetype, allow for detecting one. Esp. useful for
1721 * ":ball" used in a autocommand. If there already is a filetype we
1722 * might prefer to keep it. */
1723 if (*curbuf->b_p_ft == NUL)
1724 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001725#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001726
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001727 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 else
1730 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001731 if (!msg_silent)
1732 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1734#ifdef FEAT_AUTOCMD
1735 curwin->w_topline = 1;
1736# ifdef FEAT_DIFF
1737 curwin->w_topfill = 0;
1738# endif
1739 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1740 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1741#endif
1742 }
1743
1744 /* If autocommands did not change the cursor position, restore cursor lnum
1745 * and possibly cursor col. */
1746 if (curwin->w_cursor.lnum == 1 && inindent(0))
1747 buflist_getfpos();
1748
1749 check_arg_idx(curwin); /* check for valid arg_idx */
1750#ifdef FEAT_TITLE
1751 maketitle();
1752#endif
1753#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001754 /* when autocmds didn't change it */
1755 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756#endif
1757 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1758
Bram Moolenaar009b2592004-10-24 19:18:58 +00001759#ifdef FEAT_NETBEANS_INTG
1760 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001761 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001762#endif
1763
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001764 /* Change directories when the 'acd' option is set. */
1765 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766
1767#ifdef FEAT_KEYMAP
1768 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001769 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001771#ifdef FEAT_SPELL
1772 /* May need to set the spell language. Can only do this after the buffer
1773 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001774 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1775 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001776#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001777#ifdef FEAT_VIMINFO
1778 curbuf->b_last_used = vim_time();
1779#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001780
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 redraw_later(NOT_VALID);
1782}
1783
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001784#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1785/*
1786 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001787 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001788 */
1789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001791{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001792 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001793 && curbuf->b_ffname != NULL
1794 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001795 shorten_fnames(TRUE);
1796}
1797#endif
1798
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799/*
1800 * functions for dealing with the buffer list
1801 */
1802
Bram Moolenaar480778b2016-07-14 22:09:39 +02001803static int top_file_num = 1; /* highest file number */
1804
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805/*
1806 * Add a file name to the buffer list. Return a pointer to the buffer.
1807 * If the same file name already exists return a pointer to that buffer.
1808 * If it does not exist, or if fname == NULL, a new entry is created.
1809 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1810 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1811 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001812 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001813 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1814 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 * This is the ONLY way to create a new buffer.
1816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818buflist_new(
1819 char_u *ffname, /* full path of fname or relative */
1820 char_u *sfname, /* short fname or NULL */
1821 linenr_T lnum, /* preferred cursor line */
1822 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823{
1824 buf_T *buf;
1825#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001826 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827#endif
1828
Bram Moolenaar480778b2016-07-14 22:09:39 +02001829 if (top_file_num == 1)
1830 hash_init(&buf_hashtab);
1831
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1833
1834 /*
1835 * If file name already exists in the list, update the entry.
1836 */
1837#ifdef UNIX
1838 /* On Unix we can use inode numbers when the file exists. Works better
1839 * for hard links. */
1840 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1841 st.st_dev = (dev_T)-1;
1842#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001843 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844#ifdef UNIX
1845 buflist_findname_stat(ffname, &st)
1846#else
1847 buflist_findname(ffname)
1848#endif
1849 ) != NULL)
1850 {
1851 vim_free(ffname);
1852 if (lnum != 0)
1853 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001854
1855 if ((flags & BLN_NOOPT) == 0)
1856 /* copy the options now, if 'cpo' doesn't have 's' and not done
1857 * already */
1858 buf_copy_options(buf, 0);
1859
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1861 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001862#ifdef FEAT_AUTOCMD
1863 bufref_T bufref;
1864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 buf->b_p_bl = TRUE;
1866#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001867 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001869 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001870 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001871 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001872 return NULL;
1873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874#endif
1875 }
1876 return buf;
1877 }
1878
1879 /*
1880 * If the current buffer has no name and no contents, use the current
1881 * buffer. Otherwise: Need to allocate a new buffer structure.
1882 *
1883 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001884 * (A spell file buffer is allocated in spell.c, but that's not a normal
1885 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 */
1887 buf = NULL;
1888 if ((flags & BLN_CURBUF)
1889 && curbuf != NULL
1890 && curbuf->b_ffname == NULL
1891 && curbuf->b_nwindows <= 1
1892 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1893 {
1894 buf = curbuf;
1895#ifdef FEAT_AUTOCMD
1896 /* It's like this buffer is deleted. Watch out for autocommands that
1897 * change curbuf! If that happens, allocate a new buffer anyway. */
1898 if (curbuf->b_p_bl)
1899 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1900 if (buf == curbuf)
1901 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1902# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001903 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 return NULL;
1905# endif
1906#endif
1907#ifdef FEAT_QUICKFIX
1908# ifdef FEAT_AUTOCMD
1909 if (buf == curbuf)
1910# endif
1911 {
1912 /* Make sure 'bufhidden' and 'buftype' are empty */
1913 clear_string_option(&buf->b_p_bh);
1914 clear_string_option(&buf->b_p_bt);
1915 }
1916#endif
1917 }
1918 if (buf != curbuf || curbuf == NULL)
1919 {
1920 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1921 if (buf == NULL)
1922 {
1923 vim_free(ffname);
1924 return NULL;
1925 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001926#ifdef FEAT_EVAL
1927 /* init b: variables */
1928 buf->b_vars = dict_alloc();
1929 if (buf->b_vars == NULL)
1930 {
1931 vim_free(ffname);
1932 vim_free(buf);
1933 return NULL;
1934 }
1935 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 }
1938
1939 if (ffname != NULL)
1940 {
1941 buf->b_ffname = ffname;
1942 buf->b_sfname = vim_strsave(sfname);
1943 }
1944
1945 clear_wininfo(buf);
1946 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1947
1948 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1949 || buf->b_wininfo == NULL)
1950 {
1951 vim_free(buf->b_ffname);
1952 buf->b_ffname = NULL;
1953 vim_free(buf->b_sfname);
1954 buf->b_sfname = NULL;
1955 if (buf != curbuf)
1956 free_buffer(buf);
1957 return NULL;
1958 }
1959
1960 if (buf == curbuf)
1961 {
1962 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001963 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 if (buf != curbuf) /* autocommands deleted the buffer! */
1965 return NULL;
1966#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001967 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 return NULL;
1969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001971
1972 /* Init the options. */
1973 buf->b_p_initialized = FALSE;
1974 buf_copy_options(buf, BCO_ENTER);
1975
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976#ifdef FEAT_KEYMAP
1977 /* need to reload lmaps and set b:keymap_name */
1978 curbuf->b_kmap_state |= KEYMAP_INIT;
1979#endif
1980 }
1981 else
1982 {
1983 /*
1984 * put new buffer at the end of the buffer list
1985 */
1986 buf->b_next = NULL;
1987 if (firstbuf == NULL) /* buffer list is empty */
1988 {
1989 buf->b_prev = NULL;
1990 firstbuf = buf;
1991 }
1992 else /* append new buffer at end of list */
1993 {
1994 lastbuf->b_next = buf;
1995 buf->b_prev = lastbuf;
1996 }
1997 lastbuf = buf;
1998
1999 buf->b_fnum = top_file_num++;
2000 if (top_file_num < 0) /* wrap around (may cause duplicates) */
2001 {
2002 EMSG(_("W14: Warning: List of file names overflow"));
2003 if (emsg_silent == 0)
2004 {
2005 out_flush();
2006 ui_delay(3000L, TRUE); /* make sure it is noticed */
2007 }
2008 top_file_num = 1;
2009 }
Bram Moolenaar480778b2016-07-14 22:09:39 +02002010 buf_hashtab_add(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011
2012 /*
2013 * Always copy the options from the current buffer.
2014 */
2015 buf_copy_options(buf, BCO_ALWAYS);
2016 }
2017
2018 buf->b_wininfo->wi_fpos.lnum = lnum;
2019 buf->b_wininfo->wi_win = curwin;
2020
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00002021#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002022 hash_init(&buf->b_s.b_keywtab);
2023 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024#endif
2025
2026 buf->b_fname = buf->b_sfname;
2027#ifdef UNIX
2028 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002029 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 else
2031 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002032 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 buf->b_dev = st.st_dev;
2034 buf->b_ino = st.st_ino;
2035 }
2036#endif
2037 buf->b_u_synced = TRUE;
2038 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00002039 if (flags & BLN_DUMMY)
2040 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 buf_clear_file(buf);
2042 clrallmarks(buf); /* clear marks */
2043 fmarks_check_names(buf); /* check file marks for this file */
2044 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
2045#ifdef FEAT_AUTOCMD
2046 if (!(flags & BLN_DUMMY))
2047 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002048 bufref_T bufref;
2049
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01002050 /* Tricky: these autocommands may change the buffer list. They could
2051 * also split the window with re-using the one empty buffer. This may
2052 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002053 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02002054 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002055 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002056 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002058 {
Bram Moolenaar82404332016-07-10 17:00:38 +02002059 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02002060 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02002061 return NULL;
2062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00002064 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 return NULL;
2066# endif
2067 }
2068#endif
2069
2070 return buf;
2071}
2072
2073/*
2074 * Free the memory for the options of a buffer.
2075 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
2076 * 'fileencoding'.
2077 */
2078 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002079free_buf_options(
2080 buf_T *buf,
2081 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082{
2083 if (free_p_ff)
2084 {
2085#ifdef FEAT_MBYTE
2086 clear_string_option(&buf->b_p_fenc);
2087#endif
2088 clear_string_option(&buf->b_p_ff);
2089#ifdef FEAT_QUICKFIX
2090 clear_string_option(&buf->b_p_bh);
2091 clear_string_option(&buf->b_p_bt);
2092#endif
2093 }
2094#ifdef FEAT_FIND_ID
2095 clear_string_option(&buf->b_p_def);
2096 clear_string_option(&buf->b_p_inc);
2097# ifdef FEAT_EVAL
2098 clear_string_option(&buf->b_p_inex);
2099# endif
2100#endif
2101#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2102 clear_string_option(&buf->b_p_inde);
2103 clear_string_option(&buf->b_p_indk);
2104#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002105#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2106 clear_string_option(&buf->b_p_bexpr);
2107#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002108#if defined(FEAT_CRYPT)
2109 clear_string_option(&buf->b_p_cm);
2110#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002111#if defined(FEAT_EVAL)
2112 clear_string_option(&buf->b_p_fex);
2113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114#ifdef FEAT_CRYPT
2115 clear_string_option(&buf->b_p_key);
2116#endif
2117 clear_string_option(&buf->b_p_kp);
2118 clear_string_option(&buf->b_p_mps);
2119 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002120 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002121 clear_string_option(&buf->b_p_isk);
2122#ifdef FEAT_KEYMAP
2123 clear_string_option(&buf->b_p_keymap);
2124 ga_clear(&buf->b_kmap_ga);
2125#endif
2126#ifdef FEAT_COMMENTS
2127 clear_string_option(&buf->b_p_com);
2128#endif
2129#ifdef FEAT_FOLDING
2130 clear_string_option(&buf->b_p_cms);
2131#endif
2132 clear_string_option(&buf->b_p_nf);
2133#ifdef FEAT_SYN_HL
2134 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002135 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002136#endif
2137#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002138 clear_string_option(&buf->b_s.b_p_spc);
2139 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002140 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002141 buf->b_s.b_cap_prog = NULL;
2142 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143#endif
2144#ifdef FEAT_SEARCHPATH
2145 clear_string_option(&buf->b_p_sua);
2146#endif
2147#ifdef FEAT_AUTOCMD
2148 clear_string_option(&buf->b_p_ft);
2149#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150#ifdef FEAT_CINDENT
2151 clear_string_option(&buf->b_p_cink);
2152 clear_string_option(&buf->b_p_cino);
2153#endif
2154#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2155 clear_string_option(&buf->b_p_cinw);
2156#endif
2157#ifdef FEAT_INS_EXPAND
2158 clear_string_option(&buf->b_p_cpt);
2159#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002160#ifdef FEAT_COMPL_FUNC
2161 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002162 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164#ifdef FEAT_QUICKFIX
2165 clear_string_option(&buf->b_p_gp);
2166 clear_string_option(&buf->b_p_mp);
2167 clear_string_option(&buf->b_p_efm);
2168#endif
2169 clear_string_option(&buf->b_p_ep);
2170 clear_string_option(&buf->b_p_path);
2171 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002172 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173#ifdef FEAT_INS_EXPAND
2174 clear_string_option(&buf->b_p_dict);
2175 clear_string_option(&buf->b_p_tsr);
2176#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002177#ifdef FEAT_TEXTOBJ
2178 clear_string_option(&buf->b_p_qe);
2179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002181 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002182#ifdef FEAT_LISP
2183 clear_string_option(&buf->b_p_lw);
2184#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002185 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186}
2187
2188/*
2189 * get alternate file n
2190 * set linenr to lnum or altfpos.lnum if lnum == 0
2191 * also set cursor column to altfpos.col if 'startofline' is not set.
2192 * if (options & GETF_SETMARK) call setpcmark()
2193 * if (options & GETF_ALT) we are jumping to an alternate file.
2194 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2195 *
2196 * return FAIL for failure, OK for success
2197 */
2198 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199buflist_getfile(
2200 int n,
2201 linenr_T lnum,
2202 int options,
2203 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
2205 buf_T *buf;
2206#ifdef FEAT_WINDOWS
2207 win_T *wp = NULL;
2208#endif
2209 pos_T *fpos;
2210 colnr_T col;
2211
2212 buf = buflist_findnr(n);
2213 if (buf == NULL)
2214 {
2215 if ((options & GETF_ALT) && n == 0)
2216 EMSG(_(e_noalt));
2217 else
2218 EMSGN(_("E92: Buffer %ld not found"), n);
2219 return FAIL;
2220 }
2221
2222 /* if alternate file is the current buffer, nothing to do */
2223 if (buf == curbuf)
2224 return OK;
2225
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002226 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002227 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002228 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002230 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002231#ifdef FEAT_AUTOCMD
2232 if (curbuf_locked())
2233 return FAIL;
2234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 /* altfpos may be changed by getfile(), get it now */
2237 if (lnum == 0)
2238 {
2239 fpos = buflist_findfpos(buf);
2240 lnum = fpos->lnum;
2241 col = fpos->col;
2242 }
2243 else
2244 col = 0;
2245
2246#ifdef FEAT_WINDOWS
2247 if (options & GETF_SWITCH)
2248 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002249 /* If 'switchbuf' contains "useopen": jump to first window containing
2250 * "buf" if one exists */
2251 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002253
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002254 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002255 * page containing "buf" if one exists */
2256 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002257 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002258
2259 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2260 * current buffer isn't empty: open new tab or window */
2261 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2262 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002264 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002265 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002266 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2267 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002269 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 }
2272#endif
2273
2274 ++RedrawingDisabled;
2275 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2276 lnum, forceit) <= 0)
2277 {
2278 --RedrawingDisabled;
2279
2280 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2281 if (!p_sol && col != 0)
2282 {
2283 curwin->w_cursor.col = col;
2284 check_cursor_col();
2285#ifdef FEAT_VIRTUALEDIT
2286 curwin->w_cursor.coladd = 0;
2287#endif
2288 curwin->w_set_curswant = TRUE;
2289 }
2290 return OK;
2291 }
2292 --RedrawingDisabled;
2293 return FAIL;
2294}
2295
2296/*
2297 * go to the last know line number for the current buffer
2298 */
2299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002300buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301{
2302 pos_T *fpos;
2303
2304 fpos = buflist_findfpos(curbuf);
2305
2306 curwin->w_cursor.lnum = fpos->lnum;
2307 check_cursor_lnum();
2308
2309 if (p_sol)
2310 curwin->w_cursor.col = 0;
2311 else
2312 {
2313 curwin->w_cursor.col = fpos->col;
2314 check_cursor_col();
2315#ifdef FEAT_VIRTUALEDIT
2316 curwin->w_cursor.coladd = 0;
2317#endif
2318 curwin->w_set_curswant = TRUE;
2319 }
2320}
2321
Bram Moolenaar81695252004-12-29 20:58:21 +00002322#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2323/*
2324 * Find file in buffer list by name (it has to be for the current window).
2325 * Returns NULL if not found.
2326 */
2327 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002329{
2330 char_u *ffname;
2331 buf_T *buf = NULL;
2332
2333 /* First make the name into a full path name */
2334 ffname = FullName_save(fname,
2335#ifdef UNIX
2336 TRUE /* force expansion, get rid of symbolic links */
2337#else
2338 FALSE
2339#endif
2340 );
2341 if (ffname != NULL)
2342 {
2343 buf = buflist_findname(ffname);
2344 vim_free(ffname);
2345 }
2346 return buf;
2347}
2348#endif
2349
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350/*
2351 * Find file in buffer list by name (it has to be for the current window).
2352 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002353 * Skips dummy buffers.
2354 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 */
2356 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002357buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358{
2359#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002360 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 if (mch_stat((char *)ffname, &st) < 0)
2363 st.st_dev = (dev_T)-1;
2364 return buflist_findname_stat(ffname, &st);
2365}
2366
2367/*
2368 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2369 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002370 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 */
2372 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002373buflist_findname_stat(
2374 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002375 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376{
2377#endif
2378 buf_T *buf;
2379
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002380 /* Start at the last buffer, expect to find a match sooner. */
2381 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002382 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383#ifdef UNIX
2384 , stp
2385#endif
2386 ))
2387 return buf;
2388 return NULL;
2389}
2390
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002391#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2392 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393/*
2394 * Find file in buffer list by a regexp pattern.
2395 * Return fnum of the found buffer.
2396 * Return < 0 for error.
2397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002399buflist_findpat(
2400 char_u *pattern,
2401 char_u *pattern_end, /* pointer to first char after pattern */
2402 int unlisted, /* find unlisted buffers */
2403 int diffmode UNUSED, /* find diff-mode buffers only */
2404 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405{
2406 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 int match = -1;
2408 int find_listed;
2409 char_u *pat;
2410 char_u *patend;
2411 int attempt;
2412 char_u *p;
2413 int toggledollar;
2414
2415 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2416 {
2417 if (*pattern == '%')
2418 match = curbuf->b_fnum;
2419 else
2420 match = curwin->w_alt_fnum;
2421#ifdef FEAT_DIFF
2422 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2423 match = -1;
2424#endif
2425 }
2426
2427 /*
2428 * Try four ways of matching a listed buffer:
2429 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002430 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 * attempt == 2: with '$' at end (only match at end)
2432 * attempt == 3: with '^' at start and '$' at end (only full match)
2433 * Repeat this for finding an unlisted buffer if there was no matching
2434 * listed buffer.
2435 */
2436 else
2437 {
2438 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2439 if (pat == NULL)
2440 return -1;
2441 patend = pat + STRLEN(pat) - 1;
2442 toggledollar = (patend > pat && *patend == '$');
2443
2444 /* First try finding a listed buffer. If not found and "unlisted"
2445 * is TRUE, try finding an unlisted buffer. */
2446 find_listed = TRUE;
2447 for (;;)
2448 {
2449 for (attempt = 0; attempt <= 3; ++attempt)
2450 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002451 regmatch_T regmatch;
2452
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 /* may add '^' and '$' */
2454 if (toggledollar)
2455 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2456 p = pat;
2457 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2458 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002459 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2460 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 {
2462 vim_free(pat);
2463 return -1;
2464 }
2465
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002466 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (buf->b_p_bl == find_listed
2468#ifdef FEAT_DIFF
2469 && (!diffmode || diff_mode_buf(buf))
2470#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002471 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002473 if (curtab_only)
2474 {
2475 /* Ignore the match if the buffer is not open in
2476 * the current tab. */
2477#ifdef FEAT_WINDOWS
2478 win_T *wp;
2479
Bram Moolenaar29323592016-07-24 22:04:11 +02002480 FOR_ALL_WINDOWS(wp)
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002481 if (wp->w_buffer == buf)
2482 break;
2483 if (wp == NULL)
2484 continue;
2485#else
2486 if (curwin->w_buffer != buf)
2487 continue;
2488#endif
2489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (match >= 0) /* already found a match */
2491 {
2492 match = -2;
2493 break;
2494 }
2495 match = buf->b_fnum; /* remember first match */
2496 }
2497
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002498 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 if (match >= 0) /* found one match */
2500 break;
2501 }
2502
2503 /* Only search for unlisted buffers if there was no match with
2504 * a listed buffer. */
2505 if (!unlisted || !find_listed || match != -1)
2506 break;
2507 find_listed = FALSE;
2508 }
2509
2510 vim_free(pat);
2511 }
2512
2513 if (match == -2)
2514 EMSG2(_("E93: More than one match for %s"), pattern);
2515 else if (match < 0)
2516 EMSG2(_("E94: No matching buffer for %s"), pattern);
2517 return match;
2518}
2519#endif
2520
2521#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2522
2523/*
2524 * Find all buffer names that match.
2525 * For command line expansion of ":buf" and ":sbuf".
2526 * Return OK if matches found, FAIL otherwise.
2527 */
2528 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002529ExpandBufnames(
2530 char_u *pat,
2531 int *num_file,
2532 char_u ***file,
2533 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534{
2535 int count = 0;
2536 buf_T *buf;
2537 int round;
2538 char_u *p;
2539 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002540 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541
2542 *num_file = 0; /* return values in case of FAIL */
2543 *file = NULL;
2544
Bram Moolenaar05159a02005-02-26 23:04:13 +00002545 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2546 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002548 patc = alloc((unsigned)STRLEN(pat) + 11);
2549 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002551 STRCPY(patc, "\\(^\\|[\\/]\\)");
2552 STRCPY(patc + 11, pat + 1);
2553 }
2554 else
2555 patc = pat;
2556
2557 /*
2558 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002559 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002560 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002561 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002562 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002563 regmatch_T regmatch;
2564
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002565 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002566 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002567 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2568 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002569 {
2570 if (patc != pat)
2571 vim_free(patc);
2572 return FAIL;
2573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574
2575 /*
2576 * round == 1: Count the matches.
2577 * round == 2: Build the array to keep the matches.
2578 */
2579 for (round = 1; round <= 2; ++round)
2580 {
2581 count = 0;
Bram Moolenaar29323592016-07-24 22:04:11 +02002582 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 {
2584 if (!buf->b_p_bl) /* skip unlisted buffers */
2585 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002586 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (p != NULL)
2588 {
2589 if (round == 1)
2590 ++count;
2591 else
2592 {
2593 if (options & WILD_HOME_REPLACE)
2594 p = home_replace_save(buf, p);
2595 else
2596 p = vim_strsave(p);
2597 (*file)[count++] = p;
2598 }
2599 }
2600 }
2601 if (count == 0) /* no match found, break here */
2602 break;
2603 if (round == 1)
2604 {
2605 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2606 if (*file == NULL)
2607 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002608 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002609 if (patc != pat)
2610 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 return FAIL;
2612 }
2613 }
2614 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002615 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 if (count) /* match(es) found, break here */
2617 break;
2618 }
2619
Bram Moolenaar05159a02005-02-26 23:04:13 +00002620 if (patc != pat)
2621 vim_free(patc);
2622
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 *num_file = count;
2624 return (count == 0 ? FAIL : OK);
2625}
2626
2627#endif /* FEAT_CMDL_COMPL */
2628
2629#ifdef HAVE_BUFLIST_MATCH
2630/*
2631 * Check for a match on the file name for buffer "buf" with regprog "prog".
2632 */
2633 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002634buflist_match(
2635 regmatch_T *rmp,
2636 buf_T *buf,
2637 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638{
2639 char_u *match;
2640
2641 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002642 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002644 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645
2646 return match;
2647}
2648
2649/*
2650 * Try matching the regexp in "prog" with file name "name".
2651 * Return "name" when there is a match, NULL when not.
2652 */
2653 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002654fname_match(
2655 regmatch_T *rmp,
2656 char_u *name,
2657 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658{
2659 char_u *match = NULL;
2660 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661
2662 if (name != NULL)
2663 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002664 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002665 rmp->rm_ic = p_fic || ignore_case;
2666 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 match = name;
2668 else
2669 {
2670 /* Replace $(HOME) with '~' and try matching again. */
2671 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002672 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 match = name;
2674 vim_free(p);
2675 }
2676 }
2677
2678 return match;
2679}
2680#endif
2681
2682/*
Bram Moolenaar480778b2016-07-14 22:09:39 +02002683 * Find a file in the buffer list by buffer number.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 */
2685 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002686buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687{
Bram Moolenaar480778b2016-07-14 22:09:39 +02002688 char_u key[VIM_SIZEOF_INT * 2 + 1];
2689 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690
2691 if (nr == 0)
2692 nr = curwin->w_alt_fnum;
Bram Moolenaar480778b2016-07-14 22:09:39 +02002693 sprintf((char *)key, "%x", nr);
2694 hi = hash_find(&buf_hashtab, key);
2695
2696 if (!HASHITEM_EMPTY(hi))
2697 return (buf_T *)(hi->hi_key
2698 - ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 return NULL;
2700}
2701
2702/*
2703 * Get name of file 'n' in the buffer list.
2704 * When the file has no name an empty string is returned.
2705 * home_replace() is used to shorten the file name (used for marks).
2706 * Returns a pointer to allocated memory, of NULL when failed.
2707 */
2708 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002709buflist_nr2name(
2710 int n,
2711 int fullname,
2712 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713{
2714 buf_T *buf;
2715
2716 buf = buflist_findnr(n);
2717 if (buf == NULL)
2718 return NULL;
2719 return home_replace_save(helptail ? buf : NULL,
2720 fullname ? buf->b_ffname : buf->b_fname);
2721}
2722
2723/*
2724 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2725 * When "copy_options" is TRUE save the local window option values.
2726 * When "lnum" is 0 only do the options.
2727 */
2728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002729buflist_setfpos(
2730 buf_T *buf,
2731 win_T *win,
2732 linenr_T lnum,
2733 colnr_T col,
2734 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735{
2736 wininfo_T *wip;
2737
2738 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2739 if (wip->wi_win == win)
2740 break;
2741 if (wip == NULL)
2742 {
2743 /* allocate a new entry */
2744 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2745 if (wip == NULL)
2746 return;
2747 wip->wi_win = win;
2748 if (lnum == 0) /* set lnum even when it's 0 */
2749 lnum = 1;
2750 }
2751 else
2752 {
2753 /* remove the entry from the list */
2754 if (wip->wi_prev)
2755 wip->wi_prev->wi_next = wip->wi_next;
2756 else
2757 buf->b_wininfo = wip->wi_next;
2758 if (wip->wi_next)
2759 wip->wi_next->wi_prev = wip->wi_prev;
2760 if (copy_options && wip->wi_optset)
2761 {
2762 clear_winopt(&wip->wi_opt);
2763#ifdef FEAT_FOLDING
2764 deleteFoldRecurse(&wip->wi_folds);
2765#endif
2766 }
2767 }
2768 if (lnum != 0)
2769 {
2770 wip->wi_fpos.lnum = lnum;
2771 wip->wi_fpos.col = col;
2772 }
2773 if (copy_options)
2774 {
2775 /* Save the window-specific option values. */
2776 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2777#ifdef FEAT_FOLDING
2778 wip->wi_fold_manual = win->w_fold_manual;
2779 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2780#endif
2781 wip->wi_optset = TRUE;
2782 }
2783
2784 /* insert the entry in front of the list */
2785 wip->wi_next = buf->b_wininfo;
2786 buf->b_wininfo = wip;
2787 wip->wi_prev = NULL;
2788 if (wip->wi_next)
2789 wip->wi_next->wi_prev = wip;
2790
2791 return;
2792}
2793
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002794#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002795static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002796
2797/*
2798 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2799 * page. That's because a diff is local to a tab page.
2800 */
2801 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002802wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002803{
2804 win_T *wp;
2805
2806 if (wip->wi_opt.wo_diff)
2807 {
Bram Moolenaar29323592016-07-24 22:04:11 +02002808 FOR_ALL_WINDOWS(wp)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002809 /* return FALSE when it's a window in the current tab page, thus
2810 * the buffer was in diff mode here */
2811 if (wip->wi_win == wp)
2812 return FALSE;
2813 return TRUE;
2814 }
2815 return FALSE;
2816}
2817#endif
2818
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819/*
2820 * Find info for the current window in buffer "buf".
2821 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002822 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2823 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002824 * Returns NULL when there isn't any info.
2825 */
2826 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002827find_wininfo(
2828 buf_T *buf,
2829 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
2831 wininfo_T *wip;
2832
2833 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002834 if (wip->wi_win == curwin
2835#ifdef FEAT_DIFF
2836 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2837#endif
2838 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002839 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002840
2841 /* If no wininfo for curwin, use the first in the list (that doesn't have
2842 * 'diff' set and is in another tab page). */
2843 if (wip == NULL)
2844 {
2845#ifdef FEAT_DIFF
2846 if (skip_diff_buffer)
2847 {
2848 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2849 if (!wininfo_other_tab_diff(wip))
2850 break;
2851 }
2852 else
2853#endif
2854 wip = buf->b_wininfo;
2855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856 return wip;
2857}
2858
2859/*
2860 * Reset the local window options to the values last used in this window.
2861 * If the buffer wasn't used in this window before, use the values from
2862 * the most recently used window. If the values were never set, use the
2863 * global values for the window.
2864 */
2865 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002866get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867{
2868 wininfo_T *wip;
2869
2870 clear_winopt(&curwin->w_onebuf_opt);
2871#ifdef FEAT_FOLDING
2872 clearFolding(curwin);
2873#endif
2874
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002875 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 if (wip != NULL && wip->wi_optset)
2877 {
2878 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2879#ifdef FEAT_FOLDING
2880 curwin->w_fold_manual = wip->wi_fold_manual;
2881 curwin->w_foldinvalid = TRUE;
2882 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2883#endif
2884 }
2885 else
2886 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2887
2888#ifdef FEAT_FOLDING
2889 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2890 if (p_fdls >= 0)
2891 curwin->w_p_fdl = p_fdls;
2892#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002893#ifdef FEAT_SYN_HL
2894 check_colorcolumn(curwin);
2895#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896}
2897
2898/*
2899 * Find the position (lnum and col) for the buffer 'buf' for the current
2900 * window.
2901 * Returns a pointer to no_position if no position is found.
2902 */
2903 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002904buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002907 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002909 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 if (wip != NULL)
2911 return &(wip->wi_fpos);
2912 else
2913 return &no_position;
2914}
2915
2916/*
2917 * Find the lnum for the buffer 'buf' for the current window.
2918 */
2919 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921{
2922 return buflist_findfpos(buf)->lnum;
2923}
2924
2925#if defined(FEAT_LISTCMDS) || defined(PROTO)
2926/*
2927 * List all know file names (for :files and :buffers command).
2928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002930buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931{
2932 buf_T *buf;
2933 int len;
2934 int i;
2935
2936 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2937 {
2938 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002939 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2940 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2941 || (vim_strchr(eap->arg, '+')
2942 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2943 || (vim_strchr(eap->arg, 'a')
2944 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2945 || (vim_strchr(eap->arg, 'h')
2946 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2947 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2948 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2949 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2950 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2951 || (vim_strchr(eap->arg, '#')
2952 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002955 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 else
2957 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002958 if (message_filtered(NameBuff))
2959 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960
Bram Moolenaar77401ad2016-08-24 00:12:12 +02002961 msg_putchar('\n');
Bram Moolenaar50cde822005-06-05 21:54:54 +00002962 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002963 buf->b_fnum,
2964 buf->b_p_bl ? ' ' : 'u',
2965 buf == curbuf ? '%' :
2966 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2967 buf->b_ml.ml_mfp == NULL ? ' ' :
2968 (buf->b_nwindows == 0 ? 'h' : 'a'),
2969 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2970 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002971 : (bufIsChanged(buf) ? '+' : ' '),
2972 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002973 if (len > IOSIZE - 20)
2974 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975
2976 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 i = 40 - vim_strsize(IObuff);
2978 do
2979 {
2980 IObuff[len++] = ' ';
2981 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002982 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2983 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002984 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 msg_outtrans(IObuff);
2986 out_flush(); /* output one line at a time */
2987 ui_breakcheck();
2988 }
2989}
2990#endif
2991
2992/*
2993 * Get file name and line number for file 'fnum'.
2994 * Used by DoOneCmd() for translating '%' and '#'.
2995 * Used by insert_reg() and cmdline_paste() for '#' register.
2996 * Return FAIL if not found, OK for success.
2997 */
2998 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002999buflist_name_nr(
3000 int fnum,
3001 char_u **fname,
3002 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003{
3004 buf_T *buf;
3005
3006 buf = buflist_findnr(fnum);
3007 if (buf == NULL || buf->b_fname == NULL)
3008 return FAIL;
3009
3010 *fname = buf->b_fname;
3011 *lnum = buflist_findlnum(buf);
3012
3013 return OK;
3014}
3015
3016/*
3017 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
3018 * The file name with the full path is also remembered, for when :cd is used.
3019 * Returns FAIL for failure (file name already in use by other buffer)
3020 * OK otherwise.
3021 */
3022 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003023setfname(
3024 buf_T *buf,
3025 char_u *ffname,
3026 char_u *sfname,
3027 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
Bram Moolenaar81695252004-12-29 20:58:21 +00003029 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003031 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032#endif
3033
3034 if (ffname == NULL || *ffname == NUL)
3035 {
3036 /* Removing the name. */
3037 vim_free(buf->b_ffname);
3038 vim_free(buf->b_sfname);
3039 buf->b_ffname = NULL;
3040 buf->b_sfname = NULL;
3041#ifdef UNIX
3042 st.st_dev = (dev_T)-1;
3043#endif
3044 }
3045 else
3046 {
3047 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
3048 if (ffname == NULL) /* out of memory */
3049 return FAIL;
3050
3051 /*
3052 * if the file name is already used in another buffer:
3053 * - if the buffer is loaded, fail
3054 * - if the buffer is not loaded, delete it from the list
3055 */
3056#ifdef UNIX
3057 if (mch_stat((char *)ffname, &st) < 0)
3058 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00003059#endif
3060 if (!(buf->b_flags & BF_DUMMY))
3061#ifdef UNIX
3062 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063#else
Bram Moolenaar81695252004-12-29 20:58:21 +00003064 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065#endif
3066 if (obuf != NULL && obuf != buf)
3067 {
3068 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
3069 {
3070 if (message)
3071 EMSG(_("E95: Buffer with this name already exists"));
3072 vim_free(ffname);
3073 return FAIL;
3074 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01003075 /* delete from the list */
3076 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003077 }
3078 sfname = vim_strsave(sfname);
3079 if (ffname == NULL || sfname == NULL)
3080 {
3081 vim_free(sfname);
3082 vim_free(ffname);
3083 return FAIL;
3084 }
3085#ifdef USE_FNAME_CASE
3086# ifdef USE_LONG_FNAME
3087 if (USE_LONG_FNAME)
3088# endif
3089 fname_case(sfname, 0); /* set correct case for short file name */
3090#endif
3091 vim_free(buf->b_ffname);
3092 vim_free(buf->b_sfname);
3093 buf->b_ffname = ffname;
3094 buf->b_sfname = sfname;
3095 }
3096 buf->b_fname = buf->b_sfname;
3097#ifdef UNIX
3098 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003099 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 else
3101 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003102 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 buf->b_dev = st.st_dev;
3104 buf->b_ino = st.st_ino;
3105 }
3106#endif
3107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
3110 buf_name_changed(buf);
3111 return OK;
3112}
3113
3114/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003115 * Crude way of changing the name of a buffer. Use with care!
3116 * The name should be relative to the current directory.
3117 */
3118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003119buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003120{
3121 buf_T *buf;
3122
3123 buf = buflist_findnr(fnum);
3124 if (buf != NULL)
3125 {
3126 vim_free(buf->b_sfname);
3127 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003128 buf->b_ffname = vim_strsave(name);
3129 buf->b_sfname = NULL;
3130 /* Allocate ffname and expand into full path. Also resolves .lnk
3131 * files on Win32. */
3132 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003133 buf->b_fname = buf->b_sfname;
3134 }
3135}
3136
3137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 * Take care of what needs to be done when the name of buffer "buf" has
3139 * changed.
3140 */
3141 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003142buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
3144 /*
3145 * If the file name changed, also change the name of the swapfile
3146 */
3147 if (buf->b_ml.ml_mfp != NULL)
3148 ml_setname(buf);
3149
3150 if (curwin->w_buffer == buf)
3151 check_arg_idx(curwin); /* check file name for arg list */
3152#ifdef FEAT_TITLE
3153 maketitle(); /* set window title */
3154#endif
3155#ifdef FEAT_WINDOWS
3156 status_redraw_all(); /* status lines need to be redrawn */
3157#endif
3158 fmarks_check_names(buf); /* check named file marks */
3159 ml_timestamp(buf); /* reset timestamp */
3160}
3161
3162/*
3163 * set alternate file name for current window
3164 *
3165 * Used by do_one_cmd(), do_write() and do_ecmd().
3166 * Return the buffer.
3167 */
3168 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003169setaltfname(
3170 char_u *ffname,
3171 char_u *sfname,
3172 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173{
3174 buf_T *buf;
3175
3176 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3177 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003178 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 curwin->w_alt_fnum = buf->b_fnum;
3180 return buf;
3181}
3182
3183/*
3184 * Get alternate file name for current window.
3185 * Return NULL if there isn't any, and give error message if requested.
3186 */
3187 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003188getaltfname(
3189 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190{
3191 char_u *fname;
3192 linenr_T dummy;
3193
3194 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3195 {
3196 if (errmsg)
3197 EMSG(_(e_noalt));
3198 return NULL;
3199 }
3200 return fname;
3201}
3202
3203/*
3204 * Add a file name to the buflist and return its number.
3205 * Uses same flags as buflist_new(), except BLN_DUMMY.
3206 *
3207 * used by qf_init(), main() and doarglist()
3208 */
3209 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003210buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211{
3212 buf_T *buf;
3213
3214 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3215 if (buf != NULL)
3216 return buf->b_fnum;
3217 return 0;
3218}
3219
3220#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3221/*
3222 * Adjust slashes in file names. Called after 'shellslash' was set.
3223 */
3224 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 buf_T *bp;
3228
Bram Moolenaar29323592016-07-24 22:04:11 +02003229 FOR_ALL_BUFFERS(bp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 {
3231 if (bp->b_ffname != NULL)
3232 slash_adjust(bp->b_ffname);
3233 if (bp->b_sfname != NULL)
3234 slash_adjust(bp->b_sfname);
3235 }
3236}
3237#endif
3238
3239/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003240 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 * Also save the local window option values.
3242 */
3243 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003244buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003246 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247}
3248
3249/*
3250 * Return TRUE if 'ffname' is not the same file as current file.
3251 * Fname must have a full path (expanded by mch_FullName()).
3252 */
3253 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003254otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255{
3256 return otherfile_buf(curbuf, ffname
3257#ifdef UNIX
3258 , NULL
3259#endif
3260 );
3261}
3262
3263 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003264otherfile_buf(
3265 buf_T *buf,
3266 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003268 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003270 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271{
3272 /* no name is different */
3273 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3274 return TRUE;
3275 if (fnamecmp(ffname, buf->b_ffname) == 0)
3276 return FALSE;
3277#ifdef UNIX
3278 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003279 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar8767f522016-07-01 17:17:39 +02003281 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 if (stp == NULL)
3283 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003284 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 st.st_dev = (dev_T)-1;
3286 stp = &st;
3287 }
3288 /* Use dev/ino to check if the files are the same, even when the names
3289 * are different (possible with links). Still need to compare the
3290 * name above, for when the file doesn't exist yet.
3291 * Problem: The dev/ino changes when a file is deleted (and created
3292 * again) and remains the same when renamed/moved. We don't want to
3293 * mch_stat() each buffer each time, that would be too slow. Get the
3294 * dev/ino again when they appear to match, but not when they appear
3295 * to be different: Could skip a buffer when it's actually the same
3296 * file. */
3297 if (buf_same_ino(buf, stp))
3298 {
3299 buf_setino(buf);
3300 if (buf_same_ino(buf, stp))
3301 return FALSE;
3302 }
3303 }
3304#endif
3305 return TRUE;
3306}
3307
3308#if defined(UNIX) || defined(PROTO)
3309/*
3310 * Set inode and device number for a buffer.
3311 * Must always be called when b_fname is changed!.
3312 */
3313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003314buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003316 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317
3318 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3319 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003320 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 buf->b_dev = st.st_dev;
3322 buf->b_ino = st.st_ino;
3323 }
3324 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003325 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326}
3327
3328/*
3329 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3330 */
3331 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003332buf_same_ino(
3333 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003334 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003336 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 && stp->st_dev == buf->b_dev
3338 && stp->st_ino == buf->b_ino);
3339}
3340#endif
3341
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003342/*
3343 * Print info about the current buffer.
3344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003346fileinfo(
3347 int fullname, /* when non-zero print full path */
3348 int shorthelp,
3349 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350{
3351 char_u *name;
3352 int n;
3353 char_u *p;
3354 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003355 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356
3357 buffer = alloc(IOSIZE);
3358 if (buffer == NULL)
3359 return;
3360
3361 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3362 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003363 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 p = buffer + STRLEN(buffer);
3365 }
3366 else
3367 p = buffer;
3368
3369 *p++ = '"';
3370 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003371 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
3373 {
3374 if (!fullname && curbuf->b_fname != NULL)
3375 name = curbuf->b_fname;
3376 else
3377 name = curbuf->b_ffname;
3378 home_replace(shorthelp ? curbuf : NULL, name, p,
3379 (int)(IOSIZE - (p - buffer)), TRUE);
3380 }
3381
Bram Moolenaara800b422010-06-27 01:15:55 +02003382 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 curbufIsChanged() ? (shortmess(SHM_MOD)
3384 ? " [+]" : _(" [Modified]")) : " ",
3385 (curbuf->b_flags & BF_NOTEDITED)
3386#ifdef FEAT_QUICKFIX
3387 && !bt_dontwrite(curbuf)
3388#endif
3389 ? _("[Not edited]") : "",
3390 (curbuf->b_flags & BF_NEW)
3391#ifdef FEAT_QUICKFIX
3392 && !bt_dontwrite(curbuf)
3393#endif
3394 ? _("[New file]") : "",
3395 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003396 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 : _("[readonly]")) : "",
3398 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3399 || curbuf->b_p_ro) ?
3400 " " : "");
3401 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3402 * causes an overflow, thus for large numbers divide instead. */
3403 if (curwin->w_cursor.lnum > 1000000L)
3404 n = (int)(((long)curwin->w_cursor.lnum) /
3405 ((long)curbuf->b_ml.ml_line_count / 100L));
3406 else
3407 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3408 (long)curbuf->b_ml.ml_line_count);
3409 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3410 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003411 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 }
3413#ifdef FEAT_CMDL_INFO
3414 else if (p_ru)
3415 {
3416 /* Current line and column are already on the screen -- webb */
3417 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003418 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003420 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 (long)curbuf->b_ml.ml_line_count, n);
3422 }
3423#endif
3424 else
3425 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003426 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 _("line %ld of %ld --%d%%-- col "),
3428 (long)curwin->w_cursor.lnum,
3429 (long)curbuf->b_ml.ml_line_count,
3430 n);
3431 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003432 len = STRLEN(buffer);
3433 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3435 }
3436
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003437 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438
3439 if (dont_truncate)
3440 {
3441 /* Temporarily set msg_scroll to avoid the message being truncated.
3442 * First call msg_start() to get the message in the right place. */
3443 msg_start();
3444 n = msg_scroll;
3445 msg_scroll = TRUE;
3446 msg(buffer);
3447 msg_scroll = n;
3448 }
3449 else
3450 {
3451 p = msg_trunc_attr(buffer, FALSE, 0);
3452 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 /* Need to repeat the message after redrawing when:
3454 * - When restart_edit is set (otherwise there will be a delay
3455 * before redrawing).
3456 * - When the screen was scrolled but there is no wait-return
3457 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003458 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460
3461 vim_free(buffer);
3462}
3463
3464 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003465col_print(
3466 char_u *buf,
3467 size_t buflen,
3468 int col,
3469 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470{
3471 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003472 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003474 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475}
3476
3477#if defined(FEAT_TITLE) || defined(PROTO)
3478/*
3479 * put file name in title bar of window and in icon title
3480 */
3481
3482static char_u *lasttitle = NULL;
3483static char_u *lasticon = NULL;
3484
3485 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003486maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487{
3488 char_u *p;
3489 char_u *t_str = NULL;
3490 char_u *i_name;
3491 char_u *i_str = NULL;
3492 int maxlen = 0;
3493 int len;
3494 int mustset;
3495 char_u buf[IOSIZE];
3496 int off;
3497
3498 if (!redrawing())
3499 {
3500 /* Postpone updating the title when 'lazyredraw' is set. */
3501 need_maketitle = TRUE;
3502 return;
3503 }
3504
3505 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003506 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 return;
3508
3509 if (p_title)
3510 {
3511 if (p_titlelen > 0)
3512 {
3513 maxlen = p_titlelen * Columns / 100;
3514 if (maxlen < 10)
3515 maxlen = 10;
3516 }
3517
3518 t_str = buf;
3519 if (*p_titlestring != NUL)
3520 {
3521#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003522 if (stl_syntax & STL_IN_TITLE)
3523 {
3524 int use_sandbox = FALSE;
3525 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003526
3527# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003528 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003529# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003530 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003532 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003533 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003534 if (called_emsg)
3535 set_string_option_direct((char_u *)"titlestring", -1,
3536 (char_u *)"", OPT_FREE, SID_ERROR);
3537 called_emsg |= save_called_emsg;
3538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 else
3540#endif
3541 t_str = p_titlestring;
3542 }
3543 else
3544 {
3545 /* format: "fname + (path) (1 of 2) - VIM" */
3546
Bram Moolenaar2c666692012-09-05 13:30:40 +02003547#define SPACE_FOR_FNAME (IOSIZE - 100)
3548#define SPACE_FOR_DIR (IOSIZE - 20)
3549#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003551 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else
3553 {
3554 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003555 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 }
3558
3559 switch (bufIsChanged(curbuf)
3560 + (curbuf->b_p_ro * 2)
3561 + (!curbuf->b_p_ma * 4))
3562 {
3563 case 1: STRCAT(buf, " +"); break;
3564 case 2: STRCAT(buf, " ="); break;
3565 case 3: STRCAT(buf, " =+"); break;
3566 case 4:
3567 case 6: STRCAT(buf, " -"); break;
3568 case 5:
3569 case 7: STRCAT(buf, " -+"); break;
3570 }
3571
3572 if (curbuf->b_fname != NULL)
3573 {
3574 /* Get path of file, replace home dir with ~ */
3575 off = (int)STRLEN(buf);
3576 buf[off++] = ' ';
3577 buf[off++] = '(';
3578 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003579 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580#ifdef BACKSLASH_IN_FILENAME
3581 /* avoid "c:/name" to be reduced to "c" */
3582 if (isalpha(buf[off]) && buf[off + 1] == ':')
3583 off += 2;
3584#endif
3585 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003586 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003587 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003589 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003590 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003593
Bram Moolenaar2c666692012-09-05 13:30:40 +02003594 /* Translate unprintable chars and concatenate. Keep some
3595 * room for the server name. When there is no room (very long
3596 * file name) use (...). */
3597 if (off < SPACE_FOR_DIR)
3598 {
3599 p = transstr(buf + off);
3600 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3601 vim_free(p);
3602 }
3603 else
3604 {
3605 vim_strncpy(buf + off, (char_u *)"...",
3606 (size_t)(SPACE_FOR_ARGNR - off));
3607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 STRCAT(buf, ")");
3609 }
3610
Bram Moolenaar2c666692012-09-05 13:30:40 +02003611 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612
3613#if defined(FEAT_CLIENTSERVER)
3614 if (serverName != NULL)
3615 {
3616 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003617 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 }
3619 else
3620#endif
3621 STRCAT(buf, " - VIM");
3622
3623 if (maxlen > 0)
3624 {
3625 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003626 if (vim_strsize(buf) > maxlen)
3627 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629 }
3630 }
3631 mustset = ti_change(t_str, &lasttitle);
3632
3633 if (p_icon)
3634 {
3635 i_str = buf;
3636 if (*p_iconstring != NUL)
3637 {
3638#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003639 if (stl_syntax & STL_IN_ICON)
3640 {
3641 int use_sandbox = FALSE;
3642 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003643
3644# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003645 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003646# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003647 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003649 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003650 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003651 if (called_emsg)
3652 set_string_option_direct((char_u *)"iconstring", -1,
3653 (char_u *)"", OPT_FREE, SID_ERROR);
3654 called_emsg |= save_called_emsg;
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 else
3657#endif
3658 i_str = p_iconstring;
3659 }
3660 else
3661 {
3662 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003663 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 else /* use file name only in icon */
3665 i_name = gettail(curbuf->b_ffname);
3666 *i_str = NUL;
3667 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003668 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 if (len > 100)
3670 {
3671 len -= 100;
3672#ifdef FEAT_MBYTE
3673 if (has_mbyte)
3674 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3675#endif
3676 i_name += len;
3677 }
3678 STRCPY(i_str, i_name);
3679 trans_characters(i_str, IOSIZE);
3680 }
3681 }
3682
3683 mustset |= ti_change(i_str, &lasticon);
3684
3685 if (mustset)
3686 resettitle();
3687}
3688
3689/*
3690 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3691 * from "str" if it does.
3692 * Return TRUE when "*last" changed.
3693 */
3694 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003695ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696{
3697 if ((str == NULL) != (*last == NULL)
3698 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3699 {
3700 vim_free(*last);
3701 if (str == NULL)
3702 *last = NULL;
3703 else
3704 *last = vim_strsave(str);
3705 return TRUE;
3706 }
3707 return FALSE;
3708}
3709
3710/*
3711 * Put current window title back (used after calling a shell)
3712 */
3713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003714resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715{
3716 mch_settitle(lasttitle, lasticon);
3717}
Bram Moolenaarea408852005-06-25 22:49:46 +00003718
3719# if defined(EXITFREE) || defined(PROTO)
3720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003721free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003722{
3723 vim_free(lasttitle);
3724 vim_free(lasticon);
3725}
3726# endif
3727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728#endif /* FEAT_TITLE */
3729
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003730#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003732 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 * Return length of string in screen cells.
3734 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003735 * Normally works for window "wp", except when working for 'tabline' then it
3736 * is "curwin".
3737 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 * Items are drawn interspersed with the text that surrounds it
3739 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3740 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3741 *
3742 * If maxwidth is not zero, the string will be filled at any middle marker
3743 * or truncated if too long, fillchar is used for all whitespace.
3744 */
3745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003746build_stl_str_hl(
3747 win_T *wp,
3748 char_u *out, /* buffer to write into != NameBuff */
3749 size_t outlen, /* length of out[] */
3750 char_u *fmt,
3751 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3752 int fillchar,
3753 int maxwidth,
3754 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3755 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756{
3757 char_u *p;
3758 char_u *s;
3759 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003760 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761#ifdef FEAT_EVAL
3762 win_T *o_curwin;
3763 buf_T *o_curbuf;
3764#endif
3765 int empty_line;
3766 colnr_T virtcol;
3767 long l;
3768 long n;
3769 int prevchar_isflag;
3770 int prevchar_isitem;
3771 int itemisflag;
3772 int fillable;
3773 char_u *str;
3774 long num;
3775 int width;
3776 int itemcnt;
3777 int curitem;
3778 int groupitem[STL_MAX_ITEM];
3779 int groupdepth;
3780 struct stl_item
3781 {
3782 char_u *start;
3783 int minwid;
3784 int maxwid;
3785 enum
3786 {
3787 Normal,
3788 Empty,
3789 Group,
3790 Middle,
3791 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003792 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 Trunc
3794 } type;
3795 } item[STL_MAX_ITEM];
3796 int minwid;
3797 int maxwid;
3798 int zeropad;
3799 char_u base;
3800 char_u opt;
3801#define TMPLEN 70
3802 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003803 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003804 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003805
3806#ifdef FEAT_EVAL
3807 /*
3808 * When the format starts with "%!" then evaluate it as an expression and
3809 * use the result as the actual format string.
3810 */
3811 if (fmt[0] == '%' && fmt[1] == '!')
3812 {
3813 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3814 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003815 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003816 }
3817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818
3819 if (fillchar == 0)
3820 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003821#ifdef FEAT_MBYTE
3822 /* Can't handle a multi-byte fill character yet. */
3823 else if (mb_char2len(fillchar) > 1)
3824 fillchar = '-';
3825#endif
3826
Bram Moolenaar567199b2013-04-24 16:52:36 +02003827 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3828 * p will become invalid when getting another buffer line. */
3829 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3830 empty_line = (*p == NUL);
3831
3832 /* Get the byte value now, in case we need it below. This is more
3833 * efficient than making a copy of the line. */
3834 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3835 byteval = 0;
3836 else
3837#ifdef FEAT_MBYTE
3838 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3839#else
3840 byteval = p[wp->w_cursor.col];
3841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842
3843 groupdepth = 0;
3844 p = out;
3845 curitem = 0;
3846 prevchar_isflag = TRUE;
3847 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003848 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003850 if (curitem == STL_MAX_ITEM)
3851 {
3852 /* There are too many items. Add the error code to the statusline
3853 * to give the user a hint about what went wrong. */
3854 if (p + 6 < out + outlen)
3855 {
3856 mch_memmove(p, " E541", (size_t)5);
3857 p += 5;
3858 }
3859 break;
3860 }
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 if (*s != NUL && *s != '%')
3863 prevchar_isflag = prevchar_isitem = FALSE;
3864
3865 /*
3866 * Handle up to the next '%' or the end.
3867 */
3868 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3869 *p++ = *s++;
3870 if (*s == NUL || p + 1 >= out + outlen)
3871 break;
3872
3873 /*
3874 * Handle one '%' item.
3875 */
3876 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003877 if (*s == NUL) /* ignore trailing % */
3878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 if (*s == '%')
3880 {
3881 if (p + 1 >= out + outlen)
3882 break;
3883 *p++ = *s++;
3884 prevchar_isflag = prevchar_isitem = FALSE;
3885 continue;
3886 }
3887 if (*s == STL_MIDDLEMARK)
3888 {
3889 s++;
3890 if (groupdepth > 0)
3891 continue;
3892 item[curitem].type = Middle;
3893 item[curitem++].start = p;
3894 continue;
3895 }
3896 if (*s == STL_TRUNCMARK)
3897 {
3898 s++;
3899 item[curitem].type = Trunc;
3900 item[curitem++].start = p;
3901 continue;
3902 }
3903 if (*s == ')')
3904 {
3905 s++;
3906 if (groupdepth < 1)
3907 continue;
3908 groupdepth--;
3909
3910 t = item[groupitem[groupdepth]].start;
3911 *p = NUL;
3912 l = vim_strsize(t);
3913 if (curitem > groupitem[groupdepth] + 1
3914 && item[groupitem[groupdepth]].minwid == 0)
3915 {
3916 /* remove group if all items are empty */
3917 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003918 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 break;
3920 if (n == curitem)
3921 {
3922 p = t;
3923 l = 0;
3924 }
3925 }
3926 if (l > item[groupitem[groupdepth]].maxwid)
3927 {
3928 /* truncate, remove n bytes of text at the start */
3929#ifdef FEAT_MBYTE
3930 if (has_mbyte)
3931 {
3932 /* Find the first character that should be included. */
3933 n = 0;
3934 while (l >= item[groupitem[groupdepth]].maxwid)
3935 {
3936 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003937 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
3939 }
3940 else
3941#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003942 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003945 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 p = p - n + 1;
3947#ifdef FEAT_MBYTE
3948 /* Fill up space left over by half a double-wide char. */
3949 while (++l < item[groupitem[groupdepth]].minwid)
3950 *p++ = fillchar;
3951#endif
3952
3953 /* correct the start of the items for the truncation */
3954 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3955 {
3956 item[l].start -= n;
3957 if (item[l].start < t)
3958 item[l].start = t;
3959 }
3960 }
3961 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3962 {
3963 /* fill */
3964 n = item[groupitem[groupdepth]].minwid;
3965 if (n < 0)
3966 {
3967 /* fill by appending characters */
3968 n = 0 - n;
3969 while (l++ < n && p + 1 < out + outlen)
3970 *p++ = fillchar;
3971 }
3972 else
3973 {
3974 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003975 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 l = n - l;
3977 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003978 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979 p += l;
3980 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3981 item[n].start += l;
3982 for ( ; l > 0; l--)
3983 *t++ = fillchar;
3984 }
3985 }
3986 continue;
3987 }
3988 minwid = 0;
3989 maxwid = 9999;
3990 zeropad = FALSE;
3991 l = 1;
3992 if (*s == '0')
3993 {
3994 s++;
3995 zeropad = TRUE;
3996 }
3997 if (*s == '-')
3998 {
3999 s++;
4000 l = -1;
4001 }
4002 if (VIM_ISDIGIT(*s))
4003 {
4004 minwid = (int)getdigits(&s);
4005 if (minwid < 0) /* overflow */
4006 minwid = 0;
4007 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004008 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 {
4010 item[curitem].type = Highlight;
4011 item[curitem].start = p;
4012 item[curitem].minwid = minwid > 9 ? 1 : minwid;
4013 s++;
4014 curitem++;
4015 continue;
4016 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004017 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
4018 {
4019 if (*s == STL_TABCLOSENR)
4020 {
4021 if (minwid == 0)
4022 {
4023 /* %X ends the close label, go back to the previously
4024 * define tab label nr. */
4025 for (n = curitem - 1; n >= 0; --n)
4026 if (item[n].type == TabPage && item[n].minwid >= 0)
4027 {
4028 minwid = item[n].minwid;
4029 break;
4030 }
4031 }
4032 else
4033 /* close nrs are stored as negative values */
4034 minwid = - minwid;
4035 }
4036 item[curitem].type = TabPage;
4037 item[curitem].start = p;
4038 item[curitem].minwid = minwid;
4039 s++;
4040 curitem++;
4041 continue;
4042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (*s == '.')
4044 {
4045 s++;
4046 if (VIM_ISDIGIT(*s))
4047 {
4048 maxwid = (int)getdigits(&s);
4049 if (maxwid <= 0) /* overflow */
4050 maxwid = 50;
4051 }
4052 }
4053 minwid = (minwid > 50 ? 50 : minwid) * l;
4054 if (*s == '(')
4055 {
4056 groupitem[groupdepth++] = curitem;
4057 item[curitem].type = Group;
4058 item[curitem].start = p;
4059 item[curitem].minwid = minwid;
4060 item[curitem].maxwid = maxwid;
4061 s++;
4062 curitem++;
4063 continue;
4064 }
4065 if (vim_strchr(STL_ALL, *s) == NULL)
4066 {
4067 s++;
4068 continue;
4069 }
4070 opt = *s++;
4071
4072 /* OK - now for the real work */
4073 base = 'D';
4074 itemisflag = FALSE;
4075 fillable = TRUE;
4076 num = -1;
4077 str = NULL;
4078 switch (opt)
4079 {
4080 case STL_FILEPATH:
4081 case STL_FULLPATH:
4082 case STL_FILENAME:
4083 fillable = FALSE; /* don't change ' ' to fillchar */
4084 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02004085 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 else
4087 {
4088 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004089 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
4091 }
4092 trans_characters(NameBuff, MAXPATHL);
4093 if (opt != STL_FILENAME)
4094 str = NameBuff;
4095 else
4096 str = gettail(NameBuff);
4097 break;
4098
4099 case STL_VIM_EXPR: /* '{' */
4100 itemisflag = TRUE;
4101 t = p;
4102 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
4103 *p++ = *s++;
4104 if (*s != '}') /* missing '}' or out of space */
4105 break;
4106 s++;
4107 *p = 0;
4108 p = t;
4109
4110#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004111 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4113
4114 o_curbuf = curbuf;
4115 o_curwin = curwin;
4116 curwin = wp;
4117 curbuf = wp->w_buffer;
4118
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004119 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
4121 curwin = o_curwin;
4122 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004123 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124
4125 if (str != NULL && *str != 0)
4126 {
4127 if (*skipdigits(str) == NUL)
4128 {
4129 num = atoi((char *)str);
4130 vim_free(str);
4131 str = NULL;
4132 itemisflag = FALSE;
4133 }
4134 }
4135#endif
4136 break;
4137
4138 case STL_LINE:
4139 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4140 ? 0L : (long)(wp->w_cursor.lnum);
4141 break;
4142
4143 case STL_NUMLINES:
4144 num = wp->w_buffer->b_ml.ml_line_count;
4145 break;
4146
4147 case STL_COLUMN:
4148 num = !(State & INSERT) && empty_line
4149 ? 0 : (int)wp->w_cursor.col + 1;
4150 break;
4151
4152 case STL_VIRTCOL:
4153 case STL_VIRTCOL_ALT:
4154 /* In list mode virtcol needs to be recomputed */
4155 virtcol = wp->w_virtcol;
4156 if (wp->w_p_list && lcs_tab1 == NUL)
4157 {
4158 wp->w_p_list = FALSE;
4159 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4160 wp->w_p_list = TRUE;
4161 }
4162 ++virtcol;
4163 /* Don't display %V if it's the same as %c. */
4164 if (opt == STL_VIRTCOL_ALT
4165 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4166 ? 0 : (int)wp->w_cursor.col + 1)))
4167 break;
4168 num = (long)virtcol;
4169 break;
4170
4171 case STL_PERCENTAGE:
4172 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4173 (long)wp->w_buffer->b_ml.ml_line_count);
4174 break;
4175
4176 case STL_ALTPERCENT:
4177 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004178 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 break;
4180
4181 case STL_ARGLISTSTAT:
4182 fillable = FALSE;
4183 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004184 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 str = tmp;
4186 break;
4187
4188 case STL_KEYMAP:
4189 fillable = FALSE;
Bram Moolenaar73ac0c42016-07-24 16:17:59 +02004190 if (get_keymap_str(wp, (char_u *)"<%s>", tmp, TMPLEN))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 str = tmp;
4192 break;
4193 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004194#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004195 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196#else
4197 num = 0;
4198#endif
4199 break;
4200
4201 case STL_BUFNO:
4202 num = wp->w_buffer->b_fnum;
4203 break;
4204
4205 case STL_OFFSET_X:
4206 base = 'X';
4207 case STL_OFFSET:
4208#ifdef FEAT_BYTEOFF
4209 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4210 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4211 0L : l + 1 + (!(State & INSERT) && empty_line ?
4212 0 : (int)wp->w_cursor.col);
4213#endif
4214 break;
4215
4216 case STL_BYTEVAL_X:
4217 base = 'X';
4218 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004219 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 if (num == NL)
4221 num = 0;
4222 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4223 num = NL;
4224 break;
4225
4226 case STL_ROFLAG:
4227 case STL_ROFLAG_ALT:
4228 itemisflag = TRUE;
4229 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004230 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 break;
4232
4233 case STL_HELPFLAG:
4234 case STL_HELPFLAG_ALT:
4235 itemisflag = TRUE;
4236 if (wp->w_buffer->b_help)
4237 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004238 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 break;
4240
4241#ifdef FEAT_AUTOCMD
4242 case STL_FILETYPE:
4243 if (*wp->w_buffer->b_p_ft != NUL
4244 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4245 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004246 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4247 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 str = tmp;
4249 }
4250 break;
4251
4252 case STL_FILETYPE_ALT:
4253 itemisflag = TRUE;
4254 if (*wp->w_buffer->b_p_ft != NUL
4255 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4256 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004257 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4258 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 for (t = tmp; *t != 0; t++)
4260 *t = TOUPPER_LOC(*t);
4261 str = tmp;
4262 }
4263 break;
4264#endif
4265
4266#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4267 case STL_PREVIEWFLAG:
4268 case STL_PREVIEWFLAG_ALT:
4269 itemisflag = TRUE;
4270 if (wp->w_p_pvw)
4271 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4272 : _("[Preview]"));
4273 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004274
4275 case STL_QUICKFIX:
4276 if (bt_quickfix(wp->w_buffer))
4277 str = (char_u *)(wp->w_llist_ref
4278 ? _(msg_loclist)
4279 : _(msg_qflist));
4280 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#endif
4282
4283 case STL_MODIFIED:
4284 case STL_MODIFIED_ALT:
4285 itemisflag = TRUE;
4286 switch ((opt == STL_MODIFIED_ALT)
4287 + bufIsChanged(wp->w_buffer) * 2
4288 + (!wp->w_buffer->b_p_ma) * 4)
4289 {
4290 case 2: str = (char_u *)"[+]"; break;
4291 case 3: str = (char_u *)",+"; break;
4292 case 4: str = (char_u *)"[-]"; break;
4293 case 5: str = (char_u *)",-"; break;
4294 case 6: str = (char_u *)"[+-]"; break;
4295 case 7: str = (char_u *)",+-"; break;
4296 }
4297 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004298
4299 case STL_HIGHLIGHT:
4300 t = s;
4301 while (*s != '#' && *s != NUL)
4302 ++s;
4303 if (*s == '#')
4304 {
4305 item[curitem].type = Highlight;
4306 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004307 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004308 curitem++;
4309 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004310 if (*s != NUL)
4311 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004312 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314
4315 item[curitem].start = p;
4316 item[curitem].type = Normal;
4317 if (str != NULL && *str)
4318 {
4319 t = str;
4320 if (itemisflag)
4321 {
4322 if ((t[0] && t[1])
4323 && ((!prevchar_isitem && *t == ',')
4324 || (prevchar_isflag && *t == ' ')))
4325 t++;
4326 prevchar_isflag = TRUE;
4327 }
4328 l = vim_strsize(t);
4329 if (l > 0)
4330 prevchar_isitem = TRUE;
4331 if (l > maxwid)
4332 {
4333 while (l >= maxwid)
4334#ifdef FEAT_MBYTE
4335 if (has_mbyte)
4336 {
4337 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004338 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 }
4340 else
4341#endif
4342 l -= byte2cells(*t++);
4343 if (p + 1 >= out + outlen)
4344 break;
4345 *p++ = '<';
4346 }
4347 if (minwid > 0)
4348 {
4349 for (; l < minwid && p + 1 < out + outlen; l++)
4350 {
4351 /* Don't put a "-" in front of a digit. */
4352 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4353 *p++ = ' ';
4354 else
4355 *p++ = fillchar;
4356 }
4357 minwid = 0;
4358 }
4359 else
4360 minwid *= -1;
4361 while (*t && p + 1 < out + outlen)
4362 {
4363 *p++ = *t++;
4364 /* Change a space by fillchar, unless fillchar is '-' and a
4365 * digit follows. */
4366 if (fillable && p[-1] == ' '
4367 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4368 p[-1] = fillchar;
4369 }
4370 for (; l < minwid && p + 1 < out + outlen; l++)
4371 *p++ = fillchar;
4372 }
4373 else if (num >= 0)
4374 {
4375 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4376 char_u nstr[20];
4377
4378 if (p + 20 >= out + outlen)
4379 break; /* not sufficient space */
4380 prevchar_isitem = TRUE;
4381 t = nstr;
4382 if (opt == STL_VIRTCOL_ALT)
4383 {
4384 *t++ = '-';
4385 minwid--;
4386 }
4387 *t++ = '%';
4388 if (zeropad)
4389 *t++ = '0';
4390 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004391 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 *t = 0;
4393
4394 for (n = num, l = 1; n >= nbase; n /= nbase)
4395 l++;
4396 if (opt == STL_VIRTCOL_ALT)
4397 l++;
4398 if (l > maxwid)
4399 {
4400 l += 2;
4401 n = l - maxwid;
4402 while (l-- > maxwid)
4403 num /= nbase;
4404 *t++ = '>';
4405 *t++ = '%';
4406 *t = t[-3];
4407 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004408 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4409 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004412 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4413 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 p += STRLEN(p);
4415 }
4416 else
4417 item[curitem].type = Empty;
4418
4419 if (opt == STL_VIM_EXPR)
4420 vim_free(str);
4421
4422 if (num >= 0 || (!itemisflag && str && *str))
4423 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4424 curitem++;
4425 }
4426 *p = NUL;
4427 itemcnt = curitem;
4428
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004429#ifdef FEAT_EVAL
4430 if (usefmt != fmt)
4431 vim_free(usefmt);
4432#endif
4433
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 width = vim_strsize(out);
4435 if (maxwidth > 0 && width > maxwidth)
4436 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004437 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 l = 0;
4439 if (itemcnt == 0)
4440 s = out;
4441 else
4442 {
4443 for ( ; l < itemcnt; l++)
4444 if (item[l].type == Trunc)
4445 {
4446 /* Truncate at %< item. */
4447 s = item[l].start;
4448 break;
4449 }
4450 if (l == itemcnt)
4451 {
4452 /* No %< item, truncate first item. */
4453 s = item[0].start;
4454 l = 0;
4455 }
4456 }
4457
4458 if (width - vim_strsize(s) >= maxwidth)
4459 {
4460 /* Truncation mark is beyond max length */
4461#ifdef FEAT_MBYTE
4462 if (has_mbyte)
4463 {
4464 s = out;
4465 width = 0;
4466 for (;;)
4467 {
4468 width += ptr2cells(s);
4469 if (width >= maxwidth)
4470 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004471 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473 /* Fill up for half a double-wide character. */
4474 while (++width < maxwidth)
4475 *s++ = fillchar;
4476 }
4477 else
4478#endif
4479 s = out + maxwidth - 1;
4480 for (l = 0; l < itemcnt; l++)
4481 if (item[l].start > s)
4482 break;
4483 itemcnt = l;
4484 *s++ = '>';
4485 *s = 0;
4486 }
4487 else
4488 {
4489#ifdef FEAT_MBYTE
4490 if (has_mbyte)
4491 {
4492 n = 0;
4493 while (width >= maxwidth)
4494 {
4495 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004496 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 }
4499 else
4500#endif
4501 n = width - maxwidth + 1;
4502 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004503 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 *s = '<';
4505
4506 /* Fill up for half a double-wide character. */
4507 while (++width < maxwidth)
4508 {
4509 s = s + STRLEN(s);
4510 *s++ = fillchar;
4511 *s = NUL;
4512 }
4513
4514 --n; /* count the '<' */
4515 for (; l < itemcnt; l++)
4516 {
4517 if (item[l].start - n >= s)
4518 item[l].start -= n;
4519 else
4520 item[l].start = s;
4521 }
4522 }
4523 width = maxwidth;
4524 }
4525 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4526 {
4527 /* Apply STL_MIDDLE if any */
4528 for (l = 0; l < itemcnt; l++)
4529 if (item[l].type == Middle)
4530 break;
4531 if (l < itemcnt)
4532 {
4533 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004534 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 for (s = item[l].start; s < p; s++)
4536 *s = fillchar;
4537 for (l++; l < itemcnt; l++)
4538 item[l].start += maxwidth - width;
4539 width = maxwidth;
4540 }
4541 }
4542
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004543 /* Store the info about highlighting. */
4544 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004546 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 for (l = 0; l < itemcnt; l++)
4548 {
4549 if (item[l].type == Highlight)
4550 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004551 sp->start = item[l].start;
4552 sp->userhl = item[l].minwid;
4553 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004556 sp->start = NULL;
4557 sp->userhl = 0;
4558 }
4559
4560 /* Store the info about tab pages labels. */
4561 if (tabtab != NULL)
4562 {
4563 sp = tabtab;
4564 for (l = 0; l < itemcnt; l++)
4565 {
4566 if (item[l].type == TabPage)
4567 {
4568 sp->start = item[l].start;
4569 sp->userhl = item[l].minwid;
4570 sp++;
4571 }
4572 }
4573 sp->start = NULL;
4574 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 }
4576
4577 return width;
4578}
4579#endif /* FEAT_STL_OPT */
4580
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004581#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4582 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004584 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4585 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 */
4587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004588get_rel_pos(
4589 win_T *wp,
4590 char_u *buf,
4591 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592{
4593 long above; /* number of lines above window */
4594 long below; /* number of lines below window */
4595
Bram Moolenaar0027c212015-01-07 13:31:52 +01004596 if (buflen < 3) /* need at least 3 chars for writing */
4597 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 above = wp->w_topline - 1;
4599#ifdef FEAT_DIFF
4600 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004601 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4602 above = 0; /* All buffer lines are displayed and there is an
4603 * indication of filler lines, that can be considered
4604 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605#endif
4606 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4607 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004608 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4609 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004611 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004613 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 ? (int)(above / ((above + below) / 100L))
4615 : (int)(above * 100L / (above + below)));
4616}
4617#endif
4618
4619/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004620 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 * Return TRUE if it was appended.
4622 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004624append_arg_number(
4625 win_T *wp,
4626 char_u *buf,
4627 int buflen,
4628 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 char_u *p;
4631
4632 if (ARGCOUNT <= 1) /* nothing to do */
4633 return FALSE;
4634
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004635 p = buf + STRLEN(buf); /* go to the end of the buffer */
4636 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 return FALSE;
4638 *p++ = ' ';
4639 *p++ = '(';
4640 if (add_file)
4641 {
4642 STRCPY(p, "file ");
4643 p += 5;
4644 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004645 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4646 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4648 return TRUE;
4649}
4650
4651/*
4652 * If fname is not a full path, make it a full path.
4653 * Returns pointer to allocated memory (NULL for failure).
4654 */
4655 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004656fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004657{
4658 /*
4659 * Force expanding the path always for Unix, because symbolic links may
4660 * mess up the full path name, even though it starts with a '/'.
4661 * Also expand when there is ".." in the file name, try to remove it,
4662 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004663 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 * For MS-Windows also expand names like "longna~1" to "longname".
4665 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004666#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 return FullName_save(fname, TRUE);
4668#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004669 if (!vim_isAbsName(fname)
4670 || strstr((char *)fname, "..") != NULL
4671 || strstr((char *)fname, "//") != NULL
4672# ifdef BACKSLASH_IN_FILENAME
4673 || strstr((char *)fname, "\\\\") != NULL
4674# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004675# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004677# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 )
4679 return FullName_save(fname, FALSE);
4680
4681 fname = vim_strsave(fname);
4682
Bram Moolenaar9b942202007-10-03 12:31:33 +00004683# ifdef USE_FNAME_CASE
4684# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004686# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 {
4688 if (fname != NULL)
4689 fname_case(fname, 0); /* set correct case for file name */
4690 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004691# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
4693 return fname;
4694#endif
4695}
4696
4697/*
4698 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4699 * "ffname" becomes a pointer to allocated memory (or NULL).
4700 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004702fname_expand(
4703 buf_T *buf UNUSED,
4704 char_u **ffname,
4705 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706{
4707 if (*ffname == NULL) /* if no file name given, nothing to do */
4708 return;
4709 if (*sfname == NULL) /* if no short file name given, use ffname */
4710 *sfname = *ffname;
4711 *ffname = fix_fname(*ffname); /* expand to full path */
4712
4713#ifdef FEAT_SHORTCUT
4714 if (!buf->b_p_bin)
4715 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004716 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717
4718 /* If the file name is a shortcut file, use the file it links to. */
4719 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004720 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 {
4722 vim_free(*ffname);
4723 *ffname = rfname;
4724 *sfname = rfname;
4725 }
4726 }
4727#endif
4728}
4729
4730/*
4731 * Get the file name for an argument list entry.
4732 */
4733 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004734alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 buf_T *bp;
4737
4738 /* Use the name from the associated buffer if it exists. */
4739 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004740 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 return aep->ae_fname;
4742 return bp->b_fname;
4743}
4744
4745#if defined(FEAT_WINDOWS) || defined(PROTO)
4746/*
4747 * do_arg_all(): Open up to 'count' windows, one for each argument.
4748 */
4749 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004750do_arg_all(
4751 int count,
4752 int forceit, /* hide buffers in current windows */
4753 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754{
4755 int i;
4756 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004757 char_u *opened; /* Array of weight for which args are open:
4758 * 0: not opened
4759 * 1: opened in other tab
4760 * 2: opened in curtab
4761 * 3: opened in curtab and curwin
4762 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004763 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 int use_firstwin = FALSE; /* use first window for arglist */
4765 int split_ret = OK;
4766 int p_ea_save;
4767 alist_T *alist; /* argument list to be used */
4768 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004769 tabpage_T *tpnext;
4770 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004771 win_T *old_curwin, *last_curwin;
4772 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004773 win_T *new_curwin = NULL;
4774 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775
4776 if (ARGCOUNT <= 0)
4777 {
4778 /* Don't give an error message. We don't want it when the ":all"
4779 * command is in the .vimrc. */
4780 return;
4781 }
4782 setpcmark();
4783
4784 opened_len = ARGCOUNT;
4785 opened = alloc_clear((unsigned)opened_len);
4786 if (opened == NULL)
4787 return;
4788
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004789 /* Autocommands may do anything to the argument list. Make sure it's not
4790 * freed while we are working here by "locking" it. We still have to
4791 * watch out for its size to be changed. */
4792 alist = curwin->w_alist;
4793 ++alist->al_refcount;
4794
4795 old_curwin = curwin;
4796 old_curtab = curtab;
4797
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004798# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 /*
4803 * Try closing all windows that are not in the argument list.
4804 * Also close windows that are not full width;
4805 * When 'hidden' or "forceit" set the buffer becomes hidden.
4806 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004807 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004809 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004810 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004811 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004813 tpnext = curtab->tp_next;
4814 for (wp = firstwin; wp != NULL; wp = wpnext)
4815 {
4816 wpnext = wp->w_next;
4817 buf = wp->w_buffer;
4818 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004819 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004820 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004821 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004822 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004824 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004825 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004827 if (i < alist->al_ga.ga_len
4828 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4829 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4830 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004831 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004832 int weight = 1;
4833
4834 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004835 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004836 ++weight;
4837 if (old_curwin == wp)
4838 ++weight;
4839 }
4840
4841 if (weight > (int)opened[i])
4842 {
4843 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004844 if (i == 0)
4845 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004846 if (new_curwin != NULL)
4847 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004848 new_curwin = wp;
4849 new_curtab = curtab;
4850 }
4851 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004852 else if (keep_tabs)
4853 i = opened_len;
4854
4855 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004856 {
4857 /* Use the current argument list for all windows
4858 * containing a file from it. */
4859 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004860 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004861 ++wp->w_alist->al_refcount;
4862 }
4863 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 }
4866 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004867 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004869 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004871 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004872 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004874 /* If the buffer was changed, and we would like to hide it,
4875 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004876 if (!P_HID(buf) && buf->b_nwindows <= 1
4877 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004879#ifdef FEAT_AUTOCMD
4880 bufref_T bufref;
4881
4882 set_bufref(&bufref, buf);
4883#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004884 (void)autowrite(buf, FALSE);
4885#ifdef FEAT_AUTOCMD
4886 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004887 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004888 {
4889 wpnext = firstwin; /* start all over... */
4890 continue;
4891 }
4892#endif
4893 }
4894#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004895 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004896 if (firstwin == lastwin
4897 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004898#endif
4899 use_firstwin = TRUE;
4900#ifdef FEAT_WINDOWS
4901 else
4902 {
4903 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4904# ifdef FEAT_AUTOCMD
4905 /* check if autocommands removed the next window */
4906 if (!win_valid(wpnext))
4907 wpnext = firstwin; /* start all over... */
4908# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
4910#endif
4911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
4913 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004914
4915 /* Without the ":tab" modifier only do the current tab page. */
4916 if (had_tab == 0 || tpnext == NULL)
4917 break;
4918
4919# ifdef FEAT_AUTOCMD
4920 /* check if autocommands removed the next tab page */
4921 if (!valid_tabpage(tpnext))
4922 tpnext = first_tabpage; /* start all over...*/
4923# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004924 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926
4927 /*
4928 * Open a window for files in the argument list that don't have one.
4929 * ARGCOUNT may change while doing this, because of autocommands.
4930 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004931 if (count > opened_len || count <= 0)
4932 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933
4934#ifdef FEAT_AUTOCMD
4935 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4936 ++autocmd_no_enter;
4937 ++autocmd_no_leave;
4938#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004939 last_curwin = curwin;
4940 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004942#ifdef FEAT_WINDOWS
4943 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4944 * leaving an empty tab page when executed locally. */
4945 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4946 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4947 use_firstwin = TRUE;
4948#endif
4949
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004950 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 {
4952 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4953 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004954 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
4956 /* Move the already present window to below the current window */
4957 if (curwin->w_arg_idx != i)
4958 {
4959 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4960 {
4961 if (wpnext->w_arg_idx == i)
4962 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004963 if (keep_tabs)
4964 {
4965 new_curwin = wpnext;
4966 new_curtab = curtab;
4967 }
4968 else
4969 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 break;
4971 }
4972 }
4973 }
4974 }
4975 else if (split_ret == OK)
4976 {
4977 if (!use_firstwin) /* split current window */
4978 {
4979 p_ea_save = p_ea;
4980 p_ea = TRUE; /* use space from all windows */
4981 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4982 p_ea = p_ea_save;
4983 if (split_ret == FAIL)
4984 continue;
4985 }
4986#ifdef FEAT_AUTOCMD
4987 else /* first window: do autocmd for leaving this buffer */
4988 --autocmd_no_leave;
4989#endif
4990
4991 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004992 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 */
4994 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004995 if (i == 0)
4996 {
4997 new_curwin = curwin;
4998 new_curtab = curtab;
4999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
5001 ECMD_ONE,
5002 ((P_HID(curwin->w_buffer)
5003 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00005004 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005#ifdef FEAT_AUTOCMD
5006 if (use_firstwin)
5007 ++autocmd_no_leave;
5008#endif
5009 use_firstwin = FALSE;
5010 }
5011 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005012
5013 /* When ":tab" was used open a new tab for a new window repeatedly. */
5014 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5015 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017
5018 /* Remove the "lock" on the argument list. */
5019 alist_unlink(alist);
5020
5021#ifdef FEAT_AUTOCMD
5022 --autocmd_no_enter;
5023#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005024 /* restore last referenced tabpage's curwin */
5025 if (last_curtab != new_curtab)
5026 {
5027 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005028 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01005029 if (win_valid(last_curwin))
5030 win_enter(last_curwin, FALSE);
5031 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005032 /* to window with first arg */
5033 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005034 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00005035 if (win_valid(new_curwin))
5036 win_enter(new_curwin, FALSE);
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038#ifdef FEAT_AUTOCMD
5039 --autocmd_no_leave;
5040#endif
5041 vim_free(opened);
5042}
5043
5044# if defined(FEAT_LISTCMDS) || defined(PROTO)
5045/*
5046 * Open a window for a number of buffers.
5047 */
5048 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005049ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
5051 buf_T *buf;
5052 win_T *wp, *wpnext;
5053 int split_ret = OK;
5054 int p_ea_save;
5055 int open_wins = 0;
5056 int r;
5057 int count; /* Maximum number of windows to open. */
5058 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005059#ifdef FEAT_WINDOWS
5060 int had_tab = cmdmod.tab;
5061 tabpage_T *tpnext;
5062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 if (eap->addr_count == 0) /* make as many windows as possible */
5065 count = 9999;
5066 else
5067 count = eap->line2; /* make as many windows as specified */
5068 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
5069 all = FALSE;
5070 else
5071 all = TRUE;
5072
5073 setpcmark();
5074
5075#ifdef FEAT_GUI
5076 need_mouse_correct = TRUE;
5077#endif
5078
5079 /*
5080 * Close superfluous windows (two windows for the same buffer).
5081 * Also close windows that are not full-width.
5082 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005083#ifdef FEAT_WINDOWS
5084 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005085 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005086 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005089 tpnext = curtab->tp_next;
5090 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005092 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005093 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01005094#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005095 || ((cmdmod.split & WSP_VERT)
5096 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00005097 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005098 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005099 || (had_tab > 0 && wp != firstwin)
5100#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02005101 ) && firstwin != lastwin
5102#ifdef FEAT_AUTOCMD
5103 && !(wp->w_closing || wp->w_buffer->b_closing)
5104#endif
5105 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005106 {
5107 win_close(wp, FALSE);
5108#ifdef FEAT_AUTOCMD
5109 wpnext = firstwin; /* just in case an autocommand does
5110 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005111 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005112 open_wins = 0;
5113#endif
5114 }
5115 else
5116 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005118
5119#ifdef FEAT_WINDOWS
5120 /* Without the ":tab" modifier only do the current tab page. */
5121 if (had_tab == 0 || tpnext == NULL)
5122 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005123 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
5127 /*
5128 * Go through the buffer list. When a buffer doesn't have a window yet,
5129 * open one. Otherwise move the window to the right position.
5130 * Watch out for autocommands that delete buffers or windows!
5131 */
5132#ifdef FEAT_AUTOCMD
5133 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5134 ++autocmd_no_enter;
5135#endif
5136 win_enter(lastwin, FALSE);
5137#ifdef FEAT_AUTOCMD
5138 ++autocmd_no_leave;
5139#endif
5140 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5141 {
5142 /* Check if this buffer needs a window */
5143 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5144 continue;
5145
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005146#ifdef FEAT_WINDOWS
5147 if (had_tab != 0)
5148 {
5149 /* With the ":tab" modifier don't move the window. */
5150 if (buf->b_nwindows > 0)
5151 wp = lastwin; /* buffer has a window, skip it */
5152 else
5153 wp = NULL;
5154 }
5155 else
5156#endif
5157 {
5158 /* Check if this buffer already has a window */
Bram Moolenaar29323592016-07-24 22:04:11 +02005159 FOR_ALL_WINDOWS(wp)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005160 if (wp->w_buffer == buf)
5161 break;
5162 /* If the buffer already has a window, move it */
5163 if (wp != NULL)
5164 win_move_after(wp, curwin);
5165 }
5166
5167 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005169#ifdef FEAT_AUTOCMD
5170 bufref_T bufref;
5171
5172 set_bufref(&bufref, buf);
5173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 /* Split the window and put the buffer in it */
5175 p_ea_save = p_ea;
5176 p_ea = TRUE; /* use space from all windows */
5177 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5178 ++open_wins;
5179 p_ea = p_ea_save;
5180 if (split_ret == FAIL)
5181 continue;
5182
5183 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005184#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 swap_exists_action = SEA_DIALOG;
5186#endif
5187 set_curbuf(buf, DOBUF_GOTO);
5188#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005189 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005191 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005192#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 break;
5196 }
5197#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005198#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 if (swap_exists_action == SEA_QUIT)
5200 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005201# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5202 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005203
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005204 /* Reset the error/interrupt/exception state here so that
5205 * aborting() returns FALSE when closing a window. */
5206 enter_cleanup(&cs);
5207# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005208
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005209 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 win_close(curwin, TRUE);
5211 --open_wins;
5212 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005213 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005214
5215# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5216 /* Restore the error/interrupt/exception state if not
5217 * discarded by a new aborting error, interrupt, or uncaught
5218 * exception. */
5219 leave_cleanup(&cs);
5220# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 }
5222 else
5223 handle_swap_exists(NULL);
5224#endif
5225 }
5226
5227 ui_breakcheck();
5228 if (got_int)
5229 {
5230 (void)vgetc(); /* only break the file loading, not the rest */
5231 break;
5232 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005233#ifdef FEAT_EVAL
5234 /* Autocommands deleted the buffer or aborted script processing!!! */
5235 if (aborting())
5236 break;
5237#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005238#ifdef FEAT_WINDOWS
5239 /* When ":tab" was used open a new tab for a new window repeatedly. */
5240 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5241 cmdmod.tab = 9999;
5242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 }
5244#ifdef FEAT_AUTOCMD
5245 --autocmd_no_enter;
5246#endif
5247 win_enter(firstwin, FALSE); /* back to first window */
5248#ifdef FEAT_AUTOCMD
5249 --autocmd_no_leave;
5250#endif
5251
5252 /*
5253 * Close superfluous windows.
5254 */
5255 for (wp = lastwin; open_wins > count; )
5256 {
5257 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5258 || autowrite(wp->w_buffer, FALSE) == OK);
5259#ifdef FEAT_AUTOCMD
5260 if (!win_valid(wp))
5261 {
5262 /* BufWrite Autocommands made the window invalid, start over */
5263 wp = lastwin;
5264 }
5265 else
5266#endif
5267 if (r)
5268 {
5269 win_close(wp, !P_HID(wp->w_buffer));
5270 --open_wins;
5271 wp = lastwin;
5272 }
5273 else
5274 {
5275 wp = wp->w_prev;
5276 if (wp == NULL)
5277 break;
5278 }
5279 }
5280}
5281# endif /* FEAT_LISTCMDS */
5282
5283#endif /* FEAT_WINDOWS */
5284
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005285static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005286
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287/*
5288 * do_modelines() - process mode lines for the current file
5289 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005290 * "flags" can be:
5291 * OPT_WINONLY only set options local to window
5292 * OPT_NOWIN don't set options local to window
5293 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 * Returns immediately if the "ml" option isn't set.
5295 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005297do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005299 linenr_T lnum;
5300 int nmlines;
5301 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5304 return;
5305
5306 /* Disallow recursive entry here. Can happen when executing a modeline
5307 * triggers an autocommand, which reloads modelines with a ":do". */
5308 if (entered)
5309 return;
5310
5311 ++entered;
5312 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5313 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005314 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 nmlines = 0;
5316
5317 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5318 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005319 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 nmlines = 0;
5321 --entered;
5322}
5323
5324#include "version.h" /* for version number */
5325
5326/*
5327 * chk_modeline() - check a single line for a mode string
5328 * Return FAIL if an error encountered.
5329 */
5330 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005331chk_modeline(
5332 linenr_T lnum,
5333 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334{
5335 char_u *s;
5336 char_u *e;
5337 char_u *linecopy; /* local copy of any modeline found */
5338 int prev;
5339 int vers;
5340 int end;
5341 int retval = OK;
5342 char_u *save_sourcing_name;
5343 linenr_T save_sourcing_lnum;
5344#ifdef FEAT_EVAL
5345 scid_T save_SID;
5346#endif
5347
5348 prev = -1;
5349 for (s = ml_get(lnum); *s != NUL; ++s)
5350 {
5351 if (prev == -1 || vim_isspace(prev))
5352 {
5353 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5354 || STRNCMP(s, "vi:", (size_t)3) == 0)
5355 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005356 /* Accept both "vim" and "Vim". */
5357 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
5359 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5360 e = s + 4;
5361 else
5362 e = s + 3;
5363 vers = getdigits(&e);
5364 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005365 && (s[0] != 'V'
5366 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 && (s[3] == ':'
5368 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5369 || (VIM_VERSION_100 < vers && s[3] == '<')
5370 || (VIM_VERSION_100 > vers && s[3] == '>')
5371 || (VIM_VERSION_100 == vers && s[3] == '=')))
5372 break;
5373 }
5374 }
5375 prev = *s;
5376 }
5377
5378 if (*s)
5379 {
5380 do /* skip over "ex:", "vi:" or "vim:" */
5381 ++s;
5382 while (s[-1] != ':');
5383
5384 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5385 if (linecopy == NULL)
5386 return FAIL;
5387
5388 save_sourcing_lnum = sourcing_lnum;
5389 save_sourcing_name = sourcing_name;
5390 sourcing_lnum = lnum; /* prepare for emsg() */
5391 sourcing_name = (char_u *)"modelines";
5392
5393 end = FALSE;
5394 while (end == FALSE)
5395 {
5396 s = skipwhite(s);
5397 if (*s == NUL)
5398 break;
5399
5400 /*
5401 * Find end of set command: ':' or end of line.
5402 * Skip over "\:", replacing it with ":".
5403 */
5404 for (e = s; *e != ':' && *e != NUL; ++e)
5405 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005406 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 if (*e == NUL)
5408 end = TRUE;
5409
5410 /*
5411 * If there is a "set" command, require a terminating ':' and
5412 * ignore the stuff after the ':'.
5413 * "vi:set opt opt opt: foo" -- foo not interpreted
5414 * "vi:opt opt opt: foo" -- foo interpreted
5415 * Accept "se" for compatibility with Elvis.
5416 */
5417 if (STRNCMP(s, "set ", (size_t)4) == 0
5418 || STRNCMP(s, "se ", (size_t)3) == 0)
5419 {
5420 if (*e != ':') /* no terminating ':'? */
5421 break;
5422 end = TRUE;
5423 s = vim_strchr(s, ' ') + 1;
5424 }
5425 *e = NUL; /* truncate the set command */
5426
5427 if (*s != NUL) /* skip over an empty "::" */
5428 {
5429#ifdef FEAT_EVAL
5430 save_SID = current_SID;
5431 current_SID = SID_MODELINE;
5432#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005433 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434#ifdef FEAT_EVAL
5435 current_SID = save_SID;
5436#endif
5437 if (retval == FAIL) /* stop if error found */
5438 break;
5439 }
5440 s = e + 1; /* advance to next part */
5441 }
5442
5443 sourcing_lnum = save_sourcing_lnum;
5444 sourcing_name = save_sourcing_name;
5445
5446 vim_free(linecopy);
5447 }
5448 return retval;
5449}
5450
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005451#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005453read_viminfo_bufferlist(
5454 vir_T *virp,
5455 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456{
5457 char_u *tab;
5458 linenr_T lnum;
5459 colnr_T col;
5460 buf_T *buf;
5461 char_u *sfname;
5462 char_u *xline;
5463
5464 /* Handle long line and escaped characters. */
5465 xline = viminfo_readstring(virp, 1, FALSE);
5466
5467 /* don't read in if there are files on the command-line or if writing: */
5468 if (xline != NULL && !writing && ARGCOUNT == 0
5469 && find_viminfo_parameter('%') != NULL)
5470 {
5471 /* Format is: <fname> Tab <lnum> Tab <col>.
5472 * Watch out for a Tab in the file name, work from the end. */
5473 lnum = 0;
5474 col = 0;
5475 tab = vim_strrchr(xline, '\t');
5476 if (tab != NULL)
5477 {
5478 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005479 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 tab = vim_strrchr(xline, '\t');
5481 if (tab != NULL)
5482 {
5483 *tab++ = '\0';
5484 lnum = atol((char *)tab);
5485 }
5486 }
5487
5488 /* Expand "~/" in the file name at "line + 1" to a full path.
5489 * Then try shortening it by comparing with the current directory */
5490 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005491 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492
5493 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5494 if (buf != NULL) /* just in case... */
5495 {
5496 buf->b_last_cursor.lnum = lnum;
5497 buf->b_last_cursor.col = col;
5498 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5499 }
5500 }
5501 vim_free(xline);
5502
5503 return viminfo_readline(virp);
5504}
5505
5506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005507write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508{
5509 buf_T *buf;
5510#ifdef FEAT_WINDOWS
5511 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005512 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513#endif
5514 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005515 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
5517 if (find_viminfo_parameter('%') == NULL)
5518 return;
5519
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005520 /* Without a number -1 is returned: do all buffers. */
5521 max_buffers = get_viminfo_parameter('%');
5522
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005524#define LINE_BUF_LEN (MAXPATHL + 40)
5525 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 if (line == NULL)
5527 return;
5528
5529#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005530 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 set_last_cursor(win);
5532#else
5533 set_last_cursor(curwin);
5534#endif
5535
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005536 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar29323592016-07-24 22:04:11 +02005537 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005538 {
5539 if (buf->b_fname == NULL
5540 || !buf->b_p_bl
5541#ifdef FEAT_QUICKFIX
5542 || bt_quickfix(buf)
5543#endif
5544 || removable(buf->b_ffname))
5545 continue;
5546
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005547 if (max_buffers-- == 0)
5548 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 putc('%', fp);
5550 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005551 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 (long)buf->b_last_cursor.lnum,
5553 buf->b_last_cursor.col);
5554 viminfo_writestring(fp, line);
5555 }
5556 vim_free(line);
5557}
5558#endif
5559
5560
5561/*
5562 * Return special buffer name.
5563 * Returns NULL when the buffer has a normal file name.
5564 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005565 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005566buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567{
5568#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5569 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005570 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005571 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005572 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005573
5574 /*
5575 * For location list window, w_llist_ref points to the location list.
5576 * For quickfix window, w_llist_ref is NULL.
5577 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005578 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005579 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005580 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005581 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583#endif
5584#ifdef FEAT_QUICKFIX
5585 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5586 * contains the name as specified by the user */
5587 if (bt_nofile(buf))
5588 {
5589 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005590 return buf->b_sfname;
5591 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 }
5593#endif
5594 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005595 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 return NULL;
5597}
5598
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005599#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5600 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5601 || defined(PROTO)
5602/*
5603 * Find a window for buffer "buf".
5604 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5605 * If not found FAIL is returned.
5606 */
5607 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005608find_win_for_buf(
5609 buf_T *buf,
5610 win_T **wp,
5611 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005612{
5613 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5614 if ((*wp)->w_buffer == buf)
5615 goto win_found;
5616 return FAIL;
5617win_found:
5618 return OK;
5619}
5620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621
5622#if defined(FEAT_SIGNS) || defined(PROTO)
5623/*
5624 * Insert the sign into the signlist.
5625 */
5626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005627insert_sign(
5628 buf_T *buf, /* buffer to store sign in */
5629 signlist_T *prev, /* previous sign entry */
5630 signlist_T *next, /* next sign entry */
5631 int id, /* sign ID */
5632 linenr_T lnum, /* line number which gets the mark */
5633 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 signlist_T *newsign;
5636
5637 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5638 if (newsign != NULL)
5639 {
5640 newsign->id = id;
5641 newsign->lnum = lnum;
5642 newsign->typenr = typenr;
5643 newsign->next = next;
5644#ifdef FEAT_NETBEANS_INTG
5645 newsign->prev = prev;
5646 if (next != NULL)
5647 next->prev = newsign;
5648#endif
5649
5650 if (prev == NULL)
5651 {
5652 /* When adding first sign need to redraw the windows to create the
5653 * column for signs. */
5654 if (buf->b_signlist == NULL)
5655 {
5656 redraw_buf_later(buf, NOT_VALID);
5657 changed_cline_bef_curs();
5658 }
5659
5660 /* first sign in signlist */
5661 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005662#ifdef FEAT_NETBEANS_INTG
5663 if (netbeans_active())
5664 buf->b_has_sign_column = TRUE;
5665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666 }
5667 else
5668 prev->next = newsign;
5669 }
5670}
5671
5672/*
5673 * Add the sign into the signlist. Find the right spot to do it though.
5674 */
5675 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005676buf_addsign(
5677 buf_T *buf, /* buffer to store sign in */
5678 int id, /* sign ID */
5679 linenr_T lnum, /* line number which gets the mark */
5680 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681{
5682 signlist_T *sign; /* a sign in the signlist */
5683 signlist_T *prev; /* the previous sign */
5684
5685 prev = NULL;
5686 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5687 {
5688 if (lnum == sign->lnum && id == sign->id)
5689 {
5690 sign->typenr = typenr;
5691 return;
5692 }
5693 else if (
5694#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5695 id < 0 &&
5696#endif
5697 lnum < sign->lnum)
5698 {
5699#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5700 /* XXX - GRP: Is this because of sign slide problem? Or is it
5701 * really needed? Or is it because we allow multiple signs per
5702 * line? If so, should I add that feature to FEAT_SIGNS?
5703 */
5704 while (prev != NULL && prev->lnum == lnum)
5705 prev = prev->prev;
5706 if (prev == NULL)
5707 sign = buf->b_signlist;
5708 else
5709 sign = prev->next;
5710#endif
5711 insert_sign(buf, prev, sign, id, lnum, typenr);
5712 return;
5713 }
5714 prev = sign;
5715 }
5716#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5717 /* XXX - GRP: See previous comment */
5718 while (prev != NULL && prev->lnum == lnum)
5719 prev = prev->prev;
5720 if (prev == NULL)
5721 sign = buf->b_signlist;
5722 else
5723 sign = prev->next;
5724#endif
5725 insert_sign(buf, prev, sign, id, lnum, typenr);
5726
5727 return;
5728}
5729
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005730/*
5731 * For an existing, placed sign "markId" change the type to "typenr".
5732 * Returns the line number of the sign, or zero if the sign is not found.
5733 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005734 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005735buf_change_sign_type(
5736 buf_T *buf, /* buffer to store sign in */
5737 int markId, /* sign ID */
5738 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739{
5740 signlist_T *sign; /* a sign in the signlist */
5741
5742 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5743 {
5744 if (sign->id == markId)
5745 {
5746 sign->typenr = typenr;
5747 return sign->lnum;
5748 }
5749 }
5750
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005751 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752}
5753
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005754 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005755buf_getsigntype(
5756 buf_T *buf,
5757 linenr_T lnum,
5758 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759{
5760 signlist_T *sign; /* a sign in a b_signlist */
5761
5762 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5763 if (sign->lnum == lnum
5764 && (type == SIGN_ANY
5765# ifdef FEAT_SIGN_ICONS
5766 || (type == SIGN_ICON
5767 && sign_get_image(sign->typenr) != NULL)
5768# endif
5769 || (type == SIGN_TEXT
5770 && sign_get_text(sign->typenr) != NULL)
5771 || (type == SIGN_LINEHL
5772 && sign_get_attr(sign->typenr, TRUE) != 0)))
5773 return sign->typenr;
5774 return 0;
5775}
5776
5777
5778 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005779buf_delsign(
5780 buf_T *buf, /* buffer sign is stored in */
5781 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782{
5783 signlist_T **lastp; /* pointer to pointer to current sign */
5784 signlist_T *sign; /* a sign in a b_signlist */
5785 signlist_T *next; /* the next sign in a b_signlist */
5786 linenr_T lnum; /* line number whose sign was deleted */
5787
5788 lastp = &buf->b_signlist;
5789 lnum = 0;
5790 for (sign = buf->b_signlist; sign != NULL; sign = next)
5791 {
5792 next = sign->next;
5793 if (sign->id == id)
5794 {
5795 *lastp = next;
5796#ifdef FEAT_NETBEANS_INTG
5797 if (next != NULL)
5798 next->prev = sign->prev;
5799#endif
5800 lnum = sign->lnum;
5801 vim_free(sign);
5802 break;
5803 }
5804 else
5805 lastp = &sign->next;
5806 }
5807
5808 /* When deleted the last sign need to redraw the windows to remove the
5809 * sign column. */
5810 if (buf->b_signlist == NULL)
5811 {
5812 redraw_buf_later(buf, NOT_VALID);
5813 changed_cline_bef_curs();
5814 }
5815
5816 return lnum;
5817}
5818
5819
5820/*
5821 * Find the line number of the sign with the requested id. If the sign does
5822 * not exist, return 0 as the line number. This will still let the correct file
5823 * get loaded.
5824 */
5825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826buf_findsign(
5827 buf_T *buf, /* buffer to store sign in */
5828 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829{
5830 signlist_T *sign; /* a sign in the signlist */
5831
5832 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5833 if (sign->id == id)
5834 return sign->lnum;
5835
5836 return 0;
5837}
5838
5839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005840buf_findsign_id(
5841 buf_T *buf, /* buffer whose sign we are searching for */
5842 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843{
5844 signlist_T *sign; /* a sign in the signlist */
5845
5846 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5847 if (sign->lnum == lnum)
5848 return sign->id;
5849
5850 return 0;
5851}
5852
5853
5854# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5855/* see if a given type of sign exists on a specific line */
5856 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005857buf_findsigntype_id(
5858 buf_T *buf, /* buffer whose sign we are searching for */
5859 linenr_T lnum, /* line number of sign */
5860 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861{
5862 signlist_T *sign; /* a sign in the signlist */
5863
5864 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5865 if (sign->lnum == lnum && sign->typenr == typenr)
5866 return sign->id;
5867
5868 return 0;
5869}
5870
5871
5872# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5873/* return the number of icons on the given line */
5874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005875buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876{
5877 signlist_T *sign; /* a sign in the signlist */
5878 int count = 0;
5879
5880 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5881 if (sign->lnum == lnum)
5882 if (sign_get_image(sign->typenr) != NULL)
5883 count++;
5884
5885 return count;
5886}
5887# endif /* FEAT_SIGN_ICONS */
5888# endif /* FEAT_NETBEANS_INTG */
5889
5890
5891/*
5892 * Delete signs in buffer "buf".
5893 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005895buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896{
5897 signlist_T *next;
5898
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005899 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005900 * sign column. Not when curwin is NULL (this means we're exiting). */
5901 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005902 {
5903 redraw_buf_later(buf, NOT_VALID);
5904 changed_cline_bef_curs();
5905 }
5906
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907 while (buf->b_signlist != NULL)
5908 {
5909 next = buf->b_signlist->next;
5910 vim_free(buf->b_signlist);
5911 buf->b_signlist = next;
5912 }
5913}
5914
5915/*
5916 * Delete all signs in all buffers.
5917 */
5918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005919buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 buf_T *buf; /* buffer we are checking for signs */
5922
Bram Moolenaar29323592016-07-24 22:04:11 +02005923 FOR_ALL_BUFFERS(buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926}
5927
5928/*
5929 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5930 */
5931 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005932sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933{
5934 buf_T *buf;
5935 signlist_T *p;
5936 char lbuf[BUFSIZ];
5937
5938 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5939 msg_putchar('\n');
5940 if (rbuf == NULL)
5941 buf = firstbuf;
5942 else
5943 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005944 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {
5946 if (buf->b_signlist != NULL)
5947 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005948 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5950 msg_putchar('\n');
5951 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005952 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005954 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5956 MSG_PUTS(lbuf);
5957 msg_putchar('\n');
5958 }
5959 if (rbuf != NULL)
5960 break;
5961 buf = buf->b_next;
5962 }
5963}
5964
5965/*
5966 * Adjust a placed sign for inserted/deleted lines.
5967 */
5968 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005969sign_mark_adjust(
5970 linenr_T line1,
5971 linenr_T line2,
5972 long amount,
5973 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974{
5975 signlist_T *sign; /* a sign in a b_signlist */
5976
5977 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5978 {
5979 if (sign->lnum >= line1 && sign->lnum <= line2)
5980 {
5981 if (amount == MAXLNUM)
5982 sign->lnum = line1;
5983 else
5984 sign->lnum += amount;
5985 }
5986 else if (sign->lnum > line2)
5987 sign->lnum += amount_after;
5988 }
5989}
5990#endif /* FEAT_SIGNS */
5991
5992/*
5993 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5994 */
5995 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005996set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997{
5998 if (on != curbuf->b_p_bl)
5999 {
6000 curbuf->b_p_bl = on;
6001#ifdef FEAT_AUTOCMD
6002 if (on)
6003 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
6004 else
6005 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
6006#endif
6007 }
6008}
6009
6010/*
6011 * Read the file for "buf" again and check if the contents changed.
6012 * Return TRUE if it changed or this could not be checked.
6013 */
6014 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006015buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006016{
6017 buf_T *newbuf;
6018 int differ = TRUE;
6019 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006021 exarg_T ea;
6022
6023 /* Allocate a buffer without putting it in the buffer list. */
6024 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
6025 if (newbuf == NULL)
6026 return TRUE;
6027
6028 /* Force the 'fileencoding' and 'fileformat' to be equal. */
6029 if (prep_exarg(&ea, buf) == FAIL)
6030 {
6031 wipe_buffer(newbuf, FALSE);
6032 return TRUE;
6033 }
6034
Bram Moolenaar071d4272004-06-13 20:20:40 +00006035 /* set curwin/curbuf to buf and save a few things */
6036 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006037
Bram Moolenaar4770d092006-01-12 23:22:24 +00006038 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 && readfile(buf->b_ffname, buf->b_fname,
6040 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
6041 &ea, READ_NEW | READ_DUMMY) == OK)
6042 {
6043 /* compare the two files line by line */
6044 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
6045 {
6046 differ = FALSE;
6047 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
6048 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
6049 {
6050 differ = TRUE;
6051 break;
6052 }
6053 }
6054 }
6055 vim_free(ea.cmd);
6056
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057 /* restore curwin/curbuf and a few other things */
6058 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006059
6060 if (curbuf != newbuf) /* safety check */
6061 wipe_buffer(newbuf, FALSE);
6062
6063 return differ;
6064}
6065
6066/*
6067 * Wipe out a buffer and decrement the last buffer number if it was used for
6068 * this buffer. Call this to wipe out a temp buffer that does not contain any
6069 * marks.
6070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006072wipe_buffer(
6073 buf_T *buf,
6074 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075{
6076 if (buf->b_fnum == top_file_num - 1)
6077 --top_file_num;
6078
6079#ifdef FEAT_AUTOCMD
6080 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006081 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01006083 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006084#ifdef FEAT_AUTOCMD
6085 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00006086 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00006087#endif
6088}