blob: 9511d615976c60b598126d6f943c8670a3c2a49e [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
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +020086 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#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. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200132 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133 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 */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200287 if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 {
289 aco_save_T aco;
290
291 /* Go to the buffer that was opened. */
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200292 aucmd_prepbuf(&aco, old_curbuf.br_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000293#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 Moolenaar7c0a2f32016-07-10 22:11:16 +0200812 bufref_T old_curbuf;
813
814 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815
816 swap_exists_action = SEA_DIALOG;
817# endif
818 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
819 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000820# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
822 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000823# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
824 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000825
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000826 /* Reset the error/interrupt/exception state here so that
827 * aborting() returns FALSE when closing a window. */
828 enter_cleanup(&cs);
829# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000830
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000831 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 win_close(curwin, TRUE);
833 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000834 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000835
836# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
837 /* Restore the error/interrupt/exception state if not discarded by a
838 * new aborting error, interrupt, or uncaught exception. */
839 leave_cleanup(&cs);
840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841 }
842 else
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200843 handle_swap_exists(&old_curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844# endif
845}
846#endif
847
Bram Moolenaare64ac772005-12-07 20:54:59 +0000848#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849/*
850 * Handle the situation of swap_exists_action being set.
851 * It is allowed for "old_curbuf" to be NULL or invalid.
852 */
853 void
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200854handle_swap_exists(bufref_T *old_curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000856# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
857 cleanup_T cs;
858# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100859#ifdef FEAT_SYN_HL
860 long old_tw = curbuf->b_p_tw;
861#endif
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200862 buf_T *buf;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (swap_exists_action == SEA_QUIT)
865 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000866# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
867 /* Reset the error/interrupt/exception state here so that
868 * aborting() returns FALSE when closing a buffer. */
869 enter_cleanup(&cs);
870# endif
871
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 /* User selected Quit at ATTENTION prompt. Go back to previous
873 * buffer. If that buffer is gone or the same as the current one,
874 * open a new, empty buffer. */
875 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000876 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100877 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200878 if (old_curbuf == NULL || !bufref_valid(old_curbuf)
879 || old_curbuf->br_buf == curbuf)
880 buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
881 else
882 buf = old_curbuf->br_buf;
883 if (buf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100884 {
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +0200885 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100886#ifdef FEAT_SYN_HL
887 if (old_tw != curbuf->b_p_tw)
888 check_colorcolumn(curwin);
889#endif
890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000892
893# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
894 /* Restore the error/interrupt/exception state if not discarded by a
895 * new aborting error, interrupt, or uncaught exception. */
896 leave_cleanup(&cs);
897# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 }
899 else if (swap_exists_action == SEA_RECOVER)
900 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000901# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
902 /* Reset the error/interrupt/exception state here so that
903 * aborting() returns FALSE when closing a buffer. */
904 enter_cleanup(&cs);
905# endif
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 /* User selected Recover at ATTENTION prompt. */
908 msg_scroll = TRUE;
909 ml_recover();
910 MSG_PUTS("\n"); /* don't overwrite the last message */
911 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000912 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000913
914# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
915 /* Restore the error/interrupt/exception state if not discarded by a
916 * new aborting error, interrupt, or uncaught exception. */
917 leave_cleanup(&cs);
918# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 }
920 swap_exists_action = SEA_NONE;
921}
922#endif
923
924#if defined(FEAT_LISTCMDS) || defined(PROTO)
925/*
926 * do_bufdel() - delete or unload buffer(s)
927 *
928 * addr_count == 0: ":bdel" - delete current buffer
929 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
930 * buffer "end_bnr", then any other arguments.
931 * addr_count == 2: ":N,N bdel" - delete buffers in range
932 *
933 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
934 * DOBUF_DEL (":bdel")
935 *
936 * Returns error message or NULL
937 */
938 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100939do_bufdel(
940 int command,
941 char_u *arg, /* pointer to extra arguments */
942 int addr_count,
943 int start_bnr, /* first buffer number in a range */
944 int end_bnr, /* buffer nr or last buffer nr in a range */
945 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946{
947 int do_current = 0; /* delete current buffer? */
948 int deleted = 0; /* number of buffers deleted */
949 char_u *errormsg = NULL; /* return value */
950 int bnr; /* buffer number */
951 char_u *p;
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (addr_count == 0)
954 {
955 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
956 }
957 else
958 {
959 if (addr_count == 2)
960 {
961 if (*arg) /* both range and argument is not allowed */
962 return (char_u *)_(e_trailing);
963 bnr = start_bnr;
964 }
965 else /* addr_count == 1 */
966 bnr = end_bnr;
967
968 for ( ;!got_int; ui_breakcheck())
969 {
970 /*
971 * delete the current buffer last, otherwise when the
972 * current buffer is deleted, the next buffer becomes
973 * the current one and will be loaded, which may then
974 * also be deleted, etc.
975 */
976 if (bnr == curbuf->b_fnum)
977 do_current = bnr;
978 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
979 forceit) == OK)
980 ++deleted;
981
982 /*
983 * find next buffer number to delete/unload
984 */
985 if (addr_count == 2)
986 {
987 if (++bnr > end_bnr)
988 break;
989 }
990 else /* addr_count == 1 */
991 {
992 arg = skipwhite(arg);
993 if (*arg == NUL)
994 break;
995 if (!VIM_ISDIGIT(*arg))
996 {
997 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100998 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
999 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 if (bnr < 0) /* failed */
1001 break;
1002 arg = p;
1003 }
1004 else
1005 bnr = getdigits(&arg);
1006 }
1007 }
1008 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
1009 FORWARD, do_current, forceit) == OK)
1010 ++deleted;
1011
1012 if (deleted == 0)
1013 {
1014 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001015 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +00001017 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 else
Bram Moolenaar51485f02005-06-04 21:55:20 +00001019 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 errormsg = IObuff;
1021 }
1022 else if (deleted >= p_report)
1023 {
1024 if (command == DOBUF_UNLOAD)
1025 {
1026 if (deleted == 1)
1027 MSG(_("1 buffer unloaded"));
1028 else
1029 smsg((char_u *)_("%d buffers unloaded"), deleted);
1030 }
1031 else if (command == DOBUF_DEL)
1032 {
1033 if (deleted == 1)
1034 MSG(_("1 buffer deleted"));
1035 else
1036 smsg((char_u *)_("%d buffers deleted"), deleted);
1037 }
1038 else
1039 {
1040 if (deleted == 1)
1041 MSG(_("1 buffer wiped out"));
1042 else
1043 smsg((char_u *)_("%d buffers wiped out"), deleted);
1044 }
1045 }
1046 }
1047
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
1049 return errormsg;
1050}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001051#endif /* FEAT_LISTCMDS */
1052
1053#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1054 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01001056static int empty_curbuf(int close_others, int forceit, int action);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001057
1058/*
1059 * Make the current buffer empty.
1060 * Used when it is wiped out and it's the last buffer.
1061 */
1062 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001063empty_curbuf(
1064 int close_others,
1065 int forceit,
1066 int action)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001067{
1068 int retval;
1069 buf_T *buf = curbuf;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001070 bufref_T bufref;
Bram Moolenaara02471e2014-01-10 16:43:14 +01001071
1072 if (action == DOBUF_UNLOAD)
1073 {
1074 EMSG(_("E90: Cannot unload last buffer"));
1075 return FAIL;
1076 }
1077
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001078 set_bufref(&bufref, buf);
Bram Moolenaara02471e2014-01-10 16:43:14 +01001079#ifdef FEAT_WINDOWS
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001080 if (close_others)
1081 /* Close any other windows on this buffer, then make it empty. */
Bram Moolenaara02471e2014-01-10 16:43:14 +01001082 close_windows(buf, TRUE);
1083#endif
Bram Moolenaara02471e2014-01-10 16:43:14 +01001084
1085 setpcmark();
1086 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1087 forceit ? ECMD_FORCEIT : 0, curwin);
1088
1089 /*
1090 * do_ecmd() may create a new buffer, then we have to delete
1091 * the old one. But do_ecmd() may have done that already, check
1092 * if the buffer still exists.
1093 */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001094 if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001095 close_buffer(NULL, buf, action, FALSE);
1096 if (!close_others)
1097 need_fileinfo = FALSE;
1098 return retval;
1099}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100/*
1101 * Implementation of the commands for the buffer list.
1102 *
1103 * action == DOBUF_GOTO go to specified buffer
1104 * action == DOBUF_SPLIT split window and go to specified buffer
1105 * action == DOBUF_UNLOAD unload specified buffer(s)
1106 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1107 * action == DOBUF_WIPE delete specified buffer(s) really
1108 *
1109 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1110 * start == DOBUF_FIRST go to "count" buffer from first buffer
1111 * start == DOBUF_LAST go to "count" buffer from last buffer
1112 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1113 *
1114 * Return FAIL or OK.
1115 */
1116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001117do_buffer(
1118 int action,
1119 int start,
1120 int dir, /* FORWARD or BACKWARD */
1121 int count, /* buffer number or number of buffers */
1122 int forceit) /* TRUE for :...! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123{
1124 buf_T *buf;
1125 buf_T *bp;
1126 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1127 || action == DOBUF_WIPE);
1128
1129 switch (start)
1130 {
1131 case DOBUF_FIRST: buf = firstbuf; break;
1132 case DOBUF_LAST: buf = lastbuf; break;
1133 default: buf = curbuf; break;
1134 }
1135 if (start == DOBUF_MOD) /* find next modified buffer */
1136 {
1137 while (count-- > 0)
1138 {
1139 do
1140 {
1141 buf = buf->b_next;
1142 if (buf == NULL)
1143 buf = firstbuf;
1144 }
1145 while (buf != curbuf && !bufIsChanged(buf));
1146 }
1147 if (!bufIsChanged(buf))
1148 {
1149 EMSG(_("E84: No modified buffer found"));
1150 return FAIL;
1151 }
1152 }
1153 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1154 {
1155 while (buf != NULL && buf->b_fnum != count)
1156 buf = buf->b_next;
1157 }
1158 else
1159 {
1160 bp = NULL;
1161 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1162 {
1163 /* remember the buffer where we start, we come back there when all
1164 * buffers are unlisted. */
1165 if (bp == NULL)
1166 bp = buf;
1167 if (dir == FORWARD)
1168 {
1169 buf = buf->b_next;
1170 if (buf == NULL)
1171 buf = firstbuf;
1172 }
1173 else
1174 {
1175 buf = buf->b_prev;
1176 if (buf == NULL)
1177 buf = lastbuf;
1178 }
1179 /* don't count unlisted buffers */
1180 if (unload || buf->b_p_bl)
1181 {
1182 --count;
1183 bp = NULL; /* use this buffer as new starting point */
1184 }
1185 if (bp == buf)
1186 {
1187 /* back where we started, didn't find anything. */
1188 EMSG(_("E85: There is no listed buffer"));
1189 return FAIL;
1190 }
1191 }
1192 }
1193
1194 if (buf == NULL) /* could not find it */
1195 {
1196 if (start == DOBUF_FIRST)
1197 {
1198 /* don't warn when deleting */
1199 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001200 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 }
1202 else if (dir == FORWARD)
1203 EMSG(_("E87: Cannot go beyond last buffer"));
1204 else
1205 EMSG(_("E88: Cannot go before first buffer"));
1206 return FAIL;
1207 }
1208
1209#ifdef FEAT_GUI
1210 need_mouse_correct = TRUE;
1211#endif
1212
1213#ifdef FEAT_LISTCMDS
1214 /*
1215 * delete buffer buf from memory and/or the list
1216 */
1217 if (unload)
1218 {
1219 int forward;
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001220# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
1221 bufref_T bufref;
1222
1223 set_bufref(&bufref, buf);
1224# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225
1226 /* When unloading or deleting a buffer that's already unloaded and
1227 * unlisted: fail silently. */
1228 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1229 return FAIL;
1230
1231 if (!forceit && bufIsChanged(buf))
1232 {
1233#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1234 if ((p_confirm || cmdmod.confirm) && p_write)
1235 {
1236 dialog_changed(buf, FALSE);
1237# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001238 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 /* Autocommand deleted buffer, oops! It's not changed
1240 * now. */
1241 return FAIL;
1242# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001243 /* If it's still changed fail silently, the dialog already
1244 * mentioned why it fails. */
1245 if (bufIsChanged(buf))
1246 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249#endif
1250 {
1251 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1252 buf->b_fnum);
1253 return FAIL;
1254 }
1255 }
1256
1257 /*
1258 * If deleting the last (listed) buffer, make it empty.
1259 * The last (listed) buffer cannot be unloaded.
1260 */
1261 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1262 if (bp->b_p_bl && bp != buf)
1263 break;
1264 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001265 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267#ifdef FEAT_WINDOWS
1268 /*
1269 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001270 * (unless it's the only window). Repeat this so long as we end up in
1271 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001273 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001274# ifdef FEAT_AUTOCMD
1275 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1276# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001277 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001278 {
1279 if (win_close(curwin, FALSE) == FAIL)
1280 break;
1281 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282#endif
1283
1284 /*
1285 * If the buffer to be deleted is not the current one, delete it here.
1286 */
1287 if (buf != curbuf)
1288 {
1289#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001290 close_windows(buf, FALSE);
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001291 if (buf != curbuf && bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001293 if (buf->b_nwindows <= 0)
1294 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 return OK;
1296 }
1297
1298 /*
1299 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001300 * There should be another, otherwise it would have been handled
1301 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001302 * First use au_new_curbuf.br_buf, if it is valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 * Then prefer the buffer we most recently visited.
1304 * Else try to find one that is loaded, after the current buffer,
1305 * then before the current buffer.
1306 * Finally use any buffer.
1307 */
1308 buf = NULL; /* selected buffer */
1309 bp = NULL; /* used when no loaded buffer found */
1310#ifdef FEAT_AUTOCMD
Bram Moolenaar19ff9bf2016-07-10 19:03:57 +02001311 if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
1312 buf = au_new_curbuf.br_buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313# ifdef FEAT_JUMPLIST
1314 else
1315# endif
1316#endif
1317#ifdef FEAT_JUMPLIST
1318 if (curwin->w_jumplistlen > 0)
1319 {
1320 int jumpidx;
1321
1322 jumpidx = curwin->w_jumplistidx - 1;
1323 if (jumpidx < 0)
1324 jumpidx = curwin->w_jumplistlen - 1;
1325
1326 forward = jumpidx;
1327 while (jumpidx != curwin->w_jumplistidx)
1328 {
1329 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1330 if (buf != NULL)
1331 {
1332 if (buf == curbuf || !buf->b_p_bl)
1333 buf = NULL; /* skip current and unlisted bufs */
1334 else if (buf->b_ml.ml_mfp == NULL)
1335 {
1336 /* skip unloaded buf, but may keep it for later */
1337 if (bp == NULL)
1338 bp = buf;
1339 buf = NULL;
1340 }
1341 }
1342 if (buf != NULL) /* found a valid buffer: stop searching */
1343 break;
1344 /* advance to older entry in jump list */
1345 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1346 break;
1347 if (--jumpidx < 0)
1348 jumpidx = curwin->w_jumplistlen - 1;
1349 if (jumpidx == forward) /* List exhausted for sure */
1350 break;
1351 }
1352 }
1353#endif
1354
1355 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1356 {
1357 forward = TRUE;
1358 buf = curbuf->b_next;
1359 for (;;)
1360 {
1361 if (buf == NULL)
1362 {
1363 if (!forward) /* tried both directions */
1364 break;
1365 buf = curbuf->b_prev;
1366 forward = FALSE;
1367 continue;
1368 }
1369 /* in non-help buffer, try to skip help buffers, and vv */
1370 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1371 {
1372 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1373 break;
1374 if (bp == NULL) /* remember unloaded buf for later */
1375 bp = buf;
1376 }
1377 if (forward)
1378 buf = buf->b_next;
1379 else
1380 buf = buf->b_prev;
1381 }
1382 }
1383 if (buf == NULL) /* No loaded buffer, use unloaded one */
1384 buf = bp;
1385 if (buf == NULL) /* No loaded buffer, find listed one */
1386 {
1387 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1388 if (buf->b_p_bl && buf != curbuf)
1389 break;
1390 }
1391 if (buf == NULL) /* Still no buffer, just take one */
1392 {
1393 if (curbuf->b_next != NULL)
1394 buf = curbuf->b_next;
1395 else
1396 buf = curbuf->b_prev;
1397 }
1398 }
1399
Bram Moolenaara02471e2014-01-10 16:43:14 +01001400 if (buf == NULL)
1401 {
1402 /* Autocommands must have wiped out all other buffers. Only option
1403 * now is to make the current buffer empty. */
1404 return empty_curbuf(FALSE, forceit, action);
1405 }
1406
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 /*
1408 * make buf current buffer
1409 */
1410 if (action == DOBUF_SPLIT) /* split window first */
1411 {
1412# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001413 /* If 'switchbuf' contains "useopen": jump to first window containing
1414 * "buf" if one exists */
1415 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001416 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001417 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001418 * page containing "buf" if one exists */
1419 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 return OK;
1421 if (win_split(0, 0) == FAIL)
1422# endif
1423 return FAIL;
1424 }
1425#endif
1426
1427 /* go to current buffer - nothing to do */
1428 if (buf == curbuf)
1429 return OK;
1430
1431 /*
1432 * Check if the current buffer may be abandoned.
1433 */
1434 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1435 {
1436#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1437 if ((p_confirm || cmdmod.confirm) && p_write)
1438 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001439# ifdef FEAT_AUTOCMD
1440 bufref_T bufref;
1441
1442 set_bufref(&bufref, buf);
1443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 dialog_changed(curbuf, FALSE);
1445# ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001446 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /* Autocommand deleted buffer, oops! */
1448 return FAIL;
1449# endif
1450 }
1451 if (bufIsChanged(curbuf))
1452#endif
1453 {
1454 EMSG(_(e_nowrtmsg));
1455 return FAIL;
1456 }
1457 }
1458
1459 /* Go to the other buffer. */
1460 set_curbuf(buf, action);
1461
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001462#if defined(FEAT_LISTCMDS) \
1463 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001465 {
1466 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468#endif
1469
1470#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1471 if (aborting()) /* autocmds may abort script processing */
1472 return FAIL;
1473#endif
1474
1475 return OK;
1476}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478
1479/*
1480 * Set current buffer to "buf". Executes autocommands and closes current
1481 * buffer. "action" tells how to close the current buffer:
1482 * DOBUF_GOTO free or hide it
1483 * DOBUF_SPLIT nothing
1484 * DOBUF_UNLOAD unload it
1485 * DOBUF_DEL delete it
1486 * DOBUF_WIPE wipe it out
1487 */
1488 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489set_curbuf(buf_T *buf, int action)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
1491 buf_T *prevbuf;
1492 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1493 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001494#ifdef FEAT_SYN_HL
1495 long old_tw = curbuf->b_p_tw;
1496#endif
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001497 bufref_T bufref;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498
1499 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001500 if (!cmdmod.keepalt)
1501 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001502 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 /* Don't restart Select mode after switching to another buffer. */
1505 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506
1507 /* close_windows() or apply_autocmds() may change curbuf */
1508 prevbuf = curbuf;
Bram Moolenaar453f37d2016-07-10 18:33:59 +02001509 set_bufref(&bufref, prevbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510
1511#ifdef FEAT_AUTOCMD
Bram Moolenaar82404332016-07-10 17:00:38 +02001512 if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513# ifdef FEAT_EVAL
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001514 || (bufref_valid(&bufref) && !aborting()))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515# else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001516 || bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517# endif
1518#endif
1519 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001520#ifdef FEAT_SYN_HL
1521 if (prevbuf == curwin->w_buffer)
1522 reset_synblock(curwin);
1523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524#ifdef FEAT_WINDOWS
1525 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001526 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527#endif
1528#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001529 if (bufref_valid(&bufref) && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530#else
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001531 if (bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001533 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001534#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001535 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001536#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001537 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001538 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1540 unload ? action : (action == DOBUF_GOTO
1541 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001542 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001543#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001544 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001545 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001546 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001547#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 }
1550#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001551 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001552 * it did ":bunload") or aborted the script processing.
Bram Moolenaar9e931222012-06-20 11:55:01 +02001553 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1554 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001555# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001556 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001557# endif
1558# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001559 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001560# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001561 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001563 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001565#ifdef FEAT_SYN_HL
1566 if (old_tw != curbuf->b_p_tw)
1567 check_colorcolumn(curwin);
1568#endif
1569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570}
1571
1572/*
1573 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001574 * Old curbuf must have been abandoned already! This also means "curbuf" may
1575 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 */
1577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001578enter_buffer(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579{
1580 /* Copy buffer and window local option values. Not for a help buffer. */
1581 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1582 if (!buf->b_help)
1583 get_winopts(buf);
1584#ifdef FEAT_FOLDING
1585 else
1586 /* Remove all folds in the window. */
1587 clearFolding(curwin);
1588 foldUpdateAll(curwin); /* update folds (later). */
1589#endif
1590
1591 /* Get the buffer in the current window. */
1592 curwin->w_buffer = buf;
1593 curbuf = buf;
1594 ++curbuf->b_nwindows;
1595
1596#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001597 if (curwin->w_p_diff)
1598 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599#endif
1600
Bram Moolenaara971b822011-09-14 14:43:25 +02001601#ifdef FEAT_SYN_HL
1602 curwin->w_s = &(buf->b_s);
1603#endif
1604
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 /* Cursor on first line by default. */
1606 curwin->w_cursor.lnum = 1;
1607 curwin->w_cursor.col = 0;
1608#ifdef FEAT_VIRTUALEDIT
1609 curwin->w_cursor.coladd = 0;
1610#endif
1611 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001612#ifdef FEAT_AUTOCMD
1613 curwin->w_topline_was_set = FALSE;
1614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001616 /* mark cursor position as being invalid */
1617 curwin->w_valid = 0;
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 /* Make sure the buffer is loaded. */
1620 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001621 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001622#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001623 /* If there is no filetype, allow for detecting one. Esp. useful for
1624 * ":ball" used in a autocommand. If there already is a filetype we
1625 * might prefer to keep it. */
1626 if (*curbuf->b_p_ft == NUL)
1627 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001628#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001629
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001630 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 else
1633 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001634 if (!msg_silent)
1635 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1637#ifdef FEAT_AUTOCMD
1638 curwin->w_topline = 1;
1639# ifdef FEAT_DIFF
1640 curwin->w_topfill = 0;
1641# endif
1642 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1643 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1644#endif
1645 }
1646
1647 /* If autocommands did not change the cursor position, restore cursor lnum
1648 * and possibly cursor col. */
1649 if (curwin->w_cursor.lnum == 1 && inindent(0))
1650 buflist_getfpos();
1651
1652 check_arg_idx(curwin); /* check for valid arg_idx */
1653#ifdef FEAT_TITLE
1654 maketitle();
1655#endif
1656#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001657 /* when autocmds didn't change it */
1658 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659#endif
1660 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1661
Bram Moolenaar009b2592004-10-24 19:18:58 +00001662#ifdef FEAT_NETBEANS_INTG
1663 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001664 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001665#endif
1666
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001667 /* Change directories when the 'acd' option is set. */
1668 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669
1670#ifdef FEAT_KEYMAP
1671 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001672 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001674#ifdef FEAT_SPELL
1675 /* May need to set the spell language. Can only do this after the buffer
1676 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001677 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1678 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001679#endif
Bram Moolenaarab9c89b2016-07-03 17:47:26 +02001680#ifdef FEAT_VIMINFO
1681 curbuf->b_last_used = vim_time();
1682#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001683
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 redraw_later(NOT_VALID);
1685}
1686
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001687#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1688/*
1689 * Change to the directory of the current buffer.
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001690 * Don't do this while still starting up.
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001691 */
1692 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001693do_autochdir(void)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001694{
Bram Moolenaar5c719942016-07-09 23:40:45 +02001695 if ((starting == 0 || test_autochdir)
Bram Moolenaar6bd364e2016-02-23 16:19:07 +01001696 && curbuf->b_ffname != NULL
1697 && vim_chdirfile(curbuf->b_ffname) == OK)
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001698 shorten_fnames(TRUE);
1699}
1700#endif
1701
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702/*
1703 * functions for dealing with the buffer list
1704 */
1705
1706/*
1707 * Add a file name to the buffer list. Return a pointer to the buffer.
1708 * If the same file name already exists return a pointer to that buffer.
1709 * If it does not exist, or if fname == NULL, a new entry is created.
1710 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1711 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1712 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001713 * If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
Bram Moolenaar82404332016-07-10 17:00:38 +02001714 * If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
1715 * if the buffer already exists.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 * This is the ONLY way to create a new buffer.
1717 */
1718static int top_file_num = 1; /* highest file number */
1719
1720 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001721buflist_new(
1722 char_u *ffname, /* full path of fname or relative */
1723 char_u *sfname, /* short fname or NULL */
1724 linenr_T lnum, /* preferred cursor line */
1725 int flags) /* BLN_ defines */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726{
1727 buf_T *buf;
1728#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02001729 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#endif
1731
1732 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1733
1734 /*
1735 * If file name already exists in the list, update the entry.
1736 */
1737#ifdef UNIX
1738 /* On Unix we can use inode numbers when the file exists. Works better
1739 * for hard links. */
1740 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1741 st.st_dev = (dev_T)-1;
1742#endif
Bram Moolenaarb127cfd2016-05-29 16:24:50 +02001743 if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#ifdef UNIX
1745 buflist_findname_stat(ffname, &st)
1746#else
1747 buflist_findname(ffname)
1748#endif
1749 ) != NULL)
1750 {
1751 vim_free(ffname);
1752 if (lnum != 0)
1753 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
Bram Moolenaar82404332016-07-10 17:00:38 +02001754
1755 if ((flags & BLN_NOOPT) == 0)
1756 /* copy the options now, if 'cpo' doesn't have 's' and not done
1757 * already */
1758 buf_copy_options(buf, 0);
1759
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1761 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001762#ifdef FEAT_AUTOCMD
1763 bufref_T bufref;
1764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 buf->b_p_bl = TRUE;
1766#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001767 set_bufref(&bufref, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001769 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001770 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001771 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001772 return NULL;
1773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#endif
1775 }
1776 return buf;
1777 }
1778
1779 /*
1780 * If the current buffer has no name and no contents, use the current
1781 * buffer. Otherwise: Need to allocate a new buffer structure.
1782 *
1783 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001784 * (A spell file buffer is allocated in spell.c, but that's not a normal
1785 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 */
1787 buf = NULL;
1788 if ((flags & BLN_CURBUF)
1789 && curbuf != NULL
1790 && curbuf->b_ffname == NULL
1791 && curbuf->b_nwindows <= 1
1792 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1793 {
1794 buf = curbuf;
1795#ifdef FEAT_AUTOCMD
1796 /* It's like this buffer is deleted. Watch out for autocommands that
1797 * change curbuf! If that happens, allocate a new buffer anyway. */
1798 if (curbuf->b_p_bl)
1799 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1800 if (buf == curbuf)
1801 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1802# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001803 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804 return NULL;
1805# endif
1806#endif
1807#ifdef FEAT_QUICKFIX
1808# ifdef FEAT_AUTOCMD
1809 if (buf == curbuf)
1810# endif
1811 {
1812 /* Make sure 'bufhidden' and 'buftype' are empty */
1813 clear_string_option(&buf->b_p_bh);
1814 clear_string_option(&buf->b_p_bt);
1815 }
1816#endif
1817 }
1818 if (buf != curbuf || curbuf == NULL)
1819 {
1820 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1821 if (buf == NULL)
1822 {
1823 vim_free(ffname);
1824 return NULL;
1825 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001826#ifdef FEAT_EVAL
1827 /* init b: variables */
1828 buf->b_vars = dict_alloc();
1829 if (buf->b_vars == NULL)
1830 {
1831 vim_free(ffname);
1832 vim_free(buf);
1833 return NULL;
1834 }
1835 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 }
1838
1839 if (ffname != NULL)
1840 {
1841 buf->b_ffname = ffname;
1842 buf->b_sfname = vim_strsave(sfname);
1843 }
1844
1845 clear_wininfo(buf);
1846 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1847
1848 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1849 || buf->b_wininfo == NULL)
1850 {
1851 vim_free(buf->b_ffname);
1852 buf->b_ffname = NULL;
1853 vim_free(buf->b_sfname);
1854 buf->b_sfname = NULL;
1855 if (buf != curbuf)
1856 free_buffer(buf);
1857 return NULL;
1858 }
1859
1860 if (buf == curbuf)
1861 {
1862 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001863 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 if (buf != curbuf) /* autocommands deleted the buffer! */
1865 return NULL;
1866#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001867 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 return NULL;
1869#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001871
1872 /* Init the options. */
1873 buf->b_p_initialized = FALSE;
1874 buf_copy_options(buf, BCO_ENTER);
1875
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876#ifdef FEAT_KEYMAP
1877 /* need to reload lmaps and set b:keymap_name */
1878 curbuf->b_kmap_state |= KEYMAP_INIT;
1879#endif
1880 }
1881 else
1882 {
1883 /*
1884 * put new buffer at the end of the buffer list
1885 */
1886 buf->b_next = NULL;
1887 if (firstbuf == NULL) /* buffer list is empty */
1888 {
1889 buf->b_prev = NULL;
1890 firstbuf = buf;
1891 }
1892 else /* append new buffer at end of list */
1893 {
1894 lastbuf->b_next = buf;
1895 buf->b_prev = lastbuf;
1896 }
1897 lastbuf = buf;
1898
1899 buf->b_fnum = top_file_num++;
1900 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1901 {
1902 EMSG(_("W14: Warning: List of file names overflow"));
1903 if (emsg_silent == 0)
1904 {
1905 out_flush();
1906 ui_delay(3000L, TRUE); /* make sure it is noticed */
1907 }
1908 top_file_num = 1;
1909 }
1910
1911 /*
1912 * Always copy the options from the current buffer.
1913 */
1914 buf_copy_options(buf, BCO_ALWAYS);
1915 }
1916
1917 buf->b_wininfo->wi_fpos.lnum = lnum;
1918 buf->b_wininfo->wi_win = curwin;
1919
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001920#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001921 hash_init(&buf->b_s.b_keywtab);
1922 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923#endif
1924
1925 buf->b_fname = buf->b_sfname;
1926#ifdef UNIX
1927 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001928 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 else
1930 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001931 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 buf->b_dev = st.st_dev;
1933 buf->b_ino = st.st_ino;
1934 }
1935#endif
1936 buf->b_u_synced = TRUE;
1937 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001938 if (flags & BLN_DUMMY)
1939 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 buf_clear_file(buf);
1941 clrallmarks(buf); /* clear marks */
1942 fmarks_check_names(buf); /* check file marks for this file */
1943 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1944#ifdef FEAT_AUTOCMD
1945 if (!(flags & BLN_DUMMY))
1946 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001947 bufref_T bufref;
1948
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001949 /* Tricky: these autocommands may change the buffer list. They could
1950 * also split the window with re-using the one empty buffer. This may
1951 * result in unexpectedly losing the empty buffer. */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001952 set_bufref(&bufref, buf);
Bram Moolenaar82404332016-07-10 17:00:38 +02001953 if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001954 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001955 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001957 {
Bram Moolenaar82404332016-07-10 17:00:38 +02001958 if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02001959 && !bufref_valid(&bufref))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001960 return NULL;
1961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001963 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 return NULL;
1965# endif
1966 }
1967#endif
1968
1969 return buf;
1970}
1971
1972/*
1973 * Free the memory for the options of a buffer.
1974 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1975 * 'fileencoding'.
1976 */
1977 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001978free_buf_options(
1979 buf_T *buf,
1980 int free_p_ff)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981{
1982 if (free_p_ff)
1983 {
1984#ifdef FEAT_MBYTE
1985 clear_string_option(&buf->b_p_fenc);
1986#endif
1987 clear_string_option(&buf->b_p_ff);
1988#ifdef FEAT_QUICKFIX
1989 clear_string_option(&buf->b_p_bh);
1990 clear_string_option(&buf->b_p_bt);
1991#endif
1992 }
1993#ifdef FEAT_FIND_ID
1994 clear_string_option(&buf->b_p_def);
1995 clear_string_option(&buf->b_p_inc);
1996# ifdef FEAT_EVAL
1997 clear_string_option(&buf->b_p_inex);
1998# endif
1999#endif
2000#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
2001 clear_string_option(&buf->b_p_inde);
2002 clear_string_option(&buf->b_p_indk);
2003#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00002004#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
2005 clear_string_option(&buf->b_p_bexpr);
2006#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02002007#if defined(FEAT_CRYPT)
2008 clear_string_option(&buf->b_p_cm);
2009#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002010#if defined(FEAT_EVAL)
2011 clear_string_option(&buf->b_p_fex);
2012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013#ifdef FEAT_CRYPT
2014 clear_string_option(&buf->b_p_key);
2015#endif
2016 clear_string_option(&buf->b_p_kp);
2017 clear_string_option(&buf->b_p_mps);
2018 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002019 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 clear_string_option(&buf->b_p_isk);
2021#ifdef FEAT_KEYMAP
2022 clear_string_option(&buf->b_p_keymap);
2023 ga_clear(&buf->b_kmap_ga);
2024#endif
2025#ifdef FEAT_COMMENTS
2026 clear_string_option(&buf->b_p_com);
2027#endif
2028#ifdef FEAT_FOLDING
2029 clear_string_option(&buf->b_p_cms);
2030#endif
2031 clear_string_option(&buf->b_p_nf);
2032#ifdef FEAT_SYN_HL
2033 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01002034 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00002035#endif
2036#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02002037 clear_string_option(&buf->b_s.b_p_spc);
2038 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02002039 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002040 buf->b_s.b_cap_prog = NULL;
2041 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042#endif
2043#ifdef FEAT_SEARCHPATH
2044 clear_string_option(&buf->b_p_sua);
2045#endif
2046#ifdef FEAT_AUTOCMD
2047 clear_string_option(&buf->b_p_ft);
2048#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049#ifdef FEAT_CINDENT
2050 clear_string_option(&buf->b_p_cink);
2051 clear_string_option(&buf->b_p_cino);
2052#endif
2053#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
2054 clear_string_option(&buf->b_p_cinw);
2055#endif
2056#ifdef FEAT_INS_EXPAND
2057 clear_string_option(&buf->b_p_cpt);
2058#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002059#ifdef FEAT_COMPL_FUNC
2060 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002061 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002062#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063#ifdef FEAT_QUICKFIX
2064 clear_string_option(&buf->b_p_gp);
2065 clear_string_option(&buf->b_p_mp);
2066 clear_string_option(&buf->b_p_efm);
2067#endif
2068 clear_string_option(&buf->b_p_ep);
2069 clear_string_option(&buf->b_p_path);
2070 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01002071 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072#ifdef FEAT_INS_EXPAND
2073 clear_string_option(&buf->b_p_dict);
2074 clear_string_option(&buf->b_p_tsr);
2075#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002076#ifdef FEAT_TEXTOBJ
2077 clear_string_option(&buf->b_p_qe);
2078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002080 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002081#ifdef FEAT_LISP
2082 clear_string_option(&buf->b_p_lw);
2083#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002084 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085}
2086
2087/*
2088 * get alternate file n
2089 * set linenr to lnum or altfpos.lnum if lnum == 0
2090 * also set cursor column to altfpos.col if 'startofline' is not set.
2091 * if (options & GETF_SETMARK) call setpcmark()
2092 * if (options & GETF_ALT) we are jumping to an alternate file.
2093 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2094 *
2095 * return FAIL for failure, OK for success
2096 */
2097 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002098buflist_getfile(
2099 int n,
2100 linenr_T lnum,
2101 int options,
2102 int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103{
2104 buf_T *buf;
2105#ifdef FEAT_WINDOWS
2106 win_T *wp = NULL;
2107#endif
2108 pos_T *fpos;
2109 colnr_T col;
2110
2111 buf = buflist_findnr(n);
2112 if (buf == NULL)
2113 {
2114 if ((options & GETF_ALT) && n == 0)
2115 EMSG(_(e_noalt));
2116 else
2117 EMSGN(_("E92: Buffer %ld not found"), n);
2118 return FAIL;
2119 }
2120
2121 /* if alternate file is the current buffer, nothing to do */
2122 if (buf == curbuf)
2123 return OK;
2124
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002125 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002126 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002127 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002129 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002130#ifdef FEAT_AUTOCMD
2131 if (curbuf_locked())
2132 return FAIL;
2133#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134
2135 /* altfpos may be changed by getfile(), get it now */
2136 if (lnum == 0)
2137 {
2138 fpos = buflist_findfpos(buf);
2139 lnum = fpos->lnum;
2140 col = fpos->col;
2141 }
2142 else
2143 col = 0;
2144
2145#ifdef FEAT_WINDOWS
2146 if (options & GETF_SWITCH)
2147 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002148 /* If 'switchbuf' contains "useopen": jump to first window containing
2149 * "buf" if one exists */
2150 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002152
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002153 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002154 * page containing "buf" if one exists */
2155 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002156 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002157
2158 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2159 * current buffer isn't empty: open new tab or window */
2160 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2161 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002163 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002164 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002165 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2166 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002168 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170 }
2171#endif
2172
2173 ++RedrawingDisabled;
2174 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2175 lnum, forceit) <= 0)
2176 {
2177 --RedrawingDisabled;
2178
2179 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2180 if (!p_sol && col != 0)
2181 {
2182 curwin->w_cursor.col = col;
2183 check_cursor_col();
2184#ifdef FEAT_VIRTUALEDIT
2185 curwin->w_cursor.coladd = 0;
2186#endif
2187 curwin->w_set_curswant = TRUE;
2188 }
2189 return OK;
2190 }
2191 --RedrawingDisabled;
2192 return FAIL;
2193}
2194
2195/*
2196 * go to the last know line number for the current buffer
2197 */
2198 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199buflist_getfpos(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200{
2201 pos_T *fpos;
2202
2203 fpos = buflist_findfpos(curbuf);
2204
2205 curwin->w_cursor.lnum = fpos->lnum;
2206 check_cursor_lnum();
2207
2208 if (p_sol)
2209 curwin->w_cursor.col = 0;
2210 else
2211 {
2212 curwin->w_cursor.col = fpos->col;
2213 check_cursor_col();
2214#ifdef FEAT_VIRTUALEDIT
2215 curwin->w_cursor.coladd = 0;
2216#endif
2217 curwin->w_set_curswant = TRUE;
2218 }
2219}
2220
Bram Moolenaar81695252004-12-29 20:58:21 +00002221#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2222/*
2223 * Find file in buffer list by name (it has to be for the current window).
2224 * Returns NULL if not found.
2225 */
2226 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002227buflist_findname_exp(char_u *fname)
Bram Moolenaar81695252004-12-29 20:58:21 +00002228{
2229 char_u *ffname;
2230 buf_T *buf = NULL;
2231
2232 /* First make the name into a full path name */
2233 ffname = FullName_save(fname,
2234#ifdef UNIX
2235 TRUE /* force expansion, get rid of symbolic links */
2236#else
2237 FALSE
2238#endif
2239 );
2240 if (ffname != NULL)
2241 {
2242 buf = buflist_findname(ffname);
2243 vim_free(ffname);
2244 }
2245 return buf;
2246}
2247#endif
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249/*
2250 * Find file in buffer list by name (it has to be for the current window).
2251 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002252 * Skips dummy buffers.
2253 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 */
2255 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002256buflist_findname(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257{
2258#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002259 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
2261 if (mch_stat((char *)ffname, &st) < 0)
2262 st.st_dev = (dev_T)-1;
2263 return buflist_findname_stat(ffname, &st);
2264}
2265
2266/*
2267 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2268 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002269 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 */
2271 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002272buflist_findname_stat(
2273 char_u *ffname,
Bram Moolenaar8767f522016-07-01 17:17:39 +02002274 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275{
2276#endif
2277 buf_T *buf;
2278
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002279 /* Start at the last buffer, expect to find a match sooner. */
2280 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar81695252004-12-29 20:58:21 +00002281 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282#ifdef UNIX
2283 , stp
2284#endif
2285 ))
2286 return buf;
2287 return NULL;
2288}
2289
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002290#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2291 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292/*
2293 * Find file in buffer list by a regexp pattern.
2294 * Return fnum of the found buffer.
2295 * Return < 0 for error.
2296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002298buflist_findpat(
2299 char_u *pattern,
2300 char_u *pattern_end, /* pointer to first char after pattern */
2301 int unlisted, /* find unlisted buffers */
2302 int diffmode UNUSED, /* find diff-mode buffers only */
2303 int curtab_only) /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304{
2305 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 int match = -1;
2307 int find_listed;
2308 char_u *pat;
2309 char_u *patend;
2310 int attempt;
2311 char_u *p;
2312 int toggledollar;
2313
2314 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2315 {
2316 if (*pattern == '%')
2317 match = curbuf->b_fnum;
2318 else
2319 match = curwin->w_alt_fnum;
2320#ifdef FEAT_DIFF
2321 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2322 match = -1;
2323#endif
2324 }
2325
2326 /*
2327 * Try four ways of matching a listed buffer:
2328 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002329 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 * attempt == 2: with '$' at end (only match at end)
2331 * attempt == 3: with '^' at start and '$' at end (only full match)
2332 * Repeat this for finding an unlisted buffer if there was no matching
2333 * listed buffer.
2334 */
2335 else
2336 {
2337 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2338 if (pat == NULL)
2339 return -1;
2340 patend = pat + STRLEN(pat) - 1;
2341 toggledollar = (patend > pat && *patend == '$');
2342
2343 /* First try finding a listed buffer. If not found and "unlisted"
2344 * is TRUE, try finding an unlisted buffer. */
2345 find_listed = TRUE;
2346 for (;;)
2347 {
2348 for (attempt = 0; attempt <= 3; ++attempt)
2349 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002350 regmatch_T regmatch;
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /* may add '^' and '$' */
2353 if (toggledollar)
2354 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2355 p = pat;
2356 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2357 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002358 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2359 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
2361 vim_free(pat);
2362 return -1;
2363 }
2364
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002365 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 if (buf->b_p_bl == find_listed
2367#ifdef FEAT_DIFF
2368 && (!diffmode || diff_mode_buf(buf))
2369#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002370 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002372 if (curtab_only)
2373 {
2374 /* Ignore the match if the buffer is not open in
2375 * the current tab. */
2376#ifdef FEAT_WINDOWS
2377 win_T *wp;
2378
2379 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2380 if (wp->w_buffer == buf)
2381 break;
2382 if (wp == NULL)
2383 continue;
2384#else
2385 if (curwin->w_buffer != buf)
2386 continue;
2387#endif
2388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 if (match >= 0) /* already found a match */
2390 {
2391 match = -2;
2392 break;
2393 }
2394 match = buf->b_fnum; /* remember first match */
2395 }
2396
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002397 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398 if (match >= 0) /* found one match */
2399 break;
2400 }
2401
2402 /* Only search for unlisted buffers if there was no match with
2403 * a listed buffer. */
2404 if (!unlisted || !find_listed || match != -1)
2405 break;
2406 find_listed = FALSE;
2407 }
2408
2409 vim_free(pat);
2410 }
2411
2412 if (match == -2)
2413 EMSG2(_("E93: More than one match for %s"), pattern);
2414 else if (match < 0)
2415 EMSG2(_("E94: No matching buffer for %s"), pattern);
2416 return match;
2417}
2418#endif
2419
2420#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2421
2422/*
2423 * Find all buffer names that match.
2424 * For command line expansion of ":buf" and ":sbuf".
2425 * Return OK if matches found, FAIL otherwise.
2426 */
2427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002428ExpandBufnames(
2429 char_u *pat,
2430 int *num_file,
2431 char_u ***file,
2432 int options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433{
2434 int count = 0;
2435 buf_T *buf;
2436 int round;
2437 char_u *p;
2438 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002439 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440
2441 *num_file = 0; /* return values in case of FAIL */
2442 *file = NULL;
2443
Bram Moolenaar05159a02005-02-26 23:04:13 +00002444 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2445 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002447 patc = alloc((unsigned)STRLEN(pat) + 11);
2448 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002449 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002450 STRCPY(patc, "\\(^\\|[\\/]\\)");
2451 STRCPY(patc + 11, pat + 1);
2452 }
2453 else
2454 patc = pat;
2455
2456 /*
2457 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002458 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002459 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002460 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002461 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002462 regmatch_T regmatch;
2463
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002464 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002465 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002466 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2467 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002468 {
2469 if (patc != pat)
2470 vim_free(patc);
2471 return FAIL;
2472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473
2474 /*
2475 * round == 1: Count the matches.
2476 * round == 2: Build the array to keep the matches.
2477 */
2478 for (round = 1; round <= 2; ++round)
2479 {
2480 count = 0;
2481 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2482 {
2483 if (!buf->b_p_bl) /* skip unlisted buffers */
2484 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002485 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 if (p != NULL)
2487 {
2488 if (round == 1)
2489 ++count;
2490 else
2491 {
2492 if (options & WILD_HOME_REPLACE)
2493 p = home_replace_save(buf, p);
2494 else
2495 p = vim_strsave(p);
2496 (*file)[count++] = p;
2497 }
2498 }
2499 }
2500 if (count == 0) /* no match found, break here */
2501 break;
2502 if (round == 1)
2503 {
2504 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2505 if (*file == NULL)
2506 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002507 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002508 if (patc != pat)
2509 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 return FAIL;
2511 }
2512 }
2513 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002514 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (count) /* match(es) found, break here */
2516 break;
2517 }
2518
Bram Moolenaar05159a02005-02-26 23:04:13 +00002519 if (patc != pat)
2520 vim_free(patc);
2521
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 *num_file = count;
2523 return (count == 0 ? FAIL : OK);
2524}
2525
2526#endif /* FEAT_CMDL_COMPL */
2527
2528#ifdef HAVE_BUFLIST_MATCH
2529/*
2530 * Check for a match on the file name for buffer "buf" with regprog "prog".
2531 */
2532 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002533buflist_match(
2534 regmatch_T *rmp,
2535 buf_T *buf,
2536 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538 char_u *match;
2539
2540 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002541 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002543 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544
2545 return match;
2546}
2547
2548/*
2549 * Try matching the regexp in "prog" with file name "name".
2550 * Return "name" when there is a match, NULL when not.
2551 */
2552 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002553fname_match(
2554 regmatch_T *rmp,
2555 char_u *name,
2556 int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557{
2558 char_u *match = NULL;
2559 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
2561 if (name != NULL)
2562 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002563 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002564 rmp->rm_ic = p_fic || ignore_case;
2565 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 match = name;
2567 else
2568 {
2569 /* Replace $(HOME) with '~' and try matching again. */
2570 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002571 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 match = name;
2573 vim_free(p);
2574 }
2575 }
2576
2577 return match;
2578}
2579#endif
2580
2581/*
2582 * find file in buffer list by number
2583 */
2584 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002585buflist_findnr(int nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 buf_T *buf;
2588
2589 if (nr == 0)
2590 nr = curwin->w_alt_fnum;
Bram Moolenaarea3f2e72016-07-10 20:27:32 +02002591 /* Assume newer buffers are used more often, start from the end. */
2592 for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 if (buf->b_fnum == nr)
Bram Moolenaarfd89d7e2016-06-04 20:25:05 +02002594 return buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 return NULL;
2596}
2597
2598/*
2599 * Get name of file 'n' in the buffer list.
2600 * When the file has no name an empty string is returned.
2601 * home_replace() is used to shorten the file name (used for marks).
2602 * Returns a pointer to allocated memory, of NULL when failed.
2603 */
2604 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002605buflist_nr2name(
2606 int n,
2607 int fullname,
2608 int helptail) /* for help buffers return tail only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609{
2610 buf_T *buf;
2611
2612 buf = buflist_findnr(n);
2613 if (buf == NULL)
2614 return NULL;
2615 return home_replace_save(helptail ? buf : NULL,
2616 fullname ? buf->b_ffname : buf->b_fname);
2617}
2618
2619/*
2620 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2621 * When "copy_options" is TRUE save the local window option values.
2622 * When "lnum" is 0 only do the options.
2623 */
2624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002625buflist_setfpos(
2626 buf_T *buf,
2627 win_T *win,
2628 linenr_T lnum,
2629 colnr_T col,
2630 int copy_options)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631{
2632 wininfo_T *wip;
2633
2634 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2635 if (wip->wi_win == win)
2636 break;
2637 if (wip == NULL)
2638 {
2639 /* allocate a new entry */
2640 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2641 if (wip == NULL)
2642 return;
2643 wip->wi_win = win;
2644 if (lnum == 0) /* set lnum even when it's 0 */
2645 lnum = 1;
2646 }
2647 else
2648 {
2649 /* remove the entry from the list */
2650 if (wip->wi_prev)
2651 wip->wi_prev->wi_next = wip->wi_next;
2652 else
2653 buf->b_wininfo = wip->wi_next;
2654 if (wip->wi_next)
2655 wip->wi_next->wi_prev = wip->wi_prev;
2656 if (copy_options && wip->wi_optset)
2657 {
2658 clear_winopt(&wip->wi_opt);
2659#ifdef FEAT_FOLDING
2660 deleteFoldRecurse(&wip->wi_folds);
2661#endif
2662 }
2663 }
2664 if (lnum != 0)
2665 {
2666 wip->wi_fpos.lnum = lnum;
2667 wip->wi_fpos.col = col;
2668 }
2669 if (copy_options)
2670 {
2671 /* Save the window-specific option values. */
2672 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2673#ifdef FEAT_FOLDING
2674 wip->wi_fold_manual = win->w_fold_manual;
2675 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2676#endif
2677 wip->wi_optset = TRUE;
2678 }
2679
2680 /* insert the entry in front of the list */
2681 wip->wi_next = buf->b_wininfo;
2682 buf->b_wininfo = wip;
2683 wip->wi_prev = NULL;
2684 if (wip->wi_next)
2685 wip->wi_next->wi_prev = wip;
2686
2687 return;
2688}
2689
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002690#ifdef FEAT_DIFF
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01002691static int wininfo_other_tab_diff(wininfo_T *wip);
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002692
2693/*
2694 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2695 * page. That's because a diff is local to a tab page.
2696 */
2697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002698wininfo_other_tab_diff(wininfo_T *wip)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002699{
2700 win_T *wp;
2701
2702 if (wip->wi_opt.wo_diff)
2703 {
2704 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2705 /* return FALSE when it's a window in the current tab page, thus
2706 * the buffer was in diff mode here */
2707 if (wip->wi_win == wp)
2708 return FALSE;
2709 return TRUE;
2710 }
2711 return FALSE;
2712}
2713#endif
2714
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715/*
2716 * Find info for the current window in buffer "buf".
2717 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002718 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2719 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 * Returns NULL when there isn't any info.
2721 */
2722 static wininfo_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002723find_wininfo(
2724 buf_T *buf,
2725 int skip_diff_buffer UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726{
2727 wininfo_T *wip;
2728
2729 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002730 if (wip->wi_win == curwin
2731#ifdef FEAT_DIFF
2732 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2733#endif
2734 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002736
2737 /* If no wininfo for curwin, use the first in the list (that doesn't have
2738 * 'diff' set and is in another tab page). */
2739 if (wip == NULL)
2740 {
2741#ifdef FEAT_DIFF
2742 if (skip_diff_buffer)
2743 {
2744 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2745 if (!wininfo_other_tab_diff(wip))
2746 break;
2747 }
2748 else
2749#endif
2750 wip = buf->b_wininfo;
2751 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002752 return wip;
2753}
2754
2755/*
2756 * Reset the local window options to the values last used in this window.
2757 * If the buffer wasn't used in this window before, use the values from
2758 * the most recently used window. If the values were never set, use the
2759 * global values for the window.
2760 */
2761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002762get_winopts(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
2764 wininfo_T *wip;
2765
2766 clear_winopt(&curwin->w_onebuf_opt);
2767#ifdef FEAT_FOLDING
2768 clearFolding(curwin);
2769#endif
2770
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002771 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772 if (wip != NULL && wip->wi_optset)
2773 {
2774 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2775#ifdef FEAT_FOLDING
2776 curwin->w_fold_manual = wip->wi_fold_manual;
2777 curwin->w_foldinvalid = TRUE;
2778 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2779#endif
2780 }
2781 else
2782 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2783
2784#ifdef FEAT_FOLDING
2785 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2786 if (p_fdls >= 0)
2787 curwin->w_p_fdl = p_fdls;
2788#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002789#ifdef FEAT_SYN_HL
2790 check_colorcolumn(curwin);
2791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792}
2793
2794/*
2795 * Find the position (lnum and col) for the buffer 'buf' for the current
2796 * window.
2797 * Returns a pointer to no_position if no position is found.
2798 */
2799 pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002800buflist_findfpos(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801{
2802 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002803 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002804
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002805 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002806 if (wip != NULL)
2807 return &(wip->wi_fpos);
2808 else
2809 return &no_position;
2810}
2811
2812/*
2813 * Find the lnum for the buffer 'buf' for the current window.
2814 */
2815 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002816buflist_findlnum(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002817{
2818 return buflist_findfpos(buf)->lnum;
2819}
2820
2821#if defined(FEAT_LISTCMDS) || defined(PROTO)
2822/*
2823 * List all know file names (for :files and :buffers command).
2824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002826buflist_list(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 buf_T *buf;
2829 int len;
2830 int i;
2831
2832 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2833 {
2834 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002835 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2836 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2837 || (vim_strchr(eap->arg, '+')
2838 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2839 || (vim_strchr(eap->arg, 'a')
2840 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2841 || (vim_strchr(eap->arg, 'h')
2842 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2843 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2844 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2845 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2846 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2847 || (vim_strchr(eap->arg, '#')
2848 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 continue;
2850 msg_putchar('\n');
2851 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002852 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 else
2854 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2855
Bram Moolenaar50cde822005-06-05 21:54:54 +00002856 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 buf->b_fnum,
2858 buf->b_p_bl ? ' ' : 'u',
2859 buf == curbuf ? '%' :
2860 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2861 buf->b_ml.ml_mfp == NULL ? ' ' :
2862 (buf->b_nwindows == 0 ? 'h' : 'a'),
2863 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2864 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002865 : (bufIsChanged(buf) ? '+' : ' '),
2866 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002867 if (len > IOSIZE - 20)
2868 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869
2870 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 i = 40 - vim_strsize(IObuff);
2872 do
2873 {
2874 IObuff[len++] = ' ';
2875 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002876 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2877 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002878 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 msg_outtrans(IObuff);
2880 out_flush(); /* output one line at a time */
2881 ui_breakcheck();
2882 }
2883}
2884#endif
2885
2886/*
2887 * Get file name and line number for file 'fnum'.
2888 * Used by DoOneCmd() for translating '%' and '#'.
2889 * Used by insert_reg() and cmdline_paste() for '#' register.
2890 * Return FAIL if not found, OK for success.
2891 */
2892 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002893buflist_name_nr(
2894 int fnum,
2895 char_u **fname,
2896 linenr_T *lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897{
2898 buf_T *buf;
2899
2900 buf = buflist_findnr(fnum);
2901 if (buf == NULL || buf->b_fname == NULL)
2902 return FAIL;
2903
2904 *fname = buf->b_fname;
2905 *lnum = buflist_findlnum(buf);
2906
2907 return OK;
2908}
2909
2910/*
2911 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2912 * The file name with the full path is also remembered, for when :cd is used.
2913 * Returns FAIL for failure (file name already in use by other buffer)
2914 * OK otherwise.
2915 */
2916 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002917setfname(
2918 buf_T *buf,
2919 char_u *ffname,
2920 char_u *sfname,
2921 int message) /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922{
Bram Moolenaar81695252004-12-29 20:58:21 +00002923 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02002925 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926#endif
2927
2928 if (ffname == NULL || *ffname == NUL)
2929 {
2930 /* Removing the name. */
2931 vim_free(buf->b_ffname);
2932 vim_free(buf->b_sfname);
2933 buf->b_ffname = NULL;
2934 buf->b_sfname = NULL;
2935#ifdef UNIX
2936 st.st_dev = (dev_T)-1;
2937#endif
2938 }
2939 else
2940 {
2941 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2942 if (ffname == NULL) /* out of memory */
2943 return FAIL;
2944
2945 /*
2946 * if the file name is already used in another buffer:
2947 * - if the buffer is loaded, fail
2948 * - if the buffer is not loaded, delete it from the list
2949 */
2950#ifdef UNIX
2951 if (mch_stat((char *)ffname, &st) < 0)
2952 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002953#endif
2954 if (!(buf->b_flags & BF_DUMMY))
2955#ifdef UNIX
2956 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002958 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959#endif
2960 if (obuf != NULL && obuf != buf)
2961 {
2962 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2963 {
2964 if (message)
2965 EMSG(_("E95: Buffer with this name already exists"));
2966 vim_free(ffname);
2967 return FAIL;
2968 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002969 /* delete from the list */
2970 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 }
2972 sfname = vim_strsave(sfname);
2973 if (ffname == NULL || sfname == NULL)
2974 {
2975 vim_free(sfname);
2976 vim_free(ffname);
2977 return FAIL;
2978 }
2979#ifdef USE_FNAME_CASE
2980# ifdef USE_LONG_FNAME
2981 if (USE_LONG_FNAME)
2982# endif
2983 fname_case(sfname, 0); /* set correct case for short file name */
2984#endif
2985 vim_free(buf->b_ffname);
2986 vim_free(buf->b_sfname);
2987 buf->b_ffname = ffname;
2988 buf->b_sfname = sfname;
2989 }
2990 buf->b_fname = buf->b_sfname;
2991#ifdef UNIX
2992 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002993 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 else
2995 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002996 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 buf->b_dev = st.st_dev;
2998 buf->b_ino = st.st_ino;
2999 }
3000#endif
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 buf->b_shortname = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003003
3004 buf_name_changed(buf);
3005 return OK;
3006}
3007
3008/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00003009 * Crude way of changing the name of a buffer. Use with care!
3010 * The name should be relative to the current directory.
3011 */
3012 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003013buf_set_name(int fnum, char_u *name)
Bram Moolenaar86b68352004-12-27 21:59:20 +00003014{
3015 buf_T *buf;
3016
3017 buf = buflist_findnr(fnum);
3018 if (buf != NULL)
3019 {
3020 vim_free(buf->b_sfname);
3021 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00003022 buf->b_ffname = vim_strsave(name);
3023 buf->b_sfname = NULL;
3024 /* Allocate ffname and expand into full path. Also resolves .lnk
3025 * files on Win32. */
3026 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00003027 buf->b_fname = buf->b_sfname;
3028 }
3029}
3030
3031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 * Take care of what needs to be done when the name of buffer "buf" has
3033 * changed.
3034 */
3035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003036buf_name_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037{
3038 /*
3039 * If the file name changed, also change the name of the swapfile
3040 */
3041 if (buf->b_ml.ml_mfp != NULL)
3042 ml_setname(buf);
3043
3044 if (curwin->w_buffer == buf)
3045 check_arg_idx(curwin); /* check file name for arg list */
3046#ifdef FEAT_TITLE
3047 maketitle(); /* set window title */
3048#endif
3049#ifdef FEAT_WINDOWS
3050 status_redraw_all(); /* status lines need to be redrawn */
3051#endif
3052 fmarks_check_names(buf); /* check named file marks */
3053 ml_timestamp(buf); /* reset timestamp */
3054}
3055
3056/*
3057 * set alternate file name for current window
3058 *
3059 * Used by do_one_cmd(), do_write() and do_ecmd().
3060 * Return the buffer.
3061 */
3062 buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003063setaltfname(
3064 char_u *ffname,
3065 char_u *sfname,
3066 linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067{
3068 buf_T *buf;
3069
3070 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3071 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003072 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 curwin->w_alt_fnum = buf->b_fnum;
3074 return buf;
3075}
3076
3077/*
3078 * Get alternate file name for current window.
3079 * Return NULL if there isn't any, and give error message if requested.
3080 */
3081 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003082getaltfname(
3083 int errmsg) /* give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084{
3085 char_u *fname;
3086 linenr_T dummy;
3087
3088 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3089 {
3090 if (errmsg)
3091 EMSG(_(e_noalt));
3092 return NULL;
3093 }
3094 return fname;
3095}
3096
3097/*
3098 * Add a file name to the buflist and return its number.
3099 * Uses same flags as buflist_new(), except BLN_DUMMY.
3100 *
3101 * used by qf_init(), main() and doarglist()
3102 */
3103 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003104buflist_add(char_u *fname, int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105{
3106 buf_T *buf;
3107
3108 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3109 if (buf != NULL)
3110 return buf->b_fnum;
3111 return 0;
3112}
3113
3114#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3115/*
3116 * Adjust slashes in file names. Called after 'shellslash' was set.
3117 */
3118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003119buflist_slash_adjust(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120{
3121 buf_T *bp;
3122
3123 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3124 {
3125 if (bp->b_ffname != NULL)
3126 slash_adjust(bp->b_ffname);
3127 if (bp->b_sfname != NULL)
3128 slash_adjust(bp->b_sfname);
3129 }
3130}
3131#endif
3132
3133/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003134 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 * Also save the local window option values.
3136 */
3137 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003138buflist_altfpos(win_T *win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003140 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141}
3142
3143/*
3144 * Return TRUE if 'ffname' is not the same file as current file.
3145 * Fname must have a full path (expanded by mch_FullName()).
3146 */
3147 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003148otherfile(char_u *ffname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149{
3150 return otherfile_buf(curbuf, ffname
3151#ifdef UNIX
3152 , NULL
3153#endif
3154 );
3155}
3156
3157 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003158otherfile_buf(
3159 buf_T *buf,
3160 char_u *ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161#ifdef UNIX
Bram Moolenaar8767f522016-07-01 17:17:39 +02003162 , stat_T *stp
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +01003164 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165{
3166 /* no name is different */
3167 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3168 return TRUE;
3169 if (fnamecmp(ffname, buf->b_ffname) == 0)
3170 return FALSE;
3171#ifdef UNIX
3172 {
Bram Moolenaar8767f522016-07-01 17:17:39 +02003173 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174
Bram Moolenaar8767f522016-07-01 17:17:39 +02003175 /* If no stat_T given, get it now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 if (stp == NULL)
3177 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003178 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 st.st_dev = (dev_T)-1;
3180 stp = &st;
3181 }
3182 /* Use dev/ino to check if the files are the same, even when the names
3183 * are different (possible with links). Still need to compare the
3184 * name above, for when the file doesn't exist yet.
3185 * Problem: The dev/ino changes when a file is deleted (and created
3186 * again) and remains the same when renamed/moved. We don't want to
3187 * mch_stat() each buffer each time, that would be too slow. Get the
3188 * dev/ino again when they appear to match, but not when they appear
3189 * to be different: Could skip a buffer when it's actually the same
3190 * file. */
3191 if (buf_same_ino(buf, stp))
3192 {
3193 buf_setino(buf);
3194 if (buf_same_ino(buf, stp))
3195 return FALSE;
3196 }
3197 }
3198#endif
3199 return TRUE;
3200}
3201
3202#if defined(UNIX) || defined(PROTO)
3203/*
3204 * Set inode and device number for a buffer.
3205 * Must always be called when b_fname is changed!.
3206 */
3207 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003208buf_setino(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
Bram Moolenaar8767f522016-07-01 17:17:39 +02003210 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211
3212 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3213 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003214 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 buf->b_dev = st.st_dev;
3216 buf->b_ino = st.st_ino;
3217 }
3218 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003219 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220}
3221
3222/*
3223 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3224 */
3225 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226buf_same_ino(
3227 buf_T *buf,
Bram Moolenaar8767f522016-07-01 17:17:39 +02003228 stat_T *stp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003230 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 && stp->st_dev == buf->b_dev
3232 && stp->st_ino == buf->b_ino);
3233}
3234#endif
3235
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003236/*
3237 * Print info about the current buffer.
3238 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240fileinfo(
3241 int fullname, /* when non-zero print full path */
3242 int shorthelp,
3243 int dont_truncate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 char_u *name;
3246 int n;
3247 char_u *p;
3248 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003249 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250
3251 buffer = alloc(IOSIZE);
3252 if (buffer == NULL)
3253 return;
3254
3255 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3256 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003257 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 p = buffer + STRLEN(buffer);
3259 }
3260 else
3261 p = buffer;
3262
3263 *p++ = '"';
3264 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003265 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 else
3267 {
3268 if (!fullname && curbuf->b_fname != NULL)
3269 name = curbuf->b_fname;
3270 else
3271 name = curbuf->b_ffname;
3272 home_replace(shorthelp ? curbuf : NULL, name, p,
3273 (int)(IOSIZE - (p - buffer)), TRUE);
3274 }
3275
Bram Moolenaara800b422010-06-27 01:15:55 +02003276 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 curbufIsChanged() ? (shortmess(SHM_MOD)
3278 ? " [+]" : _(" [Modified]")) : " ",
3279 (curbuf->b_flags & BF_NOTEDITED)
3280#ifdef FEAT_QUICKFIX
3281 && !bt_dontwrite(curbuf)
3282#endif
3283 ? _("[Not edited]") : "",
3284 (curbuf->b_flags & BF_NEW)
3285#ifdef FEAT_QUICKFIX
3286 && !bt_dontwrite(curbuf)
3287#endif
3288 ? _("[New file]") : "",
3289 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003290 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 : _("[readonly]")) : "",
3292 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3293 || curbuf->b_p_ro) ?
3294 " " : "");
3295 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3296 * causes an overflow, thus for large numbers divide instead. */
3297 if (curwin->w_cursor.lnum > 1000000L)
3298 n = (int)(((long)curwin->w_cursor.lnum) /
3299 ((long)curbuf->b_ml.ml_line_count / 100L));
3300 else
3301 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3302 (long)curbuf->b_ml.ml_line_count);
3303 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3304 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003305 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003306 }
3307#ifdef FEAT_CMDL_INFO
3308 else if (p_ru)
3309 {
3310 /* Current line and column are already on the screen -- webb */
3311 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003312 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003314 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 (long)curbuf->b_ml.ml_line_count, n);
3316 }
3317#endif
3318 else
3319 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003320 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 _("line %ld of %ld --%d%%-- col "),
3322 (long)curwin->w_cursor.lnum,
3323 (long)curbuf->b_ml.ml_line_count,
3324 n);
3325 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003326 len = STRLEN(buffer);
3327 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3329 }
3330
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003331 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
3333 if (dont_truncate)
3334 {
3335 /* Temporarily set msg_scroll to avoid the message being truncated.
3336 * First call msg_start() to get the message in the right place. */
3337 msg_start();
3338 n = msg_scroll;
3339 msg_scroll = TRUE;
3340 msg(buffer);
3341 msg_scroll = n;
3342 }
3343 else
3344 {
3345 p = msg_trunc_attr(buffer, FALSE, 0);
3346 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 /* Need to repeat the message after redrawing when:
3348 * - When restart_edit is set (otherwise there will be a delay
3349 * before redrawing).
3350 * - When the screen was scrolled but there is no wait-return
3351 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003352 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353 }
3354
3355 vim_free(buffer);
3356}
3357
3358 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359col_print(
3360 char_u *buf,
3361 size_t buflen,
3362 int col,
3363 int vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364{
3365 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003366 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003368 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369}
3370
3371#if defined(FEAT_TITLE) || defined(PROTO)
3372/*
3373 * put file name in title bar of window and in icon title
3374 */
3375
3376static char_u *lasttitle = NULL;
3377static char_u *lasticon = NULL;
3378
3379 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003380maketitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 char_u *p;
3383 char_u *t_str = NULL;
3384 char_u *i_name;
3385 char_u *i_str = NULL;
3386 int maxlen = 0;
3387 int len;
3388 int mustset;
3389 char_u buf[IOSIZE];
3390 int off;
3391
3392 if (!redrawing())
3393 {
3394 /* Postpone updating the title when 'lazyredraw' is set. */
3395 need_maketitle = TRUE;
3396 return;
3397 }
3398
3399 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003400 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 return;
3402
3403 if (p_title)
3404 {
3405 if (p_titlelen > 0)
3406 {
3407 maxlen = p_titlelen * Columns / 100;
3408 if (maxlen < 10)
3409 maxlen = 10;
3410 }
3411
3412 t_str = buf;
3413 if (*p_titlestring != NUL)
3414 {
3415#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003416 if (stl_syntax & STL_IN_TITLE)
3417 {
3418 int use_sandbox = FALSE;
3419 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003420
3421# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003422 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003423# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003424 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003426 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003427 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003428 if (called_emsg)
3429 set_string_option_direct((char_u *)"titlestring", -1,
3430 (char_u *)"", OPT_FREE, SID_ERROR);
3431 called_emsg |= save_called_emsg;
3432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 else
3434#endif
3435 t_str = p_titlestring;
3436 }
3437 else
3438 {
3439 /* format: "fname + (path) (1 of 2) - VIM" */
3440
Bram Moolenaar2c666692012-09-05 13:30:40 +02003441#define SPACE_FOR_FNAME (IOSIZE - 100)
3442#define SPACE_FOR_DIR (IOSIZE - 20)
3443#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003445 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 else
3447 {
3448 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003449 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 }
3452
3453 switch (bufIsChanged(curbuf)
3454 + (curbuf->b_p_ro * 2)
3455 + (!curbuf->b_p_ma * 4))
3456 {
3457 case 1: STRCAT(buf, " +"); break;
3458 case 2: STRCAT(buf, " ="); break;
3459 case 3: STRCAT(buf, " =+"); break;
3460 case 4:
3461 case 6: STRCAT(buf, " -"); break;
3462 case 5:
3463 case 7: STRCAT(buf, " -+"); break;
3464 }
3465
3466 if (curbuf->b_fname != NULL)
3467 {
3468 /* Get path of file, replace home dir with ~ */
3469 off = (int)STRLEN(buf);
3470 buf[off++] = ' ';
3471 buf[off++] = '(';
3472 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003473 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474#ifdef BACKSLASH_IN_FILENAME
3475 /* avoid "c:/name" to be reduced to "c" */
3476 if (isalpha(buf[off]) && buf[off + 1] == ':')
3477 off += 2;
3478#endif
3479 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003480 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003483 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003484 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003487
Bram Moolenaar2c666692012-09-05 13:30:40 +02003488 /* Translate unprintable chars and concatenate. Keep some
3489 * room for the server name. When there is no room (very long
3490 * file name) use (...). */
3491 if (off < SPACE_FOR_DIR)
3492 {
3493 p = transstr(buf + off);
3494 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3495 vim_free(p);
3496 }
3497 else
3498 {
3499 vim_strncpy(buf + off, (char_u *)"...",
3500 (size_t)(SPACE_FOR_ARGNR - off));
3501 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 STRCAT(buf, ")");
3503 }
3504
Bram Moolenaar2c666692012-09-05 13:30:40 +02003505 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507#if defined(FEAT_CLIENTSERVER)
3508 if (serverName != NULL)
3509 {
3510 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003511 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 }
3513 else
3514#endif
3515 STRCAT(buf, " - VIM");
3516
3517 if (maxlen > 0)
3518 {
3519 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003520 if (vim_strsize(buf) > maxlen)
3521 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 }
3523 }
3524 }
3525 mustset = ti_change(t_str, &lasttitle);
3526
3527 if (p_icon)
3528 {
3529 i_str = buf;
3530 if (*p_iconstring != NUL)
3531 {
3532#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003533 if (stl_syntax & STL_IN_ICON)
3534 {
3535 int use_sandbox = FALSE;
3536 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003537
3538# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003539 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003540# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003541 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003543 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003544 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003545 if (called_emsg)
3546 set_string_option_direct((char_u *)"iconstring", -1,
3547 (char_u *)"", OPT_FREE, SID_ERROR);
3548 called_emsg |= save_called_emsg;
3549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 else
3551#endif
3552 i_str = p_iconstring;
3553 }
3554 else
3555 {
3556 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003557 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 else /* use file name only in icon */
3559 i_name = gettail(curbuf->b_ffname);
3560 *i_str = NUL;
3561 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003562 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 if (len > 100)
3564 {
3565 len -= 100;
3566#ifdef FEAT_MBYTE
3567 if (has_mbyte)
3568 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3569#endif
3570 i_name += len;
3571 }
3572 STRCPY(i_str, i_name);
3573 trans_characters(i_str, IOSIZE);
3574 }
3575 }
3576
3577 mustset |= ti_change(i_str, &lasticon);
3578
3579 if (mustset)
3580 resettitle();
3581}
3582
3583/*
3584 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3585 * from "str" if it does.
3586 * Return TRUE when "*last" changed.
3587 */
3588 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003589ti_change(char_u *str, char_u **last)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590{
3591 if ((str == NULL) != (*last == NULL)
3592 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3593 {
3594 vim_free(*last);
3595 if (str == NULL)
3596 *last = NULL;
3597 else
3598 *last = vim_strsave(str);
3599 return TRUE;
3600 }
3601 return FALSE;
3602}
3603
3604/*
3605 * Put current window title back (used after calling a shell)
3606 */
3607 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003608resettitle(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609{
3610 mch_settitle(lasttitle, lasticon);
3611}
Bram Moolenaarea408852005-06-25 22:49:46 +00003612
3613# if defined(EXITFREE) || defined(PROTO)
3614 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003615free_titles(void)
Bram Moolenaarea408852005-06-25 22:49:46 +00003616{
3617 vim_free(lasttitle);
3618 vim_free(lasticon);
3619}
3620# endif
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622#endif /* FEAT_TITLE */
3623
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003624#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003626 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 * Return length of string in screen cells.
3628 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003629 * Normally works for window "wp", except when working for 'tabline' then it
3630 * is "curwin".
3631 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 * Items are drawn interspersed with the text that surrounds it
3633 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3634 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3635 *
3636 * If maxwidth is not zero, the string will be filled at any middle marker
3637 * or truncated if too long, fillchar is used for all whitespace.
3638 */
3639 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003640build_stl_str_hl(
3641 win_T *wp,
3642 char_u *out, /* buffer to write into != NameBuff */
3643 size_t outlen, /* length of out[] */
3644 char_u *fmt,
3645 int use_sandbox UNUSED, /* "fmt" was set insecurely, use sandbox */
3646 int fillchar,
3647 int maxwidth,
3648 struct stl_hlrec *hltab, /* return: HL attributes (can be NULL) */
3649 struct stl_hlrec *tabtab) /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650{
3651 char_u *p;
3652 char_u *s;
3653 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003654 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655#ifdef FEAT_EVAL
3656 win_T *o_curwin;
3657 buf_T *o_curbuf;
3658#endif
3659 int empty_line;
3660 colnr_T virtcol;
3661 long l;
3662 long n;
3663 int prevchar_isflag;
3664 int prevchar_isitem;
3665 int itemisflag;
3666 int fillable;
3667 char_u *str;
3668 long num;
3669 int width;
3670 int itemcnt;
3671 int curitem;
3672 int groupitem[STL_MAX_ITEM];
3673 int groupdepth;
3674 struct stl_item
3675 {
3676 char_u *start;
3677 int minwid;
3678 int maxwid;
3679 enum
3680 {
3681 Normal,
3682 Empty,
3683 Group,
3684 Middle,
3685 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003686 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 Trunc
3688 } type;
3689 } item[STL_MAX_ITEM];
3690 int minwid;
3691 int maxwid;
3692 int zeropad;
3693 char_u base;
3694 char_u opt;
3695#define TMPLEN 70
3696 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003697 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003698 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003699
3700#ifdef FEAT_EVAL
3701 /*
3702 * When the format starts with "%!" then evaluate it as an expression and
3703 * use the result as the actual format string.
3704 */
3705 if (fmt[0] == '%' && fmt[1] == '!')
3706 {
3707 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3708 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003709 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003710 }
3711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
3713 if (fillchar == 0)
3714 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003715#ifdef FEAT_MBYTE
3716 /* Can't handle a multi-byte fill character yet. */
3717 else if (mb_char2len(fillchar) > 1)
3718 fillchar = '-';
3719#endif
3720
Bram Moolenaar567199b2013-04-24 16:52:36 +02003721 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3722 * p will become invalid when getting another buffer line. */
3723 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3724 empty_line = (*p == NUL);
3725
3726 /* Get the byte value now, in case we need it below. This is more
3727 * efficient than making a copy of the line. */
3728 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3729 byteval = 0;
3730 else
3731#ifdef FEAT_MBYTE
3732 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3733#else
3734 byteval = p[wp->w_cursor.col];
3735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
3737 groupdepth = 0;
3738 p = out;
3739 curitem = 0;
3740 prevchar_isflag = TRUE;
3741 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003742 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003744 if (curitem == STL_MAX_ITEM)
3745 {
3746 /* There are too many items. Add the error code to the statusline
3747 * to give the user a hint about what went wrong. */
3748 if (p + 6 < out + outlen)
3749 {
3750 mch_memmove(p, " E541", (size_t)5);
3751 p += 5;
3752 }
3753 break;
3754 }
3755
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 if (*s != NUL && *s != '%')
3757 prevchar_isflag = prevchar_isitem = FALSE;
3758
3759 /*
3760 * Handle up to the next '%' or the end.
3761 */
3762 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3763 *p++ = *s++;
3764 if (*s == NUL || p + 1 >= out + outlen)
3765 break;
3766
3767 /*
3768 * Handle one '%' item.
3769 */
3770 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003771 if (*s == NUL) /* ignore trailing % */
3772 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (*s == '%')
3774 {
3775 if (p + 1 >= out + outlen)
3776 break;
3777 *p++ = *s++;
3778 prevchar_isflag = prevchar_isitem = FALSE;
3779 continue;
3780 }
3781 if (*s == STL_MIDDLEMARK)
3782 {
3783 s++;
3784 if (groupdepth > 0)
3785 continue;
3786 item[curitem].type = Middle;
3787 item[curitem++].start = p;
3788 continue;
3789 }
3790 if (*s == STL_TRUNCMARK)
3791 {
3792 s++;
3793 item[curitem].type = Trunc;
3794 item[curitem++].start = p;
3795 continue;
3796 }
3797 if (*s == ')')
3798 {
3799 s++;
3800 if (groupdepth < 1)
3801 continue;
3802 groupdepth--;
3803
3804 t = item[groupitem[groupdepth]].start;
3805 *p = NUL;
3806 l = vim_strsize(t);
3807 if (curitem > groupitem[groupdepth] + 1
3808 && item[groupitem[groupdepth]].minwid == 0)
3809 {
3810 /* remove group if all items are empty */
3811 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
Bram Moolenaaraf6e36f2016-03-08 12:56:33 +01003812 if (item[n].type == Normal || item[n].type == Highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 break;
3814 if (n == curitem)
3815 {
3816 p = t;
3817 l = 0;
3818 }
3819 }
3820 if (l > item[groupitem[groupdepth]].maxwid)
3821 {
3822 /* truncate, remove n bytes of text at the start */
3823#ifdef FEAT_MBYTE
3824 if (has_mbyte)
3825 {
3826 /* Find the first character that should be included. */
3827 n = 0;
3828 while (l >= item[groupitem[groupdepth]].maxwid)
3829 {
3830 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003831 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 }
3833 }
3834 else
3835#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003836 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837
3838 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003839 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840 p = p - n + 1;
3841#ifdef FEAT_MBYTE
3842 /* Fill up space left over by half a double-wide char. */
3843 while (++l < item[groupitem[groupdepth]].minwid)
3844 *p++ = fillchar;
3845#endif
3846
3847 /* correct the start of the items for the truncation */
3848 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3849 {
3850 item[l].start -= n;
3851 if (item[l].start < t)
3852 item[l].start = t;
3853 }
3854 }
3855 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3856 {
3857 /* fill */
3858 n = item[groupitem[groupdepth]].minwid;
3859 if (n < 0)
3860 {
3861 /* fill by appending characters */
3862 n = 0 - n;
3863 while (l++ < n && p + 1 < out + outlen)
3864 *p++ = fillchar;
3865 }
3866 else
3867 {
3868 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003869 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 l = n - l;
3871 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003872 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 p += l;
3874 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3875 item[n].start += l;
3876 for ( ; l > 0; l--)
3877 *t++ = fillchar;
3878 }
3879 }
3880 continue;
3881 }
3882 minwid = 0;
3883 maxwid = 9999;
3884 zeropad = FALSE;
3885 l = 1;
3886 if (*s == '0')
3887 {
3888 s++;
3889 zeropad = TRUE;
3890 }
3891 if (*s == '-')
3892 {
3893 s++;
3894 l = -1;
3895 }
3896 if (VIM_ISDIGIT(*s))
3897 {
3898 minwid = (int)getdigits(&s);
3899 if (minwid < 0) /* overflow */
3900 minwid = 0;
3901 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003902 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 {
3904 item[curitem].type = Highlight;
3905 item[curitem].start = p;
3906 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3907 s++;
3908 curitem++;
3909 continue;
3910 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003911 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3912 {
3913 if (*s == STL_TABCLOSENR)
3914 {
3915 if (minwid == 0)
3916 {
3917 /* %X ends the close label, go back to the previously
3918 * define tab label nr. */
3919 for (n = curitem - 1; n >= 0; --n)
3920 if (item[n].type == TabPage && item[n].minwid >= 0)
3921 {
3922 minwid = item[n].minwid;
3923 break;
3924 }
3925 }
3926 else
3927 /* close nrs are stored as negative values */
3928 minwid = - minwid;
3929 }
3930 item[curitem].type = TabPage;
3931 item[curitem].start = p;
3932 item[curitem].minwid = minwid;
3933 s++;
3934 curitem++;
3935 continue;
3936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 if (*s == '.')
3938 {
3939 s++;
3940 if (VIM_ISDIGIT(*s))
3941 {
3942 maxwid = (int)getdigits(&s);
3943 if (maxwid <= 0) /* overflow */
3944 maxwid = 50;
3945 }
3946 }
3947 minwid = (minwid > 50 ? 50 : minwid) * l;
3948 if (*s == '(')
3949 {
3950 groupitem[groupdepth++] = curitem;
3951 item[curitem].type = Group;
3952 item[curitem].start = p;
3953 item[curitem].minwid = minwid;
3954 item[curitem].maxwid = maxwid;
3955 s++;
3956 curitem++;
3957 continue;
3958 }
3959 if (vim_strchr(STL_ALL, *s) == NULL)
3960 {
3961 s++;
3962 continue;
3963 }
3964 opt = *s++;
3965
3966 /* OK - now for the real work */
3967 base = 'D';
3968 itemisflag = FALSE;
3969 fillable = TRUE;
3970 num = -1;
3971 str = NULL;
3972 switch (opt)
3973 {
3974 case STL_FILEPATH:
3975 case STL_FULLPATH:
3976 case STL_FILENAME:
3977 fillable = FALSE; /* don't change ' ' to fillchar */
3978 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003979 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 else
3981 {
3982 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003983 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3985 }
3986 trans_characters(NameBuff, MAXPATHL);
3987 if (opt != STL_FILENAME)
3988 str = NameBuff;
3989 else
3990 str = gettail(NameBuff);
3991 break;
3992
3993 case STL_VIM_EXPR: /* '{' */
3994 itemisflag = TRUE;
3995 t = p;
3996 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3997 *p++ = *s++;
3998 if (*s != '}') /* missing '}' or out of space */
3999 break;
4000 s++;
4001 *p = 0;
4002 p = t;
4003
4004#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004005 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 set_internal_string_var((char_u *)"actual_curbuf", tmp);
4007
4008 o_curbuf = curbuf;
4009 o_curwin = curwin;
4010 curwin = wp;
4011 curbuf = wp->w_buffer;
4012
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00004013 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014
4015 curwin = o_curwin;
4016 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00004017 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019 if (str != NULL && *str != 0)
4020 {
4021 if (*skipdigits(str) == NUL)
4022 {
4023 num = atoi((char *)str);
4024 vim_free(str);
4025 str = NULL;
4026 itemisflag = FALSE;
4027 }
4028 }
4029#endif
4030 break;
4031
4032 case STL_LINE:
4033 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
4034 ? 0L : (long)(wp->w_cursor.lnum);
4035 break;
4036
4037 case STL_NUMLINES:
4038 num = wp->w_buffer->b_ml.ml_line_count;
4039 break;
4040
4041 case STL_COLUMN:
4042 num = !(State & INSERT) && empty_line
4043 ? 0 : (int)wp->w_cursor.col + 1;
4044 break;
4045
4046 case STL_VIRTCOL:
4047 case STL_VIRTCOL_ALT:
4048 /* In list mode virtcol needs to be recomputed */
4049 virtcol = wp->w_virtcol;
4050 if (wp->w_p_list && lcs_tab1 == NUL)
4051 {
4052 wp->w_p_list = FALSE;
4053 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
4054 wp->w_p_list = TRUE;
4055 }
4056 ++virtcol;
4057 /* Don't display %V if it's the same as %c. */
4058 if (opt == STL_VIRTCOL_ALT
4059 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4060 ? 0 : (int)wp->w_cursor.col + 1)))
4061 break;
4062 num = (long)virtcol;
4063 break;
4064
4065 case STL_PERCENTAGE:
4066 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4067 (long)wp->w_buffer->b_ml.ml_line_count);
4068 break;
4069
4070 case STL_ALTPERCENT:
4071 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004072 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 break;
4074
4075 case STL_ARGLISTSTAT:
4076 fillable = FALSE;
4077 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004078 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 str = tmp;
4080 break;
4081
4082 case STL_KEYMAP:
4083 fillable = FALSE;
4084 if (get_keymap_str(wp, tmp, TMPLEN))
4085 str = tmp;
4086 break;
4087 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004088#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004089 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090#else
4091 num = 0;
4092#endif
4093 break;
4094
4095 case STL_BUFNO:
4096 num = wp->w_buffer->b_fnum;
4097 break;
4098
4099 case STL_OFFSET_X:
4100 base = 'X';
4101 case STL_OFFSET:
4102#ifdef FEAT_BYTEOFF
4103 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4104 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4105 0L : l + 1 + (!(State & INSERT) && empty_line ?
4106 0 : (int)wp->w_cursor.col);
4107#endif
4108 break;
4109
4110 case STL_BYTEVAL_X:
4111 base = 'X';
4112 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004113 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 if (num == NL)
4115 num = 0;
4116 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4117 num = NL;
4118 break;
4119
4120 case STL_ROFLAG:
4121 case STL_ROFLAG_ALT:
4122 itemisflag = TRUE;
4123 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004124 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 break;
4126
4127 case STL_HELPFLAG:
4128 case STL_HELPFLAG_ALT:
4129 itemisflag = TRUE;
4130 if (wp->w_buffer->b_help)
4131 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004132 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134
4135#ifdef FEAT_AUTOCMD
4136 case STL_FILETYPE:
4137 if (*wp->w_buffer->b_p_ft != NUL
4138 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4139 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004140 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4141 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 str = tmp;
4143 }
4144 break;
4145
4146 case STL_FILETYPE_ALT:
4147 itemisflag = TRUE;
4148 if (*wp->w_buffer->b_p_ft != NUL
4149 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4150 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004151 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4152 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 for (t = tmp; *t != 0; t++)
4154 *t = TOUPPER_LOC(*t);
4155 str = tmp;
4156 }
4157 break;
4158#endif
4159
4160#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4161 case STL_PREVIEWFLAG:
4162 case STL_PREVIEWFLAG_ALT:
4163 itemisflag = TRUE;
4164 if (wp->w_p_pvw)
4165 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4166 : _("[Preview]"));
4167 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004168
4169 case STL_QUICKFIX:
4170 if (bt_quickfix(wp->w_buffer))
4171 str = (char_u *)(wp->w_llist_ref
4172 ? _(msg_loclist)
4173 : _(msg_qflist));
4174 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175#endif
4176
4177 case STL_MODIFIED:
4178 case STL_MODIFIED_ALT:
4179 itemisflag = TRUE;
4180 switch ((opt == STL_MODIFIED_ALT)
4181 + bufIsChanged(wp->w_buffer) * 2
4182 + (!wp->w_buffer->b_p_ma) * 4)
4183 {
4184 case 2: str = (char_u *)"[+]"; break;
4185 case 3: str = (char_u *)",+"; break;
4186 case 4: str = (char_u *)"[-]"; break;
4187 case 5: str = (char_u *)",-"; break;
4188 case 6: str = (char_u *)"[+-]"; break;
4189 case 7: str = (char_u *)",+-"; break;
4190 }
4191 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004192
4193 case STL_HIGHLIGHT:
4194 t = s;
4195 while (*s != '#' && *s != NUL)
4196 ++s;
4197 if (*s == '#')
4198 {
4199 item[curitem].type = Highlight;
4200 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004201 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004202 curitem++;
4203 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004204 if (*s != NUL)
4205 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004206 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 }
4208
4209 item[curitem].start = p;
4210 item[curitem].type = Normal;
4211 if (str != NULL && *str)
4212 {
4213 t = str;
4214 if (itemisflag)
4215 {
4216 if ((t[0] && t[1])
4217 && ((!prevchar_isitem && *t == ',')
4218 || (prevchar_isflag && *t == ' ')))
4219 t++;
4220 prevchar_isflag = TRUE;
4221 }
4222 l = vim_strsize(t);
4223 if (l > 0)
4224 prevchar_isitem = TRUE;
4225 if (l > maxwid)
4226 {
4227 while (l >= maxwid)
4228#ifdef FEAT_MBYTE
4229 if (has_mbyte)
4230 {
4231 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004232 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234 else
4235#endif
4236 l -= byte2cells(*t++);
4237 if (p + 1 >= out + outlen)
4238 break;
4239 *p++ = '<';
4240 }
4241 if (minwid > 0)
4242 {
4243 for (; l < minwid && p + 1 < out + outlen; l++)
4244 {
4245 /* Don't put a "-" in front of a digit. */
4246 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4247 *p++ = ' ';
4248 else
4249 *p++ = fillchar;
4250 }
4251 minwid = 0;
4252 }
4253 else
4254 minwid *= -1;
4255 while (*t && p + 1 < out + outlen)
4256 {
4257 *p++ = *t++;
4258 /* Change a space by fillchar, unless fillchar is '-' and a
4259 * digit follows. */
4260 if (fillable && p[-1] == ' '
4261 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4262 p[-1] = fillchar;
4263 }
4264 for (; l < minwid && p + 1 < out + outlen; l++)
4265 *p++ = fillchar;
4266 }
4267 else if (num >= 0)
4268 {
4269 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4270 char_u nstr[20];
4271
4272 if (p + 20 >= out + outlen)
4273 break; /* not sufficient space */
4274 prevchar_isitem = TRUE;
4275 t = nstr;
4276 if (opt == STL_VIRTCOL_ALT)
4277 {
4278 *t++ = '-';
4279 minwid--;
4280 }
4281 *t++ = '%';
4282 if (zeropad)
4283 *t++ = '0';
4284 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004285 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 *t = 0;
4287
4288 for (n = num, l = 1; n >= nbase; n /= nbase)
4289 l++;
4290 if (opt == STL_VIRTCOL_ALT)
4291 l++;
4292 if (l > maxwid)
4293 {
4294 l += 2;
4295 n = l - maxwid;
4296 while (l-- > maxwid)
4297 num /= nbase;
4298 *t++ = '>';
4299 *t++ = '%';
4300 *t = t[-3];
4301 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004302 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4303 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 }
4305 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004306 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4307 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 p += STRLEN(p);
4309 }
4310 else
4311 item[curitem].type = Empty;
4312
4313 if (opt == STL_VIM_EXPR)
4314 vim_free(str);
4315
4316 if (num >= 0 || (!itemisflag && str && *str))
4317 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4318 curitem++;
4319 }
4320 *p = NUL;
4321 itemcnt = curitem;
4322
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004323#ifdef FEAT_EVAL
4324 if (usefmt != fmt)
4325 vim_free(usefmt);
4326#endif
4327
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 width = vim_strsize(out);
4329 if (maxwidth > 0 && width > maxwidth)
4330 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004331 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 l = 0;
4333 if (itemcnt == 0)
4334 s = out;
4335 else
4336 {
4337 for ( ; l < itemcnt; l++)
4338 if (item[l].type == Trunc)
4339 {
4340 /* Truncate at %< item. */
4341 s = item[l].start;
4342 break;
4343 }
4344 if (l == itemcnt)
4345 {
4346 /* No %< item, truncate first item. */
4347 s = item[0].start;
4348 l = 0;
4349 }
4350 }
4351
4352 if (width - vim_strsize(s) >= maxwidth)
4353 {
4354 /* Truncation mark is beyond max length */
4355#ifdef FEAT_MBYTE
4356 if (has_mbyte)
4357 {
4358 s = out;
4359 width = 0;
4360 for (;;)
4361 {
4362 width += ptr2cells(s);
4363 if (width >= maxwidth)
4364 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004365 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 /* Fill up for half a double-wide character. */
4368 while (++width < maxwidth)
4369 *s++ = fillchar;
4370 }
4371 else
4372#endif
4373 s = out + maxwidth - 1;
4374 for (l = 0; l < itemcnt; l++)
4375 if (item[l].start > s)
4376 break;
4377 itemcnt = l;
4378 *s++ = '>';
4379 *s = 0;
4380 }
4381 else
4382 {
4383#ifdef FEAT_MBYTE
4384 if (has_mbyte)
4385 {
4386 n = 0;
4387 while (width >= maxwidth)
4388 {
4389 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004390 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 }
4392 }
4393 else
4394#endif
4395 n = width - maxwidth + 1;
4396 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004397 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 *s = '<';
4399
4400 /* Fill up for half a double-wide character. */
4401 while (++width < maxwidth)
4402 {
4403 s = s + STRLEN(s);
4404 *s++ = fillchar;
4405 *s = NUL;
4406 }
4407
4408 --n; /* count the '<' */
4409 for (; l < itemcnt; l++)
4410 {
4411 if (item[l].start - n >= s)
4412 item[l].start -= n;
4413 else
4414 item[l].start = s;
4415 }
4416 }
4417 width = maxwidth;
4418 }
4419 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4420 {
4421 /* Apply STL_MIDDLE if any */
4422 for (l = 0; l < itemcnt; l++)
4423 if (item[l].type == Middle)
4424 break;
4425 if (l < itemcnt)
4426 {
4427 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004428 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 for (s = item[l].start; s < p; s++)
4430 *s = fillchar;
4431 for (l++; l < itemcnt; l++)
4432 item[l].start += maxwidth - width;
4433 width = maxwidth;
4434 }
4435 }
4436
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004437 /* Store the info about highlighting. */
4438 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004440 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 for (l = 0; l < itemcnt; l++)
4442 {
4443 if (item[l].type == Highlight)
4444 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004445 sp->start = item[l].start;
4446 sp->userhl = item[l].minwid;
4447 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 }
4449 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004450 sp->start = NULL;
4451 sp->userhl = 0;
4452 }
4453
4454 /* Store the info about tab pages labels. */
4455 if (tabtab != NULL)
4456 {
4457 sp = tabtab;
4458 for (l = 0; l < itemcnt; l++)
4459 {
4460 if (item[l].type == TabPage)
4461 {
4462 sp->start = item[l].start;
4463 sp->userhl = item[l].minwid;
4464 sp++;
4465 }
4466 }
4467 sp->start = NULL;
4468 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 }
4470
4471 return width;
4472}
4473#endif /* FEAT_STL_OPT */
4474
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004475#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4476 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004478 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4479 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 */
4481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004482get_rel_pos(
4483 win_T *wp,
4484 char_u *buf,
4485 int buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486{
4487 long above; /* number of lines above window */
4488 long below; /* number of lines below window */
4489
Bram Moolenaar0027c212015-01-07 13:31:52 +01004490 if (buflen < 3) /* need at least 3 chars for writing */
4491 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 above = wp->w_topline - 1;
4493#ifdef FEAT_DIFF
4494 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004495 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4496 above = 0; /* All buffer lines are displayed and there is an
4497 * indication of filler lines, that can be considered
4498 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499#endif
4500 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4501 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004502 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4503 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004505 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004507 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 ? (int)(above / ((above + below) / 100L))
4509 : (int)(above * 100L / (above + below)));
4510}
4511#endif
4512
4513/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004514 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 * Return TRUE if it was appended.
4516 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004517 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004518append_arg_number(
4519 win_T *wp,
4520 char_u *buf,
4521 int buflen,
4522 int add_file) /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523{
4524 char_u *p;
4525
4526 if (ARGCOUNT <= 1) /* nothing to do */
4527 return FALSE;
4528
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004529 p = buf + STRLEN(buf); /* go to the end of the buffer */
4530 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 return FALSE;
4532 *p++ = ' ';
4533 *p++ = '(';
4534 if (add_file)
4535 {
4536 STRCPY(p, "file ");
4537 p += 5;
4538 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004539 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4540 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4542 return TRUE;
4543}
4544
4545/*
4546 * If fname is not a full path, make it a full path.
4547 * Returns pointer to allocated memory (NULL for failure).
4548 */
4549 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004550fix_fname(char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551{
4552 /*
4553 * Force expanding the path always for Unix, because symbolic links may
4554 * mess up the full path name, even though it starts with a '/'.
4555 * Also expand when there is ".." in the file name, try to remove it,
4556 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004557 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 * For MS-Windows also expand names like "longna~1" to "longname".
4559 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004560#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 return FullName_save(fname, TRUE);
4562#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004563 if (!vim_isAbsName(fname)
4564 || strstr((char *)fname, "..") != NULL
4565 || strstr((char *)fname, "//") != NULL
4566# ifdef BACKSLASH_IN_FILENAME
4567 || strstr((char *)fname, "\\\\") != NULL
4568# endif
Bram Moolenaar48e330a2016-02-23 14:53:34 +01004569# if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004571# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 )
4573 return FullName_save(fname, FALSE);
4574
4575 fname = vim_strsave(fname);
4576
Bram Moolenaar9b942202007-10-03 12:31:33 +00004577# ifdef USE_FNAME_CASE
4578# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004580# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
4582 if (fname != NULL)
4583 fname_case(fname, 0); /* set correct case for file name */
4584 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004585# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586
4587 return fname;
4588#endif
4589}
4590
4591/*
4592 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4593 * "ffname" becomes a pointer to allocated memory (or NULL).
4594 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004596fname_expand(
4597 buf_T *buf UNUSED,
4598 char_u **ffname,
4599 char_u **sfname)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600{
4601 if (*ffname == NULL) /* if no file name given, nothing to do */
4602 return;
4603 if (*sfname == NULL) /* if no short file name given, use ffname */
4604 *sfname = *ffname;
4605 *ffname = fix_fname(*ffname); /* expand to full path */
4606
4607#ifdef FEAT_SHORTCUT
4608 if (!buf->b_p_bin)
4609 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004610 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611
4612 /* If the file name is a shortcut file, use the file it links to. */
4613 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004614 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 {
4616 vim_free(*ffname);
4617 *ffname = rfname;
4618 *sfname = rfname;
4619 }
4620 }
4621#endif
4622}
4623
4624/*
4625 * Get the file name for an argument list entry.
4626 */
4627 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004628alist_name(aentry_T *aep)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629{
4630 buf_T *bp;
4631
4632 /* Use the name from the associated buffer if it exists. */
4633 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004634 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 return aep->ae_fname;
4636 return bp->b_fname;
4637}
4638
4639#if defined(FEAT_WINDOWS) || defined(PROTO)
4640/*
4641 * do_arg_all(): Open up to 'count' windows, one for each argument.
4642 */
4643 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004644do_arg_all(
4645 int count,
4646 int forceit, /* hide buffers in current windows */
4647 int keep_tabs) /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
4649 int i;
4650 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004651 char_u *opened; /* Array of weight for which args are open:
4652 * 0: not opened
4653 * 1: opened in other tab
4654 * 2: opened in curtab
4655 * 3: opened in curtab and curwin
4656 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004657 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 int use_firstwin = FALSE; /* use first window for arglist */
4659 int split_ret = OK;
4660 int p_ea_save;
4661 alist_T *alist; /* argument list to be used */
4662 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004663 tabpage_T *tpnext;
4664 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004665 win_T *old_curwin, *last_curwin;
4666 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004667 win_T *new_curwin = NULL;
4668 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669
4670 if (ARGCOUNT <= 0)
4671 {
4672 /* Don't give an error message. We don't want it when the ":all"
4673 * command is in the .vimrc. */
4674 return;
4675 }
4676 setpcmark();
4677
4678 opened_len = ARGCOUNT;
4679 opened = alloc_clear((unsigned)opened_len);
4680 if (opened == NULL)
4681 return;
4682
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004683 /* Autocommands may do anything to the argument list. Make sure it's not
4684 * freed while we are working here by "locking" it. We still have to
4685 * watch out for its size to be changed. */
4686 alist = curwin->w_alist;
4687 ++alist->al_refcount;
4688
4689 old_curwin = curwin;
4690 old_curtab = curtab;
4691
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004692# ifdef FEAT_GUI
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 need_mouse_correct = TRUE;
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004694# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
4696 /*
4697 * Try closing all windows that are not in the argument list.
4698 * Also close windows that are not full width;
4699 * When 'hidden' or "forceit" set the buffer becomes hidden.
4700 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004701 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004703 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004704 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004705 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004707 tpnext = curtab->tp_next;
4708 for (wp = firstwin; wp != NULL; wp = wpnext)
4709 {
4710 wpnext = wp->w_next;
4711 buf = wp->w_buffer;
4712 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004713 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004714 || wp->w_width != Columns)
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004715 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004716 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004718 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004719 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004721 if (i < alist->al_ga.ga_len
4722 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4723 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4724 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004726 int weight = 1;
4727
4728 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004729 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004730 ++weight;
4731 if (old_curwin == wp)
4732 ++weight;
4733 }
4734
4735 if (weight > (int)opened[i])
4736 {
4737 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004738 if (i == 0)
4739 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004740 if (new_curwin != NULL)
4741 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004742 new_curwin = wp;
4743 new_curtab = curtab;
4744 }
4745 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004746 else if (keep_tabs)
4747 i = opened_len;
4748
4749 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004750 {
4751 /* Use the current argument list for all windows
4752 * containing a file from it. */
4753 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004754 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004755 ++wp->w_alist->al_refcount;
4756 }
4757 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 }
4760 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004761 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004763 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004765 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004766 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004768 /* If the buffer was changed, and we would like to hide it,
4769 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004770 if (!P_HID(buf) && buf->b_nwindows <= 1
4771 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004773#ifdef FEAT_AUTOCMD
4774 bufref_T bufref;
4775
4776 set_bufref(&bufref, buf);
4777#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004778 (void)autowrite(buf, FALSE);
4779#ifdef FEAT_AUTOCMD
4780 /* check if autocommands removed the window */
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02004781 if (!win_valid(wp) || !bufref_valid(&bufref))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004782 {
4783 wpnext = firstwin; /* start all over... */
4784 continue;
4785 }
4786#endif
4787 }
4788#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004789 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004790 if (firstwin == lastwin
4791 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004792#endif
4793 use_firstwin = TRUE;
4794#ifdef FEAT_WINDOWS
4795 else
4796 {
4797 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4798# ifdef FEAT_AUTOCMD
4799 /* check if autocommands removed the next window */
4800 if (!win_valid(wpnext))
4801 wpnext = firstwin; /* start all over... */
4802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
4804#endif
4805 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
4807 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004808
4809 /* Without the ":tab" modifier only do the current tab page. */
4810 if (had_tab == 0 || tpnext == NULL)
4811 break;
4812
4813# ifdef FEAT_AUTOCMD
4814 /* check if autocommands removed the next tab page */
4815 if (!valid_tabpage(tpnext))
4816 tpnext = first_tabpage; /* start all over...*/
4817# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004818 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820
4821 /*
4822 * Open a window for files in the argument list that don't have one.
4823 * ARGCOUNT may change while doing this, because of autocommands.
4824 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004825 if (count > opened_len || count <= 0)
4826 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827
4828#ifdef FEAT_AUTOCMD
4829 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4830 ++autocmd_no_enter;
4831 ++autocmd_no_leave;
4832#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004833 last_curwin = curwin;
4834 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004836#ifdef FEAT_WINDOWS
4837 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4838 * leaving an empty tab page when executed locally. */
4839 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4840 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4841 use_firstwin = TRUE;
4842#endif
4843
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004844 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 {
4846 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4847 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004848 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
4850 /* Move the already present window to below the current window */
4851 if (curwin->w_arg_idx != i)
4852 {
4853 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4854 {
4855 if (wpnext->w_arg_idx == i)
4856 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004857 if (keep_tabs)
4858 {
4859 new_curwin = wpnext;
4860 new_curtab = curtab;
4861 }
4862 else
4863 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 break;
4865 }
4866 }
4867 }
4868 }
4869 else if (split_ret == OK)
4870 {
4871 if (!use_firstwin) /* split current window */
4872 {
4873 p_ea_save = p_ea;
4874 p_ea = TRUE; /* use space from all windows */
4875 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4876 p_ea = p_ea_save;
4877 if (split_ret == FAIL)
4878 continue;
4879 }
4880#ifdef FEAT_AUTOCMD
4881 else /* first window: do autocmd for leaving this buffer */
4882 --autocmd_no_leave;
4883#endif
4884
4885 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004886 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 */
4888 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004889 if (i == 0)
4890 {
4891 new_curwin = curwin;
4892 new_curtab = curtab;
4893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4895 ECMD_ONE,
4896 ((P_HID(curwin->w_buffer)
4897 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004898 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#ifdef FEAT_AUTOCMD
4900 if (use_firstwin)
4901 ++autocmd_no_leave;
4902#endif
4903 use_firstwin = FALSE;
4904 }
4905 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004906
4907 /* When ":tab" was used open a new tab for a new window repeatedly. */
4908 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4909 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911
4912 /* Remove the "lock" on the argument list. */
4913 alist_unlink(alist);
4914
4915#ifdef FEAT_AUTOCMD
4916 --autocmd_no_enter;
4917#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004918 /* restore last referenced tabpage's curwin */
4919 if (last_curtab != new_curtab)
4920 {
4921 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004922 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004923 if (win_valid(last_curwin))
4924 win_enter(last_curwin, FALSE);
4925 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004926 /* to window with first arg */
4927 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004928 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004929 if (win_valid(new_curwin))
4930 win_enter(new_curwin, FALSE);
4931
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932#ifdef FEAT_AUTOCMD
4933 --autocmd_no_leave;
4934#endif
4935 vim_free(opened);
4936}
4937
4938# if defined(FEAT_LISTCMDS) || defined(PROTO)
4939/*
4940 * Open a window for a number of buffers.
4941 */
4942 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004943ex_buffer_all(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944{
4945 buf_T *buf;
4946 win_T *wp, *wpnext;
4947 int split_ret = OK;
4948 int p_ea_save;
4949 int open_wins = 0;
4950 int r;
4951 int count; /* Maximum number of windows to open. */
4952 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004953#ifdef FEAT_WINDOWS
4954 int had_tab = cmdmod.tab;
4955 tabpage_T *tpnext;
4956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957
4958 if (eap->addr_count == 0) /* make as many windows as possible */
4959 count = 9999;
4960 else
4961 count = eap->line2; /* make as many windows as specified */
4962 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4963 all = FALSE;
4964 else
4965 all = TRUE;
4966
4967 setpcmark();
4968
4969#ifdef FEAT_GUI
4970 need_mouse_correct = TRUE;
4971#endif
4972
4973 /*
4974 * Close superfluous windows (two windows for the same buffer).
4975 * Also close windows that are not full-width.
4976 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004977#ifdef FEAT_WINDOWS
4978 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004979 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004980 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004983 tpnext = curtab->tp_next;
4984 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004986 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004987 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaar44a2f922016-03-19 22:11:51 +01004988#ifdef FEAT_WINDOWS
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004989 || ((cmdmod.split & WSP_VERT)
4990 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004991 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004992 : wp->w_width != Columns)
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004993 || (had_tab > 0 && wp != firstwin)
4994#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004995 ) && firstwin != lastwin
4996#ifdef FEAT_AUTOCMD
4997 && !(wp->w_closing || wp->w_buffer->b_closing)
4998#endif
4999 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005000 {
5001 win_close(wp, FALSE);
5002#ifdef FEAT_AUTOCMD
5003 wpnext = firstwin; /* just in case an autocommand does
5004 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005005 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005006 open_wins = 0;
5007#endif
5008 }
5009 else
5010 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005012
5013#ifdef FEAT_WINDOWS
5014 /* Without the ":tab" modifier only do the current tab page. */
5015 if (had_tab == 0 || tpnext == NULL)
5016 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02005017 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020
5021 /*
5022 * Go through the buffer list. When a buffer doesn't have a window yet,
5023 * open one. Otherwise move the window to the right position.
5024 * Watch out for autocommands that delete buffers or windows!
5025 */
5026#ifdef FEAT_AUTOCMD
5027 /* Don't execute Win/Buf Enter/Leave autocommands here. */
5028 ++autocmd_no_enter;
5029#endif
5030 win_enter(lastwin, FALSE);
5031#ifdef FEAT_AUTOCMD
5032 ++autocmd_no_leave;
5033#endif
5034 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
5035 {
5036 /* Check if this buffer needs a window */
5037 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
5038 continue;
5039
Bram Moolenaarb475fb92006-03-02 22:40:52 +00005040#ifdef FEAT_WINDOWS
5041 if (had_tab != 0)
5042 {
5043 /* With the ":tab" modifier don't move the window. */
5044 if (buf->b_nwindows > 0)
5045 wp = lastwin; /* buffer has a window, skip it */
5046 else
5047 wp = NULL;
5048 }
5049 else
5050#endif
5051 {
5052 /* Check if this buffer already has a window */
5053 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5054 if (wp->w_buffer == buf)
5055 break;
5056 /* If the buffer already has a window, move it */
5057 if (wp != NULL)
5058 win_move_after(wp, curwin);
5059 }
5060
5061 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005063#ifdef FEAT_AUTOCMD
5064 bufref_T bufref;
5065
5066 set_bufref(&bufref, buf);
5067#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 /* Split the window and put the buffer in it */
5069 p_ea_save = p_ea;
5070 p_ea = TRUE; /* use space from all windows */
5071 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5072 ++open_wins;
5073 p_ea = p_ea_save;
5074 if (split_ret == FAIL)
5075 continue;
5076
5077 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005078#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 swap_exists_action = SEA_DIALOG;
5080#endif
5081 set_curbuf(buf, DOBUF_GOTO);
5082#ifdef FEAT_AUTOCMD
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005083 if (!bufref_valid(&bufref))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
Bram Moolenaarb25f9a92016-07-10 18:21:50 +02005085 /* autocommands deleted the buffer!!! */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005086#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 break;
5090 }
5091#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005092#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005093 if (swap_exists_action == SEA_QUIT)
5094 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005095# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5096 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005097
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005098 /* Reset the error/interrupt/exception state here so that
5099 * aborting() returns FALSE when closing a window. */
5100 enter_cleanup(&cs);
5101# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005102
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005103 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 win_close(curwin, TRUE);
5105 --open_wins;
5106 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005107 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005108
5109# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5110 /* Restore the error/interrupt/exception state if not
5111 * discarded by a new aborting error, interrupt, or uncaught
5112 * exception. */
5113 leave_cleanup(&cs);
5114# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 else
5117 handle_swap_exists(NULL);
5118#endif
5119 }
5120
5121 ui_breakcheck();
5122 if (got_int)
5123 {
5124 (void)vgetc(); /* only break the file loading, not the rest */
5125 break;
5126 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005127#ifdef FEAT_EVAL
5128 /* Autocommands deleted the buffer or aborted script processing!!! */
5129 if (aborting())
5130 break;
5131#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005132#ifdef FEAT_WINDOWS
5133 /* When ":tab" was used open a new tab for a new window repeatedly. */
5134 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5135 cmdmod.tab = 9999;
5136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 }
5138#ifdef FEAT_AUTOCMD
5139 --autocmd_no_enter;
5140#endif
5141 win_enter(firstwin, FALSE); /* back to first window */
5142#ifdef FEAT_AUTOCMD
5143 --autocmd_no_leave;
5144#endif
5145
5146 /*
5147 * Close superfluous windows.
5148 */
5149 for (wp = lastwin; open_wins > count; )
5150 {
5151 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5152 || autowrite(wp->w_buffer, FALSE) == OK);
5153#ifdef FEAT_AUTOCMD
5154 if (!win_valid(wp))
5155 {
5156 /* BufWrite Autocommands made the window invalid, start over */
5157 wp = lastwin;
5158 }
5159 else
5160#endif
5161 if (r)
5162 {
5163 win_close(wp, !P_HID(wp->w_buffer));
5164 --open_wins;
5165 wp = lastwin;
5166 }
5167 else
5168 {
5169 wp = wp->w_prev;
5170 if (wp == NULL)
5171 break;
5172 }
5173 }
5174}
5175# endif /* FEAT_LISTCMDS */
5176
5177#endif /* FEAT_WINDOWS */
5178
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01005179static int chk_modeline(linenr_T, int);
Bram Moolenaara3227e22006-03-08 21:32:40 +00005180
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181/*
5182 * do_modelines() - process mode lines for the current file
5183 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005184 * "flags" can be:
5185 * OPT_WINONLY only set options local to window
5186 * OPT_NOWIN don't set options local to window
5187 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 * Returns immediately if the "ml" option isn't set.
5189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005191do_modelines(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005193 linenr_T lnum;
5194 int nmlines;
5195 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
5197 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5198 return;
5199
5200 /* Disallow recursive entry here. Can happen when executing a modeline
5201 * triggers an autocommand, which reloads modelines with a ":do". */
5202 if (entered)
5203 return;
5204
5205 ++entered;
5206 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5207 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005208 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 nmlines = 0;
5210
5211 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5212 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005213 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 nmlines = 0;
5215 --entered;
5216}
5217
5218#include "version.h" /* for version number */
5219
5220/*
5221 * chk_modeline() - check a single line for a mode string
5222 * Return FAIL if an error encountered.
5223 */
5224 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005225chk_modeline(
5226 linenr_T lnum,
5227 int flags) /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228{
5229 char_u *s;
5230 char_u *e;
5231 char_u *linecopy; /* local copy of any modeline found */
5232 int prev;
5233 int vers;
5234 int end;
5235 int retval = OK;
5236 char_u *save_sourcing_name;
5237 linenr_T save_sourcing_lnum;
5238#ifdef FEAT_EVAL
5239 scid_T save_SID;
5240#endif
5241
5242 prev = -1;
5243 for (s = ml_get(lnum); *s != NUL; ++s)
5244 {
5245 if (prev == -1 || vim_isspace(prev))
5246 {
5247 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5248 || STRNCMP(s, "vi:", (size_t)3) == 0)
5249 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005250 /* Accept both "vim" and "Vim". */
5251 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 {
5253 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5254 e = s + 4;
5255 else
5256 e = s + 3;
5257 vers = getdigits(&e);
5258 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005259 && (s[0] != 'V'
5260 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 && (s[3] == ':'
5262 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5263 || (VIM_VERSION_100 < vers && s[3] == '<')
5264 || (VIM_VERSION_100 > vers && s[3] == '>')
5265 || (VIM_VERSION_100 == vers && s[3] == '=')))
5266 break;
5267 }
5268 }
5269 prev = *s;
5270 }
5271
5272 if (*s)
5273 {
5274 do /* skip over "ex:", "vi:" or "vim:" */
5275 ++s;
5276 while (s[-1] != ':');
5277
5278 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5279 if (linecopy == NULL)
5280 return FAIL;
5281
5282 save_sourcing_lnum = sourcing_lnum;
5283 save_sourcing_name = sourcing_name;
5284 sourcing_lnum = lnum; /* prepare for emsg() */
5285 sourcing_name = (char_u *)"modelines";
5286
5287 end = FALSE;
5288 while (end == FALSE)
5289 {
5290 s = skipwhite(s);
5291 if (*s == NUL)
5292 break;
5293
5294 /*
5295 * Find end of set command: ':' or end of line.
5296 * Skip over "\:", replacing it with ":".
5297 */
5298 for (e = s; *e != ':' && *e != NUL; ++e)
5299 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005300 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 if (*e == NUL)
5302 end = TRUE;
5303
5304 /*
5305 * If there is a "set" command, require a terminating ':' and
5306 * ignore the stuff after the ':'.
5307 * "vi:set opt opt opt: foo" -- foo not interpreted
5308 * "vi:opt opt opt: foo" -- foo interpreted
5309 * Accept "se" for compatibility with Elvis.
5310 */
5311 if (STRNCMP(s, "set ", (size_t)4) == 0
5312 || STRNCMP(s, "se ", (size_t)3) == 0)
5313 {
5314 if (*e != ':') /* no terminating ':'? */
5315 break;
5316 end = TRUE;
5317 s = vim_strchr(s, ' ') + 1;
5318 }
5319 *e = NUL; /* truncate the set command */
5320
5321 if (*s != NUL) /* skip over an empty "::" */
5322 {
5323#ifdef FEAT_EVAL
5324 save_SID = current_SID;
5325 current_SID = SID_MODELINE;
5326#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005327 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328#ifdef FEAT_EVAL
5329 current_SID = save_SID;
5330#endif
5331 if (retval == FAIL) /* stop if error found */
5332 break;
5333 }
5334 s = e + 1; /* advance to next part */
5335 }
5336
5337 sourcing_lnum = save_sourcing_lnum;
5338 sourcing_name = save_sourcing_name;
5339
5340 vim_free(linecopy);
5341 }
5342 return retval;
5343}
5344
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005345#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005347read_viminfo_bufferlist(
5348 vir_T *virp,
5349 int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350{
5351 char_u *tab;
5352 linenr_T lnum;
5353 colnr_T col;
5354 buf_T *buf;
5355 char_u *sfname;
5356 char_u *xline;
5357
5358 /* Handle long line and escaped characters. */
5359 xline = viminfo_readstring(virp, 1, FALSE);
5360
5361 /* don't read in if there are files on the command-line or if writing: */
5362 if (xline != NULL && !writing && ARGCOUNT == 0
5363 && find_viminfo_parameter('%') != NULL)
5364 {
5365 /* Format is: <fname> Tab <lnum> Tab <col>.
5366 * Watch out for a Tab in the file name, work from the end. */
5367 lnum = 0;
5368 col = 0;
5369 tab = vim_strrchr(xline, '\t');
5370 if (tab != NULL)
5371 {
5372 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005373 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 tab = vim_strrchr(xline, '\t');
5375 if (tab != NULL)
5376 {
5377 *tab++ = '\0';
5378 lnum = atol((char *)tab);
5379 }
5380 }
5381
5382 /* Expand "~/" in the file name at "line + 1" to a full path.
5383 * Then try shortening it by comparing with the current directory */
5384 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005385 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386
5387 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5388 if (buf != NULL) /* just in case... */
5389 {
5390 buf->b_last_cursor.lnum = lnum;
5391 buf->b_last_cursor.col = col;
5392 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5393 }
5394 }
5395 vim_free(xline);
5396
5397 return viminfo_readline(virp);
5398}
5399
5400 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005401write_viminfo_bufferlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402{
5403 buf_T *buf;
5404#ifdef FEAT_WINDOWS
5405 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005406 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407#endif
5408 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005409 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 if (find_viminfo_parameter('%') == NULL)
5412 return;
5413
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005414 /* Without a number -1 is returned: do all buffers. */
5415 max_buffers = get_viminfo_parameter('%');
5416
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005418#define LINE_BUF_LEN (MAXPATHL + 40)
5419 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 if (line == NULL)
5421 return;
5422
5423#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005424 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 set_last_cursor(win);
5426#else
5427 set_last_cursor(curwin);
5428#endif
5429
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005430 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5432 {
5433 if (buf->b_fname == NULL
5434 || !buf->b_p_bl
5435#ifdef FEAT_QUICKFIX
5436 || bt_quickfix(buf)
5437#endif
5438 || removable(buf->b_ffname))
5439 continue;
5440
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005441 if (max_buffers-- == 0)
5442 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 putc('%', fp);
5444 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005445 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 (long)buf->b_last_cursor.lnum,
5447 buf->b_last_cursor.col);
5448 viminfo_writestring(fp, line);
5449 }
5450 vim_free(line);
5451}
5452#endif
5453
5454
5455/*
5456 * Return special buffer name.
5457 * Returns NULL when the buffer has a normal file name.
5458 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005460buf_spname(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461{
5462#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5463 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005464 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005465 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005466 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005467
5468 /*
5469 * For location list window, w_llist_ref points to the location list.
5470 * For quickfix window, w_llist_ref is NULL.
5471 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005472 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005473 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005474 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005475 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477#endif
5478#ifdef FEAT_QUICKFIX
5479 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5480 * contains the name as specified by the user */
5481 if (bt_nofile(buf))
5482 {
5483 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005484 return buf->b_sfname;
5485 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 }
5487#endif
5488 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005489 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 return NULL;
5491}
5492
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005493#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5494 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5495 || defined(PROTO)
5496/*
5497 * Find a window for buffer "buf".
5498 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5499 * If not found FAIL is returned.
5500 */
5501 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005502find_win_for_buf(
5503 buf_T *buf,
5504 win_T **wp,
5505 tabpage_T **tp)
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005506{
5507 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5508 if ((*wp)->w_buffer == buf)
5509 goto win_found;
5510 return FAIL;
5511win_found:
5512 return OK;
5513}
5514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515
5516#if defined(FEAT_SIGNS) || defined(PROTO)
5517/*
5518 * Insert the sign into the signlist.
5519 */
5520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005521insert_sign(
5522 buf_T *buf, /* buffer to store sign in */
5523 signlist_T *prev, /* previous sign entry */
5524 signlist_T *next, /* next sign entry */
5525 int id, /* sign ID */
5526 linenr_T lnum, /* line number which gets the mark */
5527 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528{
5529 signlist_T *newsign;
5530
5531 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5532 if (newsign != NULL)
5533 {
5534 newsign->id = id;
5535 newsign->lnum = lnum;
5536 newsign->typenr = typenr;
5537 newsign->next = next;
5538#ifdef FEAT_NETBEANS_INTG
5539 newsign->prev = prev;
5540 if (next != NULL)
5541 next->prev = newsign;
5542#endif
5543
5544 if (prev == NULL)
5545 {
5546 /* When adding first sign need to redraw the windows to create the
5547 * column for signs. */
5548 if (buf->b_signlist == NULL)
5549 {
5550 redraw_buf_later(buf, NOT_VALID);
5551 changed_cline_bef_curs();
5552 }
5553
5554 /* first sign in signlist */
5555 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005556#ifdef FEAT_NETBEANS_INTG
5557 if (netbeans_active())
5558 buf->b_has_sign_column = TRUE;
5559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561 else
5562 prev->next = newsign;
5563 }
5564}
5565
5566/*
5567 * Add the sign into the signlist. Find the right spot to do it though.
5568 */
5569 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005570buf_addsign(
5571 buf_T *buf, /* buffer to store sign in */
5572 int id, /* sign ID */
5573 linenr_T lnum, /* line number which gets the mark */
5574 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575{
5576 signlist_T *sign; /* a sign in the signlist */
5577 signlist_T *prev; /* the previous sign */
5578
5579 prev = NULL;
5580 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5581 {
5582 if (lnum == sign->lnum && id == sign->id)
5583 {
5584 sign->typenr = typenr;
5585 return;
5586 }
5587 else if (
5588#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5589 id < 0 &&
5590#endif
5591 lnum < sign->lnum)
5592 {
5593#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5594 /* XXX - GRP: Is this because of sign slide problem? Or is it
5595 * really needed? Or is it because we allow multiple signs per
5596 * line? If so, should I add that feature to FEAT_SIGNS?
5597 */
5598 while (prev != NULL && prev->lnum == lnum)
5599 prev = prev->prev;
5600 if (prev == NULL)
5601 sign = buf->b_signlist;
5602 else
5603 sign = prev->next;
5604#endif
5605 insert_sign(buf, prev, sign, id, lnum, typenr);
5606 return;
5607 }
5608 prev = sign;
5609 }
5610#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5611 /* XXX - GRP: See previous comment */
5612 while (prev != NULL && prev->lnum == lnum)
5613 prev = prev->prev;
5614 if (prev == NULL)
5615 sign = buf->b_signlist;
5616 else
5617 sign = prev->next;
5618#endif
5619 insert_sign(buf, prev, sign, id, lnum, typenr);
5620
5621 return;
5622}
5623
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005624/*
5625 * For an existing, placed sign "markId" change the type to "typenr".
5626 * Returns the line number of the sign, or zero if the sign is not found.
5627 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005628 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005629buf_change_sign_type(
5630 buf_T *buf, /* buffer to store sign in */
5631 int markId, /* sign ID */
5632 int typenr) /* typenr of sign we are adding */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633{
5634 signlist_T *sign; /* a sign in the signlist */
5635
5636 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5637 {
5638 if (sign->id == markId)
5639 {
5640 sign->typenr = typenr;
5641 return sign->lnum;
5642 }
5643 }
5644
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005645 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646}
5647
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005648 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005649buf_getsigntype(
5650 buf_T *buf,
5651 linenr_T lnum,
5652 int type) /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653{
5654 signlist_T *sign; /* a sign in a b_signlist */
5655
5656 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5657 if (sign->lnum == lnum
5658 && (type == SIGN_ANY
5659# ifdef FEAT_SIGN_ICONS
5660 || (type == SIGN_ICON
5661 && sign_get_image(sign->typenr) != NULL)
5662# endif
5663 || (type == SIGN_TEXT
5664 && sign_get_text(sign->typenr) != NULL)
5665 || (type == SIGN_LINEHL
5666 && sign_get_attr(sign->typenr, TRUE) != 0)))
5667 return sign->typenr;
5668 return 0;
5669}
5670
5671
5672 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01005673buf_delsign(
5674 buf_T *buf, /* buffer sign is stored in */
5675 int id) /* sign id */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676{
5677 signlist_T **lastp; /* pointer to pointer to current sign */
5678 signlist_T *sign; /* a sign in a b_signlist */
5679 signlist_T *next; /* the next sign in a b_signlist */
5680 linenr_T lnum; /* line number whose sign was deleted */
5681
5682 lastp = &buf->b_signlist;
5683 lnum = 0;
5684 for (sign = buf->b_signlist; sign != NULL; sign = next)
5685 {
5686 next = sign->next;
5687 if (sign->id == id)
5688 {
5689 *lastp = next;
5690#ifdef FEAT_NETBEANS_INTG
5691 if (next != NULL)
5692 next->prev = sign->prev;
5693#endif
5694 lnum = sign->lnum;
5695 vim_free(sign);
5696 break;
5697 }
5698 else
5699 lastp = &sign->next;
5700 }
5701
5702 /* When deleted the last sign need to redraw the windows to remove the
5703 * sign column. */
5704 if (buf->b_signlist == NULL)
5705 {
5706 redraw_buf_later(buf, NOT_VALID);
5707 changed_cline_bef_curs();
5708 }
5709
5710 return lnum;
5711}
5712
5713
5714/*
5715 * Find the line number of the sign with the requested id. If the sign does
5716 * not exist, return 0 as the line number. This will still let the correct file
5717 * get loaded.
5718 */
5719 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005720buf_findsign(
5721 buf_T *buf, /* buffer to store sign in */
5722 int id) /* sign ID */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723{
5724 signlist_T *sign; /* a sign in the signlist */
5725
5726 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5727 if (sign->id == id)
5728 return sign->lnum;
5729
5730 return 0;
5731}
5732
5733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734buf_findsign_id(
5735 buf_T *buf, /* buffer whose sign we are searching for */
5736 linenr_T lnum) /* line number of sign */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737{
5738 signlist_T *sign; /* a sign in the signlist */
5739
5740 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5741 if (sign->lnum == lnum)
5742 return sign->id;
5743
5744 return 0;
5745}
5746
5747
5748# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5749/* see if a given type of sign exists on a specific line */
5750 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005751buf_findsigntype_id(
5752 buf_T *buf, /* buffer whose sign we are searching for */
5753 linenr_T lnum, /* line number of sign */
5754 int typenr) /* sign type number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755{
5756 signlist_T *sign; /* a sign in the signlist */
5757
5758 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5759 if (sign->lnum == lnum && sign->typenr == typenr)
5760 return sign->id;
5761
5762 return 0;
5763}
5764
5765
5766# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5767/* return the number of icons on the given line */
5768 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005769buf_signcount(buf_T *buf, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770{
5771 signlist_T *sign; /* a sign in the signlist */
5772 int count = 0;
5773
5774 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5775 if (sign->lnum == lnum)
5776 if (sign_get_image(sign->typenr) != NULL)
5777 count++;
5778
5779 return count;
5780}
5781# endif /* FEAT_SIGN_ICONS */
5782# endif /* FEAT_NETBEANS_INTG */
5783
5784
5785/*
5786 * Delete signs in buffer "buf".
5787 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005789buf_delete_signs(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790{
5791 signlist_T *next;
5792
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005793 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005794 * sign column. Not when curwin is NULL (this means we're exiting). */
5795 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005796 {
5797 redraw_buf_later(buf, NOT_VALID);
5798 changed_cline_bef_curs();
5799 }
5800
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 while (buf->b_signlist != NULL)
5802 {
5803 next = buf->b_signlist->next;
5804 vim_free(buf->b_signlist);
5805 buf->b_signlist = next;
5806 }
5807}
5808
5809/*
5810 * Delete all signs in all buffers.
5811 */
5812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005813buf_delete_all_signs(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814{
5815 buf_T *buf; /* buffer we are checking for signs */
5816
5817 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5818 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820}
5821
5822/*
5823 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5824 */
5825 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005826sign_list_placed(buf_T *rbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827{
5828 buf_T *buf;
5829 signlist_T *p;
5830 char lbuf[BUFSIZ];
5831
5832 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5833 msg_putchar('\n');
5834 if (rbuf == NULL)
5835 buf = firstbuf;
5836 else
5837 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005838 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 {
5840 if (buf->b_signlist != NULL)
5841 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005842 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5844 msg_putchar('\n');
5845 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005846 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005848 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5850 MSG_PUTS(lbuf);
5851 msg_putchar('\n');
5852 }
5853 if (rbuf != NULL)
5854 break;
5855 buf = buf->b_next;
5856 }
5857}
5858
5859/*
5860 * Adjust a placed sign for inserted/deleted lines.
5861 */
5862 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005863sign_mark_adjust(
5864 linenr_T line1,
5865 linenr_T line2,
5866 long amount,
5867 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 signlist_T *sign; /* a sign in a b_signlist */
5870
5871 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5872 {
5873 if (sign->lnum >= line1 && sign->lnum <= line2)
5874 {
5875 if (amount == MAXLNUM)
5876 sign->lnum = line1;
5877 else
5878 sign->lnum += amount;
5879 }
5880 else if (sign->lnum > line2)
5881 sign->lnum += amount_after;
5882 }
5883}
5884#endif /* FEAT_SIGNS */
5885
5886/*
5887 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5888 */
5889 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005890set_buflisted(int on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891{
5892 if (on != curbuf->b_p_bl)
5893 {
5894 curbuf->b_p_bl = on;
5895#ifdef FEAT_AUTOCMD
5896 if (on)
5897 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5898 else
5899 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5900#endif
5901 }
5902}
5903
5904/*
5905 * Read the file for "buf" again and check if the contents changed.
5906 * Return TRUE if it changed or this could not be checked.
5907 */
5908 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005909buf_contents_changed(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005910{
5911 buf_T *newbuf;
5912 int differ = TRUE;
5913 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 exarg_T ea;
5916
5917 /* Allocate a buffer without putting it in the buffer list. */
5918 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5919 if (newbuf == NULL)
5920 return TRUE;
5921
5922 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5923 if (prep_exarg(&ea, buf) == FAIL)
5924 {
5925 wipe_buffer(newbuf, FALSE);
5926 return TRUE;
5927 }
5928
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 /* set curwin/curbuf to buf and save a few things */
5930 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931
Bram Moolenaar4770d092006-01-12 23:22:24 +00005932 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 && readfile(buf->b_ffname, buf->b_fname,
5934 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5935 &ea, READ_NEW | READ_DUMMY) == OK)
5936 {
5937 /* compare the two files line by line */
5938 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5939 {
5940 differ = FALSE;
5941 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5942 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5943 {
5944 differ = TRUE;
5945 break;
5946 }
5947 }
5948 }
5949 vim_free(ea.cmd);
5950
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 /* restore curwin/curbuf and a few other things */
5952 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
5954 if (curbuf != newbuf) /* safety check */
5955 wipe_buffer(newbuf, FALSE);
5956
5957 return differ;
5958}
5959
5960/*
5961 * Wipe out a buffer and decrement the last buffer number if it was used for
5962 * this buffer. Call this to wipe out a temp buffer that does not contain any
5963 * marks.
5964 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005966wipe_buffer(
5967 buf_T *buf,
5968 int aucmd UNUSED) /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969{
5970 if (buf->b_fnum == top_file_num - 1)
5971 --top_file_num;
5972
5973#ifdef FEAT_AUTOCMD
5974 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005975 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005976#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005977 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005978#ifdef FEAT_AUTOCMD
5979 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005980 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981#endif
5982}