blob: c4d5e340baacc8d4d697f505faf890061a2bd527 [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;
Bram Moolenaar34d72d42015-07-17 14:18:08 +0200550 buf->b_p_fixeol = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 buf->b_start_eol = TRUE;
552#ifdef FEAT_MBYTE
553 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000554 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#endif
556 buf->b_ml.ml_mfp = NULL;
557 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
558#ifdef FEAT_NETBEANS_INTG
559 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560#endif
561}
562
563/*
564 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200565 * the file. flags:
566 * BFA_DEL buffer is going to be deleted
567 * BFA_WIPE buffer is going to be wiped out
568 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200571buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200573 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574{
575#ifdef FEAT_AUTOCMD
576 int is_curbuf = (buf == curbuf);
577
Bram Moolenaar362ce482012-06-06 19:02:45 +0200578 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
580 if (!buf_valid(buf)) /* autocommands may delete the buffer */
581 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200582 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
584 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
585 if (!buf_valid(buf)) /* autocommands may delete the buffer */
586 return;
587 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200588 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 {
590 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
591 FALSE, buf);
592 if (!buf_valid(buf)) /* autocommands may delete the buffer */
593 return;
594 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200595 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000597 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 return;
599# endif
600
601 /*
602 * It's possible that autocommands change curbuf to the one being deleted.
603 * This might cause curbuf to be deleted unexpectedly. But in some cases
604 * it's OK to delete the curbuf, because a new one is obtained anyway.
605 * Therefore only return if curbuf changed to the deleted buffer.
606 */
607 if (buf == curbuf && !is_curbuf)
608 return;
609#endif
610#ifdef FEAT_DIFF
611 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
612#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200613#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100614 /* Remove any ownsyntax, unless exiting. */
615 if (firstwin != NULL && curwin->w_buffer == buf)
616 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200617#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000618
619#ifdef FEAT_FOLDING
620 /* No folds in an empty buffer. */
621# ifdef FEAT_WINDOWS
622 {
623 win_T *win;
624 tabpage_T *tp;
625
626 FOR_ALL_TAB_WINDOWS(tp, win)
627 if (win->w_buffer == buf)
628 clearFolding(win);
629 }
630# else
631 if (curwin->w_buffer == buf)
632 clearFolding(curwin);
633# endif
634#endif
635
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636#ifdef FEAT_TCL
637 tcl_buffer_free(buf);
638#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 ml_close(buf, TRUE); /* close and delete the memline/memfile */
640 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200641 if ((flags & BFA_KEEP_UNDO) == 0)
642 {
643 u_blockfree(buf); /* free the memory allocated for undo */
644 u_clearall(buf); /* reset all undo information */
645 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200647 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000649 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650}
651
652/*
653 * Free a buffer structure and the things it contains related to the buffer
654 * itself (not the file, that must have been done already).
655 */
656 static void
657free_buffer(buf)
658 buf_T *buf;
659{
660 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200661#ifdef FEAT_EVAL
662 unref_var_dict(buf->b_vars);
663#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200664#ifdef FEAT_LUA
665 lua_buffer_free(buf);
666#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000667#ifdef FEAT_MZSCHEME
668 mzscheme_buffer_free(buf);
669#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670#ifdef FEAT_PERL
671 perl_buf_free(buf);
672#endif
673#ifdef FEAT_PYTHON
674 python_buffer_free(buf);
675#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200676#ifdef FEAT_PYTHON3
677 python3_buffer_free(buf);
678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679#ifdef FEAT_RUBY
680 ruby_buffer_free(buf);
681#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000682#ifdef FEAT_AUTOCMD
683 aubuflocal_remove(buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200684 if (autocmd_busy)
685 {
686 /* Do not free the buffer structure while autocommands are executing,
687 * it's still needed. Free it when autocmd_busy is reset. */
688 buf->b_next = au_pending_free_buf;
689 au_pending_free_buf = buf;
690 }
691 else
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000692#endif
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +0200693 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694}
695
696/*
697 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
698 */
699 static void
700free_buffer_stuff(buf, free_options)
701 buf_T *buf;
702 int free_options; /* free options as well */
703{
704 if (free_options)
705 {
706 clear_wininfo(buf); /* including window-local options */
707 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200708#ifdef FEAT_SPELL
709 ga_clear(&buf->b_s.b_langp);
710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 }
712#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200713 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
714 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#endif
716#ifdef FEAT_USR_CMDS
717 uc_clear(&buf->b_ucmds); /* clear local user commands */
718#endif
719#ifdef FEAT_SIGNS
720 buf_delete_signs(buf); /* delete any signs */
721#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000722#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200723 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef FEAT_LOCALMAP
726 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
727 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
728#endif
729#ifdef FEAT_MBYTE
730 vim_free(buf->b_start_fenc);
731 buf->b_start_fenc = NULL;
732#endif
733}
734
735/*
736 * Free the b_wininfo list for buffer "buf".
737 */
738 static void
739clear_wininfo(buf)
740 buf_T *buf;
741{
742 wininfo_T *wip;
743
744 while (buf->b_wininfo != NULL)
745 {
746 wip = buf->b_wininfo;
747 buf->b_wininfo = wip->wi_next;
748 if (wip->wi_optset)
749 {
750 clear_winopt(&wip->wi_opt);
751#ifdef FEAT_FOLDING
752 deleteFoldRecurse(&wip->wi_folds);
753#endif
754 }
755 vim_free(wip);
756 }
757}
758
759#if defined(FEAT_LISTCMDS) || defined(PROTO)
760/*
761 * Go to another buffer. Handles the result of the ATTENTION dialog.
762 */
763 void
764goto_buffer(eap, start, dir, count)
765 exarg_T *eap;
766 int start;
767 int dir;
768 int count;
769{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000770# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 buf_T *old_curbuf = curbuf;
772
773 swap_exists_action = SEA_DIALOG;
774# endif
775 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
776 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000777# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
779 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000780# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
781 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000782
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000783 /* Reset the error/interrupt/exception state here so that
784 * aborting() returns FALSE when closing a window. */
785 enter_cleanup(&cs);
786# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000787
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000788 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 win_close(curwin, TRUE);
790 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000791 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000792
793# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
794 /* Restore the error/interrupt/exception state if not discarded by a
795 * new aborting error, interrupt, or uncaught exception. */
796 leave_cleanup(&cs);
797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
799 else
800 handle_swap_exists(old_curbuf);
801# endif
802}
803#endif
804
Bram Moolenaare64ac772005-12-07 20:54:59 +0000805#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806/*
807 * Handle the situation of swap_exists_action being set.
808 * It is allowed for "old_curbuf" to be NULL or invalid.
809 */
810 void
811handle_swap_exists(old_curbuf)
812 buf_T *old_curbuf;
813{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000814# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
815 cleanup_T cs;
816# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100817#ifdef FEAT_SYN_HL
818 long old_tw = curbuf->b_p_tw;
819#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000820
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 if (swap_exists_action == SEA_QUIT)
822 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000823# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
824 /* Reset the error/interrupt/exception state here so that
825 * aborting() returns FALSE when closing a buffer. */
826 enter_cleanup(&cs);
827# endif
828
Bram Moolenaar071d4272004-06-13 20:20:40 +0000829 /* User selected Quit at ATTENTION prompt. Go back to previous
830 * buffer. If that buffer is gone or the same as the current one,
831 * open a new, empty buffer. */
832 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000833 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100834 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000836 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100838 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100840#ifdef FEAT_SYN_HL
841 if (old_tw != curbuf->b_p_tw)
842 check_colorcolumn(curwin);
843#endif
844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000846
847# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
848 /* Restore the error/interrupt/exception state if not discarded by a
849 * new aborting error, interrupt, or uncaught exception. */
850 leave_cleanup(&cs);
851# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 }
853 else if (swap_exists_action == SEA_RECOVER)
854 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000855# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
856 /* Reset the error/interrupt/exception state here so that
857 * aborting() returns FALSE when closing a buffer. */
858 enter_cleanup(&cs);
859# endif
860
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 /* User selected Recover at ATTENTION prompt. */
862 msg_scroll = TRUE;
863 ml_recover();
864 MSG_PUTS("\n"); /* don't overwrite the last message */
865 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000866 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000867
868# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
869 /* Restore the error/interrupt/exception state if not discarded by a
870 * new aborting error, interrupt, or uncaught exception. */
871 leave_cleanup(&cs);
872# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 }
874 swap_exists_action = SEA_NONE;
875}
876#endif
877
878#if defined(FEAT_LISTCMDS) || defined(PROTO)
879/*
880 * do_bufdel() - delete or unload buffer(s)
881 *
882 * addr_count == 0: ":bdel" - delete current buffer
883 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
884 * buffer "end_bnr", then any other arguments.
885 * addr_count == 2: ":N,N bdel" - delete buffers in range
886 *
887 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
888 * DOBUF_DEL (":bdel")
889 *
890 * Returns error message or NULL
891 */
892 char_u *
893do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
894 int command;
895 char_u *arg; /* pointer to extra arguments */
896 int addr_count;
897 int start_bnr; /* first buffer number in a range */
898 int end_bnr; /* buffer nr or last buffer nr in a range */
899 int forceit;
900{
901 int do_current = 0; /* delete current buffer? */
902 int deleted = 0; /* number of buffers deleted */
903 char_u *errormsg = NULL; /* return value */
904 int bnr; /* buffer number */
905 char_u *p;
906
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 if (addr_count == 0)
908 {
909 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
910 }
911 else
912 {
913 if (addr_count == 2)
914 {
915 if (*arg) /* both range and argument is not allowed */
916 return (char_u *)_(e_trailing);
917 bnr = start_bnr;
918 }
919 else /* addr_count == 1 */
920 bnr = end_bnr;
921
922 for ( ;!got_int; ui_breakcheck())
923 {
924 /*
925 * delete the current buffer last, otherwise when the
926 * current buffer is deleted, the next buffer becomes
927 * the current one and will be loaded, which may then
928 * also be deleted, etc.
929 */
930 if (bnr == curbuf->b_fnum)
931 do_current = bnr;
932 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
933 forceit) == OK)
934 ++deleted;
935
936 /*
937 * find next buffer number to delete/unload
938 */
939 if (addr_count == 2)
940 {
941 if (++bnr > end_bnr)
942 break;
943 }
944 else /* addr_count == 1 */
945 {
946 arg = skipwhite(arg);
947 if (*arg == NUL)
948 break;
949 if (!VIM_ISDIGIT(*arg))
950 {
951 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100952 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
953 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 if (bnr < 0) /* failed */
955 break;
956 arg = p;
957 }
958 else
959 bnr = getdigits(&arg);
960 }
961 }
962 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
963 FORWARD, do_current, forceit) == OK)
964 ++deleted;
965
966 if (deleted == 0)
967 {
968 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000969 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000971 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000973 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 errormsg = IObuff;
975 }
976 else if (deleted >= p_report)
977 {
978 if (command == DOBUF_UNLOAD)
979 {
980 if (deleted == 1)
981 MSG(_("1 buffer unloaded"));
982 else
983 smsg((char_u *)_("%d buffers unloaded"), deleted);
984 }
985 else if (command == DOBUF_DEL)
986 {
987 if (deleted == 1)
988 MSG(_("1 buffer deleted"));
989 else
990 smsg((char_u *)_("%d buffers deleted"), deleted);
991 }
992 else
993 {
994 if (deleted == 1)
995 MSG(_("1 buffer wiped out"));
996 else
997 smsg((char_u *)_("%d buffers wiped out"), deleted);
998 }
999 }
1000 }
1001
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002
1003 return errormsg;
1004}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001005#endif /* FEAT_LISTCMDS */
1006
1007#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
1008 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009
Bram Moolenaara02471e2014-01-10 16:43:14 +01001010static int empty_curbuf __ARGS((int close_others, int forceit, int action));
1011
1012/*
1013 * Make the current buffer empty.
1014 * Used when it is wiped out and it's the last buffer.
1015 */
1016 static int
1017empty_curbuf(close_others, forceit, action)
1018 int close_others;
1019 int forceit;
1020 int action;
1021{
1022 int retval;
1023 buf_T *buf = curbuf;
1024
1025 if (action == DOBUF_UNLOAD)
1026 {
1027 EMSG(_("E90: Cannot unload last buffer"));
1028 return FAIL;
1029 }
1030
1031 if (close_others)
1032 {
1033 /* Close any other windows on this buffer, then make it empty. */
1034#ifdef FEAT_WINDOWS
1035 close_windows(buf, TRUE);
1036#endif
1037 }
1038
1039 setpcmark();
1040 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
1041 forceit ? ECMD_FORCEIT : 0, curwin);
1042
1043 /*
1044 * do_ecmd() may create a new buffer, then we have to delete
1045 * the old one. But do_ecmd() may have done that already, check
1046 * if the buffer still exists.
1047 */
1048 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1049 close_buffer(NULL, buf, action, FALSE);
1050 if (!close_others)
1051 need_fileinfo = FALSE;
1052 return retval;
1053}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054/*
1055 * Implementation of the commands for the buffer list.
1056 *
1057 * action == DOBUF_GOTO go to specified buffer
1058 * action == DOBUF_SPLIT split window and go to specified buffer
1059 * action == DOBUF_UNLOAD unload specified buffer(s)
1060 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1061 * action == DOBUF_WIPE delete specified buffer(s) really
1062 *
1063 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1064 * start == DOBUF_FIRST go to "count" buffer from first buffer
1065 * start == DOBUF_LAST go to "count" buffer from last buffer
1066 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1067 *
1068 * Return FAIL or OK.
1069 */
1070 int
1071do_buffer(action, start, dir, count, forceit)
1072 int action;
1073 int start;
1074 int dir; /* FORWARD or BACKWARD */
1075 int count; /* buffer number or number of buffers */
1076 int forceit; /* TRUE for :...! */
1077{
1078 buf_T *buf;
1079 buf_T *bp;
1080 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1081 || action == DOBUF_WIPE);
1082
1083 switch (start)
1084 {
1085 case DOBUF_FIRST: buf = firstbuf; break;
1086 case DOBUF_LAST: buf = lastbuf; break;
1087 default: buf = curbuf; break;
1088 }
1089 if (start == DOBUF_MOD) /* find next modified buffer */
1090 {
1091 while (count-- > 0)
1092 {
1093 do
1094 {
1095 buf = buf->b_next;
1096 if (buf == NULL)
1097 buf = firstbuf;
1098 }
1099 while (buf != curbuf && !bufIsChanged(buf));
1100 }
1101 if (!bufIsChanged(buf))
1102 {
1103 EMSG(_("E84: No modified buffer found"));
1104 return FAIL;
1105 }
1106 }
1107 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1108 {
1109 while (buf != NULL && buf->b_fnum != count)
1110 buf = buf->b_next;
1111 }
1112 else
1113 {
1114 bp = NULL;
1115 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1116 {
1117 /* remember the buffer where we start, we come back there when all
1118 * buffers are unlisted. */
1119 if (bp == NULL)
1120 bp = buf;
1121 if (dir == FORWARD)
1122 {
1123 buf = buf->b_next;
1124 if (buf == NULL)
1125 buf = firstbuf;
1126 }
1127 else
1128 {
1129 buf = buf->b_prev;
1130 if (buf == NULL)
1131 buf = lastbuf;
1132 }
1133 /* don't count unlisted buffers */
1134 if (unload || buf->b_p_bl)
1135 {
1136 --count;
1137 bp = NULL; /* use this buffer as new starting point */
1138 }
1139 if (bp == buf)
1140 {
1141 /* back where we started, didn't find anything. */
1142 EMSG(_("E85: There is no listed buffer"));
1143 return FAIL;
1144 }
1145 }
1146 }
1147
1148 if (buf == NULL) /* could not find it */
1149 {
1150 if (start == DOBUF_FIRST)
1151 {
1152 /* don't warn when deleting */
1153 if (!unload)
Bram Moolenaar3b3a9492015-01-27 18:44:16 +01001154 EMSGN(_(e_nobufnr), count);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155 }
1156 else if (dir == FORWARD)
1157 EMSG(_("E87: Cannot go beyond last buffer"));
1158 else
1159 EMSG(_("E88: Cannot go before first buffer"));
1160 return FAIL;
1161 }
1162
1163#ifdef FEAT_GUI
1164 need_mouse_correct = TRUE;
1165#endif
1166
1167#ifdef FEAT_LISTCMDS
1168 /*
1169 * delete buffer buf from memory and/or the list
1170 */
1171 if (unload)
1172 {
1173 int forward;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174
1175 /* When unloading or deleting a buffer that's already unloaded and
1176 * unlisted: fail silently. */
1177 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1178 return FAIL;
1179
1180 if (!forceit && bufIsChanged(buf))
1181 {
1182#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1183 if ((p_confirm || cmdmod.confirm) && p_write)
1184 {
1185 dialog_changed(buf, FALSE);
1186# ifdef FEAT_AUTOCMD
1187 if (!buf_valid(buf))
1188 /* Autocommand deleted buffer, oops! It's not changed
1189 * now. */
1190 return FAIL;
1191# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001192 /* If it's still changed fail silently, the dialog already
1193 * mentioned why it fails. */
1194 if (bufIsChanged(buf))
1195 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001197 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198#endif
1199 {
1200 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1201 buf->b_fnum);
1202 return FAIL;
1203 }
1204 }
1205
1206 /*
1207 * If deleting the last (listed) buffer, make it empty.
1208 * The last (listed) buffer cannot be unloaded.
1209 */
1210 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1211 if (bp->b_p_bl && bp != buf)
1212 break;
1213 if (bp == NULL && buf == curbuf)
Bram Moolenaara02471e2014-01-10 16:43:14 +01001214 return empty_curbuf(TRUE, forceit, action);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215
1216#ifdef FEAT_WINDOWS
1217 /*
1218 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001219 * (unless it's the only window). Repeat this so long as we end up in
1220 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001222 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001223# ifdef FEAT_AUTOCMD
1224 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1225# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001226 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001227 {
1228 if (win_close(curwin, FALSE) == FAIL)
1229 break;
1230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231#endif
1232
1233 /*
1234 * If the buffer to be deleted is not the current one, delete it here.
1235 */
1236 if (buf != curbuf)
1237 {
1238#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001239 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240#endif
1241 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001242 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 return OK;
1244 }
1245
1246 /*
1247 * Deleting the current buffer: Need to find another buffer to go to.
Bram Moolenaara02471e2014-01-10 16:43:14 +01001248 * There should be another, otherwise it would have been handled
1249 * above. However, autocommands may have deleted all buffers.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 * First use au_new_curbuf, if it is valid.
1251 * Then prefer the buffer we most recently visited.
1252 * Else try to find one that is loaded, after the current buffer,
1253 * then before the current buffer.
1254 * Finally use any buffer.
1255 */
1256 buf = NULL; /* selected buffer */
1257 bp = NULL; /* used when no loaded buffer found */
1258#ifdef FEAT_AUTOCMD
1259 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1260 buf = au_new_curbuf;
1261# ifdef FEAT_JUMPLIST
1262 else
1263# endif
1264#endif
1265#ifdef FEAT_JUMPLIST
1266 if (curwin->w_jumplistlen > 0)
1267 {
1268 int jumpidx;
1269
1270 jumpidx = curwin->w_jumplistidx - 1;
1271 if (jumpidx < 0)
1272 jumpidx = curwin->w_jumplistlen - 1;
1273
1274 forward = jumpidx;
1275 while (jumpidx != curwin->w_jumplistidx)
1276 {
1277 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1278 if (buf != NULL)
1279 {
1280 if (buf == curbuf || !buf->b_p_bl)
1281 buf = NULL; /* skip current and unlisted bufs */
1282 else if (buf->b_ml.ml_mfp == NULL)
1283 {
1284 /* skip unloaded buf, but may keep it for later */
1285 if (bp == NULL)
1286 bp = buf;
1287 buf = NULL;
1288 }
1289 }
1290 if (buf != NULL) /* found a valid buffer: stop searching */
1291 break;
1292 /* advance to older entry in jump list */
1293 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1294 break;
1295 if (--jumpidx < 0)
1296 jumpidx = curwin->w_jumplistlen - 1;
1297 if (jumpidx == forward) /* List exhausted for sure */
1298 break;
1299 }
1300 }
1301#endif
1302
1303 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1304 {
1305 forward = TRUE;
1306 buf = curbuf->b_next;
1307 for (;;)
1308 {
1309 if (buf == NULL)
1310 {
1311 if (!forward) /* tried both directions */
1312 break;
1313 buf = curbuf->b_prev;
1314 forward = FALSE;
1315 continue;
1316 }
1317 /* in non-help buffer, try to skip help buffers, and vv */
1318 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1319 {
1320 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1321 break;
1322 if (bp == NULL) /* remember unloaded buf for later */
1323 bp = buf;
1324 }
1325 if (forward)
1326 buf = buf->b_next;
1327 else
1328 buf = buf->b_prev;
1329 }
1330 }
1331 if (buf == NULL) /* No loaded buffer, use unloaded one */
1332 buf = bp;
1333 if (buf == NULL) /* No loaded buffer, find listed one */
1334 {
1335 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1336 if (buf->b_p_bl && buf != curbuf)
1337 break;
1338 }
1339 if (buf == NULL) /* Still no buffer, just take one */
1340 {
1341 if (curbuf->b_next != NULL)
1342 buf = curbuf->b_next;
1343 else
1344 buf = curbuf->b_prev;
1345 }
1346 }
1347
Bram Moolenaara02471e2014-01-10 16:43:14 +01001348 if (buf == NULL)
1349 {
1350 /* Autocommands must have wiped out all other buffers. Only option
1351 * now is to make the current buffer empty. */
1352 return empty_curbuf(FALSE, forceit, action);
1353 }
1354
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 /*
1356 * make buf current buffer
1357 */
1358 if (action == DOBUF_SPLIT) /* split window first */
1359 {
1360# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001361 /* If 'switchbuf' contains "useopen": jump to first window containing
1362 * "buf" if one exists */
1363 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001364 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001365 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001366 * page containing "buf" if one exists */
1367 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 return OK;
1369 if (win_split(0, 0) == FAIL)
1370# endif
1371 return FAIL;
1372 }
1373#endif
1374
1375 /* go to current buffer - nothing to do */
1376 if (buf == curbuf)
1377 return OK;
1378
1379 /*
1380 * Check if the current buffer may be abandoned.
1381 */
1382 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1383 {
1384#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1385 if ((p_confirm || cmdmod.confirm) && p_write)
1386 {
1387 dialog_changed(curbuf, FALSE);
1388# ifdef FEAT_AUTOCMD
1389 if (!buf_valid(buf))
1390 /* Autocommand deleted buffer, oops! */
1391 return FAIL;
1392# endif
1393 }
1394 if (bufIsChanged(curbuf))
1395#endif
1396 {
1397 EMSG(_(e_nowrtmsg));
1398 return FAIL;
1399 }
1400 }
1401
1402 /* Go to the other buffer. */
1403 set_curbuf(buf, action);
1404
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001405#if defined(FEAT_LISTCMDS) \
1406 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001408 {
1409 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411#endif
1412
1413#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1414 if (aborting()) /* autocmds may abort script processing */
1415 return FAIL;
1416#endif
1417
1418 return OK;
1419}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421
1422/*
1423 * Set current buffer to "buf". Executes autocommands and closes current
1424 * buffer. "action" tells how to close the current buffer:
1425 * DOBUF_GOTO free or hide it
1426 * DOBUF_SPLIT nothing
1427 * DOBUF_UNLOAD unload it
1428 * DOBUF_DEL delete it
1429 * DOBUF_WIPE wipe it out
1430 */
1431 void
1432set_curbuf(buf, action)
1433 buf_T *buf;
1434 int action;
1435{
1436 buf_T *prevbuf;
1437 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1438 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001439#ifdef FEAT_SYN_HL
1440 long old_tw = curbuf->b_p_tw;
1441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442
1443 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001444 if (!cmdmod.keepalt)
1445 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001446 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 /* Don't restart Select mode after switching to another buffer. */
1449 VIsual_reselect = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450
1451 /* close_windows() or apply_autocmds() may change curbuf */
1452 prevbuf = curbuf;
1453
1454#ifdef FEAT_AUTOCMD
1455 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1456# ifdef FEAT_EVAL
1457 if (buf_valid(prevbuf) && !aborting())
1458# else
1459 if (buf_valid(prevbuf))
1460# endif
1461#endif
1462 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001463#ifdef FEAT_SYN_HL
1464 if (prevbuf == curwin->w_buffer)
1465 reset_synblock(curwin);
1466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467#ifdef FEAT_WINDOWS
1468 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001469 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#endif
1471#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1472 if (buf_valid(prevbuf) && !aborting())
1473#else
1474 if (buf_valid(prevbuf))
1475#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001476 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001477#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001478 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001479#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001480 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001481 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1483 unload ? action : (action == DOBUF_GOTO
1484 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001485 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001486#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001487 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001488 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001489 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001490#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 }
1493#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001494 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001495 * it did ":bunload") or aborted the script processing!
1496 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1497 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001498# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001499 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001500# endif
1501# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001502 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001503# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001504 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001506 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001508#ifdef FEAT_SYN_HL
1509 if (old_tw != curbuf->b_p_tw)
1510 check_colorcolumn(curwin);
1511#endif
1512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513}
1514
1515/*
1516 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001517 * Old curbuf must have been abandoned already! This also means "curbuf" may
1518 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001519 */
1520 void
1521enter_buffer(buf)
1522 buf_T *buf;
1523{
1524 /* Copy buffer and window local option values. Not for a help buffer. */
1525 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1526 if (!buf->b_help)
1527 get_winopts(buf);
1528#ifdef FEAT_FOLDING
1529 else
1530 /* Remove all folds in the window. */
1531 clearFolding(curwin);
1532 foldUpdateAll(curwin); /* update folds (later). */
1533#endif
1534
1535 /* Get the buffer in the current window. */
1536 curwin->w_buffer = buf;
1537 curbuf = buf;
1538 ++curbuf->b_nwindows;
1539
1540#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001541 if (curwin->w_p_diff)
1542 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543#endif
1544
Bram Moolenaara971b822011-09-14 14:43:25 +02001545#ifdef FEAT_SYN_HL
1546 curwin->w_s = &(buf->b_s);
1547#endif
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 /* Cursor on first line by default. */
1550 curwin->w_cursor.lnum = 1;
1551 curwin->w_cursor.col = 0;
1552#ifdef FEAT_VIRTUALEDIT
1553 curwin->w_cursor.coladd = 0;
1554#endif
1555 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001556#ifdef FEAT_AUTOCMD
1557 curwin->w_topline_was_set = FALSE;
1558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001560 /* mark cursor position as being invalid */
1561 curwin->w_valid = 0;
1562
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 /* Make sure the buffer is loaded. */
1564 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001565 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001566#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001567 /* If there is no filetype, allow for detecting one. Esp. useful for
1568 * ":ball" used in a autocommand. If there already is a filetype we
1569 * might prefer to keep it. */
1570 if (*curbuf->b_p_ft == NUL)
1571 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001572#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001573
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001574 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001575 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 else
1577 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001578 if (!msg_silent)
1579 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1581#ifdef FEAT_AUTOCMD
1582 curwin->w_topline = 1;
1583# ifdef FEAT_DIFF
1584 curwin->w_topfill = 0;
1585# endif
1586 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1587 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1588#endif
1589 }
1590
1591 /* If autocommands did not change the cursor position, restore cursor lnum
1592 * and possibly cursor col. */
1593 if (curwin->w_cursor.lnum == 1 && inindent(0))
1594 buflist_getfpos();
1595
1596 check_arg_idx(curwin); /* check for valid arg_idx */
1597#ifdef FEAT_TITLE
1598 maketitle();
1599#endif
1600#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001601 /* when autocmds didn't change it */
1602 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603#endif
1604 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1605
Bram Moolenaar009b2592004-10-24 19:18:58 +00001606#ifdef FEAT_NETBEANS_INTG
1607 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001608 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001609#endif
1610
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001611 /* Change directories when the 'acd' option is set. */
1612 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614#ifdef FEAT_KEYMAP
1615 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001616 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001618#ifdef FEAT_SPELL
1619 /* May need to set the spell language. Can only do this after the buffer
1620 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001621 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1622 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001623#endif
1624
Bram Moolenaar071d4272004-06-13 20:20:40 +00001625 redraw_later(NOT_VALID);
1626}
1627
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001628#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1629/*
1630 * Change to the directory of the current buffer.
1631 */
1632 void
1633do_autochdir()
1634{
1635 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1636 shorten_fnames(TRUE);
1637}
1638#endif
1639
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640/*
1641 * functions for dealing with the buffer list
1642 */
1643
1644/*
1645 * Add a file name to the buffer list. Return a pointer to the buffer.
1646 * If the same file name already exists return a pointer to that buffer.
1647 * If it does not exist, or if fname == NULL, a new entry is created.
1648 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1649 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1650 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 * This is the ONLY way to create a new buffer.
1652 */
1653static int top_file_num = 1; /* highest file number */
1654
1655 buf_T *
1656buflist_new(ffname, sfname, lnum, flags)
1657 char_u *ffname; /* full path of fname or relative */
1658 char_u *sfname; /* short fname or NULL */
1659 linenr_T lnum; /* preferred cursor line */
1660 int flags; /* BLN_ defines */
1661{
1662 buf_T *buf;
1663#ifdef UNIX
1664 struct stat st;
1665#endif
1666
1667 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1668
1669 /*
1670 * If file name already exists in the list, update the entry.
1671 */
1672#ifdef UNIX
1673 /* On Unix we can use inode numbers when the file exists. Works better
1674 * for hard links. */
1675 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1676 st.st_dev = (dev_T)-1;
1677#endif
1678 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1679#ifdef UNIX
1680 buflist_findname_stat(ffname, &st)
1681#else
1682 buflist_findname(ffname)
1683#endif
1684 ) != NULL)
1685 {
1686 vim_free(ffname);
1687 if (lnum != 0)
1688 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1689 /* copy the options now, if 'cpo' doesn't have 's' and not done
1690 * already */
1691 buf_copy_options(buf, 0);
1692 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1693 {
1694 buf->b_p_bl = TRUE;
1695#ifdef FEAT_AUTOCMD
1696 if (!(flags & BLN_DUMMY))
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001699 if (!buf_valid(buf))
1700 return NULL;
1701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#endif
1703 }
1704 return buf;
1705 }
1706
1707 /*
1708 * If the current buffer has no name and no contents, use the current
1709 * buffer. Otherwise: Need to allocate a new buffer structure.
1710 *
1711 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001712 * (A spell file buffer is allocated in spell.c, but that's not a normal
1713 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 */
1715 buf = NULL;
1716 if ((flags & BLN_CURBUF)
1717 && curbuf != NULL
1718 && curbuf->b_ffname == NULL
1719 && curbuf->b_nwindows <= 1
1720 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1721 {
1722 buf = curbuf;
1723#ifdef FEAT_AUTOCMD
1724 /* It's like this buffer is deleted. Watch out for autocommands that
1725 * change curbuf! If that happens, allocate a new buffer anyway. */
1726 if (curbuf->b_p_bl)
1727 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1728 if (buf == curbuf)
1729 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1730# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001731 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 return NULL;
1733# endif
1734#endif
1735#ifdef FEAT_QUICKFIX
1736# ifdef FEAT_AUTOCMD
1737 if (buf == curbuf)
1738# endif
1739 {
1740 /* Make sure 'bufhidden' and 'buftype' are empty */
1741 clear_string_option(&buf->b_p_bh);
1742 clear_string_option(&buf->b_p_bt);
1743 }
1744#endif
1745 }
1746 if (buf != curbuf || curbuf == NULL)
1747 {
1748 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1749 if (buf == NULL)
1750 {
1751 vim_free(ffname);
1752 return NULL;
1753 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001754#ifdef FEAT_EVAL
1755 /* init b: variables */
1756 buf->b_vars = dict_alloc();
1757 if (buf->b_vars == NULL)
1758 {
1759 vim_free(ffname);
1760 vim_free(buf);
1761 return NULL;
1762 }
1763 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1764#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 }
1766
1767 if (ffname != NULL)
1768 {
1769 buf->b_ffname = ffname;
1770 buf->b_sfname = vim_strsave(sfname);
1771 }
1772
1773 clear_wininfo(buf);
1774 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1775
1776 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1777 || buf->b_wininfo == NULL)
1778 {
1779 vim_free(buf->b_ffname);
1780 buf->b_ffname = NULL;
1781 vim_free(buf->b_sfname);
1782 buf->b_sfname = NULL;
1783 if (buf != curbuf)
1784 free_buffer(buf);
1785 return NULL;
1786 }
1787
1788 if (buf == curbuf)
1789 {
1790 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001791 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792 if (buf != curbuf) /* autocommands deleted the buffer! */
1793 return NULL;
1794#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001795 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 return NULL;
1797#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001799
1800 /* Init the options. */
1801 buf->b_p_initialized = FALSE;
1802 buf_copy_options(buf, BCO_ENTER);
1803
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#ifdef FEAT_KEYMAP
1805 /* need to reload lmaps and set b:keymap_name */
1806 curbuf->b_kmap_state |= KEYMAP_INIT;
1807#endif
1808 }
1809 else
1810 {
1811 /*
1812 * put new buffer at the end of the buffer list
1813 */
1814 buf->b_next = NULL;
1815 if (firstbuf == NULL) /* buffer list is empty */
1816 {
1817 buf->b_prev = NULL;
1818 firstbuf = buf;
1819 }
1820 else /* append new buffer at end of list */
1821 {
1822 lastbuf->b_next = buf;
1823 buf->b_prev = lastbuf;
1824 }
1825 lastbuf = buf;
1826
1827 buf->b_fnum = top_file_num++;
1828 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1829 {
1830 EMSG(_("W14: Warning: List of file names overflow"));
1831 if (emsg_silent == 0)
1832 {
1833 out_flush();
1834 ui_delay(3000L, TRUE); /* make sure it is noticed */
1835 }
1836 top_file_num = 1;
1837 }
1838
1839 /*
1840 * Always copy the options from the current buffer.
1841 */
1842 buf_copy_options(buf, BCO_ALWAYS);
1843 }
1844
1845 buf->b_wininfo->wi_fpos.lnum = lnum;
1846 buf->b_wininfo->wi_win = curwin;
1847
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001848#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001849 hash_init(&buf->b_s.b_keywtab);
1850 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851#endif
1852
1853 buf->b_fname = buf->b_sfname;
1854#ifdef UNIX
1855 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001856 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 else
1858 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001859 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 buf->b_dev = st.st_dev;
1861 buf->b_ino = st.st_ino;
1862 }
1863#endif
1864 buf->b_u_synced = TRUE;
1865 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001866 if (flags & BLN_DUMMY)
1867 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 buf_clear_file(buf);
1869 clrallmarks(buf); /* clear marks */
1870 fmarks_check_names(buf); /* check file marks for this file */
1871 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1872#ifdef FEAT_AUTOCMD
1873 if (!(flags & BLN_DUMMY))
1874 {
Bram Moolenaar8da9bbf2015-02-27 19:34:56 +01001875 /* Tricky: these autocommands may change the buffer list. They could
1876 * also split the window with re-using the one empty buffer. This may
1877 * result in unexpectedly losing the empty buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001879 if (!buf_valid(buf))
1880 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (flags & BLN_LISTED)
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001882 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
Bram Moolenaar4c7ab1b2014-04-06 20:45:43 +02001884 if (!buf_valid(buf))
1885 return NULL;
1886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001888 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 return NULL;
1890# endif
1891 }
1892#endif
1893
1894 return buf;
1895}
1896
1897/*
1898 * Free the memory for the options of a buffer.
1899 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1900 * 'fileencoding'.
1901 */
1902 void
1903free_buf_options(buf, free_p_ff)
1904 buf_T *buf;
1905 int free_p_ff;
1906{
1907 if (free_p_ff)
1908 {
1909#ifdef FEAT_MBYTE
1910 clear_string_option(&buf->b_p_fenc);
1911#endif
1912 clear_string_option(&buf->b_p_ff);
1913#ifdef FEAT_QUICKFIX
1914 clear_string_option(&buf->b_p_bh);
1915 clear_string_option(&buf->b_p_bt);
1916#endif
1917 }
1918#ifdef FEAT_FIND_ID
1919 clear_string_option(&buf->b_p_def);
1920 clear_string_option(&buf->b_p_inc);
1921# ifdef FEAT_EVAL
1922 clear_string_option(&buf->b_p_inex);
1923# endif
1924#endif
1925#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1926 clear_string_option(&buf->b_p_inde);
1927 clear_string_option(&buf->b_p_indk);
1928#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001929#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1930 clear_string_option(&buf->b_p_bexpr);
1931#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001932#if defined(FEAT_CRYPT)
1933 clear_string_option(&buf->b_p_cm);
1934#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001935#if defined(FEAT_EVAL)
1936 clear_string_option(&buf->b_p_fex);
1937#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#ifdef FEAT_CRYPT
1939 clear_string_option(&buf->b_p_key);
1940#endif
1941 clear_string_option(&buf->b_p_kp);
1942 clear_string_option(&buf->b_p_mps);
1943 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001944 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 clear_string_option(&buf->b_p_isk);
1946#ifdef FEAT_KEYMAP
1947 clear_string_option(&buf->b_p_keymap);
1948 ga_clear(&buf->b_kmap_ga);
1949#endif
1950#ifdef FEAT_COMMENTS
1951 clear_string_option(&buf->b_p_com);
1952#endif
1953#ifdef FEAT_FOLDING
1954 clear_string_option(&buf->b_p_cms);
1955#endif
1956 clear_string_option(&buf->b_p_nf);
1957#ifdef FEAT_SYN_HL
1958 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001959#endif
1960#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001961 clear_string_option(&buf->b_s.b_p_spc);
1962 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001963 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001964 buf->b_s.b_cap_prog = NULL;
1965 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966#endif
1967#ifdef FEAT_SEARCHPATH
1968 clear_string_option(&buf->b_p_sua);
1969#endif
1970#ifdef FEAT_AUTOCMD
1971 clear_string_option(&buf->b_p_ft);
1972#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973#ifdef FEAT_CINDENT
1974 clear_string_option(&buf->b_p_cink);
1975 clear_string_option(&buf->b_p_cino);
1976#endif
1977#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1978 clear_string_option(&buf->b_p_cinw);
1979#endif
1980#ifdef FEAT_INS_EXPAND
1981 clear_string_option(&buf->b_p_cpt);
1982#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001983#ifdef FEAT_COMPL_FUNC
1984 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001985 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987#ifdef FEAT_QUICKFIX
1988 clear_string_option(&buf->b_p_gp);
1989 clear_string_option(&buf->b_p_mp);
1990 clear_string_option(&buf->b_p_efm);
1991#endif
1992 clear_string_option(&buf->b_p_ep);
1993 clear_string_option(&buf->b_p_path);
1994 clear_string_option(&buf->b_p_tags);
1995#ifdef FEAT_INS_EXPAND
1996 clear_string_option(&buf->b_p_dict);
1997 clear_string_option(&buf->b_p_tsr);
1998#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001999#ifdef FEAT_TEXTOBJ
2000 clear_string_option(&buf->b_p_qe);
2001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01002003 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaaraf6c1312014-03-12 18:55:58 +01002004#ifdef FEAT_LISP
2005 clear_string_option(&buf->b_p_lw);
2006#endif
Bram Moolenaarb8ee25a2014-09-23 15:45:08 +02002007 clear_string_option(&buf->b_p_bkc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008}
2009
2010/*
2011 * get alternate file n
2012 * set linenr to lnum or altfpos.lnum if lnum == 0
2013 * also set cursor column to altfpos.col if 'startofline' is not set.
2014 * if (options & GETF_SETMARK) call setpcmark()
2015 * if (options & GETF_ALT) we are jumping to an alternate file.
2016 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
2017 *
2018 * return FAIL for failure, OK for success
2019 */
2020 int
2021buflist_getfile(n, lnum, options, forceit)
2022 int n;
2023 linenr_T lnum;
2024 int options;
2025 int forceit;
2026{
2027 buf_T *buf;
2028#ifdef FEAT_WINDOWS
2029 win_T *wp = NULL;
2030#endif
2031 pos_T *fpos;
2032 colnr_T col;
2033
2034 buf = buflist_findnr(n);
2035 if (buf == NULL)
2036 {
2037 if ((options & GETF_ALT) && n == 0)
2038 EMSG(_(e_noalt));
2039 else
2040 EMSGN(_("E92: Buffer %ld not found"), n);
2041 return FAIL;
2042 }
2043
2044 /* if alternate file is the current buffer, nothing to do */
2045 if (buf == curbuf)
2046 return OK;
2047
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002048 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002049 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00002050 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00002052 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002053#ifdef FEAT_AUTOCMD
2054 if (curbuf_locked())
2055 return FAIL;
2056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057
2058 /* altfpos may be changed by getfile(), get it now */
2059 if (lnum == 0)
2060 {
2061 fpos = buflist_findfpos(buf);
2062 lnum = fpos->lnum;
2063 col = fpos->col;
2064 }
2065 else
2066 col = 0;
2067
2068#ifdef FEAT_WINDOWS
2069 if (options & GETF_SWITCH)
2070 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002071 /* If 'switchbuf' contains "useopen": jump to first window containing
2072 * "buf" if one exists */
2073 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 wp = buf_jump_open_win(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002075
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002076 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002077 * page containing "buf" if one exists */
2078 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002079 wp = buf_jump_open_tab(buf);
Bram Moolenaara594d772015-06-19 14:41:49 +02002080
2081 /* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
2082 * current buffer isn't empty: open new tab or window */
2083 if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
2084 && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 {
Bram Moolenaara594d772015-06-19 14:41:49 +02002086 if (swb_flags & SWB_NEWTAB)
Bram Moolenaarf2330482008-06-24 20:19:36 +00002087 tabpage_new();
Bram Moolenaara594d772015-06-19 14:41:49 +02002088 else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
2089 == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002091 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
2093 }
2094#endif
2095
2096 ++RedrawingDisabled;
2097 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2098 lnum, forceit) <= 0)
2099 {
2100 --RedrawingDisabled;
2101
2102 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2103 if (!p_sol && col != 0)
2104 {
2105 curwin->w_cursor.col = col;
2106 check_cursor_col();
2107#ifdef FEAT_VIRTUALEDIT
2108 curwin->w_cursor.coladd = 0;
2109#endif
2110 curwin->w_set_curswant = TRUE;
2111 }
2112 return OK;
2113 }
2114 --RedrawingDisabled;
2115 return FAIL;
2116}
2117
2118/*
2119 * go to the last know line number for the current buffer
2120 */
2121 void
2122buflist_getfpos()
2123{
2124 pos_T *fpos;
2125
2126 fpos = buflist_findfpos(curbuf);
2127
2128 curwin->w_cursor.lnum = fpos->lnum;
2129 check_cursor_lnum();
2130
2131 if (p_sol)
2132 curwin->w_cursor.col = 0;
2133 else
2134 {
2135 curwin->w_cursor.col = fpos->col;
2136 check_cursor_col();
2137#ifdef FEAT_VIRTUALEDIT
2138 curwin->w_cursor.coladd = 0;
2139#endif
2140 curwin->w_set_curswant = TRUE;
2141 }
2142}
2143
Bram Moolenaar81695252004-12-29 20:58:21 +00002144#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2145/*
2146 * Find file in buffer list by name (it has to be for the current window).
2147 * Returns NULL if not found.
2148 */
2149 buf_T *
2150buflist_findname_exp(fname)
2151 char_u *fname;
2152{
2153 char_u *ffname;
2154 buf_T *buf = NULL;
2155
2156 /* First make the name into a full path name */
2157 ffname = FullName_save(fname,
2158#ifdef UNIX
2159 TRUE /* force expansion, get rid of symbolic links */
2160#else
2161 FALSE
2162#endif
2163 );
2164 if (ffname != NULL)
2165 {
2166 buf = buflist_findname(ffname);
2167 vim_free(ffname);
2168 }
2169 return buf;
2170}
2171#endif
2172
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173/*
2174 * Find file in buffer list by name (it has to be for the current window).
2175 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002176 * Skips dummy buffers.
2177 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 */
2179 buf_T *
2180buflist_findname(ffname)
2181 char_u *ffname;
2182{
2183#ifdef UNIX
2184 struct stat st;
2185
2186 if (mch_stat((char *)ffname, &st) < 0)
2187 st.st_dev = (dev_T)-1;
2188 return buflist_findname_stat(ffname, &st);
2189}
2190
2191/*
2192 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2193 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002194 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 */
2196 static buf_T *
2197buflist_findname_stat(ffname, stp)
2198 char_u *ffname;
2199 struct stat *stp;
2200{
2201#endif
2202 buf_T *buf;
2203
2204 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002205 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206#ifdef UNIX
2207 , stp
2208#endif
2209 ))
2210 return buf;
2211 return NULL;
2212}
2213
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002214#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2215 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216/*
2217 * Find file in buffer list by a regexp pattern.
2218 * Return fnum of the found buffer.
2219 * Return < 0 for error.
2220 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002222buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 char_u *pattern;
2224 char_u *pattern_end; /* pointer to first char after pattern */
2225 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002226 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002227 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228{
2229 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 int match = -1;
2231 int find_listed;
2232 char_u *pat;
2233 char_u *patend;
2234 int attempt;
2235 char_u *p;
2236 int toggledollar;
2237
2238 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2239 {
2240 if (*pattern == '%')
2241 match = curbuf->b_fnum;
2242 else
2243 match = curwin->w_alt_fnum;
2244#ifdef FEAT_DIFF
2245 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2246 match = -1;
2247#endif
2248 }
2249
2250 /*
2251 * Try four ways of matching a listed buffer:
2252 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002253 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 * attempt == 2: with '$' at end (only match at end)
2255 * attempt == 3: with '^' at start and '$' at end (only full match)
2256 * Repeat this for finding an unlisted buffer if there was no matching
2257 * listed buffer.
2258 */
2259 else
2260 {
2261 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2262 if (pat == NULL)
2263 return -1;
2264 patend = pat + STRLEN(pat) - 1;
2265 toggledollar = (patend > pat && *patend == '$');
2266
2267 /* First try finding a listed buffer. If not found and "unlisted"
2268 * is TRUE, try finding an unlisted buffer. */
2269 find_listed = TRUE;
2270 for (;;)
2271 {
2272 for (attempt = 0; attempt <= 3; ++attempt)
2273 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002274 regmatch_T regmatch;
2275
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 /* may add '^' and '$' */
2277 if (toggledollar)
2278 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2279 p = pat;
2280 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2281 ++p;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002282 regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2283 if (regmatch.regprog == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284 {
2285 vim_free(pat);
2286 return -1;
2287 }
2288
2289 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2290 if (buf->b_p_bl == find_listed
2291#ifdef FEAT_DIFF
2292 && (!diffmode || diff_mode_buf(buf))
2293#endif
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002294 && buflist_match(&regmatch, buf, FALSE) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002296 if (curtab_only)
2297 {
2298 /* Ignore the match if the buffer is not open in
2299 * the current tab. */
2300#ifdef FEAT_WINDOWS
2301 win_T *wp;
2302
2303 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2304 if (wp->w_buffer == buf)
2305 break;
2306 if (wp == NULL)
2307 continue;
2308#else
2309 if (curwin->w_buffer != buf)
2310 continue;
2311#endif
2312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 if (match >= 0) /* already found a match */
2314 {
2315 match = -2;
2316 break;
2317 }
2318 match = buf->b_fnum; /* remember first match */
2319 }
2320
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002321 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 if (match >= 0) /* found one match */
2323 break;
2324 }
2325
2326 /* Only search for unlisted buffers if there was no match with
2327 * a listed buffer. */
2328 if (!unlisted || !find_listed || match != -1)
2329 break;
2330 find_listed = FALSE;
2331 }
2332
2333 vim_free(pat);
2334 }
2335
2336 if (match == -2)
2337 EMSG2(_("E93: More than one match for %s"), pattern);
2338 else if (match < 0)
2339 EMSG2(_("E94: No matching buffer for %s"), pattern);
2340 return match;
2341}
2342#endif
2343
2344#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2345
2346/*
2347 * Find all buffer names that match.
2348 * For command line expansion of ":buf" and ":sbuf".
2349 * Return OK if matches found, FAIL otherwise.
2350 */
2351 int
2352ExpandBufnames(pat, num_file, file, options)
2353 char_u *pat;
2354 int *num_file;
2355 char_u ***file;
2356 int options;
2357{
2358 int count = 0;
2359 buf_T *buf;
2360 int round;
2361 char_u *p;
2362 int attempt;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002363 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364
2365 *num_file = 0; /* return values in case of FAIL */
2366 *file = NULL;
2367
Bram Moolenaar05159a02005-02-26 23:04:13 +00002368 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2369 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002371 patc = alloc((unsigned)STRLEN(pat) + 11);
2372 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002374 STRCPY(patc, "\\(^\\|[\\/]\\)");
2375 STRCPY(patc + 11, pat + 1);
2376 }
2377 else
2378 patc = pat;
2379
2380 /*
2381 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002382 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002383 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002384 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002385 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002386 regmatch_T regmatch;
2387
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002388 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002389 break; /* there was no anchor, no need to try again */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002390 regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
2391 if (regmatch.regprog == NULL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002392 {
2393 if (patc != pat)
2394 vim_free(patc);
2395 return FAIL;
2396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 /*
2399 * round == 1: Count the matches.
2400 * round == 2: Build the array to keep the matches.
2401 */
2402 for (round = 1; round <= 2; ++round)
2403 {
2404 count = 0;
2405 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2406 {
2407 if (!buf->b_p_bl) /* skip unlisted buffers */
2408 continue;
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002409 p = buflist_match(&regmatch, buf, p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 if (p != NULL)
2411 {
2412 if (round == 1)
2413 ++count;
2414 else
2415 {
2416 if (options & WILD_HOME_REPLACE)
2417 p = home_replace_save(buf, p);
2418 else
2419 p = vim_strsave(p);
2420 (*file)[count++] = p;
2421 }
2422 }
2423 }
2424 if (count == 0) /* no match found, break here */
2425 break;
2426 if (round == 1)
2427 {
2428 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2429 if (*file == NULL)
2430 {
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002431 vim_regfree(regmatch.regprog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002432 if (patc != pat)
2433 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 return FAIL;
2435 }
2436 }
2437 }
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002438 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 if (count) /* match(es) found, break here */
2440 break;
2441 }
2442
Bram Moolenaar05159a02005-02-26 23:04:13 +00002443 if (patc != pat)
2444 vim_free(patc);
2445
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 *num_file = count;
2447 return (count == 0 ? FAIL : OK);
2448}
2449
2450#endif /* FEAT_CMDL_COMPL */
2451
2452#ifdef HAVE_BUFLIST_MATCH
2453/*
2454 * Check for a match on the file name for buffer "buf" with regprog "prog".
2455 */
2456 static char_u *
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002457buflist_match(rmp, buf, ignore_case)
2458 regmatch_T *rmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459 buf_T *buf;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002460 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461{
2462 char_u *match;
2463
2464 /* First try the short file name, then the long file name. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002465 match = fname_match(rmp, buf->b_sfname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 if (match == NULL)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002467 match = fname_match(rmp, buf->b_ffname, ignore_case);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
2469 return match;
2470}
2471
2472/*
2473 * Try matching the regexp in "prog" with file name "name".
2474 * Return "name" when there is a match, NULL when not.
2475 */
2476 static char_u *
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002477fname_match(rmp, name, ignore_case)
2478 regmatch_T *rmp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 char_u *name;
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002480 int ignore_case; /* when TRUE ignore case, when FALSE use 'fic' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
2482 char_u *match = NULL;
2483 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 if (name != NULL)
2486 {
Bram Moolenaar4b9d6372014-09-23 14:24:40 +02002487 /* Ignore case when 'fileignorecase' or the argument is set. */
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002488 rmp->rm_ic = p_fic || ignore_case;
2489 if (vim_regexec(rmp, name, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 match = name;
2491 else
2492 {
2493 /* Replace $(HOME) with '~' and try matching again. */
2494 p = home_replace_save(NULL, name);
Bram Moolenaardffa5b82014-11-19 16:38:07 +01002495 if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 match = name;
2497 vim_free(p);
2498 }
2499 }
2500
2501 return match;
2502}
2503#endif
2504
2505/*
2506 * find file in buffer list by number
2507 */
2508 buf_T *
2509buflist_findnr(nr)
2510 int nr;
2511{
2512 buf_T *buf;
2513
2514 if (nr == 0)
2515 nr = curwin->w_alt_fnum;
2516 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2517 if (buf->b_fnum == nr)
2518 return (buf);
2519 return NULL;
2520}
2521
2522/*
2523 * Get name of file 'n' in the buffer list.
2524 * When the file has no name an empty string is returned.
2525 * home_replace() is used to shorten the file name (used for marks).
2526 * Returns a pointer to allocated memory, of NULL when failed.
2527 */
2528 char_u *
2529buflist_nr2name(n, fullname, helptail)
2530 int n;
2531 int fullname;
2532 int helptail; /* for help buffers return tail only */
2533{
2534 buf_T *buf;
2535
2536 buf = buflist_findnr(n);
2537 if (buf == NULL)
2538 return NULL;
2539 return home_replace_save(helptail ? buf : NULL,
2540 fullname ? buf->b_ffname : buf->b_fname);
2541}
2542
2543/*
2544 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2545 * When "copy_options" is TRUE save the local window option values.
2546 * When "lnum" is 0 only do the options.
2547 */
2548 static void
2549buflist_setfpos(buf, win, lnum, col, copy_options)
2550 buf_T *buf;
2551 win_T *win;
2552 linenr_T lnum;
2553 colnr_T col;
2554 int copy_options;
2555{
2556 wininfo_T *wip;
2557
2558 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2559 if (wip->wi_win == win)
2560 break;
2561 if (wip == NULL)
2562 {
2563 /* allocate a new entry */
2564 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2565 if (wip == NULL)
2566 return;
2567 wip->wi_win = win;
2568 if (lnum == 0) /* set lnum even when it's 0 */
2569 lnum = 1;
2570 }
2571 else
2572 {
2573 /* remove the entry from the list */
2574 if (wip->wi_prev)
2575 wip->wi_prev->wi_next = wip->wi_next;
2576 else
2577 buf->b_wininfo = wip->wi_next;
2578 if (wip->wi_next)
2579 wip->wi_next->wi_prev = wip->wi_prev;
2580 if (copy_options && wip->wi_optset)
2581 {
2582 clear_winopt(&wip->wi_opt);
2583#ifdef FEAT_FOLDING
2584 deleteFoldRecurse(&wip->wi_folds);
2585#endif
2586 }
2587 }
2588 if (lnum != 0)
2589 {
2590 wip->wi_fpos.lnum = lnum;
2591 wip->wi_fpos.col = col;
2592 }
2593 if (copy_options)
2594 {
2595 /* Save the window-specific option values. */
2596 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2597#ifdef FEAT_FOLDING
2598 wip->wi_fold_manual = win->w_fold_manual;
2599 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2600#endif
2601 wip->wi_optset = TRUE;
2602 }
2603
2604 /* insert the entry in front of the list */
2605 wip->wi_next = buf->b_wininfo;
2606 buf->b_wininfo = wip;
2607 wip->wi_prev = NULL;
2608 if (wip->wi_next)
2609 wip->wi_next->wi_prev = wip;
2610
2611 return;
2612}
2613
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002614#ifdef FEAT_DIFF
2615static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2616
2617/*
2618 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2619 * page. That's because a diff is local to a tab page.
2620 */
2621 static int
2622wininfo_other_tab_diff(wip)
2623 wininfo_T *wip;
2624{
2625 win_T *wp;
2626
2627 if (wip->wi_opt.wo_diff)
2628 {
2629 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2630 /* return FALSE when it's a window in the current tab page, thus
2631 * the buffer was in diff mode here */
2632 if (wip->wi_win == wp)
2633 return FALSE;
2634 return TRUE;
2635 }
2636 return FALSE;
2637}
2638#endif
2639
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640/*
2641 * Find info for the current window in buffer "buf".
2642 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002643 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2644 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 * Returns NULL when there isn't any info.
2646 */
2647 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002648find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002650 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651{
2652 wininfo_T *wip;
2653
2654 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002655 if (wip->wi_win == curwin
2656#ifdef FEAT_DIFF
2657 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2658#endif
2659 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002661
2662 /* If no wininfo for curwin, use the first in the list (that doesn't have
2663 * 'diff' set and is in another tab page). */
2664 if (wip == NULL)
2665 {
2666#ifdef FEAT_DIFF
2667 if (skip_diff_buffer)
2668 {
2669 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2670 if (!wininfo_other_tab_diff(wip))
2671 break;
2672 }
2673 else
2674#endif
2675 wip = buf->b_wininfo;
2676 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 return wip;
2678}
2679
2680/*
2681 * Reset the local window options to the values last used in this window.
2682 * If the buffer wasn't used in this window before, use the values from
2683 * the most recently used window. If the values were never set, use the
2684 * global values for the window.
2685 */
2686 void
2687get_winopts(buf)
2688 buf_T *buf;
2689{
2690 wininfo_T *wip;
2691
2692 clear_winopt(&curwin->w_onebuf_opt);
2693#ifdef FEAT_FOLDING
2694 clearFolding(curwin);
2695#endif
2696
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002697 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 if (wip != NULL && wip->wi_optset)
2699 {
2700 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2701#ifdef FEAT_FOLDING
2702 curwin->w_fold_manual = wip->wi_fold_manual;
2703 curwin->w_foldinvalid = TRUE;
2704 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2705#endif
2706 }
2707 else
2708 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2709
2710#ifdef FEAT_FOLDING
2711 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2712 if (p_fdls >= 0)
2713 curwin->w_p_fdl = p_fdls;
2714#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002715#ifdef FEAT_SYN_HL
2716 check_colorcolumn(curwin);
2717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718}
2719
2720/*
2721 * Find the position (lnum and col) for the buffer 'buf' for the current
2722 * window.
2723 * Returns a pointer to no_position if no position is found.
2724 */
2725 pos_T *
2726buflist_findfpos(buf)
2727 buf_T *buf;
2728{
2729 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002730 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002732 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 if (wip != NULL)
2734 return &(wip->wi_fpos);
2735 else
2736 return &no_position;
2737}
2738
2739/*
2740 * Find the lnum for the buffer 'buf' for the current window.
2741 */
2742 linenr_T
2743buflist_findlnum(buf)
2744 buf_T *buf;
2745{
2746 return buflist_findfpos(buf)->lnum;
2747}
2748
2749#if defined(FEAT_LISTCMDS) || defined(PROTO)
2750/*
2751 * List all know file names (for :files and :buffers command).
2752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 void
2754buflist_list(eap)
2755 exarg_T *eap;
2756{
2757 buf_T *buf;
2758 int len;
2759 int i;
2760
2761 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2762 {
2763 /* skip unlisted buffers, unless ! was used */
2764 if (!buf->b_p_bl && !eap->forceit)
2765 continue;
2766 msg_putchar('\n');
2767 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002768 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769 else
2770 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2771
Bram Moolenaar50cde822005-06-05 21:54:54 +00002772 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 buf->b_fnum,
2774 buf->b_p_bl ? ' ' : 'u',
2775 buf == curbuf ? '%' :
2776 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2777 buf->b_ml.ml_mfp == NULL ? ' ' :
2778 (buf->b_nwindows == 0 ? 'h' : 'a'),
2779 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2780 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002781 : (bufIsChanged(buf) ? '+' : ' '),
2782 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783
2784 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 i = 40 - vim_strsize(IObuff);
2786 do
2787 {
2788 IObuff[len++] = ' ';
2789 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002790 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2791 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002792 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793 msg_outtrans(IObuff);
2794 out_flush(); /* output one line at a time */
2795 ui_breakcheck();
2796 }
2797}
2798#endif
2799
2800/*
2801 * Get file name and line number for file 'fnum'.
2802 * Used by DoOneCmd() for translating '%' and '#'.
2803 * Used by insert_reg() and cmdline_paste() for '#' register.
2804 * Return FAIL if not found, OK for success.
2805 */
2806 int
2807buflist_name_nr(fnum, fname, lnum)
2808 int fnum;
2809 char_u **fname;
2810 linenr_T *lnum;
2811{
2812 buf_T *buf;
2813
2814 buf = buflist_findnr(fnum);
2815 if (buf == NULL || buf->b_fname == NULL)
2816 return FAIL;
2817
2818 *fname = buf->b_fname;
2819 *lnum = buflist_findlnum(buf);
2820
2821 return OK;
2822}
2823
2824/*
2825 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2826 * The file name with the full path is also remembered, for when :cd is used.
2827 * Returns FAIL for failure (file name already in use by other buffer)
2828 * OK otherwise.
2829 */
2830 int
2831setfname(buf, ffname, sfname, message)
2832 buf_T *buf;
2833 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002834 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002835{
Bram Moolenaar81695252004-12-29 20:58:21 +00002836 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837#ifdef UNIX
2838 struct stat st;
2839#endif
2840
2841 if (ffname == NULL || *ffname == NUL)
2842 {
2843 /* Removing the name. */
2844 vim_free(buf->b_ffname);
2845 vim_free(buf->b_sfname);
2846 buf->b_ffname = NULL;
2847 buf->b_sfname = NULL;
2848#ifdef UNIX
2849 st.st_dev = (dev_T)-1;
2850#endif
2851 }
2852 else
2853 {
2854 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2855 if (ffname == NULL) /* out of memory */
2856 return FAIL;
2857
2858 /*
2859 * if the file name is already used in another buffer:
2860 * - if the buffer is loaded, fail
2861 * - if the buffer is not loaded, delete it from the list
2862 */
2863#ifdef UNIX
2864 if (mch_stat((char *)ffname, &st) < 0)
2865 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002866#endif
2867 if (!(buf->b_flags & BF_DUMMY))
2868#ifdef UNIX
2869 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002871 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872#endif
2873 if (obuf != NULL && obuf != buf)
2874 {
2875 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2876 {
2877 if (message)
2878 EMSG(_("E95: Buffer with this name already exists"));
2879 vim_free(ffname);
2880 return FAIL;
2881 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002882 /* delete from the list */
2883 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 }
2885 sfname = vim_strsave(sfname);
2886 if (ffname == NULL || sfname == NULL)
2887 {
2888 vim_free(sfname);
2889 vim_free(ffname);
2890 return FAIL;
2891 }
2892#ifdef USE_FNAME_CASE
2893# ifdef USE_LONG_FNAME
2894 if (USE_LONG_FNAME)
2895# endif
2896 fname_case(sfname, 0); /* set correct case for short file name */
2897#endif
2898 vim_free(buf->b_ffname);
2899 vim_free(buf->b_sfname);
2900 buf->b_ffname = ffname;
2901 buf->b_sfname = sfname;
2902 }
2903 buf->b_fname = buf->b_sfname;
2904#ifdef UNIX
2905 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002906 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 else
2908 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002909 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 buf->b_dev = st.st_dev;
2911 buf->b_ino = st.st_ino;
2912 }
2913#endif
2914
2915#ifndef SHORT_FNAME
2916 buf->b_shortname = FALSE;
2917#endif
2918
2919 buf_name_changed(buf);
2920 return OK;
2921}
2922
2923/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002924 * Crude way of changing the name of a buffer. Use with care!
2925 * The name should be relative to the current directory.
2926 */
2927 void
2928buf_set_name(fnum, name)
2929 int fnum;
2930 char_u *name;
2931{
2932 buf_T *buf;
2933
2934 buf = buflist_findnr(fnum);
2935 if (buf != NULL)
2936 {
2937 vim_free(buf->b_sfname);
2938 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002939 buf->b_ffname = vim_strsave(name);
2940 buf->b_sfname = NULL;
2941 /* Allocate ffname and expand into full path. Also resolves .lnk
2942 * files on Win32. */
2943 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002944 buf->b_fname = buf->b_sfname;
2945 }
2946}
2947
2948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 * Take care of what needs to be done when the name of buffer "buf" has
2950 * changed.
2951 */
2952 void
2953buf_name_changed(buf)
2954 buf_T *buf;
2955{
2956 /*
2957 * If the file name changed, also change the name of the swapfile
2958 */
2959 if (buf->b_ml.ml_mfp != NULL)
2960 ml_setname(buf);
2961
2962 if (curwin->w_buffer == buf)
2963 check_arg_idx(curwin); /* check file name for arg list */
2964#ifdef FEAT_TITLE
2965 maketitle(); /* set window title */
2966#endif
2967#ifdef FEAT_WINDOWS
2968 status_redraw_all(); /* status lines need to be redrawn */
2969#endif
2970 fmarks_check_names(buf); /* check named file marks */
2971 ml_timestamp(buf); /* reset timestamp */
2972}
2973
2974/*
2975 * set alternate file name for current window
2976 *
2977 * Used by do_one_cmd(), do_write() and do_ecmd().
2978 * Return the buffer.
2979 */
2980 buf_T *
2981setaltfname(ffname, sfname, lnum)
2982 char_u *ffname;
2983 char_u *sfname;
2984 linenr_T lnum;
2985{
2986 buf_T *buf;
2987
2988 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2989 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002990 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 curwin->w_alt_fnum = buf->b_fnum;
2992 return buf;
2993}
2994
2995/*
2996 * Get alternate file name for current window.
2997 * Return NULL if there isn't any, and give error message if requested.
2998 */
2999 char_u *
3000getaltfname(errmsg)
3001 int errmsg; /* give error message */
3002{
3003 char_u *fname;
3004 linenr_T dummy;
3005
3006 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
3007 {
3008 if (errmsg)
3009 EMSG(_(e_noalt));
3010 return NULL;
3011 }
3012 return fname;
3013}
3014
3015/*
3016 * Add a file name to the buflist and return its number.
3017 * Uses same flags as buflist_new(), except BLN_DUMMY.
3018 *
3019 * used by qf_init(), main() and doarglist()
3020 */
3021 int
3022buflist_add(fname, flags)
3023 char_u *fname;
3024 int flags;
3025{
3026 buf_T *buf;
3027
3028 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
3029 if (buf != NULL)
3030 return buf->b_fnum;
3031 return 0;
3032}
3033
3034#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
3035/*
3036 * Adjust slashes in file names. Called after 'shellslash' was set.
3037 */
3038 void
3039buflist_slash_adjust()
3040{
3041 buf_T *bp;
3042
3043 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
3044 {
3045 if (bp->b_ffname != NULL)
3046 slash_adjust(bp->b_ffname);
3047 if (bp->b_sfname != NULL)
3048 slash_adjust(bp->b_sfname);
3049 }
3050}
3051#endif
3052
3053/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003054 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 * Also save the local window option values.
3056 */
3057 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003058buflist_altfpos(win)
3059 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00003061 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062}
3063
3064/*
3065 * Return TRUE if 'ffname' is not the same file as current file.
3066 * Fname must have a full path (expanded by mch_FullName()).
3067 */
3068 int
3069otherfile(ffname)
3070 char_u *ffname;
3071{
3072 return otherfile_buf(curbuf, ffname
3073#ifdef UNIX
3074 , NULL
3075#endif
3076 );
3077}
3078
3079 static int
3080otherfile_buf(buf, ffname
3081#ifdef UNIX
3082 , stp
3083#endif
3084 )
3085 buf_T *buf;
3086 char_u *ffname;
3087#ifdef UNIX
3088 struct stat *stp;
3089#endif
3090{
3091 /* no name is different */
3092 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3093 return TRUE;
3094 if (fnamecmp(ffname, buf->b_ffname) == 0)
3095 return FALSE;
3096#ifdef UNIX
3097 {
3098 struct stat st;
3099
3100 /* If no struct stat given, get it now */
3101 if (stp == NULL)
3102 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003103 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 st.st_dev = (dev_T)-1;
3105 stp = &st;
3106 }
3107 /* Use dev/ino to check if the files are the same, even when the names
3108 * are different (possible with links). Still need to compare the
3109 * name above, for when the file doesn't exist yet.
3110 * Problem: The dev/ino changes when a file is deleted (and created
3111 * again) and remains the same when renamed/moved. We don't want to
3112 * mch_stat() each buffer each time, that would be too slow. Get the
3113 * dev/ino again when they appear to match, but not when they appear
3114 * to be different: Could skip a buffer when it's actually the same
3115 * file. */
3116 if (buf_same_ino(buf, stp))
3117 {
3118 buf_setino(buf);
3119 if (buf_same_ino(buf, stp))
3120 return FALSE;
3121 }
3122 }
3123#endif
3124 return TRUE;
3125}
3126
3127#if defined(UNIX) || defined(PROTO)
3128/*
3129 * Set inode and device number for a buffer.
3130 * Must always be called when b_fname is changed!.
3131 */
3132 void
3133buf_setino(buf)
3134 buf_T *buf;
3135{
3136 struct stat st;
3137
3138 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3139 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003140 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 buf->b_dev = st.st_dev;
3142 buf->b_ino = st.st_ino;
3143 }
3144 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003145 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146}
3147
3148/*
3149 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3150 */
3151 static int
3152buf_same_ino(buf, stp)
3153 buf_T *buf;
3154 struct stat *stp;
3155{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003156 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 && stp->st_dev == buf->b_dev
3158 && stp->st_ino == buf->b_ino);
3159}
3160#endif
3161
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003162/*
3163 * Print info about the current buffer.
3164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 void
3166fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003167 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 int shorthelp;
3169 int dont_truncate;
3170{
3171 char_u *name;
3172 int n;
3173 char_u *p;
3174 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003175 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176
3177 buffer = alloc(IOSIZE);
3178 if (buffer == NULL)
3179 return;
3180
3181 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3182 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003183 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 p = buffer + STRLEN(buffer);
3185 }
3186 else
3187 p = buffer;
3188
3189 *p++ = '"';
3190 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003191 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 else
3193 {
3194 if (!fullname && curbuf->b_fname != NULL)
3195 name = curbuf->b_fname;
3196 else
3197 name = curbuf->b_ffname;
3198 home_replace(shorthelp ? curbuf : NULL, name, p,
3199 (int)(IOSIZE - (p - buffer)), TRUE);
3200 }
3201
Bram Moolenaara800b422010-06-27 01:15:55 +02003202 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203 curbufIsChanged() ? (shortmess(SHM_MOD)
3204 ? " [+]" : _(" [Modified]")) : " ",
3205 (curbuf->b_flags & BF_NOTEDITED)
3206#ifdef FEAT_QUICKFIX
3207 && !bt_dontwrite(curbuf)
3208#endif
3209 ? _("[Not edited]") : "",
3210 (curbuf->b_flags & BF_NEW)
3211#ifdef FEAT_QUICKFIX
3212 && !bt_dontwrite(curbuf)
3213#endif
3214 ? _("[New file]") : "",
3215 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003216 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 : _("[readonly]")) : "",
3218 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3219 || curbuf->b_p_ro) ?
3220 " " : "");
3221 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3222 * causes an overflow, thus for large numbers divide instead. */
3223 if (curwin->w_cursor.lnum > 1000000L)
3224 n = (int)(((long)curwin->w_cursor.lnum) /
3225 ((long)curbuf->b_ml.ml_line_count / 100L));
3226 else
3227 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3228 (long)curbuf->b_ml.ml_line_count);
3229 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3230 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003231 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233#ifdef FEAT_CMDL_INFO
3234 else if (p_ru)
3235 {
3236 /* Current line and column are already on the screen -- webb */
3237 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003238 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003240 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 (long)curbuf->b_ml.ml_line_count, n);
3242 }
3243#endif
3244 else
3245 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003246 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 _("line %ld of %ld --%d%%-- col "),
3248 (long)curwin->w_cursor.lnum,
3249 (long)curbuf->b_ml.ml_line_count,
3250 n);
3251 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003252 len = STRLEN(buffer);
3253 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003254 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3255 }
3256
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003257 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258
3259 if (dont_truncate)
3260 {
3261 /* Temporarily set msg_scroll to avoid the message being truncated.
3262 * First call msg_start() to get the message in the right place. */
3263 msg_start();
3264 n = msg_scroll;
3265 msg_scroll = TRUE;
3266 msg(buffer);
3267 msg_scroll = n;
3268 }
3269 else
3270 {
3271 p = msg_trunc_attr(buffer, FALSE, 0);
3272 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 /* Need to repeat the message after redrawing when:
3274 * - When restart_edit is set (otherwise there will be a delay
3275 * before redrawing).
3276 * - When the screen was scrolled but there is no wait-return
3277 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003278 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279 }
3280
3281 vim_free(buffer);
3282}
3283
3284 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003285col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003287 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 int col;
3289 int vcol;
3290{
3291 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003292 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003294 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295}
3296
3297#if defined(FEAT_TITLE) || defined(PROTO)
3298/*
3299 * put file name in title bar of window and in icon title
3300 */
3301
3302static char_u *lasttitle = NULL;
3303static char_u *lasticon = NULL;
3304
3305 void
3306maketitle()
3307{
3308 char_u *p;
3309 char_u *t_str = NULL;
3310 char_u *i_name;
3311 char_u *i_str = NULL;
3312 int maxlen = 0;
3313 int len;
3314 int mustset;
3315 char_u buf[IOSIZE];
3316 int off;
3317
3318 if (!redrawing())
3319 {
3320 /* Postpone updating the title when 'lazyredraw' is set. */
3321 need_maketitle = TRUE;
3322 return;
3323 }
3324
3325 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003326 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 return;
3328
3329 if (p_title)
3330 {
3331 if (p_titlelen > 0)
3332 {
3333 maxlen = p_titlelen * Columns / 100;
3334 if (maxlen < 10)
3335 maxlen = 10;
3336 }
3337
3338 t_str = buf;
3339 if (*p_titlestring != NUL)
3340 {
3341#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003342 if (stl_syntax & STL_IN_TITLE)
3343 {
3344 int use_sandbox = FALSE;
3345 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003346
3347# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003348 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003349# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003350 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003352 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003353 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003354 if (called_emsg)
3355 set_string_option_direct((char_u *)"titlestring", -1,
3356 (char_u *)"", OPT_FREE, SID_ERROR);
3357 called_emsg |= save_called_emsg;
3358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 else
3360#endif
3361 t_str = p_titlestring;
3362 }
3363 else
3364 {
3365 /* format: "fname + (path) (1 of 2) - VIM" */
3366
Bram Moolenaar2c666692012-09-05 13:30:40 +02003367#define SPACE_FOR_FNAME (IOSIZE - 100)
3368#define SPACE_FOR_DIR (IOSIZE - 20)
3369#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003371 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 else
3373 {
3374 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003375 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 }
3378
3379 switch (bufIsChanged(curbuf)
3380 + (curbuf->b_p_ro * 2)
3381 + (!curbuf->b_p_ma * 4))
3382 {
3383 case 1: STRCAT(buf, " +"); break;
3384 case 2: STRCAT(buf, " ="); break;
3385 case 3: STRCAT(buf, " =+"); break;
3386 case 4:
3387 case 6: STRCAT(buf, " -"); break;
3388 case 5:
3389 case 7: STRCAT(buf, " -+"); break;
3390 }
3391
3392 if (curbuf->b_fname != NULL)
3393 {
3394 /* Get path of file, replace home dir with ~ */
3395 off = (int)STRLEN(buf);
3396 buf[off++] = ' ';
3397 buf[off++] = '(';
3398 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003399 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400#ifdef BACKSLASH_IN_FILENAME
3401 /* avoid "c:/name" to be reduced to "c" */
3402 if (isalpha(buf[off]) && buf[off + 1] == ':')
3403 off += 2;
3404#endif
3405 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003406 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003409 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003410 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003412 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003413
Bram Moolenaar2c666692012-09-05 13:30:40 +02003414 /* Translate unprintable chars and concatenate. Keep some
3415 * room for the server name. When there is no room (very long
3416 * file name) use (...). */
3417 if (off < SPACE_FOR_DIR)
3418 {
3419 p = transstr(buf + off);
3420 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3421 vim_free(p);
3422 }
3423 else
3424 {
3425 vim_strncpy(buf + off, (char_u *)"...",
3426 (size_t)(SPACE_FOR_ARGNR - off));
3427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003428 STRCAT(buf, ")");
3429 }
3430
Bram Moolenaar2c666692012-09-05 13:30:40 +02003431 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433#if defined(FEAT_CLIENTSERVER)
3434 if (serverName != NULL)
3435 {
3436 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003437 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 }
3439 else
3440#endif
3441 STRCAT(buf, " - VIM");
3442
3443 if (maxlen > 0)
3444 {
3445 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003446 if (vim_strsize(buf) > maxlen)
3447 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 }
3449 }
3450 }
3451 mustset = ti_change(t_str, &lasttitle);
3452
3453 if (p_icon)
3454 {
3455 i_str = buf;
3456 if (*p_iconstring != NUL)
3457 {
3458#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003459 if (stl_syntax & STL_IN_ICON)
3460 {
3461 int use_sandbox = FALSE;
3462 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003463
3464# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003465 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003466# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003467 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003469 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003470 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003471 if (called_emsg)
3472 set_string_option_direct((char_u *)"iconstring", -1,
3473 (char_u *)"", OPT_FREE, SID_ERROR);
3474 called_emsg |= save_called_emsg;
3475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 else
3477#endif
3478 i_str = p_iconstring;
3479 }
3480 else
3481 {
3482 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003483 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 else /* use file name only in icon */
3485 i_name = gettail(curbuf->b_ffname);
3486 *i_str = NUL;
3487 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003488 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (len > 100)
3490 {
3491 len -= 100;
3492#ifdef FEAT_MBYTE
3493 if (has_mbyte)
3494 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3495#endif
3496 i_name += len;
3497 }
3498 STRCPY(i_str, i_name);
3499 trans_characters(i_str, IOSIZE);
3500 }
3501 }
3502
3503 mustset |= ti_change(i_str, &lasticon);
3504
3505 if (mustset)
3506 resettitle();
3507}
3508
3509/*
3510 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3511 * from "str" if it does.
3512 * Return TRUE when "*last" changed.
3513 */
3514 static int
3515ti_change(str, last)
3516 char_u *str;
3517 char_u **last;
3518{
3519 if ((str == NULL) != (*last == NULL)
3520 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3521 {
3522 vim_free(*last);
3523 if (str == NULL)
3524 *last = NULL;
3525 else
3526 *last = vim_strsave(str);
3527 return TRUE;
3528 }
3529 return FALSE;
3530}
3531
3532/*
3533 * Put current window title back (used after calling a shell)
3534 */
3535 void
3536resettitle()
3537{
3538 mch_settitle(lasttitle, lasticon);
3539}
Bram Moolenaarea408852005-06-25 22:49:46 +00003540
3541# if defined(EXITFREE) || defined(PROTO)
3542 void
3543free_titles()
3544{
3545 vim_free(lasttitle);
3546 vim_free(lasticon);
3547}
3548# endif
3549
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550#endif /* FEAT_TITLE */
3551
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003552#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003554 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 * Return length of string in screen cells.
3556 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003557 * Normally works for window "wp", except when working for 'tabline' then it
3558 * is "curwin".
3559 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 * Items are drawn interspersed with the text that surrounds it
3561 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3562 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3563 *
3564 * If maxwidth is not zero, the string will be filled at any middle marker
3565 * or truncated if too long, fillchar is used for all whitespace.
3566 */
3567 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003568build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3569 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003571 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 size_t outlen; /* length of out[] */
3573 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003574 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 int fillchar;
3576 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003577 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3578 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579{
3580 char_u *p;
3581 char_u *s;
3582 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003583 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584#ifdef FEAT_EVAL
3585 win_T *o_curwin;
3586 buf_T *o_curbuf;
3587#endif
3588 int empty_line;
3589 colnr_T virtcol;
3590 long l;
3591 long n;
3592 int prevchar_isflag;
3593 int prevchar_isitem;
3594 int itemisflag;
3595 int fillable;
3596 char_u *str;
3597 long num;
3598 int width;
3599 int itemcnt;
3600 int curitem;
3601 int groupitem[STL_MAX_ITEM];
3602 int groupdepth;
3603 struct stl_item
3604 {
3605 char_u *start;
3606 int minwid;
3607 int maxwid;
3608 enum
3609 {
3610 Normal,
3611 Empty,
3612 Group,
3613 Middle,
3614 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003615 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616 Trunc
3617 } type;
3618 } item[STL_MAX_ITEM];
3619 int minwid;
3620 int maxwid;
3621 int zeropad;
3622 char_u base;
3623 char_u opt;
3624#define TMPLEN 70
3625 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003626 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003627 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003628
3629#ifdef FEAT_EVAL
3630 /*
3631 * When the format starts with "%!" then evaluate it as an expression and
3632 * use the result as the actual format string.
3633 */
3634 if (fmt[0] == '%' && fmt[1] == '!')
3635 {
3636 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3637 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003638 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003639 }
3640#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
3642 if (fillchar == 0)
3643 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003644#ifdef FEAT_MBYTE
3645 /* Can't handle a multi-byte fill character yet. */
3646 else if (mb_char2len(fillchar) > 1)
3647 fillchar = '-';
3648#endif
3649
Bram Moolenaar567199b2013-04-24 16:52:36 +02003650 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3651 * p will become invalid when getting another buffer line. */
3652 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3653 empty_line = (*p == NUL);
3654
3655 /* Get the byte value now, in case we need it below. This is more
3656 * efficient than making a copy of the line. */
3657 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3658 byteval = 0;
3659 else
3660#ifdef FEAT_MBYTE
3661 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3662#else
3663 byteval = p[wp->w_cursor.col];
3664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
3666 groupdepth = 0;
3667 p = out;
3668 curitem = 0;
3669 prevchar_isflag = TRUE;
3670 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003671 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003673 if (curitem == STL_MAX_ITEM)
3674 {
3675 /* There are too many items. Add the error code to the statusline
3676 * to give the user a hint about what went wrong. */
3677 if (p + 6 < out + outlen)
3678 {
3679 mch_memmove(p, " E541", (size_t)5);
3680 p += 5;
3681 }
3682 break;
3683 }
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (*s != NUL && *s != '%')
3686 prevchar_isflag = prevchar_isitem = FALSE;
3687
3688 /*
3689 * Handle up to the next '%' or the end.
3690 */
3691 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3692 *p++ = *s++;
3693 if (*s == NUL || p + 1 >= out + outlen)
3694 break;
3695
3696 /*
3697 * Handle one '%' item.
3698 */
3699 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003700 if (*s == NUL) /* ignore trailing % */
3701 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (*s == '%')
3703 {
3704 if (p + 1 >= out + outlen)
3705 break;
3706 *p++ = *s++;
3707 prevchar_isflag = prevchar_isitem = FALSE;
3708 continue;
3709 }
3710 if (*s == STL_MIDDLEMARK)
3711 {
3712 s++;
3713 if (groupdepth > 0)
3714 continue;
3715 item[curitem].type = Middle;
3716 item[curitem++].start = p;
3717 continue;
3718 }
3719 if (*s == STL_TRUNCMARK)
3720 {
3721 s++;
3722 item[curitem].type = Trunc;
3723 item[curitem++].start = p;
3724 continue;
3725 }
3726 if (*s == ')')
3727 {
3728 s++;
3729 if (groupdepth < 1)
3730 continue;
3731 groupdepth--;
3732
3733 t = item[groupitem[groupdepth]].start;
3734 *p = NUL;
3735 l = vim_strsize(t);
3736 if (curitem > groupitem[groupdepth] + 1
3737 && item[groupitem[groupdepth]].minwid == 0)
3738 {
3739 /* remove group if all items are empty */
3740 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3741 if (item[n].type == Normal)
3742 break;
3743 if (n == curitem)
3744 {
3745 p = t;
3746 l = 0;
3747 }
3748 }
3749 if (l > item[groupitem[groupdepth]].maxwid)
3750 {
3751 /* truncate, remove n bytes of text at the start */
3752#ifdef FEAT_MBYTE
3753 if (has_mbyte)
3754 {
3755 /* Find the first character that should be included. */
3756 n = 0;
3757 while (l >= item[groupitem[groupdepth]].maxwid)
3758 {
3759 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003760 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 }
3762 }
3763 else
3764#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003765 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766
3767 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003768 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 p = p - n + 1;
3770#ifdef FEAT_MBYTE
3771 /* Fill up space left over by half a double-wide char. */
3772 while (++l < item[groupitem[groupdepth]].minwid)
3773 *p++ = fillchar;
3774#endif
3775
3776 /* correct the start of the items for the truncation */
3777 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3778 {
3779 item[l].start -= n;
3780 if (item[l].start < t)
3781 item[l].start = t;
3782 }
3783 }
3784 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3785 {
3786 /* fill */
3787 n = item[groupitem[groupdepth]].minwid;
3788 if (n < 0)
3789 {
3790 /* fill by appending characters */
3791 n = 0 - n;
3792 while (l++ < n && p + 1 < out + outlen)
3793 *p++ = fillchar;
3794 }
3795 else
3796 {
3797 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003798 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 l = n - l;
3800 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003801 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 p += l;
3803 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3804 item[n].start += l;
3805 for ( ; l > 0; l--)
3806 *t++ = fillchar;
3807 }
3808 }
3809 continue;
3810 }
3811 minwid = 0;
3812 maxwid = 9999;
3813 zeropad = FALSE;
3814 l = 1;
3815 if (*s == '0')
3816 {
3817 s++;
3818 zeropad = TRUE;
3819 }
3820 if (*s == '-')
3821 {
3822 s++;
3823 l = -1;
3824 }
3825 if (VIM_ISDIGIT(*s))
3826 {
3827 minwid = (int)getdigits(&s);
3828 if (minwid < 0) /* overflow */
3829 minwid = 0;
3830 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003831 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 {
3833 item[curitem].type = Highlight;
3834 item[curitem].start = p;
3835 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3836 s++;
3837 curitem++;
3838 continue;
3839 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003840 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3841 {
3842 if (*s == STL_TABCLOSENR)
3843 {
3844 if (minwid == 0)
3845 {
3846 /* %X ends the close label, go back to the previously
3847 * define tab label nr. */
3848 for (n = curitem - 1; n >= 0; --n)
3849 if (item[n].type == TabPage && item[n].minwid >= 0)
3850 {
3851 minwid = item[n].minwid;
3852 break;
3853 }
3854 }
3855 else
3856 /* close nrs are stored as negative values */
3857 minwid = - minwid;
3858 }
3859 item[curitem].type = TabPage;
3860 item[curitem].start = p;
3861 item[curitem].minwid = minwid;
3862 s++;
3863 curitem++;
3864 continue;
3865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (*s == '.')
3867 {
3868 s++;
3869 if (VIM_ISDIGIT(*s))
3870 {
3871 maxwid = (int)getdigits(&s);
3872 if (maxwid <= 0) /* overflow */
3873 maxwid = 50;
3874 }
3875 }
3876 minwid = (minwid > 50 ? 50 : minwid) * l;
3877 if (*s == '(')
3878 {
3879 groupitem[groupdepth++] = curitem;
3880 item[curitem].type = Group;
3881 item[curitem].start = p;
3882 item[curitem].minwid = minwid;
3883 item[curitem].maxwid = maxwid;
3884 s++;
3885 curitem++;
3886 continue;
3887 }
3888 if (vim_strchr(STL_ALL, *s) == NULL)
3889 {
3890 s++;
3891 continue;
3892 }
3893 opt = *s++;
3894
3895 /* OK - now for the real work */
3896 base = 'D';
3897 itemisflag = FALSE;
3898 fillable = TRUE;
3899 num = -1;
3900 str = NULL;
3901 switch (opt)
3902 {
3903 case STL_FILEPATH:
3904 case STL_FULLPATH:
3905 case STL_FILENAME:
3906 fillable = FALSE; /* don't change ' ' to fillchar */
3907 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003908 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 else
3910 {
3911 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003912 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3914 }
3915 trans_characters(NameBuff, MAXPATHL);
3916 if (opt != STL_FILENAME)
3917 str = NameBuff;
3918 else
3919 str = gettail(NameBuff);
3920 break;
3921
3922 case STL_VIM_EXPR: /* '{' */
3923 itemisflag = TRUE;
3924 t = p;
3925 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3926 *p++ = *s++;
3927 if (*s != '}') /* missing '}' or out of space */
3928 break;
3929 s++;
3930 *p = 0;
3931 p = t;
3932
3933#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003934 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3936
3937 o_curbuf = curbuf;
3938 o_curwin = curwin;
3939 curwin = wp;
3940 curbuf = wp->w_buffer;
3941
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003942 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
3944 curwin = o_curwin;
3945 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003946 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947
3948 if (str != NULL && *str != 0)
3949 {
3950 if (*skipdigits(str) == NUL)
3951 {
3952 num = atoi((char *)str);
3953 vim_free(str);
3954 str = NULL;
3955 itemisflag = FALSE;
3956 }
3957 }
3958#endif
3959 break;
3960
3961 case STL_LINE:
3962 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3963 ? 0L : (long)(wp->w_cursor.lnum);
3964 break;
3965
3966 case STL_NUMLINES:
3967 num = wp->w_buffer->b_ml.ml_line_count;
3968 break;
3969
3970 case STL_COLUMN:
3971 num = !(State & INSERT) && empty_line
3972 ? 0 : (int)wp->w_cursor.col + 1;
3973 break;
3974
3975 case STL_VIRTCOL:
3976 case STL_VIRTCOL_ALT:
3977 /* In list mode virtcol needs to be recomputed */
3978 virtcol = wp->w_virtcol;
3979 if (wp->w_p_list && lcs_tab1 == NUL)
3980 {
3981 wp->w_p_list = FALSE;
3982 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3983 wp->w_p_list = TRUE;
3984 }
3985 ++virtcol;
3986 /* Don't display %V if it's the same as %c. */
3987 if (opt == STL_VIRTCOL_ALT
3988 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3989 ? 0 : (int)wp->w_cursor.col + 1)))
3990 break;
3991 num = (long)virtcol;
3992 break;
3993
3994 case STL_PERCENTAGE:
3995 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3996 (long)wp->w_buffer->b_ml.ml_line_count);
3997 break;
3998
3999 case STL_ALTPERCENT:
4000 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004001 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002 break;
4003
4004 case STL_ARGLISTSTAT:
4005 fillable = FALSE;
4006 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004007 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 str = tmp;
4009 break;
4010
4011 case STL_KEYMAP:
4012 fillable = FALSE;
4013 if (get_keymap_str(wp, tmp, TMPLEN))
4014 str = tmp;
4015 break;
4016 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004017#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004018 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019#else
4020 num = 0;
4021#endif
4022 break;
4023
4024 case STL_BUFNO:
4025 num = wp->w_buffer->b_fnum;
4026 break;
4027
4028 case STL_OFFSET_X:
4029 base = 'X';
4030 case STL_OFFSET:
4031#ifdef FEAT_BYTEOFF
4032 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
4033 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
4034 0L : l + 1 + (!(State & INSERT) && empty_line ?
4035 0 : (int)wp->w_cursor.col);
4036#endif
4037 break;
4038
4039 case STL_BYTEVAL_X:
4040 base = 'X';
4041 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02004042 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 if (num == NL)
4044 num = 0;
4045 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
4046 num = NL;
4047 break;
4048
4049 case STL_ROFLAG:
4050 case STL_ROFLAG_ALT:
4051 itemisflag = TRUE;
4052 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02004053 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 break;
4055
4056 case STL_HELPFLAG:
4057 case STL_HELPFLAG_ALT:
4058 itemisflag = TRUE;
4059 if (wp->w_buffer->b_help)
4060 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00004061 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 break;
4063
4064#ifdef FEAT_AUTOCMD
4065 case STL_FILETYPE:
4066 if (*wp->w_buffer->b_p_ft != NUL
4067 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4068 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004069 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4070 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 str = tmp;
4072 }
4073 break;
4074
4075 case STL_FILETYPE_ALT:
4076 itemisflag = TRUE;
4077 if (*wp->w_buffer->b_p_ft != NUL
4078 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4079 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004080 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4081 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 for (t = tmp; *t != 0; t++)
4083 *t = TOUPPER_LOC(*t);
4084 str = tmp;
4085 }
4086 break;
4087#endif
4088
4089#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4090 case STL_PREVIEWFLAG:
4091 case STL_PREVIEWFLAG_ALT:
4092 itemisflag = TRUE;
4093 if (wp->w_p_pvw)
4094 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4095 : _("[Preview]"));
4096 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004097
4098 case STL_QUICKFIX:
4099 if (bt_quickfix(wp->w_buffer))
4100 str = (char_u *)(wp->w_llist_ref
4101 ? _(msg_loclist)
4102 : _(msg_qflist));
4103 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104#endif
4105
4106 case STL_MODIFIED:
4107 case STL_MODIFIED_ALT:
4108 itemisflag = TRUE;
4109 switch ((opt == STL_MODIFIED_ALT)
4110 + bufIsChanged(wp->w_buffer) * 2
4111 + (!wp->w_buffer->b_p_ma) * 4)
4112 {
4113 case 2: str = (char_u *)"[+]"; break;
4114 case 3: str = (char_u *)",+"; break;
4115 case 4: str = (char_u *)"[-]"; break;
4116 case 5: str = (char_u *)",-"; break;
4117 case 6: str = (char_u *)"[+-]"; break;
4118 case 7: str = (char_u *)",+-"; break;
4119 }
4120 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004121
4122 case STL_HIGHLIGHT:
4123 t = s;
4124 while (*s != '#' && *s != NUL)
4125 ++s;
4126 if (*s == '#')
4127 {
4128 item[curitem].type = Highlight;
4129 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004130 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004131 curitem++;
4132 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004133 if (*s != NUL)
4134 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004135 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 }
4137
4138 item[curitem].start = p;
4139 item[curitem].type = Normal;
4140 if (str != NULL && *str)
4141 {
4142 t = str;
4143 if (itemisflag)
4144 {
4145 if ((t[0] && t[1])
4146 && ((!prevchar_isitem && *t == ',')
4147 || (prevchar_isflag && *t == ' ')))
4148 t++;
4149 prevchar_isflag = TRUE;
4150 }
4151 l = vim_strsize(t);
4152 if (l > 0)
4153 prevchar_isitem = TRUE;
4154 if (l > maxwid)
4155 {
4156 while (l >= maxwid)
4157#ifdef FEAT_MBYTE
4158 if (has_mbyte)
4159 {
4160 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004161 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162 }
4163 else
4164#endif
4165 l -= byte2cells(*t++);
4166 if (p + 1 >= out + outlen)
4167 break;
4168 *p++ = '<';
4169 }
4170 if (minwid > 0)
4171 {
4172 for (; l < minwid && p + 1 < out + outlen; l++)
4173 {
4174 /* Don't put a "-" in front of a digit. */
4175 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4176 *p++ = ' ';
4177 else
4178 *p++ = fillchar;
4179 }
4180 minwid = 0;
4181 }
4182 else
4183 minwid *= -1;
4184 while (*t && p + 1 < out + outlen)
4185 {
4186 *p++ = *t++;
4187 /* Change a space by fillchar, unless fillchar is '-' and a
4188 * digit follows. */
4189 if (fillable && p[-1] == ' '
4190 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4191 p[-1] = fillchar;
4192 }
4193 for (; l < minwid && p + 1 < out + outlen; l++)
4194 *p++ = fillchar;
4195 }
4196 else if (num >= 0)
4197 {
4198 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4199 char_u nstr[20];
4200
4201 if (p + 20 >= out + outlen)
4202 break; /* not sufficient space */
4203 prevchar_isitem = TRUE;
4204 t = nstr;
4205 if (opt == STL_VIRTCOL_ALT)
4206 {
4207 *t++ = '-';
4208 minwid--;
4209 }
4210 *t++ = '%';
4211 if (zeropad)
4212 *t++ = '0';
4213 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004214 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 *t = 0;
4216
4217 for (n = num, l = 1; n >= nbase; n /= nbase)
4218 l++;
4219 if (opt == STL_VIRTCOL_ALT)
4220 l++;
4221 if (l > maxwid)
4222 {
4223 l += 2;
4224 n = l - maxwid;
4225 while (l-- > maxwid)
4226 num /= nbase;
4227 *t++ = '>';
4228 *t++ = '%';
4229 *t = t[-3];
4230 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004231 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4232 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004235 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4236 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 p += STRLEN(p);
4238 }
4239 else
4240 item[curitem].type = Empty;
4241
4242 if (opt == STL_VIM_EXPR)
4243 vim_free(str);
4244
4245 if (num >= 0 || (!itemisflag && str && *str))
4246 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4247 curitem++;
4248 }
4249 *p = NUL;
4250 itemcnt = curitem;
4251
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004252#ifdef FEAT_EVAL
4253 if (usefmt != fmt)
4254 vim_free(usefmt);
4255#endif
4256
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 width = vim_strsize(out);
4258 if (maxwidth > 0 && width > maxwidth)
4259 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004260 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 l = 0;
4262 if (itemcnt == 0)
4263 s = out;
4264 else
4265 {
4266 for ( ; l < itemcnt; l++)
4267 if (item[l].type == Trunc)
4268 {
4269 /* Truncate at %< item. */
4270 s = item[l].start;
4271 break;
4272 }
4273 if (l == itemcnt)
4274 {
4275 /* No %< item, truncate first item. */
4276 s = item[0].start;
4277 l = 0;
4278 }
4279 }
4280
4281 if (width - vim_strsize(s) >= maxwidth)
4282 {
4283 /* Truncation mark is beyond max length */
4284#ifdef FEAT_MBYTE
4285 if (has_mbyte)
4286 {
4287 s = out;
4288 width = 0;
4289 for (;;)
4290 {
4291 width += ptr2cells(s);
4292 if (width >= maxwidth)
4293 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004294 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 /* Fill up for half a double-wide character. */
4297 while (++width < maxwidth)
4298 *s++ = fillchar;
4299 }
4300 else
4301#endif
4302 s = out + maxwidth - 1;
4303 for (l = 0; l < itemcnt; l++)
4304 if (item[l].start > s)
4305 break;
4306 itemcnt = l;
4307 *s++ = '>';
4308 *s = 0;
4309 }
4310 else
4311 {
4312#ifdef FEAT_MBYTE
4313 if (has_mbyte)
4314 {
4315 n = 0;
4316 while (width >= maxwidth)
4317 {
4318 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004319 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 }
4321 }
4322 else
4323#endif
4324 n = width - maxwidth + 1;
4325 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004326 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 *s = '<';
4328
4329 /* Fill up for half a double-wide character. */
4330 while (++width < maxwidth)
4331 {
4332 s = s + STRLEN(s);
4333 *s++ = fillchar;
4334 *s = NUL;
4335 }
4336
4337 --n; /* count the '<' */
4338 for (; l < itemcnt; l++)
4339 {
4340 if (item[l].start - n >= s)
4341 item[l].start -= n;
4342 else
4343 item[l].start = s;
4344 }
4345 }
4346 width = maxwidth;
4347 }
4348 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4349 {
4350 /* Apply STL_MIDDLE if any */
4351 for (l = 0; l < itemcnt; l++)
4352 if (item[l].type == Middle)
4353 break;
4354 if (l < itemcnt)
4355 {
4356 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004357 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 for (s = item[l].start; s < p; s++)
4359 *s = fillchar;
4360 for (l++; l < itemcnt; l++)
4361 item[l].start += maxwidth - width;
4362 width = maxwidth;
4363 }
4364 }
4365
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004366 /* Store the info about highlighting. */
4367 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004369 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 for (l = 0; l < itemcnt; l++)
4371 {
4372 if (item[l].type == Highlight)
4373 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004374 sp->start = item[l].start;
4375 sp->userhl = item[l].minwid;
4376 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
4378 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004379 sp->start = NULL;
4380 sp->userhl = 0;
4381 }
4382
4383 /* Store the info about tab pages labels. */
4384 if (tabtab != NULL)
4385 {
4386 sp = tabtab;
4387 for (l = 0; l < itemcnt; l++)
4388 {
4389 if (item[l].type == TabPage)
4390 {
4391 sp->start = item[l].start;
4392 sp->userhl = item[l].minwid;
4393 sp++;
4394 }
4395 }
4396 sp->start = NULL;
4397 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
4399
4400 return width;
4401}
4402#endif /* FEAT_STL_OPT */
4403
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004404#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4405 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004407 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4408 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 */
4410 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004411get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004413 char_u *buf;
4414 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415{
4416 long above; /* number of lines above window */
4417 long below; /* number of lines below window */
4418
Bram Moolenaar0027c212015-01-07 13:31:52 +01004419 if (buflen < 3) /* need at least 3 chars for writing */
4420 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 above = wp->w_topline - 1;
4422#ifdef FEAT_DIFF
4423 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4424#endif
4425 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4426 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004427 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4428 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004430 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004432 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 ? (int)(above / ((above + below) / 100L))
4434 : (int)(above * 100L / (above + below)));
4435}
4436#endif
4437
4438/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004439 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 * Return TRUE if it was appended.
4441 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004442 static int
4443append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 win_T *wp;
4445 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004446 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448{
4449 char_u *p;
4450
4451 if (ARGCOUNT <= 1) /* nothing to do */
4452 return FALSE;
4453
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004454 p = buf + STRLEN(buf); /* go to the end of the buffer */
4455 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 return FALSE;
4457 *p++ = ' ';
4458 *p++ = '(';
4459 if (add_file)
4460 {
4461 STRCPY(p, "file ");
4462 p += 5;
4463 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004464 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4465 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4467 return TRUE;
4468}
4469
4470/*
4471 * If fname is not a full path, make it a full path.
4472 * Returns pointer to allocated memory (NULL for failure).
4473 */
4474 char_u *
4475fix_fname(fname)
4476 char_u *fname;
4477{
4478 /*
4479 * Force expanding the path always for Unix, because symbolic links may
4480 * mess up the full path name, even though it starts with a '/'.
4481 * Also expand when there is ".." in the file name, try to remove it,
4482 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004483 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 * For MS-Windows also expand names like "longna~1" to "longname".
4485 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004486#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 return FullName_save(fname, TRUE);
4488#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004489 if (!vim_isAbsName(fname)
4490 || strstr((char *)fname, "..") != NULL
4491 || strstr((char *)fname, "//") != NULL
4492# ifdef BACKSLASH_IN_FILENAME
4493 || strstr((char *)fname, "\\\\") != NULL
4494# endif
4495# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004497# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 )
4499 return FullName_save(fname, FALSE);
4500
4501 fname = vim_strsave(fname);
4502
Bram Moolenaar9b942202007-10-03 12:31:33 +00004503# ifdef USE_FNAME_CASE
4504# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004506# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
4508 if (fname != NULL)
4509 fname_case(fname, 0); /* set correct case for file name */
4510 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004511# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512
4513 return fname;
4514#endif
4515}
4516
4517/*
4518 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4519 * "ffname" becomes a pointer to allocated memory (or NULL).
4520 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 void
4522fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004523 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 char_u **ffname;
4525 char_u **sfname;
4526{
4527 if (*ffname == NULL) /* if no file name given, nothing to do */
4528 return;
4529 if (*sfname == NULL) /* if no short file name given, use ffname */
4530 *sfname = *ffname;
4531 *ffname = fix_fname(*ffname); /* expand to full path */
4532
4533#ifdef FEAT_SHORTCUT
4534 if (!buf->b_p_bin)
4535 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004536 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 /* If the file name is a shortcut file, use the file it links to. */
4539 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004540 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
4542 vim_free(*ffname);
4543 *ffname = rfname;
4544 *sfname = rfname;
4545 }
4546 }
4547#endif
4548}
4549
4550/*
4551 * Get the file name for an argument list entry.
4552 */
4553 char_u *
4554alist_name(aep)
4555 aentry_T *aep;
4556{
4557 buf_T *bp;
4558
4559 /* Use the name from the associated buffer if it exists. */
4560 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004561 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 return aep->ae_fname;
4563 return bp->b_fname;
4564}
4565
4566#if defined(FEAT_WINDOWS) || defined(PROTO)
4567/*
4568 * do_arg_all(): Open up to 'count' windows, one for each argument.
4569 */
4570 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004571do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 int count;
4573 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004574 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575{
4576 int i;
4577 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004578 char_u *opened; /* Array of weight for which args are open:
4579 * 0: not opened
4580 * 1: opened in other tab
4581 * 2: opened in curtab
4582 * 3: opened in curtab and curwin
4583 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004584 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 int use_firstwin = FALSE; /* use first window for arglist */
4586 int split_ret = OK;
4587 int p_ea_save;
4588 alist_T *alist; /* argument list to be used */
4589 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004590 tabpage_T *tpnext;
4591 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004592 win_T *old_curwin, *last_curwin;
4593 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004594 win_T *new_curwin = NULL;
4595 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596
4597 if (ARGCOUNT <= 0)
4598 {
4599 /* Don't give an error message. We don't want it when the ":all"
4600 * command is in the .vimrc. */
4601 return;
4602 }
4603 setpcmark();
4604
4605 opened_len = ARGCOUNT;
4606 opened = alloc_clear((unsigned)opened_len);
4607 if (opened == NULL)
4608 return;
4609
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004610 /* Autocommands may do anything to the argument list. Make sure it's not
4611 * freed while we are working here by "locking" it. We still have to
4612 * watch out for its size to be changed. */
4613 alist = curwin->w_alist;
4614 ++alist->al_refcount;
4615
4616 old_curwin = curwin;
4617 old_curtab = curtab;
4618
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619#ifdef FEAT_GUI
4620 need_mouse_correct = TRUE;
4621#endif
4622
4623 /*
4624 * Try closing all windows that are not in the argument list.
4625 * Also close windows that are not full width;
4626 * When 'hidden' or "forceit" set the buffer becomes hidden.
4627 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004628 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004630 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004631 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004632 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004634 tpnext = curtab->tp_next;
4635 for (wp = firstwin; wp != NULL; wp = wpnext)
4636 {
4637 wpnext = wp->w_next;
4638 buf = wp->w_buffer;
4639 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004640 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004642 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004644 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004645 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004646 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004648 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004649 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004651 if (i < alist->al_ga.ga_len
4652 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4653 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4654 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004656 int weight = 1;
4657
4658 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004659 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004660 ++weight;
4661 if (old_curwin == wp)
4662 ++weight;
4663 }
4664
4665 if (weight > (int)opened[i])
4666 {
4667 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004668 if (i == 0)
4669 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004670 if (new_curwin != NULL)
4671 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004672 new_curwin = wp;
4673 new_curtab = curtab;
4674 }
4675 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004676 else if (keep_tabs)
4677 i = opened_len;
4678
4679 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004680 {
4681 /* Use the current argument list for all windows
4682 * containing a file from it. */
4683 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004684 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004685 ++wp->w_alist->al_refcount;
4686 }
4687 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004691 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004693 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004695 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004696 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004698 /* If the buffer was changed, and we would like to hide it,
4699 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004700 if (!P_HID(buf) && buf->b_nwindows <= 1
4701 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004703 (void)autowrite(buf, FALSE);
4704#ifdef FEAT_AUTOCMD
4705 /* check if autocommands removed the window */
4706 if (!win_valid(wp) || !buf_valid(buf))
4707 {
4708 wpnext = firstwin; /* start all over... */
4709 continue;
4710 }
4711#endif
4712 }
4713#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004714 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004715 if (firstwin == lastwin
4716 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004717#endif
4718 use_firstwin = TRUE;
4719#ifdef FEAT_WINDOWS
4720 else
4721 {
4722 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4723# ifdef FEAT_AUTOCMD
4724 /* check if autocommands removed the next window */
4725 if (!win_valid(wpnext))
4726 wpnext = firstwin; /* start all over... */
4727# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729#endif
4730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 }
4732 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004733
4734 /* Without the ":tab" modifier only do the current tab page. */
4735 if (had_tab == 0 || tpnext == NULL)
4736 break;
4737
4738# ifdef FEAT_AUTOCMD
4739 /* check if autocommands removed the next tab page */
4740 if (!valid_tabpage(tpnext))
4741 tpnext = first_tabpage; /* start all over...*/
4742# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004743 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745
4746 /*
4747 * Open a window for files in the argument list that don't have one.
4748 * ARGCOUNT may change while doing this, because of autocommands.
4749 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004750 if (count > opened_len || count <= 0)
4751 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
4753#ifdef FEAT_AUTOCMD
4754 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4755 ++autocmd_no_enter;
4756 ++autocmd_no_leave;
4757#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004758 last_curwin = curwin;
4759 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004761#ifdef FEAT_WINDOWS
4762 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4763 * leaving an empty tab page when executed locally. */
4764 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4765 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4766 use_firstwin = TRUE;
4767#endif
4768
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004769 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 {
4771 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4772 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004773 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
4775 /* Move the already present window to below the current window */
4776 if (curwin->w_arg_idx != i)
4777 {
4778 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4779 {
4780 if (wpnext->w_arg_idx == i)
4781 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004782 if (keep_tabs)
4783 {
4784 new_curwin = wpnext;
4785 new_curtab = curtab;
4786 }
4787 else
4788 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 break;
4790 }
4791 }
4792 }
4793 }
4794 else if (split_ret == OK)
4795 {
4796 if (!use_firstwin) /* split current window */
4797 {
4798 p_ea_save = p_ea;
4799 p_ea = TRUE; /* use space from all windows */
4800 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4801 p_ea = p_ea_save;
4802 if (split_ret == FAIL)
4803 continue;
4804 }
4805#ifdef FEAT_AUTOCMD
4806 else /* first window: do autocmd for leaving this buffer */
4807 --autocmd_no_leave;
4808#endif
4809
4810 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004811 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 */
4813 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004814 if (i == 0)
4815 {
4816 new_curwin = curwin;
4817 new_curtab = curtab;
4818 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4820 ECMD_ONE,
4821 ((P_HID(curwin->w_buffer)
4822 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004823 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#ifdef FEAT_AUTOCMD
4825 if (use_firstwin)
4826 ++autocmd_no_leave;
4827#endif
4828 use_firstwin = FALSE;
4829 }
4830 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004831
4832 /* When ":tab" was used open a new tab for a new window repeatedly. */
4833 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4834 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836
4837 /* Remove the "lock" on the argument list. */
4838 alist_unlink(alist);
4839
4840#ifdef FEAT_AUTOCMD
4841 --autocmd_no_enter;
4842#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004843 /* restore last referenced tabpage's curwin */
4844 if (last_curtab != new_curtab)
4845 {
4846 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004847 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004848 if (win_valid(last_curwin))
4849 win_enter(last_curwin, FALSE);
4850 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004851 /* to window with first arg */
4852 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004853 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004854 if (win_valid(new_curwin))
4855 win_enter(new_curwin, FALSE);
4856
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857#ifdef FEAT_AUTOCMD
4858 --autocmd_no_leave;
4859#endif
4860 vim_free(opened);
4861}
4862
4863# if defined(FEAT_LISTCMDS) || defined(PROTO)
4864/*
4865 * Open a window for a number of buffers.
4866 */
4867 void
4868ex_buffer_all(eap)
4869 exarg_T *eap;
4870{
4871 buf_T *buf;
4872 win_T *wp, *wpnext;
4873 int split_ret = OK;
4874 int p_ea_save;
4875 int open_wins = 0;
4876 int r;
4877 int count; /* Maximum number of windows to open. */
4878 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004879#ifdef FEAT_WINDOWS
4880 int had_tab = cmdmod.tab;
4881 tabpage_T *tpnext;
4882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 if (eap->addr_count == 0) /* make as many windows as possible */
4885 count = 9999;
4886 else
4887 count = eap->line2; /* make as many windows as specified */
4888 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4889 all = FALSE;
4890 else
4891 all = TRUE;
4892
4893 setpcmark();
4894
4895#ifdef FEAT_GUI
4896 need_mouse_correct = TRUE;
4897#endif
4898
4899 /*
4900 * Close superfluous windows (two windows for the same buffer).
4901 * Also close windows that are not full-width.
4902 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004903#ifdef FEAT_WINDOWS
4904 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004905 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004906 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004909 tpnext = curtab->tp_next;
4910 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004912 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004913 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004914#ifdef FEAT_VERTSPLIT
4915 || ((cmdmod.split & WSP_VERT)
4916 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004917 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004918 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004920#ifdef FEAT_WINDOWS
4921 || (had_tab > 0 && wp != firstwin)
4922#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004923 ) && firstwin != lastwin
4924#ifdef FEAT_AUTOCMD
4925 && !(wp->w_closing || wp->w_buffer->b_closing)
4926#endif
4927 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004928 {
4929 win_close(wp, FALSE);
4930#ifdef FEAT_AUTOCMD
4931 wpnext = firstwin; /* just in case an autocommand does
4932 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004933 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004934 open_wins = 0;
4935#endif
4936 }
4937 else
4938 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004940
4941#ifdef FEAT_WINDOWS
4942 /* Without the ":tab" modifier only do the current tab page. */
4943 if (had_tab == 0 || tpnext == NULL)
4944 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004945 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004947#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948
4949 /*
4950 * Go through the buffer list. When a buffer doesn't have a window yet,
4951 * open one. Otherwise move the window to the right position.
4952 * Watch out for autocommands that delete buffers or windows!
4953 */
4954#ifdef FEAT_AUTOCMD
4955 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4956 ++autocmd_no_enter;
4957#endif
4958 win_enter(lastwin, FALSE);
4959#ifdef FEAT_AUTOCMD
4960 ++autocmd_no_leave;
4961#endif
4962 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4963 {
4964 /* Check if this buffer needs a window */
4965 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4966 continue;
4967
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004968#ifdef FEAT_WINDOWS
4969 if (had_tab != 0)
4970 {
4971 /* With the ":tab" modifier don't move the window. */
4972 if (buf->b_nwindows > 0)
4973 wp = lastwin; /* buffer has a window, skip it */
4974 else
4975 wp = NULL;
4976 }
4977 else
4978#endif
4979 {
4980 /* Check if this buffer already has a window */
4981 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4982 if (wp->w_buffer == buf)
4983 break;
4984 /* If the buffer already has a window, move it */
4985 if (wp != NULL)
4986 win_move_after(wp, curwin);
4987 }
4988
4989 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 /* Split the window and put the buffer in it */
4992 p_ea_save = p_ea;
4993 p_ea = TRUE; /* use space from all windows */
4994 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4995 ++open_wins;
4996 p_ea = p_ea_save;
4997 if (split_ret == FAIL)
4998 continue;
4999
5000 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00005001#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 swap_exists_action = SEA_DIALOG;
5003#endif
5004 set_curbuf(buf, DOBUF_GOTO);
5005#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00005008#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005010# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 break;
5012 }
5013#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00005014#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 if (swap_exists_action == SEA_QUIT)
5016 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005017# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5018 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005019
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005020 /* Reset the error/interrupt/exception state here so that
5021 * aborting() returns FALSE when closing a window. */
5022 enter_cleanup(&cs);
5023# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005024
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005025 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 win_close(curwin, TRUE);
5027 --open_wins;
5028 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00005029 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00005030
5031# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
5032 /* Restore the error/interrupt/exception state if not
5033 * discarded by a new aborting error, interrupt, or uncaught
5034 * exception. */
5035 leave_cleanup(&cs);
5036# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
5038 else
5039 handle_swap_exists(NULL);
5040#endif
5041 }
5042
5043 ui_breakcheck();
5044 if (got_int)
5045 {
5046 (void)vgetc(); /* only break the file loading, not the rest */
5047 break;
5048 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00005049#ifdef FEAT_EVAL
5050 /* Autocommands deleted the buffer or aborted script processing!!! */
5051 if (aborting())
5052 break;
5053#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00005054#ifdef FEAT_WINDOWS
5055 /* When ":tab" was used open a new tab for a new window repeatedly. */
5056 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
5057 cmdmod.tab = 9999;
5058#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060#ifdef FEAT_AUTOCMD
5061 --autocmd_no_enter;
5062#endif
5063 win_enter(firstwin, FALSE); /* back to first window */
5064#ifdef FEAT_AUTOCMD
5065 --autocmd_no_leave;
5066#endif
5067
5068 /*
5069 * Close superfluous windows.
5070 */
5071 for (wp = lastwin; open_wins > count; )
5072 {
5073 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5074 || autowrite(wp->w_buffer, FALSE) == OK);
5075#ifdef FEAT_AUTOCMD
5076 if (!win_valid(wp))
5077 {
5078 /* BufWrite Autocommands made the window invalid, start over */
5079 wp = lastwin;
5080 }
5081 else
5082#endif
5083 if (r)
5084 {
5085 win_close(wp, !P_HID(wp->w_buffer));
5086 --open_wins;
5087 wp = lastwin;
5088 }
5089 else
5090 {
5091 wp = wp->w_prev;
5092 if (wp == NULL)
5093 break;
5094 }
5095 }
5096}
5097# endif /* FEAT_LISTCMDS */
5098
5099#endif /* FEAT_WINDOWS */
5100
Bram Moolenaara3227e22006-03-08 21:32:40 +00005101static int chk_modeline __ARGS((linenr_T, int));
5102
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103/*
5104 * do_modelines() - process mode lines for the current file
5105 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005106 * "flags" can be:
5107 * OPT_WINONLY only set options local to window
5108 * OPT_NOWIN don't set options local to window
5109 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 * Returns immediately if the "ml" option isn't set.
5111 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005113do_modelines(flags)
5114 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005116 linenr_T lnum;
5117 int nmlines;
5118 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
5120 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5121 return;
5122
5123 /* Disallow recursive entry here. Can happen when executing a modeline
5124 * triggers an autocommand, which reloads modelines with a ":do". */
5125 if (entered)
5126 return;
5127
5128 ++entered;
5129 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5130 ++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
5134 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5135 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005136 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 nmlines = 0;
5138 --entered;
5139}
5140
5141#include "version.h" /* for version number */
5142
5143/*
5144 * chk_modeline() - check a single line for a mode string
5145 * Return FAIL if an error encountered.
5146 */
5147 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005148chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005150 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151{
5152 char_u *s;
5153 char_u *e;
5154 char_u *linecopy; /* local copy of any modeline found */
5155 int prev;
5156 int vers;
5157 int end;
5158 int retval = OK;
5159 char_u *save_sourcing_name;
5160 linenr_T save_sourcing_lnum;
5161#ifdef FEAT_EVAL
5162 scid_T save_SID;
5163#endif
5164
5165 prev = -1;
5166 for (s = ml_get(lnum); *s != NUL; ++s)
5167 {
5168 if (prev == -1 || vim_isspace(prev))
5169 {
5170 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5171 || STRNCMP(s, "vi:", (size_t)3) == 0)
5172 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005173 /* Accept both "vim" and "Vim". */
5174 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
5176 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5177 e = s + 4;
5178 else
5179 e = s + 3;
5180 vers = getdigits(&e);
5181 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005182 && (s[0] != 'V'
5183 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 && (s[3] == ':'
5185 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5186 || (VIM_VERSION_100 < vers && s[3] == '<')
5187 || (VIM_VERSION_100 > vers && s[3] == '>')
5188 || (VIM_VERSION_100 == vers && s[3] == '=')))
5189 break;
5190 }
5191 }
5192 prev = *s;
5193 }
5194
5195 if (*s)
5196 {
5197 do /* skip over "ex:", "vi:" or "vim:" */
5198 ++s;
5199 while (s[-1] != ':');
5200
5201 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5202 if (linecopy == NULL)
5203 return FAIL;
5204
5205 save_sourcing_lnum = sourcing_lnum;
5206 save_sourcing_name = sourcing_name;
5207 sourcing_lnum = lnum; /* prepare for emsg() */
5208 sourcing_name = (char_u *)"modelines";
5209
5210 end = FALSE;
5211 while (end == FALSE)
5212 {
5213 s = skipwhite(s);
5214 if (*s == NUL)
5215 break;
5216
5217 /*
5218 * Find end of set command: ':' or end of line.
5219 * Skip over "\:", replacing it with ":".
5220 */
5221 for (e = s; *e != ':' && *e != NUL; ++e)
5222 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005223 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 if (*e == NUL)
5225 end = TRUE;
5226
5227 /*
5228 * If there is a "set" command, require a terminating ':' and
5229 * ignore the stuff after the ':'.
5230 * "vi:set opt opt opt: foo" -- foo not interpreted
5231 * "vi:opt opt opt: foo" -- foo interpreted
5232 * Accept "se" for compatibility with Elvis.
5233 */
5234 if (STRNCMP(s, "set ", (size_t)4) == 0
5235 || STRNCMP(s, "se ", (size_t)3) == 0)
5236 {
5237 if (*e != ':') /* no terminating ':'? */
5238 break;
5239 end = TRUE;
5240 s = vim_strchr(s, ' ') + 1;
5241 }
5242 *e = NUL; /* truncate the set command */
5243
5244 if (*s != NUL) /* skip over an empty "::" */
5245 {
5246#ifdef FEAT_EVAL
5247 save_SID = current_SID;
5248 current_SID = SID_MODELINE;
5249#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005250 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251#ifdef FEAT_EVAL
5252 current_SID = save_SID;
5253#endif
5254 if (retval == FAIL) /* stop if error found */
5255 break;
5256 }
5257 s = e + 1; /* advance to next part */
5258 }
5259
5260 sourcing_lnum = save_sourcing_lnum;
5261 sourcing_name = save_sourcing_name;
5262
5263 vim_free(linecopy);
5264 }
5265 return retval;
5266}
5267
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005268#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 int
5270read_viminfo_bufferlist(virp, writing)
5271 vir_T *virp;
5272 int writing;
5273{
5274 char_u *tab;
5275 linenr_T lnum;
5276 colnr_T col;
5277 buf_T *buf;
5278 char_u *sfname;
5279 char_u *xline;
5280
5281 /* Handle long line and escaped characters. */
5282 xline = viminfo_readstring(virp, 1, FALSE);
5283
5284 /* don't read in if there are files on the command-line or if writing: */
5285 if (xline != NULL && !writing && ARGCOUNT == 0
5286 && find_viminfo_parameter('%') != NULL)
5287 {
5288 /* Format is: <fname> Tab <lnum> Tab <col>.
5289 * Watch out for a Tab in the file name, work from the end. */
5290 lnum = 0;
5291 col = 0;
5292 tab = vim_strrchr(xline, '\t');
5293 if (tab != NULL)
5294 {
5295 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005296 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 tab = vim_strrchr(xline, '\t');
5298 if (tab != NULL)
5299 {
5300 *tab++ = '\0';
5301 lnum = atol((char *)tab);
5302 }
5303 }
5304
5305 /* Expand "~/" in the file name at "line + 1" to a full path.
5306 * Then try shortening it by comparing with the current directory */
5307 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005308 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309
5310 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5311 if (buf != NULL) /* just in case... */
5312 {
5313 buf->b_last_cursor.lnum = lnum;
5314 buf->b_last_cursor.col = col;
5315 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5316 }
5317 }
5318 vim_free(xline);
5319
5320 return viminfo_readline(virp);
5321}
5322
5323 void
5324write_viminfo_bufferlist(fp)
5325 FILE *fp;
5326{
5327 buf_T *buf;
5328#ifdef FEAT_WINDOWS
5329 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005330 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331#endif
5332 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005333 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334
5335 if (find_viminfo_parameter('%') == NULL)
5336 return;
5337
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005338 /* Without a number -1 is returned: do all buffers. */
5339 max_buffers = get_viminfo_parameter('%');
5340
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005342#define LINE_BUF_LEN (MAXPATHL + 40)
5343 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 if (line == NULL)
5345 return;
5346
5347#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005348 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 set_last_cursor(win);
5350#else
5351 set_last_cursor(curwin);
5352#endif
5353
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005354 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5356 {
5357 if (buf->b_fname == NULL
5358 || !buf->b_p_bl
5359#ifdef FEAT_QUICKFIX
5360 || bt_quickfix(buf)
5361#endif
5362 || removable(buf->b_ffname))
5363 continue;
5364
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005365 if (max_buffers-- == 0)
5366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 putc('%', fp);
5368 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005369 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 (long)buf->b_last_cursor.lnum,
5371 buf->b_last_cursor.col);
5372 viminfo_writestring(fp, line);
5373 }
5374 vim_free(line);
5375}
5376#endif
5377
5378
5379/*
5380 * Return special buffer name.
5381 * Returns NULL when the buffer has a normal file name.
5382 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005383 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384buf_spname(buf)
5385 buf_T *buf;
5386{
5387#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5388 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005389 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005390 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005391 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005392
5393 /*
5394 * For location list window, w_llist_ref points to the location list.
5395 * For quickfix window, w_llist_ref is NULL.
5396 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005397 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005398 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005399 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005400 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005401 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402#endif
5403#ifdef FEAT_QUICKFIX
5404 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5405 * contains the name as specified by the user */
5406 if (bt_nofile(buf))
5407 {
5408 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005409 return buf->b_sfname;
5410 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411 }
5412#endif
5413 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005414 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 return NULL;
5416}
5417
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005418#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5419 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5420 || defined(PROTO)
5421/*
5422 * Find a window for buffer "buf".
5423 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5424 * If not found FAIL is returned.
5425 */
5426 int
5427find_win_for_buf(buf, wp, tp)
5428 buf_T *buf;
5429 win_T **wp;
5430 tabpage_T **tp;
5431{
5432 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5433 if ((*wp)->w_buffer == buf)
5434 goto win_found;
5435 return FAIL;
5436win_found:
5437 return OK;
5438}
5439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441#if defined(FEAT_SIGNS) || defined(PROTO)
5442/*
5443 * Insert the sign into the signlist.
5444 */
5445 static void
5446insert_sign(buf, prev, next, id, lnum, typenr)
5447 buf_T *buf; /* buffer to store sign in */
5448 signlist_T *prev; /* previous sign entry */
5449 signlist_T *next; /* next sign entry */
5450 int id; /* sign ID */
5451 linenr_T lnum; /* line number which gets the mark */
5452 int typenr; /* typenr of sign we are adding */
5453{
5454 signlist_T *newsign;
5455
5456 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5457 if (newsign != NULL)
5458 {
5459 newsign->id = id;
5460 newsign->lnum = lnum;
5461 newsign->typenr = typenr;
5462 newsign->next = next;
5463#ifdef FEAT_NETBEANS_INTG
5464 newsign->prev = prev;
5465 if (next != NULL)
5466 next->prev = newsign;
5467#endif
5468
5469 if (prev == NULL)
5470 {
5471 /* When adding first sign need to redraw the windows to create the
5472 * column for signs. */
5473 if (buf->b_signlist == NULL)
5474 {
5475 redraw_buf_later(buf, NOT_VALID);
5476 changed_cline_bef_curs();
5477 }
5478
5479 /* first sign in signlist */
5480 buf->b_signlist = newsign;
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01005481#ifdef FEAT_NETBEANS_INTG
5482 if (netbeans_active())
5483 buf->b_has_sign_column = TRUE;
5484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 }
5486 else
5487 prev->next = newsign;
5488 }
5489}
5490
5491/*
5492 * Add the sign into the signlist. Find the right spot to do it though.
5493 */
5494 void
5495buf_addsign(buf, id, lnum, typenr)
5496 buf_T *buf; /* buffer to store sign in */
5497 int id; /* sign ID */
5498 linenr_T lnum; /* line number which gets the mark */
5499 int typenr; /* typenr of sign we are adding */
5500{
5501 signlist_T *sign; /* a sign in the signlist */
5502 signlist_T *prev; /* the previous sign */
5503
5504 prev = NULL;
5505 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5506 {
5507 if (lnum == sign->lnum && id == sign->id)
5508 {
5509 sign->typenr = typenr;
5510 return;
5511 }
5512 else if (
5513#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5514 id < 0 &&
5515#endif
5516 lnum < sign->lnum)
5517 {
5518#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5519 /* XXX - GRP: Is this because of sign slide problem? Or is it
5520 * really needed? Or is it because we allow multiple signs per
5521 * line? If so, should I add that feature to FEAT_SIGNS?
5522 */
5523 while (prev != NULL && prev->lnum == lnum)
5524 prev = prev->prev;
5525 if (prev == NULL)
5526 sign = buf->b_signlist;
5527 else
5528 sign = prev->next;
5529#endif
5530 insert_sign(buf, prev, sign, id, lnum, typenr);
5531 return;
5532 }
5533 prev = sign;
5534 }
5535#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5536 /* XXX - GRP: See previous comment */
5537 while (prev != NULL && prev->lnum == lnum)
5538 prev = prev->prev;
5539 if (prev == NULL)
5540 sign = buf->b_signlist;
5541 else
5542 sign = prev->next;
5543#endif
5544 insert_sign(buf, prev, sign, id, lnum, typenr);
5545
5546 return;
5547}
5548
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005549/*
5550 * For an existing, placed sign "markId" change the type to "typenr".
5551 * Returns the line number of the sign, or zero if the sign is not found.
5552 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005553 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554buf_change_sign_type(buf, markId, typenr)
5555 buf_T *buf; /* buffer to store sign in */
5556 int markId; /* sign ID */
5557 int typenr; /* typenr of sign we are adding */
5558{
5559 signlist_T *sign; /* a sign in the signlist */
5560
5561 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5562 {
5563 if (sign->id == markId)
5564 {
5565 sign->typenr = typenr;
5566 return sign->lnum;
5567 }
5568 }
5569
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005570 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571}
5572
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005573 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574buf_getsigntype(buf, lnum, type)
5575 buf_T *buf;
5576 linenr_T lnum;
5577 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5578{
5579 signlist_T *sign; /* a sign in a b_signlist */
5580
5581 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5582 if (sign->lnum == lnum
5583 && (type == SIGN_ANY
5584# ifdef FEAT_SIGN_ICONS
5585 || (type == SIGN_ICON
5586 && sign_get_image(sign->typenr) != NULL)
5587# endif
5588 || (type == SIGN_TEXT
5589 && sign_get_text(sign->typenr) != NULL)
5590 || (type == SIGN_LINEHL
5591 && sign_get_attr(sign->typenr, TRUE) != 0)))
5592 return sign->typenr;
5593 return 0;
5594}
5595
5596
5597 linenr_T
5598buf_delsign(buf, id)
5599 buf_T *buf; /* buffer sign is stored in */
5600 int id; /* sign id */
5601{
5602 signlist_T **lastp; /* pointer to pointer to current sign */
5603 signlist_T *sign; /* a sign in a b_signlist */
5604 signlist_T *next; /* the next sign in a b_signlist */
5605 linenr_T lnum; /* line number whose sign was deleted */
5606
5607 lastp = &buf->b_signlist;
5608 lnum = 0;
5609 for (sign = buf->b_signlist; sign != NULL; sign = next)
5610 {
5611 next = sign->next;
5612 if (sign->id == id)
5613 {
5614 *lastp = next;
5615#ifdef FEAT_NETBEANS_INTG
5616 if (next != NULL)
5617 next->prev = sign->prev;
5618#endif
5619 lnum = sign->lnum;
5620 vim_free(sign);
5621 break;
5622 }
5623 else
5624 lastp = &sign->next;
5625 }
5626
5627 /* When deleted the last sign need to redraw the windows to remove the
5628 * sign column. */
5629 if (buf->b_signlist == NULL)
5630 {
5631 redraw_buf_later(buf, NOT_VALID);
5632 changed_cline_bef_curs();
5633 }
5634
5635 return lnum;
5636}
5637
5638
5639/*
5640 * Find the line number of the sign with the requested id. If the sign does
5641 * not exist, return 0 as the line number. This will still let the correct file
5642 * get loaded.
5643 */
5644 int
5645buf_findsign(buf, id)
5646 buf_T *buf; /* buffer to store sign in */
5647 int id; /* sign ID */
5648{
5649 signlist_T *sign; /* a sign in the signlist */
5650
5651 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5652 if (sign->id == id)
5653 return sign->lnum;
5654
5655 return 0;
5656}
5657
5658 int
5659buf_findsign_id(buf, lnum)
5660 buf_T *buf; /* buffer whose sign we are searching for */
5661 linenr_T lnum; /* line number of sign */
5662{
5663 signlist_T *sign; /* a sign in the signlist */
5664
5665 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5666 if (sign->lnum == lnum)
5667 return sign->id;
5668
5669 return 0;
5670}
5671
5672
5673# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5674/* see if a given type of sign exists on a specific line */
5675 int
5676buf_findsigntype_id(buf, lnum, typenr)
5677 buf_T *buf; /* buffer whose sign we are searching for */
5678 linenr_T lnum; /* line number of sign */
5679 int typenr; /* sign type number */
5680{
5681 signlist_T *sign; /* a sign in the signlist */
5682
5683 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5684 if (sign->lnum == lnum && sign->typenr == typenr)
5685 return sign->id;
5686
5687 return 0;
5688}
5689
5690
5691# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5692/* return the number of icons on the given line */
5693 int
5694buf_signcount(buf, lnum)
5695 buf_T *buf;
5696 linenr_T lnum;
5697{
5698 signlist_T *sign; /* a sign in the signlist */
5699 int count = 0;
5700
5701 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5702 if (sign->lnum == lnum)
5703 if (sign_get_image(sign->typenr) != NULL)
5704 count++;
5705
5706 return count;
5707}
5708# endif /* FEAT_SIGN_ICONS */
5709# endif /* FEAT_NETBEANS_INTG */
5710
5711
5712/*
5713 * Delete signs in buffer "buf".
5714 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005715 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716buf_delete_signs(buf)
5717 buf_T *buf;
5718{
5719 signlist_T *next;
5720
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005721 /* When deleting the last sign need to redraw the windows to remove the
Bram Moolenaar4e036c92014-07-16 16:30:28 +02005722 * sign column. Not when curwin is NULL (this means we're exiting). */
5723 if (buf->b_signlist != NULL && curwin != NULL)
Bram Moolenaar0d3d5e02014-05-07 16:35:08 +02005724 {
5725 redraw_buf_later(buf, NOT_VALID);
5726 changed_cline_bef_curs();
5727 }
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 while (buf->b_signlist != NULL)
5730 {
5731 next = buf->b_signlist->next;
5732 vim_free(buf->b_signlist);
5733 buf->b_signlist = next;
5734 }
5735}
5736
5737/*
5738 * Delete all signs in all buffers.
5739 */
5740 void
5741buf_delete_all_signs()
5742{
5743 buf_T *buf; /* buffer we are checking for signs */
5744
5745 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5746 if (buf->b_signlist != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 buf_delete_signs(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748}
5749
5750/*
5751 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5752 */
5753 void
5754sign_list_placed(rbuf)
5755 buf_T *rbuf;
5756{
5757 buf_T *buf;
5758 signlist_T *p;
5759 char lbuf[BUFSIZ];
5760
5761 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5762 msg_putchar('\n');
5763 if (rbuf == NULL)
5764 buf = firstbuf;
5765 else
5766 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005767 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 {
5769 if (buf->b_signlist != NULL)
5770 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005771 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5773 msg_putchar('\n');
5774 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005775 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005777 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5779 MSG_PUTS(lbuf);
5780 msg_putchar('\n');
5781 }
5782 if (rbuf != NULL)
5783 break;
5784 buf = buf->b_next;
5785 }
5786}
5787
5788/*
5789 * Adjust a placed sign for inserted/deleted lines.
5790 */
5791 void
5792sign_mark_adjust(line1, line2, amount, amount_after)
5793 linenr_T line1;
5794 linenr_T line2;
5795 long amount;
5796 long amount_after;
5797{
5798 signlist_T *sign; /* a sign in a b_signlist */
5799
5800 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5801 {
5802 if (sign->lnum >= line1 && sign->lnum <= line2)
5803 {
5804 if (amount == MAXLNUM)
5805 sign->lnum = line1;
5806 else
5807 sign->lnum += amount;
5808 }
5809 else if (sign->lnum > line2)
5810 sign->lnum += amount_after;
5811 }
5812}
5813#endif /* FEAT_SIGNS */
5814
5815/*
5816 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5817 */
5818 void
5819set_buflisted(on)
5820 int on;
5821{
5822 if (on != curbuf->b_p_bl)
5823 {
5824 curbuf->b_p_bl = on;
5825#ifdef FEAT_AUTOCMD
5826 if (on)
5827 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5828 else
5829 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5830#endif
5831 }
5832}
5833
5834/*
5835 * Read the file for "buf" again and check if the contents changed.
5836 * Return TRUE if it changed or this could not be checked.
5837 */
5838 int
5839buf_contents_changed(buf)
5840 buf_T *buf;
5841{
5842 buf_T *newbuf;
5843 int differ = TRUE;
5844 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 exarg_T ea;
5847
5848 /* Allocate a buffer without putting it in the buffer list. */
5849 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5850 if (newbuf == NULL)
5851 return TRUE;
5852
5853 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5854 if (prep_exarg(&ea, buf) == FAIL)
5855 {
5856 wipe_buffer(newbuf, FALSE);
5857 return TRUE;
5858 }
5859
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 /* set curwin/curbuf to buf and save a few things */
5861 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862
Bram Moolenaar4770d092006-01-12 23:22:24 +00005863 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 && readfile(buf->b_ffname, buf->b_fname,
5865 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5866 &ea, READ_NEW | READ_DUMMY) == OK)
5867 {
5868 /* compare the two files line by line */
5869 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5870 {
5871 differ = FALSE;
5872 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5873 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5874 {
5875 differ = TRUE;
5876 break;
5877 }
5878 }
5879 }
5880 vim_free(ea.cmd);
5881
Bram Moolenaar071d4272004-06-13 20:20:40 +00005882 /* restore curwin/curbuf and a few other things */
5883 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884
5885 if (curbuf != newbuf) /* safety check */
5886 wipe_buffer(newbuf, FALSE);
5887
5888 return differ;
5889}
5890
5891/*
5892 * Wipe out a buffer and decrement the last buffer number if it was used for
5893 * this buffer. Call this to wipe out a temp buffer that does not contain any
5894 * marks.
5895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 void
5897wipe_buffer(buf, aucmd)
5898 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005899 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005900{
5901 if (buf->b_fnum == top_file_num - 1)
5902 --top_file_num;
5903
5904#ifdef FEAT_AUTOCMD
5905 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005906 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005907#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005908 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909#ifdef FEAT_AUTOCMD
5910 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005911 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005912#endif
5913}