blob: 903a94aebf1fd0ead988e3bd372c1ed0d1c34f08 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar071d4272004-06-13 20:20:40 +000073/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020074 * Open current buffer, that is: open the memfile and read the file into
75 * memory.
76 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 */
78 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010079open_buffer(
80 int read_stdin, /* read file from stdin */
81 exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
82 int flags) /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000083{
84 int retval = OK;
85#ifdef FEAT_AUTOCMD
86 buf_T *old_curbuf;
87#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +010088#ifdef FEAT_SYN_HL
89 long old_tw = curbuf->b_p_tw;
90#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92 /*
93 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
94 * When re-entering the same buffer, it should not change, because the
95 * user may have reset the flag by hand.
96 */
97 if (readonlymode && curbuf->b_ffname != NULL
98 && (curbuf->b_flags & BF_NEVERLOADED))
99 curbuf->b_p_ro = TRUE;
100
Bram Moolenaar4770d092006-01-12 23:22:24 +0000101 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 {
103 /*
104 * There MUST be a memfile, otherwise we can't do anything
105 * If we can't create one for the current buffer, take another buffer
106 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100107 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
109 if (curbuf->b_ml.ml_mfp != NULL)
110 break;
111 /*
112 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000113 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 */
115 if (curbuf == NULL)
116 {
117 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
118 getout(2);
119 }
120 EMSG(_("E83: Cannot allocate buffer, using other one..."));
121 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100122#ifdef FEAT_SYN_HL
123 if (old_tw != curbuf->b_p_tw)
124 check_colorcolumn(curwin);
125#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126 return FAIL;
127 }
128
129#ifdef FEAT_AUTOCMD
130 /* The autocommands in readfile() may change the buffer, but only AFTER
131 * reading the file. */
132 old_curbuf = curbuf;
133 modified_was_set = FALSE;
134#endif
135
136 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100137 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000138
139 if (curbuf->b_ffname != NULL
140#ifdef FEAT_NETBEANS_INTG
141 && netbeansReadFile
142#endif
143 )
144 {
Bram Moolenaar426dd022016-03-15 15:09:29 +0100145 int old_msg_silent = msg_silent;
146
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147#ifdef FEAT_NETBEANS_INTG
148 int oldFire = netbeansFireChanges;
149
150 netbeansFireChanges = 0;
151#endif
Bram Moolenaar426dd022016-03-15 15:09:29 +0100152 if (shortmess(SHM_FILEINFO))
153 msg_silent = 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200155 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
156 flags | READ_NEW);
Bram Moolenaar426dd022016-03-15 15:09:29 +0100157 msg_silent = old_msg_silent;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158#ifdef FEAT_NETBEANS_INTG
159 netbeansFireChanges = oldFire;
160#endif
161 /* Help buffer is filtered. */
162 if (curbuf->b_help)
163 fix_help_buffer();
164 }
165 else if (read_stdin)
166 {
167 int save_bin = curbuf->b_p_bin;
168 linenr_T line_count;
169
170 /*
171 * First read the text in binary mode into the buffer.
172 * Then read from that same buffer and append at the end. This makes
173 * it possible to retry when 'fileformat' or 'fileencoding' was
174 * guessed wrong.
175 */
176 curbuf->b_p_bin = TRUE;
177 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200178 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
179 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180 curbuf->b_p_bin = save_bin;
181 if (retval == OK)
182 {
183 line_count = curbuf->b_ml.ml_line_count;
184 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200185 (linenr_T)0, (linenr_T)MAXLNUM, eap,
186 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000187 if (retval == OK)
188 {
189 /* Delete the binary lines. */
190 while (--line_count >= 0)
191 ml_delete((linenr_T)1, FALSE);
192 }
193 else
194 {
195 /* Delete the converted lines. */
196 while (curbuf->b_ml.ml_line_count > line_count)
197 ml_delete(line_count, FALSE);
198 }
199 /* Put the cursor on the first line. */
200 curwin->w_cursor.lnum = 1;
201 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000202
203 /* Set or reset 'modified' before executing autocommands, so that
204 * it can be changed there. */
205 if (!readonlymode && !bufempty())
206 changed();
207 else if (retval != FAIL)
208 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000209#ifdef FEAT_AUTOCMD
210# ifdef FEAT_EVAL
211 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
212 curbuf, &retval);
213# else
214 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
215# endif
216#endif
217 }
218 }
219
220 /* if first time loading this buffer, init b_chartab[] */
221 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100222 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100224#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100225 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100226#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228
229 /*
230 * Set/reset the Changed flag first, autocmds may change the buffer.
231 * Apply the automatic commands, before processing the modelines.
232 * So the modelines have priority over auto commands.
233 */
234 /* When reading stdin, the buffer contents always needs writing, so set
235 * the changed flag. Unless in readonly mode: "ls | gview -".
236 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000237 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238#ifdef FEAT_AUTOCMD
239 || modified_was_set /* ":set modified" used in autocmd */
240# ifdef FEAT_EVAL
241 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
242# endif
243#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000244 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000246 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 unchanged(curbuf, FALSE);
248 save_file_ff(curbuf); /* keep this fileformat */
249
250 /* require "!" to overwrite the file, because it wasn't read completely */
251#ifdef FEAT_EVAL
252 if (aborting())
253#else
254 if (got_int)
255#endif
256 curbuf->b_flags |= BF_READERR;
257
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000258#ifdef FEAT_FOLDING
259 /* Need to update automatic folding. Do this before the autocommands,
260 * they may use the fold info. */
261 foldUpdateAll(curwin);
262#endif
263
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#ifdef FEAT_AUTOCMD
265 /* need to set w_topline, unless some autocommand already did that. */
266 if (!(curwin->w_valid & VALID_TOPLINE))
267 {
268 curwin->w_topline = 1;
269# ifdef FEAT_DIFF
270 curwin->w_topfill = 0;
271# endif
272 }
273# ifdef FEAT_EVAL
274 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
275# else
276 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
277# endif
278#endif
279
280 if (retval != FAIL)
281 {
282#ifdef FEAT_AUTOCMD
283 /*
284 * The autocommands may have changed the current buffer. Apply the
285 * modelines to the correct buffer, if it still exists and is loaded.
286 */
287 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
288 {
289 aco_save_T aco;
290
291 /* Go to the buffer that was opened. */
292 aucmd_prepbuf(&aco, old_curbuf);
293#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000294 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
296
297#ifdef FEAT_AUTOCMD
298# ifdef FEAT_EVAL
299 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
300 &retval);
301# else
302 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
303# endif
304
305 /* restore curwin/curbuf and a few other things */
306 aucmd_restbuf(&aco);
307 }
308#endif
309 }
310
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 return retval;
312}
313
314/*
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200315 * Store "buf" in "bufref" and set the free count.
316 */
317 void
318set_bufref(bufref_T *bufref, buf_T *buf)
319{
320 bufref->br_buf = buf;
321 bufref->br_buf_free_count = buf_free_count;
322}
323
324/*
325 * Return TRUE if "bufref->br_buf" points to a valid buffer.
326 * Only goes through the buffer list if buf_free_count changed.
327 */
328 int
329bufref_valid(bufref_T *bufref)
330{
331 return bufref->br_buf_free_count == buf_free_count
332 ? TRUE : buf_valid(bufref->br_buf);
333}
334
335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200337 * This can be slow if there are many buffers, prefer using bufref_valid().
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 */
339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100340buf_valid(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000341{
342 buf_T *bp;
343
Bram Moolenaar82404332016-07-10 17:00:38 +0200344 /* Assume that we more often have a recent buffer, start with the last
345 * one. */
346 for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347 if (bp == buf)
348 return TRUE;
349 return FALSE;
350}
351
352/*
353 * Close the link to a buffer.
354 * "action" is used when there is no longer a window for the buffer.
355 * It can be:
356 * 0 buffer becomes hidden
357 * DOBUF_UNLOAD buffer is unloaded
358 * DOBUF_DELETE buffer is unloaded and removed from buffer list
359 * DOBUF_WIPE buffer is unloaded and really deleted
360 * When doing all but the first one on the current buffer, the caller should
361 * get a new buffer very soon!
362 *
363 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100364 *
365 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
366 * cause there to be only one window with this buffer. e.g. when ":quit" is
367 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 */
369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100370close_buffer(
371 win_T *win, /* if not NULL, set b_last_cursor */
372 buf_T *buf,
373 int action,
374 int abort_if_last UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000375{
376#ifdef FEAT_AUTOCMD
377 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100378 int nwindows;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200379 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#endif
381 int unload_buf = (action != 0);
382 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
383 int wipe_buf = (action == DOBUF_WIPE);
384
385#ifdef FEAT_QUICKFIX
386 /*
387 * Force unloading or deleting when 'bufhidden' says so.
388 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
389 * "hide" (otherwise we could never free or delete a buffer).
390 */
391 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
392 {
393 del_buf = TRUE;
394 unload_buf = TRUE;
395 }
396 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
397 {
398 del_buf = TRUE;
399 unload_buf = TRUE;
400 wipe_buf = TRUE;
401 }
402 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
403 unload_buf = TRUE;
404#endif
405
Bram Moolenaar3be85852014-06-12 14:01:31 +0200406 if (win != NULL
407#ifdef FEAT_WINDOWS
408 && win_valid(win) /* in case autocommands closed the window */
409#endif
410 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 {
412 /* Set b_last_cursor when closing the last window for the buffer.
413 * Remember the last cursor position and window options of the buffer.
414 * This used to be only for the current window, but then options like
415 * 'foldmethod' may be lost with a ":only" command. */
416 if (buf->b_nwindows == 1)
417 set_last_cursor(win);
418 buflist_setfpos(buf, win,
419 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
420 win->w_cursor.col, TRUE);
421 }
422
423#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200424 set_bufref(&bufref, buf);
425
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 /* When the buffer is no longer in a window, trigger BufWinLeave */
427 if (buf->b_nwindows == 1)
428 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200429 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200430 if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
431 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200432 && !bufref_valid(&bufref))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100433 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200434 /* Autocommands deleted the buffer. */
435aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100436 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100438 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200439 buf->b_closing = FALSE;
440 if (abort_if_last && one_window())
441 /* Autocommands made this the only window. */
442 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443
444 /* When the buffer becomes hidden, but is not unloaded, trigger
445 * BufHidden */
446 if (!unload_buf)
447 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200448 buf->b_closing = TRUE;
Bram Moolenaar82404332016-07-10 17:00:38 +0200449 if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
450 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200451 && !bufref_valid(&bufref))
Bram Moolenaar362ce482012-06-06 19:02:45 +0200452 /* Autocommands deleted the buffer. */
453 goto aucmd_abort;
454 buf->b_closing = FALSE;
455 if (abort_if_last && one_window())
456 /* Autocommands made this the only window. */
457 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 }
459# ifdef FEAT_EVAL
460 if (aborting()) /* autocmds may abort script processing */
461 return;
462# endif
463 }
464 nwindows = buf->b_nwindows;
465#endif
466
467 /* decrease the link count from windows (unless not in any window) */
468 if (buf->b_nwindows > 0)
469 --buf->b_nwindows;
470
471 /* Return when a window is displaying the buffer or when it's not
472 * unloaded. */
473 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000474 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475
476 /* Always remove the buffer when there is no file name. */
477 if (buf->b_ffname == NULL)
478 del_buf = TRUE;
479
480 /*
481 * Free all things allocated for this buffer.
482 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
483 */
484#ifdef FEAT_AUTOCMD
485 /* Remember if we are closing the current buffer. Restore the number of
486 * windows, so that autocommands in buf_freeall() don't get confused. */
487 is_curbuf = (buf == curbuf);
488 buf->b_nwindows = nwindows;
489#endif
490
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200491 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000492
493#ifdef FEAT_AUTOCMD
494 /* Autocommands may have deleted the buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200495 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 return;
497# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000498 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 return;
500# endif
501
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 /*
503 * It's possible that autocommands change curbuf to the one being deleted.
504 * This might cause the previous curbuf to be deleted unexpectedly. But
505 * in some cases it's OK to delete the curbuf, because a new one is
506 * obtained anyway. Therefore only return if curbuf changed to the
507 * deleted buffer.
508 */
509 if (buf == curbuf && !is_curbuf)
510 return;
Bram Moolenaar30445cb2016-07-09 15:21:02 +0200511
512 if (
513#ifdef FEAT_WINDOWS
514 win_valid(win) &&
515#else
516 win != NULL &&
517#endif
518 win->w_buffer == buf)
519 win->w_buffer = NULL; /* make sure we don't use the buffer now */
520
521 /* Autocommands may have opened or closed windows for this buffer.
522 * Decrement the count for the close we do here. */
523 if (buf->b_nwindows > 0)
524 --buf->b_nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525#endif
526
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000527 /* Change directories when the 'acd' option is set. */
528 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529
530 /*
531 * Remove the buffer from the list.
532 */
533 if (wipe_buf)
534 {
535#ifdef FEAT_SUN_WORKSHOP
536 if (usingSunWorkShop)
537 workshop_file_closed_lineno((char *)buf->b_ffname,
538 (int)buf->b_last_cursor.lnum);
539#endif
540 vim_free(buf->b_ffname);
541 vim_free(buf->b_sfname);
542 if (buf->b_prev == NULL)
543 firstbuf = buf->b_next;
544 else
545 buf->b_prev->b_next = buf->b_next;
546 if (buf->b_next == NULL)
547 lastbuf = buf->b_prev;
548 else
549 buf->b_next->b_prev = buf->b_prev;
550 free_buffer(buf);
551 }
552 else
553 {
554 if (del_buf)
555 {
556 /* Free all internal variables and reset option values, to make
557 * ":bdel" compatible with Vim 5.7. */
558 free_buffer_stuff(buf, TRUE);
559
560 /* Make it look like a new buffer. */
561 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
562
563 /* Init the options when loaded again. */
564 buf->b_p_initialized = FALSE;
565 }
566 buf_clear_file(buf);
567 if (del_buf)
568 buf->b_p_bl = FALSE;
569 }
570}
571
572/*
573 * Make buffer not contain a file.
574 */
575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100576buf_clear_file(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577{
578 buf->b_ml.ml_line_count = 1;
579 unchanged(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 buf->b_p_eol = TRUE;
582 buf->b_start_eol = TRUE;
583#ifdef FEAT_MBYTE
584 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000585 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586#endif
587 buf->b_ml.ml_mfp = NULL;
588 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
589#ifdef FEAT_NETBEANS_INTG
590 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#endif
592}
593
594/*
595 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200596 * the file. flags:
597 * BFA_DEL buffer is going to be deleted
598 * BFA_WIPE buffer is going to be wiped out
599 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100602buf_freeall(buf_T *buf, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603{
604#ifdef FEAT_AUTOCMD
605 int is_curbuf = (buf == curbuf);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200606 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
Bram Moolenaar362ce482012-06-06 19:02:45 +0200608 buf->b_closing = TRUE;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200609 set_bufref(&bufref, buf);
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200610 if (buf->b_ml.ml_mfp != NULL)
611 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200612 if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
613 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200614 && !bufref_valid(&bufref))
615 /* autocommands deleted the buffer */
Bram Moolenaarc67e8922016-05-24 16:07:40 +0200616 return;
617 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200618 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200620 if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
621 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200622 && !bufref_valid(&bufref))
623 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 return;
625 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200626 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {
Bram Moolenaar82404332016-07-10 17:00:38 +0200628 if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
629 FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200630 && !bufref_valid(&bufref))
631 /* autocommands deleted the buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632 return;
633 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200634 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000636 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 return;
638# endif
639
640 /*
641 * It's possible that autocommands change curbuf to the one being deleted.
642 * This might cause curbuf to be deleted unexpectedly. But in some cases
643 * it's OK to delete the curbuf, because a new one is obtained anyway.
644 * Therefore only return if curbuf changed to the deleted buffer.
645 */
646 if (buf == curbuf && !is_curbuf)
647 return;
648#endif
649#ifdef FEAT_DIFF
650 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
651#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200652#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100653 /* Remove any ownsyntax, unless exiting. */
654 if (firstwin != NULL && curwin->w_buffer == buf)
655 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200656#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000657
658#ifdef FEAT_FOLDING
659 /* No folds in an empty buffer. */
660# ifdef FEAT_WINDOWS
661 {
662 win_T *win;
663 tabpage_T *tp;
664
665 FOR_ALL_TAB_WINDOWS(tp, win)
666 if (win->w_buffer == buf)
667 clearFolding(win);
668 }
669# else
670 if (curwin->w_buffer == buf)
671 clearFolding(curwin);
672# endif
673#endif
674
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675#ifdef FEAT_TCL
676 tcl_buffer_free(buf);
677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 ml_close(buf, TRUE); /* close and delete the memline/memfile */
679 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200680 if ((flags & BFA_KEEP_UNDO) == 0)
681 {
682 u_blockfree(buf); /* free the memory allocated for undo */
683 u_clearall(buf); /* reset all undo information */
684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000685#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200686 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000688 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000689}
690
691/*
692 * Free a buffer structure and the things it contains related to the buffer
693 * itself (not the file, that must have been done already).
694 */
695 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100696free_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697{
Bram Moolenaarb25f9a92016-07-10 18:21:50 +0200698 ++buf_free_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000699 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200700#ifdef FEAT_EVAL
701 unref_var_dict(buf->b_vars);
702#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200703#ifdef FEAT_LUA
704 lua_buffer_free(buf);
705#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000706#ifdef FEAT_MZSCHEME
707 mzscheme_buffer_free(buf);
708#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#ifdef FEAT_PERL
710 perl_buf_free(buf);
711#endif
712#ifdef FEAT_PYTHON
713 python_buffer_free(buf);
714#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200715#ifdef FEAT_PYTHON3
716 python3_buffer_free(buf);
717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718#ifdef FEAT_RUBY
719 ruby_buffer_free(buf);
720#endif
Bram Moolenaare0f76d02016-05-09 20:38:53 +0200721#ifdef FEAT_JOB_CHANNEL
722 channel_buffer_free(buf);
723#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000724#ifdef FEAT_AUTOCMD
725 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200726 if (autocmd_busy)
727 {
728 /* Do not free the buffer structure while autocommands are executing,
729 * it's still needed. Free it when autocmd_busy is reset. */
730 buf->b_next = au_pending_free_buf;
731 au_pending_free_buf = buf;
732 }
733 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000734#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200735 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736}
737
738/*
739 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
740 */
741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100742free_buffer_stuff(
743 buf_T *buf,
744 int free_options) /* free options as well */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745{
746 if (free_options)
747 {
748 clear_wininfo(buf); /* including window-local options */
749 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200750#ifdef FEAT_SPELL
751 ga_clear(&buf->b_s.b_langp);
752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753 }
754#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200755 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
756 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000757#endif
758#ifdef FEAT_USR_CMDS
759 uc_clear(&buf->b_ucmds); /* clear local user commands */
760#endif
761#ifdef FEAT_SIGNS
762 buf_delete_signs(buf); /* delete any signs */
763#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000764#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200765 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000766#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767#ifdef FEAT_LOCALMAP
768 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
769 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
770#endif
771#ifdef FEAT_MBYTE
772 vim_free(buf->b_start_fenc);
773 buf->b_start_fenc = NULL;
774#endif
775}
776
777/*
778 * Free the b_wininfo list for buffer "buf".
779 */
780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100781clear_wininfo(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782{
783 wininfo_T *wip;
784
785 while (buf->b_wininfo != NULL)
786 {
787 wip = buf->b_wininfo;
788 buf->b_wininfo = wip->wi_next;
789 if (wip->wi_optset)
790 {
791 clear_winopt(&wip->wi_opt);
792#ifdef FEAT_FOLDING
793 deleteFoldRecurse(&wip->wi_folds);
794#endif
795 }
796 vim_free(wip);
797 }
798}
799
800#if defined(FEAT_LISTCMDS) || defined(PROTO)
801/*
802 * Go to another buffer. Handles the result of the ATTENTION dialog.
803 */
804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100805goto_buffer(
806 exarg_T *eap,
807 int start,
808 int dir,
809 int count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000811# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 buf_T *old_curbuf = curbuf;
813
814 swap_exists_action = SEA_DIALOG;
815# endif
816 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
817 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000818# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
820 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000821# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
822 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000823
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000824 /* Reset the error/interrupt/exception state here so that
825 * aborting() returns FALSE when closing a window. */
826 enter_cleanup(&cs);
827# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000828
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000829 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000830 win_close(curwin, TRUE);
831 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000832 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000833
834# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
835 /* Restore the error/interrupt/exception state if not discarded by a
836 * new aborting error, interrupt, or uncaught exception. */
837 leave_cleanup(&cs);
838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 else
841 handle_swap_exists(old_curbuf);
842# endif
843}
844#endif
845
Bram Moolenaare64ac772005-12-07 20:54:59 +0000846#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847/*
848 * Handle the situation of swap_exists_action being set.
849 * It is allowed for "old_curbuf" to be NULL or invalid.
850 */
851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100852handle_swap_exists(buf_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000854# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
855 cleanup_T cs;
856# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100857#ifdef FEAT_SYN_HL
858 long old_tw = curbuf->b_p_tw;
859#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000860
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 if (swap_exists_action == SEA_QUIT)
862 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000863# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
864 /* Reset the error/interrupt/exception state here so that
865 * aborting() returns FALSE when closing a buffer. */
866 enter_cleanup(&cs);
867# endif
868
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 /* User selected Quit at ATTENTION prompt. Go back to previous
870 * buffer. If that buffer is gone or the same as the current one,
871 * open a new, empty buffer. */
872 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000873 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100874 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000876 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000877 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100880#ifdef FEAT_SYN_HL
881 if (old_tw != curbuf->b_p_tw)
882 check_colorcolumn(curwin);
883#endif
884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000886
887# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
888 /* Restore the error/interrupt/exception state if not discarded by a
889 * new aborting error, interrupt, or uncaught exception. */
890 leave_cleanup(&cs);
891# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 }
893 else if (swap_exists_action == SEA_RECOVER)
894 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000895# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
896 /* Reset the error/interrupt/exception state here so that
897 * aborting() returns FALSE when closing a buffer. */
898 enter_cleanup(&cs);
899# endif
900
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 /* User selected Recover at ATTENTION prompt. */
902 msg_scroll = TRUE;
903 ml_recover();
904 MSG_PUTS("\n"); /* don't overwrite the last message */
905 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000906 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000907
908# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
909 /* Restore the error/interrupt/exception state if not discarded by a
910 * new aborting error, interrupt, or uncaught exception. */
911 leave_cleanup(&cs);
912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 }
914 swap_exists_action = SEA_NONE;
915}
916#endif
917
918#if defined(FEAT_LISTCMDS) || defined(PROTO)
919/*
920 * do_bufdel() - delete or unload buffer(s)
921 *
922 * addr_count == 0: ":bdel" - delete current buffer
923 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
924 * buffer "end_bnr", then any other arguments.
925 * addr_count == 2: ":N,N bdel" - delete buffers in range
926 *
927 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
928 * DOBUF_DEL (":bdel")
929 *
930 * Returns error message or NULL
931 */
932 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100933do_bufdel(
934 int command,
935 char_u *arg, /* pointer to extra arguments */
936 int addr_count,
937 int start_bnr, /* first buffer number in a range */
938 int end_bnr, /* buffer nr or last buffer nr in a range */
939 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940{
941 int do_current = 0; /* delete current buffer? */
942 int deleted = 0; /* number of buffers deleted */
943 char_u *errormsg = NULL; /* return value */
944 int bnr; /* buffer number */
945 char_u *p;
946
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (addr_count == 0)
948 {
949 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
950 }
951 else
952 {
953 if (addr_count == 2)
954 {
955 if (*arg) /* both range and argument is not allowed */
956 return (char_u *)_(e_trailing);
957 bnr = start_bnr;
958 }
959 else /* addr_count == 1 */
960 bnr = end_bnr;
961
962 for ( ;!got_int; ui_breakcheck())
963 {
964 /*
965 * delete the current buffer last, otherwise when the
966 * current buffer is deleted, the next buffer becomes
967 * the current one and will be loaded, which may then
968 * also be deleted, etc.
969 */
970 if (bnr == curbuf->b_fnum)
971 do_current = bnr;
972 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
973 forceit) == OK)
974 ++deleted;
975
976 /*
977 * find next buffer number to delete/unload
978 */
979 if (addr_count == 2)
980 {
981 if (++bnr > end_bnr)
982 break;
983 }
984 else /* addr_count == 1 */
985 {
986 arg = skipwhite(arg);
987 if (*arg == NUL)
988 break;
989 if (!VIM_ISDIGIT(*arg))
990 {
991 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100992 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
993 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 if (bnr < 0) /* failed */
995 break;
996 arg = p;
997 }
998 else
999 bnr = getdigits(&arg);
1000 }
1001 }
1002 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1003 FORWARD, do_current, forceit) == OK)
1004 ++deleted;
1005
1006 if (deleted == 0)
1007 {
1008 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001009 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001011 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001013 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 errormsg = IObuff;
1015 }
1016 else if (deleted >= p_report)
1017 {
1018 if (command == DOBUF_UNLOAD)
1019 {
1020 if (deleted == 1)
1021 MSG(_("1 buffer unloaded"));
1022 else
1023 smsg((char_u *)_("%d buffers unloaded"), deleted);
1024 }
1025 else if (command == DOBUF_DEL)
1026 {
1027 if (deleted == 1)
1028 MSG(_("1 buffer deleted"));
1029 else
1030 smsg((char_u *)_("%d buffers deleted"), deleted);
1031 }
1032 else
1033 {
1034 if (deleted == 1)
1035 MSG(_("1 buffer wiped out"));
1036 else
1037 smsg((char_u *)_("%d buffers wiped out"), deleted);
1038 }
1039 }
1040 }
1041
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042
1043 return errormsg;
1044}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001045#endif /* FEAT_LISTCMDS */
1046
1047#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1048 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001050static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001051
1052/*
1053 * Make the current buffer empty.
1054 * Used when it is wiped out and it's the last buffer.
1055 */
1056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001057empty_curbuf(
1058 int close_others,
1059 int forceit,
1060 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001061{
1062 int retval;
1063 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001064 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001065
1066 if (action == DOBUF_UNLOAD)
1067 {
1068 EMSG(_("E90: Cannot unload last buffer"));
1069 return FAIL;
1070 }
1071
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001072 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001073#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001074 if (close_others)
1075 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001076 close_windows(buf, TRUE);
1077#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001078
1079 setpcmark();
1080 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1081 forceit ? ECMD_FORCEIT : 0, curwin);
1082
1083 /*
1084 * do_ecmd() may create a new buffer, then we have to delete
1085 * the old one. But do_ecmd() may have done that already, check
1086 * if the buffer still exists.
1087 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001088 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001089 close_buffer(NULL, buf, action, FALSE);
1090 if (!close_others)
1091 need_fileinfo = FALSE;
1092 return retval;
1093}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094/*
1095 * Implementation of the commands for the buffer list.
1096 *
1097 * action == DOBUF_GOTO go to specified buffer
1098 * action == DOBUF_SPLIT split window and go to specified buffer
1099 * action == DOBUF_UNLOAD unload specified buffer(s)
1100 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1101 * action == DOBUF_WIPE delete specified buffer(s) really
1102 *
1103 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1104 * start == DOBUF_FIRST go to "count" buffer from first buffer
1105 * start == DOBUF_LAST go to "count" buffer from last buffer
1106 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1107 *
1108 * Return FAIL or OK.
1109 */
1110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001111do_buffer(
1112 int action,
1113 int start,
1114 int dir, /* FORWARD or BACKWARD */
1115 int count, /* buffer number or number of buffers */
1116 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117{
1118 buf_T *buf;
1119 buf_T *bp;
1120 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1121 || action == DOBUF_WIPE);
1122
1123 switch (start)
1124 {
1125 case DOBUF_FIRST: buf = firstbuf; break;
1126 case DOBUF_LAST: buf = lastbuf; break;
1127 default: buf = curbuf; break;
1128 }
1129 if (start == DOBUF_MOD) /* find next modified buffer */
1130 {
1131 while (count-- > 0)
1132 {
1133 do
1134 {
1135 buf = buf->b_next;
1136 if (buf == NULL)
1137 buf = firstbuf;
1138 }
1139 while (buf != curbuf && !bufIsChanged(buf));
1140 }
1141 if (!bufIsChanged(buf))
1142 {
1143 EMSG(_("E84: No modified buffer found"));
1144 return FAIL;
1145 }
1146 }
1147 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1148 {
1149 while (buf != NULL && buf->b_fnum != count)
1150 buf = buf->b_next;
1151 }
1152 else
1153 {
1154 bp = NULL;
1155 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1156 {
1157 /* remember the buffer where we start, we come back there when all
1158 * buffers are unlisted. */
1159 if (bp == NULL)
1160 bp = buf;
1161 if (dir == FORWARD)
1162 {
1163 buf = buf->b_next;
1164 if (buf == NULL)
1165 buf = firstbuf;
1166 }
1167 else
1168 {
1169 buf = buf->b_prev;
1170 if (buf == NULL)
1171 buf = lastbuf;
1172 }
1173 /* don't count unlisted buffers */
1174 if (unload || buf->b_p_bl)
1175 {
1176 --count;
1177 bp = NULL; /* use this buffer as new starting point */
1178 }
1179 if (bp == buf)
1180 {
1181 /* back where we started, didn't find anything. */
1182 EMSG(_("E85: There is no listed buffer"));
1183 return FAIL;
1184 }
1185 }
1186 }
1187
1188 if (buf == NULL) /* could not find it */
1189 {
1190 if (start == DOBUF_FIRST)
1191 {
1192 /* don't warn when deleting */
1193 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001194 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 }
1196 else if (dir == FORWARD)
1197 EMSG(_("E87: Cannot go beyond last buffer"));
1198 else
1199 EMSG(_("E88: Cannot go before first buffer"));
1200 return FAIL;
1201 }
1202
1203#ifdef FEAT_GUI
1204 need_mouse_correct = TRUE;
1205#endif
1206
1207#ifdef FEAT_LISTCMDS
1208 /*
1209 * delete buffer buf from memory and/or the list
1210 */
1211 if (unload)
1212 {
1213 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001214# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1215 bufref_T bufref;
1216
1217 set_bufref(&bufref, buf);
1218# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219
1220 /* When unloading or deleting a buffer that's already unloaded and
1221 * unlisted: fail silently. */
1222 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1223 return FAIL;
1224
1225 if (!forceit && bufIsChanged(buf))
1226 {
1227#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1228 if ((p_confirm || cmdmod.confirm) && p_write)
1229 {
1230 dialog_changed(buf, FALSE);
1231# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001232 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 /* Autocommand deleted buffer, oops! It's not changed
1234 * now. */
1235 return FAIL;
1236# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001237 /* If it's still changed fail silently, the dialog already
1238 * mentioned why it fails. */
1239 if (bufIsChanged(buf))
1240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#endif
1244 {
1245 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1246 buf->b_fnum);
1247 return FAIL;
1248 }
1249 }
1250
1251 /*
1252 * If deleting the last (listed) buffer, make it empty.
1253 * The last (listed) buffer cannot be unloaded.
1254 */
1255 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1256 if (bp->b_p_bl && bp != buf)
1257 break;
1258 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001259 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
1261#ifdef FEAT_WINDOWS
1262 /*
1263 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001264 * (unless it's the only window). Repeat this so long as we end up in
1265 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001267 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001268# ifdef FEAT_AUTOCMD
1269 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1270# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001271 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001272 {
1273 if (win_close(curwin, FALSE) == FAIL)
1274 break;
1275 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276#endif
1277
1278 /*
1279 * If the buffer to be deleted is not the current one, delete it here.
1280 */
1281 if (buf != curbuf)
1282 {
1283#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001284 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001285 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001287 if (buf->b_nwindows <= 0)
1288 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 return OK;
1290 }
1291
1292 /*
1293 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001294 * There should be another, otherwise it would have been handled
1295 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001296 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 * Then prefer the buffer we most recently visited.
1298 * Else try to find one that is loaded, after the current buffer,
1299 * then before the current buffer.
1300 * Finally use any buffer.
1301 */
1302 buf = NULL; /* selected buffer */
1303 bp = NULL; /* used when no loaded buffer found */
1304#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001305 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1306 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307# ifdef FEAT_JUMPLIST
1308 else
1309# endif
1310#endif
1311#ifdef FEAT_JUMPLIST
1312 if (curwin->w_jumplistlen > 0)
1313 {
1314 int jumpidx;
1315
1316 jumpidx = curwin->w_jumplistidx - 1;
1317 if (jumpidx < 0)
1318 jumpidx = curwin->w_jumplistlen - 1;
1319
1320 forward = jumpidx;
1321 while (jumpidx != curwin->w_jumplistidx)
1322 {
1323 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1324 if (buf != NULL)
1325 {
1326 if (buf == curbuf || !buf->b_p_bl)
1327 buf = NULL; /* skip current and unlisted bufs */
1328 else if (buf->b_ml.ml_mfp == NULL)
1329 {
1330 /* skip unloaded buf, but may keep it for later */
1331 if (bp == NULL)
1332 bp = buf;
1333 buf = NULL;
1334 }
1335 }
1336 if (buf != NULL) /* found a valid buffer: stop searching */
1337 break;
1338 /* advance to older entry in jump list */
1339 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1340 break;
1341 if (--jumpidx < 0)
1342 jumpidx = curwin->w_jumplistlen - 1;
1343 if (jumpidx == forward) /* List exhausted for sure */
1344 break;
1345 }
1346 }
1347#endif
1348
1349 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1350 {
1351 forward = TRUE;
1352 buf = curbuf->b_next;
1353 for (;;)
1354 {
1355 if (buf == NULL)
1356 {
1357 if (!forward) /* tried both directions */
1358 break;
1359 buf = curbuf->b_prev;
1360 forward = FALSE;
1361 continue;
1362 }
1363 /* in non-help buffer, try to skip help buffers, and vv */
1364 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1365 {
1366 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1367 break;
1368 if (bp == NULL) /* remember unloaded buf for later */
1369 bp = buf;
1370 }
1371 if (forward)
1372 buf = buf->b_next;
1373 else
1374 buf = buf->b_prev;
1375 }
1376 }
1377 if (buf == NULL) /* No loaded buffer, use unloaded one */
1378 buf = bp;
1379 if (buf == NULL) /* No loaded buffer, find listed one */
1380 {
1381 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1382 if (buf->b_p_bl && buf != curbuf)
1383 break;
1384 }
1385 if (buf == NULL) /* Still no buffer, just take one */
1386 {
1387 if (curbuf->b_next != NULL)
1388 buf = curbuf->b_next;
1389 else
1390 buf = curbuf->b_prev;
1391 }
1392 }
1393
Bram Moolenaara02471e2014-01-10 16:43:14 +01001394 if (buf == NULL)
1395 {
1396 /* Autocommands must have wiped out all other buffers. Only option
1397 * now is to make the current buffer empty. */
1398 return empty_curbuf(FALSE, forceit, action);
1399 }
1400
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 /*
1402 * make buf current buffer
1403 */
1404 if (action == DOBUF_SPLIT) /* split window first */
1405 {
1406# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001407 /* If 'switchbuf' contains "useopen": jump to first window containing
1408 * "buf" if one exists */
1409 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001410 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001411 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001412 * page containing "buf" if one exists */
1413 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 return OK;
1415 if (win_split(0, 0) == FAIL)
1416# endif
1417 return FAIL;
1418 }
1419#endif
1420
1421 /* go to current buffer - nothing to do */
1422 if (buf == curbuf)
1423 return OK;
1424
1425 /*
1426 * Check if the current buffer may be abandoned.
1427 */
1428 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1429 {
1430#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1431 if ((p_confirm || cmdmod.confirm) && p_write)
1432 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001433# ifdef FEAT_AUTOCMD
1434 bufref_T bufref;
1435
1436 set_bufref(&bufref, buf);
1437# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 dialog_changed(curbuf, FALSE);
1439# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001440 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 /* Autocommand deleted buffer, oops! */
1442 return FAIL;
1443# endif
1444 }
1445 if (bufIsChanged(curbuf))
1446#endif
1447 {
1448 EMSG(_(e_nowrtmsg));
1449 return FAIL;
1450 }
1451 }
1452
1453 /* Go to the other buffer. */
1454 set_curbuf(buf, action);
1455
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001456#if defined(FEAT_LISTCMDS) \
1457 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001459 {
1460 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462#endif
1463
1464#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1465 if (aborting()) /* autocmds may abort script processing */
1466 return FAIL;
1467#endif
1468
1469 return OK;
1470}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473/*
1474 * Set current buffer to "buf". Executes autocommands and closes current
1475 * buffer. "action" tells how to close the current buffer:
1476 * DOBUF_GOTO free or hide it
1477 * DOBUF_SPLIT nothing
1478 * DOBUF_UNLOAD unload it
1479 * DOBUF_DEL delete it
1480 * DOBUF_WIPE wipe it out
1481 */
1482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
1485 buf_T *prevbuf;
1486 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1487 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001488#ifdef FEAT_SYN_HL
1489 long old_tw = curbuf->b_p_tw;
1490#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001491 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492
1493 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001494 if (!cmdmod.keepalt)
1495 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001496 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 /* Don't restart Select mode after switching to another buffer. */
1499 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
1501 /* close_windows() or apply_autocmds() may change curbuf */
1502 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001503 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001506 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001508 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001510 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511# endif
1512#endif
1513 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001514#ifdef FEAT_SYN_HL
1515 if (prevbuf == curwin->w_buffer)
1516 reset_synblock(curwin);
1517#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518#ifdef FEAT_WINDOWS
1519 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001520 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521#endif
1522#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001523 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001525 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001527 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001528#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001529 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001530#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001531 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001532 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1534 unload ? action : (action == DOBUF_GOTO
1535 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001536 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001537#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001538 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001539 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001540 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001541#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 }
1544#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001545 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001546 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001547 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1548 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001549# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001550 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001551# endif
1552# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001553 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001554# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001555 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001557 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001559#ifdef FEAT_SYN_HL
1560 if (old_tw != curbuf->b_p_tw)
1561 check_colorcolumn(curwin);
1562#endif
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564}
1565
1566/*
1567 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001568 * Old curbuf must have been abandoned already! This also means "curbuf" may
1569 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 */
1571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001572enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573{
1574 /* Copy buffer and window local option values. Not for a help buffer. */
1575 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1576 if (!buf->b_help)
1577 get_winopts(buf);
1578#ifdef FEAT_FOLDING
1579 else
1580 /* Remove all folds in the window. */
1581 clearFolding(curwin);
1582 foldUpdateAll(curwin); /* update folds (later). */
1583#endif
1584
1585 /* Get the buffer in the current window. */
1586 curwin->w_buffer = buf;
1587 curbuf = buf;
1588 ++curbuf->b_nwindows;
1589
1590#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001591 if (curwin->w_p_diff)
1592 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001593#endif
1594
Bram Moolenaara971b822011-09-14 14:43:25 +02001595#ifdef FEAT_SYN_HL
1596 curwin->w_s = &(buf->b_s);
1597#endif
1598
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 /* Cursor on first line by default. */
1600 curwin->w_cursor.lnum = 1;
1601 curwin->w_cursor.col = 0;
1602#ifdef FEAT_VIRTUALEDIT
1603 curwin->w_cursor.coladd = 0;
1604#endif
1605 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001606#ifdef FEAT_AUTOCMD
1607 curwin->w_topline_was_set = FALSE;
1608#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001610 /* mark cursor position as being invalid */
1611 curwin->w_valid = 0;
1612
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /* Make sure the buffer is loaded. */
1614 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001615 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001616#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001617 /* If there is no filetype, allow for detecting one. Esp. useful for
1618 * ":ball" used in a autocommand. If there already is a filetype we
1619 * might prefer to keep it. */
1620 if (*curbuf->b_p_ft == NUL)
1621 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001622#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001623
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001624 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 else
1627 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001628 if (!msg_silent)
1629 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1631#ifdef FEAT_AUTOCMD
1632 curwin->w_topline = 1;
1633# ifdef FEAT_DIFF
1634 curwin->w_topfill = 0;
1635# endif
1636 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1637 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1638#endif
1639 }
1640
1641 /* If autocommands did not change the cursor position, restore cursor lnum
1642 * and possibly cursor col. */
1643 if (curwin->w_cursor.lnum == 1 && inindent(0))
1644 buflist_getfpos();
1645
1646 check_arg_idx(curwin); /* check for valid arg_idx */
1647#ifdef FEAT_TITLE
1648 maketitle();
1649#endif
1650#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001651 /* when autocmds didn't change it */
1652 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653#endif
1654 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1655
Bram Moolenaar009b2592004-10-24 19:18:58 +00001656#ifdef FEAT_NETBEANS_INTG
1657 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001658 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001659#endif
1660
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001661 /* Change directories when the 'acd' option is set. */
1662 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663
1664#ifdef FEAT_KEYMAP
1665 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001666 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001668#ifdef FEAT_SPELL
1669 /* May need to set the spell language. Can only do this after the buffer
1670 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001671 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1672 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001673#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001674#ifdef FEAT_VIMINFO
1675 curbuf->b_last_used = vim_time();
1676#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001677
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 redraw_later(NOT_VALID);
1679}
1680
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001681#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1682/*
1683 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001684 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001685 */
1686 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001687do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001688{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001689 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001690 && curbuf->b_ffname != NULL
1691 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001692 shorten_fnames(TRUE);
1693}
1694#endif
1695
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696/*
1697 * functions for dealing with the buffer list
1698 */
1699
1700/*
1701 * Add a file name to the buffer list. Return a pointer to the buffer.
1702 * If the same file name already exists return a pointer to that buffer.
1703 * If it does not exist, or if fname == NULL, a new entry is created.
1704 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1705 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1706 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001707 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001708 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1709 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 * This is the ONLY way to create a new buffer.
1711 */
1712static int top_file_num = 1; /* highest file number */
1713
1714 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001715buflist_new(
1716 char_u *ffname, /* full path of fname or relative */
1717 char_u *sfname, /* short fname or NULL */
1718 linenr_T lnum, /* preferred cursor line */
1719 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720{
1721 buf_T *buf;
1722#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001723 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724#endif
1725
1726 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1727
1728 /*
1729 * If file name already exists in the list, update the entry.
1730 */
1731#ifdef UNIX
1732 /* On Unix we can use inode numbers when the file exists. Works better
1733 * for hard links. */
1734 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1735 st.st_dev = (dev_T)-1;
1736#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001737 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738#ifdef UNIX
1739 buflist_findname_stat(ffname, &st)
1740#else
1741 buflist_findname(ffname)
1742#endif
1743 ) != NULL)
1744 {
1745 vim_free(ffname);
1746 if (lnum != 0)
1747 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001748
1749 if ((flags & BLN_NOOPT) == 0)
1750 /* copy the options now, if 'cpo' doesn't have 's' and not done
1751 * already */
1752 buf_copy_options(buf, 0);
1753
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1755 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001756#ifdef FEAT_AUTOCMD
1757 bufref_T bufref;
1758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 buf->b_p_bl = TRUE;
1760#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001761 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001763 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001764 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001765 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001766 return NULL;
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768#endif
1769 }
1770 return buf;
1771 }
1772
1773 /*
1774 * If the current buffer has no name and no contents, use the current
1775 * buffer. Otherwise: Need to allocate a new buffer structure.
1776 *
1777 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001778 * (A spell file buffer is allocated in spell.c, but that's not a normal
1779 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 */
1781 buf = NULL;
1782 if ((flags & BLN_CURBUF)
1783 && curbuf != NULL
1784 && curbuf->b_ffname == NULL
1785 && curbuf->b_nwindows <= 1
1786 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1787 {
1788 buf = curbuf;
1789#ifdef FEAT_AUTOCMD
1790 /* It's like this buffer is deleted. Watch out for autocommands that
1791 * change curbuf! If that happens, allocate a new buffer anyway. */
1792 if (curbuf->b_p_bl)
1793 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1794 if (buf == curbuf)
1795 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1796# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001797 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 return NULL;
1799# endif
1800#endif
1801#ifdef FEAT_QUICKFIX
1802# ifdef FEAT_AUTOCMD
1803 if (buf == curbuf)
1804# endif
1805 {
1806 /* Make sure 'bufhidden' and 'buftype' are empty */
1807 clear_string_option(&buf->b_p_bh);
1808 clear_string_option(&buf->b_p_bt);
1809 }
1810#endif
1811 }
1812 if (buf != curbuf || curbuf == NULL)
1813 {
1814 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1815 if (buf == NULL)
1816 {
1817 vim_free(ffname);
1818 return NULL;
1819 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001820#ifdef FEAT_EVAL
1821 /* init b: variables */
1822 buf->b_vars = dict_alloc();
1823 if (buf->b_vars == NULL)
1824 {
1825 vim_free(ffname);
1826 vim_free(buf);
1827 return NULL;
1828 }
1829 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 }
1832
1833 if (ffname != NULL)
1834 {
1835 buf->b_ffname = ffname;
1836 buf->b_sfname = vim_strsave(sfname);
1837 }
1838
1839 clear_wininfo(buf);
1840 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1841
1842 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1843 || buf->b_wininfo == NULL)
1844 {
1845 vim_free(buf->b_ffname);
1846 buf->b_ffname = NULL;
1847 vim_free(buf->b_sfname);
1848 buf->b_sfname = NULL;
1849 if (buf != curbuf)
1850 free_buffer(buf);
1851 return NULL;
1852 }
1853
1854 if (buf == curbuf)
1855 {
1856 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001857 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 if (buf != curbuf) /* autocommands deleted the buffer! */
1859 return NULL;
1860#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001861 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 return NULL;
1863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001865
1866 /* Init the options. */
1867 buf->b_p_initialized = FALSE;
1868 buf_copy_options(buf, BCO_ENTER);
1869
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#ifdef FEAT_KEYMAP
1871 /* need to reload lmaps and set b:keymap_name */
1872 curbuf->b_kmap_state |= KEYMAP_INIT;
1873#endif
1874 }
1875 else
1876 {
1877 /*
1878 * put new buffer at the end of the buffer list
1879 */
1880 buf->b_next = NULL;
1881 if (firstbuf == NULL) /* buffer list is empty */
1882 {
1883 buf->b_prev = NULL;
1884 firstbuf = buf;
1885 }
1886 else /* append new buffer at end of list */
1887 {
1888 lastbuf->b_next = buf;
1889 buf->b_prev = lastbuf;
1890 }
1891 lastbuf = buf;
1892
1893 buf->b_fnum = top_file_num++;
1894 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1895 {
1896 EMSG(_("W14: Warning: List of file names overflow"));
1897 if (emsg_silent == 0)
1898 {
1899 out_flush();
1900 ui_delay(3000L, TRUE); /* make sure it is noticed */
1901 }
1902 top_file_num = 1;
1903 }
1904
1905 /*
1906 * Always copy the options from the current buffer.
1907 */
1908 buf_copy_options(buf, BCO_ALWAYS);
1909 }
1910
1911 buf->b_wininfo->wi_fpos.lnum = lnum;
1912 buf->b_wininfo->wi_win = curwin;
1913
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001914#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001915 hash_init(&buf->b_s.b_keywtab);
1916 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917#endif
1918
1919 buf->b_fname = buf->b_sfname;
1920#ifdef UNIX
1921 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001922 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001925 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 buf->b_dev = st.st_dev;
1927 buf->b_ino = st.st_ino;
1928 }
1929#endif
1930 buf->b_u_synced = TRUE;
1931 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001932 if (flags & BLN_DUMMY)
1933 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 buf_clear_file(buf);
1935 clrallmarks(buf); /* clear marks */
1936 fmarks_check_names(buf); /* check file marks for this file */
1937 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1938#ifdef FEAT_AUTOCMD
1939 if (!(flags & BLN_DUMMY))
1940 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001941 bufref_T bufref;
1942
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001943 /* Tricky: these autocommands may change the buffer list. They could
1944 * also split the window with re-using the one empty buffer. This may
1945 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001946 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001947 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001948 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001949 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001950 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001951 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001952 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001953 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001954 return NULL;
1955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001957 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 return NULL;
1959# endif
1960 }
1961#endif
1962
1963 return buf;
1964}
1965
1966/*
1967 * Free the memory for the options of a buffer.
1968 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1969 * 'fileencoding'.
1970 */
1971 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001972free_buf_options(
1973 buf_T *buf,
1974 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975{
1976 if (free_p_ff)
1977 {
1978#ifdef FEAT_MBYTE
1979 clear_string_option(&buf->b_p_fenc);
1980#endif
1981 clear_string_option(&buf->b_p_ff);
1982#ifdef FEAT_QUICKFIX
1983 clear_string_option(&buf->b_p_bh);
1984 clear_string_option(&buf->b_p_bt);
1985#endif
1986 }
1987#ifdef FEAT_FIND_ID
1988 clear_string_option(&buf->b_p_def);
1989 clear_string_option(&buf->b_p_inc);
1990# ifdef FEAT_EVAL
1991 clear_string_option(&buf->b_p_inex);
1992# endif
1993#endif
1994#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1995 clear_string_option(&buf->b_p_inde);
1996 clear_string_option(&buf->b_p_indk);
1997#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001998#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1999 clear_string_option(&buf->b_p_bexpr);
2000#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002001#if defined(FEAT_CRYPT)
2002 clear_string_option(&buf->b_p_cm);
2003#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002004#if defined(FEAT_EVAL)
2005 clear_string_option(&buf->b_p_fex);
2006#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007#ifdef FEAT_CRYPT
2008 clear_string_option(&buf->b_p_key);
2009#endif
2010 clear_string_option(&buf->b_p_kp);
2011 clear_string_option(&buf->b_p_mps);
2012 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002013 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 clear_string_option(&buf->b_p_isk);
2015#ifdef FEAT_KEYMAP
2016 clear_string_option(&buf->b_p_keymap);
2017 ga_clear(&buf->b_kmap_ga);
2018#endif
2019#ifdef FEAT_COMMENTS
2020 clear_string_option(&buf->b_p_com);
2021#endif
2022#ifdef FEAT_FOLDING
2023 clear_string_option(&buf->b_p_cms);
2024#endif
2025 clear_string_option(&buf->b_p_nf);
2026#ifdef FEAT_SYN_HL
2027 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002028 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002029#endif
2030#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002031 clear_string_option(&buf->b_s.b_p_spc);
2032 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002033 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002034 buf->b_s.b_cap_prog = NULL;
2035 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002036#endif
2037#ifdef FEAT_SEARCHPATH
2038 clear_string_option(&buf->b_p_sua);
2039#endif
2040#ifdef FEAT_AUTOCMD
2041 clear_string_option(&buf->b_p_ft);
2042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043#ifdef FEAT_CINDENT
2044 clear_string_option(&buf->b_p_cink);
2045 clear_string_option(&buf->b_p_cino);
2046#endif
2047#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2048 clear_string_option(&buf->b_p_cinw);
2049#endif
2050#ifdef FEAT_INS_EXPAND
2051 clear_string_option(&buf->b_p_cpt);
2052#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002053#ifdef FEAT_COMPL_FUNC
2054 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002055 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057#ifdef FEAT_QUICKFIX
2058 clear_string_option(&buf->b_p_gp);
2059 clear_string_option(&buf->b_p_mp);
2060 clear_string_option(&buf->b_p_efm);
2061#endif
2062 clear_string_option(&buf->b_p_ep);
2063 clear_string_option(&buf->b_p_path);
2064 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002065 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066#ifdef FEAT_INS_EXPAND
2067 clear_string_option(&buf->b_p_dict);
2068 clear_string_option(&buf->b_p_tsr);
2069#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002070#ifdef FEAT_TEXTOBJ
2071 clear_string_option(&buf->b_p_qe);
2072#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002074 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002075#ifdef FEAT_LISP
2076 clear_string_option(&buf->b_p_lw);
2077#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002078 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079}
2080
2081/*
2082 * get alternate file n
2083 * set linenr to lnum or altfpos.lnum if lnum == 0
2084 * also set cursor column to altfpos.col if 'startofline' is not set.
2085 * if (options & GETF_SETMARK) call setpcmark()
2086 * if (options & GETF_ALT) we are jumping to an alternate file.
2087 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2088 *
2089 * return FAIL for failure, OK for success
2090 */
2091 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002092buflist_getfile(
2093 int n,
2094 linenr_T lnum,
2095 int options,
2096 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097{
2098 buf_T *buf;
2099#ifdef FEAT_WINDOWS
2100 win_T *wp = NULL;
2101#endif
2102 pos_T *fpos;
2103 colnr_T col;
2104
2105 buf = buflist_findnr(n);
2106 if (buf == NULL)
2107 {
2108 if ((options & GETF_ALT) && n == 0)
2109 EMSG(_(e_noalt));
2110 else
2111 EMSGN(_("E92: Buffer %ld not found"), n);
2112 return FAIL;
2113 }
2114
2115 /* if alternate file is the current buffer, nothing to do */
2116 if (buf == curbuf)
2117 return OK;
2118
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002119 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002120 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002121 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002123 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002124#ifdef FEAT_AUTOCMD
2125 if (curbuf_locked())
2126 return FAIL;
2127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128
2129 /* altfpos may be changed by getfile(), get it now */
2130 if (lnum == 0)
2131 {
2132 fpos = buflist_findfpos(buf);
2133 lnum = fpos->lnum;
2134 col = fpos->col;
2135 }
2136 else
2137 col = 0;
2138
2139#ifdef FEAT_WINDOWS
2140 if (options & GETF_SWITCH)
2141 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002142 /* If 'switchbuf' contains "useopen": jump to first window containing
2143 * "buf" if one exists */
2144 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002146
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002147 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002148 * page containing "buf" if one exists */
2149 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002150 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002151
2152 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2153 * current buffer isn't empty: open new tab or window */
2154 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2155 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002157 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002158 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002159 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2160 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002162 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 }
2164 }
2165#endif
2166
2167 ++RedrawingDisabled;
2168 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2169 lnum, forceit) <= 0)
2170 {
2171 --RedrawingDisabled;
2172
2173 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2174 if (!p_sol && col != 0)
2175 {
2176 curwin->w_cursor.col = col;
2177 check_cursor_col();
2178#ifdef FEAT_VIRTUALEDIT
2179 curwin->w_cursor.coladd = 0;
2180#endif
2181 curwin->w_set_curswant = TRUE;
2182 }
2183 return OK;
2184 }
2185 --RedrawingDisabled;
2186 return FAIL;
2187}
2188
2189/*
2190 * go to the last know line number for the current buffer
2191 */
2192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002193buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194{
2195 pos_T *fpos;
2196
2197 fpos = buflist_findfpos(curbuf);
2198
2199 curwin->w_cursor.lnum = fpos->lnum;
2200 check_cursor_lnum();
2201
2202 if (p_sol)
2203 curwin->w_cursor.col = 0;
2204 else
2205 {
2206 curwin->w_cursor.col = fpos->col;
2207 check_cursor_col();
2208#ifdef FEAT_VIRTUALEDIT
2209 curwin->w_cursor.coladd = 0;
2210#endif
2211 curwin->w_set_curswant = TRUE;
2212 }
2213}
2214
Bram Moolenaar81695252004-12-29 20:58:21 +00002215#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2216/*
2217 * Find file in buffer list by name (it has to be for the current window).
2218 * Returns NULL if not found.
2219 */
2220 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002221buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002222{
2223 char_u *ffname;
2224 buf_T *buf = NULL;
2225
2226 /* First make the name into a full path name */
2227 ffname = FullName_save(fname,
2228#ifdef UNIX
2229 TRUE /* force expansion, get rid of symbolic links */
2230#else
2231 FALSE
2232#endif
2233 );
2234 if (ffname != NULL)
2235 {
2236 buf = buflist_findname(ffname);
2237 vim_free(ffname);
2238 }
2239 return buf;
2240}
2241#endif
2242
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243/*
2244 * Find file in buffer list by name (it has to be for the current window).
2245 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002246 * Skips dummy buffers.
2247 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002248 */
2249 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251{
2252#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002253 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254
2255 if (mch_stat((char *)ffname, &st) < 0)
2256 st.st_dev = (dev_T)-1;
2257 return buflist_findname_stat(ffname, &st);
2258}
2259
2260/*
2261 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2262 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002263 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 */
2265 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002266buflist_findname_stat(
2267 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002268 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269{
2270#endif
2271 buf_T *buf;
2272
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002273 /* Start at the last buffer, expect to find a match sooner. */
2274 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002275 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276#ifdef UNIX
2277 , stp
2278#endif
2279 ))
2280 return buf;
2281 return NULL;
2282}
2283
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002284#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2285 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002286/*
2287 * Find file in buffer list by a regexp pattern.
2288 * Return fnum of the found buffer.
2289 * Return < 0 for error.
2290 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002292buflist_findpat(
2293 char_u *pattern,
2294 char_u *pattern_end, /* pointer to first char after pattern */
2295 int unlisted, /* find unlisted buffers */
2296 int diffmode UNUSED, /* find diff-mode buffers only */
2297 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298{
2299 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 int match = -1;
2301 int find_listed;
2302 char_u *pat;
2303 char_u *patend;
2304 int attempt;
2305 char_u *p;
2306 int toggledollar;
2307
2308 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2309 {
2310 if (*pattern == '%')
2311 match = curbuf->b_fnum;
2312 else
2313 match = curwin->w_alt_fnum;
2314#ifdef FEAT_DIFF
2315 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2316 match = -1;
2317#endif
2318 }
2319
2320 /*
2321 * Try four ways of matching a listed buffer:
2322 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002323 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 * attempt == 2: with '$' at end (only match at end)
2325 * attempt == 3: with '^' at start and '$' at end (only full match)
2326 * Repeat this for finding an unlisted buffer if there was no matching
2327 * listed buffer.
2328 */
2329 else
2330 {
2331 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2332 if (pat == NULL)
2333 return -1;
2334 patend = pat + STRLEN(pat) - 1;
2335 toggledollar = (patend > pat && *patend == '$');
2336
2337 /* First try finding a listed buffer. If not found and "unlisted"
2338 * is TRUE, try finding an unlisted buffer. */
2339 find_listed = TRUE;
2340 for (;;)
2341 {
2342 for (attempt = 0; attempt <= 3; ++attempt)
2343 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002344 regmatch_T regmatch;
2345
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 /* may add '^' and '$' */
2347 if (toggledollar)
2348 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2349 p = pat;
2350 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2351 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002352 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2353 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
2355 vim_free(pat);
2356 return -1;
2357 }
2358
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002359 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (buf->b_p_bl == find_listed
2361#ifdef FEAT_DIFF
2362 && (!diffmode || diff_mode_buf(buf))
2363#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002364 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002366 if (curtab_only)
2367 {
2368 /* Ignore the match if the buffer is not open in
2369 * the current tab. */
2370#ifdef FEAT_WINDOWS
2371 win_T *wp;
2372
2373 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2374 if (wp->w_buffer == buf)
2375 break;
2376 if (wp == NULL)
2377 continue;
2378#else
2379 if (curwin->w_buffer != buf)
2380 continue;
2381#endif
2382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 if (match >= 0) /* already found a match */
2384 {
2385 match = -2;
2386 break;
2387 }
2388 match = buf->b_fnum; /* remember first match */
2389 }
2390
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002391 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 if (match >= 0) /* found one match */
2393 break;
2394 }
2395
2396 /* Only search for unlisted buffers if there was no match with
2397 * a listed buffer. */
2398 if (!unlisted || !find_listed || match != -1)
2399 break;
2400 find_listed = FALSE;
2401 }
2402
2403 vim_free(pat);
2404 }
2405
2406 if (match == -2)
2407 EMSG2(_("E93: More than one match for %s"), pattern);
2408 else if (match < 0)
2409 EMSG2(_("E94: No matching buffer for %s"), pattern);
2410 return match;
2411}
2412#endif
2413
2414#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2415
2416/*
2417 * Find all buffer names that match.
2418 * For command line expansion of ":buf" and ":sbuf".
2419 * Return OK if matches found, FAIL otherwise.
2420 */
2421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002422ExpandBufnames(
2423 char_u *pat,
2424 int *num_file,
2425 char_u ***file,
2426 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427{
2428 int count = 0;
2429 buf_T *buf;
2430 int round;
2431 char_u *p;
2432 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002433 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434
2435 *num_file = 0; /* return values in case of FAIL */
2436 *file = NULL;
2437
Bram Moolenaar05159a02005-02-26 23:04:13 +00002438 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2439 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002441 patc = alloc((unsigned)STRLEN(pat) + 11);
2442 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002444 STRCPY(patc, "\\(^\\|[\\/]\\)");
2445 STRCPY(patc + 11, pat + 1);
2446 }
2447 else
2448 patc = pat;
2449
2450 /*
2451 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002452 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002453 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002454 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002455 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002456 regmatch_T regmatch;
2457
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002458 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002459 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002460 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2461 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002462 {
2463 if (patc != pat)
2464 vim_free(patc);
2465 return FAIL;
2466 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467
2468 /*
2469 * round == 1: Count the matches.
2470 * round == 2: Build the array to keep the matches.
2471 */
2472 for (round = 1; round <= 2; ++round)
2473 {
2474 count = 0;
2475 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2476 {
2477 if (!buf->b_p_bl) /* skip unlisted buffers */
2478 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002479 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 if (p != NULL)
2481 {
2482 if (round == 1)
2483 ++count;
2484 else
2485 {
2486 if (options & WILD_HOME_REPLACE)
2487 p = home_replace_save(buf, p);
2488 else
2489 p = vim_strsave(p);
2490 (*file)[count++] = p;
2491 }
2492 }
2493 }
2494 if (count == 0) /* no match found, break here */
2495 break;
2496 if (round == 1)
2497 {
2498 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2499 if (*file == NULL)
2500 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002501 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002502 if (patc != pat)
2503 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 return FAIL;
2505 }
2506 }
2507 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002508 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 if (count) /* match(es) found, break here */
2510 break;
2511 }
2512
Bram Moolenaar05159a02005-02-26 23:04:13 +00002513 if (patc != pat)
2514 vim_free(patc);
2515
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 *num_file = count;
2517 return (count == 0 ? FAIL : OK);
2518}
2519
2520#endif /* FEAT_CMDL_COMPL */
2521
2522#ifdef HAVE_BUFLIST_MATCH
2523/*
2524 * Check for a match on the file name for buffer "buf" with regprog "prog".
2525 */
2526 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002527buflist_match(
2528 regmatch_T *rmp,
2529 buf_T *buf,
2530 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 char_u *match;
2533
2534 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002535 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002537 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
2539 return match;
2540}
2541
2542/*
2543 * Try matching the regexp in "prog" with file name "name".
2544 * Return "name" when there is a match, NULL when not.
2545 */
2546 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002547fname_match(
2548 regmatch_T *rmp,
2549 char_u *name,
2550 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551{
2552 char_u *match = NULL;
2553 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
2555 if (name != NULL)
2556 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002557 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002558 rmp->rm_ic = p_fic || ignore_case;
2559 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 match = name;
2561 else
2562 {
2563 /* Replace $(HOME) with '~' and try matching again. */
2564 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002565 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 match = name;
2567 vim_free(p);
2568 }
2569 }
2570
2571 return match;
2572}
2573#endif
2574
2575/*
2576 * find file in buffer list by number
2577 */
2578 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002579buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580{
2581 buf_T *buf;
2582
2583 if (nr == 0)
2584 nr = curwin->w_alt_fnum;
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002585 /* Assume newer buffers are used more often, start from the end. */
2586 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 if (buf->b_fnum == nr)
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002588 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 return NULL;
2590}
2591
2592/*
2593 * Get name of file 'n' in the buffer list.
2594 * When the file has no name an empty string is returned.
2595 * home_replace() is used to shorten the file name (used for marks).
2596 * Returns a pointer to allocated memory, of NULL when failed.
2597 */
2598 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002599buflist_nr2name(
2600 int n,
2601 int fullname,
2602 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603{
2604 buf_T *buf;
2605
2606 buf = buflist_findnr(n);
2607 if (buf == NULL)
2608 return NULL;
2609 return home_replace_save(helptail ? buf : NULL,
2610 fullname ? buf->b_ffname : buf->b_fname);
2611}
2612
2613/*
2614 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2615 * When "copy_options" is TRUE save the local window option values.
2616 * When "lnum" is 0 only do the options.
2617 */
2618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002619buflist_setfpos(
2620 buf_T *buf,
2621 win_T *win,
2622 linenr_T lnum,
2623 colnr_T col,
2624 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625{
2626 wininfo_T *wip;
2627
2628 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2629 if (wip->wi_win == win)
2630 break;
2631 if (wip == NULL)
2632 {
2633 /* allocate a new entry */
2634 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2635 if (wip == NULL)
2636 return;
2637 wip->wi_win = win;
2638 if (lnum == 0) /* set lnum even when it's 0 */
2639 lnum = 1;
2640 }
2641 else
2642 {
2643 /* remove the entry from the list */
2644 if (wip->wi_prev)
2645 wip->wi_prev->wi_next = wip->wi_next;
2646 else
2647 buf->b_wininfo = wip->wi_next;
2648 if (wip->wi_next)
2649 wip->wi_next->wi_prev = wip->wi_prev;
2650 if (copy_options && wip->wi_optset)
2651 {
2652 clear_winopt(&wip->wi_opt);
2653#ifdef FEAT_FOLDING
2654 deleteFoldRecurse(&wip->wi_folds);
2655#endif
2656 }
2657 }
2658 if (lnum != 0)
2659 {
2660 wip->wi_fpos.lnum = lnum;
2661 wip->wi_fpos.col = col;
2662 }
2663 if (copy_options)
2664 {
2665 /* Save the window-specific option values. */
2666 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2667#ifdef FEAT_FOLDING
2668 wip->wi_fold_manual = win->w_fold_manual;
2669 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2670#endif
2671 wip->wi_optset = TRUE;
2672 }
2673
2674 /* insert the entry in front of the list */
2675 wip->wi_next = buf->b_wininfo;
2676 buf->b_wininfo = wip;
2677 wip->wi_prev = NULL;
2678 if (wip->wi_next)
2679 wip->wi_next->wi_prev = wip;
2680
2681 return;
2682}
2683
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002684#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002685static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002686
2687/*
2688 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2689 * page. That's because a diff is local to a tab page.
2690 */
2691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002692wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002693{
2694 win_T *wp;
2695
2696 if (wip->wi_opt.wo_diff)
2697 {
2698 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2699 /* return FALSE when it's a window in the current tab page, thus
2700 * the buffer was in diff mode here */
2701 if (wip->wi_win == wp)
2702 return FALSE;
2703 return TRUE;
2704 }
2705 return FALSE;
2706}
2707#endif
2708
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709/*
2710 * Find info for the current window in buffer "buf".
2711 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002712 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2713 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 * Returns NULL when there isn't any info.
2715 */
2716 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002717find_wininfo(
2718 buf_T *buf,
2719 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720{
2721 wininfo_T *wip;
2722
2723 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002724 if (wip->wi_win == curwin
2725#ifdef FEAT_DIFF
2726 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2727#endif
2728 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002729 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002730
2731 /* If no wininfo for curwin, use the first in the list (that doesn't have
2732 * 'diff' set and is in another tab page). */
2733 if (wip == NULL)
2734 {
2735#ifdef FEAT_DIFF
2736 if (skip_diff_buffer)
2737 {
2738 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2739 if (!wininfo_other_tab_diff(wip))
2740 break;
2741 }
2742 else
2743#endif
2744 wip = buf->b_wininfo;
2745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 return wip;
2747}
2748
2749/*
2750 * Reset the local window options to the values last used in this window.
2751 * If the buffer wasn't used in this window before, use the values from
2752 * the most recently used window. If the values were never set, use the
2753 * global values for the window.
2754 */
2755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002756get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757{
2758 wininfo_T *wip;
2759
2760 clear_winopt(&curwin->w_onebuf_opt);
2761#ifdef FEAT_FOLDING
2762 clearFolding(curwin);
2763#endif
2764
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002765 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002766 if (wip != NULL && wip->wi_optset)
2767 {
2768 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2769#ifdef FEAT_FOLDING
2770 curwin->w_fold_manual = wip->wi_fold_manual;
2771 curwin->w_foldinvalid = TRUE;
2772 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2773#endif
2774 }
2775 else
2776 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2777
2778#ifdef FEAT_FOLDING
2779 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2780 if (p_fdls >= 0)
2781 curwin->w_p_fdl = p_fdls;
2782#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002783#ifdef FEAT_SYN_HL
2784 check_colorcolumn(curwin);
2785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786}
2787
2788/*
2789 * Find the position (lnum and col) for the buffer 'buf' for the current
2790 * window.
2791 * Returns a pointer to no_position if no position is found.
2792 */
2793 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002797 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002798
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002799 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 if (wip != NULL)
2801 return &(wip->wi_fpos);
2802 else
2803 return &no_position;
2804}
2805
2806/*
2807 * Find the lnum for the buffer 'buf' for the current window.
2808 */
2809 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002810buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002811{
2812 return buflist_findfpos(buf)->lnum;
2813}
2814
2815#if defined(FEAT_LISTCMDS) || defined(PROTO)
2816/*
2817 * List all know file names (for :files and :buffers command).
2818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002820buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821{
2822 buf_T *buf;
2823 int len;
2824 int i;
2825
2826 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2827 {
2828 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002829 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2830 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2831 || (vim_strchr(eap->arg, '+')
2832 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2833 || (vim_strchr(eap->arg, 'a')
2834 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2835 || (vim_strchr(eap->arg, 'h')
2836 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2837 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2838 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2839 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2840 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2841 || (vim_strchr(eap->arg, '#')
2842 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 continue;
2844 msg_putchar('\n');
2845 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002846 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 else
2848 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2849
Bram Moolenaar50cde822005-06-05 21:54:54 +00002850 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851 buf->b_fnum,
2852 buf->b_p_bl ? ' ' : 'u',
2853 buf == curbuf ? '%' :
2854 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2855 buf->b_ml.ml_mfp == NULL ? ' ' :
2856 (buf->b_nwindows == 0 ? 'h' : 'a'),
2857 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2858 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002859 : (bufIsChanged(buf) ? '+' : ' '),
2860 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002861 if (len > IOSIZE - 20)
2862 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863
2864 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865 i = 40 - vim_strsize(IObuff);
2866 do
2867 {
2868 IObuff[len++] = ' ';
2869 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002870 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2871 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002872 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 msg_outtrans(IObuff);
2874 out_flush(); /* output one line at a time */
2875 ui_breakcheck();
2876 }
2877}
2878#endif
2879
2880/*
2881 * Get file name and line number for file 'fnum'.
2882 * Used by DoOneCmd() for translating '%' and '#'.
2883 * Used by insert_reg() and cmdline_paste() for '#' register.
2884 * Return FAIL if not found, OK for success.
2885 */
2886 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002887buflist_name_nr(
2888 int fnum,
2889 char_u **fname,
2890 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002891{
2892 buf_T *buf;
2893
2894 buf = buflist_findnr(fnum);
2895 if (buf == NULL || buf->b_fname == NULL)
2896 return FAIL;
2897
2898 *fname = buf->b_fname;
2899 *lnum = buflist_findlnum(buf);
2900
2901 return OK;
2902}
2903
2904/*
2905 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2906 * The file name with the full path is also remembered, for when :cd is used.
2907 * Returns FAIL for failure (file name already in use by other buffer)
2908 * OK otherwise.
2909 */
2910 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002911setfname(
2912 buf_T *buf,
2913 char_u *ffname,
2914 char_u *sfname,
2915 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916{
Bram Moolenaar81695252004-12-29 20:58:21 +00002917 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002919 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920#endif
2921
2922 if (ffname == NULL || *ffname == NUL)
2923 {
2924 /* Removing the name. */
2925 vim_free(buf->b_ffname);
2926 vim_free(buf->b_sfname);
2927 buf->b_ffname = NULL;
2928 buf->b_sfname = NULL;
2929#ifdef UNIX
2930 st.st_dev = (dev_T)-1;
2931#endif
2932 }
2933 else
2934 {
2935 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2936 if (ffname == NULL) /* out of memory */
2937 return FAIL;
2938
2939 /*
2940 * if the file name is already used in another buffer:
2941 * - if the buffer is loaded, fail
2942 * - if the buffer is not loaded, delete it from the list
2943 */
2944#ifdef UNIX
2945 if (mch_stat((char *)ffname, &st) < 0)
2946 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002947#endif
2948 if (!(buf->b_flags & BF_DUMMY))
2949#ifdef UNIX
2950 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002951#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002952 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953#endif
2954 if (obuf != NULL && obuf != buf)
2955 {
2956 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2957 {
2958 if (message)
2959 EMSG(_("E95: Buffer with this name already exists"));
2960 vim_free(ffname);
2961 return FAIL;
2962 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002963 /* delete from the list */
2964 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 }
2966 sfname = vim_strsave(sfname);
2967 if (ffname == NULL || sfname == NULL)
2968 {
2969 vim_free(sfname);
2970 vim_free(ffname);
2971 return FAIL;
2972 }
2973#ifdef USE_FNAME_CASE
2974# ifdef USE_LONG_FNAME
2975 if (USE_LONG_FNAME)
2976# endif
2977 fname_case(sfname, 0); /* set correct case for short file name */
2978#endif
2979 vim_free(buf->b_ffname);
2980 vim_free(buf->b_sfname);
2981 buf->b_ffname = ffname;
2982 buf->b_sfname = sfname;
2983 }
2984 buf->b_fname = buf->b_sfname;
2985#ifdef UNIX
2986 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002987 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002988 else
2989 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002990 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 buf->b_dev = st.st_dev;
2992 buf->b_ino = st.st_ino;
2993 }
2994#endif
2995
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 buf_name_changed(buf);
2999 return OK;
3000}
3001
3002/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003003 * Crude way of changing the name of a buffer. Use with care!
3004 * The name should be relative to the current directory.
3005 */
3006 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003007buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003008{
3009 buf_T *buf;
3010
3011 buf = buflist_findnr(fnum);
3012 if (buf != NULL)
3013 {
3014 vim_free(buf->b_sfname);
3015 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003016 buf->b_ffname = vim_strsave(name);
3017 buf->b_sfname = NULL;
3018 /* Allocate ffname and expand into full path. Also resolves .lnk
3019 * files on Win32. */
3020 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003021 buf->b_fname = buf->b_sfname;
3022 }
3023}
3024
3025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 * Take care of what needs to be done when the name of buffer "buf" has
3027 * changed.
3028 */
3029 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003030buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031{
3032 /*
3033 * If the file name changed, also change the name of the swapfile
3034 */
3035 if (buf->b_ml.ml_mfp != NULL)
3036 ml_setname(buf);
3037
3038 if (curwin->w_buffer == buf)
3039 check_arg_idx(curwin); /* check file name for arg list */
3040#ifdef FEAT_TITLE
3041 maketitle(); /* set window title */
3042#endif
3043#ifdef FEAT_WINDOWS
3044 status_redraw_all(); /* status lines need to be redrawn */
3045#endif
3046 fmarks_check_names(buf); /* check named file marks */
3047 ml_timestamp(buf); /* reset timestamp */
3048}
3049
3050/*
3051 * set alternate file name for current window
3052 *
3053 * Used by do_one_cmd(), do_write() and do_ecmd().
3054 * Return the buffer.
3055 */
3056 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003057setaltfname(
3058 char_u *ffname,
3059 char_u *sfname,
3060 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061{
3062 buf_T *buf;
3063
3064 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3065 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003066 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 curwin->w_alt_fnum = buf->b_fnum;
3068 return buf;
3069}
3070
3071/*
3072 * Get alternate file name for current window.
3073 * Return NULL if there isn't any, and give error message if requested.
3074 */
3075 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003076getaltfname(
3077 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078{
3079 char_u *fname;
3080 linenr_T dummy;
3081
3082 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3083 {
3084 if (errmsg)
3085 EMSG(_(e_noalt));
3086 return NULL;
3087 }
3088 return fname;
3089}
3090
3091/*
3092 * Add a file name to the buflist and return its number.
3093 * Uses same flags as buflist_new(), except BLN_DUMMY.
3094 *
3095 * used by qf_init(), main() and doarglist()
3096 */
3097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003098buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099{
3100 buf_T *buf;
3101
3102 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3103 if (buf != NULL)
3104 return buf->b_fnum;
3105 return 0;
3106}
3107
3108#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3109/*
3110 * Adjust slashes in file names. Called after 'shellslash' was set.
3111 */
3112 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003113buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114{
3115 buf_T *bp;
3116
3117 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3118 {
3119 if (bp->b_ffname != NULL)
3120 slash_adjust(bp->b_ffname);
3121 if (bp->b_sfname != NULL)
3122 slash_adjust(bp->b_sfname);
3123 }
3124}
3125#endif
3126
3127/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003128 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 * Also save the local window option values.
3130 */
3131 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003132buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003134 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135}
3136
3137/*
3138 * Return TRUE if 'ffname' is not the same file as current file.
3139 * Fname must have a full path (expanded by mch_FullName()).
3140 */
3141 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003142otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143{
3144 return otherfile_buf(curbuf, ffname
3145#ifdef UNIX
3146 , NULL
3147#endif
3148 );
3149}
3150
3151 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003152otherfile_buf(
3153 buf_T *buf,
3154 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003156 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003158 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159{
3160 /* no name is different */
3161 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3162 return TRUE;
3163 if (fnamecmp(ffname, buf->b_ffname) == 0)
3164 return FALSE;
3165#ifdef UNIX
3166 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003167 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168
Bram Moolenaar8767f522016-07-01 17:17:39 +02003169 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 if (stp == NULL)
3171 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003172 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 st.st_dev = (dev_T)-1;
3174 stp = &st;
3175 }
3176 /* Use dev/ino to check if the files are the same, even when the names
3177 * are different (possible with links). Still need to compare the
3178 * name above, for when the file doesn't exist yet.
3179 * Problem: The dev/ino changes when a file is deleted (and created
3180 * again) and remains the same when renamed/moved. We don't want to
3181 * mch_stat() each buffer each time, that would be too slow. Get the
3182 * dev/ino again when they appear to match, but not when they appear
3183 * to be different: Could skip a buffer when it's actually the same
3184 * file. */
3185 if (buf_same_ino(buf, stp))
3186 {
3187 buf_setino(buf);
3188 if (buf_same_ino(buf, stp))
3189 return FALSE;
3190 }
3191 }
3192#endif
3193 return TRUE;
3194}
3195
3196#if defined(UNIX) || defined(PROTO)
3197/*
3198 * Set inode and device number for a buffer.
3199 * Must always be called when b_fname is changed!.
3200 */
3201 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003202buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003204 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205
3206 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3207 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003208 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 buf->b_dev = st.st_dev;
3210 buf->b_ino = st.st_ino;
3211 }
3212 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003213 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214}
3215
3216/*
3217 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3218 */
3219 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003220buf_same_ino(
3221 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003222 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003224 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 && stp->st_dev == buf->b_dev
3226 && stp->st_ino == buf->b_ino);
3227}
3228#endif
3229
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003230/*
3231 * Print info about the current buffer.
3232 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234fileinfo(
3235 int fullname, /* when non-zero print full path */
3236 int shorthelp,
3237 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 char_u *name;
3240 int n;
3241 char_u *p;
3242 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003243 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245 buffer = alloc(IOSIZE);
3246 if (buffer == NULL)
3247 return;
3248
3249 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3250 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003251 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003252 p = buffer + STRLEN(buffer);
3253 }
3254 else
3255 p = buffer;
3256
3257 *p++ = '"';
3258 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003259 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 else
3261 {
3262 if (!fullname && curbuf->b_fname != NULL)
3263 name = curbuf->b_fname;
3264 else
3265 name = curbuf->b_ffname;
3266 home_replace(shorthelp ? curbuf : NULL, name, p,
3267 (int)(IOSIZE - (p - buffer)), TRUE);
3268 }
3269
Bram Moolenaara800b422010-06-27 01:15:55 +02003270 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 curbufIsChanged() ? (shortmess(SHM_MOD)
3272 ? " [+]" : _(" [Modified]")) : " ",
3273 (curbuf->b_flags & BF_NOTEDITED)
3274#ifdef FEAT_QUICKFIX
3275 && !bt_dontwrite(curbuf)
3276#endif
3277 ? _("[Not edited]") : "",
3278 (curbuf->b_flags & BF_NEW)
3279#ifdef FEAT_QUICKFIX
3280 && !bt_dontwrite(curbuf)
3281#endif
3282 ? _("[New file]") : "",
3283 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003284 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 : _("[readonly]")) : "",
3286 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3287 || curbuf->b_p_ro) ?
3288 " " : "");
3289 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3290 * causes an overflow, thus for large numbers divide instead. */
3291 if (curwin->w_cursor.lnum > 1000000L)
3292 n = (int)(((long)curwin->w_cursor.lnum) /
3293 ((long)curbuf->b_ml.ml_line_count / 100L));
3294 else
3295 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3296 (long)curbuf->b_ml.ml_line_count);
3297 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3298 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003299 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300 }
3301#ifdef FEAT_CMDL_INFO
3302 else if (p_ru)
3303 {
3304 /* Current line and column are already on the screen -- webb */
3305 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003306 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003308 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 (long)curbuf->b_ml.ml_line_count, n);
3310 }
3311#endif
3312 else
3313 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003314 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 _("line %ld of %ld --%d%%-- col "),
3316 (long)curwin->w_cursor.lnum,
3317 (long)curbuf->b_ml.ml_line_count,
3318 n);
3319 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003320 len = STRLEN(buffer);
3321 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3323 }
3324
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003325 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326
3327 if (dont_truncate)
3328 {
3329 /* Temporarily set msg_scroll to avoid the message being truncated.
3330 * First call msg_start() to get the message in the right place. */
3331 msg_start();
3332 n = msg_scroll;
3333 msg_scroll = TRUE;
3334 msg(buffer);
3335 msg_scroll = n;
3336 }
3337 else
3338 {
3339 p = msg_trunc_attr(buffer, FALSE, 0);
3340 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 /* Need to repeat the message after redrawing when:
3342 * - When restart_edit is set (otherwise there will be a delay
3343 * before redrawing).
3344 * - When the screen was scrolled but there is no wait-return
3345 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003346 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 }
3348
3349 vim_free(buffer);
3350}
3351
3352 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353col_print(
3354 char_u *buf,
3355 size_t buflen,
3356 int col,
3357 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358{
3359 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003360 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003362 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363}
3364
3365#if defined(FEAT_TITLE) || defined(PROTO)
3366/*
3367 * put file name in title bar of window and in icon title
3368 */
3369
3370static char_u *lasttitle = NULL;
3371static char_u *lasticon = NULL;
3372
3373 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375{
3376 char_u *p;
3377 char_u *t_str = NULL;
3378 char_u *i_name;
3379 char_u *i_str = NULL;
3380 int maxlen = 0;
3381 int len;
3382 int mustset;
3383 char_u buf[IOSIZE];
3384 int off;
3385
3386 if (!redrawing())
3387 {
3388 /* Postpone updating the title when 'lazyredraw' is set. */
3389 need_maketitle = TRUE;
3390 return;
3391 }
3392
3393 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003394 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 return;
3396
3397 if (p_title)
3398 {
3399 if (p_titlelen > 0)
3400 {
3401 maxlen = p_titlelen * Columns / 100;
3402 if (maxlen < 10)
3403 maxlen = 10;
3404 }
3405
3406 t_str = buf;
3407 if (*p_titlestring != NUL)
3408 {
3409#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003410 if (stl_syntax & STL_IN_TITLE)
3411 {
3412 int use_sandbox = FALSE;
3413 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003414
3415# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003416 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003417# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003418 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003420 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003421 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003422 if (called_emsg)
3423 set_string_option_direct((char_u *)"titlestring", -1,
3424 (char_u *)"", OPT_FREE, SID_ERROR);
3425 called_emsg |= save_called_emsg;
3426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 else
3428#endif
3429 t_str = p_titlestring;
3430 }
3431 else
3432 {
3433 /* format: "fname + (path) (1 of 2) - VIM" */
3434
Bram Moolenaar2c666692012-09-05 13:30:40 +02003435#define SPACE_FOR_FNAME (IOSIZE - 100)
3436#define SPACE_FOR_DIR (IOSIZE - 20)
3437#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003439 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 else
3441 {
3442 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003443 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 }
3446
3447 switch (bufIsChanged(curbuf)
3448 + (curbuf->b_p_ro * 2)
3449 + (!curbuf->b_p_ma * 4))
3450 {
3451 case 1: STRCAT(buf, " +"); break;
3452 case 2: STRCAT(buf, " ="); break;
3453 case 3: STRCAT(buf, " =+"); break;
3454 case 4:
3455 case 6: STRCAT(buf, " -"); break;
3456 case 5:
3457 case 7: STRCAT(buf, " -+"); break;
3458 }
3459
3460 if (curbuf->b_fname != NULL)
3461 {
3462 /* Get path of file, replace home dir with ~ */
3463 off = (int)STRLEN(buf);
3464 buf[off++] = ' ';
3465 buf[off++] = '(';
3466 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003467 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468#ifdef BACKSLASH_IN_FILENAME
3469 /* avoid "c:/name" to be reduced to "c" */
3470 if (isalpha(buf[off]) && buf[off + 1] == ':')
3471 off += 2;
3472#endif
3473 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003474 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003477 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003478 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003481
Bram Moolenaar2c666692012-09-05 13:30:40 +02003482 /* Translate unprintable chars and concatenate. Keep some
3483 * room for the server name. When there is no room (very long
3484 * file name) use (...). */
3485 if (off < SPACE_FOR_DIR)
3486 {
3487 p = transstr(buf + off);
3488 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3489 vim_free(p);
3490 }
3491 else
3492 {
3493 vim_strncpy(buf + off, (char_u *)"...",
3494 (size_t)(SPACE_FOR_ARGNR - off));
3495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496 STRCAT(buf, ")");
3497 }
3498
Bram Moolenaar2c666692012-09-05 13:30:40 +02003499 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500
3501#if defined(FEAT_CLIENTSERVER)
3502 if (serverName != NULL)
3503 {
3504 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003505 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 }
3507 else
3508#endif
3509 STRCAT(buf, " - VIM");
3510
3511 if (maxlen > 0)
3512 {
3513 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003514 if (vim_strsize(buf) > maxlen)
3515 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 }
3517 }
3518 }
3519 mustset = ti_change(t_str, &lasttitle);
3520
3521 if (p_icon)
3522 {
3523 i_str = buf;
3524 if (*p_iconstring != NUL)
3525 {
3526#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003527 if (stl_syntax & STL_IN_ICON)
3528 {
3529 int use_sandbox = FALSE;
3530 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003531
3532# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003533 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003534# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003535 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003537 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003538 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003539 if (called_emsg)
3540 set_string_option_direct((char_u *)"iconstring", -1,
3541 (char_u *)"", OPT_FREE, SID_ERROR);
3542 called_emsg |= save_called_emsg;
3543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 else
3545#endif
3546 i_str = p_iconstring;
3547 }
3548 else
3549 {
3550 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003551 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 else /* use file name only in icon */
3553 i_name = gettail(curbuf->b_ffname);
3554 *i_str = NUL;
3555 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003556 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (len > 100)
3558 {
3559 len -= 100;
3560#ifdef FEAT_MBYTE
3561 if (has_mbyte)
3562 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3563#endif
3564 i_name += len;
3565 }
3566 STRCPY(i_str, i_name);
3567 trans_characters(i_str, IOSIZE);
3568 }
3569 }
3570
3571 mustset |= ti_change(i_str, &lasticon);
3572
3573 if (mustset)
3574 resettitle();
3575}
3576
3577/*
3578 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3579 * from "str" if it does.
3580 * Return TRUE when "*last" changed.
3581 */
3582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
3585 if ((str == NULL) != (*last == NULL)
3586 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3587 {
3588 vim_free(*last);
3589 if (str == NULL)
3590 *last = NULL;
3591 else
3592 *last = vim_strsave(str);
3593 return TRUE;
3594 }
3595 return FALSE;
3596}
3597
3598/*
3599 * Put current window title back (used after calling a shell)
3600 */
3601 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003602resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603{
3604 mch_settitle(lasttitle, lasticon);
3605}
Bram Moolenaarea408852005-06-25 22:49:46 +00003606
3607# if defined(EXITFREE) || defined(PROTO)
3608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003609free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003610{
3611 vim_free(lasttitle);
3612 vim_free(lasticon);
3613}
3614# endif
3615
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616#endif /* FEAT_TITLE */
3617
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003618#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003620 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 * Return length of string in screen cells.
3622 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003623 * Normally works for window "wp", except when working for 'tabline' then it
3624 * is "curwin".
3625 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 * Items are drawn interspersed with the text that surrounds it
3627 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3628 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3629 *
3630 * If maxwidth is not zero, the string will be filled at any middle marker
3631 * or truncated if too long, fillchar is used for all whitespace.
3632 */
3633 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003634build_stl_str_hl(
3635 win_T *wp,
3636 char_u *out, /* buffer to write into != NameBuff */
3637 size_t outlen, /* length of out[] */
3638 char_u *fmt,
3639 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3640 int fillchar,
3641 int maxwidth,
3642 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3643 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644{
3645 char_u *p;
3646 char_u *s;
3647 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003648 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649#ifdef FEAT_EVAL
3650 win_T *o_curwin;
3651 buf_T *o_curbuf;
3652#endif
3653 int empty_line;
3654 colnr_T virtcol;
3655 long l;
3656 long n;
3657 int prevchar_isflag;
3658 int prevchar_isitem;
3659 int itemisflag;
3660 int fillable;
3661 char_u *str;
3662 long num;
3663 int width;
3664 int itemcnt;
3665 int curitem;
3666 int groupitem[STL_MAX_ITEM];
3667 int groupdepth;
3668 struct stl_item
3669 {
3670 char_u *start;
3671 int minwid;
3672 int maxwid;
3673 enum
3674 {
3675 Normal,
3676 Empty,
3677 Group,
3678 Middle,
3679 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003680 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681 Trunc
3682 } type;
3683 } item[STL_MAX_ITEM];
3684 int minwid;
3685 int maxwid;
3686 int zeropad;
3687 char_u base;
3688 char_u opt;
3689#define TMPLEN 70
3690 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003691 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003692 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003693
3694#ifdef FEAT_EVAL
3695 /*
3696 * When the format starts with "%!" then evaluate it as an expression and
3697 * use the result as the actual format string.
3698 */
3699 if (fmt[0] == '%' && fmt[1] == '!')
3700 {
3701 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3702 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003703 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003704 }
3705#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706
3707 if (fillchar == 0)
3708 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003709#ifdef FEAT_MBYTE
3710 /* Can't handle a multi-byte fill character yet. */
3711 else if (mb_char2len(fillchar) > 1)
3712 fillchar = '-';
3713#endif
3714
Bram Moolenaar567199b2013-04-24 16:52:36 +02003715 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3716 * p will become invalid when getting another buffer line. */
3717 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3718 empty_line = (*p == NUL);
3719
3720 /* Get the byte value now, in case we need it below. This is more
3721 * efficient than making a copy of the line. */
3722 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3723 byteval = 0;
3724 else
3725#ifdef FEAT_MBYTE
3726 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3727#else
3728 byteval = p[wp->w_cursor.col];
3729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730
3731 groupdepth = 0;
3732 p = out;
3733 curitem = 0;
3734 prevchar_isflag = TRUE;
3735 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003736 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003738 if (curitem == STL_MAX_ITEM)
3739 {
3740 /* There are too many items. Add the error code to the statusline
3741 * to give the user a hint about what went wrong. */
3742 if (p + 6 < out + outlen)
3743 {
3744 mch_memmove(p, " E541", (size_t)5);
3745 p += 5;
3746 }
3747 break;
3748 }
3749
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 if (*s != NUL && *s != '%')
3751 prevchar_isflag = prevchar_isitem = FALSE;
3752
3753 /*
3754 * Handle up to the next '%' or the end.
3755 */
3756 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3757 *p++ = *s++;
3758 if (*s == NUL || p + 1 >= out + outlen)
3759 break;
3760
3761 /*
3762 * Handle one '%' item.
3763 */
3764 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003765 if (*s == NUL) /* ignore trailing % */
3766 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 if (*s == '%')
3768 {
3769 if (p + 1 >= out + outlen)
3770 break;
3771 *p++ = *s++;
3772 prevchar_isflag = prevchar_isitem = FALSE;
3773 continue;
3774 }
3775 if (*s == STL_MIDDLEMARK)
3776 {
3777 s++;
3778 if (groupdepth > 0)
3779 continue;
3780 item[curitem].type = Middle;
3781 item[curitem++].start = p;
3782 continue;
3783 }
3784 if (*s == STL_TRUNCMARK)
3785 {
3786 s++;
3787 item[curitem].type = Trunc;
3788 item[curitem++].start = p;
3789 continue;
3790 }
3791 if (*s == ')')
3792 {
3793 s++;
3794 if (groupdepth < 1)
3795 continue;
3796 groupdepth--;
3797
3798 t = item[groupitem[groupdepth]].start;
3799 *p = NUL;
3800 l = vim_strsize(t);
3801 if (curitem > groupitem[groupdepth] + 1
3802 && item[groupitem[groupdepth]].minwid == 0)
3803 {
3804 /* remove group if all items are empty */
3805 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003806 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 break;
3808 if (n == curitem)
3809 {
3810 p = t;
3811 l = 0;
3812 }
3813 }
3814 if (l > item[groupitem[groupdepth]].maxwid)
3815 {
3816 /* truncate, remove n bytes of text at the start */
3817#ifdef FEAT_MBYTE
3818 if (has_mbyte)
3819 {
3820 /* Find the first character that should be included. */
3821 n = 0;
3822 while (l >= item[groupitem[groupdepth]].maxwid)
3823 {
3824 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003825 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 }
3827 }
3828 else
3829#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003830 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831
3832 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003833 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 p = p - n + 1;
3835#ifdef FEAT_MBYTE
3836 /* Fill up space left over by half a double-wide char. */
3837 while (++l < item[groupitem[groupdepth]].minwid)
3838 *p++ = fillchar;
3839#endif
3840
3841 /* correct the start of the items for the truncation */
3842 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3843 {
3844 item[l].start -= n;
3845 if (item[l].start < t)
3846 item[l].start = t;
3847 }
3848 }
3849 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3850 {
3851 /* fill */
3852 n = item[groupitem[groupdepth]].minwid;
3853 if (n < 0)
3854 {
3855 /* fill by appending characters */
3856 n = 0 - n;
3857 while (l++ < n && p + 1 < out + outlen)
3858 *p++ = fillchar;
3859 }
3860 else
3861 {
3862 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003863 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 l = n - l;
3865 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003866 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003867 p += l;
3868 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3869 item[n].start += l;
3870 for ( ; l > 0; l--)
3871 *t++ = fillchar;
3872 }
3873 }
3874 continue;
3875 }
3876 minwid = 0;
3877 maxwid = 9999;
3878 zeropad = FALSE;
3879 l = 1;
3880 if (*s == '0')
3881 {
3882 s++;
3883 zeropad = TRUE;
3884 }
3885 if (*s == '-')
3886 {
3887 s++;
3888 l = -1;
3889 }
3890 if (VIM_ISDIGIT(*s))
3891 {
3892 minwid = (int)getdigits(&s);
3893 if (minwid < 0) /* overflow */
3894 minwid = 0;
3895 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003896 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 {
3898 item[curitem].type = Highlight;
3899 item[curitem].start = p;
3900 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3901 s++;
3902 curitem++;
3903 continue;
3904 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003905 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3906 {
3907 if (*s == STL_TABCLOSENR)
3908 {
3909 if (minwid == 0)
3910 {
3911 /* %X ends the close label, go back to the previously
3912 * define tab label nr. */
3913 for (n = curitem - 1; n >= 0; --n)
3914 if (item[n].type == TabPage && item[n].minwid >= 0)
3915 {
3916 minwid = item[n].minwid;
3917 break;
3918 }
3919 }
3920 else
3921 /* close nrs are stored as negative values */
3922 minwid = - minwid;
3923 }
3924 item[curitem].type = TabPage;
3925 item[curitem].start = p;
3926 item[curitem].minwid = minwid;
3927 s++;
3928 curitem++;
3929 continue;
3930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 if (*s == '.')
3932 {
3933 s++;
3934 if (VIM_ISDIGIT(*s))
3935 {
3936 maxwid = (int)getdigits(&s);
3937 if (maxwid <= 0) /* overflow */
3938 maxwid = 50;
3939 }
3940 }
3941 minwid = (minwid > 50 ? 50 : minwid) * l;
3942 if (*s == '(')
3943 {
3944 groupitem[groupdepth++] = curitem;
3945 item[curitem].type = Group;
3946 item[curitem].start = p;
3947 item[curitem].minwid = minwid;
3948 item[curitem].maxwid = maxwid;
3949 s++;
3950 curitem++;
3951 continue;
3952 }
3953 if (vim_strchr(STL_ALL, *s) == NULL)
3954 {
3955 s++;
3956 continue;
3957 }
3958 opt = *s++;
3959
3960 /* OK - now for the real work */
3961 base = 'D';
3962 itemisflag = FALSE;
3963 fillable = TRUE;
3964 num = -1;
3965 str = NULL;
3966 switch (opt)
3967 {
3968 case STL_FILEPATH:
3969 case STL_FULLPATH:
3970 case STL_FILENAME:
3971 fillable = FALSE; /* don't change ' ' to fillchar */
3972 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003973 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 else
3975 {
3976 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003977 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003978 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3979 }
3980 trans_characters(NameBuff, MAXPATHL);
3981 if (opt != STL_FILENAME)
3982 str = NameBuff;
3983 else
3984 str = gettail(NameBuff);
3985 break;
3986
3987 case STL_VIM_EXPR: /* '{' */
3988 itemisflag = TRUE;
3989 t = p;
3990 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3991 *p++ = *s++;
3992 if (*s != '}') /* missing '}' or out of space */
3993 break;
3994 s++;
3995 *p = 0;
3996 p = t;
3997
3998#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003999 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4001
4002 o_curbuf = curbuf;
4003 o_curwin = curwin;
4004 curwin = wp;
4005 curbuf = wp->w_buffer;
4006
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004007 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008
4009 curwin = o_curwin;
4010 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004011 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012
4013 if (str != NULL && *str != 0)
4014 {
4015 if (*skipdigits(str) == NUL)
4016 {
4017 num = atoi((char *)str);
4018 vim_free(str);
4019 str = NULL;
4020 itemisflag = FALSE;
4021 }
4022 }
4023#endif
4024 break;
4025
4026 case STL_LINE:
4027 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4028 ? 0L : (long)(wp->w_cursor.lnum);
4029 break;
4030
4031 case STL_NUMLINES:
4032 num = wp->w_buffer->b_ml.ml_line_count;
4033 break;
4034
4035 case STL_COLUMN:
4036 num = !(State & INSERT) && empty_line
4037 ? 0 : (int)wp->w_cursor.col + 1;
4038 break;
4039
4040 case STL_VIRTCOL:
4041 case STL_VIRTCOL_ALT:
4042 /* In list mode virtcol needs to be recomputed */
4043 virtcol = wp->w_virtcol;
4044 if (wp->w_p_list && lcs_tab1 == NUL)
4045 {
4046 wp->w_p_list = FALSE;
4047 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4048 wp->w_p_list = TRUE;
4049 }
4050 ++virtcol;
4051 /* Don't display %V if it's the same as %c. */
4052 if (opt == STL_VIRTCOL_ALT
4053 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4054 ? 0 : (int)wp->w_cursor.col + 1)))
4055 break;
4056 num = (long)virtcol;
4057 break;
4058
4059 case STL_PERCENTAGE:
4060 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4061 (long)wp->w_buffer->b_ml.ml_line_count);
4062 break;
4063
4064 case STL_ALTPERCENT:
4065 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004066 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 break;
4068
4069 case STL_ARGLISTSTAT:
4070 fillable = FALSE;
4071 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004072 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 str = tmp;
4074 break;
4075
4076 case STL_KEYMAP:
4077 fillable = FALSE;
4078 if (get_keymap_str(wp, tmp, TMPLEN))
4079 str = tmp;
4080 break;
4081 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004082#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004083 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084#else
4085 num = 0;
4086#endif
4087 break;
4088
4089 case STL_BUFNO:
4090 num = wp->w_buffer->b_fnum;
4091 break;
4092
4093 case STL_OFFSET_X:
4094 base = 'X';
4095 case STL_OFFSET:
4096#ifdef FEAT_BYTEOFF
4097 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4098 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4099 0L : l + 1 + (!(State & INSERT) && empty_line ?
4100 0 : (int)wp->w_cursor.col);
4101#endif
4102 break;
4103
4104 case STL_BYTEVAL_X:
4105 base = 'X';
4106 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004107 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 if (num == NL)
4109 num = 0;
4110 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4111 num = NL;
4112 break;
4113
4114 case STL_ROFLAG:
4115 case STL_ROFLAG_ALT:
4116 itemisflag = TRUE;
4117 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004118 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 break;
4120
4121 case STL_HELPFLAG:
4122 case STL_HELPFLAG_ALT:
4123 itemisflag = TRUE;
4124 if (wp->w_buffer->b_help)
4125 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004126 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 break;
4128
4129#ifdef FEAT_AUTOCMD
4130 case STL_FILETYPE:
4131 if (*wp->w_buffer->b_p_ft != NUL
4132 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4133 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004134 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4135 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 str = tmp;
4137 }
4138 break;
4139
4140 case STL_FILETYPE_ALT:
4141 itemisflag = TRUE;
4142 if (*wp->w_buffer->b_p_ft != NUL
4143 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4144 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004145 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4146 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 for (t = tmp; *t != 0; t++)
4148 *t = TOUPPER_LOC(*t);
4149 str = tmp;
4150 }
4151 break;
4152#endif
4153
4154#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4155 case STL_PREVIEWFLAG:
4156 case STL_PREVIEWFLAG_ALT:
4157 itemisflag = TRUE;
4158 if (wp->w_p_pvw)
4159 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4160 : _("[Preview]"));
4161 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004162
4163 case STL_QUICKFIX:
4164 if (bt_quickfix(wp->w_buffer))
4165 str = (char_u *)(wp->w_llist_ref
4166 ? _(msg_loclist)
4167 : _(msg_qflist));
4168 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169#endif
4170
4171 case STL_MODIFIED:
4172 case STL_MODIFIED_ALT:
4173 itemisflag = TRUE;
4174 switch ((opt == STL_MODIFIED_ALT)
4175 + bufIsChanged(wp->w_buffer) * 2
4176 + (!wp->w_buffer->b_p_ma) * 4)
4177 {
4178 case 2: str = (char_u *)"[+]"; break;
4179 case 3: str = (char_u *)",+"; break;
4180 case 4: str = (char_u *)"[-]"; break;
4181 case 5: str = (char_u *)",-"; break;
4182 case 6: str = (char_u *)"[+-]"; break;
4183 case 7: str = (char_u *)",+-"; break;
4184 }
4185 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004186
4187 case STL_HIGHLIGHT:
4188 t = s;
4189 while (*s != '#' && *s != NUL)
4190 ++s;
4191 if (*s == '#')
4192 {
4193 item[curitem].type = Highlight;
4194 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004195 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004196 curitem++;
4197 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004198 if (*s != NUL)
4199 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004200 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201 }
4202
4203 item[curitem].start = p;
4204 item[curitem].type = Normal;
4205 if (str != NULL && *str)
4206 {
4207 t = str;
4208 if (itemisflag)
4209 {
4210 if ((t[0] && t[1])
4211 && ((!prevchar_isitem && *t == ',')
4212 || (prevchar_isflag && *t == ' ')))
4213 t++;
4214 prevchar_isflag = TRUE;
4215 }
4216 l = vim_strsize(t);
4217 if (l > 0)
4218 prevchar_isitem = TRUE;
4219 if (l > maxwid)
4220 {
4221 while (l >= maxwid)
4222#ifdef FEAT_MBYTE
4223 if (has_mbyte)
4224 {
4225 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004226 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 }
4228 else
4229#endif
4230 l -= byte2cells(*t++);
4231 if (p + 1 >= out + outlen)
4232 break;
4233 *p++ = '<';
4234 }
4235 if (minwid > 0)
4236 {
4237 for (; l < minwid && p + 1 < out + outlen; l++)
4238 {
4239 /* Don't put a "-" in front of a digit. */
4240 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4241 *p++ = ' ';
4242 else
4243 *p++ = fillchar;
4244 }
4245 minwid = 0;
4246 }
4247 else
4248 minwid *= -1;
4249 while (*t && p + 1 < out + outlen)
4250 {
4251 *p++ = *t++;
4252 /* Change a space by fillchar, unless fillchar is '-' and a
4253 * digit follows. */
4254 if (fillable && p[-1] == ' '
4255 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4256 p[-1] = fillchar;
4257 }
4258 for (; l < minwid && p + 1 < out + outlen; l++)
4259 *p++ = fillchar;
4260 }
4261 else if (num >= 0)
4262 {
4263 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4264 char_u nstr[20];
4265
4266 if (p + 20 >= out + outlen)
4267 break; /* not sufficient space */
4268 prevchar_isitem = TRUE;
4269 t = nstr;
4270 if (opt == STL_VIRTCOL_ALT)
4271 {
4272 *t++ = '-';
4273 minwid--;
4274 }
4275 *t++ = '%';
4276 if (zeropad)
4277 *t++ = '0';
4278 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004279 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 *t = 0;
4281
4282 for (n = num, l = 1; n >= nbase; n /= nbase)
4283 l++;
4284 if (opt == STL_VIRTCOL_ALT)
4285 l++;
4286 if (l > maxwid)
4287 {
4288 l += 2;
4289 n = l - maxwid;
4290 while (l-- > maxwid)
4291 num /= nbase;
4292 *t++ = '>';
4293 *t++ = '%';
4294 *t = t[-3];
4295 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004296 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4297 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004300 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4301 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 p += STRLEN(p);
4303 }
4304 else
4305 item[curitem].type = Empty;
4306
4307 if (opt == STL_VIM_EXPR)
4308 vim_free(str);
4309
4310 if (num >= 0 || (!itemisflag && str && *str))
4311 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4312 curitem++;
4313 }
4314 *p = NUL;
4315 itemcnt = curitem;
4316
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004317#ifdef FEAT_EVAL
4318 if (usefmt != fmt)
4319 vim_free(usefmt);
4320#endif
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 width = vim_strsize(out);
4323 if (maxwidth > 0 && width > maxwidth)
4324 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004325 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 l = 0;
4327 if (itemcnt == 0)
4328 s = out;
4329 else
4330 {
4331 for ( ; l < itemcnt; l++)
4332 if (item[l].type == Trunc)
4333 {
4334 /* Truncate at %< item. */
4335 s = item[l].start;
4336 break;
4337 }
4338 if (l == itemcnt)
4339 {
4340 /* No %< item, truncate first item. */
4341 s = item[0].start;
4342 l = 0;
4343 }
4344 }
4345
4346 if (width - vim_strsize(s) >= maxwidth)
4347 {
4348 /* Truncation mark is beyond max length */
4349#ifdef FEAT_MBYTE
4350 if (has_mbyte)
4351 {
4352 s = out;
4353 width = 0;
4354 for (;;)
4355 {
4356 width += ptr2cells(s);
4357 if (width >= maxwidth)
4358 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004359 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 /* Fill up for half a double-wide character. */
4362 while (++width < maxwidth)
4363 *s++ = fillchar;
4364 }
4365 else
4366#endif
4367 s = out + maxwidth - 1;
4368 for (l = 0; l < itemcnt; l++)
4369 if (item[l].start > s)
4370 break;
4371 itemcnt = l;
4372 *s++ = '>';
4373 *s = 0;
4374 }
4375 else
4376 {
4377#ifdef FEAT_MBYTE
4378 if (has_mbyte)
4379 {
4380 n = 0;
4381 while (width >= maxwidth)
4382 {
4383 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004384 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 }
4386 }
4387 else
4388#endif
4389 n = width - maxwidth + 1;
4390 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004391 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 *s = '<';
4393
4394 /* Fill up for half a double-wide character. */
4395 while (++width < maxwidth)
4396 {
4397 s = s + STRLEN(s);
4398 *s++ = fillchar;
4399 *s = NUL;
4400 }
4401
4402 --n; /* count the '<' */
4403 for (; l < itemcnt; l++)
4404 {
4405 if (item[l].start - n >= s)
4406 item[l].start -= n;
4407 else
4408 item[l].start = s;
4409 }
4410 }
4411 width = maxwidth;
4412 }
4413 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4414 {
4415 /* Apply STL_MIDDLE if any */
4416 for (l = 0; l < itemcnt; l++)
4417 if (item[l].type == Middle)
4418 break;
4419 if (l < itemcnt)
4420 {
4421 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004422 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 for (s = item[l].start; s < p; s++)
4424 *s = fillchar;
4425 for (l++; l < itemcnt; l++)
4426 item[l].start += maxwidth - width;
4427 width = maxwidth;
4428 }
4429 }
4430
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004431 /* Store the info about highlighting. */
4432 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004434 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 for (l = 0; l < itemcnt; l++)
4436 {
4437 if (item[l].type == Highlight)
4438 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004439 sp->start = item[l].start;
4440 sp->userhl = item[l].minwid;
4441 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004444 sp->start = NULL;
4445 sp->userhl = 0;
4446 }
4447
4448 /* Store the info about tab pages labels. */
4449 if (tabtab != NULL)
4450 {
4451 sp = tabtab;
4452 for (l = 0; l < itemcnt; l++)
4453 {
4454 if (item[l].type == TabPage)
4455 {
4456 sp->start = item[l].start;
4457 sp->userhl = item[l].minwid;
4458 sp++;
4459 }
4460 }
4461 sp->start = NULL;
4462 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464
4465 return width;
4466}
4467#endif /* FEAT_STL_OPT */
4468
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004469#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4470 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004472 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4473 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 */
4475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004476get_rel_pos(
4477 win_T *wp,
4478 char_u *buf,
4479 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480{
4481 long above; /* number of lines above window */
4482 long below; /* number of lines below window */
4483
Bram Moolenaar0027c212015-01-07 13:31:52 +01004484 if (buflen < 3) /* need at least 3 chars for writing */
4485 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 above = wp->w_topline - 1;
4487#ifdef FEAT_DIFF
4488 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004489 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4490 above = 0; /* All buffer lines are displayed and there is an
4491 * indication of filler lines, that can be considered
4492 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493#endif
4494 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4495 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004496 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4497 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004499 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004501 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 ? (int)(above / ((above + below) / 100L))
4503 : (int)(above * 100L / (above + below)));
4504}
4505#endif
4506
4507/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004508 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 * Return TRUE if it was appended.
4510 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004511 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004512append_arg_number(
4513 win_T *wp,
4514 char_u *buf,
4515 int buflen,
4516 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517{
4518 char_u *p;
4519
4520 if (ARGCOUNT <= 1) /* nothing to do */
4521 return FALSE;
4522
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004523 p = buf + STRLEN(buf); /* go to the end of the buffer */
4524 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 return FALSE;
4526 *p++ = ' ';
4527 *p++ = '(';
4528 if (add_file)
4529 {
4530 STRCPY(p, "file ");
4531 p += 5;
4532 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004533 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4534 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4536 return TRUE;
4537}
4538
4539/*
4540 * If fname is not a full path, make it a full path.
4541 * Returns pointer to allocated memory (NULL for failure).
4542 */
4543 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004544fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
4546 /*
4547 * Force expanding the path always for Unix, because symbolic links may
4548 * mess up the full path name, even though it starts with a '/'.
4549 * Also expand when there is ".." in the file name, try to remove it,
4550 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004551 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 * For MS-Windows also expand names like "longna~1" to "longname".
4553 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004554#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 return FullName_save(fname, TRUE);
4556#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004557 if (!vim_isAbsName(fname)
4558 || strstr((char *)fname, "..") != NULL
4559 || strstr((char *)fname, "//") != NULL
4560# ifdef BACKSLASH_IN_FILENAME
4561 || strstr((char *)fname, "\\\\") != NULL
4562# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004563# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004565# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 )
4567 return FullName_save(fname, FALSE);
4568
4569 fname = vim_strsave(fname);
4570
Bram Moolenaar9b942202007-10-03 12:31:33 +00004571# ifdef USE_FNAME_CASE
4572# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004574# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
4576 if (fname != NULL)
4577 fname_case(fname, 0); /* set correct case for file name */
4578 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580
4581 return fname;
4582#endif
4583}
4584
4585/*
4586 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4587 * "ffname" becomes a pointer to allocated memory (or NULL).
4588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004590fname_expand(
4591 buf_T *buf UNUSED,
4592 char_u **ffname,
4593 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594{
4595 if (*ffname == NULL) /* if no file name given, nothing to do */
4596 return;
4597 if (*sfname == NULL) /* if no short file name given, use ffname */
4598 *sfname = *ffname;
4599 *ffname = fix_fname(*ffname); /* expand to full path */
4600
4601#ifdef FEAT_SHORTCUT
4602 if (!buf->b_p_bin)
4603 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004604 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
4606 /* If the file name is a shortcut file, use the file it links to. */
4607 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004608 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 {
4610 vim_free(*ffname);
4611 *ffname = rfname;
4612 *sfname = rfname;
4613 }
4614 }
4615#endif
4616}
4617
4618/*
4619 * Get the file name for an argument list entry.
4620 */
4621 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004622alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623{
4624 buf_T *bp;
4625
4626 /* Use the name from the associated buffer if it exists. */
4627 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004628 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 return aep->ae_fname;
4630 return bp->b_fname;
4631}
4632
4633#if defined(FEAT_WINDOWS) || defined(PROTO)
4634/*
4635 * do_arg_all(): Open up to 'count' windows, one for each argument.
4636 */
4637 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004638do_arg_all(
4639 int count,
4640 int forceit, /* hide buffers in current windows */
4641 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642{
4643 int i;
4644 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004645 char_u *opened; /* Array of weight for which args are open:
4646 * 0: not opened
4647 * 1: opened in other tab
4648 * 2: opened in curtab
4649 * 3: opened in curtab and curwin
4650 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004651 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 int use_firstwin = FALSE; /* use first window for arglist */
4653 int split_ret = OK;
4654 int p_ea_save;
4655 alist_T *alist; /* argument list to be used */
4656 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004657 tabpage_T *tpnext;
4658 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004659 win_T *old_curwin, *last_curwin;
4660 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004661 win_T *new_curwin = NULL;
4662 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663
4664 if (ARGCOUNT <= 0)
4665 {
4666 /* Don't give an error message. We don't want it when the ":all"
4667 * command is in the .vimrc. */
4668 return;
4669 }
4670 setpcmark();
4671
4672 opened_len = ARGCOUNT;
4673 opened = alloc_clear((unsigned)opened_len);
4674 if (opened == NULL)
4675 return;
4676
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004677 /* Autocommands may do anything to the argument list. Make sure it's not
4678 * freed while we are working here by "locking" it. We still have to
4679 * watch out for its size to be changed. */
4680 alist = curwin->w_alist;
4681 ++alist->al_refcount;
4682
4683 old_curwin = curwin;
4684 old_curtab = curtab;
4685
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004686# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689
4690 /*
4691 * Try closing all windows that are not in the argument list.
4692 * Also close windows that are not full width;
4693 * When 'hidden' or "forceit" set the buffer becomes hidden.
4694 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004695 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004697 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004698 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004699 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004701 tpnext = curtab->tp_next;
4702 for (wp = firstwin; wp != NULL; wp = wpnext)
4703 {
4704 wpnext = wp->w_next;
4705 buf = wp->w_buffer;
4706 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004707 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004708 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004709 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004710 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004712 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004713 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004715 if (i < alist->al_ga.ga_len
4716 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4717 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4718 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004720 int weight = 1;
4721
4722 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004723 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004724 ++weight;
4725 if (old_curwin == wp)
4726 ++weight;
4727 }
4728
4729 if (weight > (int)opened[i])
4730 {
4731 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004732 if (i == 0)
4733 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004734 if (new_curwin != NULL)
4735 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004736 new_curwin = wp;
4737 new_curtab = curtab;
4738 }
4739 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004740 else if (keep_tabs)
4741 i = opened_len;
4742
4743 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004744 {
4745 /* Use the current argument list for all windows
4746 * containing a file from it. */
4747 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004748 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004749 ++wp->w_alist->al_refcount;
4750 }
4751 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004755 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004757 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004759 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004760 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004762 /* If the buffer was changed, and we would like to hide it,
4763 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004764 if (!P_HID(buf) && buf->b_nwindows <= 1
4765 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004767#ifdef FEAT_AUTOCMD
4768 bufref_T bufref;
4769
4770 set_bufref(&bufref, buf);
4771#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004772 (void)autowrite(buf, FALSE);
4773#ifdef FEAT_AUTOCMD
4774 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004775 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004776 {
4777 wpnext = firstwin; /* start all over... */
4778 continue;
4779 }
4780#endif
4781 }
4782#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004783 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004784 if (firstwin == lastwin
4785 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004786#endif
4787 use_firstwin = TRUE;
4788#ifdef FEAT_WINDOWS
4789 else
4790 {
4791 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4792# ifdef FEAT_AUTOCMD
4793 /* check if autocommands removed the next window */
4794 if (!win_valid(wpnext))
4795 wpnext = firstwin; /* start all over... */
4796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798#endif
4799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
4801 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004802
4803 /* Without the ":tab" modifier only do the current tab page. */
4804 if (had_tab == 0 || tpnext == NULL)
4805 break;
4806
4807# ifdef FEAT_AUTOCMD
4808 /* check if autocommands removed the next tab page */
4809 if (!valid_tabpage(tpnext))
4810 tpnext = first_tabpage; /* start all over...*/
4811# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004812 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
4814
4815 /*
4816 * Open a window for files in the argument list that don't have one.
4817 * ARGCOUNT may change while doing this, because of autocommands.
4818 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004819 if (count > opened_len || count <= 0)
4820 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821
4822#ifdef FEAT_AUTOCMD
4823 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4824 ++autocmd_no_enter;
4825 ++autocmd_no_leave;
4826#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004827 last_curwin = curwin;
4828 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004830#ifdef FEAT_WINDOWS
4831 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4832 * leaving an empty tab page when executed locally. */
4833 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4834 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4835 use_firstwin = TRUE;
4836#endif
4837
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004838 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
4840 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4841 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004842 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
4844 /* Move the already present window to below the current window */
4845 if (curwin->w_arg_idx != i)
4846 {
4847 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4848 {
4849 if (wpnext->w_arg_idx == i)
4850 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004851 if (keep_tabs)
4852 {
4853 new_curwin = wpnext;
4854 new_curtab = curtab;
4855 }
4856 else
4857 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 break;
4859 }
4860 }
4861 }
4862 }
4863 else if (split_ret == OK)
4864 {
4865 if (!use_firstwin) /* split current window */
4866 {
4867 p_ea_save = p_ea;
4868 p_ea = TRUE; /* use space from all windows */
4869 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4870 p_ea = p_ea_save;
4871 if (split_ret == FAIL)
4872 continue;
4873 }
4874#ifdef FEAT_AUTOCMD
4875 else /* first window: do autocmd for leaving this buffer */
4876 --autocmd_no_leave;
4877#endif
4878
4879 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004880 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 */
4882 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004883 if (i == 0)
4884 {
4885 new_curwin = curwin;
4886 new_curtab = curtab;
4887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4889 ECMD_ONE,
4890 ((P_HID(curwin->w_buffer)
4891 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004892 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#ifdef FEAT_AUTOCMD
4894 if (use_firstwin)
4895 ++autocmd_no_leave;
4896#endif
4897 use_firstwin = FALSE;
4898 }
4899 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004900
4901 /* When ":tab" was used open a new tab for a new window repeatedly. */
4902 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4903 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 }
4905
4906 /* Remove the "lock" on the argument list. */
4907 alist_unlink(alist);
4908
4909#ifdef FEAT_AUTOCMD
4910 --autocmd_no_enter;
4911#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004912 /* restore last referenced tabpage's curwin */
4913 if (last_curtab != new_curtab)
4914 {
4915 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004916 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004917 if (win_valid(last_curwin))
4918 win_enter(last_curwin, FALSE);
4919 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004920 /* to window with first arg */
4921 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004922 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004923 if (win_valid(new_curwin))
4924 win_enter(new_curwin, FALSE);
4925
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926#ifdef FEAT_AUTOCMD
4927 --autocmd_no_leave;
4928#endif
4929 vim_free(opened);
4930}
4931
4932# if defined(FEAT_LISTCMDS) || defined(PROTO)
4933/*
4934 * Open a window for a number of buffers.
4935 */
4936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004937ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938{
4939 buf_T *buf;
4940 win_T *wp, *wpnext;
4941 int split_ret = OK;
4942 int p_ea_save;
4943 int open_wins = 0;
4944 int r;
4945 int count; /* Maximum number of windows to open. */
4946 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004947#ifdef FEAT_WINDOWS
4948 int had_tab = cmdmod.tab;
4949 tabpage_T *tpnext;
4950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951
4952 if (eap->addr_count == 0) /* make as many windows as possible */
4953 count = 9999;
4954 else
4955 count = eap->line2; /* make as many windows as specified */
4956 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4957 all = FALSE;
4958 else
4959 all = TRUE;
4960
4961 setpcmark();
4962
4963#ifdef FEAT_GUI
4964 need_mouse_correct = TRUE;
4965#endif
4966
4967 /*
4968 * Close superfluous windows (two windows for the same buffer).
4969 * Also close windows that are not full-width.
4970 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004971#ifdef FEAT_WINDOWS
4972 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004973 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004974 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004977 tpnext = curtab->tp_next;
4978 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004980 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004981 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004982#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004983 || ((cmdmod.split & WSP_VERT)
4984 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004985 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004986 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004987 || (had_tab > 0 && wp != firstwin)
4988#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004989 ) && firstwin != lastwin
4990#ifdef FEAT_AUTOCMD
4991 && !(wp->w_closing || wp->w_buffer->b_closing)
4992#endif
4993 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004994 {
4995 win_close(wp, FALSE);
4996#ifdef FEAT_AUTOCMD
4997 wpnext = firstwin; /* just in case an autocommand does
4998 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004999 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005000 open_wins = 0;
5001#endif
5002 }
5003 else
5004 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005006
5007#ifdef FEAT_WINDOWS
5008 /* Without the ":tab" modifier only do the current tab page. */
5009 if (had_tab == 0 || tpnext == NULL)
5010 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005011 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
5015 /*
5016 * Go through the buffer list. When a buffer doesn't have a window yet,
5017 * open one. Otherwise move the window to the right position.
5018 * Watch out for autocommands that delete buffers or windows!
5019 */
5020#ifdef FEAT_AUTOCMD
5021 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5022 ++autocmd_no_enter;
5023#endif
5024 win_enter(lastwin, FALSE);
5025#ifdef FEAT_AUTOCMD
5026 ++autocmd_no_leave;
5027#endif
5028 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5029 {
5030 /* Check if this buffer needs a window */
5031 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5032 continue;
5033
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005034#ifdef FEAT_WINDOWS
5035 if (had_tab != 0)
5036 {
5037 /* With the ":tab" modifier don't move the window. */
5038 if (buf->b_nwindows > 0)
5039 wp = lastwin; /* buffer has a window, skip it */
5040 else
5041 wp = NULL;
5042 }
5043 else
5044#endif
5045 {
5046 /* Check if this buffer already has a window */
5047 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5048 if (wp->w_buffer == buf)
5049 break;
5050 /* If the buffer already has a window, move it */
5051 if (wp != NULL)
5052 win_move_after(wp, curwin);
5053 }
5054
5055 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005057#ifdef FEAT_AUTOCMD
5058 bufref_T bufref;
5059
5060 set_bufref(&bufref, buf);
5061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 /* Split the window and put the buffer in it */
5063 p_ea_save = p_ea;
5064 p_ea = TRUE; /* use space from all windows */
5065 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5066 ++open_wins;
5067 p_ea = p_ea_save;
5068 if (split_ret == FAIL)
5069 continue;
5070
5071 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005072#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 swap_exists_action = SEA_DIALOG;
5074#endif
5075 set_curbuf(buf, DOBUF_GOTO);
5076#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005077 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005079 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005080#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005082# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
5084 }
5085#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005086#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 if (swap_exists_action == SEA_QUIT)
5088 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005089# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5090 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005091
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005092 /* Reset the error/interrupt/exception state here so that
5093 * aborting() returns FALSE when closing a window. */
5094 enter_cleanup(&cs);
5095# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005096
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005097 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 win_close(curwin, TRUE);
5099 --open_wins;
5100 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005101 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005102
5103# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5104 /* Restore the error/interrupt/exception state if not
5105 * discarded by a new aborting error, interrupt, or uncaught
5106 * exception. */
5107 leave_cleanup(&cs);
5108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110 else
5111 handle_swap_exists(NULL);
5112#endif
5113 }
5114
5115 ui_breakcheck();
5116 if (got_int)
5117 {
5118 (void)vgetc(); /* only break the file loading, not the rest */
5119 break;
5120 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005121#ifdef FEAT_EVAL
5122 /* Autocommands deleted the buffer or aborted script processing!!! */
5123 if (aborting())
5124 break;
5125#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005126#ifdef FEAT_WINDOWS
5127 /* When ":tab" was used open a new tab for a new window repeatedly. */
5128 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5129 cmdmod.tab = 9999;
5130#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 }
5132#ifdef FEAT_AUTOCMD
5133 --autocmd_no_enter;
5134#endif
5135 win_enter(firstwin, FALSE); /* back to first window */
5136#ifdef FEAT_AUTOCMD
5137 --autocmd_no_leave;
5138#endif
5139
5140 /*
5141 * Close superfluous windows.
5142 */
5143 for (wp = lastwin; open_wins > count; )
5144 {
5145 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5146 || autowrite(wp->w_buffer, FALSE) == OK);
5147#ifdef FEAT_AUTOCMD
5148 if (!win_valid(wp))
5149 {
5150 /* BufWrite Autocommands made the window invalid, start over */
5151 wp = lastwin;
5152 }
5153 else
5154#endif
5155 if (r)
5156 {
5157 win_close(wp, !P_HID(wp->w_buffer));
5158 --open_wins;
5159 wp = lastwin;
5160 }
5161 else
5162 {
5163 wp = wp->w_prev;
5164 if (wp == NULL)
5165 break;
5166 }
5167 }
5168}
5169# endif /* FEAT_LISTCMDS */
5170
5171#endif /* FEAT_WINDOWS */
5172
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005173static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005174
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175/*
5176 * do_modelines() - process mode lines for the current file
5177 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005178 * "flags" can be:
5179 * OPT_WINONLY only set options local to window
5180 * OPT_NOWIN don't set options local to window
5181 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 * Returns immediately if the "ml" option isn't set.
5183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005185do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005187 linenr_T lnum;
5188 int nmlines;
5189 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5192 return;
5193
5194 /* Disallow recursive entry here. Can happen when executing a modeline
5195 * triggers an autocommand, which reloads modelines with a ":do". */
5196 if (entered)
5197 return;
5198
5199 ++entered;
5200 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5201 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005202 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 nmlines = 0;
5204
5205 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5206 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005207 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 nmlines = 0;
5209 --entered;
5210}
5211
5212#include "version.h" /* for version number */
5213
5214/*
5215 * chk_modeline() - check a single line for a mode string
5216 * Return FAIL if an error encountered.
5217 */
5218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005219chk_modeline(
5220 linenr_T lnum,
5221 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222{
5223 char_u *s;
5224 char_u *e;
5225 char_u *linecopy; /* local copy of any modeline found */
5226 int prev;
5227 int vers;
5228 int end;
5229 int retval = OK;
5230 char_u *save_sourcing_name;
5231 linenr_T save_sourcing_lnum;
5232#ifdef FEAT_EVAL
5233 scid_T save_SID;
5234#endif
5235
5236 prev = -1;
5237 for (s = ml_get(lnum); *s != NUL; ++s)
5238 {
5239 if (prev == -1 || vim_isspace(prev))
5240 {
5241 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5242 || STRNCMP(s, "vi:", (size_t)3) == 0)
5243 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005244 /* Accept both "vim" and "Vim". */
5245 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246 {
5247 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5248 e = s + 4;
5249 else
5250 e = s + 3;
5251 vers = getdigits(&e);
5252 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005253 && (s[0] != 'V'
5254 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 && (s[3] == ':'
5256 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5257 || (VIM_VERSION_100 < vers && s[3] == '<')
5258 || (VIM_VERSION_100 > vers && s[3] == '>')
5259 || (VIM_VERSION_100 == vers && s[3] == '=')))
5260 break;
5261 }
5262 }
5263 prev = *s;
5264 }
5265
5266 if (*s)
5267 {
5268 do /* skip over "ex:", "vi:" or "vim:" */
5269 ++s;
5270 while (s[-1] != ':');
5271
5272 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5273 if (linecopy == NULL)
5274 return FAIL;
5275
5276 save_sourcing_lnum = sourcing_lnum;
5277 save_sourcing_name = sourcing_name;
5278 sourcing_lnum = lnum; /* prepare for emsg() */
5279 sourcing_name = (char_u *)"modelines";
5280
5281 end = FALSE;
5282 while (end == FALSE)
5283 {
5284 s = skipwhite(s);
5285 if (*s == NUL)
5286 break;
5287
5288 /*
5289 * Find end of set command: ':' or end of line.
5290 * Skip over "\:", replacing it with ":".
5291 */
5292 for (e = s; *e != ':' && *e != NUL; ++e)
5293 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005294 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 if (*e == NUL)
5296 end = TRUE;
5297
5298 /*
5299 * If there is a "set" command, require a terminating ':' and
5300 * ignore the stuff after the ':'.
5301 * "vi:set opt opt opt: foo" -- foo not interpreted
5302 * "vi:opt opt opt: foo" -- foo interpreted
5303 * Accept "se" for compatibility with Elvis.
5304 */
5305 if (STRNCMP(s, "set ", (size_t)4) == 0
5306 || STRNCMP(s, "se ", (size_t)3) == 0)
5307 {
5308 if (*e != ':') /* no terminating ':'? */
5309 break;
5310 end = TRUE;
5311 s = vim_strchr(s, ' ') + 1;
5312 }
5313 *e = NUL; /* truncate the set command */
5314
5315 if (*s != NUL) /* skip over an empty "::" */
5316 {
5317#ifdef FEAT_EVAL
5318 save_SID = current_SID;
5319 current_SID = SID_MODELINE;
5320#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005321 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322#ifdef FEAT_EVAL
5323 current_SID = save_SID;
5324#endif
5325 if (retval == FAIL) /* stop if error found */
5326 break;
5327 }
5328 s = e + 1; /* advance to next part */
5329 }
5330
5331 sourcing_lnum = save_sourcing_lnum;
5332 sourcing_name = save_sourcing_name;
5333
5334 vim_free(linecopy);
5335 }
5336 return retval;
5337}
5338
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005339#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005341read_viminfo_bufferlist(
5342 vir_T *virp,
5343 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344{
5345 char_u *tab;
5346 linenr_T lnum;
5347 colnr_T col;
5348 buf_T *buf;
5349 char_u *sfname;
5350 char_u *xline;
5351
5352 /* Handle long line and escaped characters. */
5353 xline = viminfo_readstring(virp, 1, FALSE);
5354
5355 /* don't read in if there are files on the command-line or if writing: */
5356 if (xline != NULL && !writing && ARGCOUNT == 0
5357 && find_viminfo_parameter('%') != NULL)
5358 {
5359 /* Format is: <fname> Tab <lnum> Tab <col>.
5360 * Watch out for a Tab in the file name, work from the end. */
5361 lnum = 0;
5362 col = 0;
5363 tab = vim_strrchr(xline, '\t');
5364 if (tab != NULL)
5365 {
5366 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005367 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005368 tab = vim_strrchr(xline, '\t');
5369 if (tab != NULL)
5370 {
5371 *tab++ = '\0';
5372 lnum = atol((char *)tab);
5373 }
5374 }
5375
5376 /* Expand "~/" in the file name at "line + 1" to a full path.
5377 * Then try shortening it by comparing with the current directory */
5378 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005379 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380
5381 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5382 if (buf != NULL) /* just in case... */
5383 {
5384 buf->b_last_cursor.lnum = lnum;
5385 buf->b_last_cursor.col = col;
5386 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5387 }
5388 }
5389 vim_free(xline);
5390
5391 return viminfo_readline(virp);
5392}
5393
5394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005395write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396{
5397 buf_T *buf;
5398#ifdef FEAT_WINDOWS
5399 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005400 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401#endif
5402 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005403 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404
5405 if (find_viminfo_parameter('%') == NULL)
5406 return;
5407
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005408 /* Without a number -1 is returned: do all buffers. */
5409 max_buffers = get_viminfo_parameter('%');
5410
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005412#define LINE_BUF_LEN (MAXPATHL + 40)
5413 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 if (line == NULL)
5415 return;
5416
5417#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005418 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 set_last_cursor(win);
5420#else
5421 set_last_cursor(curwin);
5422#endif
5423
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005424 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5426 {
5427 if (buf->b_fname == NULL
5428 || !buf->b_p_bl
5429#ifdef FEAT_QUICKFIX
5430 || bt_quickfix(buf)
5431#endif
5432 || removable(buf->b_ffname))
5433 continue;
5434
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005435 if (max_buffers-- == 0)
5436 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 putc('%', fp);
5438 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005439 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440 (long)buf->b_last_cursor.lnum,
5441 buf->b_last_cursor.col);
5442 viminfo_writestring(fp, line);
5443 }
5444 vim_free(line);
5445}
5446#endif
5447
5448
5449/*
5450 * Return special buffer name.
5451 * Returns NULL when the buffer has a normal file name.
5452 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005453 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005454buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455{
5456#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5457 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005458 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005459 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005460 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005461
5462 /*
5463 * For location list window, w_llist_ref points to the location list.
5464 * For quickfix window, w_llist_ref is NULL.
5465 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005466 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005467 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005468 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005469 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471#endif
5472#ifdef FEAT_QUICKFIX
5473 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5474 * contains the name as specified by the user */
5475 if (bt_nofile(buf))
5476 {
5477 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005478 return buf->b_sfname;
5479 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
5481#endif
5482 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005483 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 return NULL;
5485}
5486
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005487#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5488 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5489 || defined(PROTO)
5490/*
5491 * Find a window for buffer "buf".
5492 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5493 * If not found FAIL is returned.
5494 */
5495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005496find_win_for_buf(
5497 buf_T *buf,
5498 win_T **wp,
5499 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005500{
5501 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5502 if ((*wp)->w_buffer == buf)
5503 goto win_found;
5504 return FAIL;
5505win_found:
5506 return OK;
5507}
5508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
5510#if defined(FEAT_SIGNS) || defined(PROTO)
5511/*
5512 * Insert the sign into the signlist.
5513 */
5514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005515insert_sign(
5516 buf_T *buf, /* buffer to store sign in */
5517 signlist_T *prev, /* previous sign entry */
5518 signlist_T *next, /* next sign entry */
5519 int id, /* sign ID */
5520 linenr_T lnum, /* line number which gets the mark */
5521 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522{
5523 signlist_T *newsign;
5524
5525 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5526 if (newsign != NULL)
5527 {
5528 newsign->id = id;
5529 newsign->lnum = lnum;
5530 newsign->typenr = typenr;
5531 newsign->next = next;
5532#ifdef FEAT_NETBEANS_INTG
5533 newsign->prev = prev;
5534 if (next != NULL)
5535 next->prev = newsign;
5536#endif
5537
5538 if (prev == NULL)
5539 {
5540 /* When adding first sign need to redraw the windows to create the
5541 * column for signs. */
5542 if (buf->b_signlist == NULL)
5543 {
5544 redraw_buf_later(buf, NOT_VALID);
5545 changed_cline_bef_curs();
5546 }
5547
5548 /* first sign in signlist */
5549 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005550#ifdef FEAT_NETBEANS_INTG
5551 if (netbeans_active())
5552 buf->b_has_sign_column = TRUE;
5553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 }
5555 else
5556 prev->next = newsign;
5557 }
5558}
5559
5560/*
5561 * Add the sign into the signlist. Find the right spot to do it though.
5562 */
5563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005564buf_addsign(
5565 buf_T *buf, /* buffer to store sign in */
5566 int id, /* sign ID */
5567 linenr_T lnum, /* line number which gets the mark */
5568 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569{
5570 signlist_T *sign; /* a sign in the signlist */
5571 signlist_T *prev; /* the previous sign */
5572
5573 prev = NULL;
5574 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5575 {
5576 if (lnum == sign->lnum && id == sign->id)
5577 {
5578 sign->typenr = typenr;
5579 return;
5580 }
5581 else if (
5582#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5583 id < 0 &&
5584#endif
5585 lnum < sign->lnum)
5586 {
5587#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5588 /* XXX - GRP: Is this because of sign slide problem? Or is it
5589 * really needed? Or is it because we allow multiple signs per
5590 * line? If so, should I add that feature to FEAT_SIGNS?
5591 */
5592 while (prev != NULL && prev->lnum == lnum)
5593 prev = prev->prev;
5594 if (prev == NULL)
5595 sign = buf->b_signlist;
5596 else
5597 sign = prev->next;
5598#endif
5599 insert_sign(buf, prev, sign, id, lnum, typenr);
5600 return;
5601 }
5602 prev = sign;
5603 }
5604#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5605 /* XXX - GRP: See previous comment */
5606 while (prev != NULL && prev->lnum == lnum)
5607 prev = prev->prev;
5608 if (prev == NULL)
5609 sign = buf->b_signlist;
5610 else
5611 sign = prev->next;
5612#endif
5613 insert_sign(buf, prev, sign, id, lnum, typenr);
5614
5615 return;
5616}
5617
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005618/*
5619 * For an existing, placed sign "markId" change the type to "typenr".
5620 * Returns the line number of the sign, or zero if the sign is not found.
5621 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005622 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005623buf_change_sign_type(
5624 buf_T *buf, /* buffer to store sign in */
5625 int markId, /* sign ID */
5626 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 signlist_T *sign; /* a sign in the signlist */
5629
5630 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5631 {
5632 if (sign->id == markId)
5633 {
5634 sign->typenr = typenr;
5635 return sign->lnum;
5636 }
5637 }
5638
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005639 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640}
5641
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005642 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005643buf_getsigntype(
5644 buf_T *buf,
5645 linenr_T lnum,
5646 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647{
5648 signlist_T *sign; /* a sign in a b_signlist */
5649
5650 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5651 if (sign->lnum == lnum
5652 && (type == SIGN_ANY
5653# ifdef FEAT_SIGN_ICONS
5654 || (type == SIGN_ICON
5655 && sign_get_image(sign->typenr) != NULL)
5656# endif
5657 || (type == SIGN_TEXT
5658 && sign_get_text(sign->typenr) != NULL)
5659 || (type == SIGN_LINEHL
5660 && sign_get_attr(sign->typenr, TRUE) != 0)))
5661 return sign->typenr;
5662 return 0;
5663}
5664
5665
5666 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005667buf_delsign(
5668 buf_T *buf, /* buffer sign is stored in */
5669 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670{
5671 signlist_T **lastp; /* pointer to pointer to current sign */
5672 signlist_T *sign; /* a sign in a b_signlist */
5673 signlist_T *next; /* the next sign in a b_signlist */
5674 linenr_T lnum; /* line number whose sign was deleted */
5675
5676 lastp = &buf->b_signlist;
5677 lnum = 0;
5678 for (sign = buf->b_signlist; sign != NULL; sign = next)
5679 {
5680 next = sign->next;
5681 if (sign->id == id)
5682 {
5683 *lastp = next;
5684#ifdef FEAT_NETBEANS_INTG
5685 if (next != NULL)
5686 next->prev = sign->prev;
5687#endif
5688 lnum = sign->lnum;
5689 vim_free(sign);
5690 break;
5691 }
5692 else
5693 lastp = &sign->next;
5694 }
5695
5696 /* When deleted the last sign need to redraw the windows to remove the
5697 * sign column. */
5698 if (buf->b_signlist == NULL)
5699 {
5700 redraw_buf_later(buf, NOT_VALID);
5701 changed_cline_bef_curs();
5702 }
5703
5704 return lnum;
5705}
5706
5707
5708/*
5709 * Find the line number of the sign with the requested id. If the sign does
5710 * not exist, return 0 as the line number. This will still let the correct file
5711 * get loaded.
5712 */
5713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005714buf_findsign(
5715 buf_T *buf, /* buffer to store sign in */
5716 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717{
5718 signlist_T *sign; /* a sign in the signlist */
5719
5720 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5721 if (sign->id == id)
5722 return sign->lnum;
5723
5724 return 0;
5725}
5726
5727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005728buf_findsign_id(
5729 buf_T *buf, /* buffer whose sign we are searching for */
5730 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731{
5732 signlist_T *sign; /* a sign in the signlist */
5733
5734 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5735 if (sign->lnum == lnum)
5736 return sign->id;
5737
5738 return 0;
5739}
5740
5741
5742# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5743/* see if a given type of sign exists on a specific line */
5744 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005745buf_findsigntype_id(
5746 buf_T *buf, /* buffer whose sign we are searching for */
5747 linenr_T lnum, /* line number of sign */
5748 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749{
5750 signlist_T *sign; /* a sign in the signlist */
5751
5752 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5753 if (sign->lnum == lnum && sign->typenr == typenr)
5754 return sign->id;
5755
5756 return 0;
5757}
5758
5759
5760# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5761/* return the number of icons on the given line */
5762 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005763buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764{
5765 signlist_T *sign; /* a sign in the signlist */
5766 int count = 0;
5767
5768 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5769 if (sign->lnum == lnum)
5770 if (sign_get_image(sign->typenr) != NULL)
5771 count++;
5772
5773 return count;
5774}
5775# endif /* FEAT_SIGN_ICONS */
5776# endif /* FEAT_NETBEANS_INTG */
5777
5778
5779/*
5780 * Delete signs in buffer "buf".
5781 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005782 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005783buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784{
5785 signlist_T *next;
5786
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005787 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005788 * sign column. Not when curwin is NULL (this means we're exiting). */
5789 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005790 {
5791 redraw_buf_later(buf, NOT_VALID);
5792 changed_cline_bef_curs();
5793 }
5794
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 while (buf->b_signlist != NULL)
5796 {
5797 next = buf->b_signlist->next;
5798 vim_free(buf->b_signlist);
5799 buf->b_signlist = next;
5800 }
5801}
5802
5803/*
5804 * Delete all signs in all buffers.
5805 */
5806 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005807buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808{
5809 buf_T *buf; /* buffer we are checking for signs */
5810
5811 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5812 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814}
5815
5816/*
5817 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5818 */
5819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005820sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821{
5822 buf_T *buf;
5823 signlist_T *p;
5824 char lbuf[BUFSIZ];
5825
5826 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5827 msg_putchar('\n');
5828 if (rbuf == NULL)
5829 buf = firstbuf;
5830 else
5831 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005832 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 {
5834 if (buf->b_signlist != NULL)
5835 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005836 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5838 msg_putchar('\n');
5839 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005840 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005842 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5844 MSG_PUTS(lbuf);
5845 msg_putchar('\n');
5846 }
5847 if (rbuf != NULL)
5848 break;
5849 buf = buf->b_next;
5850 }
5851}
5852
5853/*
5854 * Adjust a placed sign for inserted/deleted lines.
5855 */
5856 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005857sign_mark_adjust(
5858 linenr_T line1,
5859 linenr_T line2,
5860 long amount,
5861 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862{
5863 signlist_T *sign; /* a sign in a b_signlist */
5864
5865 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5866 {
5867 if (sign->lnum >= line1 && sign->lnum <= line2)
5868 {
5869 if (amount == MAXLNUM)
5870 sign->lnum = line1;
5871 else
5872 sign->lnum += amount;
5873 }
5874 else if (sign->lnum > line2)
5875 sign->lnum += amount_after;
5876 }
5877}
5878#endif /* FEAT_SIGNS */
5879
5880/*
5881 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5882 */
5883 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005884set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885{
5886 if (on != curbuf->b_p_bl)
5887 {
5888 curbuf->b_p_bl = on;
5889#ifdef FEAT_AUTOCMD
5890 if (on)
5891 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5892 else
5893 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5894#endif
5895 }
5896}
5897
5898/*
5899 * Read the file for "buf" again and check if the contents changed.
5900 * Return TRUE if it changed or this could not be checked.
5901 */
5902 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005903buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904{
5905 buf_T *newbuf;
5906 int differ = TRUE;
5907 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005908 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 exarg_T ea;
5910
5911 /* Allocate a buffer without putting it in the buffer list. */
5912 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5913 if (newbuf == NULL)
5914 return TRUE;
5915
5916 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5917 if (prep_exarg(&ea, buf) == FAIL)
5918 {
5919 wipe_buffer(newbuf, FALSE);
5920 return TRUE;
5921 }
5922
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 /* set curwin/curbuf to buf and save a few things */
5924 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925
Bram Moolenaar4770d092006-01-12 23:22:24 +00005926 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 && readfile(buf->b_ffname, buf->b_fname,
5928 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5929 &ea, READ_NEW | READ_DUMMY) == OK)
5930 {
5931 /* compare the two files line by line */
5932 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5933 {
5934 differ = FALSE;
5935 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5936 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5937 {
5938 differ = TRUE;
5939 break;
5940 }
5941 }
5942 }
5943 vim_free(ea.cmd);
5944
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 /* restore curwin/curbuf and a few other things */
5946 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948 if (curbuf != newbuf) /* safety check */
5949 wipe_buffer(newbuf, FALSE);
5950
5951 return differ;
5952}
5953
5954/*
5955 * Wipe out a buffer and decrement the last buffer number if it was used for
5956 * this buffer. Call this to wipe out a temp buffer that does not contain any
5957 * marks.
5958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005960wipe_buffer(
5961 buf_T *buf,
5962 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005963{
5964 if (buf->b_fnum == top_file_num - 1)
5965 --top_file_num;
5966
5967#ifdef FEAT_AUTOCMD
5968 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005969 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005971 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972#ifdef FEAT_AUTOCMD
5973 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005974 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975#endif
5976}