blob: 8986ac4e2223932804a2b9fb3489120d7407efb3 [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 Moolenaardffa5b82014-11-19 16:38:07 +010031static char_u *buflist_match __ARGS((regmatch_T *rmp, buf_T *buf, int ignore_case));
Bram Moolenaar071d4272004-06-13 20:20:40 +000032# define HAVE_BUFLIST_MATCH
Bram Moolenaardffa5b82014-11-19 16:38:07 +010033static char_u *fname_match __ARGS((regmatch_T *rmp, char_u *name, int ignore_case));
Bram Moolenaar071d4272004-06-13 20:20:40 +000034#endif
35static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
Bram Moolenaar701f7af2008-11-15 13:12:07 +000036static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
38static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
39static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
40static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
41#else
42static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
43#endif
44#ifdef FEAT_TITLE
45static int ti_change __ARGS((char_u *str, char_u **last));
46#endif
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000047static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void free_buffer __ARGS((buf_T *));
49static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50static void clear_wininfo __ARGS((buf_T *buf));
51
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
59static void insert_sign __ARGS((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 Moolenaar071d4272004-06-13 20:20:40 +000070/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020071 * Open current buffer, that is: open the memfile and read the file into
72 * memory.
73 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000074 */
75 int
Bram Moolenaar59f931e2010-07-24 20:27:03 +020076open_buffer(read_stdin, eap, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 int read_stdin; /* read file from stdin */
78 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
Bram Moolenaar59f931e2010-07-24 20:27:03 +020079 int flags; /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080{
81 int retval = OK;
82#ifdef FEAT_AUTOCMD
83 buf_T *old_curbuf;
84#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +010085#ifdef FEAT_SYN_HL
86 long old_tw = curbuf->b_p_tw;
87#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000088
89 /*
90 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
91 * When re-entering the same buffer, it should not change, because the
92 * user may have reset the flag by hand.
93 */
94 if (readonlymode && curbuf->b_ffname != NULL
95 && (curbuf->b_flags & BF_NEVERLOADED))
96 curbuf->b_p_ro = TRUE;
97
Bram Moolenaar4770d092006-01-12 23:22:24 +000098 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 {
100 /*
101 * There MUST be a memfile, otherwise we can't do anything
102 * If we can't create one for the current buffer, take another buffer
103 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100104 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
106 if (curbuf->b_ml.ml_mfp != NULL)
107 break;
108 /*
109 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000110 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 */
112 if (curbuf == NULL)
113 {
114 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
115 getout(2);
116 }
117 EMSG(_("E83: Cannot allocate buffer, using other one..."));
118 enter_buffer(curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100119#ifdef FEAT_SYN_HL
120 if (old_tw != curbuf->b_p_tw)
121 check_colorcolumn(curwin);
122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123 return FAIL;
124 }
125
126#ifdef FEAT_AUTOCMD
127 /* The autocommands in readfile() may change the buffer, but only AFTER
128 * reading the file. */
129 old_curbuf = curbuf;
130 modified_was_set = FALSE;
131#endif
132
133 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100134 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135
136 if (curbuf->b_ffname != NULL
137#ifdef FEAT_NETBEANS_INTG
138 && netbeansReadFile
139#endif
140 )
141 {
142#ifdef FEAT_NETBEANS_INTG
143 int oldFire = netbeansFireChanges;
144
145 netbeansFireChanges = 0;
146#endif
147 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200148 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
149 flags | READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150#ifdef FEAT_NETBEANS_INTG
151 netbeansFireChanges = oldFire;
152#endif
153 /* Help buffer is filtered. */
154 if (curbuf->b_help)
155 fix_help_buffer();
156 }
157 else if (read_stdin)
158 {
159 int save_bin = curbuf->b_p_bin;
160 linenr_T line_count;
161
162 /*
163 * First read the text in binary mode into the buffer.
164 * Then read from that same buffer and append at the end. This makes
165 * it possible to retry when 'fileformat' or 'fileencoding' was
166 * guessed wrong.
167 */
168 curbuf->b_p_bin = TRUE;
169 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200170 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
171 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 curbuf->b_p_bin = save_bin;
173 if (retval == OK)
174 {
175 line_count = curbuf->b_ml.ml_line_count;
176 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200177 (linenr_T)0, (linenr_T)MAXLNUM, eap,
178 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000179 if (retval == OK)
180 {
181 /* Delete the binary lines. */
182 while (--line_count >= 0)
183 ml_delete((linenr_T)1, FALSE);
184 }
185 else
186 {
187 /* Delete the converted lines. */
188 while (curbuf->b_ml.ml_line_count > line_count)
189 ml_delete(line_count, FALSE);
190 }
191 /* Put the cursor on the first line. */
192 curwin->w_cursor.lnum = 1;
193 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000194
195 /* Set or reset 'modified' before executing autocommands, so that
196 * it can be changed there. */
197 if (!readonlymode && !bufempty())
198 changed();
199 else if (retval != FAIL)
200 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201#ifdef FEAT_AUTOCMD
202# ifdef FEAT_EVAL
203 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
204 curbuf, &retval);
205# else
206 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
207# endif
208#endif
209 }
210 }
211
212 /* if first time loading this buffer, init b_chartab[] */
213 if (curbuf->b_flags & BF_NEVERLOADED)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 (void)buf_init_chartab(curbuf, FALSE);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100216#ifdef FEAT_CINDENT
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100217 parse_cino(curbuf);
Bram Moolenaardce7c912013-11-05 17:40:52 +0100218#endif
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +0100219 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220
221 /*
222 * Set/reset the Changed flag first, autocmds may change the buffer.
223 * Apply the automatic commands, before processing the modelines.
224 * So the modelines have priority over auto commands.
225 */
226 /* When reading stdin, the buffer contents always needs writing, so set
227 * the changed flag. Unless in readonly mode: "ls | gview -".
228 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000229 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_AUTOCMD
231 || modified_was_set /* ":set modified" used in autocmd */
232# ifdef FEAT_EVAL
233 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
234# endif
235#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000236 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000238 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000239 unchanged(curbuf, FALSE);
240 save_file_ff(curbuf); /* keep this fileformat */
241
242 /* require "!" to overwrite the file, because it wasn't read completely */
243#ifdef FEAT_EVAL
244 if (aborting())
245#else
246 if (got_int)
247#endif
248 curbuf->b_flags |= BF_READERR;
249
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000250#ifdef FEAT_FOLDING
251 /* Need to update automatic folding. Do this before the autocommands,
252 * they may use the fold info. */
253 foldUpdateAll(curwin);
254#endif
255
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256#ifdef FEAT_AUTOCMD
257 /* need to set w_topline, unless some autocommand already did that. */
258 if (!(curwin->w_valid & VALID_TOPLINE))
259 {
260 curwin->w_topline = 1;
261# ifdef FEAT_DIFF
262 curwin->w_topfill = 0;
263# endif
264 }
265# ifdef FEAT_EVAL
266 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
267# else
268 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
269# endif
270#endif
271
272 if (retval != FAIL)
273 {
274#ifdef FEAT_AUTOCMD
275 /*
276 * The autocommands may have changed the current buffer. Apply the
277 * modelines to the correct buffer, if it still exists and is loaded.
278 */
279 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
280 {
281 aco_save_T aco;
282
283 /* Go to the buffer that was opened. */
284 aucmd_prepbuf(&aco, old_curbuf);
285#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000286 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
288
289#ifdef FEAT_AUTOCMD
290# ifdef FEAT_EVAL
291 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
292 &retval);
293# else
294 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
295# endif
296
297 /* restore curwin/curbuf and a few other things */
298 aucmd_restbuf(&aco);
299 }
300#endif
301 }
302
Bram Moolenaar071d4272004-06-13 20:20:40 +0000303 return retval;
304}
305
306/*
307 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
308 */
309 int
310buf_valid(buf)
311 buf_T *buf;
312{
313 buf_T *bp;
314
315 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
316 if (bp == buf)
317 return TRUE;
318 return FALSE;
319}
320
321/*
322 * Close the link to a buffer.
323 * "action" is used when there is no longer a window for the buffer.
324 * It can be:
325 * 0 buffer becomes hidden
326 * DOBUF_UNLOAD buffer is unloaded
327 * DOBUF_DELETE buffer is unloaded and removed from buffer list
328 * DOBUF_WIPE buffer is unloaded and really deleted
329 * When doing all but the first one on the current buffer, the caller should
330 * get a new buffer very soon!
331 *
332 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100333 *
334 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
335 * cause there to be only one window with this buffer. e.g. when ":quit" is
336 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337 */
338 void
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100339close_buffer(win, buf, action, abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 win_T *win; /* if not NULL, set b_last_cursor */
341 buf_T *buf;
342 int action;
Bram Moolenaar5fbe6992012-03-07 22:52:36 +0100343 int abort_if_last UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000344{
345#ifdef FEAT_AUTOCMD
346 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100347 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348#endif
349 int unload_buf = (action != 0);
350 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
351 int wipe_buf = (action == DOBUF_WIPE);
352
353#ifdef FEAT_QUICKFIX
354 /*
355 * Force unloading or deleting when 'bufhidden' says so.
356 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
357 * "hide" (otherwise we could never free or delete a buffer).
358 */
359 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
360 {
361 del_buf = TRUE;
362 unload_buf = TRUE;
363 }
364 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
365 {
366 del_buf = TRUE;
367 unload_buf = TRUE;
368 wipe_buf = TRUE;
369 }
370 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
371 unload_buf = TRUE;
372#endif
373
Bram Moolenaar3be85852014-06-12 14:01:31 +0200374 if (win != NULL
375#ifdef FEAT_WINDOWS
376 && win_valid(win) /* in case autocommands closed the window */
377#endif
378 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000379 {
380 /* Set b_last_cursor when closing the last window for the buffer.
381 * Remember the last cursor position and window options of the buffer.
382 * This used to be only for the current window, but then options like
383 * 'foldmethod' may be lost with a ":only" command. */
384 if (buf->b_nwindows == 1)
385 set_last_cursor(win);
386 buflist_setfpos(buf, win,
387 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
388 win->w_cursor.col, TRUE);
389 }
390
391#ifdef FEAT_AUTOCMD
392 /* When the buffer is no longer in a window, trigger BufWinLeave */
393 if (buf->b_nwindows == 1)
394 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200395 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
397 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200398 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100399 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200400 /* Autocommands deleted the buffer. */
401aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100402 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000403 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100404 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200405 buf->b_closing = FALSE;
406 if (abort_if_last && one_window())
407 /* Autocommands made this the only window. */
408 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409
410 /* When the buffer becomes hidden, but is not unloaded, trigger
411 * BufHidden */
412 if (!unload_buf)
413 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200414 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000415 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
416 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200417 if (!buf_valid(buf))
418 /* Autocommands deleted the buffer. */
419 goto aucmd_abort;
420 buf->b_closing = FALSE;
421 if (abort_if_last && one_window())
422 /* Autocommands made this the only window. */
423 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 }
425# ifdef FEAT_EVAL
426 if (aborting()) /* autocmds may abort script processing */
427 return;
428# endif
429 }
430 nwindows = buf->b_nwindows;
431#endif
432
433 /* decrease the link count from windows (unless not in any window) */
434 if (buf->b_nwindows > 0)
435 --buf->b_nwindows;
436
437 /* Return when a window is displaying the buffer or when it's not
438 * unloaded. */
439 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441
442 /* Always remove the buffer when there is no file name. */
443 if (buf->b_ffname == NULL)
444 del_buf = TRUE;
445
446 /*
447 * Free all things allocated for this buffer.
448 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
449 */
450#ifdef FEAT_AUTOCMD
451 /* Remember if we are closing the current buffer. Restore the number of
452 * windows, so that autocommands in buf_freeall() don't get confused. */
453 is_curbuf = (buf == curbuf);
454 buf->b_nwindows = nwindows;
455#endif
456
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200457 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200458 if (
459#ifdef FEAT_WINDOWS
460 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100461#else
462 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200463#endif
464 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200465 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466
467#ifdef FEAT_AUTOCMD
468 /* Autocommands may have deleted the buffer. */
469 if (!buf_valid(buf))
470 return;
471# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000472 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473 return;
474# endif
475
476 /* Autocommands may have opened or closed windows for this buffer.
477 * Decrement the count for the close we do here. */
478 if (buf->b_nwindows > 0)
479 --buf->b_nwindows;
480
481 /*
482 * It's possible that autocommands change curbuf to the one being deleted.
483 * This might cause the previous curbuf to be deleted unexpectedly. But
484 * in some cases it's OK to delete the curbuf, because a new one is
485 * obtained anyway. Therefore only return if curbuf changed to the
486 * deleted buffer.
487 */
488 if (buf == curbuf && !is_curbuf)
489 return;
490#endif
491
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000492 /* Change directories when the 'acd' option is set. */
493 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494
495 /*
496 * Remove the buffer from the list.
497 */
498 if (wipe_buf)
499 {
500#ifdef FEAT_SUN_WORKSHOP
501 if (usingSunWorkShop)
502 workshop_file_closed_lineno((char *)buf->b_ffname,
503 (int)buf->b_last_cursor.lnum);
504#endif
505 vim_free(buf->b_ffname);
506 vim_free(buf->b_sfname);
507 if (buf->b_prev == NULL)
508 firstbuf = buf->b_next;
509 else
510 buf->b_prev->b_next = buf->b_next;
511 if (buf->b_next == NULL)
512 lastbuf = buf->b_prev;
513 else
514 buf->b_next->b_prev = buf->b_prev;
515 free_buffer(buf);
516 }
517 else
518 {
519 if (del_buf)
520 {
521 /* Free all internal variables and reset option values, to make
522 * ":bdel" compatible with Vim 5.7. */
523 free_buffer_stuff(buf, TRUE);
524
525 /* Make it look like a new buffer. */
526 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
527
528 /* Init the options when loaded again. */
529 buf->b_p_initialized = FALSE;
530 }
531 buf_clear_file(buf);
532 if (del_buf)
533 buf->b_p_bl = FALSE;
534 }
535}
536
537/*
538 * Make buffer not contain a file.
539 */
540 void
541buf_clear_file(buf)
542 buf_T *buf;
543{
544 buf->b_ml.ml_line_count = 1;
545 unchanged(buf, TRUE);
546#ifndef SHORT_FNAME
547 buf->b_shortname = FALSE;
548#endif
549 buf->b_p_eol = TRUE;
550 buf->b_start_eol = TRUE;
551#ifdef FEAT_MBYTE
552 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000553 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#endif
555 buf->b_ml.ml_mfp = NULL;
556 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
557#ifdef FEAT_NETBEANS_INTG
558 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559#endif
560}
561
562/*
563 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200564 * the file. flags:
565 * BFA_DEL buffer is going to be deleted
566 * BFA_WIPE buffer is going to be wiped out
567 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200570buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200572 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574#ifdef FEAT_AUTOCMD
575 int is_curbuf = (buf == curbuf);
576
Bram Moolenaar362ce482012-06-06 19:02:45 +0200577 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
579 if (!buf_valid(buf)) /* autocommands may delete the buffer */
580 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200581 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 {
583 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
584 if (!buf_valid(buf)) /* autocommands may delete the buffer */
585 return;
586 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200587 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 {
589 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
590 FALSE, buf);
591 if (!buf_valid(buf)) /* autocommands may delete the buffer */
592 return;
593 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200594 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000596 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 return;
598# endif
599
600 /*
601 * It's possible that autocommands change curbuf to the one being deleted.
602 * This might cause curbuf to be deleted unexpectedly. But in some cases
603 * it's OK to delete the curbuf, because a new one is obtained anyway.
604 * Therefore only return if curbuf changed to the deleted buffer.
605 */
606 if (buf == curbuf && !is_curbuf)
607 return;
608#endif
609#ifdef FEAT_DIFF
610 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
611#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200612#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100613 /* Remove any ownsyntax, unless exiting. */
614 if (firstwin != NULL && curwin->w_buffer == buf)
615 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200616#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000617
618#ifdef FEAT_FOLDING
619 /* No folds in an empty buffer. */
620# ifdef FEAT_WINDOWS
621 {
622 win_T *win;
623 tabpage_T *tp;
624
625 FOR_ALL_TAB_WINDOWS(tp, win)
626 if (win->w_buffer == buf)
627 clearFolding(win);
628 }
629# else
630 if (curwin->w_buffer == buf)
631 clearFolding(curwin);
632# endif
633#endif
634
Bram Moolenaar071d4272004-06-13 20:20:40 +0000635#ifdef FEAT_TCL
636 tcl_buffer_free(buf);
637#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 ml_close(buf, TRUE); /* close and delete the memline/memfile */
639 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200640 if ((flags & BFA_KEEP_UNDO) == 0)
641 {
642 u_blockfree(buf); /* free the memory allocated for undo */
643 u_clearall(buf); /* reset all undo information */
644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200646 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000648 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649}
650
651/*
652 * Free a buffer structure and the things it contains related to the buffer
653 * itself (not the file, that must have been done already).
654 */
655 static void
656free_buffer(buf)
657 buf_T *buf;
658{
659 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200660#ifdef FEAT_EVAL
661 unref_var_dict(buf->b_vars);
662#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200663#ifdef FEAT_LUA
664 lua_buffer_free(buf);
665#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000666#ifdef FEAT_MZSCHEME
667 mzscheme_buffer_free(buf);
668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#ifdef FEAT_PERL
670 perl_buf_free(buf);
671#endif
672#ifdef FEAT_PYTHON
673 python_buffer_free(buf);
674#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200675#ifdef FEAT_PYTHON3
676 python3_buffer_free(buf);
677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678#ifdef FEAT_RUBY
679 ruby_buffer_free(buf);
680#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000681#ifdef FEAT_AUTOCMD
682 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200683 if (autocmd_busy)
684 {
685 /* Do not free the buffer structure while autocommands are executing,
686 * it's still needed. Free it when autocmd_busy is reset. */
687 buf->b_next = au_pending_free_buf;
688 au_pending_free_buf = buf;
689 }
690 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000691#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200692 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693}
694
695/*
696 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
697 */
698 static void
699free_buffer_stuff(buf, free_options)
700 buf_T *buf;
701 int free_options; /* free options as well */
702{
703 if (free_options)
704 {
705 clear_wininfo(buf); /* including window-local options */
706 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200707#ifdef FEAT_SPELL
708 ga_clear(&buf->b_s.b_langp);
709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200712 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
713 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714#endif
715#ifdef FEAT_USR_CMDS
716 uc_clear(&buf->b_ucmds); /* clear local user commands */
717#endif
718#ifdef FEAT_SIGNS
719 buf_delete_signs(buf); /* delete any signs */
720#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000721#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200722 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724#ifdef FEAT_LOCALMAP
725 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
726 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
727#endif
728#ifdef FEAT_MBYTE
729 vim_free(buf->b_start_fenc);
730 buf->b_start_fenc = NULL;
731#endif
732}
733
734/*
735 * Free the b_wininfo list for buffer "buf".
736 */
737 static void
738clear_wininfo(buf)
739 buf_T *buf;
740{
741 wininfo_T *wip;
742
743 while (buf->b_wininfo != NULL)
744 {
745 wip = buf->b_wininfo;
746 buf->b_wininfo = wip->wi_next;
747 if (wip->wi_optset)
748 {
749 clear_winopt(&wip->wi_opt);
750#ifdef FEAT_FOLDING
751 deleteFoldRecurse(&wip->wi_folds);
752#endif
753 }
754 vim_free(wip);
755 }
756}
757
758#if defined(FEAT_LISTCMDS) || defined(PROTO)
759/*
760 * Go to another buffer. Handles the result of the ATTENTION dialog.
761 */
762 void
763goto_buffer(eap, start, dir, count)
764 exarg_T *eap;
765 int start;
766 int dir;
767 int count;
768{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000769# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 buf_T *old_curbuf = curbuf;
771
772 swap_exists_action = SEA_DIALOG;
773# endif
774 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
775 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000776# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
778 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000779# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
780 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000781
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000782 /* Reset the error/interrupt/exception state here so that
783 * aborting() returns FALSE when closing a window. */
784 enter_cleanup(&cs);
785# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000786
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000787 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 win_close(curwin, TRUE);
789 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000790 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000791
792# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
793 /* Restore the error/interrupt/exception state if not discarded by a
794 * new aborting error, interrupt, or uncaught exception. */
795 leave_cleanup(&cs);
796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 }
798 else
799 handle_swap_exists(old_curbuf);
800# endif
801}
802#endif
803
Bram Moolenaare64ac772005-12-07 20:54:59 +0000804#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805/*
806 * Handle the situation of swap_exists_action being set.
807 * It is allowed for "old_curbuf" to be NULL or invalid.
808 */
809 void
810handle_swap_exists(old_curbuf)
811 buf_T *old_curbuf;
812{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000813# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
814 cleanup_T cs;
815# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100816#ifdef FEAT_SYN_HL
817 long old_tw = curbuf->b_p_tw;
818#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000819
Bram Moolenaar071d4272004-06-13 20:20:40 +0000820 if (swap_exists_action == SEA_QUIT)
821 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000822# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
823 /* Reset the error/interrupt/exception state here so that
824 * aborting() returns FALSE when closing a buffer. */
825 enter_cleanup(&cs);
826# endif
827
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 /* User selected Quit at ATTENTION prompt. Go back to previous
829 * buffer. If that buffer is gone or the same as the current one,
830 * open a new, empty buffer. */
831 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000832 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100833 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000835 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100837 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100839#ifdef FEAT_SYN_HL
840 if (old_tw != curbuf->b_p_tw)
841 check_colorcolumn(curwin);
842#endif
843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000845
846# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
847 /* Restore the error/interrupt/exception state if not discarded by a
848 * new aborting error, interrupt, or uncaught exception. */
849 leave_cleanup(&cs);
850# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 else if (swap_exists_action == SEA_RECOVER)
853 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000854# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
855 /* Reset the error/interrupt/exception state here so that
856 * aborting() returns FALSE when closing a buffer. */
857 enter_cleanup(&cs);
858# endif
859
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 /* User selected Recover at ATTENTION prompt. */
861 msg_scroll = TRUE;
862 ml_recover();
863 MSG_PUTS("\n"); /* don't overwrite the last message */
864 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000865 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000866
867# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
868 /* Restore the error/interrupt/exception state if not discarded by a
869 * new aborting error, interrupt, or uncaught exception. */
870 leave_cleanup(&cs);
871# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000872 }
873 swap_exists_action = SEA_NONE;
874}
875#endif
876
877#if defined(FEAT_LISTCMDS) || defined(PROTO)
878/*
879 * do_bufdel() - delete or unload buffer(s)
880 *
881 * addr_count == 0: ":bdel" - delete current buffer
882 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
883 * buffer "end_bnr", then any other arguments.
884 * addr_count == 2: ":N,N bdel" - delete buffers in range
885 *
886 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
887 * DOBUF_DEL (":bdel")
888 *
889 * Returns error message or NULL
890 */
891 char_u *
892do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
893 int command;
894 char_u *arg; /* pointer to extra arguments */
895 int addr_count;
896 int start_bnr; /* first buffer number in a range */
897 int end_bnr; /* buffer nr or last buffer nr in a range */
898 int forceit;
899{
900 int do_current = 0; /* delete current buffer? */
901 int deleted = 0; /* number of buffers deleted */
902 char_u *errormsg = NULL; /* return value */
903 int bnr; /* buffer number */
904 char_u *p;
905
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 if (addr_count == 0)
907 {
908 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
909 }
910 else
911 {
912 if (addr_count == 2)
913 {
914 if (*arg) /* both range and argument is not allowed */
915 return (char_u *)_(e_trailing);
916 bnr = start_bnr;
917 }
918 else /* addr_count == 1 */
919 bnr = end_bnr;
920
921 for ( ;!got_int; ui_breakcheck())
922 {
923 /*
924 * delete the current buffer last, otherwise when the
925 * current buffer is deleted, the next buffer becomes
926 * the current one and will be loaded, which may then
927 * also be deleted, etc.
928 */
929 if (bnr == curbuf->b_fnum)
930 do_current = bnr;
931 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
932 forceit) == OK)
933 ++deleted;
934
935 /*
936 * find next buffer number to delete/unload
937 */
938 if (addr_count == 2)
939 {
940 if (++bnr > end_bnr)
941 break;
942 }
943 else /* addr_count == 1 */
944 {
945 arg = skipwhite(arg);
946 if (*arg == NUL)
947 break;
948 if (!VIM_ISDIGIT(*arg))
949 {
950 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100951 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
952 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 if (bnr < 0) /* failed */
954 break;
955 arg = p;
956 }
957 else
958 bnr = getdigits(&arg);
959 }
960 }
961 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
962 FORWARD, do_current, forceit) == OK)
963 ++deleted;
964
965 if (deleted == 0)
966 {
967 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000968 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000970 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000972 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 errormsg = IObuff;
974 }
975 else if (deleted >= p_report)
976 {
977 if (command == DOBUF_UNLOAD)
978 {
979 if (deleted == 1)
980 MSG(_("1 buffer unloaded"));
981 else
982 smsg((char_u *)_("%d buffers unloaded"), deleted);
983 }
984 else if (command == DOBUF_DEL)
985 {
986 if (deleted == 1)
987 MSG(_("1 buffer deleted"));
988 else
989 smsg((char_u *)_("%d buffers deleted"), deleted);
990 }
991 else
992 {
993 if (deleted == 1)
994 MSG(_("1 buffer wiped out"));
995 else
996 smsg((char_u *)_("%d buffers wiped out"), deleted);
997 }
998 }
999 }
1000
Bram Moolenaar071d4272004-06-13 20:20:40 +00001001
1002 return errormsg;
1003}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001004#endif /* FEAT_LISTCMDS */
1005
1006#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1007 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008
Bram Moolenaara02471e2014-01-10 16:43:14 +01001009static int empty_curbuf __ARGS((int close_others, int forceit, int action));
1010
1011/*
1012 * Make the current buffer empty.
1013 * Used when it is wiped out and it's the last buffer.
1014 */
1015 static int
1016empty_curbuf(close_others, forceit, action)
1017 int close_others;
1018 int forceit;
1019 int action;
1020{
1021 int retval;
1022 buf_T *buf = curbuf;
1023
1024 if (action == DOBUF_UNLOAD)
1025 {
1026 EMSG(_("E90: Cannot unload last buffer"));
1027 return FAIL;
1028 }
1029
1030 if (close_others)
1031 {
1032 /* Close any other windows on this buffer, then make it empty. */
1033#ifdef FEAT_WINDOWS
1034 close_windows(buf, TRUE);
1035#endif
1036 }
1037
1038 setpcmark();
1039 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1040 forceit ? ECMD_FORCEIT : 0, curwin);
1041
1042 /*
1043 * do_ecmd() may create a new buffer, then we have to delete
1044 * the old one. But do_ecmd() may have done that already, check
1045 * if the buffer still exists.
1046 */
1047 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1048 close_buffer(NULL, buf, action, FALSE);
1049 if (!close_others)
1050 need_fileinfo = FALSE;
1051 return retval;
1052}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053/*
1054 * Implementation of the commands for the buffer list.
1055 *
1056 * action == DOBUF_GOTO go to specified buffer
1057 * action == DOBUF_SPLIT split window and go to specified buffer
1058 * action == DOBUF_UNLOAD unload specified buffer(s)
1059 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1060 * action == DOBUF_WIPE delete specified buffer(s) really
1061 *
1062 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1063 * start == DOBUF_FIRST go to "count" buffer from first buffer
1064 * start == DOBUF_LAST go to "count" buffer from last buffer
1065 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1066 *
1067 * Return FAIL or OK.
1068 */
1069 int
1070do_buffer(action, start, dir, count, forceit)
1071 int action;
1072 int start;
1073 int dir; /* FORWARD or BACKWARD */
1074 int count; /* buffer number or number of buffers */
1075 int forceit; /* TRUE for :...! */
1076{
1077 buf_T *buf;
1078 buf_T *bp;
1079 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1080 || action == DOBUF_WIPE);
1081
1082 switch (start)
1083 {
1084 case DOBUF_FIRST: buf = firstbuf; break;
1085 case DOBUF_LAST: buf = lastbuf; break;
1086 default: buf = curbuf; break;
1087 }
1088 if (start == DOBUF_MOD) /* find next modified buffer */
1089 {
1090 while (count-- > 0)
1091 {
1092 do
1093 {
1094 buf = buf->b_next;
1095 if (buf == NULL)
1096 buf = firstbuf;
1097 }
1098 while (buf != curbuf && !bufIsChanged(buf));
1099 }
1100 if (!bufIsChanged(buf))
1101 {
1102 EMSG(_("E84: No modified buffer found"));
1103 return FAIL;
1104 }
1105 }
1106 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1107 {
1108 while (buf != NULL && buf->b_fnum != count)
1109 buf = buf->b_next;
1110 }
1111 else
1112 {
1113 bp = NULL;
1114 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1115 {
1116 /* remember the buffer where we start, we come back there when all
1117 * buffers are unlisted. */
1118 if (bp == NULL)
1119 bp = buf;
1120 if (dir == FORWARD)
1121 {
1122 buf = buf->b_next;
1123 if (buf == NULL)
1124 buf = firstbuf;
1125 }
1126 else
1127 {
1128 buf = buf->b_prev;
1129 if (buf == NULL)
1130 buf = lastbuf;
1131 }
1132 /* don't count unlisted buffers */
1133 if (unload || buf->b_p_bl)
1134 {
1135 --count;
1136 bp = NULL; /* use this buffer as new starting point */
1137 }
1138 if (bp == buf)
1139 {
1140 /* back where we started, didn't find anything. */
1141 EMSG(_("E85: There is no listed buffer"));
1142 return FAIL;
1143 }
1144 }
1145 }
1146
1147 if (buf == NULL) /* could not find it */
1148 {
1149 if (start == DOBUF_FIRST)
1150 {
1151 /* don't warn when deleting */
1152 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001153 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 }
1155 else if (dir == FORWARD)
1156 EMSG(_("E87: Cannot go beyond last buffer"));
1157 else
1158 EMSG(_("E88: Cannot go before first buffer"));
1159 return FAIL;
1160 }
1161
1162#ifdef FEAT_GUI
1163 need_mouse_correct = TRUE;
1164#endif
1165
1166#ifdef FEAT_LISTCMDS
1167 /*
1168 * delete buffer buf from memory and/or the list
1169 */
1170 if (unload)
1171 {
1172 int forward;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173
1174 /* When unloading or deleting a buffer that's already unloaded and
1175 * unlisted: fail silently. */
1176 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1177 return FAIL;
1178
1179 if (!forceit && bufIsChanged(buf))
1180 {
1181#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1182 if ((p_confirm || cmdmod.confirm) && p_write)
1183 {
1184 dialog_changed(buf, FALSE);
1185# ifdef FEAT_AUTOCMD
1186 if (!buf_valid(buf))
1187 /* Autocommand deleted buffer, oops! It's not changed
1188 * now. */
1189 return FAIL;
1190# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001191 /* If it's still changed fail silently, the dialog already
1192 * mentioned why it fails. */
1193 if (bufIsChanged(buf))
1194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001196 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197#endif
1198 {
1199 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1200 buf->b_fnum);
1201 return FAIL;
1202 }
1203 }
1204
1205 /*
1206 * If deleting the last (listed) buffer, make it empty.
1207 * The last (listed) buffer cannot be unloaded.
1208 */
1209 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1210 if (bp->b_p_bl && bp != buf)
1211 break;
1212 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001213 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214
1215#ifdef FEAT_WINDOWS
1216 /*
1217 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001218 * (unless it's the only window). Repeat this so long as we end up in
1219 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001221 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001222# ifdef FEAT_AUTOCMD
1223 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1224# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001225 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001226 {
1227 if (win_close(curwin, FALSE) == FAIL)
1228 break;
1229 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230#endif
1231
1232 /*
1233 * If the buffer to be deleted is not the current one, delete it here.
1234 */
1235 if (buf != curbuf)
1236 {
1237#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001238 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#endif
1240 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001241 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 return OK;
1243 }
1244
1245 /*
1246 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001247 * There should be another, otherwise it would have been handled
1248 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 * First use au_new_curbuf, if it is valid.
1250 * Then prefer the buffer we most recently visited.
1251 * Else try to find one that is loaded, after the current buffer,
1252 * then before the current buffer.
1253 * Finally use any buffer.
1254 */
1255 buf = NULL; /* selected buffer */
1256 bp = NULL; /* used when no loaded buffer found */
1257#ifdef FEAT_AUTOCMD
1258 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1259 buf = au_new_curbuf;
1260# ifdef FEAT_JUMPLIST
1261 else
1262# endif
1263#endif
1264#ifdef FEAT_JUMPLIST
1265 if (curwin->w_jumplistlen > 0)
1266 {
1267 int jumpidx;
1268
1269 jumpidx = curwin->w_jumplistidx - 1;
1270 if (jumpidx < 0)
1271 jumpidx = curwin->w_jumplistlen - 1;
1272
1273 forward = jumpidx;
1274 while (jumpidx != curwin->w_jumplistidx)
1275 {
1276 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1277 if (buf != NULL)
1278 {
1279 if (buf == curbuf || !buf->b_p_bl)
1280 buf = NULL; /* skip current and unlisted bufs */
1281 else if (buf->b_ml.ml_mfp == NULL)
1282 {
1283 /* skip unloaded buf, but may keep it for later */
1284 if (bp == NULL)
1285 bp = buf;
1286 buf = NULL;
1287 }
1288 }
1289 if (buf != NULL) /* found a valid buffer: stop searching */
1290 break;
1291 /* advance to older entry in jump list */
1292 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1293 break;
1294 if (--jumpidx < 0)
1295 jumpidx = curwin->w_jumplistlen - 1;
1296 if (jumpidx == forward) /* List exhausted for sure */
1297 break;
1298 }
1299 }
1300#endif
1301
1302 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1303 {
1304 forward = TRUE;
1305 buf = curbuf->b_next;
1306 for (;;)
1307 {
1308 if (buf == NULL)
1309 {
1310 if (!forward) /* tried both directions */
1311 break;
1312 buf = curbuf->b_prev;
1313 forward = FALSE;
1314 continue;
1315 }
1316 /* in non-help buffer, try to skip help buffers, and vv */
1317 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1318 {
1319 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1320 break;
1321 if (bp == NULL) /* remember unloaded buf for later */
1322 bp = buf;
1323 }
1324 if (forward)
1325 buf = buf->b_next;
1326 else
1327 buf = buf->b_prev;
1328 }
1329 }
1330 if (buf == NULL) /* No loaded buffer, use unloaded one */
1331 buf = bp;
1332 if (buf == NULL) /* No loaded buffer, find listed one */
1333 {
1334 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1335 if (buf->b_p_bl && buf != curbuf)
1336 break;
1337 }
1338 if (buf == NULL) /* Still no buffer, just take one */
1339 {
1340 if (curbuf->b_next != NULL)
1341 buf = curbuf->b_next;
1342 else
1343 buf = curbuf->b_prev;
1344 }
1345 }
1346
Bram Moolenaara02471e2014-01-10 16:43:14 +01001347 if (buf == NULL)
1348 {
1349 /* Autocommands must have wiped out all other buffers. Only option
1350 * now is to make the current buffer empty. */
1351 return empty_curbuf(FALSE, forceit, action);
1352 }
1353
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 /*
1355 * make buf current buffer
1356 */
1357 if (action == DOBUF_SPLIT) /* split window first */
1358 {
1359# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001360 /* If 'switchbuf' contains "useopen": jump to first window containing
1361 * "buf" if one exists */
1362 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001363 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001364 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001365 * page containing "buf" if one exists */
1366 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 return OK;
1368 if (win_split(0, 0) == FAIL)
1369# endif
1370 return FAIL;
1371 }
1372#endif
1373
1374 /* go to current buffer - nothing to do */
1375 if (buf == curbuf)
1376 return OK;
1377
1378 /*
1379 * Check if the current buffer may be abandoned.
1380 */
1381 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1382 {
1383#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1384 if ((p_confirm || cmdmod.confirm) && p_write)
1385 {
1386 dialog_changed(curbuf, FALSE);
1387# ifdef FEAT_AUTOCMD
1388 if (!buf_valid(buf))
1389 /* Autocommand deleted buffer, oops! */
1390 return FAIL;
1391# endif
1392 }
1393 if (bufIsChanged(curbuf))
1394#endif
1395 {
1396 EMSG(_(e_nowrtmsg));
1397 return FAIL;
1398 }
1399 }
1400
1401 /* Go to the other buffer. */
1402 set_curbuf(buf, action);
1403
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001404#if defined(FEAT_LISTCMDS) \
1405 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001407 {
1408 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#endif
1411
1412#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1413 if (aborting()) /* autocmds may abort script processing */
1414 return FAIL;
1415#endif
1416
1417 return OK;
1418}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
1421/*
1422 * Set current buffer to "buf". Executes autocommands and closes current
1423 * buffer. "action" tells how to close the current buffer:
1424 * DOBUF_GOTO free or hide it
1425 * DOBUF_SPLIT nothing
1426 * DOBUF_UNLOAD unload it
1427 * DOBUF_DEL delete it
1428 * DOBUF_WIPE wipe it out
1429 */
1430 void
1431set_curbuf(buf, action)
1432 buf_T *buf;
1433 int action;
1434{
1435 buf_T *prevbuf;
1436 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1437 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001438#ifdef FEAT_SYN_HL
1439 long old_tw = curbuf->b_p_tw;
1440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441
1442 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001443 if (!cmdmod.keepalt)
1444 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001445 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /* Don't restart Select mode after switching to another buffer. */
1448 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449
1450 /* close_windows() or apply_autocmds() may change curbuf */
1451 prevbuf = curbuf;
1452
1453#ifdef FEAT_AUTOCMD
1454 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1455# ifdef FEAT_EVAL
1456 if (buf_valid(prevbuf) && !aborting())
1457# else
1458 if (buf_valid(prevbuf))
1459# endif
1460#endif
1461 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001462#ifdef FEAT_SYN_HL
1463 if (prevbuf == curwin->w_buffer)
1464 reset_synblock(curwin);
1465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#ifdef FEAT_WINDOWS
1467 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001468 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469#endif
1470#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1471 if (buf_valid(prevbuf) && !aborting())
1472#else
1473 if (buf_valid(prevbuf))
1474#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001475 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001476#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001477 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001478#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001479 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001480 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1482 unload ? action : (action == DOBUF_GOTO
1483 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001484 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001485#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001486 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001487 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001488 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001489#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 }
1492#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001493 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001494 * it did ":bunload") or aborted the script processing!
1495 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1496 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001497# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001498 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001499# endif
1500# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001501 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001502# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001503 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001505 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001507#ifdef FEAT_SYN_HL
1508 if (old_tw != curbuf->b_p_tw)
1509 check_colorcolumn(curwin);
1510#endif
1511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512}
1513
1514/*
1515 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001516 * Old curbuf must have been abandoned already! This also means "curbuf" may
1517 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001518 */
1519 void
1520enter_buffer(buf)
1521 buf_T *buf;
1522{
1523 /* Copy buffer and window local option values. Not for a help buffer. */
1524 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1525 if (!buf->b_help)
1526 get_winopts(buf);
1527#ifdef FEAT_FOLDING
1528 else
1529 /* Remove all folds in the window. */
1530 clearFolding(curwin);
1531 foldUpdateAll(curwin); /* update folds (later). */
1532#endif
1533
1534 /* Get the buffer in the current window. */
1535 curwin->w_buffer = buf;
1536 curbuf = buf;
1537 ++curbuf->b_nwindows;
1538
1539#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001540 if (curwin->w_p_diff)
1541 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542#endif
1543
Bram Moolenaara971b822011-09-14 14:43:25 +02001544#ifdef FEAT_SYN_HL
1545 curwin->w_s = &(buf->b_s);
1546#endif
1547
Bram Moolenaar071d4272004-06-13 20:20:40 +00001548 /* Cursor on first line by default. */
1549 curwin->w_cursor.lnum = 1;
1550 curwin->w_cursor.col = 0;
1551#ifdef FEAT_VIRTUALEDIT
1552 curwin->w_cursor.coladd = 0;
1553#endif
1554 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001555#ifdef FEAT_AUTOCMD
1556 curwin->w_topline_was_set = FALSE;
1557#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001559 /* mark cursor position as being invalid */
1560 curwin->w_valid = 0;
1561
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 /* Make sure the buffer is loaded. */
1563 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001564 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001565#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001566 /* If there is no filetype, allow for detecting one. Esp. useful for
1567 * ":ball" used in a autocommand. If there already is a filetype we
1568 * might prefer to keep it. */
1569 if (*curbuf->b_p_ft == NUL)
1570 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001571#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001572
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001573 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 else
1576 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001577 if (!msg_silent)
1578 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1580#ifdef FEAT_AUTOCMD
1581 curwin->w_topline = 1;
1582# ifdef FEAT_DIFF
1583 curwin->w_topfill = 0;
1584# endif
1585 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1586 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1587#endif
1588 }
1589
1590 /* If autocommands did not change the cursor position, restore cursor lnum
1591 * and possibly cursor col. */
1592 if (curwin->w_cursor.lnum == 1 && inindent(0))
1593 buflist_getfpos();
1594
1595 check_arg_idx(curwin); /* check for valid arg_idx */
1596#ifdef FEAT_TITLE
1597 maketitle();
1598#endif
1599#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001600 /* when autocmds didn't change it */
1601 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602#endif
1603 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1604
Bram Moolenaar009b2592004-10-24 19:18:58 +00001605#ifdef FEAT_NETBEANS_INTG
1606 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001607 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001608#endif
1609
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001610 /* Change directories when the 'acd' option is set. */
1611 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613#ifdef FEAT_KEYMAP
1614 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001615 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001617#ifdef FEAT_SPELL
1618 /* May need to set the spell language. Can only do this after the buffer
1619 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001620 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1621 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001622#endif
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 redraw_later(NOT_VALID);
1625}
1626
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001627#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1628/*
1629 * Change to the directory of the current buffer.
1630 */
1631 void
1632do_autochdir()
1633{
1634 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1635 shorten_fnames(TRUE);
1636}
1637#endif
1638
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639/*
1640 * functions for dealing with the buffer list
1641 */
1642
1643/*
1644 * Add a file name to the buffer list. Return a pointer to the buffer.
1645 * If the same file name already exists return a pointer to that buffer.
1646 * If it does not exist, or if fname == NULL, a new entry is created.
1647 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1648 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1649 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 * This is the ONLY way to create a new buffer.
1651 */
1652static int top_file_num = 1; /* highest file number */
1653
1654 buf_T *
1655buflist_new(ffname, sfname, lnum, flags)
1656 char_u *ffname; /* full path of fname or relative */
1657 char_u *sfname; /* short fname or NULL */
1658 linenr_T lnum; /* preferred cursor line */
1659 int flags; /* BLN_ defines */
1660{
1661 buf_T *buf;
1662#ifdef UNIX
1663 struct stat st;
1664#endif
1665
1666 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1667
1668 /*
1669 * If file name already exists in the list, update the entry.
1670 */
1671#ifdef UNIX
1672 /* On Unix we can use inode numbers when the file exists. Works better
1673 * for hard links. */
1674 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1675 st.st_dev = (dev_T)-1;
1676#endif
1677 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1678#ifdef UNIX
1679 buflist_findname_stat(ffname, &st)
1680#else
1681 buflist_findname(ffname)
1682#endif
1683 ) != NULL)
1684 {
1685 vim_free(ffname);
1686 if (lnum != 0)
1687 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1688 /* copy the options now, if 'cpo' doesn't have 's' and not done
1689 * already */
1690 buf_copy_options(buf, 0);
1691 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1692 {
1693 buf->b_p_bl = TRUE;
1694#ifdef FEAT_AUTOCMD
1695 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001696 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001698 if (!buf_valid(buf))
1699 return NULL;
1700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701#endif
1702 }
1703 return buf;
1704 }
1705
1706 /*
1707 * If the current buffer has no name and no contents, use the current
1708 * buffer. Otherwise: Need to allocate a new buffer structure.
1709 *
1710 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001711 * (A spell file buffer is allocated in spell.c, but that's not a normal
1712 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713 */
1714 buf = NULL;
1715 if ((flags & BLN_CURBUF)
1716 && curbuf != NULL
1717 && curbuf->b_ffname == NULL
1718 && curbuf->b_nwindows <= 1
1719 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1720 {
1721 buf = curbuf;
1722#ifdef FEAT_AUTOCMD
1723 /* It's like this buffer is deleted. Watch out for autocommands that
1724 * change curbuf! If that happens, allocate a new buffer anyway. */
1725 if (curbuf->b_p_bl)
1726 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1727 if (buf == curbuf)
1728 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1729# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001730 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 return NULL;
1732# endif
1733#endif
1734#ifdef FEAT_QUICKFIX
1735# ifdef FEAT_AUTOCMD
1736 if (buf == curbuf)
1737# endif
1738 {
1739 /* Make sure 'bufhidden' and 'buftype' are empty */
1740 clear_string_option(&buf->b_p_bh);
1741 clear_string_option(&buf->b_p_bt);
1742 }
1743#endif
1744 }
1745 if (buf != curbuf || curbuf == NULL)
1746 {
1747 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1748 if (buf == NULL)
1749 {
1750 vim_free(ffname);
1751 return NULL;
1752 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001753#ifdef FEAT_EVAL
1754 /* init b: variables */
1755 buf->b_vars = dict_alloc();
1756 if (buf->b_vars == NULL)
1757 {
1758 vim_free(ffname);
1759 vim_free(buf);
1760 return NULL;
1761 }
1762 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 }
1765
1766 if (ffname != NULL)
1767 {
1768 buf->b_ffname = ffname;
1769 buf->b_sfname = vim_strsave(sfname);
1770 }
1771
1772 clear_wininfo(buf);
1773 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1774
1775 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1776 || buf->b_wininfo == NULL)
1777 {
1778 vim_free(buf->b_ffname);
1779 buf->b_ffname = NULL;
1780 vim_free(buf->b_sfname);
1781 buf->b_sfname = NULL;
1782 if (buf != curbuf)
1783 free_buffer(buf);
1784 return NULL;
1785 }
1786
1787 if (buf == curbuf)
1788 {
1789 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001790 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 if (buf != curbuf) /* autocommands deleted the buffer! */
1792 return NULL;
1793#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001794 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 return NULL;
1796#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001798
1799 /* Init the options. */
1800 buf->b_p_initialized = FALSE;
1801 buf_copy_options(buf, BCO_ENTER);
1802
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803#ifdef FEAT_KEYMAP
1804 /* need to reload lmaps and set b:keymap_name */
1805 curbuf->b_kmap_state |= KEYMAP_INIT;
1806#endif
1807 }
1808 else
1809 {
1810 /*
1811 * put new buffer at the end of the buffer list
1812 */
1813 buf->b_next = NULL;
1814 if (firstbuf == NULL) /* buffer list is empty */
1815 {
1816 buf->b_prev = NULL;
1817 firstbuf = buf;
1818 }
1819 else /* append new buffer at end of list */
1820 {
1821 lastbuf->b_next = buf;
1822 buf->b_prev = lastbuf;
1823 }
1824 lastbuf = buf;
1825
1826 buf->b_fnum = top_file_num++;
1827 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1828 {
1829 EMSG(_("W14: Warning: List of file names overflow"));
1830 if (emsg_silent == 0)
1831 {
1832 out_flush();
1833 ui_delay(3000L, TRUE); /* make sure it is noticed */
1834 }
1835 top_file_num = 1;
1836 }
1837
1838 /*
1839 * Always copy the options from the current buffer.
1840 */
1841 buf_copy_options(buf, BCO_ALWAYS);
1842 }
1843
1844 buf->b_wininfo->wi_fpos.lnum = lnum;
1845 buf->b_wininfo->wi_win = curwin;
1846
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001847#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001848 hash_init(&buf->b_s.b_keywtab);
1849 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850#endif
1851
1852 buf->b_fname = buf->b_sfname;
1853#ifdef UNIX
1854 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001855 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001858 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 buf->b_dev = st.st_dev;
1860 buf->b_ino = st.st_ino;
1861 }
1862#endif
1863 buf->b_u_synced = TRUE;
1864 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001865 if (flags & BLN_DUMMY)
1866 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 buf_clear_file(buf);
1868 clrallmarks(buf); /* clear marks */
1869 fmarks_check_names(buf); /* check file marks for this file */
1870 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1871#ifdef FEAT_AUTOCMD
1872 if (!(flags & BLN_DUMMY))
1873 {
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001874 /* Tricky: these autocommands may change the buffer list. They could
1875 * also split the window with re-using the one empty buffer. This may
1876 * result in unexpectedly losing the empty buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001878 if (!buf_valid(buf))
1879 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001881 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001883 if (!buf_valid(buf))
1884 return NULL;
1885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001887 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 return NULL;
1889# endif
1890 }
1891#endif
1892
1893 return buf;
1894}
1895
1896/*
1897 * Free the memory for the options of a buffer.
1898 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1899 * 'fileencoding'.
1900 */
1901 void
1902free_buf_options(buf, free_p_ff)
1903 buf_T *buf;
1904 int free_p_ff;
1905{
1906 if (free_p_ff)
1907 {
1908#ifdef FEAT_MBYTE
1909 clear_string_option(&buf->b_p_fenc);
1910#endif
1911 clear_string_option(&buf->b_p_ff);
1912#ifdef FEAT_QUICKFIX
1913 clear_string_option(&buf->b_p_bh);
1914 clear_string_option(&buf->b_p_bt);
1915#endif
1916 }
1917#ifdef FEAT_FIND_ID
1918 clear_string_option(&buf->b_p_def);
1919 clear_string_option(&buf->b_p_inc);
1920# ifdef FEAT_EVAL
1921 clear_string_option(&buf->b_p_inex);
1922# endif
1923#endif
1924#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1925 clear_string_option(&buf->b_p_inde);
1926 clear_string_option(&buf->b_p_indk);
1927#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001928#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1929 clear_string_option(&buf->b_p_bexpr);
1930#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001931#if defined(FEAT_CRYPT)
1932 clear_string_option(&buf->b_p_cm);
1933#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001934#if defined(FEAT_EVAL)
1935 clear_string_option(&buf->b_p_fex);
1936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937#ifdef FEAT_CRYPT
1938 clear_string_option(&buf->b_p_key);
1939#endif
1940 clear_string_option(&buf->b_p_kp);
1941 clear_string_option(&buf->b_p_mps);
1942 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001943 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 clear_string_option(&buf->b_p_isk);
1945#ifdef FEAT_KEYMAP
1946 clear_string_option(&buf->b_p_keymap);
1947 ga_clear(&buf->b_kmap_ga);
1948#endif
1949#ifdef FEAT_COMMENTS
1950 clear_string_option(&buf->b_p_com);
1951#endif
1952#ifdef FEAT_FOLDING
1953 clear_string_option(&buf->b_p_cms);
1954#endif
1955 clear_string_option(&buf->b_p_nf);
1956#ifdef FEAT_SYN_HL
1957 clear_string_option(&buf->b_p_syn);
Bram Moolenaarb8060fe2016-01-19 22:29:28 +01001958 clear_string_option(&buf->b_s.b_syn_isk);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001959#endif
1960#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001961 clear_string_option(&buf->b_s.b_p_spc);
1962 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001963 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001964 buf->b_s.b_cap_prog = NULL;
1965 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966#endif
1967#ifdef FEAT_SEARCHPATH
1968 clear_string_option(&buf->b_p_sua);
1969#endif
1970#ifdef FEAT_AUTOCMD
1971 clear_string_option(&buf->b_p_ft);
1972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973#ifdef FEAT_CINDENT
1974 clear_string_option(&buf->b_p_cink);
1975 clear_string_option(&buf->b_p_cino);
1976#endif
1977#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1978 clear_string_option(&buf->b_p_cinw);
1979#endif
1980#ifdef FEAT_INS_EXPAND
1981 clear_string_option(&buf->b_p_cpt);
1982#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001983#ifdef FEAT_COMPL_FUNC
1984 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001985 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987#ifdef FEAT_QUICKFIX
1988 clear_string_option(&buf->b_p_gp);
1989 clear_string_option(&buf->b_p_mp);
1990 clear_string_option(&buf->b_p_efm);
1991#endif
1992 clear_string_option(&buf->b_p_ep);
1993 clear_string_option(&buf->b_p_path);
1994 clear_string_option(&buf->b_p_tags);
Bram Moolenaar0f6562e2015-11-24 18:48:14 +01001995 clear_string_option(&buf->b_p_tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996#ifdef FEAT_INS_EXPAND
1997 clear_string_option(&buf->b_p_dict);
1998 clear_string_option(&buf->b_p_tsr);
1999#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002000#ifdef FEAT_TEXTOBJ
2001 clear_string_option(&buf->b_p_qe);
2002#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002004 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002005#ifdef FEAT_LISP
2006 clear_string_option(&buf->b_p_lw);
2007#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002008 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009}
2010
2011/*
2012 * get alternate file n
2013 * set linenr to lnum or altfpos.lnum if lnum == 0
2014 * also set cursor column to altfpos.col if 'startofline' is not set.
2015 * if (options & GETF_SETMARK) call setpcmark()
2016 * if (options & GETF_ALT) we are jumping to an alternate file.
2017 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2018 *
2019 * return FAIL for failure, OK for success
2020 */
2021 int
2022buflist_getfile(n, lnum, options, forceit)
2023 int n;
2024 linenr_T lnum;
2025 int options;
2026 int forceit;
2027{
2028 buf_T *buf;
2029#ifdef FEAT_WINDOWS
2030 win_T *wp = NULL;
2031#endif
2032 pos_T *fpos;
2033 colnr_T col;
2034
2035 buf = buflist_findnr(n);
2036 if (buf == NULL)
2037 {
2038 if ((options & GETF_ALT) && n == 0)
2039 EMSG(_(e_noalt));
2040 else
2041 EMSGN(_("E92: Buffer %ld not found"), n);
2042 return FAIL;
2043 }
2044
2045 /* if alternate file is the current buffer, nothing to do */
2046 if (buf == curbuf)
2047 return OK;
2048
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002049 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002050 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002051 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002053 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002054#ifdef FEAT_AUTOCMD
2055 if (curbuf_locked())
2056 return FAIL;
2057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058
2059 /* altfpos may be changed by getfile(), get it now */
2060 if (lnum == 0)
2061 {
2062 fpos = buflist_findfpos(buf);
2063 lnum = fpos->lnum;
2064 col = fpos->col;
2065 }
2066 else
2067 col = 0;
2068
2069#ifdef FEAT_WINDOWS
2070 if (options & GETF_SWITCH)
2071 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002072 /* If 'switchbuf' contains "useopen": jump to first window containing
2073 * "buf" if one exists */
2074 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002076
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002077 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002078 * page containing "buf" if one exists */
2079 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002080 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002081
2082 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2083 * current buffer isn't empty: open new tab or window */
2084 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2085 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002087 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002088 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002089 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2090 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002092 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 }
2094 }
2095#endif
2096
2097 ++RedrawingDisabled;
2098 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2099 lnum, forceit) <= 0)
2100 {
2101 --RedrawingDisabled;
2102
2103 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2104 if (!p_sol && col != 0)
2105 {
2106 curwin->w_cursor.col = col;
2107 check_cursor_col();
2108#ifdef FEAT_VIRTUALEDIT
2109 curwin->w_cursor.coladd = 0;
2110#endif
2111 curwin->w_set_curswant = TRUE;
2112 }
2113 return OK;
2114 }
2115 --RedrawingDisabled;
2116 return FAIL;
2117}
2118
2119/*
2120 * go to the last know line number for the current buffer
2121 */
2122 void
2123buflist_getfpos()
2124{
2125 pos_T *fpos;
2126
2127 fpos = buflist_findfpos(curbuf);
2128
2129 curwin->w_cursor.lnum = fpos->lnum;
2130 check_cursor_lnum();
2131
2132 if (p_sol)
2133 curwin->w_cursor.col = 0;
2134 else
2135 {
2136 curwin->w_cursor.col = fpos->col;
2137 check_cursor_col();
2138#ifdef FEAT_VIRTUALEDIT
2139 curwin->w_cursor.coladd = 0;
2140#endif
2141 curwin->w_set_curswant = TRUE;
2142 }
2143}
2144
Bram Moolenaar81695252004-12-29 20:58:21 +00002145#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2146/*
2147 * Find file in buffer list by name (it has to be for the current window).
2148 * Returns NULL if not found.
2149 */
2150 buf_T *
2151buflist_findname_exp(fname)
2152 char_u *fname;
2153{
2154 char_u *ffname;
2155 buf_T *buf = NULL;
2156
2157 /* First make the name into a full path name */
2158 ffname = FullName_save(fname,
2159#ifdef UNIX
2160 TRUE /* force expansion, get rid of symbolic links */
2161#else
2162 FALSE
2163#endif
2164 );
2165 if (ffname != NULL)
2166 {
2167 buf = buflist_findname(ffname);
2168 vim_free(ffname);
2169 }
2170 return buf;
2171}
2172#endif
2173
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174/*
2175 * Find file in buffer list by name (it has to be for the current window).
2176 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002177 * Skips dummy buffers.
2178 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 */
2180 buf_T *
2181buflist_findname(ffname)
2182 char_u *ffname;
2183{
2184#ifdef UNIX
2185 struct stat st;
2186
2187 if (mch_stat((char *)ffname, &st) < 0)
2188 st.st_dev = (dev_T)-1;
2189 return buflist_findname_stat(ffname, &st);
2190}
2191
2192/*
2193 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2194 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002195 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 */
2197 static buf_T *
2198buflist_findname_stat(ffname, stp)
2199 char_u *ffname;
2200 struct stat *stp;
2201{
2202#endif
2203 buf_T *buf;
2204
2205 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002206 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#ifdef UNIX
2208 , stp
2209#endif
2210 ))
2211 return buf;
2212 return NULL;
2213}
2214
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002215#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2216 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217/*
2218 * Find file in buffer list by a regexp pattern.
2219 * Return fnum of the found buffer.
2220 * Return < 0 for error.
2221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002223buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 char_u *pattern;
2225 char_u *pattern_end; /* pointer to first char after pattern */
2226 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002227 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002228 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229{
2230 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 int match = -1;
2232 int find_listed;
2233 char_u *pat;
2234 char_u *patend;
2235 int attempt;
2236 char_u *p;
2237 int toggledollar;
2238
2239 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2240 {
2241 if (*pattern == '%')
2242 match = curbuf->b_fnum;
2243 else
2244 match = curwin->w_alt_fnum;
2245#ifdef FEAT_DIFF
2246 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2247 match = -1;
2248#endif
2249 }
2250
2251 /*
2252 * Try four ways of matching a listed buffer:
2253 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002254 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 * attempt == 2: with '$' at end (only match at end)
2256 * attempt == 3: with '^' at start and '$' at end (only full match)
2257 * Repeat this for finding an unlisted buffer if there was no matching
2258 * listed buffer.
2259 */
2260 else
2261 {
2262 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2263 if (pat == NULL)
2264 return -1;
2265 patend = pat + STRLEN(pat) - 1;
2266 toggledollar = (patend > pat && *patend == '$');
2267
2268 /* First try finding a listed buffer. If not found and "unlisted"
2269 * is TRUE, try finding an unlisted buffer. */
2270 find_listed = TRUE;
2271 for (;;)
2272 {
2273 for (attempt = 0; attempt <= 3; ++attempt)
2274 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002275 regmatch_T regmatch;
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 /* may add '^' and '$' */
2278 if (toggledollar)
2279 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2280 p = pat;
2281 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2282 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002283 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2284 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 {
2286 vim_free(pat);
2287 return -1;
2288 }
2289
2290 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2291 if (buf->b_p_bl == find_listed
2292#ifdef FEAT_DIFF
2293 && (!diffmode || diff_mode_buf(buf))
2294#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002295 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002297 if (curtab_only)
2298 {
2299 /* Ignore the match if the buffer is not open in
2300 * the current tab. */
2301#ifdef FEAT_WINDOWS
2302 win_T *wp;
2303
2304 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2305 if (wp->w_buffer == buf)
2306 break;
2307 if (wp == NULL)
2308 continue;
2309#else
2310 if (curwin->w_buffer != buf)
2311 continue;
2312#endif
2313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 if (match >= 0) /* already found a match */
2315 {
2316 match = -2;
2317 break;
2318 }
2319 match = buf->b_fnum; /* remember first match */
2320 }
2321
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002322 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323 if (match >= 0) /* found one match */
2324 break;
2325 }
2326
2327 /* Only search for unlisted buffers if there was no match with
2328 * a listed buffer. */
2329 if (!unlisted || !find_listed || match != -1)
2330 break;
2331 find_listed = FALSE;
2332 }
2333
2334 vim_free(pat);
2335 }
2336
2337 if (match == -2)
2338 EMSG2(_("E93: More than one match for %s"), pattern);
2339 else if (match < 0)
2340 EMSG2(_("E94: No matching buffer for %s"), pattern);
2341 return match;
2342}
2343#endif
2344
2345#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2346
2347/*
2348 * Find all buffer names that match.
2349 * For command line expansion of ":buf" and ":sbuf".
2350 * Return OK if matches found, FAIL otherwise.
2351 */
2352 int
2353ExpandBufnames(pat, num_file, file, options)
2354 char_u *pat;
2355 int *num_file;
2356 char_u ***file;
2357 int options;
2358{
2359 int count = 0;
2360 buf_T *buf;
2361 int round;
2362 char_u *p;
2363 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002364 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
2366 *num_file = 0; /* return values in case of FAIL */
2367 *file = NULL;
2368
Bram Moolenaar05159a02005-02-26 23:04:13 +00002369 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2370 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002372 patc = alloc((unsigned)STRLEN(pat) + 11);
2373 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002375 STRCPY(patc, "\\(^\\|[\\/]\\)");
2376 STRCPY(patc + 11, pat + 1);
2377 }
2378 else
2379 patc = pat;
2380
2381 /*
2382 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002383 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002384 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002385 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002386 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002387 regmatch_T regmatch;
2388
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002389 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002390 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002391 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2392 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002393 {
2394 if (patc != pat)
2395 vim_free(patc);
2396 return FAIL;
2397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 /*
2400 * round == 1: Count the matches.
2401 * round == 2: Build the array to keep the matches.
2402 */
2403 for (round = 1; round <= 2; ++round)
2404 {
2405 count = 0;
2406 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2407 {
2408 if (!buf->b_p_bl) /* skip unlisted buffers */
2409 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002410 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 if (p != NULL)
2412 {
2413 if (round == 1)
2414 ++count;
2415 else
2416 {
2417 if (options & WILD_HOME_REPLACE)
2418 p = home_replace_save(buf, p);
2419 else
2420 p = vim_strsave(p);
2421 (*file)[count++] = p;
2422 }
2423 }
2424 }
2425 if (count == 0) /* no match found, break here */
2426 break;
2427 if (round == 1)
2428 {
2429 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2430 if (*file == NULL)
2431 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002432 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002433 if (patc != pat)
2434 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return FAIL;
2436 }
2437 }
2438 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002439 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 if (count) /* match(es) found, break here */
2441 break;
2442 }
2443
Bram Moolenaar05159a02005-02-26 23:04:13 +00002444 if (patc != pat)
2445 vim_free(patc);
2446
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 *num_file = count;
2448 return (count == 0 ? FAIL : OK);
2449}
2450
2451#endif /* FEAT_CMDL_COMPL */
2452
2453#ifdef HAVE_BUFLIST_MATCH
2454/*
2455 * Check for a match on the file name for buffer "buf" with regprog "prog".
2456 */
2457 static char_u *
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002458buflist_match(rmp, buf, ignore_case)
2459 regmatch_T *rmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 buf_T *buf;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002461 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462{
2463 char_u *match;
2464
2465 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002466 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002468 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 return match;
2471}
2472
2473/*
2474 * Try matching the regexp in "prog" with file name "name".
2475 * Return "name" when there is a match, NULL when not.
2476 */
2477 static char_u *
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002478fname_match(rmp, name, ignore_case)
2479 regmatch_T *rmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 char_u *name;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002481 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482{
2483 char_u *match = NULL;
2484 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485
2486 if (name != NULL)
2487 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002488 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002489 rmp->rm_ic = p_fic || ignore_case;
2490 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 match = name;
2492 else
2493 {
2494 /* Replace $(HOME) with '~' and try matching again. */
2495 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002496 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 match = name;
2498 vim_free(p);
2499 }
2500 }
2501
2502 return match;
2503}
2504#endif
2505
2506/*
2507 * find file in buffer list by number
2508 */
2509 buf_T *
2510buflist_findnr(nr)
2511 int nr;
2512{
2513 buf_T *buf;
2514
2515 if (nr == 0)
2516 nr = curwin->w_alt_fnum;
2517 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2518 if (buf->b_fnum == nr)
2519 return (buf);
2520 return NULL;
2521}
2522
2523/*
2524 * Get name of file 'n' in the buffer list.
2525 * When the file has no name an empty string is returned.
2526 * home_replace() is used to shorten the file name (used for marks).
2527 * Returns a pointer to allocated memory, of NULL when failed.
2528 */
2529 char_u *
2530buflist_nr2name(n, fullname, helptail)
2531 int n;
2532 int fullname;
2533 int helptail; /* for help buffers return tail only */
2534{
2535 buf_T *buf;
2536
2537 buf = buflist_findnr(n);
2538 if (buf == NULL)
2539 return NULL;
2540 return home_replace_save(helptail ? buf : NULL,
2541 fullname ? buf->b_ffname : buf->b_fname);
2542}
2543
2544/*
2545 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2546 * When "copy_options" is TRUE save the local window option values.
2547 * When "lnum" is 0 only do the options.
2548 */
2549 static void
2550buflist_setfpos(buf, win, lnum, col, copy_options)
2551 buf_T *buf;
2552 win_T *win;
2553 linenr_T lnum;
2554 colnr_T col;
2555 int copy_options;
2556{
2557 wininfo_T *wip;
2558
2559 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2560 if (wip->wi_win == win)
2561 break;
2562 if (wip == NULL)
2563 {
2564 /* allocate a new entry */
2565 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2566 if (wip == NULL)
2567 return;
2568 wip->wi_win = win;
2569 if (lnum == 0) /* set lnum even when it's 0 */
2570 lnum = 1;
2571 }
2572 else
2573 {
2574 /* remove the entry from the list */
2575 if (wip->wi_prev)
2576 wip->wi_prev->wi_next = wip->wi_next;
2577 else
2578 buf->b_wininfo = wip->wi_next;
2579 if (wip->wi_next)
2580 wip->wi_next->wi_prev = wip->wi_prev;
2581 if (copy_options && wip->wi_optset)
2582 {
2583 clear_winopt(&wip->wi_opt);
2584#ifdef FEAT_FOLDING
2585 deleteFoldRecurse(&wip->wi_folds);
2586#endif
2587 }
2588 }
2589 if (lnum != 0)
2590 {
2591 wip->wi_fpos.lnum = lnum;
2592 wip->wi_fpos.col = col;
2593 }
2594 if (copy_options)
2595 {
2596 /* Save the window-specific option values. */
2597 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2598#ifdef FEAT_FOLDING
2599 wip->wi_fold_manual = win->w_fold_manual;
2600 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2601#endif
2602 wip->wi_optset = TRUE;
2603 }
2604
2605 /* insert the entry in front of the list */
2606 wip->wi_next = buf->b_wininfo;
2607 buf->b_wininfo = wip;
2608 wip->wi_prev = NULL;
2609 if (wip->wi_next)
2610 wip->wi_next->wi_prev = wip;
2611
2612 return;
2613}
2614
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002615#ifdef FEAT_DIFF
2616static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2617
2618/*
2619 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2620 * page. That's because a diff is local to a tab page.
2621 */
2622 static int
2623wininfo_other_tab_diff(wip)
2624 wininfo_T *wip;
2625{
2626 win_T *wp;
2627
2628 if (wip->wi_opt.wo_diff)
2629 {
2630 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2631 /* return FALSE when it's a window in the current tab page, thus
2632 * the buffer was in diff mode here */
2633 if (wip->wi_win == wp)
2634 return FALSE;
2635 return TRUE;
2636 }
2637 return FALSE;
2638}
2639#endif
2640
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641/*
2642 * Find info for the current window in buffer "buf".
2643 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002644 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2645 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 * Returns NULL when there isn't any info.
2647 */
2648 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002649find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002651 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652{
2653 wininfo_T *wip;
2654
2655 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002656 if (wip->wi_win == curwin
2657#ifdef FEAT_DIFF
2658 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2659#endif
2660 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002662
2663 /* If no wininfo for curwin, use the first in the list (that doesn't have
2664 * 'diff' set and is in another tab page). */
2665 if (wip == NULL)
2666 {
2667#ifdef FEAT_DIFF
2668 if (skip_diff_buffer)
2669 {
2670 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2671 if (!wininfo_other_tab_diff(wip))
2672 break;
2673 }
2674 else
2675#endif
2676 wip = buf->b_wininfo;
2677 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 return wip;
2679}
2680
2681/*
2682 * Reset the local window options to the values last used in this window.
2683 * If the buffer wasn't used in this window before, use the values from
2684 * the most recently used window. If the values were never set, use the
2685 * global values for the window.
2686 */
2687 void
2688get_winopts(buf)
2689 buf_T *buf;
2690{
2691 wininfo_T *wip;
2692
2693 clear_winopt(&curwin->w_onebuf_opt);
2694#ifdef FEAT_FOLDING
2695 clearFolding(curwin);
2696#endif
2697
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002698 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 if (wip != NULL && wip->wi_optset)
2700 {
2701 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2702#ifdef FEAT_FOLDING
2703 curwin->w_fold_manual = wip->wi_fold_manual;
2704 curwin->w_foldinvalid = TRUE;
2705 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2706#endif
2707 }
2708 else
2709 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2710
2711#ifdef FEAT_FOLDING
2712 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2713 if (p_fdls >= 0)
2714 curwin->w_p_fdl = p_fdls;
2715#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002716#ifdef FEAT_SYN_HL
2717 check_colorcolumn(curwin);
2718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719}
2720
2721/*
2722 * Find the position (lnum and col) for the buffer 'buf' for the current
2723 * window.
2724 * Returns a pointer to no_position if no position is found.
2725 */
2726 pos_T *
2727buflist_findfpos(buf)
2728 buf_T *buf;
2729{
2730 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002731 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002733 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 if (wip != NULL)
2735 return &(wip->wi_fpos);
2736 else
2737 return &no_position;
2738}
2739
2740/*
2741 * Find the lnum for the buffer 'buf' for the current window.
2742 */
2743 linenr_T
2744buflist_findlnum(buf)
2745 buf_T *buf;
2746{
2747 return buflist_findfpos(buf)->lnum;
2748}
2749
2750#if defined(FEAT_LISTCMDS) || defined(PROTO)
2751/*
2752 * List all know file names (for :files and :buffers command).
2753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 void
2755buflist_list(eap)
2756 exarg_T *eap;
2757{
2758 buf_T *buf;
2759 int len;
2760 int i;
2761
2762 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2763 {
2764 /* skip unlisted buffers, unless ! was used */
Bram Moolenaard51cb702015-07-21 15:03:06 +02002765 if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
2766 || (vim_strchr(eap->arg, 'u') && buf->b_p_bl)
2767 || (vim_strchr(eap->arg, '+')
2768 && ((buf->b_flags & BF_READERR) || !bufIsChanged(buf)))
2769 || (vim_strchr(eap->arg, 'a')
2770 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows == 0))
2771 || (vim_strchr(eap->arg, 'h')
2772 && (buf->b_ml.ml_mfp == NULL || buf->b_nwindows != 0))
2773 || (vim_strchr(eap->arg, '-') && buf->b_p_ma)
2774 || (vim_strchr(eap->arg, '=') && !buf->b_p_ro)
2775 || (vim_strchr(eap->arg, 'x') && !(buf->b_flags & BF_READERR))
2776 || (vim_strchr(eap->arg, '%') && buf != curbuf)
2777 || (vim_strchr(eap->arg, '#')
2778 && (buf == curbuf || curwin->w_alt_fnum != buf->b_fnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 continue;
2780 msg_putchar('\n');
2781 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002782 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 else
2784 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2785
Bram Moolenaar50cde822005-06-05 21:54:54 +00002786 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 buf->b_fnum,
2788 buf->b_p_bl ? ' ' : 'u',
2789 buf == curbuf ? '%' :
2790 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2791 buf->b_ml.ml_mfp == NULL ? ' ' :
2792 (buf->b_nwindows == 0 ? 'h' : 'a'),
2793 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2794 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002795 : (bufIsChanged(buf) ? '+' : ' '),
2796 NameBuff);
Bram Moolenaar507edf62016-01-10 20:54:17 +01002797 if (len > IOSIZE - 20)
2798 len = IOSIZE - 20;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799
2800 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 i = 40 - vim_strsize(IObuff);
2802 do
2803 {
2804 IObuff[len++] = ' ';
2805 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002806 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2807 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002808 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809 msg_outtrans(IObuff);
2810 out_flush(); /* output one line at a time */
2811 ui_breakcheck();
2812 }
2813}
2814#endif
2815
2816/*
2817 * Get file name and line number for file 'fnum'.
2818 * Used by DoOneCmd() for translating '%' and '#'.
2819 * Used by insert_reg() and cmdline_paste() for '#' register.
2820 * Return FAIL if not found, OK for success.
2821 */
2822 int
2823buflist_name_nr(fnum, fname, lnum)
2824 int fnum;
2825 char_u **fname;
2826 linenr_T *lnum;
2827{
2828 buf_T *buf;
2829
2830 buf = buflist_findnr(fnum);
2831 if (buf == NULL || buf->b_fname == NULL)
2832 return FAIL;
2833
2834 *fname = buf->b_fname;
2835 *lnum = buflist_findlnum(buf);
2836
2837 return OK;
2838}
2839
2840/*
2841 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2842 * The file name with the full path is also remembered, for when :cd is used.
2843 * Returns FAIL for failure (file name already in use by other buffer)
2844 * OK otherwise.
2845 */
2846 int
2847setfname(buf, ffname, sfname, message)
2848 buf_T *buf;
2849 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002850 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002851{
Bram Moolenaar81695252004-12-29 20:58:21 +00002852 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853#ifdef UNIX
2854 struct stat st;
2855#endif
2856
2857 if (ffname == NULL || *ffname == NUL)
2858 {
2859 /* Removing the name. */
2860 vim_free(buf->b_ffname);
2861 vim_free(buf->b_sfname);
2862 buf->b_ffname = NULL;
2863 buf->b_sfname = NULL;
2864#ifdef UNIX
2865 st.st_dev = (dev_T)-1;
2866#endif
2867 }
2868 else
2869 {
2870 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2871 if (ffname == NULL) /* out of memory */
2872 return FAIL;
2873
2874 /*
2875 * if the file name is already used in another buffer:
2876 * - if the buffer is loaded, fail
2877 * - if the buffer is not loaded, delete it from the list
2878 */
2879#ifdef UNIX
2880 if (mch_stat((char *)ffname, &st) < 0)
2881 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002882#endif
2883 if (!(buf->b_flags & BF_DUMMY))
2884#ifdef UNIX
2885 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002887 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888#endif
2889 if (obuf != NULL && obuf != buf)
2890 {
2891 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2892 {
2893 if (message)
2894 EMSG(_("E95: Buffer with this name already exists"));
2895 vim_free(ffname);
2896 return FAIL;
2897 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002898 /* delete from the list */
2899 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002900 }
2901 sfname = vim_strsave(sfname);
2902 if (ffname == NULL || sfname == NULL)
2903 {
2904 vim_free(sfname);
2905 vim_free(ffname);
2906 return FAIL;
2907 }
2908#ifdef USE_FNAME_CASE
2909# ifdef USE_LONG_FNAME
2910 if (USE_LONG_FNAME)
2911# endif
2912 fname_case(sfname, 0); /* set correct case for short file name */
2913#endif
2914 vim_free(buf->b_ffname);
2915 vim_free(buf->b_sfname);
2916 buf->b_ffname = ffname;
2917 buf->b_sfname = sfname;
2918 }
2919 buf->b_fname = buf->b_sfname;
2920#ifdef UNIX
2921 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002922 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 else
2924 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002925 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926 buf->b_dev = st.st_dev;
2927 buf->b_ino = st.st_ino;
2928 }
2929#endif
2930
2931#ifndef SHORT_FNAME
2932 buf->b_shortname = FALSE;
2933#endif
2934
2935 buf_name_changed(buf);
2936 return OK;
2937}
2938
2939/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002940 * Crude way of changing the name of a buffer. Use with care!
2941 * The name should be relative to the current directory.
2942 */
2943 void
2944buf_set_name(fnum, name)
2945 int fnum;
2946 char_u *name;
2947{
2948 buf_T *buf;
2949
2950 buf = buflist_findnr(fnum);
2951 if (buf != NULL)
2952 {
2953 vim_free(buf->b_sfname);
2954 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002955 buf->b_ffname = vim_strsave(name);
2956 buf->b_sfname = NULL;
2957 /* Allocate ffname and expand into full path. Also resolves .lnk
2958 * files on Win32. */
2959 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002960 buf->b_fname = buf->b_sfname;
2961 }
2962}
2963
2964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 * Take care of what needs to be done when the name of buffer "buf" has
2966 * changed.
2967 */
2968 void
2969buf_name_changed(buf)
2970 buf_T *buf;
2971{
2972 /*
2973 * If the file name changed, also change the name of the swapfile
2974 */
2975 if (buf->b_ml.ml_mfp != NULL)
2976 ml_setname(buf);
2977
2978 if (curwin->w_buffer == buf)
2979 check_arg_idx(curwin); /* check file name for arg list */
2980#ifdef FEAT_TITLE
2981 maketitle(); /* set window title */
2982#endif
2983#ifdef FEAT_WINDOWS
2984 status_redraw_all(); /* status lines need to be redrawn */
2985#endif
2986 fmarks_check_names(buf); /* check named file marks */
2987 ml_timestamp(buf); /* reset timestamp */
2988}
2989
2990/*
2991 * set alternate file name for current window
2992 *
2993 * Used by do_one_cmd(), do_write() and do_ecmd().
2994 * Return the buffer.
2995 */
2996 buf_T *
2997setaltfname(ffname, sfname, lnum)
2998 char_u *ffname;
2999 char_u *sfname;
3000 linenr_T lnum;
3001{
3002 buf_T *buf;
3003
3004 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
3005 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00003006 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 curwin->w_alt_fnum = buf->b_fnum;
3008 return buf;
3009}
3010
3011/*
3012 * Get alternate file name for current window.
3013 * Return NULL if there isn't any, and give error message if requested.
3014 */
3015 char_u *
3016getaltfname(errmsg)
3017 int errmsg; /* give error message */
3018{
3019 char_u *fname;
3020 linenr_T dummy;
3021
3022 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3023 {
3024 if (errmsg)
3025 EMSG(_(e_noalt));
3026 return NULL;
3027 }
3028 return fname;
3029}
3030
3031/*
3032 * Add a file name to the buflist and return its number.
3033 * Uses same flags as buflist_new(), except BLN_DUMMY.
3034 *
3035 * used by qf_init(), main() and doarglist()
3036 */
3037 int
3038buflist_add(fname, flags)
3039 char_u *fname;
3040 int flags;
3041{
3042 buf_T *buf;
3043
3044 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3045 if (buf != NULL)
3046 return buf->b_fnum;
3047 return 0;
3048}
3049
3050#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3051/*
3052 * Adjust slashes in file names. Called after 'shellslash' was set.
3053 */
3054 void
3055buflist_slash_adjust()
3056{
3057 buf_T *bp;
3058
3059 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3060 {
3061 if (bp->b_ffname != NULL)
3062 slash_adjust(bp->b_ffname);
3063 if (bp->b_sfname != NULL)
3064 slash_adjust(bp->b_sfname);
3065 }
3066}
3067#endif
3068
3069/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003070 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 * Also save the local window option values.
3072 */
3073 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003074buflist_altfpos(win)
3075 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003077 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078}
3079
3080/*
3081 * Return TRUE if 'ffname' is not the same file as current file.
3082 * Fname must have a full path (expanded by mch_FullName()).
3083 */
3084 int
3085otherfile(ffname)
3086 char_u *ffname;
3087{
3088 return otherfile_buf(curbuf, ffname
3089#ifdef UNIX
3090 , NULL
3091#endif
3092 );
3093}
3094
3095 static int
3096otherfile_buf(buf, ffname
3097#ifdef UNIX
3098 , stp
3099#endif
3100 )
3101 buf_T *buf;
3102 char_u *ffname;
3103#ifdef UNIX
3104 struct stat *stp;
3105#endif
3106{
3107 /* no name is different */
3108 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3109 return TRUE;
3110 if (fnamecmp(ffname, buf->b_ffname) == 0)
3111 return FALSE;
3112#ifdef UNIX
3113 {
3114 struct stat st;
3115
3116 /* If no struct stat given, get it now */
3117 if (stp == NULL)
3118 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003119 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 st.st_dev = (dev_T)-1;
3121 stp = &st;
3122 }
3123 /* Use dev/ino to check if the files are the same, even when the names
3124 * are different (possible with links). Still need to compare the
3125 * name above, for when the file doesn't exist yet.
3126 * Problem: The dev/ino changes when a file is deleted (and created
3127 * again) and remains the same when renamed/moved. We don't want to
3128 * mch_stat() each buffer each time, that would be too slow. Get the
3129 * dev/ino again when they appear to match, but not when they appear
3130 * to be different: Could skip a buffer when it's actually the same
3131 * file. */
3132 if (buf_same_ino(buf, stp))
3133 {
3134 buf_setino(buf);
3135 if (buf_same_ino(buf, stp))
3136 return FALSE;
3137 }
3138 }
3139#endif
3140 return TRUE;
3141}
3142
3143#if defined(UNIX) || defined(PROTO)
3144/*
3145 * Set inode and device number for a buffer.
3146 * Must always be called when b_fname is changed!.
3147 */
3148 void
3149buf_setino(buf)
3150 buf_T *buf;
3151{
3152 struct stat st;
3153
3154 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3155 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003156 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 buf->b_dev = st.st_dev;
3158 buf->b_ino = st.st_ino;
3159 }
3160 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003161 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162}
3163
3164/*
3165 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3166 */
3167 static int
3168buf_same_ino(buf, stp)
3169 buf_T *buf;
3170 struct stat *stp;
3171{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003172 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 && stp->st_dev == buf->b_dev
3174 && stp->st_ino == buf->b_ino);
3175}
3176#endif
3177
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003178/*
3179 * Print info about the current buffer.
3180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 void
3182fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003183 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 int shorthelp;
3185 int dont_truncate;
3186{
3187 char_u *name;
3188 int n;
3189 char_u *p;
3190 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003191 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192
3193 buffer = alloc(IOSIZE);
3194 if (buffer == NULL)
3195 return;
3196
3197 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3198 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003199 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 p = buffer + STRLEN(buffer);
3201 }
3202 else
3203 p = buffer;
3204
3205 *p++ = '"';
3206 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003207 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 else
3209 {
3210 if (!fullname && curbuf->b_fname != NULL)
3211 name = curbuf->b_fname;
3212 else
3213 name = curbuf->b_ffname;
3214 home_replace(shorthelp ? curbuf : NULL, name, p,
3215 (int)(IOSIZE - (p - buffer)), TRUE);
3216 }
3217
Bram Moolenaara800b422010-06-27 01:15:55 +02003218 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 curbufIsChanged() ? (shortmess(SHM_MOD)
3220 ? " [+]" : _(" [Modified]")) : " ",
3221 (curbuf->b_flags & BF_NOTEDITED)
3222#ifdef FEAT_QUICKFIX
3223 && !bt_dontwrite(curbuf)
3224#endif
3225 ? _("[Not edited]") : "",
3226 (curbuf->b_flags & BF_NEW)
3227#ifdef FEAT_QUICKFIX
3228 && !bt_dontwrite(curbuf)
3229#endif
3230 ? _("[New file]") : "",
3231 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003232 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 : _("[readonly]")) : "",
3234 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3235 || curbuf->b_p_ro) ?
3236 " " : "");
3237 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3238 * causes an overflow, thus for large numbers divide instead. */
3239 if (curwin->w_cursor.lnum > 1000000L)
3240 n = (int)(((long)curwin->w_cursor.lnum) /
3241 ((long)curbuf->b_ml.ml_line_count / 100L));
3242 else
3243 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3244 (long)curbuf->b_ml.ml_line_count);
3245 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3246 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003247 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 }
3249#ifdef FEAT_CMDL_INFO
3250 else if (p_ru)
3251 {
3252 /* Current line and column are already on the screen -- webb */
3253 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003254 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003256 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 (long)curbuf->b_ml.ml_line_count, n);
3258 }
3259#endif
3260 else
3261 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003262 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 _("line %ld of %ld --%d%%-- col "),
3264 (long)curwin->w_cursor.lnum,
3265 (long)curbuf->b_ml.ml_line_count,
3266 n);
3267 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003268 len = STRLEN(buffer);
3269 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3271 }
3272
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003273 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274
3275 if (dont_truncate)
3276 {
3277 /* Temporarily set msg_scroll to avoid the message being truncated.
3278 * First call msg_start() to get the message in the right place. */
3279 msg_start();
3280 n = msg_scroll;
3281 msg_scroll = TRUE;
3282 msg(buffer);
3283 msg_scroll = n;
3284 }
3285 else
3286 {
3287 p = msg_trunc_attr(buffer, FALSE, 0);
3288 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 /* Need to repeat the message after redrawing when:
3290 * - When restart_edit is set (otherwise there will be a delay
3291 * before redrawing).
3292 * - When the screen was scrolled but there is no wait-return
3293 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003294 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 }
3296
3297 vim_free(buffer);
3298}
3299
3300 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003301col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003303 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003304 int col;
3305 int vcol;
3306{
3307 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003308 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003310 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311}
3312
3313#if defined(FEAT_TITLE) || defined(PROTO)
3314/*
3315 * put file name in title bar of window and in icon title
3316 */
3317
3318static char_u *lasttitle = NULL;
3319static char_u *lasticon = NULL;
3320
3321 void
3322maketitle()
3323{
3324 char_u *p;
3325 char_u *t_str = NULL;
3326 char_u *i_name;
3327 char_u *i_str = NULL;
3328 int maxlen = 0;
3329 int len;
3330 int mustset;
3331 char_u buf[IOSIZE];
3332 int off;
3333
3334 if (!redrawing())
3335 {
3336 /* Postpone updating the title when 'lazyredraw' is set. */
3337 need_maketitle = TRUE;
3338 return;
3339 }
3340
3341 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003342 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 return;
3344
3345 if (p_title)
3346 {
3347 if (p_titlelen > 0)
3348 {
3349 maxlen = p_titlelen * Columns / 100;
3350 if (maxlen < 10)
3351 maxlen = 10;
3352 }
3353
3354 t_str = buf;
3355 if (*p_titlestring != NUL)
3356 {
3357#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003358 if (stl_syntax & STL_IN_TITLE)
3359 {
3360 int use_sandbox = FALSE;
3361 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003362
3363# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003364 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003365# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003366 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003368 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003369 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003370 if (called_emsg)
3371 set_string_option_direct((char_u *)"titlestring", -1,
3372 (char_u *)"", OPT_FREE, SID_ERROR);
3373 called_emsg |= save_called_emsg;
3374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 else
3376#endif
3377 t_str = p_titlestring;
3378 }
3379 else
3380 {
3381 /* format: "fname + (path) (1 of 2) - VIM" */
3382
Bram Moolenaar2c666692012-09-05 13:30:40 +02003383#define SPACE_FOR_FNAME (IOSIZE - 100)
3384#define SPACE_FOR_DIR (IOSIZE - 20)
3385#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003387 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 else
3389 {
3390 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003391 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 }
3394
3395 switch (bufIsChanged(curbuf)
3396 + (curbuf->b_p_ro * 2)
3397 + (!curbuf->b_p_ma * 4))
3398 {
3399 case 1: STRCAT(buf, " +"); break;
3400 case 2: STRCAT(buf, " ="); break;
3401 case 3: STRCAT(buf, " =+"); break;
3402 case 4:
3403 case 6: STRCAT(buf, " -"); break;
3404 case 5:
3405 case 7: STRCAT(buf, " -+"); break;
3406 }
3407
3408 if (curbuf->b_fname != NULL)
3409 {
3410 /* Get path of file, replace home dir with ~ */
3411 off = (int)STRLEN(buf);
3412 buf[off++] = ' ';
3413 buf[off++] = '(';
3414 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003415 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416#ifdef BACKSLASH_IN_FILENAME
3417 /* avoid "c:/name" to be reduced to "c" */
3418 if (isalpha(buf[off]) && buf[off + 1] == ':')
3419 off += 2;
3420#endif
3421 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003422 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003425 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003426 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003429
Bram Moolenaar2c666692012-09-05 13:30:40 +02003430 /* Translate unprintable chars and concatenate. Keep some
3431 * room for the server name. When there is no room (very long
3432 * file name) use (...). */
3433 if (off < SPACE_FOR_DIR)
3434 {
3435 p = transstr(buf + off);
3436 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3437 vim_free(p);
3438 }
3439 else
3440 {
3441 vim_strncpy(buf + off, (char_u *)"...",
3442 (size_t)(SPACE_FOR_ARGNR - off));
3443 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 STRCAT(buf, ")");
3445 }
3446
Bram Moolenaar2c666692012-09-05 13:30:40 +02003447 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449#if defined(FEAT_CLIENTSERVER)
3450 if (serverName != NULL)
3451 {
3452 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003453 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 }
3455 else
3456#endif
3457 STRCAT(buf, " - VIM");
3458
3459 if (maxlen > 0)
3460 {
3461 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003462 if (vim_strsize(buf) > maxlen)
3463 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 }
3465 }
3466 }
3467 mustset = ti_change(t_str, &lasttitle);
3468
3469 if (p_icon)
3470 {
3471 i_str = buf;
3472 if (*p_iconstring != NUL)
3473 {
3474#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003475 if (stl_syntax & STL_IN_ICON)
3476 {
3477 int use_sandbox = FALSE;
3478 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003479
3480# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003481 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003482# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003483 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003485 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003486 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003487 if (called_emsg)
3488 set_string_option_direct((char_u *)"iconstring", -1,
3489 (char_u *)"", OPT_FREE, SID_ERROR);
3490 called_emsg |= save_called_emsg;
3491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 else
3493#endif
3494 i_str = p_iconstring;
3495 }
3496 else
3497 {
3498 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003499 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 else /* use file name only in icon */
3501 i_name = gettail(curbuf->b_ffname);
3502 *i_str = NUL;
3503 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003504 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505 if (len > 100)
3506 {
3507 len -= 100;
3508#ifdef FEAT_MBYTE
3509 if (has_mbyte)
3510 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3511#endif
3512 i_name += len;
3513 }
3514 STRCPY(i_str, i_name);
3515 trans_characters(i_str, IOSIZE);
3516 }
3517 }
3518
3519 mustset |= ti_change(i_str, &lasticon);
3520
3521 if (mustset)
3522 resettitle();
3523}
3524
3525/*
3526 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3527 * from "str" if it does.
3528 * Return TRUE when "*last" changed.
3529 */
3530 static int
3531ti_change(str, last)
3532 char_u *str;
3533 char_u **last;
3534{
3535 if ((str == NULL) != (*last == NULL)
3536 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3537 {
3538 vim_free(*last);
3539 if (str == NULL)
3540 *last = NULL;
3541 else
3542 *last = vim_strsave(str);
3543 return TRUE;
3544 }
3545 return FALSE;
3546}
3547
3548/*
3549 * Put current window title back (used after calling a shell)
3550 */
3551 void
3552resettitle()
3553{
3554 mch_settitle(lasttitle, lasticon);
3555}
Bram Moolenaarea408852005-06-25 22:49:46 +00003556
3557# if defined(EXITFREE) || defined(PROTO)
3558 void
3559free_titles()
3560{
3561 vim_free(lasttitle);
3562 vim_free(lasticon);
3563}
3564# endif
3565
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566#endif /* FEAT_TITLE */
3567
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003568#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003570 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 * Return length of string in screen cells.
3572 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003573 * Normally works for window "wp", except when working for 'tabline' then it
3574 * is "curwin".
3575 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 * Items are drawn interspersed with the text that surrounds it
3577 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3578 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3579 *
3580 * If maxwidth is not zero, the string will be filled at any middle marker
3581 * or truncated if too long, fillchar is used for all whitespace.
3582 */
3583 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003584build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3585 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003587 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 size_t outlen; /* length of out[] */
3589 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003590 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003591 int fillchar;
3592 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003593 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3594 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595{
3596 char_u *p;
3597 char_u *s;
3598 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003599 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003600#ifdef FEAT_EVAL
3601 win_T *o_curwin;
3602 buf_T *o_curbuf;
3603#endif
3604 int empty_line;
3605 colnr_T virtcol;
3606 long l;
3607 long n;
3608 int prevchar_isflag;
3609 int prevchar_isitem;
3610 int itemisflag;
3611 int fillable;
3612 char_u *str;
3613 long num;
3614 int width;
3615 int itemcnt;
3616 int curitem;
3617 int groupitem[STL_MAX_ITEM];
3618 int groupdepth;
3619 struct stl_item
3620 {
3621 char_u *start;
3622 int minwid;
3623 int maxwid;
3624 enum
3625 {
3626 Normal,
3627 Empty,
3628 Group,
3629 Middle,
3630 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003631 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 Trunc
3633 } type;
3634 } item[STL_MAX_ITEM];
3635 int minwid;
3636 int maxwid;
3637 int zeropad;
3638 char_u base;
3639 char_u opt;
3640#define TMPLEN 70
3641 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003642 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003643 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003644
3645#ifdef FEAT_EVAL
3646 /*
3647 * When the format starts with "%!" then evaluate it as an expression and
3648 * use the result as the actual format string.
3649 */
3650 if (fmt[0] == '%' && fmt[1] == '!')
3651 {
3652 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3653 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003654 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003655 }
3656#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
3658 if (fillchar == 0)
3659 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003660#ifdef FEAT_MBYTE
3661 /* Can't handle a multi-byte fill character yet. */
3662 else if (mb_char2len(fillchar) > 1)
3663 fillchar = '-';
3664#endif
3665
Bram Moolenaar567199b2013-04-24 16:52:36 +02003666 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3667 * p will become invalid when getting another buffer line. */
3668 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3669 empty_line = (*p == NUL);
3670
3671 /* Get the byte value now, in case we need it below. This is more
3672 * efficient than making a copy of the line. */
3673 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3674 byteval = 0;
3675 else
3676#ifdef FEAT_MBYTE
3677 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3678#else
3679 byteval = p[wp->w_cursor.col];
3680#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003681
3682 groupdepth = 0;
3683 p = out;
3684 curitem = 0;
3685 prevchar_isflag = TRUE;
3686 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003687 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003689 if (curitem == STL_MAX_ITEM)
3690 {
3691 /* There are too many items. Add the error code to the statusline
3692 * to give the user a hint about what went wrong. */
3693 if (p + 6 < out + outlen)
3694 {
3695 mch_memmove(p, " E541", (size_t)5);
3696 p += 5;
3697 }
3698 break;
3699 }
3700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 if (*s != NUL && *s != '%')
3702 prevchar_isflag = prevchar_isitem = FALSE;
3703
3704 /*
3705 * Handle up to the next '%' or the end.
3706 */
3707 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3708 *p++ = *s++;
3709 if (*s == NUL || p + 1 >= out + outlen)
3710 break;
3711
3712 /*
3713 * Handle one '%' item.
3714 */
3715 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003716 if (*s == NUL) /* ignore trailing % */
3717 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718 if (*s == '%')
3719 {
3720 if (p + 1 >= out + outlen)
3721 break;
3722 *p++ = *s++;
3723 prevchar_isflag = prevchar_isitem = FALSE;
3724 continue;
3725 }
3726 if (*s == STL_MIDDLEMARK)
3727 {
3728 s++;
3729 if (groupdepth > 0)
3730 continue;
3731 item[curitem].type = Middle;
3732 item[curitem++].start = p;
3733 continue;
3734 }
3735 if (*s == STL_TRUNCMARK)
3736 {
3737 s++;
3738 item[curitem].type = Trunc;
3739 item[curitem++].start = p;
3740 continue;
3741 }
3742 if (*s == ')')
3743 {
3744 s++;
3745 if (groupdepth < 1)
3746 continue;
3747 groupdepth--;
3748
3749 t = item[groupitem[groupdepth]].start;
3750 *p = NUL;
3751 l = vim_strsize(t);
3752 if (curitem > groupitem[groupdepth] + 1
3753 && item[groupitem[groupdepth]].minwid == 0)
3754 {
3755 /* remove group if all items are empty */
3756 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3757 if (item[n].type == Normal)
3758 break;
3759 if (n == curitem)
3760 {
3761 p = t;
3762 l = 0;
3763 }
3764 }
3765 if (l > item[groupitem[groupdepth]].maxwid)
3766 {
3767 /* truncate, remove n bytes of text at the start */
3768#ifdef FEAT_MBYTE
3769 if (has_mbyte)
3770 {
3771 /* Find the first character that should be included. */
3772 n = 0;
3773 while (l >= item[groupitem[groupdepth]].maxwid)
3774 {
3775 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003776 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 }
3778 }
3779 else
3780#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003781 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
3783 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003784 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 p = p - n + 1;
3786#ifdef FEAT_MBYTE
3787 /* Fill up space left over by half a double-wide char. */
3788 while (++l < item[groupitem[groupdepth]].minwid)
3789 *p++ = fillchar;
3790#endif
3791
3792 /* correct the start of the items for the truncation */
3793 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3794 {
3795 item[l].start -= n;
3796 if (item[l].start < t)
3797 item[l].start = t;
3798 }
3799 }
3800 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3801 {
3802 /* fill */
3803 n = item[groupitem[groupdepth]].minwid;
3804 if (n < 0)
3805 {
3806 /* fill by appending characters */
3807 n = 0 - n;
3808 while (l++ < n && p + 1 < out + outlen)
3809 *p++ = fillchar;
3810 }
3811 else
3812 {
3813 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003814 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 l = n - l;
3816 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003817 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 p += l;
3819 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3820 item[n].start += l;
3821 for ( ; l > 0; l--)
3822 *t++ = fillchar;
3823 }
3824 }
3825 continue;
3826 }
3827 minwid = 0;
3828 maxwid = 9999;
3829 zeropad = FALSE;
3830 l = 1;
3831 if (*s == '0')
3832 {
3833 s++;
3834 zeropad = TRUE;
3835 }
3836 if (*s == '-')
3837 {
3838 s++;
3839 l = -1;
3840 }
3841 if (VIM_ISDIGIT(*s))
3842 {
3843 minwid = (int)getdigits(&s);
3844 if (minwid < 0) /* overflow */
3845 minwid = 0;
3846 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003847 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
3849 item[curitem].type = Highlight;
3850 item[curitem].start = p;
3851 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3852 s++;
3853 curitem++;
3854 continue;
3855 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003856 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3857 {
3858 if (*s == STL_TABCLOSENR)
3859 {
3860 if (minwid == 0)
3861 {
3862 /* %X ends the close label, go back to the previously
3863 * define tab label nr. */
3864 for (n = curitem - 1; n >= 0; --n)
3865 if (item[n].type == TabPage && item[n].minwid >= 0)
3866 {
3867 minwid = item[n].minwid;
3868 break;
3869 }
3870 }
3871 else
3872 /* close nrs are stored as negative values */
3873 minwid = - minwid;
3874 }
3875 item[curitem].type = TabPage;
3876 item[curitem].start = p;
3877 item[curitem].minwid = minwid;
3878 s++;
3879 curitem++;
3880 continue;
3881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 if (*s == '.')
3883 {
3884 s++;
3885 if (VIM_ISDIGIT(*s))
3886 {
3887 maxwid = (int)getdigits(&s);
3888 if (maxwid <= 0) /* overflow */
3889 maxwid = 50;
3890 }
3891 }
3892 minwid = (minwid > 50 ? 50 : minwid) * l;
3893 if (*s == '(')
3894 {
3895 groupitem[groupdepth++] = curitem;
3896 item[curitem].type = Group;
3897 item[curitem].start = p;
3898 item[curitem].minwid = minwid;
3899 item[curitem].maxwid = maxwid;
3900 s++;
3901 curitem++;
3902 continue;
3903 }
3904 if (vim_strchr(STL_ALL, *s) == NULL)
3905 {
3906 s++;
3907 continue;
3908 }
3909 opt = *s++;
3910
3911 /* OK - now for the real work */
3912 base = 'D';
3913 itemisflag = FALSE;
3914 fillable = TRUE;
3915 num = -1;
3916 str = NULL;
3917 switch (opt)
3918 {
3919 case STL_FILEPATH:
3920 case STL_FULLPATH:
3921 case STL_FILENAME:
3922 fillable = FALSE; /* don't change ' ' to fillchar */
3923 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003924 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 else
3926 {
3927 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003928 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3930 }
3931 trans_characters(NameBuff, MAXPATHL);
3932 if (opt != STL_FILENAME)
3933 str = NameBuff;
3934 else
3935 str = gettail(NameBuff);
3936 break;
3937
3938 case STL_VIM_EXPR: /* '{' */
3939 itemisflag = TRUE;
3940 t = p;
3941 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3942 *p++ = *s++;
3943 if (*s != '}') /* missing '}' or out of space */
3944 break;
3945 s++;
3946 *p = 0;
3947 p = t;
3948
3949#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003950 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3952
3953 o_curbuf = curbuf;
3954 o_curwin = curwin;
3955 curwin = wp;
3956 curbuf = wp->w_buffer;
3957
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003958 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959
3960 curwin = o_curwin;
3961 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003962 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963
3964 if (str != NULL && *str != 0)
3965 {
3966 if (*skipdigits(str) == NUL)
3967 {
3968 num = atoi((char *)str);
3969 vim_free(str);
3970 str = NULL;
3971 itemisflag = FALSE;
3972 }
3973 }
3974#endif
3975 break;
3976
3977 case STL_LINE:
3978 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3979 ? 0L : (long)(wp->w_cursor.lnum);
3980 break;
3981
3982 case STL_NUMLINES:
3983 num = wp->w_buffer->b_ml.ml_line_count;
3984 break;
3985
3986 case STL_COLUMN:
3987 num = !(State & INSERT) && empty_line
3988 ? 0 : (int)wp->w_cursor.col + 1;
3989 break;
3990
3991 case STL_VIRTCOL:
3992 case STL_VIRTCOL_ALT:
3993 /* In list mode virtcol needs to be recomputed */
3994 virtcol = wp->w_virtcol;
3995 if (wp->w_p_list && lcs_tab1 == NUL)
3996 {
3997 wp->w_p_list = FALSE;
3998 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3999 wp->w_p_list = TRUE;
4000 }
4001 ++virtcol;
4002 /* Don't display %V if it's the same as %c. */
4003 if (opt == STL_VIRTCOL_ALT
4004 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
4005 ? 0 : (int)wp->w_cursor.col + 1)))
4006 break;
4007 num = (long)virtcol;
4008 break;
4009
4010 case STL_PERCENTAGE:
4011 num = (int)(((long)wp->w_cursor.lnum * 100L) /
4012 (long)wp->w_buffer->b_ml.ml_line_count);
4013 break;
4014
4015 case STL_ALTPERCENT:
4016 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004017 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 break;
4019
4020 case STL_ARGLISTSTAT:
4021 fillable = FALSE;
4022 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004023 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 str = tmp;
4025 break;
4026
4027 case STL_KEYMAP:
4028 fillable = FALSE;
4029 if (get_keymap_str(wp, tmp, TMPLEN))
4030 str = tmp;
4031 break;
4032 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004033#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004034 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004035#else
4036 num = 0;
4037#endif
4038 break;
4039
4040 case STL_BUFNO:
4041 num = wp->w_buffer->b_fnum;
4042 break;
4043
4044 case STL_OFFSET_X:
4045 base = 'X';
4046 case STL_OFFSET:
4047#ifdef FEAT_BYTEOFF
4048 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4049 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4050 0L : l + 1 + (!(State & INSERT) && empty_line ?
4051 0 : (int)wp->w_cursor.col);
4052#endif
4053 break;
4054
4055 case STL_BYTEVAL_X:
4056 base = 'X';
4057 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004058 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 if (num == NL)
4060 num = 0;
4061 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4062 num = NL;
4063 break;
4064
4065 case STL_ROFLAG:
4066 case STL_ROFLAG_ALT:
4067 itemisflag = TRUE;
4068 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004069 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 break;
4071
4072 case STL_HELPFLAG:
4073 case STL_HELPFLAG_ALT:
4074 itemisflag = TRUE;
4075 if (wp->w_buffer->b_help)
4076 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004077 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 break;
4079
4080#ifdef FEAT_AUTOCMD
4081 case STL_FILETYPE:
4082 if (*wp->w_buffer->b_p_ft != NUL
4083 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4084 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004085 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4086 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 str = tmp;
4088 }
4089 break;
4090
4091 case STL_FILETYPE_ALT:
4092 itemisflag = TRUE;
4093 if (*wp->w_buffer->b_p_ft != NUL
4094 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4095 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004096 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4097 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 for (t = tmp; *t != 0; t++)
4099 *t = TOUPPER_LOC(*t);
4100 str = tmp;
4101 }
4102 break;
4103#endif
4104
4105#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4106 case STL_PREVIEWFLAG:
4107 case STL_PREVIEWFLAG_ALT:
4108 itemisflag = TRUE;
4109 if (wp->w_p_pvw)
4110 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4111 : _("[Preview]"));
4112 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004113
4114 case STL_QUICKFIX:
4115 if (bt_quickfix(wp->w_buffer))
4116 str = (char_u *)(wp->w_llist_ref
4117 ? _(msg_loclist)
4118 : _(msg_qflist));
4119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120#endif
4121
4122 case STL_MODIFIED:
4123 case STL_MODIFIED_ALT:
4124 itemisflag = TRUE;
4125 switch ((opt == STL_MODIFIED_ALT)
4126 + bufIsChanged(wp->w_buffer) * 2
4127 + (!wp->w_buffer->b_p_ma) * 4)
4128 {
4129 case 2: str = (char_u *)"[+]"; break;
4130 case 3: str = (char_u *)",+"; break;
4131 case 4: str = (char_u *)"[-]"; break;
4132 case 5: str = (char_u *)",-"; break;
4133 case 6: str = (char_u *)"[+-]"; break;
4134 case 7: str = (char_u *)",+-"; break;
4135 }
4136 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004137
4138 case STL_HIGHLIGHT:
4139 t = s;
4140 while (*s != '#' && *s != NUL)
4141 ++s;
4142 if (*s == '#')
4143 {
4144 item[curitem].type = Highlight;
4145 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004146 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004147 curitem++;
4148 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004149 if (*s != NUL)
4150 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004151 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153
4154 item[curitem].start = p;
4155 item[curitem].type = Normal;
4156 if (str != NULL && *str)
4157 {
4158 t = str;
4159 if (itemisflag)
4160 {
4161 if ((t[0] && t[1])
4162 && ((!prevchar_isitem && *t == ',')
4163 || (prevchar_isflag && *t == ' ')))
4164 t++;
4165 prevchar_isflag = TRUE;
4166 }
4167 l = vim_strsize(t);
4168 if (l > 0)
4169 prevchar_isitem = TRUE;
4170 if (l > maxwid)
4171 {
4172 while (l >= maxwid)
4173#ifdef FEAT_MBYTE
4174 if (has_mbyte)
4175 {
4176 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004177 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 else
4180#endif
4181 l -= byte2cells(*t++);
4182 if (p + 1 >= out + outlen)
4183 break;
4184 *p++ = '<';
4185 }
4186 if (minwid > 0)
4187 {
4188 for (; l < minwid && p + 1 < out + outlen; l++)
4189 {
4190 /* Don't put a "-" in front of a digit. */
4191 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4192 *p++ = ' ';
4193 else
4194 *p++ = fillchar;
4195 }
4196 minwid = 0;
4197 }
4198 else
4199 minwid *= -1;
4200 while (*t && p + 1 < out + outlen)
4201 {
4202 *p++ = *t++;
4203 /* Change a space by fillchar, unless fillchar is '-' and a
4204 * digit follows. */
4205 if (fillable && p[-1] == ' '
4206 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4207 p[-1] = fillchar;
4208 }
4209 for (; l < minwid && p + 1 < out + outlen; l++)
4210 *p++ = fillchar;
4211 }
4212 else if (num >= 0)
4213 {
4214 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4215 char_u nstr[20];
4216
4217 if (p + 20 >= out + outlen)
4218 break; /* not sufficient space */
4219 prevchar_isitem = TRUE;
4220 t = nstr;
4221 if (opt == STL_VIRTCOL_ALT)
4222 {
4223 *t++ = '-';
4224 minwid--;
4225 }
4226 *t++ = '%';
4227 if (zeropad)
4228 *t++ = '0';
4229 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004230 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 *t = 0;
4232
4233 for (n = num, l = 1; n >= nbase; n /= nbase)
4234 l++;
4235 if (opt == STL_VIRTCOL_ALT)
4236 l++;
4237 if (l > maxwid)
4238 {
4239 l += 2;
4240 n = l - maxwid;
4241 while (l-- > maxwid)
4242 num /= nbase;
4243 *t++ = '>';
4244 *t++ = '%';
4245 *t = t[-3];
4246 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004247 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4248 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 }
4250 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004251 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4252 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 p += STRLEN(p);
4254 }
4255 else
4256 item[curitem].type = Empty;
4257
4258 if (opt == STL_VIM_EXPR)
4259 vim_free(str);
4260
4261 if (num >= 0 || (!itemisflag && str && *str))
4262 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4263 curitem++;
4264 }
4265 *p = NUL;
4266 itemcnt = curitem;
4267
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004268#ifdef FEAT_EVAL
4269 if (usefmt != fmt)
4270 vim_free(usefmt);
4271#endif
4272
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 width = vim_strsize(out);
4274 if (maxwidth > 0 && width > maxwidth)
4275 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004276 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 l = 0;
4278 if (itemcnt == 0)
4279 s = out;
4280 else
4281 {
4282 for ( ; l < itemcnt; l++)
4283 if (item[l].type == Trunc)
4284 {
4285 /* Truncate at %< item. */
4286 s = item[l].start;
4287 break;
4288 }
4289 if (l == itemcnt)
4290 {
4291 /* No %< item, truncate first item. */
4292 s = item[0].start;
4293 l = 0;
4294 }
4295 }
4296
4297 if (width - vim_strsize(s) >= maxwidth)
4298 {
4299 /* Truncation mark is beyond max length */
4300#ifdef FEAT_MBYTE
4301 if (has_mbyte)
4302 {
4303 s = out;
4304 width = 0;
4305 for (;;)
4306 {
4307 width += ptr2cells(s);
4308 if (width >= maxwidth)
4309 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004310 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 }
4312 /* Fill up for half a double-wide character. */
4313 while (++width < maxwidth)
4314 *s++ = fillchar;
4315 }
4316 else
4317#endif
4318 s = out + maxwidth - 1;
4319 for (l = 0; l < itemcnt; l++)
4320 if (item[l].start > s)
4321 break;
4322 itemcnt = l;
4323 *s++ = '>';
4324 *s = 0;
4325 }
4326 else
4327 {
4328#ifdef FEAT_MBYTE
4329 if (has_mbyte)
4330 {
4331 n = 0;
4332 while (width >= maxwidth)
4333 {
4334 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004335 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337 }
4338 else
4339#endif
4340 n = width - maxwidth + 1;
4341 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004342 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 *s = '<';
4344
4345 /* Fill up for half a double-wide character. */
4346 while (++width < maxwidth)
4347 {
4348 s = s + STRLEN(s);
4349 *s++ = fillchar;
4350 *s = NUL;
4351 }
4352
4353 --n; /* count the '<' */
4354 for (; l < itemcnt; l++)
4355 {
4356 if (item[l].start - n >= s)
4357 item[l].start -= n;
4358 else
4359 item[l].start = s;
4360 }
4361 }
4362 width = maxwidth;
4363 }
4364 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4365 {
4366 /* Apply STL_MIDDLE if any */
4367 for (l = 0; l < itemcnt; l++)
4368 if (item[l].type == Middle)
4369 break;
4370 if (l < itemcnt)
4371 {
4372 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004373 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 for (s = item[l].start; s < p; s++)
4375 *s = fillchar;
4376 for (l++; l < itemcnt; l++)
4377 item[l].start += maxwidth - width;
4378 width = maxwidth;
4379 }
4380 }
4381
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004382 /* Store the info about highlighting. */
4383 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004385 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 for (l = 0; l < itemcnt; l++)
4387 {
4388 if (item[l].type == Highlight)
4389 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004390 sp->start = item[l].start;
4391 sp->userhl = item[l].minwid;
4392 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
4394 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004395 sp->start = NULL;
4396 sp->userhl = 0;
4397 }
4398
4399 /* Store the info about tab pages labels. */
4400 if (tabtab != NULL)
4401 {
4402 sp = tabtab;
4403 for (l = 0; l < itemcnt; l++)
4404 {
4405 if (item[l].type == TabPage)
4406 {
4407 sp->start = item[l].start;
4408 sp->userhl = item[l].minwid;
4409 sp++;
4410 }
4411 }
4412 sp->start = NULL;
4413 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 }
4415
4416 return width;
4417}
4418#endif /* FEAT_STL_OPT */
4419
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004420#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4421 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004423 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4424 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 */
4426 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004427get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004429 char_u *buf;
4430 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431{
4432 long above; /* number of lines above window */
4433 long below; /* number of lines below window */
4434
Bram Moolenaar0027c212015-01-07 13:31:52 +01004435 if (buflen < 3) /* need at least 3 chars for writing */
4436 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 above = wp->w_topline - 1;
4438#ifdef FEAT_DIFF
4439 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
Bram Moolenaar29bc9db2015-08-04 17:43:25 +02004440 if (wp->w_topline == 1 && wp->w_topfill >= 1)
4441 above = 0; /* All buffer lines are displayed and there is an
4442 * indication of filler lines, that can be considered
4443 * seeing all lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444#endif
4445 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4446 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004447 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4448 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004450 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004452 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 ? (int)(above / ((above + below) / 100L))
4454 : (int)(above * 100L / (above + below)));
4455}
4456#endif
4457
4458/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004459 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 * Return TRUE if it was appended.
4461 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004462 static int
4463append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 win_T *wp;
4465 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004466 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468{
4469 char_u *p;
4470
4471 if (ARGCOUNT <= 1) /* nothing to do */
4472 return FALSE;
4473
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004474 p = buf + STRLEN(buf); /* go to the end of the buffer */
4475 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 return FALSE;
4477 *p++ = ' ';
4478 *p++ = '(';
4479 if (add_file)
4480 {
4481 STRCPY(p, "file ");
4482 p += 5;
4483 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004484 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4485 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4487 return TRUE;
4488}
4489
4490/*
4491 * If fname is not a full path, make it a full path.
4492 * Returns pointer to allocated memory (NULL for failure).
4493 */
4494 char_u *
4495fix_fname(fname)
4496 char_u *fname;
4497{
4498 /*
4499 * Force expanding the path always for Unix, because symbolic links may
4500 * mess up the full path name, even though it starts with a '/'.
4501 * Also expand when there is ".." in the file name, try to remove it,
4502 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004503 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 * For MS-Windows also expand names like "longna~1" to "longname".
4505 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004506#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 return FullName_save(fname, TRUE);
4508#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004509 if (!vim_isAbsName(fname)
4510 || strstr((char *)fname, "..") != NULL
4511 || strstr((char *)fname, "//") != NULL
4512# ifdef BACKSLASH_IN_FILENAME
4513 || strstr((char *)fname, "\\\\") != NULL
4514# endif
4515# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004517# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 )
4519 return FullName_save(fname, FALSE);
4520
4521 fname = vim_strsave(fname);
4522
Bram Moolenaar9b942202007-10-03 12:31:33 +00004523# ifdef USE_FNAME_CASE
4524# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004526# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 {
4528 if (fname != NULL)
4529 fname_case(fname, 0); /* set correct case for file name */
4530 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004531# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 return fname;
4534#endif
4535}
4536
4537/*
4538 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4539 * "ffname" becomes a pointer to allocated memory (or NULL).
4540 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 void
4542fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004543 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 char_u **ffname;
4545 char_u **sfname;
4546{
4547 if (*ffname == NULL) /* if no file name given, nothing to do */
4548 return;
4549 if (*sfname == NULL) /* if no short file name given, use ffname */
4550 *sfname = *ffname;
4551 *ffname = fix_fname(*ffname); /* expand to full path */
4552
4553#ifdef FEAT_SHORTCUT
4554 if (!buf->b_p_bin)
4555 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004556 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557
4558 /* If the file name is a shortcut file, use the file it links to. */
4559 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004560 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 {
4562 vim_free(*ffname);
4563 *ffname = rfname;
4564 *sfname = rfname;
4565 }
4566 }
4567#endif
4568}
4569
4570/*
4571 * Get the file name for an argument list entry.
4572 */
4573 char_u *
4574alist_name(aep)
4575 aentry_T *aep;
4576{
4577 buf_T *bp;
4578
4579 /* Use the name from the associated buffer if it exists. */
4580 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004581 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 return aep->ae_fname;
4583 return bp->b_fname;
4584}
4585
4586#if defined(FEAT_WINDOWS) || defined(PROTO)
4587/*
4588 * do_arg_all(): Open up to 'count' windows, one for each argument.
4589 */
4590 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004591do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 int count;
4593 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004594 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595{
4596 int i;
4597 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004598 char_u *opened; /* Array of weight for which args are open:
4599 * 0: not opened
4600 * 1: opened in other tab
4601 * 2: opened in curtab
4602 * 3: opened in curtab and curwin
4603 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004604 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 int use_firstwin = FALSE; /* use first window for arglist */
4606 int split_ret = OK;
4607 int p_ea_save;
4608 alist_T *alist; /* argument list to be used */
4609 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004610 tabpage_T *tpnext;
4611 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004612 win_T *old_curwin, *last_curwin;
4613 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004614 win_T *new_curwin = NULL;
4615 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616
4617 if (ARGCOUNT <= 0)
4618 {
4619 /* Don't give an error message. We don't want it when the ":all"
4620 * command is in the .vimrc. */
4621 return;
4622 }
4623 setpcmark();
4624
4625 opened_len = ARGCOUNT;
4626 opened = alloc_clear((unsigned)opened_len);
4627 if (opened == NULL)
4628 return;
4629
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004630 /* Autocommands may do anything to the argument list. Make sure it's not
4631 * freed while we are working here by "locking" it. We still have to
4632 * watch out for its size to be changed. */
4633 alist = curwin->w_alist;
4634 ++alist->al_refcount;
4635
4636 old_curwin = curwin;
4637 old_curtab = curtab;
4638
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639#ifdef FEAT_GUI
4640 need_mouse_correct = TRUE;
4641#endif
4642
4643 /*
4644 * Try closing all windows that are not in the argument list.
4645 * Also close windows that are not full width;
4646 * When 'hidden' or "forceit" set the buffer becomes hidden.
4647 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004648 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004650 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004651 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004652 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004654 tpnext = curtab->tp_next;
4655 for (wp = firstwin; wp != NULL; wp = wpnext)
4656 {
4657 wpnext = wp->w_next;
4658 buf = wp->w_buffer;
4659 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004660 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004662 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004664 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004665 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004666 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004668 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004669 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004671 if (i < alist->al_ga.ga_len
4672 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4673 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4674 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004676 int weight = 1;
4677
4678 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004679 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004680 ++weight;
4681 if (old_curwin == wp)
4682 ++weight;
4683 }
4684
4685 if (weight > (int)opened[i])
4686 {
4687 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004688 if (i == 0)
4689 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004690 if (new_curwin != NULL)
4691 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004692 new_curwin = wp;
4693 new_curtab = curtab;
4694 }
4695 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004696 else if (keep_tabs)
4697 i = opened_len;
4698
4699 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004700 {
4701 /* Use the current argument list for all windows
4702 * containing a file from it. */
4703 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004704 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004705 ++wp->w_alist->al_refcount;
4706 }
4707 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 }
4710 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004711 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004713 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004715 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004716 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004718 /* If the buffer was changed, and we would like to hide it,
4719 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004720 if (!P_HID(buf) && buf->b_nwindows <= 1
4721 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004723 (void)autowrite(buf, FALSE);
4724#ifdef FEAT_AUTOCMD
4725 /* check if autocommands removed the window */
4726 if (!win_valid(wp) || !buf_valid(buf))
4727 {
4728 wpnext = firstwin; /* start all over... */
4729 continue;
4730 }
4731#endif
4732 }
4733#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004734 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004735 if (firstwin == lastwin
4736 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004737#endif
4738 use_firstwin = TRUE;
4739#ifdef FEAT_WINDOWS
4740 else
4741 {
4742 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4743# ifdef FEAT_AUTOCMD
4744 /* check if autocommands removed the next window */
4745 if (!win_valid(wpnext))
4746 wpnext = firstwin; /* start all over... */
4747# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 }
4749#endif
4750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
4752 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004753
4754 /* Without the ":tab" modifier only do the current tab page. */
4755 if (had_tab == 0 || tpnext == NULL)
4756 break;
4757
4758# ifdef FEAT_AUTOCMD
4759 /* check if autocommands removed the next tab page */
4760 if (!valid_tabpage(tpnext))
4761 tpnext = first_tabpage; /* start all over...*/
4762# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004763 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765
4766 /*
4767 * Open a window for files in the argument list that don't have one.
4768 * ARGCOUNT may change while doing this, because of autocommands.
4769 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004770 if (count > opened_len || count <= 0)
4771 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772
4773#ifdef FEAT_AUTOCMD
4774 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4775 ++autocmd_no_enter;
4776 ++autocmd_no_leave;
4777#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004778 last_curwin = curwin;
4779 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004781#ifdef FEAT_WINDOWS
4782 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4783 * leaving an empty tab page when executed locally. */
4784 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4785 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4786 use_firstwin = TRUE;
4787#endif
4788
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004789 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 {
4791 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4792 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004793 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 {
4795 /* Move the already present window to below the current window */
4796 if (curwin->w_arg_idx != i)
4797 {
4798 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4799 {
4800 if (wpnext->w_arg_idx == i)
4801 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004802 if (keep_tabs)
4803 {
4804 new_curwin = wpnext;
4805 new_curtab = curtab;
4806 }
4807 else
4808 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 break;
4810 }
4811 }
4812 }
4813 }
4814 else if (split_ret == OK)
4815 {
4816 if (!use_firstwin) /* split current window */
4817 {
4818 p_ea_save = p_ea;
4819 p_ea = TRUE; /* use space from all windows */
4820 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4821 p_ea = p_ea_save;
4822 if (split_ret == FAIL)
4823 continue;
4824 }
4825#ifdef FEAT_AUTOCMD
4826 else /* first window: do autocmd for leaving this buffer */
4827 --autocmd_no_leave;
4828#endif
4829
4830 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004831 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 */
4833 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004834 if (i == 0)
4835 {
4836 new_curwin = curwin;
4837 new_curtab = curtab;
4838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4840 ECMD_ONE,
4841 ((P_HID(curwin->w_buffer)
4842 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004843 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844#ifdef FEAT_AUTOCMD
4845 if (use_firstwin)
4846 ++autocmd_no_leave;
4847#endif
4848 use_firstwin = FALSE;
4849 }
4850 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004851
4852 /* When ":tab" was used open a new tab for a new window repeatedly. */
4853 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4854 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856
4857 /* Remove the "lock" on the argument list. */
4858 alist_unlink(alist);
4859
4860#ifdef FEAT_AUTOCMD
4861 --autocmd_no_enter;
4862#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004863 /* restore last referenced tabpage's curwin */
4864 if (last_curtab != new_curtab)
4865 {
4866 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004867 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004868 if (win_valid(last_curwin))
4869 win_enter(last_curwin, FALSE);
4870 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004871 /* to window with first arg */
4872 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004873 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004874 if (win_valid(new_curwin))
4875 win_enter(new_curwin, FALSE);
4876
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877#ifdef FEAT_AUTOCMD
4878 --autocmd_no_leave;
4879#endif
4880 vim_free(opened);
4881}
4882
4883# if defined(FEAT_LISTCMDS) || defined(PROTO)
4884/*
4885 * Open a window for a number of buffers.
4886 */
4887 void
4888ex_buffer_all(eap)
4889 exarg_T *eap;
4890{
4891 buf_T *buf;
4892 win_T *wp, *wpnext;
4893 int split_ret = OK;
4894 int p_ea_save;
4895 int open_wins = 0;
4896 int r;
4897 int count; /* Maximum number of windows to open. */
4898 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004899#ifdef FEAT_WINDOWS
4900 int had_tab = cmdmod.tab;
4901 tabpage_T *tpnext;
4902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903
4904 if (eap->addr_count == 0) /* make as many windows as possible */
4905 count = 9999;
4906 else
4907 count = eap->line2; /* make as many windows as specified */
4908 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4909 all = FALSE;
4910 else
4911 all = TRUE;
4912
4913 setpcmark();
4914
4915#ifdef FEAT_GUI
4916 need_mouse_correct = TRUE;
4917#endif
4918
4919 /*
4920 * Close superfluous windows (two windows for the same buffer).
4921 * Also close windows that are not full-width.
4922 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004923#ifdef FEAT_WINDOWS
4924 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004925 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004926 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929 tpnext = curtab->tp_next;
4930 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004932 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004933 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004934#ifdef FEAT_VERTSPLIT
4935 || ((cmdmod.split & WSP_VERT)
4936 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004937 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004938 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004940#ifdef FEAT_WINDOWS
4941 || (had_tab > 0 && wp != firstwin)
4942#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004943 ) && firstwin != lastwin
4944#ifdef FEAT_AUTOCMD
4945 && !(wp->w_closing || wp->w_buffer->b_closing)
4946#endif
4947 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004948 {
4949 win_close(wp, FALSE);
4950#ifdef FEAT_AUTOCMD
4951 wpnext = firstwin; /* just in case an autocommand does
4952 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004953 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004954 open_wins = 0;
4955#endif
4956 }
4957 else
4958 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004960
4961#ifdef FEAT_WINDOWS
4962 /* Without the ":tab" modifier only do the current tab page. */
4963 if (had_tab == 0 || tpnext == NULL)
4964 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004965 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968
4969 /*
4970 * Go through the buffer list. When a buffer doesn't have a window yet,
4971 * open one. Otherwise move the window to the right position.
4972 * Watch out for autocommands that delete buffers or windows!
4973 */
4974#ifdef FEAT_AUTOCMD
4975 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4976 ++autocmd_no_enter;
4977#endif
4978 win_enter(lastwin, FALSE);
4979#ifdef FEAT_AUTOCMD
4980 ++autocmd_no_leave;
4981#endif
4982 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4983 {
4984 /* Check if this buffer needs a window */
4985 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4986 continue;
4987
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004988#ifdef FEAT_WINDOWS
4989 if (had_tab != 0)
4990 {
4991 /* With the ":tab" modifier don't move the window. */
4992 if (buf->b_nwindows > 0)
4993 wp = lastwin; /* buffer has a window, skip it */
4994 else
4995 wp = NULL;
4996 }
4997 else
4998#endif
4999 {
5000 /* Check if this buffer already has a window */
5001 for (wp = firstwin; wp != NULL; wp = wp->w_next)
5002 if (wp->w_buffer == buf)
5003 break;
5004 /* If the buffer already has a window, move it */
5005 if (wp != NULL)
5006 win_move_after(wp, curwin);
5007 }
5008
5009 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
5011 /* Split the window and put the buffer in it */
5012 p_ea_save = p_ea;
5013 p_ea = TRUE; /* use space from all windows */
5014 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
5015 ++open_wins;
5016 p_ea = p_ea_save;
5017 if (split_ret == FAIL)
5018 continue;
5019
5020 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005021#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 swap_exists_action = SEA_DIALOG;
5023#endif
5024 set_curbuf(buf, DOBUF_GOTO);
5025#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00005028#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 break;
5032 }
5033#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005034#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 if (swap_exists_action == SEA_QUIT)
5036 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005037# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5038 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005039
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005040 /* Reset the error/interrupt/exception state here so that
5041 * aborting() returns FALSE when closing a window. */
5042 enter_cleanup(&cs);
5043# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005044
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005045 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 win_close(curwin, TRUE);
5047 --open_wins;
5048 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005049 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005050
5051# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5052 /* Restore the error/interrupt/exception state if not
5053 * discarded by a new aborting error, interrupt, or uncaught
5054 * exception. */
5055 leave_cleanup(&cs);
5056# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058 else
5059 handle_swap_exists(NULL);
5060#endif
5061 }
5062
5063 ui_breakcheck();
5064 if (got_int)
5065 {
5066 (void)vgetc(); /* only break the file loading, not the rest */
5067 break;
5068 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005069#ifdef FEAT_EVAL
5070 /* Autocommands deleted the buffer or aborted script processing!!! */
5071 if (aborting())
5072 break;
5073#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005074#ifdef FEAT_WINDOWS
5075 /* When ":tab" was used open a new tab for a new window repeatedly. */
5076 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5077 cmdmod.tab = 9999;
5078#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080#ifdef FEAT_AUTOCMD
5081 --autocmd_no_enter;
5082#endif
5083 win_enter(firstwin, FALSE); /* back to first window */
5084#ifdef FEAT_AUTOCMD
5085 --autocmd_no_leave;
5086#endif
5087
5088 /*
5089 * Close superfluous windows.
5090 */
5091 for (wp = lastwin; open_wins > count; )
5092 {
5093 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5094 || autowrite(wp->w_buffer, FALSE) == OK);
5095#ifdef FEAT_AUTOCMD
5096 if (!win_valid(wp))
5097 {
5098 /* BufWrite Autocommands made the window invalid, start over */
5099 wp = lastwin;
5100 }
5101 else
5102#endif
5103 if (r)
5104 {
5105 win_close(wp, !P_HID(wp->w_buffer));
5106 --open_wins;
5107 wp = lastwin;
5108 }
5109 else
5110 {
5111 wp = wp->w_prev;
5112 if (wp == NULL)
5113 break;
5114 }
5115 }
5116}
5117# endif /* FEAT_LISTCMDS */
5118
5119#endif /* FEAT_WINDOWS */
5120
Bram Moolenaara3227e22006-03-08 21:32:40 +00005121static int chk_modeline __ARGS((linenr_T, int));
5122
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123/*
5124 * do_modelines() - process mode lines for the current file
5125 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005126 * "flags" can be:
5127 * OPT_WINONLY only set options local to window
5128 * OPT_NOWIN don't set options local to window
5129 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 * Returns immediately if the "ml" option isn't set.
5131 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005133do_modelines(flags)
5134 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005136 linenr_T lnum;
5137 int nmlines;
5138 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139
5140 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5141 return;
5142
5143 /* Disallow recursive entry here. Can happen when executing a modeline
5144 * triggers an autocommand, which reloads modelines with a ":do". */
5145 if (entered)
5146 return;
5147
5148 ++entered;
5149 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5150 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005151 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 nmlines = 0;
5153
5154 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5155 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005156 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 nmlines = 0;
5158 --entered;
5159}
5160
5161#include "version.h" /* for version number */
5162
5163/*
5164 * chk_modeline() - check a single line for a mode string
5165 * Return FAIL if an error encountered.
5166 */
5167 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005168chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005170 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171{
5172 char_u *s;
5173 char_u *e;
5174 char_u *linecopy; /* local copy of any modeline found */
5175 int prev;
5176 int vers;
5177 int end;
5178 int retval = OK;
5179 char_u *save_sourcing_name;
5180 linenr_T save_sourcing_lnum;
5181#ifdef FEAT_EVAL
5182 scid_T save_SID;
5183#endif
5184
5185 prev = -1;
5186 for (s = ml_get(lnum); *s != NUL; ++s)
5187 {
5188 if (prev == -1 || vim_isspace(prev))
5189 {
5190 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5191 || STRNCMP(s, "vi:", (size_t)3) == 0)
5192 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005193 /* Accept both "vim" and "Vim". */
5194 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 {
5196 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5197 e = s + 4;
5198 else
5199 e = s + 3;
5200 vers = getdigits(&e);
5201 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005202 && (s[0] != 'V'
5203 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 && (s[3] == ':'
5205 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5206 || (VIM_VERSION_100 < vers && s[3] == '<')
5207 || (VIM_VERSION_100 > vers && s[3] == '>')
5208 || (VIM_VERSION_100 == vers && s[3] == '=')))
5209 break;
5210 }
5211 }
5212 prev = *s;
5213 }
5214
5215 if (*s)
5216 {
5217 do /* skip over "ex:", "vi:" or "vim:" */
5218 ++s;
5219 while (s[-1] != ':');
5220
5221 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5222 if (linecopy == NULL)
5223 return FAIL;
5224
5225 save_sourcing_lnum = sourcing_lnum;
5226 save_sourcing_name = sourcing_name;
5227 sourcing_lnum = lnum; /* prepare for emsg() */
5228 sourcing_name = (char_u *)"modelines";
5229
5230 end = FALSE;
5231 while (end == FALSE)
5232 {
5233 s = skipwhite(s);
5234 if (*s == NUL)
5235 break;
5236
5237 /*
5238 * Find end of set command: ':' or end of line.
5239 * Skip over "\:", replacing it with ":".
5240 */
5241 for (e = s; *e != ':' && *e != NUL; ++e)
5242 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005243 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 if (*e == NUL)
5245 end = TRUE;
5246
5247 /*
5248 * If there is a "set" command, require a terminating ':' and
5249 * ignore the stuff after the ':'.
5250 * "vi:set opt opt opt: foo" -- foo not interpreted
5251 * "vi:opt opt opt: foo" -- foo interpreted
5252 * Accept "se" for compatibility with Elvis.
5253 */
5254 if (STRNCMP(s, "set ", (size_t)4) == 0
5255 || STRNCMP(s, "se ", (size_t)3) == 0)
5256 {
5257 if (*e != ':') /* no terminating ':'? */
5258 break;
5259 end = TRUE;
5260 s = vim_strchr(s, ' ') + 1;
5261 }
5262 *e = NUL; /* truncate the set command */
5263
5264 if (*s != NUL) /* skip over an empty "::" */
5265 {
5266#ifdef FEAT_EVAL
5267 save_SID = current_SID;
5268 current_SID = SID_MODELINE;
5269#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005270 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271#ifdef FEAT_EVAL
5272 current_SID = save_SID;
5273#endif
5274 if (retval == FAIL) /* stop if error found */
5275 break;
5276 }
5277 s = e + 1; /* advance to next part */
5278 }
5279
5280 sourcing_lnum = save_sourcing_lnum;
5281 sourcing_name = save_sourcing_name;
5282
5283 vim_free(linecopy);
5284 }
5285 return retval;
5286}
5287
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005288#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 int
5290read_viminfo_bufferlist(virp, writing)
5291 vir_T *virp;
5292 int writing;
5293{
5294 char_u *tab;
5295 linenr_T lnum;
5296 colnr_T col;
5297 buf_T *buf;
5298 char_u *sfname;
5299 char_u *xline;
5300
5301 /* Handle long line and escaped characters. */
5302 xline = viminfo_readstring(virp, 1, FALSE);
5303
5304 /* don't read in if there are files on the command-line or if writing: */
5305 if (xline != NULL && !writing && ARGCOUNT == 0
5306 && find_viminfo_parameter('%') != NULL)
5307 {
5308 /* Format is: <fname> Tab <lnum> Tab <col>.
5309 * Watch out for a Tab in the file name, work from the end. */
5310 lnum = 0;
5311 col = 0;
5312 tab = vim_strrchr(xline, '\t');
5313 if (tab != NULL)
5314 {
5315 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005316 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 tab = vim_strrchr(xline, '\t');
5318 if (tab != NULL)
5319 {
5320 *tab++ = '\0';
5321 lnum = atol((char *)tab);
5322 }
5323 }
5324
5325 /* Expand "~/" in the file name at "line + 1" to a full path.
5326 * Then try shortening it by comparing with the current directory */
5327 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005328 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5331 if (buf != NULL) /* just in case... */
5332 {
5333 buf->b_last_cursor.lnum = lnum;
5334 buf->b_last_cursor.col = col;
5335 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5336 }
5337 }
5338 vim_free(xline);
5339
5340 return viminfo_readline(virp);
5341}
5342
5343 void
5344write_viminfo_bufferlist(fp)
5345 FILE *fp;
5346{
5347 buf_T *buf;
5348#ifdef FEAT_WINDOWS
5349 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005350 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351#endif
5352 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005353 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354
5355 if (find_viminfo_parameter('%') == NULL)
5356 return;
5357
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005358 /* Without a number -1 is returned: do all buffers. */
5359 max_buffers = get_viminfo_parameter('%');
5360
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005362#define LINE_BUF_LEN (MAXPATHL + 40)
5363 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 if (line == NULL)
5365 return;
5366
5367#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005368 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 set_last_cursor(win);
5370#else
5371 set_last_cursor(curwin);
5372#endif
5373
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005374 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5376 {
5377 if (buf->b_fname == NULL
5378 || !buf->b_p_bl
5379#ifdef FEAT_QUICKFIX
5380 || bt_quickfix(buf)
5381#endif
5382 || removable(buf->b_ffname))
5383 continue;
5384
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005385 if (max_buffers-- == 0)
5386 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387 putc('%', fp);
5388 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005389 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005390 (long)buf->b_last_cursor.lnum,
5391 buf->b_last_cursor.col);
5392 viminfo_writestring(fp, line);
5393 }
5394 vim_free(line);
5395}
5396#endif
5397
5398
5399/*
5400 * Return special buffer name.
5401 * Returns NULL when the buffer has a normal file name.
5402 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005403 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404buf_spname(buf)
5405 buf_T *buf;
5406{
5407#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5408 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005409 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005410 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005411 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005412
5413 /*
5414 * For location list window, w_llist_ref points to the location list.
5415 * For quickfix window, w_llist_ref is NULL.
5416 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005417 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005418 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005419 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005420 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422#endif
5423#ifdef FEAT_QUICKFIX
5424 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5425 * contains the name as specified by the user */
5426 if (bt_nofile(buf))
5427 {
5428 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005429 return buf->b_sfname;
5430 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432#endif
5433 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005434 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 return NULL;
5436}
5437
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005438#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5439 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5440 || defined(PROTO)
5441/*
5442 * Find a window for buffer "buf".
5443 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5444 * If not found FAIL is returned.
5445 */
5446 int
5447find_win_for_buf(buf, wp, tp)
5448 buf_T *buf;
5449 win_T **wp;
5450 tabpage_T **tp;
5451{
5452 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5453 if ((*wp)->w_buffer == buf)
5454 goto win_found;
5455 return FAIL;
5456win_found:
5457 return OK;
5458}
5459#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460
5461#if defined(FEAT_SIGNS) || defined(PROTO)
5462/*
5463 * Insert the sign into the signlist.
5464 */
5465 static void
5466insert_sign(buf, prev, next, id, lnum, typenr)
5467 buf_T *buf; /* buffer to store sign in */
5468 signlist_T *prev; /* previous sign entry */
5469 signlist_T *next; /* next sign entry */
5470 int id; /* sign ID */
5471 linenr_T lnum; /* line number which gets the mark */
5472 int typenr; /* typenr of sign we are adding */
5473{
5474 signlist_T *newsign;
5475
5476 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5477 if (newsign != NULL)
5478 {
5479 newsign->id = id;
5480 newsign->lnum = lnum;
5481 newsign->typenr = typenr;
5482 newsign->next = next;
5483#ifdef FEAT_NETBEANS_INTG
5484 newsign->prev = prev;
5485 if (next != NULL)
5486 next->prev = newsign;
5487#endif
5488
5489 if (prev == NULL)
5490 {
5491 /* When adding first sign need to redraw the windows to create the
5492 * column for signs. */
5493 if (buf->b_signlist == NULL)
5494 {
5495 redraw_buf_later(buf, NOT_VALID);
5496 changed_cline_bef_curs();
5497 }
5498
5499 /* first sign in signlist */
5500 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005501#ifdef FEAT_NETBEANS_INTG
5502 if (netbeans_active())
5503 buf->b_has_sign_column = TRUE;
5504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 }
5506 else
5507 prev->next = newsign;
5508 }
5509}
5510
5511/*
5512 * Add the sign into the signlist. Find the right spot to do it though.
5513 */
5514 void
5515buf_addsign(buf, id, lnum, typenr)
5516 buf_T *buf; /* buffer to store sign in */
5517 int id; /* sign ID */
5518 linenr_T lnum; /* line number which gets the mark */
5519 int typenr; /* typenr of sign we are adding */
5520{
5521 signlist_T *sign; /* a sign in the signlist */
5522 signlist_T *prev; /* the previous sign */
5523
5524 prev = NULL;
5525 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5526 {
5527 if (lnum == sign->lnum && id == sign->id)
5528 {
5529 sign->typenr = typenr;
5530 return;
5531 }
5532 else if (
5533#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5534 id < 0 &&
5535#endif
5536 lnum < sign->lnum)
5537 {
5538#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5539 /* XXX - GRP: Is this because of sign slide problem? Or is it
5540 * really needed? Or is it because we allow multiple signs per
5541 * line? If so, should I add that feature to FEAT_SIGNS?
5542 */
5543 while (prev != NULL && prev->lnum == lnum)
5544 prev = prev->prev;
5545 if (prev == NULL)
5546 sign = buf->b_signlist;
5547 else
5548 sign = prev->next;
5549#endif
5550 insert_sign(buf, prev, sign, id, lnum, typenr);
5551 return;
5552 }
5553 prev = sign;
5554 }
5555#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5556 /* XXX - GRP: See previous comment */
5557 while (prev != NULL && prev->lnum == lnum)
5558 prev = prev->prev;
5559 if (prev == NULL)
5560 sign = buf->b_signlist;
5561 else
5562 sign = prev->next;
5563#endif
5564 insert_sign(buf, prev, sign, id, lnum, typenr);
5565
5566 return;
5567}
5568
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005569/*
5570 * For an existing, placed sign "markId" change the type to "typenr".
5571 * Returns the line number of the sign, or zero if the sign is not found.
5572 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005573 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574buf_change_sign_type(buf, markId, typenr)
5575 buf_T *buf; /* buffer to store sign in */
5576 int markId; /* sign ID */
5577 int typenr; /* typenr of sign we are adding */
5578{
5579 signlist_T *sign; /* a sign in the signlist */
5580
5581 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5582 {
5583 if (sign->id == markId)
5584 {
5585 sign->typenr = typenr;
5586 return sign->lnum;
5587 }
5588 }
5589
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005590 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591}
5592
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005593 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594buf_getsigntype(buf, lnum, type)
5595 buf_T *buf;
5596 linenr_T lnum;
5597 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5598{
5599 signlist_T *sign; /* a sign in a b_signlist */
5600
5601 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5602 if (sign->lnum == lnum
5603 && (type == SIGN_ANY
5604# ifdef FEAT_SIGN_ICONS
5605 || (type == SIGN_ICON
5606 && sign_get_image(sign->typenr) != NULL)
5607# endif
5608 || (type == SIGN_TEXT
5609 && sign_get_text(sign->typenr) != NULL)
5610 || (type == SIGN_LINEHL
5611 && sign_get_attr(sign->typenr, TRUE) != 0)))
5612 return sign->typenr;
5613 return 0;
5614}
5615
5616
5617 linenr_T
5618buf_delsign(buf, id)
5619 buf_T *buf; /* buffer sign is stored in */
5620 int id; /* sign id */
5621{
5622 signlist_T **lastp; /* pointer to pointer to current sign */
5623 signlist_T *sign; /* a sign in a b_signlist */
5624 signlist_T *next; /* the next sign in a b_signlist */
5625 linenr_T lnum; /* line number whose sign was deleted */
5626
5627 lastp = &buf->b_signlist;
5628 lnum = 0;
5629 for (sign = buf->b_signlist; sign != NULL; sign = next)
5630 {
5631 next = sign->next;
5632 if (sign->id == id)
5633 {
5634 *lastp = next;
5635#ifdef FEAT_NETBEANS_INTG
5636 if (next != NULL)
5637 next->prev = sign->prev;
5638#endif
5639 lnum = sign->lnum;
5640 vim_free(sign);
5641 break;
5642 }
5643 else
5644 lastp = &sign->next;
5645 }
5646
5647 /* When deleted the last sign need to redraw the windows to remove the
5648 * sign column. */
5649 if (buf->b_signlist == NULL)
5650 {
5651 redraw_buf_later(buf, NOT_VALID);
5652 changed_cline_bef_curs();
5653 }
5654
5655 return lnum;
5656}
5657
5658
5659/*
5660 * Find the line number of the sign with the requested id. If the sign does
5661 * not exist, return 0 as the line number. This will still let the correct file
5662 * get loaded.
5663 */
5664 int
5665buf_findsign(buf, id)
5666 buf_T *buf; /* buffer to store sign in */
5667 int id; /* sign ID */
5668{
5669 signlist_T *sign; /* a sign in the signlist */
5670
5671 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5672 if (sign->id == id)
5673 return sign->lnum;
5674
5675 return 0;
5676}
5677
5678 int
5679buf_findsign_id(buf, lnum)
5680 buf_T *buf; /* buffer whose sign we are searching for */
5681 linenr_T lnum; /* line number of sign */
5682{
5683 signlist_T *sign; /* a sign in the signlist */
5684
5685 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5686 if (sign->lnum == lnum)
5687 return sign->id;
5688
5689 return 0;
5690}
5691
5692
5693# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5694/* see if a given type of sign exists on a specific line */
5695 int
5696buf_findsigntype_id(buf, lnum, typenr)
5697 buf_T *buf; /* buffer whose sign we are searching for */
5698 linenr_T lnum; /* line number of sign */
5699 int typenr; /* sign type number */
5700{
5701 signlist_T *sign; /* a sign in the signlist */
5702
5703 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5704 if (sign->lnum == lnum && sign->typenr == typenr)
5705 return sign->id;
5706
5707 return 0;
5708}
5709
5710
5711# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5712/* return the number of icons on the given line */
5713 int
5714buf_signcount(buf, lnum)
5715 buf_T *buf;
5716 linenr_T lnum;
5717{
5718 signlist_T *sign; /* a sign in the signlist */
5719 int count = 0;
5720
5721 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5722 if (sign->lnum == lnum)
5723 if (sign_get_image(sign->typenr) != NULL)
5724 count++;
5725
5726 return count;
5727}
5728# endif /* FEAT_SIGN_ICONS */
5729# endif /* FEAT_NETBEANS_INTG */
5730
5731
5732/*
5733 * Delete signs in buffer "buf".
5734 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005735 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736buf_delete_signs(buf)
5737 buf_T *buf;
5738{
5739 signlist_T *next;
5740
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005741 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005742 * sign column. Not when curwin is NULL (this means we're exiting). */
5743 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005744 {
5745 redraw_buf_later(buf, NOT_VALID);
5746 changed_cline_bef_curs();
5747 }
5748
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749 while (buf->b_signlist != NULL)
5750 {
5751 next = buf->b_signlist->next;
5752 vim_free(buf->b_signlist);
5753 buf->b_signlist = next;
5754 }
5755}
5756
5757/*
5758 * Delete all signs in all buffers.
5759 */
5760 void
5761buf_delete_all_signs()
5762{
5763 buf_T *buf; /* buffer we are checking for signs */
5764
5765 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5766 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768}
5769
5770/*
5771 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5772 */
5773 void
5774sign_list_placed(rbuf)
5775 buf_T *rbuf;
5776{
5777 buf_T *buf;
5778 signlist_T *p;
5779 char lbuf[BUFSIZ];
5780
5781 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5782 msg_putchar('\n');
5783 if (rbuf == NULL)
5784 buf = firstbuf;
5785 else
5786 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005787 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 {
5789 if (buf->b_signlist != NULL)
5790 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005791 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5793 msg_putchar('\n');
5794 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005795 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005797 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5799 MSG_PUTS(lbuf);
5800 msg_putchar('\n');
5801 }
5802 if (rbuf != NULL)
5803 break;
5804 buf = buf->b_next;
5805 }
5806}
5807
5808/*
5809 * Adjust a placed sign for inserted/deleted lines.
5810 */
5811 void
5812sign_mark_adjust(line1, line2, amount, amount_after)
5813 linenr_T line1;
5814 linenr_T line2;
5815 long amount;
5816 long amount_after;
5817{
5818 signlist_T *sign; /* a sign in a b_signlist */
5819
5820 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5821 {
5822 if (sign->lnum >= line1 && sign->lnum <= line2)
5823 {
5824 if (amount == MAXLNUM)
5825 sign->lnum = line1;
5826 else
5827 sign->lnum += amount;
5828 }
5829 else if (sign->lnum > line2)
5830 sign->lnum += amount_after;
5831 }
5832}
5833#endif /* FEAT_SIGNS */
5834
5835/*
5836 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5837 */
5838 void
5839set_buflisted(on)
5840 int on;
5841{
5842 if (on != curbuf->b_p_bl)
5843 {
5844 curbuf->b_p_bl = on;
5845#ifdef FEAT_AUTOCMD
5846 if (on)
5847 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5848 else
5849 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5850#endif
5851 }
5852}
5853
5854/*
5855 * Read the file for "buf" again and check if the contents changed.
5856 * Return TRUE if it changed or this could not be checked.
5857 */
5858 int
5859buf_contents_changed(buf)
5860 buf_T *buf;
5861{
5862 buf_T *newbuf;
5863 int differ = TRUE;
5864 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 exarg_T ea;
5867
5868 /* Allocate a buffer without putting it in the buffer list. */
5869 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5870 if (newbuf == NULL)
5871 return TRUE;
5872
5873 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5874 if (prep_exarg(&ea, buf) == FAIL)
5875 {
5876 wipe_buffer(newbuf, FALSE);
5877 return TRUE;
5878 }
5879
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 /* set curwin/curbuf to buf and save a few things */
5881 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882
Bram Moolenaar4770d092006-01-12 23:22:24 +00005883 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 && readfile(buf->b_ffname, buf->b_fname,
5885 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5886 &ea, READ_NEW | READ_DUMMY) == OK)
5887 {
5888 /* compare the two files line by line */
5889 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5890 {
5891 differ = FALSE;
5892 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5893 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5894 {
5895 differ = TRUE;
5896 break;
5897 }
5898 }
5899 }
5900 vim_free(ea.cmd);
5901
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 /* restore curwin/curbuf and a few other things */
5903 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904
5905 if (curbuf != newbuf) /* safety check */
5906 wipe_buffer(newbuf, FALSE);
5907
5908 return differ;
5909}
5910
5911/*
5912 * Wipe out a buffer and decrement the last buffer number if it was used for
5913 * this buffer. Call this to wipe out a temp buffer that does not contain any
5914 * marks.
5915 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005916 void
5917wipe_buffer(buf, aucmd)
5918 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005919 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920{
5921 if (buf->b_fnum == top_file_num - 1)
5922 --top_file_num;
5923
5924#ifdef FEAT_AUTOCMD
5925 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005926 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005928 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929#ifdef FEAT_AUTOCMD
5930 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005931 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932#endif
5933}