blob: 6dfe007c28e0e4dfcaab57005032b0f98ff539a2 [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 Moolenaarf71a3db2006-03-12 21:50:18 +00001958#endif
1959#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001960 clear_string_option(&buf->b_s.b_p_spc);
1961 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001962 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001963 buf->b_s.b_cap_prog = NULL;
1964 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965#endif
1966#ifdef FEAT_SEARCHPATH
1967 clear_string_option(&buf->b_p_sua);
1968#endif
1969#ifdef FEAT_AUTOCMD
1970 clear_string_option(&buf->b_p_ft);
1971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972#ifdef FEAT_CINDENT
1973 clear_string_option(&buf->b_p_cink);
1974 clear_string_option(&buf->b_p_cino);
1975#endif
1976#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1977 clear_string_option(&buf->b_p_cinw);
1978#endif
1979#ifdef FEAT_INS_EXPAND
1980 clear_string_option(&buf->b_p_cpt);
1981#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001982#ifdef FEAT_COMPL_FUNC
1983 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001984 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986#ifdef FEAT_QUICKFIX
1987 clear_string_option(&buf->b_p_gp);
1988 clear_string_option(&buf->b_p_mp);
1989 clear_string_option(&buf->b_p_efm);
1990#endif
1991 clear_string_option(&buf->b_p_ep);
1992 clear_string_option(&buf->b_p_path);
1993 clear_string_option(&buf->b_p_tags);
1994#ifdef FEAT_INS_EXPAND
1995 clear_string_option(&buf->b_p_dict);
1996 clear_string_option(&buf->b_p_tsr);
1997#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001998#ifdef FEAT_TEXTOBJ
1999 clear_string_option(&buf->b_p_qe);
2000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002002 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002003#ifdef FEAT_LISP
2004 clear_string_option(&buf->b_p_lw);
2005#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002006 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007}
2008
2009/*
2010 * get alternate file n
2011 * set linenr to lnum or altfpos.lnum if lnum == 0
2012 * also set cursor column to altfpos.col if 'startofline' is not set.
2013 * if (options & GETF_SETMARK) call setpcmark()
2014 * if (options & GETF_ALT) we are jumping to an alternate file.
2015 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2016 *
2017 * return FAIL for failure, OK for success
2018 */
2019 int
2020buflist_getfile(n, lnum, options, forceit)
2021 int n;
2022 linenr_T lnum;
2023 int options;
2024 int forceit;
2025{
2026 buf_T *buf;
2027#ifdef FEAT_WINDOWS
2028 win_T *wp = NULL;
2029#endif
2030 pos_T *fpos;
2031 colnr_T col;
2032
2033 buf = buflist_findnr(n);
2034 if (buf == NULL)
2035 {
2036 if ((options & GETF_ALT) && n == 0)
2037 EMSG(_(e_noalt));
2038 else
2039 EMSGN(_("E92: Buffer %ld not found"), n);
2040 return FAIL;
2041 }
2042
2043 /* if alternate file is the current buffer, nothing to do */
2044 if (buf == curbuf)
2045 return OK;
2046
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002047 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002048 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002049 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002051 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002052#ifdef FEAT_AUTOCMD
2053 if (curbuf_locked())
2054 return FAIL;
2055#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056
2057 /* altfpos may be changed by getfile(), get it now */
2058 if (lnum == 0)
2059 {
2060 fpos = buflist_findfpos(buf);
2061 lnum = fpos->lnum;
2062 col = fpos->col;
2063 }
2064 else
2065 col = 0;
2066
2067#ifdef FEAT_WINDOWS
2068 if (options & GETF_SWITCH)
2069 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002070 /* If 'switchbuf' contains "useopen": jump to first window containing
2071 * "buf" if one exists */
2072 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 wp = buf_jump_open_win(buf);
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002074 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002075 * page containing "buf" if one exists */
2076 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002077 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00002078 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
2079 * isn't empty: open new window */
2080 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002082 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
2083 tabpage_new();
2084 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002086 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 }
2088 }
2089#endif
2090
2091 ++RedrawingDisabled;
2092 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2093 lnum, forceit) <= 0)
2094 {
2095 --RedrawingDisabled;
2096
2097 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2098 if (!p_sol && col != 0)
2099 {
2100 curwin->w_cursor.col = col;
2101 check_cursor_col();
2102#ifdef FEAT_VIRTUALEDIT
2103 curwin->w_cursor.coladd = 0;
2104#endif
2105 curwin->w_set_curswant = TRUE;
2106 }
2107 return OK;
2108 }
2109 --RedrawingDisabled;
2110 return FAIL;
2111}
2112
2113/*
2114 * go to the last know line number for the current buffer
2115 */
2116 void
2117buflist_getfpos()
2118{
2119 pos_T *fpos;
2120
2121 fpos = buflist_findfpos(curbuf);
2122
2123 curwin->w_cursor.lnum = fpos->lnum;
2124 check_cursor_lnum();
2125
2126 if (p_sol)
2127 curwin->w_cursor.col = 0;
2128 else
2129 {
2130 curwin->w_cursor.col = fpos->col;
2131 check_cursor_col();
2132#ifdef FEAT_VIRTUALEDIT
2133 curwin->w_cursor.coladd = 0;
2134#endif
2135 curwin->w_set_curswant = TRUE;
2136 }
2137}
2138
Bram Moolenaar81695252004-12-29 20:58:21 +00002139#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2140/*
2141 * Find file in buffer list by name (it has to be for the current window).
2142 * Returns NULL if not found.
2143 */
2144 buf_T *
2145buflist_findname_exp(fname)
2146 char_u *fname;
2147{
2148 char_u *ffname;
2149 buf_T *buf = NULL;
2150
2151 /* First make the name into a full path name */
2152 ffname = FullName_save(fname,
2153#ifdef UNIX
2154 TRUE /* force expansion, get rid of symbolic links */
2155#else
2156 FALSE
2157#endif
2158 );
2159 if (ffname != NULL)
2160 {
2161 buf = buflist_findname(ffname);
2162 vim_free(ffname);
2163 }
2164 return buf;
2165}
2166#endif
2167
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168/*
2169 * Find file in buffer list by name (it has to be for the current window).
2170 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002171 * Skips dummy buffers.
2172 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 */
2174 buf_T *
2175buflist_findname(ffname)
2176 char_u *ffname;
2177{
2178#ifdef UNIX
2179 struct stat st;
2180
2181 if (mch_stat((char *)ffname, &st) < 0)
2182 st.st_dev = (dev_T)-1;
2183 return buflist_findname_stat(ffname, &st);
2184}
2185
2186/*
2187 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2188 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002189 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 */
2191 static buf_T *
2192buflist_findname_stat(ffname, stp)
2193 char_u *ffname;
2194 struct stat *stp;
2195{
2196#endif
2197 buf_T *buf;
2198
2199 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002200 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201#ifdef UNIX
2202 , stp
2203#endif
2204 ))
2205 return buf;
2206 return NULL;
2207}
2208
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002209#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2210 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211/*
2212 * Find file in buffer list by a regexp pattern.
2213 * Return fnum of the found buffer.
2214 * Return < 0 for error.
2215 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002217buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002218 char_u *pattern;
2219 char_u *pattern_end; /* pointer to first char after pattern */
2220 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002221 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002222 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
2224 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 int match = -1;
2226 int find_listed;
2227 char_u *pat;
2228 char_u *patend;
2229 int attempt;
2230 char_u *p;
2231 int toggledollar;
2232
2233 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2234 {
2235 if (*pattern == '%')
2236 match = curbuf->b_fnum;
2237 else
2238 match = curwin->w_alt_fnum;
2239#ifdef FEAT_DIFF
2240 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2241 match = -1;
2242#endif
2243 }
2244
2245 /*
2246 * Try four ways of matching a listed buffer:
2247 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002248 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 * attempt == 2: with '$' at end (only match at end)
2250 * attempt == 3: with '^' at start and '$' at end (only full match)
2251 * Repeat this for finding an unlisted buffer if there was no matching
2252 * listed buffer.
2253 */
2254 else
2255 {
2256 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2257 if (pat == NULL)
2258 return -1;
2259 patend = pat + STRLEN(pat) - 1;
2260 toggledollar = (patend > pat && *patend == '$');
2261
2262 /* First try finding a listed buffer. If not found and "unlisted"
2263 * is TRUE, try finding an unlisted buffer. */
2264 find_listed = TRUE;
2265 for (;;)
2266 {
2267 for (attempt = 0; attempt <= 3; ++attempt)
2268 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002269 regmatch_T regmatch;
2270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 /* may add '^' and '$' */
2272 if (toggledollar)
2273 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2274 p = pat;
2275 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2276 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002277 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2278 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 {
2280 vim_free(pat);
2281 return -1;
2282 }
2283
2284 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2285 if (buf->b_p_bl == find_listed
2286#ifdef FEAT_DIFF
2287 && (!diffmode || diff_mode_buf(buf))
2288#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002289 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002291 if (curtab_only)
2292 {
2293 /* Ignore the match if the buffer is not open in
2294 * the current tab. */
2295#ifdef FEAT_WINDOWS
2296 win_T *wp;
2297
2298 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2299 if (wp->w_buffer == buf)
2300 break;
2301 if (wp == NULL)
2302 continue;
2303#else
2304 if (curwin->w_buffer != buf)
2305 continue;
2306#endif
2307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 if (match >= 0) /* already found a match */
2309 {
2310 match = -2;
2311 break;
2312 }
2313 match = buf->b_fnum; /* remember first match */
2314 }
2315
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002316 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (match >= 0) /* found one match */
2318 break;
2319 }
2320
2321 /* Only search for unlisted buffers if there was no match with
2322 * a listed buffer. */
2323 if (!unlisted || !find_listed || match != -1)
2324 break;
2325 find_listed = FALSE;
2326 }
2327
2328 vim_free(pat);
2329 }
2330
2331 if (match == -2)
2332 EMSG2(_("E93: More than one match for %s"), pattern);
2333 else if (match < 0)
2334 EMSG2(_("E94: No matching buffer for %s"), pattern);
2335 return match;
2336}
2337#endif
2338
2339#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2340
2341/*
2342 * Find all buffer names that match.
2343 * For command line expansion of ":buf" and ":sbuf".
2344 * Return OK if matches found, FAIL otherwise.
2345 */
2346 int
2347ExpandBufnames(pat, num_file, file, options)
2348 char_u *pat;
2349 int *num_file;
2350 char_u ***file;
2351 int options;
2352{
2353 int count = 0;
2354 buf_T *buf;
2355 int round;
2356 char_u *p;
2357 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002358 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 *num_file = 0; /* return values in case of FAIL */
2361 *file = NULL;
2362
Bram Moolenaar05159a02005-02-26 23:04:13 +00002363 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2364 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002366 patc = alloc((unsigned)STRLEN(pat) + 11);
2367 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002369 STRCPY(patc, "\\(^\\|[\\/]\\)");
2370 STRCPY(patc + 11, pat + 1);
2371 }
2372 else
2373 patc = pat;
2374
2375 /*
2376 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002377 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002378 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002379 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002380 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002381 regmatch_T regmatch;
2382
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002383 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002384 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002385 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2386 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002387 {
2388 if (patc != pat)
2389 vim_free(patc);
2390 return FAIL;
2391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392
2393 /*
2394 * round == 1: Count the matches.
2395 * round == 2: Build the array to keep the matches.
2396 */
2397 for (round = 1; round <= 2; ++round)
2398 {
2399 count = 0;
2400 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2401 {
2402 if (!buf->b_p_bl) /* skip unlisted buffers */
2403 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002404 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 if (p != NULL)
2406 {
2407 if (round == 1)
2408 ++count;
2409 else
2410 {
2411 if (options & WILD_HOME_REPLACE)
2412 p = home_replace_save(buf, p);
2413 else
2414 p = vim_strsave(p);
2415 (*file)[count++] = p;
2416 }
2417 }
2418 }
2419 if (count == 0) /* no match found, break here */
2420 break;
2421 if (round == 1)
2422 {
2423 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2424 if (*file == NULL)
2425 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002426 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002427 if (patc != pat)
2428 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 return FAIL;
2430 }
2431 }
2432 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002433 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 if (count) /* match(es) found, break here */
2435 break;
2436 }
2437
Bram Moolenaar05159a02005-02-26 23:04:13 +00002438 if (patc != pat)
2439 vim_free(patc);
2440
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 *num_file = count;
2442 return (count == 0 ? FAIL : OK);
2443}
2444
2445#endif /* FEAT_CMDL_COMPL */
2446
2447#ifdef HAVE_BUFLIST_MATCH
2448/*
2449 * Check for a match on the file name for buffer "buf" with regprog "prog".
2450 */
2451 static char_u *
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002452buflist_match(rmp, buf, ignore_case)
2453 regmatch_T *rmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 buf_T *buf;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002455 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 char_u *match;
2458
2459 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002460 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002462 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463
2464 return match;
2465}
2466
2467/*
2468 * Try matching the regexp in "prog" with file name "name".
2469 * Return "name" when there is a match, NULL when not.
2470 */
2471 static char_u *
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002472fname_match(rmp, name, ignore_case)
2473 regmatch_T *rmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 char_u *name;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002475 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476{
2477 char_u *match = NULL;
2478 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479
2480 if (name != NULL)
2481 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002482 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002483 rmp->rm_ic = p_fic || ignore_case;
2484 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 match = name;
2486 else
2487 {
2488 /* Replace $(HOME) with '~' and try matching again. */
2489 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002490 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 match = name;
2492 vim_free(p);
2493 }
2494 }
2495
2496 return match;
2497}
2498#endif
2499
2500/*
2501 * find file in buffer list by number
2502 */
2503 buf_T *
2504buflist_findnr(nr)
2505 int nr;
2506{
2507 buf_T *buf;
2508
2509 if (nr == 0)
2510 nr = curwin->w_alt_fnum;
2511 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2512 if (buf->b_fnum == nr)
2513 return (buf);
2514 return NULL;
2515}
2516
2517/*
2518 * Get name of file 'n' in the buffer list.
2519 * When the file has no name an empty string is returned.
2520 * home_replace() is used to shorten the file name (used for marks).
2521 * Returns a pointer to allocated memory, of NULL when failed.
2522 */
2523 char_u *
2524buflist_nr2name(n, fullname, helptail)
2525 int n;
2526 int fullname;
2527 int helptail; /* for help buffers return tail only */
2528{
2529 buf_T *buf;
2530
2531 buf = buflist_findnr(n);
2532 if (buf == NULL)
2533 return NULL;
2534 return home_replace_save(helptail ? buf : NULL,
2535 fullname ? buf->b_ffname : buf->b_fname);
2536}
2537
2538/*
2539 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2540 * When "copy_options" is TRUE save the local window option values.
2541 * When "lnum" is 0 only do the options.
2542 */
2543 static void
2544buflist_setfpos(buf, win, lnum, col, copy_options)
2545 buf_T *buf;
2546 win_T *win;
2547 linenr_T lnum;
2548 colnr_T col;
2549 int copy_options;
2550{
2551 wininfo_T *wip;
2552
2553 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2554 if (wip->wi_win == win)
2555 break;
2556 if (wip == NULL)
2557 {
2558 /* allocate a new entry */
2559 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2560 if (wip == NULL)
2561 return;
2562 wip->wi_win = win;
2563 if (lnum == 0) /* set lnum even when it's 0 */
2564 lnum = 1;
2565 }
2566 else
2567 {
2568 /* remove the entry from the list */
2569 if (wip->wi_prev)
2570 wip->wi_prev->wi_next = wip->wi_next;
2571 else
2572 buf->b_wininfo = wip->wi_next;
2573 if (wip->wi_next)
2574 wip->wi_next->wi_prev = wip->wi_prev;
2575 if (copy_options && wip->wi_optset)
2576 {
2577 clear_winopt(&wip->wi_opt);
2578#ifdef FEAT_FOLDING
2579 deleteFoldRecurse(&wip->wi_folds);
2580#endif
2581 }
2582 }
2583 if (lnum != 0)
2584 {
2585 wip->wi_fpos.lnum = lnum;
2586 wip->wi_fpos.col = col;
2587 }
2588 if (copy_options)
2589 {
2590 /* Save the window-specific option values. */
2591 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2592#ifdef FEAT_FOLDING
2593 wip->wi_fold_manual = win->w_fold_manual;
2594 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2595#endif
2596 wip->wi_optset = TRUE;
2597 }
2598
2599 /* insert the entry in front of the list */
2600 wip->wi_next = buf->b_wininfo;
2601 buf->b_wininfo = wip;
2602 wip->wi_prev = NULL;
2603 if (wip->wi_next)
2604 wip->wi_next->wi_prev = wip;
2605
2606 return;
2607}
2608
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002609#ifdef FEAT_DIFF
2610static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2611
2612/*
2613 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2614 * page. That's because a diff is local to a tab page.
2615 */
2616 static int
2617wininfo_other_tab_diff(wip)
2618 wininfo_T *wip;
2619{
2620 win_T *wp;
2621
2622 if (wip->wi_opt.wo_diff)
2623 {
2624 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2625 /* return FALSE when it's a window in the current tab page, thus
2626 * the buffer was in diff mode here */
2627 if (wip->wi_win == wp)
2628 return FALSE;
2629 return TRUE;
2630 }
2631 return FALSE;
2632}
2633#endif
2634
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635/*
2636 * Find info for the current window in buffer "buf".
2637 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002638 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2639 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 * Returns NULL when there isn't any info.
2641 */
2642 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002643find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002645 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646{
2647 wininfo_T *wip;
2648
2649 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002650 if (wip->wi_win == curwin
2651#ifdef FEAT_DIFF
2652 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2653#endif
2654 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002656
2657 /* If no wininfo for curwin, use the first in the list (that doesn't have
2658 * 'diff' set and is in another tab page). */
2659 if (wip == NULL)
2660 {
2661#ifdef FEAT_DIFF
2662 if (skip_diff_buffer)
2663 {
2664 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2665 if (!wininfo_other_tab_diff(wip))
2666 break;
2667 }
2668 else
2669#endif
2670 wip = buf->b_wininfo;
2671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 return wip;
2673}
2674
2675/*
2676 * Reset the local window options to the values last used in this window.
2677 * If the buffer wasn't used in this window before, use the values from
2678 * the most recently used window. If the values were never set, use the
2679 * global values for the window.
2680 */
2681 void
2682get_winopts(buf)
2683 buf_T *buf;
2684{
2685 wininfo_T *wip;
2686
2687 clear_winopt(&curwin->w_onebuf_opt);
2688#ifdef FEAT_FOLDING
2689 clearFolding(curwin);
2690#endif
2691
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002692 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693 if (wip != NULL && wip->wi_optset)
2694 {
2695 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2696#ifdef FEAT_FOLDING
2697 curwin->w_fold_manual = wip->wi_fold_manual;
2698 curwin->w_foldinvalid = TRUE;
2699 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2700#endif
2701 }
2702 else
2703 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2704
2705#ifdef FEAT_FOLDING
2706 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2707 if (p_fdls >= 0)
2708 curwin->w_p_fdl = p_fdls;
2709#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002710#ifdef FEAT_SYN_HL
2711 check_colorcolumn(curwin);
2712#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002713}
2714
2715/*
2716 * Find the position (lnum and col) for the buffer 'buf' for the current
2717 * window.
2718 * Returns a pointer to no_position if no position is found.
2719 */
2720 pos_T *
2721buflist_findfpos(buf)
2722 buf_T *buf;
2723{
2724 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002725 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002727 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 if (wip != NULL)
2729 return &(wip->wi_fpos);
2730 else
2731 return &no_position;
2732}
2733
2734/*
2735 * Find the lnum for the buffer 'buf' for the current window.
2736 */
2737 linenr_T
2738buflist_findlnum(buf)
2739 buf_T *buf;
2740{
2741 return buflist_findfpos(buf)->lnum;
2742}
2743
2744#if defined(FEAT_LISTCMDS) || defined(PROTO)
2745/*
2746 * List all know file names (for :files and :buffers command).
2747 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 void
2749buflist_list(eap)
2750 exarg_T *eap;
2751{
2752 buf_T *buf;
2753 int len;
2754 int i;
2755
2756 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2757 {
2758 /* skip unlisted buffers, unless ! was used */
2759 if (!buf->b_p_bl && !eap->forceit)
2760 continue;
2761 msg_putchar('\n');
2762 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002763 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 else
2765 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2766
Bram Moolenaar50cde822005-06-05 21:54:54 +00002767 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 buf->b_fnum,
2769 buf->b_p_bl ? ' ' : 'u',
2770 buf == curbuf ? '%' :
2771 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2772 buf->b_ml.ml_mfp == NULL ? ' ' :
2773 (buf->b_nwindows == 0 ? 'h' : 'a'),
2774 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2775 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002776 : (bufIsChanged(buf) ? '+' : ' '),
2777 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002778
2779 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 i = 40 - vim_strsize(IObuff);
2781 do
2782 {
2783 IObuff[len++] = ' ';
2784 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002785 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2786 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002787 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 msg_outtrans(IObuff);
2789 out_flush(); /* output one line at a time */
2790 ui_breakcheck();
2791 }
2792}
2793#endif
2794
2795/*
2796 * Get file name and line number for file 'fnum'.
2797 * Used by DoOneCmd() for translating '%' and '#'.
2798 * Used by insert_reg() and cmdline_paste() for '#' register.
2799 * Return FAIL if not found, OK for success.
2800 */
2801 int
2802buflist_name_nr(fnum, fname, lnum)
2803 int fnum;
2804 char_u **fname;
2805 linenr_T *lnum;
2806{
2807 buf_T *buf;
2808
2809 buf = buflist_findnr(fnum);
2810 if (buf == NULL || buf->b_fname == NULL)
2811 return FAIL;
2812
2813 *fname = buf->b_fname;
2814 *lnum = buflist_findlnum(buf);
2815
2816 return OK;
2817}
2818
2819/*
2820 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2821 * The file name with the full path is also remembered, for when :cd is used.
2822 * Returns FAIL for failure (file name already in use by other buffer)
2823 * OK otherwise.
2824 */
2825 int
2826setfname(buf, ffname, sfname, message)
2827 buf_T *buf;
2828 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002829 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002830{
Bram Moolenaar81695252004-12-29 20:58:21 +00002831 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832#ifdef UNIX
2833 struct stat st;
2834#endif
2835
2836 if (ffname == NULL || *ffname == NUL)
2837 {
2838 /* Removing the name. */
2839 vim_free(buf->b_ffname);
2840 vim_free(buf->b_sfname);
2841 buf->b_ffname = NULL;
2842 buf->b_sfname = NULL;
2843#ifdef UNIX
2844 st.st_dev = (dev_T)-1;
2845#endif
2846 }
2847 else
2848 {
2849 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2850 if (ffname == NULL) /* out of memory */
2851 return FAIL;
2852
2853 /*
2854 * if the file name is already used in another buffer:
2855 * - if the buffer is loaded, fail
2856 * - if the buffer is not loaded, delete it from the list
2857 */
2858#ifdef UNIX
2859 if (mch_stat((char *)ffname, &st) < 0)
2860 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002861#endif
2862 if (!(buf->b_flags & BF_DUMMY))
2863#ifdef UNIX
2864 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002866 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867#endif
2868 if (obuf != NULL && obuf != buf)
2869 {
2870 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2871 {
2872 if (message)
2873 EMSG(_("E95: Buffer with this name already exists"));
2874 vim_free(ffname);
2875 return FAIL;
2876 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002877 /* delete from the list */
2878 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 }
2880 sfname = vim_strsave(sfname);
2881 if (ffname == NULL || sfname == NULL)
2882 {
2883 vim_free(sfname);
2884 vim_free(ffname);
2885 return FAIL;
2886 }
2887#ifdef USE_FNAME_CASE
2888# ifdef USE_LONG_FNAME
2889 if (USE_LONG_FNAME)
2890# endif
2891 fname_case(sfname, 0); /* set correct case for short file name */
2892#endif
2893 vim_free(buf->b_ffname);
2894 vim_free(buf->b_sfname);
2895 buf->b_ffname = ffname;
2896 buf->b_sfname = sfname;
2897 }
2898 buf->b_fname = buf->b_sfname;
2899#ifdef UNIX
2900 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002901 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 else
2903 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002904 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 buf->b_dev = st.st_dev;
2906 buf->b_ino = st.st_ino;
2907 }
2908#endif
2909
2910#ifndef SHORT_FNAME
2911 buf->b_shortname = FALSE;
2912#endif
2913
2914 buf_name_changed(buf);
2915 return OK;
2916}
2917
2918/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002919 * Crude way of changing the name of a buffer. Use with care!
2920 * The name should be relative to the current directory.
2921 */
2922 void
2923buf_set_name(fnum, name)
2924 int fnum;
2925 char_u *name;
2926{
2927 buf_T *buf;
2928
2929 buf = buflist_findnr(fnum);
2930 if (buf != NULL)
2931 {
2932 vim_free(buf->b_sfname);
2933 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002934 buf->b_ffname = vim_strsave(name);
2935 buf->b_sfname = NULL;
2936 /* Allocate ffname and expand into full path. Also resolves .lnk
2937 * files on Win32. */
2938 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002939 buf->b_fname = buf->b_sfname;
2940 }
2941}
2942
2943/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944 * Take care of what needs to be done when the name of buffer "buf" has
2945 * changed.
2946 */
2947 void
2948buf_name_changed(buf)
2949 buf_T *buf;
2950{
2951 /*
2952 * If the file name changed, also change the name of the swapfile
2953 */
2954 if (buf->b_ml.ml_mfp != NULL)
2955 ml_setname(buf);
2956
2957 if (curwin->w_buffer == buf)
2958 check_arg_idx(curwin); /* check file name for arg list */
2959#ifdef FEAT_TITLE
2960 maketitle(); /* set window title */
2961#endif
2962#ifdef FEAT_WINDOWS
2963 status_redraw_all(); /* status lines need to be redrawn */
2964#endif
2965 fmarks_check_names(buf); /* check named file marks */
2966 ml_timestamp(buf); /* reset timestamp */
2967}
2968
2969/*
2970 * set alternate file name for current window
2971 *
2972 * Used by do_one_cmd(), do_write() and do_ecmd().
2973 * Return the buffer.
2974 */
2975 buf_T *
2976setaltfname(ffname, sfname, lnum)
2977 char_u *ffname;
2978 char_u *sfname;
2979 linenr_T lnum;
2980{
2981 buf_T *buf;
2982
2983 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2984 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002985 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 curwin->w_alt_fnum = buf->b_fnum;
2987 return buf;
2988}
2989
2990/*
2991 * Get alternate file name for current window.
2992 * Return NULL if there isn't any, and give error message if requested.
2993 */
2994 char_u *
2995getaltfname(errmsg)
2996 int errmsg; /* give error message */
2997{
2998 char_u *fname;
2999 linenr_T dummy;
3000
3001 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3002 {
3003 if (errmsg)
3004 EMSG(_(e_noalt));
3005 return NULL;
3006 }
3007 return fname;
3008}
3009
3010/*
3011 * Add a file name to the buflist and return its number.
3012 * Uses same flags as buflist_new(), except BLN_DUMMY.
3013 *
3014 * used by qf_init(), main() and doarglist()
3015 */
3016 int
3017buflist_add(fname, flags)
3018 char_u *fname;
3019 int flags;
3020{
3021 buf_T *buf;
3022
3023 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3024 if (buf != NULL)
3025 return buf->b_fnum;
3026 return 0;
3027}
3028
3029#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3030/*
3031 * Adjust slashes in file names. Called after 'shellslash' was set.
3032 */
3033 void
3034buflist_slash_adjust()
3035{
3036 buf_T *bp;
3037
3038 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3039 {
3040 if (bp->b_ffname != NULL)
3041 slash_adjust(bp->b_ffname);
3042 if (bp->b_sfname != NULL)
3043 slash_adjust(bp->b_sfname);
3044 }
3045}
3046#endif
3047
3048/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003049 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 * Also save the local window option values.
3051 */
3052 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003053buflist_altfpos(win)
3054 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003056 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057}
3058
3059/*
3060 * Return TRUE if 'ffname' is not the same file as current file.
3061 * Fname must have a full path (expanded by mch_FullName()).
3062 */
3063 int
3064otherfile(ffname)
3065 char_u *ffname;
3066{
3067 return otherfile_buf(curbuf, ffname
3068#ifdef UNIX
3069 , NULL
3070#endif
3071 );
3072}
3073
3074 static int
3075otherfile_buf(buf, ffname
3076#ifdef UNIX
3077 , stp
3078#endif
3079 )
3080 buf_T *buf;
3081 char_u *ffname;
3082#ifdef UNIX
3083 struct stat *stp;
3084#endif
3085{
3086 /* no name is different */
3087 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3088 return TRUE;
3089 if (fnamecmp(ffname, buf->b_ffname) == 0)
3090 return FALSE;
3091#ifdef UNIX
3092 {
3093 struct stat st;
3094
3095 /* If no struct stat given, get it now */
3096 if (stp == NULL)
3097 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003098 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 st.st_dev = (dev_T)-1;
3100 stp = &st;
3101 }
3102 /* Use dev/ino to check if the files are the same, even when the names
3103 * are different (possible with links). Still need to compare the
3104 * name above, for when the file doesn't exist yet.
3105 * Problem: The dev/ino changes when a file is deleted (and created
3106 * again) and remains the same when renamed/moved. We don't want to
3107 * mch_stat() each buffer each time, that would be too slow. Get the
3108 * dev/ino again when they appear to match, but not when they appear
3109 * to be different: Could skip a buffer when it's actually the same
3110 * file. */
3111 if (buf_same_ino(buf, stp))
3112 {
3113 buf_setino(buf);
3114 if (buf_same_ino(buf, stp))
3115 return FALSE;
3116 }
3117 }
3118#endif
3119 return TRUE;
3120}
3121
3122#if defined(UNIX) || defined(PROTO)
3123/*
3124 * Set inode and device number for a buffer.
3125 * Must always be called when b_fname is changed!.
3126 */
3127 void
3128buf_setino(buf)
3129 buf_T *buf;
3130{
3131 struct stat st;
3132
3133 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3134 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003135 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 buf->b_dev = st.st_dev;
3137 buf->b_ino = st.st_ino;
3138 }
3139 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003140 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141}
3142
3143/*
3144 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3145 */
3146 static int
3147buf_same_ino(buf, stp)
3148 buf_T *buf;
3149 struct stat *stp;
3150{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003151 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 && stp->st_dev == buf->b_dev
3153 && stp->st_ino == buf->b_ino);
3154}
3155#endif
3156
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003157/*
3158 * Print info about the current buffer.
3159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 void
3161fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003162 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 int shorthelp;
3164 int dont_truncate;
3165{
3166 char_u *name;
3167 int n;
3168 char_u *p;
3169 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003170 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171
3172 buffer = alloc(IOSIZE);
3173 if (buffer == NULL)
3174 return;
3175
3176 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3177 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003178 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 p = buffer + STRLEN(buffer);
3180 }
3181 else
3182 p = buffer;
3183
3184 *p++ = '"';
3185 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003186 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 else
3188 {
3189 if (!fullname && curbuf->b_fname != NULL)
3190 name = curbuf->b_fname;
3191 else
3192 name = curbuf->b_ffname;
3193 home_replace(shorthelp ? curbuf : NULL, name, p,
3194 (int)(IOSIZE - (p - buffer)), TRUE);
3195 }
3196
Bram Moolenaara800b422010-06-27 01:15:55 +02003197 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 curbufIsChanged() ? (shortmess(SHM_MOD)
3199 ? " [+]" : _(" [Modified]")) : " ",
3200 (curbuf->b_flags & BF_NOTEDITED)
3201#ifdef FEAT_QUICKFIX
3202 && !bt_dontwrite(curbuf)
3203#endif
3204 ? _("[Not edited]") : "",
3205 (curbuf->b_flags & BF_NEW)
3206#ifdef FEAT_QUICKFIX
3207 && !bt_dontwrite(curbuf)
3208#endif
3209 ? _("[New file]") : "",
3210 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003211 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 : _("[readonly]")) : "",
3213 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3214 || curbuf->b_p_ro) ?
3215 " " : "");
3216 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3217 * causes an overflow, thus for large numbers divide instead. */
3218 if (curwin->w_cursor.lnum > 1000000L)
3219 n = (int)(((long)curwin->w_cursor.lnum) /
3220 ((long)curbuf->b_ml.ml_line_count / 100L));
3221 else
3222 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3223 (long)curbuf->b_ml.ml_line_count);
3224 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3225 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003226 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 }
3228#ifdef FEAT_CMDL_INFO
3229 else if (p_ru)
3230 {
3231 /* Current line and column are already on the screen -- webb */
3232 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003233 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003235 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 (long)curbuf->b_ml.ml_line_count, n);
3237 }
3238#endif
3239 else
3240 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003241 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 _("line %ld of %ld --%d%%-- col "),
3243 (long)curwin->w_cursor.lnum,
3244 (long)curbuf->b_ml.ml_line_count,
3245 n);
3246 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003247 len = STRLEN(buffer);
3248 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3250 }
3251
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003252 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253
3254 if (dont_truncate)
3255 {
3256 /* Temporarily set msg_scroll to avoid the message being truncated.
3257 * First call msg_start() to get the message in the right place. */
3258 msg_start();
3259 n = msg_scroll;
3260 msg_scroll = TRUE;
3261 msg(buffer);
3262 msg_scroll = n;
3263 }
3264 else
3265 {
3266 p = msg_trunc_attr(buffer, FALSE, 0);
3267 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 /* Need to repeat the message after redrawing when:
3269 * - When restart_edit is set (otherwise there will be a delay
3270 * before redrawing).
3271 * - When the screen was scrolled but there is no wait-return
3272 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003273 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003274 }
3275
3276 vim_free(buffer);
3277}
3278
3279 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003280col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003282 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 int col;
3284 int vcol;
3285{
3286 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003287 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003289 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290}
3291
3292#if defined(FEAT_TITLE) || defined(PROTO)
3293/*
3294 * put file name in title bar of window and in icon title
3295 */
3296
3297static char_u *lasttitle = NULL;
3298static char_u *lasticon = NULL;
3299
3300 void
3301maketitle()
3302{
3303 char_u *p;
3304 char_u *t_str = NULL;
3305 char_u *i_name;
3306 char_u *i_str = NULL;
3307 int maxlen = 0;
3308 int len;
3309 int mustset;
3310 char_u buf[IOSIZE];
3311 int off;
3312
3313 if (!redrawing())
3314 {
3315 /* Postpone updating the title when 'lazyredraw' is set. */
3316 need_maketitle = TRUE;
3317 return;
3318 }
3319
3320 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003321 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 return;
3323
3324 if (p_title)
3325 {
3326 if (p_titlelen > 0)
3327 {
3328 maxlen = p_titlelen * Columns / 100;
3329 if (maxlen < 10)
3330 maxlen = 10;
3331 }
3332
3333 t_str = buf;
3334 if (*p_titlestring != NUL)
3335 {
3336#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003337 if (stl_syntax & STL_IN_TITLE)
3338 {
3339 int use_sandbox = FALSE;
3340 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003341
3342# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003343 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003344# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003345 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003347 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003348 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003349 if (called_emsg)
3350 set_string_option_direct((char_u *)"titlestring", -1,
3351 (char_u *)"", OPT_FREE, SID_ERROR);
3352 called_emsg |= save_called_emsg;
3353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 else
3355#endif
3356 t_str = p_titlestring;
3357 }
3358 else
3359 {
3360 /* format: "fname + (path) (1 of 2) - VIM" */
3361
Bram Moolenaar2c666692012-09-05 13:30:40 +02003362#define SPACE_FOR_FNAME (IOSIZE - 100)
3363#define SPACE_FOR_DIR (IOSIZE - 20)
3364#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003366 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 else
3368 {
3369 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003370 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 }
3373
3374 switch (bufIsChanged(curbuf)
3375 + (curbuf->b_p_ro * 2)
3376 + (!curbuf->b_p_ma * 4))
3377 {
3378 case 1: STRCAT(buf, " +"); break;
3379 case 2: STRCAT(buf, " ="); break;
3380 case 3: STRCAT(buf, " =+"); break;
3381 case 4:
3382 case 6: STRCAT(buf, " -"); break;
3383 case 5:
3384 case 7: STRCAT(buf, " -+"); break;
3385 }
3386
3387 if (curbuf->b_fname != NULL)
3388 {
3389 /* Get path of file, replace home dir with ~ */
3390 off = (int)STRLEN(buf);
3391 buf[off++] = ' ';
3392 buf[off++] = '(';
3393 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003394 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#ifdef BACKSLASH_IN_FILENAME
3396 /* avoid "c:/name" to be reduced to "c" */
3397 if (isalpha(buf[off]) && buf[off + 1] == ':')
3398 off += 2;
3399#endif
3400 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003401 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003404 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003405 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003408
Bram Moolenaar2c666692012-09-05 13:30:40 +02003409 /* Translate unprintable chars and concatenate. Keep some
3410 * room for the server name. When there is no room (very long
3411 * file name) use (...). */
3412 if (off < SPACE_FOR_DIR)
3413 {
3414 p = transstr(buf + off);
3415 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3416 vim_free(p);
3417 }
3418 else
3419 {
3420 vim_strncpy(buf + off, (char_u *)"...",
3421 (size_t)(SPACE_FOR_ARGNR - off));
3422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423 STRCAT(buf, ")");
3424 }
3425
Bram Moolenaar2c666692012-09-05 13:30:40 +02003426 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
3428#if defined(FEAT_CLIENTSERVER)
3429 if (serverName != NULL)
3430 {
3431 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003432 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 }
3434 else
3435#endif
3436 STRCAT(buf, " - VIM");
3437
3438 if (maxlen > 0)
3439 {
3440 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003441 if (vim_strsize(buf) > maxlen)
3442 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443 }
3444 }
3445 }
3446 mustset = ti_change(t_str, &lasttitle);
3447
3448 if (p_icon)
3449 {
3450 i_str = buf;
3451 if (*p_iconstring != NUL)
3452 {
3453#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003454 if (stl_syntax & STL_IN_ICON)
3455 {
3456 int use_sandbox = FALSE;
3457 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003458
3459# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003460 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003461# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003462 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003464 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003465 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003466 if (called_emsg)
3467 set_string_option_direct((char_u *)"iconstring", -1,
3468 (char_u *)"", OPT_FREE, SID_ERROR);
3469 called_emsg |= save_called_emsg;
3470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 else
3472#endif
3473 i_str = p_iconstring;
3474 }
3475 else
3476 {
3477 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003478 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 else /* use file name only in icon */
3480 i_name = gettail(curbuf->b_ffname);
3481 *i_str = NUL;
3482 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003483 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (len > 100)
3485 {
3486 len -= 100;
3487#ifdef FEAT_MBYTE
3488 if (has_mbyte)
3489 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3490#endif
3491 i_name += len;
3492 }
3493 STRCPY(i_str, i_name);
3494 trans_characters(i_str, IOSIZE);
3495 }
3496 }
3497
3498 mustset |= ti_change(i_str, &lasticon);
3499
3500 if (mustset)
3501 resettitle();
3502}
3503
3504/*
3505 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3506 * from "str" if it does.
3507 * Return TRUE when "*last" changed.
3508 */
3509 static int
3510ti_change(str, last)
3511 char_u *str;
3512 char_u **last;
3513{
3514 if ((str == NULL) != (*last == NULL)
3515 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3516 {
3517 vim_free(*last);
3518 if (str == NULL)
3519 *last = NULL;
3520 else
3521 *last = vim_strsave(str);
3522 return TRUE;
3523 }
3524 return FALSE;
3525}
3526
3527/*
3528 * Put current window title back (used after calling a shell)
3529 */
3530 void
3531resettitle()
3532{
3533 mch_settitle(lasttitle, lasticon);
3534}
Bram Moolenaarea408852005-06-25 22:49:46 +00003535
3536# if defined(EXITFREE) || defined(PROTO)
3537 void
3538free_titles()
3539{
3540 vim_free(lasttitle);
3541 vim_free(lasticon);
3542}
3543# endif
3544
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545#endif /* FEAT_TITLE */
3546
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003547#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003549 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 * Return length of string in screen cells.
3551 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003552 * Normally works for window "wp", except when working for 'tabline' then it
3553 * is "curwin".
3554 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 * Items are drawn interspersed with the text that surrounds it
3556 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3557 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3558 *
3559 * If maxwidth is not zero, the string will be filled at any middle marker
3560 * or truncated if too long, fillchar is used for all whitespace.
3561 */
3562 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003563build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3564 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003566 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 size_t outlen; /* length of out[] */
3568 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003569 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 int fillchar;
3571 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003572 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3573 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574{
3575 char_u *p;
3576 char_u *s;
3577 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003578 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579#ifdef FEAT_EVAL
3580 win_T *o_curwin;
3581 buf_T *o_curbuf;
3582#endif
3583 int empty_line;
3584 colnr_T virtcol;
3585 long l;
3586 long n;
3587 int prevchar_isflag;
3588 int prevchar_isitem;
3589 int itemisflag;
3590 int fillable;
3591 char_u *str;
3592 long num;
3593 int width;
3594 int itemcnt;
3595 int curitem;
3596 int groupitem[STL_MAX_ITEM];
3597 int groupdepth;
3598 struct stl_item
3599 {
3600 char_u *start;
3601 int minwid;
3602 int maxwid;
3603 enum
3604 {
3605 Normal,
3606 Empty,
3607 Group,
3608 Middle,
3609 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003610 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 Trunc
3612 } type;
3613 } item[STL_MAX_ITEM];
3614 int minwid;
3615 int maxwid;
3616 int zeropad;
3617 char_u base;
3618 char_u opt;
3619#define TMPLEN 70
3620 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003621 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003622 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003623
3624#ifdef FEAT_EVAL
3625 /*
3626 * When the format starts with "%!" then evaluate it as an expression and
3627 * use the result as the actual format string.
3628 */
3629 if (fmt[0] == '%' && fmt[1] == '!')
3630 {
3631 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3632 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003633 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003634 }
3635#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 if (fillchar == 0)
3638 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003639#ifdef FEAT_MBYTE
3640 /* Can't handle a multi-byte fill character yet. */
3641 else if (mb_char2len(fillchar) > 1)
3642 fillchar = '-';
3643#endif
3644
Bram Moolenaar567199b2013-04-24 16:52:36 +02003645 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3646 * p will become invalid when getting another buffer line. */
3647 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3648 empty_line = (*p == NUL);
3649
3650 /* Get the byte value now, in case we need it below. This is more
3651 * efficient than making a copy of the line. */
3652 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3653 byteval = 0;
3654 else
3655#ifdef FEAT_MBYTE
3656 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3657#else
3658 byteval = p[wp->w_cursor.col];
3659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
3661 groupdepth = 0;
3662 p = out;
3663 curitem = 0;
3664 prevchar_isflag = TRUE;
3665 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003666 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003668 if (curitem == STL_MAX_ITEM)
3669 {
3670 /* There are too many items. Add the error code to the statusline
3671 * to give the user a hint about what went wrong. */
3672 if (p + 6 < out + outlen)
3673 {
3674 mch_memmove(p, " E541", (size_t)5);
3675 p += 5;
3676 }
3677 break;
3678 }
3679
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 if (*s != NUL && *s != '%')
3681 prevchar_isflag = prevchar_isitem = FALSE;
3682
3683 /*
3684 * Handle up to the next '%' or the end.
3685 */
3686 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3687 *p++ = *s++;
3688 if (*s == NUL || p + 1 >= out + outlen)
3689 break;
3690
3691 /*
3692 * Handle one '%' item.
3693 */
3694 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003695 if (*s == NUL) /* ignore trailing % */
3696 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (*s == '%')
3698 {
3699 if (p + 1 >= out + outlen)
3700 break;
3701 *p++ = *s++;
3702 prevchar_isflag = prevchar_isitem = FALSE;
3703 continue;
3704 }
3705 if (*s == STL_MIDDLEMARK)
3706 {
3707 s++;
3708 if (groupdepth > 0)
3709 continue;
3710 item[curitem].type = Middle;
3711 item[curitem++].start = p;
3712 continue;
3713 }
3714 if (*s == STL_TRUNCMARK)
3715 {
3716 s++;
3717 item[curitem].type = Trunc;
3718 item[curitem++].start = p;
3719 continue;
3720 }
3721 if (*s == ')')
3722 {
3723 s++;
3724 if (groupdepth < 1)
3725 continue;
3726 groupdepth--;
3727
3728 t = item[groupitem[groupdepth]].start;
3729 *p = NUL;
3730 l = vim_strsize(t);
3731 if (curitem > groupitem[groupdepth] + 1
3732 && item[groupitem[groupdepth]].minwid == 0)
3733 {
3734 /* remove group if all items are empty */
3735 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3736 if (item[n].type == Normal)
3737 break;
3738 if (n == curitem)
3739 {
3740 p = t;
3741 l = 0;
3742 }
3743 }
3744 if (l > item[groupitem[groupdepth]].maxwid)
3745 {
3746 /* truncate, remove n bytes of text at the start */
3747#ifdef FEAT_MBYTE
3748 if (has_mbyte)
3749 {
3750 /* Find the first character that should be included. */
3751 n = 0;
3752 while (l >= item[groupitem[groupdepth]].maxwid)
3753 {
3754 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003755 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 }
3757 }
3758 else
3759#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003760 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761
3762 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003763 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 p = p - n + 1;
3765#ifdef FEAT_MBYTE
3766 /* Fill up space left over by half a double-wide char. */
3767 while (++l < item[groupitem[groupdepth]].minwid)
3768 *p++ = fillchar;
3769#endif
3770
3771 /* correct the start of the items for the truncation */
3772 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3773 {
3774 item[l].start -= n;
3775 if (item[l].start < t)
3776 item[l].start = t;
3777 }
3778 }
3779 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3780 {
3781 /* fill */
3782 n = item[groupitem[groupdepth]].minwid;
3783 if (n < 0)
3784 {
3785 /* fill by appending characters */
3786 n = 0 - n;
3787 while (l++ < n && p + 1 < out + outlen)
3788 *p++ = fillchar;
3789 }
3790 else
3791 {
3792 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003793 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 l = n - l;
3795 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003796 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 p += l;
3798 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3799 item[n].start += l;
3800 for ( ; l > 0; l--)
3801 *t++ = fillchar;
3802 }
3803 }
3804 continue;
3805 }
3806 minwid = 0;
3807 maxwid = 9999;
3808 zeropad = FALSE;
3809 l = 1;
3810 if (*s == '0')
3811 {
3812 s++;
3813 zeropad = TRUE;
3814 }
3815 if (*s == '-')
3816 {
3817 s++;
3818 l = -1;
3819 }
3820 if (VIM_ISDIGIT(*s))
3821 {
3822 minwid = (int)getdigits(&s);
3823 if (minwid < 0) /* overflow */
3824 minwid = 0;
3825 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003826 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 {
3828 item[curitem].type = Highlight;
3829 item[curitem].start = p;
3830 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3831 s++;
3832 curitem++;
3833 continue;
3834 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003835 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3836 {
3837 if (*s == STL_TABCLOSENR)
3838 {
3839 if (minwid == 0)
3840 {
3841 /* %X ends the close label, go back to the previously
3842 * define tab label nr. */
3843 for (n = curitem - 1; n >= 0; --n)
3844 if (item[n].type == TabPage && item[n].minwid >= 0)
3845 {
3846 minwid = item[n].minwid;
3847 break;
3848 }
3849 }
3850 else
3851 /* close nrs are stored as negative values */
3852 minwid = - minwid;
3853 }
3854 item[curitem].type = TabPage;
3855 item[curitem].start = p;
3856 item[curitem].minwid = minwid;
3857 s++;
3858 curitem++;
3859 continue;
3860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (*s == '.')
3862 {
3863 s++;
3864 if (VIM_ISDIGIT(*s))
3865 {
3866 maxwid = (int)getdigits(&s);
3867 if (maxwid <= 0) /* overflow */
3868 maxwid = 50;
3869 }
3870 }
3871 minwid = (minwid > 50 ? 50 : minwid) * l;
3872 if (*s == '(')
3873 {
3874 groupitem[groupdepth++] = curitem;
3875 item[curitem].type = Group;
3876 item[curitem].start = p;
3877 item[curitem].minwid = minwid;
3878 item[curitem].maxwid = maxwid;
3879 s++;
3880 curitem++;
3881 continue;
3882 }
3883 if (vim_strchr(STL_ALL, *s) == NULL)
3884 {
3885 s++;
3886 continue;
3887 }
3888 opt = *s++;
3889
3890 /* OK - now for the real work */
3891 base = 'D';
3892 itemisflag = FALSE;
3893 fillable = TRUE;
3894 num = -1;
3895 str = NULL;
3896 switch (opt)
3897 {
3898 case STL_FILEPATH:
3899 case STL_FULLPATH:
3900 case STL_FILENAME:
3901 fillable = FALSE; /* don't change ' ' to fillchar */
3902 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003903 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 else
3905 {
3906 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003907 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3909 }
3910 trans_characters(NameBuff, MAXPATHL);
3911 if (opt != STL_FILENAME)
3912 str = NameBuff;
3913 else
3914 str = gettail(NameBuff);
3915 break;
3916
3917 case STL_VIM_EXPR: /* '{' */
3918 itemisflag = TRUE;
3919 t = p;
3920 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3921 *p++ = *s++;
3922 if (*s != '}') /* missing '}' or out of space */
3923 break;
3924 s++;
3925 *p = 0;
3926 p = t;
3927
3928#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003929 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3931
3932 o_curbuf = curbuf;
3933 o_curwin = curwin;
3934 curwin = wp;
3935 curbuf = wp->w_buffer;
3936
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003937 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938
3939 curwin = o_curwin;
3940 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003941 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942
3943 if (str != NULL && *str != 0)
3944 {
3945 if (*skipdigits(str) == NUL)
3946 {
3947 num = atoi((char *)str);
3948 vim_free(str);
3949 str = NULL;
3950 itemisflag = FALSE;
3951 }
3952 }
3953#endif
3954 break;
3955
3956 case STL_LINE:
3957 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3958 ? 0L : (long)(wp->w_cursor.lnum);
3959 break;
3960
3961 case STL_NUMLINES:
3962 num = wp->w_buffer->b_ml.ml_line_count;
3963 break;
3964
3965 case STL_COLUMN:
3966 num = !(State & INSERT) && empty_line
3967 ? 0 : (int)wp->w_cursor.col + 1;
3968 break;
3969
3970 case STL_VIRTCOL:
3971 case STL_VIRTCOL_ALT:
3972 /* In list mode virtcol needs to be recomputed */
3973 virtcol = wp->w_virtcol;
3974 if (wp->w_p_list && lcs_tab1 == NUL)
3975 {
3976 wp->w_p_list = FALSE;
3977 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3978 wp->w_p_list = TRUE;
3979 }
3980 ++virtcol;
3981 /* Don't display %V if it's the same as %c. */
3982 if (opt == STL_VIRTCOL_ALT
3983 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3984 ? 0 : (int)wp->w_cursor.col + 1)))
3985 break;
3986 num = (long)virtcol;
3987 break;
3988
3989 case STL_PERCENTAGE:
3990 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3991 (long)wp->w_buffer->b_ml.ml_line_count);
3992 break;
3993
3994 case STL_ALTPERCENT:
3995 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003996 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 break;
3998
3999 case STL_ARGLISTSTAT:
4000 fillable = FALSE;
4001 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004002 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 str = tmp;
4004 break;
4005
4006 case STL_KEYMAP:
4007 fillable = FALSE;
4008 if (get_keymap_str(wp, tmp, TMPLEN))
4009 str = tmp;
4010 break;
4011 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004012#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004013 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014#else
4015 num = 0;
4016#endif
4017 break;
4018
4019 case STL_BUFNO:
4020 num = wp->w_buffer->b_fnum;
4021 break;
4022
4023 case STL_OFFSET_X:
4024 base = 'X';
4025 case STL_OFFSET:
4026#ifdef FEAT_BYTEOFF
4027 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4028 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4029 0L : l + 1 + (!(State & INSERT) && empty_line ?
4030 0 : (int)wp->w_cursor.col);
4031#endif
4032 break;
4033
4034 case STL_BYTEVAL_X:
4035 base = 'X';
4036 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004037 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (num == NL)
4039 num = 0;
4040 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4041 num = NL;
4042 break;
4043
4044 case STL_ROFLAG:
4045 case STL_ROFLAG_ALT:
4046 itemisflag = TRUE;
4047 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004048 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 break;
4050
4051 case STL_HELPFLAG:
4052 case STL_HELPFLAG_ALT:
4053 itemisflag = TRUE;
4054 if (wp->w_buffer->b_help)
4055 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004056 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 break;
4058
4059#ifdef FEAT_AUTOCMD
4060 case STL_FILETYPE:
4061 if (*wp->w_buffer->b_p_ft != NUL
4062 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4063 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004064 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4065 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 str = tmp;
4067 }
4068 break;
4069
4070 case STL_FILETYPE_ALT:
4071 itemisflag = TRUE;
4072 if (*wp->w_buffer->b_p_ft != NUL
4073 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4074 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004075 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4076 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 for (t = tmp; *t != 0; t++)
4078 *t = TOUPPER_LOC(*t);
4079 str = tmp;
4080 }
4081 break;
4082#endif
4083
4084#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4085 case STL_PREVIEWFLAG:
4086 case STL_PREVIEWFLAG_ALT:
4087 itemisflag = TRUE;
4088 if (wp->w_p_pvw)
4089 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4090 : _("[Preview]"));
4091 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004092
4093 case STL_QUICKFIX:
4094 if (bt_quickfix(wp->w_buffer))
4095 str = (char_u *)(wp->w_llist_ref
4096 ? _(msg_loclist)
4097 : _(msg_qflist));
4098 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099#endif
4100
4101 case STL_MODIFIED:
4102 case STL_MODIFIED_ALT:
4103 itemisflag = TRUE;
4104 switch ((opt == STL_MODIFIED_ALT)
4105 + bufIsChanged(wp->w_buffer) * 2
4106 + (!wp->w_buffer->b_p_ma) * 4)
4107 {
4108 case 2: str = (char_u *)"[+]"; break;
4109 case 3: str = (char_u *)",+"; break;
4110 case 4: str = (char_u *)"[-]"; break;
4111 case 5: str = (char_u *)",-"; break;
4112 case 6: str = (char_u *)"[+-]"; break;
4113 case 7: str = (char_u *)",+-"; break;
4114 }
4115 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004116
4117 case STL_HIGHLIGHT:
4118 t = s;
4119 while (*s != '#' && *s != NUL)
4120 ++s;
4121 if (*s == '#')
4122 {
4123 item[curitem].type = Highlight;
4124 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004125 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004126 curitem++;
4127 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004128 if (*s != NUL)
4129 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004130 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 }
4132
4133 item[curitem].start = p;
4134 item[curitem].type = Normal;
4135 if (str != NULL && *str)
4136 {
4137 t = str;
4138 if (itemisflag)
4139 {
4140 if ((t[0] && t[1])
4141 && ((!prevchar_isitem && *t == ',')
4142 || (prevchar_isflag && *t == ' ')))
4143 t++;
4144 prevchar_isflag = TRUE;
4145 }
4146 l = vim_strsize(t);
4147 if (l > 0)
4148 prevchar_isitem = TRUE;
4149 if (l > maxwid)
4150 {
4151 while (l >= maxwid)
4152#ifdef FEAT_MBYTE
4153 if (has_mbyte)
4154 {
4155 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004156 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 }
4158 else
4159#endif
4160 l -= byte2cells(*t++);
4161 if (p + 1 >= out + outlen)
4162 break;
4163 *p++ = '<';
4164 }
4165 if (minwid > 0)
4166 {
4167 for (; l < minwid && p + 1 < out + outlen; l++)
4168 {
4169 /* Don't put a "-" in front of a digit. */
4170 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4171 *p++ = ' ';
4172 else
4173 *p++ = fillchar;
4174 }
4175 minwid = 0;
4176 }
4177 else
4178 minwid *= -1;
4179 while (*t && p + 1 < out + outlen)
4180 {
4181 *p++ = *t++;
4182 /* Change a space by fillchar, unless fillchar is '-' and a
4183 * digit follows. */
4184 if (fillable && p[-1] == ' '
4185 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4186 p[-1] = fillchar;
4187 }
4188 for (; l < minwid && p + 1 < out + outlen; l++)
4189 *p++ = fillchar;
4190 }
4191 else if (num >= 0)
4192 {
4193 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4194 char_u nstr[20];
4195
4196 if (p + 20 >= out + outlen)
4197 break; /* not sufficient space */
4198 prevchar_isitem = TRUE;
4199 t = nstr;
4200 if (opt == STL_VIRTCOL_ALT)
4201 {
4202 *t++ = '-';
4203 minwid--;
4204 }
4205 *t++ = '%';
4206 if (zeropad)
4207 *t++ = '0';
4208 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004209 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 *t = 0;
4211
4212 for (n = num, l = 1; n >= nbase; n /= nbase)
4213 l++;
4214 if (opt == STL_VIRTCOL_ALT)
4215 l++;
4216 if (l > maxwid)
4217 {
4218 l += 2;
4219 n = l - maxwid;
4220 while (l-- > maxwid)
4221 num /= nbase;
4222 *t++ = '>';
4223 *t++ = '%';
4224 *t = t[-3];
4225 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004226 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4227 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 }
4229 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004230 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4231 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 p += STRLEN(p);
4233 }
4234 else
4235 item[curitem].type = Empty;
4236
4237 if (opt == STL_VIM_EXPR)
4238 vim_free(str);
4239
4240 if (num >= 0 || (!itemisflag && str && *str))
4241 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4242 curitem++;
4243 }
4244 *p = NUL;
4245 itemcnt = curitem;
4246
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004247#ifdef FEAT_EVAL
4248 if (usefmt != fmt)
4249 vim_free(usefmt);
4250#endif
4251
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 width = vim_strsize(out);
4253 if (maxwidth > 0 && width > maxwidth)
4254 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004255 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 l = 0;
4257 if (itemcnt == 0)
4258 s = out;
4259 else
4260 {
4261 for ( ; l < itemcnt; l++)
4262 if (item[l].type == Trunc)
4263 {
4264 /* Truncate at %< item. */
4265 s = item[l].start;
4266 break;
4267 }
4268 if (l == itemcnt)
4269 {
4270 /* No %< item, truncate first item. */
4271 s = item[0].start;
4272 l = 0;
4273 }
4274 }
4275
4276 if (width - vim_strsize(s) >= maxwidth)
4277 {
4278 /* Truncation mark is beyond max length */
4279#ifdef FEAT_MBYTE
4280 if (has_mbyte)
4281 {
4282 s = out;
4283 width = 0;
4284 for (;;)
4285 {
4286 width += ptr2cells(s);
4287 if (width >= maxwidth)
4288 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004289 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 /* Fill up for half a double-wide character. */
4292 while (++width < maxwidth)
4293 *s++ = fillchar;
4294 }
4295 else
4296#endif
4297 s = out + maxwidth - 1;
4298 for (l = 0; l < itemcnt; l++)
4299 if (item[l].start > s)
4300 break;
4301 itemcnt = l;
4302 *s++ = '>';
4303 *s = 0;
4304 }
4305 else
4306 {
4307#ifdef FEAT_MBYTE
4308 if (has_mbyte)
4309 {
4310 n = 0;
4311 while (width >= maxwidth)
4312 {
4313 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004314 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 }
4317 else
4318#endif
4319 n = width - maxwidth + 1;
4320 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004321 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 *s = '<';
4323
4324 /* Fill up for half a double-wide character. */
4325 while (++width < maxwidth)
4326 {
4327 s = s + STRLEN(s);
4328 *s++ = fillchar;
4329 *s = NUL;
4330 }
4331
4332 --n; /* count the '<' */
4333 for (; l < itemcnt; l++)
4334 {
4335 if (item[l].start - n >= s)
4336 item[l].start -= n;
4337 else
4338 item[l].start = s;
4339 }
4340 }
4341 width = maxwidth;
4342 }
4343 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4344 {
4345 /* Apply STL_MIDDLE if any */
4346 for (l = 0; l < itemcnt; l++)
4347 if (item[l].type == Middle)
4348 break;
4349 if (l < itemcnt)
4350 {
4351 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004352 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 for (s = item[l].start; s < p; s++)
4354 *s = fillchar;
4355 for (l++; l < itemcnt; l++)
4356 item[l].start += maxwidth - width;
4357 width = maxwidth;
4358 }
4359 }
4360
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004361 /* Store the info about highlighting. */
4362 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004364 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 for (l = 0; l < itemcnt; l++)
4366 {
4367 if (item[l].type == Highlight)
4368 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004369 sp->start = item[l].start;
4370 sp->userhl = item[l].minwid;
4371 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 }
4373 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004374 sp->start = NULL;
4375 sp->userhl = 0;
4376 }
4377
4378 /* Store the info about tab pages labels. */
4379 if (tabtab != NULL)
4380 {
4381 sp = tabtab;
4382 for (l = 0; l < itemcnt; l++)
4383 {
4384 if (item[l].type == TabPage)
4385 {
4386 sp->start = item[l].start;
4387 sp->userhl = item[l].minwid;
4388 sp++;
4389 }
4390 }
4391 sp->start = NULL;
4392 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 }
4394
4395 return width;
4396}
4397#endif /* FEAT_STL_OPT */
4398
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004399#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4400 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004402 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4403 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 */
4405 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004406get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004408 char_u *buf;
4409 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410{
4411 long above; /* number of lines above window */
4412 long below; /* number of lines below window */
4413
Bram Moolenaar0027c212015-01-07 13:31:52 +01004414 if (buflen < 3) /* need at least 3 chars for writing */
4415 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 above = wp->w_topline - 1;
4417#ifdef FEAT_DIFF
4418 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4419#endif
4420 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4421 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004422 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4423 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004425 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004427 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 ? (int)(above / ((above + below) / 100L))
4429 : (int)(above * 100L / (above + below)));
4430}
4431#endif
4432
4433/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004434 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 * Return TRUE if it was appended.
4436 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004437 static int
4438append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 win_T *wp;
4440 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004441 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443{
4444 char_u *p;
4445
4446 if (ARGCOUNT <= 1) /* nothing to do */
4447 return FALSE;
4448
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004449 p = buf + STRLEN(buf); /* go to the end of the buffer */
4450 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 return FALSE;
4452 *p++ = ' ';
4453 *p++ = '(';
4454 if (add_file)
4455 {
4456 STRCPY(p, "file ");
4457 p += 5;
4458 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004459 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4460 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4462 return TRUE;
4463}
4464
4465/*
4466 * If fname is not a full path, make it a full path.
4467 * Returns pointer to allocated memory (NULL for failure).
4468 */
4469 char_u *
4470fix_fname(fname)
4471 char_u *fname;
4472{
4473 /*
4474 * Force expanding the path always for Unix, because symbolic links may
4475 * mess up the full path name, even though it starts with a '/'.
4476 * Also expand when there is ".." in the file name, try to remove it,
4477 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004478 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 * For MS-Windows also expand names like "longna~1" to "longname".
4480 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004481#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 return FullName_save(fname, TRUE);
4483#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004484 if (!vim_isAbsName(fname)
4485 || strstr((char *)fname, "..") != NULL
4486 || strstr((char *)fname, "//") != NULL
4487# ifdef BACKSLASH_IN_FILENAME
4488 || strstr((char *)fname, "\\\\") != NULL
4489# endif
4490# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004492# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 )
4494 return FullName_save(fname, FALSE);
4495
4496 fname = vim_strsave(fname);
4497
Bram Moolenaar9b942202007-10-03 12:31:33 +00004498# ifdef USE_FNAME_CASE
4499# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
4503 if (fname != NULL)
4504 fname_case(fname, 0); /* set correct case for file name */
4505 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507
4508 return fname;
4509#endif
4510}
4511
4512/*
4513 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4514 * "ffname" becomes a pointer to allocated memory (or NULL).
4515 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 void
4517fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004518 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 char_u **ffname;
4520 char_u **sfname;
4521{
4522 if (*ffname == NULL) /* if no file name given, nothing to do */
4523 return;
4524 if (*sfname == NULL) /* if no short file name given, use ffname */
4525 *sfname = *ffname;
4526 *ffname = fix_fname(*ffname); /* expand to full path */
4527
4528#ifdef FEAT_SHORTCUT
4529 if (!buf->b_p_bin)
4530 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004531 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 /* If the file name is a shortcut file, use the file it links to. */
4534 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004535 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
4537 vim_free(*ffname);
4538 *ffname = rfname;
4539 *sfname = rfname;
4540 }
4541 }
4542#endif
4543}
4544
4545/*
4546 * Get the file name for an argument list entry.
4547 */
4548 char_u *
4549alist_name(aep)
4550 aentry_T *aep;
4551{
4552 buf_T *bp;
4553
4554 /* Use the name from the associated buffer if it exists. */
4555 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004556 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 return aep->ae_fname;
4558 return bp->b_fname;
4559}
4560
4561#if defined(FEAT_WINDOWS) || defined(PROTO)
4562/*
4563 * do_arg_all(): Open up to 'count' windows, one for each argument.
4564 */
4565 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004566do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 int count;
4568 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004569 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
4571 int i;
4572 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004573 char_u *opened; /* Array of weight for which args are open:
4574 * 0: not opened
4575 * 1: opened in other tab
4576 * 2: opened in curtab
4577 * 3: opened in curtab and curwin
4578 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004579 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 int use_firstwin = FALSE; /* use first window for arglist */
4581 int split_ret = OK;
4582 int p_ea_save;
4583 alist_T *alist; /* argument list to be used */
4584 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004585 tabpage_T *tpnext;
4586 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004587 win_T *old_curwin, *last_curwin;
4588 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004589 win_T *new_curwin = NULL;
4590 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591
4592 if (ARGCOUNT <= 0)
4593 {
4594 /* Don't give an error message. We don't want it when the ":all"
4595 * command is in the .vimrc. */
4596 return;
4597 }
4598 setpcmark();
4599
4600 opened_len = ARGCOUNT;
4601 opened = alloc_clear((unsigned)opened_len);
4602 if (opened == NULL)
4603 return;
4604
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004605 /* Autocommands may do anything to the argument list. Make sure it's not
4606 * freed while we are working here by "locking" it. We still have to
4607 * watch out for its size to be changed. */
4608 alist = curwin->w_alist;
4609 ++alist->al_refcount;
4610
4611 old_curwin = curwin;
4612 old_curtab = curtab;
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#ifdef FEAT_GUI
4615 need_mouse_correct = TRUE;
4616#endif
4617
4618 /*
4619 * Try closing all windows that are not in the argument list.
4620 * Also close windows that are not full width;
4621 * When 'hidden' or "forceit" set the buffer becomes hidden.
4622 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004623 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004625 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004626 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004627 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004629 tpnext = curtab->tp_next;
4630 for (wp = firstwin; wp != NULL; wp = wpnext)
4631 {
4632 wpnext = wp->w_next;
4633 buf = wp->w_buffer;
4634 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004635 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004637 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004639 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004640 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004641 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004643 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004644 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004646 if (i < alist->al_ga.ga_len
4647 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4648 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4649 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004651 int weight = 1;
4652
4653 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004654 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004655 ++weight;
4656 if (old_curwin == wp)
4657 ++weight;
4658 }
4659
4660 if (weight > (int)opened[i])
4661 {
4662 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004663 if (i == 0)
4664 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004665 if (new_curwin != NULL)
4666 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004667 new_curwin = wp;
4668 new_curtab = curtab;
4669 }
4670 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004671 else if (keep_tabs)
4672 i = opened_len;
4673
4674 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004675 {
4676 /* Use the current argument list for all windows
4677 * containing a file from it. */
4678 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004679 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004680 ++wp->w_alist->al_refcount;
4681 }
4682 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 }
4685 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004686 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004688 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004690 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004691 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004693 /* If the buffer was changed, and we would like to hide it,
4694 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004695 if (!P_HID(buf) && buf->b_nwindows <= 1
4696 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004698 (void)autowrite(buf, FALSE);
4699#ifdef FEAT_AUTOCMD
4700 /* check if autocommands removed the window */
4701 if (!win_valid(wp) || !buf_valid(buf))
4702 {
4703 wpnext = firstwin; /* start all over... */
4704 continue;
4705 }
4706#endif
4707 }
4708#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004709 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004710 if (firstwin == lastwin
4711 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004712#endif
4713 use_firstwin = TRUE;
4714#ifdef FEAT_WINDOWS
4715 else
4716 {
4717 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4718# ifdef FEAT_AUTOCMD
4719 /* check if autocommands removed the next window */
4720 if (!win_valid(wpnext))
4721 wpnext = firstwin; /* start all over... */
4722# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 }
4724#endif
4725 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 }
4727 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004728
4729 /* Without the ":tab" modifier only do the current tab page. */
4730 if (had_tab == 0 || tpnext == NULL)
4731 break;
4732
4733# ifdef FEAT_AUTOCMD
4734 /* check if autocommands removed the next tab page */
4735 if (!valid_tabpage(tpnext))
4736 tpnext = first_tabpage; /* start all over...*/
4737# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004738 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
4740
4741 /*
4742 * Open a window for files in the argument list that don't have one.
4743 * ARGCOUNT may change while doing this, because of autocommands.
4744 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004745 if (count > opened_len || count <= 0)
4746 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747
4748#ifdef FEAT_AUTOCMD
4749 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4750 ++autocmd_no_enter;
4751 ++autocmd_no_leave;
4752#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004753 last_curwin = curwin;
4754 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004756#ifdef FEAT_WINDOWS
4757 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4758 * leaving an empty tab page when executed locally. */
4759 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4760 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4761 use_firstwin = TRUE;
4762#endif
4763
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004764 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
4766 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4767 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004768 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 /* Move the already present window to below the current window */
4771 if (curwin->w_arg_idx != i)
4772 {
4773 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4774 {
4775 if (wpnext->w_arg_idx == i)
4776 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004777 if (keep_tabs)
4778 {
4779 new_curwin = wpnext;
4780 new_curtab = curtab;
4781 }
4782 else
4783 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 break;
4785 }
4786 }
4787 }
4788 }
4789 else if (split_ret == OK)
4790 {
4791 if (!use_firstwin) /* split current window */
4792 {
4793 p_ea_save = p_ea;
4794 p_ea = TRUE; /* use space from all windows */
4795 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4796 p_ea = p_ea_save;
4797 if (split_ret == FAIL)
4798 continue;
4799 }
4800#ifdef FEAT_AUTOCMD
4801 else /* first window: do autocmd for leaving this buffer */
4802 --autocmd_no_leave;
4803#endif
4804
4805 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004806 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 */
4808 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004809 if (i == 0)
4810 {
4811 new_curwin = curwin;
4812 new_curtab = curtab;
4813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4815 ECMD_ONE,
4816 ((P_HID(curwin->w_buffer)
4817 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004818 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819#ifdef FEAT_AUTOCMD
4820 if (use_firstwin)
4821 ++autocmd_no_leave;
4822#endif
4823 use_firstwin = FALSE;
4824 }
4825 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004826
4827 /* When ":tab" was used open a new tab for a new window repeatedly. */
4828 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4829 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 }
4831
4832 /* Remove the "lock" on the argument list. */
4833 alist_unlink(alist);
4834
4835#ifdef FEAT_AUTOCMD
4836 --autocmd_no_enter;
4837#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004838 /* restore last referenced tabpage's curwin */
4839 if (last_curtab != new_curtab)
4840 {
4841 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004842 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004843 if (win_valid(last_curwin))
4844 win_enter(last_curwin, FALSE);
4845 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004846 /* to window with first arg */
4847 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004848 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004849 if (win_valid(new_curwin))
4850 win_enter(new_curwin, FALSE);
4851
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852#ifdef FEAT_AUTOCMD
4853 --autocmd_no_leave;
4854#endif
4855 vim_free(opened);
4856}
4857
4858# if defined(FEAT_LISTCMDS) || defined(PROTO)
4859/*
4860 * Open a window for a number of buffers.
4861 */
4862 void
4863ex_buffer_all(eap)
4864 exarg_T *eap;
4865{
4866 buf_T *buf;
4867 win_T *wp, *wpnext;
4868 int split_ret = OK;
4869 int p_ea_save;
4870 int open_wins = 0;
4871 int r;
4872 int count; /* Maximum number of windows to open. */
4873 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004874#ifdef FEAT_WINDOWS
4875 int had_tab = cmdmod.tab;
4876 tabpage_T *tpnext;
4877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878
4879 if (eap->addr_count == 0) /* make as many windows as possible */
4880 count = 9999;
4881 else
4882 count = eap->line2; /* make as many windows as specified */
4883 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4884 all = FALSE;
4885 else
4886 all = TRUE;
4887
4888 setpcmark();
4889
4890#ifdef FEAT_GUI
4891 need_mouse_correct = TRUE;
4892#endif
4893
4894 /*
4895 * Close superfluous windows (two windows for the same buffer).
4896 * Also close windows that are not full-width.
4897 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004898#ifdef FEAT_WINDOWS
4899 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004900 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004901 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004904 tpnext = curtab->tp_next;
4905 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004907 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004908 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004909#ifdef FEAT_VERTSPLIT
4910 || ((cmdmod.split & WSP_VERT)
4911 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004912 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004913 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004915#ifdef FEAT_WINDOWS
4916 || (had_tab > 0 && wp != firstwin)
4917#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004918 ) && firstwin != lastwin
4919#ifdef FEAT_AUTOCMD
4920 && !(wp->w_closing || wp->w_buffer->b_closing)
4921#endif
4922 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004923 {
4924 win_close(wp, FALSE);
4925#ifdef FEAT_AUTOCMD
4926 wpnext = firstwin; /* just in case an autocommand does
4927 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004928 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004929 open_wins = 0;
4930#endif
4931 }
4932 else
4933 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004935
4936#ifdef FEAT_WINDOWS
4937 /* Without the ":tab" modifier only do the current tab page. */
4938 if (had_tab == 0 || tpnext == NULL)
4939 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004940 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943
4944 /*
4945 * Go through the buffer list. When a buffer doesn't have a window yet,
4946 * open one. Otherwise move the window to the right position.
4947 * Watch out for autocommands that delete buffers or windows!
4948 */
4949#ifdef FEAT_AUTOCMD
4950 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4951 ++autocmd_no_enter;
4952#endif
4953 win_enter(lastwin, FALSE);
4954#ifdef FEAT_AUTOCMD
4955 ++autocmd_no_leave;
4956#endif
4957 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4958 {
4959 /* Check if this buffer needs a window */
4960 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4961 continue;
4962
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004963#ifdef FEAT_WINDOWS
4964 if (had_tab != 0)
4965 {
4966 /* With the ":tab" modifier don't move the window. */
4967 if (buf->b_nwindows > 0)
4968 wp = lastwin; /* buffer has a window, skip it */
4969 else
4970 wp = NULL;
4971 }
4972 else
4973#endif
4974 {
4975 /* Check if this buffer already has a window */
4976 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4977 if (wp->w_buffer == buf)
4978 break;
4979 /* If the buffer already has a window, move it */
4980 if (wp != NULL)
4981 win_move_after(wp, curwin);
4982 }
4983
4984 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
4986 /* Split the window and put the buffer in it */
4987 p_ea_save = p_ea;
4988 p_ea = TRUE; /* use space from all windows */
4989 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4990 ++open_wins;
4991 p_ea = p_ea_save;
4992 if (split_ret == FAIL)
4993 continue;
4994
4995 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004996#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 swap_exists_action = SEA_DIALOG;
4998#endif
4999 set_curbuf(buf, DOBUF_GOTO);
5000#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00005003#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005005# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 break;
5007 }
5008#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005009#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (swap_exists_action == SEA_QUIT)
5011 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005012# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5013 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005014
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005015 /* Reset the error/interrupt/exception state here so that
5016 * aborting() returns FALSE when closing a window. */
5017 enter_cleanup(&cs);
5018# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005019
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005020 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 win_close(curwin, TRUE);
5022 --open_wins;
5023 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005024 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005025
5026# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5027 /* Restore the error/interrupt/exception state if not
5028 * discarded by a new aborting error, interrupt, or uncaught
5029 * exception. */
5030 leave_cleanup(&cs);
5031# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 else
5034 handle_swap_exists(NULL);
5035#endif
5036 }
5037
5038 ui_breakcheck();
5039 if (got_int)
5040 {
5041 (void)vgetc(); /* only break the file loading, not the rest */
5042 break;
5043 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005044#ifdef FEAT_EVAL
5045 /* Autocommands deleted the buffer or aborted script processing!!! */
5046 if (aborting())
5047 break;
5048#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005049#ifdef FEAT_WINDOWS
5050 /* When ":tab" was used open a new tab for a new window repeatedly. */
5051 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5052 cmdmod.tab = 9999;
5053#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055#ifdef FEAT_AUTOCMD
5056 --autocmd_no_enter;
5057#endif
5058 win_enter(firstwin, FALSE); /* back to first window */
5059#ifdef FEAT_AUTOCMD
5060 --autocmd_no_leave;
5061#endif
5062
5063 /*
5064 * Close superfluous windows.
5065 */
5066 for (wp = lastwin; open_wins > count; )
5067 {
5068 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5069 || autowrite(wp->w_buffer, FALSE) == OK);
5070#ifdef FEAT_AUTOCMD
5071 if (!win_valid(wp))
5072 {
5073 /* BufWrite Autocommands made the window invalid, start over */
5074 wp = lastwin;
5075 }
5076 else
5077#endif
5078 if (r)
5079 {
5080 win_close(wp, !P_HID(wp->w_buffer));
5081 --open_wins;
5082 wp = lastwin;
5083 }
5084 else
5085 {
5086 wp = wp->w_prev;
5087 if (wp == NULL)
5088 break;
5089 }
5090 }
5091}
5092# endif /* FEAT_LISTCMDS */
5093
5094#endif /* FEAT_WINDOWS */
5095
Bram Moolenaara3227e22006-03-08 21:32:40 +00005096static int chk_modeline __ARGS((linenr_T, int));
5097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098/*
5099 * do_modelines() - process mode lines for the current file
5100 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005101 * "flags" can be:
5102 * OPT_WINONLY only set options local to window
5103 * OPT_NOWIN don't set options local to window
5104 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 * Returns immediately if the "ml" option isn't set.
5106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005108do_modelines(flags)
5109 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005111 linenr_T lnum;
5112 int nmlines;
5113 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114
5115 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5116 return;
5117
5118 /* Disallow recursive entry here. Can happen when executing a modeline
5119 * triggers an autocommand, which reloads modelines with a ":do". */
5120 if (entered)
5121 return;
5122
5123 ++entered;
5124 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5125 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005126 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 nmlines = 0;
5128
5129 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5130 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005131 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 nmlines = 0;
5133 --entered;
5134}
5135
5136#include "version.h" /* for version number */
5137
5138/*
5139 * chk_modeline() - check a single line for a mode string
5140 * Return FAIL if an error encountered.
5141 */
5142 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005143chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005145 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146{
5147 char_u *s;
5148 char_u *e;
5149 char_u *linecopy; /* local copy of any modeline found */
5150 int prev;
5151 int vers;
5152 int end;
5153 int retval = OK;
5154 char_u *save_sourcing_name;
5155 linenr_T save_sourcing_lnum;
5156#ifdef FEAT_EVAL
5157 scid_T save_SID;
5158#endif
5159
5160 prev = -1;
5161 for (s = ml_get(lnum); *s != NUL; ++s)
5162 {
5163 if (prev == -1 || vim_isspace(prev))
5164 {
5165 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5166 || STRNCMP(s, "vi:", (size_t)3) == 0)
5167 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005168 /* Accept both "vim" and "Vim". */
5169 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
5171 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5172 e = s + 4;
5173 else
5174 e = s + 3;
5175 vers = getdigits(&e);
5176 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005177 && (s[0] != 'V'
5178 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 && (s[3] == ':'
5180 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5181 || (VIM_VERSION_100 < vers && s[3] == '<')
5182 || (VIM_VERSION_100 > vers && s[3] == '>')
5183 || (VIM_VERSION_100 == vers && s[3] == '=')))
5184 break;
5185 }
5186 }
5187 prev = *s;
5188 }
5189
5190 if (*s)
5191 {
5192 do /* skip over "ex:", "vi:" or "vim:" */
5193 ++s;
5194 while (s[-1] != ':');
5195
5196 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5197 if (linecopy == NULL)
5198 return FAIL;
5199
5200 save_sourcing_lnum = sourcing_lnum;
5201 save_sourcing_name = sourcing_name;
5202 sourcing_lnum = lnum; /* prepare for emsg() */
5203 sourcing_name = (char_u *)"modelines";
5204
5205 end = FALSE;
5206 while (end == FALSE)
5207 {
5208 s = skipwhite(s);
5209 if (*s == NUL)
5210 break;
5211
5212 /*
5213 * Find end of set command: ':' or end of line.
5214 * Skip over "\:", replacing it with ":".
5215 */
5216 for (e = s; *e != ':' && *e != NUL; ++e)
5217 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005218 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 if (*e == NUL)
5220 end = TRUE;
5221
5222 /*
5223 * If there is a "set" command, require a terminating ':' and
5224 * ignore the stuff after the ':'.
5225 * "vi:set opt opt opt: foo" -- foo not interpreted
5226 * "vi:opt opt opt: foo" -- foo interpreted
5227 * Accept "se" for compatibility with Elvis.
5228 */
5229 if (STRNCMP(s, "set ", (size_t)4) == 0
5230 || STRNCMP(s, "se ", (size_t)3) == 0)
5231 {
5232 if (*e != ':') /* no terminating ':'? */
5233 break;
5234 end = TRUE;
5235 s = vim_strchr(s, ' ') + 1;
5236 }
5237 *e = NUL; /* truncate the set command */
5238
5239 if (*s != NUL) /* skip over an empty "::" */
5240 {
5241#ifdef FEAT_EVAL
5242 save_SID = current_SID;
5243 current_SID = SID_MODELINE;
5244#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005245 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005246#ifdef FEAT_EVAL
5247 current_SID = save_SID;
5248#endif
5249 if (retval == FAIL) /* stop if error found */
5250 break;
5251 }
5252 s = e + 1; /* advance to next part */
5253 }
5254
5255 sourcing_lnum = save_sourcing_lnum;
5256 sourcing_name = save_sourcing_name;
5257
5258 vim_free(linecopy);
5259 }
5260 return retval;
5261}
5262
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005263#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 int
5265read_viminfo_bufferlist(virp, writing)
5266 vir_T *virp;
5267 int writing;
5268{
5269 char_u *tab;
5270 linenr_T lnum;
5271 colnr_T col;
5272 buf_T *buf;
5273 char_u *sfname;
5274 char_u *xline;
5275
5276 /* Handle long line and escaped characters. */
5277 xline = viminfo_readstring(virp, 1, FALSE);
5278
5279 /* don't read in if there are files on the command-line or if writing: */
5280 if (xline != NULL && !writing && ARGCOUNT == 0
5281 && find_viminfo_parameter('%') != NULL)
5282 {
5283 /* Format is: <fname> Tab <lnum> Tab <col>.
5284 * Watch out for a Tab in the file name, work from the end. */
5285 lnum = 0;
5286 col = 0;
5287 tab = vim_strrchr(xline, '\t');
5288 if (tab != NULL)
5289 {
5290 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005291 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 tab = vim_strrchr(xline, '\t');
5293 if (tab != NULL)
5294 {
5295 *tab++ = '\0';
5296 lnum = atol((char *)tab);
5297 }
5298 }
5299
5300 /* Expand "~/" in the file name at "line + 1" to a full path.
5301 * Then try shortening it by comparing with the current directory */
5302 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005303 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304
5305 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5306 if (buf != NULL) /* just in case... */
5307 {
5308 buf->b_last_cursor.lnum = lnum;
5309 buf->b_last_cursor.col = col;
5310 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5311 }
5312 }
5313 vim_free(xline);
5314
5315 return viminfo_readline(virp);
5316}
5317
5318 void
5319write_viminfo_bufferlist(fp)
5320 FILE *fp;
5321{
5322 buf_T *buf;
5323#ifdef FEAT_WINDOWS
5324 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005325 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326#endif
5327 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005328 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 if (find_viminfo_parameter('%') == NULL)
5331 return;
5332
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005333 /* Without a number -1 is returned: do all buffers. */
5334 max_buffers = get_viminfo_parameter('%');
5335
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005337#define LINE_BUF_LEN (MAXPATHL + 40)
5338 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 if (line == NULL)
5340 return;
5341
5342#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005343 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 set_last_cursor(win);
5345#else
5346 set_last_cursor(curwin);
5347#endif
5348
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005349 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5351 {
5352 if (buf->b_fname == NULL
5353 || !buf->b_p_bl
5354#ifdef FEAT_QUICKFIX
5355 || bt_quickfix(buf)
5356#endif
5357 || removable(buf->b_ffname))
5358 continue;
5359
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005360 if (max_buffers-- == 0)
5361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 putc('%', fp);
5363 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005364 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 (long)buf->b_last_cursor.lnum,
5366 buf->b_last_cursor.col);
5367 viminfo_writestring(fp, line);
5368 }
5369 vim_free(line);
5370}
5371#endif
5372
5373
5374/*
5375 * Return special buffer name.
5376 * Returns NULL when the buffer has a normal file name.
5377 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005378 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379buf_spname(buf)
5380 buf_T *buf;
5381{
5382#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5383 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005384 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005385 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005386 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005387
5388 /*
5389 * For location list window, w_llist_ref points to the location list.
5390 * For quickfix window, w_llist_ref is NULL.
5391 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005392 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005393 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005394 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005395 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397#endif
5398#ifdef FEAT_QUICKFIX
5399 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5400 * contains the name as specified by the user */
5401 if (bt_nofile(buf))
5402 {
5403 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005404 return buf->b_sfname;
5405 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 }
5407#endif
5408 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005409 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 return NULL;
5411}
5412
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005413#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5414 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5415 || defined(PROTO)
5416/*
5417 * Find a window for buffer "buf".
5418 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5419 * If not found FAIL is returned.
5420 */
5421 int
5422find_win_for_buf(buf, wp, tp)
5423 buf_T *buf;
5424 win_T **wp;
5425 tabpage_T **tp;
5426{
5427 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5428 if ((*wp)->w_buffer == buf)
5429 goto win_found;
5430 return FAIL;
5431win_found:
5432 return OK;
5433}
5434#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
5436#if defined(FEAT_SIGNS) || defined(PROTO)
5437/*
5438 * Insert the sign into the signlist.
5439 */
5440 static void
5441insert_sign(buf, prev, next, id, lnum, typenr)
5442 buf_T *buf; /* buffer to store sign in */
5443 signlist_T *prev; /* previous sign entry */
5444 signlist_T *next; /* next sign entry */
5445 int id; /* sign ID */
5446 linenr_T lnum; /* line number which gets the mark */
5447 int typenr; /* typenr of sign we are adding */
5448{
5449 signlist_T *newsign;
5450
5451 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5452 if (newsign != NULL)
5453 {
5454 newsign->id = id;
5455 newsign->lnum = lnum;
5456 newsign->typenr = typenr;
5457 newsign->next = next;
5458#ifdef FEAT_NETBEANS_INTG
5459 newsign->prev = prev;
5460 if (next != NULL)
5461 next->prev = newsign;
5462#endif
5463
5464 if (prev == NULL)
5465 {
5466 /* When adding first sign need to redraw the windows to create the
5467 * column for signs. */
5468 if (buf->b_signlist == NULL)
5469 {
5470 redraw_buf_later(buf, NOT_VALID);
5471 changed_cline_bef_curs();
5472 }
5473
5474 /* first sign in signlist */
5475 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005476#ifdef FEAT_NETBEANS_INTG
5477 if (netbeans_active())
5478 buf->b_has_sign_column = TRUE;
5479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 }
5481 else
5482 prev->next = newsign;
5483 }
5484}
5485
5486/*
5487 * Add the sign into the signlist. Find the right spot to do it though.
5488 */
5489 void
5490buf_addsign(buf, id, lnum, typenr)
5491 buf_T *buf; /* buffer to store sign in */
5492 int id; /* sign ID */
5493 linenr_T lnum; /* line number which gets the mark */
5494 int typenr; /* typenr of sign we are adding */
5495{
5496 signlist_T *sign; /* a sign in the signlist */
5497 signlist_T *prev; /* the previous sign */
5498
5499 prev = NULL;
5500 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5501 {
5502 if (lnum == sign->lnum && id == sign->id)
5503 {
5504 sign->typenr = typenr;
5505 return;
5506 }
5507 else if (
5508#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5509 id < 0 &&
5510#endif
5511 lnum < sign->lnum)
5512 {
5513#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5514 /* XXX - GRP: Is this because of sign slide problem? Or is it
5515 * really needed? Or is it because we allow multiple signs per
5516 * line? If so, should I add that feature to FEAT_SIGNS?
5517 */
5518 while (prev != NULL && prev->lnum == lnum)
5519 prev = prev->prev;
5520 if (prev == NULL)
5521 sign = buf->b_signlist;
5522 else
5523 sign = prev->next;
5524#endif
5525 insert_sign(buf, prev, sign, id, lnum, typenr);
5526 return;
5527 }
5528 prev = sign;
5529 }
5530#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5531 /* XXX - GRP: See previous comment */
5532 while (prev != NULL && prev->lnum == lnum)
5533 prev = prev->prev;
5534 if (prev == NULL)
5535 sign = buf->b_signlist;
5536 else
5537 sign = prev->next;
5538#endif
5539 insert_sign(buf, prev, sign, id, lnum, typenr);
5540
5541 return;
5542}
5543
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005544/*
5545 * For an existing, placed sign "markId" change the type to "typenr".
5546 * Returns the line number of the sign, or zero if the sign is not found.
5547 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005548 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549buf_change_sign_type(buf, markId, typenr)
5550 buf_T *buf; /* buffer to store sign in */
5551 int markId; /* sign ID */
5552 int typenr; /* typenr of sign we are adding */
5553{
5554 signlist_T *sign; /* a sign in the signlist */
5555
5556 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5557 {
5558 if (sign->id == markId)
5559 {
5560 sign->typenr = typenr;
5561 return sign->lnum;
5562 }
5563 }
5564
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005565 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566}
5567
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005568 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569buf_getsigntype(buf, lnum, type)
5570 buf_T *buf;
5571 linenr_T lnum;
5572 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5573{
5574 signlist_T *sign; /* a sign in a b_signlist */
5575
5576 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5577 if (sign->lnum == lnum
5578 && (type == SIGN_ANY
5579# ifdef FEAT_SIGN_ICONS
5580 || (type == SIGN_ICON
5581 && sign_get_image(sign->typenr) != NULL)
5582# endif
5583 || (type == SIGN_TEXT
5584 && sign_get_text(sign->typenr) != NULL)
5585 || (type == SIGN_LINEHL
5586 && sign_get_attr(sign->typenr, TRUE) != 0)))
5587 return sign->typenr;
5588 return 0;
5589}
5590
5591
5592 linenr_T
5593buf_delsign(buf, id)
5594 buf_T *buf; /* buffer sign is stored in */
5595 int id; /* sign id */
5596{
5597 signlist_T **lastp; /* pointer to pointer to current sign */
5598 signlist_T *sign; /* a sign in a b_signlist */
5599 signlist_T *next; /* the next sign in a b_signlist */
5600 linenr_T lnum; /* line number whose sign was deleted */
5601
5602 lastp = &buf->b_signlist;
5603 lnum = 0;
5604 for (sign = buf->b_signlist; sign != NULL; sign = next)
5605 {
5606 next = sign->next;
5607 if (sign->id == id)
5608 {
5609 *lastp = next;
5610#ifdef FEAT_NETBEANS_INTG
5611 if (next != NULL)
5612 next->prev = sign->prev;
5613#endif
5614 lnum = sign->lnum;
5615 vim_free(sign);
5616 break;
5617 }
5618 else
5619 lastp = &sign->next;
5620 }
5621
5622 /* When deleted the last sign need to redraw the windows to remove the
5623 * sign column. */
5624 if (buf->b_signlist == NULL)
5625 {
5626 redraw_buf_later(buf, NOT_VALID);
5627 changed_cline_bef_curs();
5628 }
5629
5630 return lnum;
5631}
5632
5633
5634/*
5635 * Find the line number of the sign with the requested id. If the sign does
5636 * not exist, return 0 as the line number. This will still let the correct file
5637 * get loaded.
5638 */
5639 int
5640buf_findsign(buf, id)
5641 buf_T *buf; /* buffer to store sign in */
5642 int id; /* sign ID */
5643{
5644 signlist_T *sign; /* a sign in the signlist */
5645
5646 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5647 if (sign->id == id)
5648 return sign->lnum;
5649
5650 return 0;
5651}
5652
5653 int
5654buf_findsign_id(buf, lnum)
5655 buf_T *buf; /* buffer whose sign we are searching for */
5656 linenr_T lnum; /* line number of sign */
5657{
5658 signlist_T *sign; /* a sign in the signlist */
5659
5660 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5661 if (sign->lnum == lnum)
5662 return sign->id;
5663
5664 return 0;
5665}
5666
5667
5668# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5669/* see if a given type of sign exists on a specific line */
5670 int
5671buf_findsigntype_id(buf, lnum, typenr)
5672 buf_T *buf; /* buffer whose sign we are searching for */
5673 linenr_T lnum; /* line number of sign */
5674 int typenr; /* sign type number */
5675{
5676 signlist_T *sign; /* a sign in the signlist */
5677
5678 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5679 if (sign->lnum == lnum && sign->typenr == typenr)
5680 return sign->id;
5681
5682 return 0;
5683}
5684
5685
5686# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5687/* return the number of icons on the given line */
5688 int
5689buf_signcount(buf, lnum)
5690 buf_T *buf;
5691 linenr_T lnum;
5692{
5693 signlist_T *sign; /* a sign in the signlist */
5694 int count = 0;
5695
5696 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5697 if (sign->lnum == lnum)
5698 if (sign_get_image(sign->typenr) != NULL)
5699 count++;
5700
5701 return count;
5702}
5703# endif /* FEAT_SIGN_ICONS */
5704# endif /* FEAT_NETBEANS_INTG */
5705
5706
5707/*
5708 * Delete signs in buffer "buf".
5709 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005710 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711buf_delete_signs(buf)
5712 buf_T *buf;
5713{
5714 signlist_T *next;
5715
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005716 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005717 * sign column. Not when curwin is NULL (this means we're exiting). */
5718 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005719 {
5720 redraw_buf_later(buf, NOT_VALID);
5721 changed_cline_bef_curs();
5722 }
5723
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 while (buf->b_signlist != NULL)
5725 {
5726 next = buf->b_signlist->next;
5727 vim_free(buf->b_signlist);
5728 buf->b_signlist = next;
5729 }
5730}
5731
5732/*
5733 * Delete all signs in all buffers.
5734 */
5735 void
5736buf_delete_all_signs()
5737{
5738 buf_T *buf; /* buffer we are checking for signs */
5739
5740 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5741 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743}
5744
5745/*
5746 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5747 */
5748 void
5749sign_list_placed(rbuf)
5750 buf_T *rbuf;
5751{
5752 buf_T *buf;
5753 signlist_T *p;
5754 char lbuf[BUFSIZ];
5755
5756 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5757 msg_putchar('\n');
5758 if (rbuf == NULL)
5759 buf = firstbuf;
5760 else
5761 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005762 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763 {
5764 if (buf->b_signlist != NULL)
5765 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005766 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5768 msg_putchar('\n');
5769 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005770 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005772 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5774 MSG_PUTS(lbuf);
5775 msg_putchar('\n');
5776 }
5777 if (rbuf != NULL)
5778 break;
5779 buf = buf->b_next;
5780 }
5781}
5782
5783/*
5784 * Adjust a placed sign for inserted/deleted lines.
5785 */
5786 void
5787sign_mark_adjust(line1, line2, amount, amount_after)
5788 linenr_T line1;
5789 linenr_T line2;
5790 long amount;
5791 long amount_after;
5792{
5793 signlist_T *sign; /* a sign in a b_signlist */
5794
5795 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5796 {
5797 if (sign->lnum >= line1 && sign->lnum <= line2)
5798 {
5799 if (amount == MAXLNUM)
5800 sign->lnum = line1;
5801 else
5802 sign->lnum += amount;
5803 }
5804 else if (sign->lnum > line2)
5805 sign->lnum += amount_after;
5806 }
5807}
5808#endif /* FEAT_SIGNS */
5809
5810/*
5811 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5812 */
5813 void
5814set_buflisted(on)
5815 int on;
5816{
5817 if (on != curbuf->b_p_bl)
5818 {
5819 curbuf->b_p_bl = on;
5820#ifdef FEAT_AUTOCMD
5821 if (on)
5822 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5823 else
5824 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5825#endif
5826 }
5827}
5828
5829/*
5830 * Read the file for "buf" again and check if the contents changed.
5831 * Return TRUE if it changed or this could not be checked.
5832 */
5833 int
5834buf_contents_changed(buf)
5835 buf_T *buf;
5836{
5837 buf_T *newbuf;
5838 int differ = TRUE;
5839 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 exarg_T ea;
5842
5843 /* Allocate a buffer without putting it in the buffer list. */
5844 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5845 if (newbuf == NULL)
5846 return TRUE;
5847
5848 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5849 if (prep_exarg(&ea, buf) == FAIL)
5850 {
5851 wipe_buffer(newbuf, FALSE);
5852 return TRUE;
5853 }
5854
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 /* set curwin/curbuf to buf and save a few things */
5856 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
Bram Moolenaar4770d092006-01-12 23:22:24 +00005858 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005859 && readfile(buf->b_ffname, buf->b_fname,
5860 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5861 &ea, READ_NEW | READ_DUMMY) == OK)
5862 {
5863 /* compare the two files line by line */
5864 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5865 {
5866 differ = FALSE;
5867 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5868 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5869 {
5870 differ = TRUE;
5871 break;
5872 }
5873 }
5874 }
5875 vim_free(ea.cmd);
5876
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 /* restore curwin/curbuf and a few other things */
5878 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879
5880 if (curbuf != newbuf) /* safety check */
5881 wipe_buffer(newbuf, FALSE);
5882
5883 return differ;
5884}
5885
5886/*
5887 * Wipe out a buffer and decrement the last buffer number if it was used for
5888 * this buffer. Call this to wipe out a temp buffer that does not contain any
5889 * marks.
5890 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 void
5892wipe_buffer(buf, aucmd)
5893 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005894 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895{
5896 if (buf->b_fnum == top_file_num - 1)
5897 --top_file_num;
5898
5899#ifdef FEAT_AUTOCMD
5900 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005901 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005903 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904#ifdef FEAT_AUTOCMD
5905 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005906 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907#endif
5908}