blob: 9dc7378f56d952b1bad55744391dbb244dafde4b [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)
31static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
32# define HAVE_BUFLIST_MATCH
33static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
34#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
374 if (win != NULL)
375 {
376 /* Set b_last_cursor when closing the last window for the buffer.
377 * Remember the last cursor position and window options of the buffer.
378 * This used to be only for the current window, but then options like
379 * 'foldmethod' may be lost with a ":only" command. */
380 if (buf->b_nwindows == 1)
381 set_last_cursor(win);
382 buflist_setfpos(buf, win,
383 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
384 win->w_cursor.col, TRUE);
385 }
386
387#ifdef FEAT_AUTOCMD
388 /* When the buffer is no longer in a window, trigger BufWinLeave */
389 if (buf->b_nwindows == 1)
390 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200391 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000392 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
393 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200394 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100395 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200396 /* Autocommands deleted the buffer. */
397aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100398 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100400 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200401 buf->b_closing = FALSE;
402 if (abort_if_last && one_window())
403 /* Autocommands made this the only window. */
404 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000405
406 /* When the buffer becomes hidden, but is not unloaded, trigger
407 * BufHidden */
408 if (!unload_buf)
409 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200410 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
412 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200413 if (!buf_valid(buf))
414 /* Autocommands deleted the buffer. */
415 goto aucmd_abort;
416 buf->b_closing = FALSE;
417 if (abort_if_last && one_window())
418 /* Autocommands made this the only window. */
419 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 }
421# ifdef FEAT_EVAL
422 if (aborting()) /* autocmds may abort script processing */
423 return;
424# endif
425 }
426 nwindows = buf->b_nwindows;
427#endif
428
429 /* decrease the link count from windows (unless not in any window) */
430 if (buf->b_nwindows > 0)
431 --buf->b_nwindows;
432
433 /* Return when a window is displaying the buffer or when it's not
434 * unloaded. */
435 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000437
438 /* Always remove the buffer when there is no file name. */
439 if (buf->b_ffname == NULL)
440 del_buf = TRUE;
441
442 /*
443 * Free all things allocated for this buffer.
444 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
445 */
446#ifdef FEAT_AUTOCMD
447 /* Remember if we are closing the current buffer. Restore the number of
448 * windows, so that autocommands in buf_freeall() don't get confused. */
449 is_curbuf = (buf == curbuf);
450 buf->b_nwindows = nwindows;
451#endif
452
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200453 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200454 if (
455#ifdef FEAT_WINDOWS
456 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100457#else
458 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200459#endif
460 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200461 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
463#ifdef FEAT_AUTOCMD
464 /* Autocommands may have deleted the buffer. */
465 if (!buf_valid(buf))
466 return;
467# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000468 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469 return;
470# endif
471
472 /* Autocommands may have opened or closed windows for this buffer.
473 * Decrement the count for the close we do here. */
474 if (buf->b_nwindows > 0)
475 --buf->b_nwindows;
476
477 /*
478 * It's possible that autocommands change curbuf to the one being deleted.
479 * This might cause the previous curbuf to be deleted unexpectedly. But
480 * in some cases it's OK to delete the curbuf, because a new one is
481 * obtained anyway. Therefore only return if curbuf changed to the
482 * deleted buffer.
483 */
484 if (buf == curbuf && !is_curbuf)
485 return;
486#endif
487
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000488 /* Change directories when the 'acd' option is set. */
489 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490
491 /*
492 * Remove the buffer from the list.
493 */
494 if (wipe_buf)
495 {
496#ifdef FEAT_SUN_WORKSHOP
497 if (usingSunWorkShop)
498 workshop_file_closed_lineno((char *)buf->b_ffname,
499 (int)buf->b_last_cursor.lnum);
500#endif
501 vim_free(buf->b_ffname);
502 vim_free(buf->b_sfname);
503 if (buf->b_prev == NULL)
504 firstbuf = buf->b_next;
505 else
506 buf->b_prev->b_next = buf->b_next;
507 if (buf->b_next == NULL)
508 lastbuf = buf->b_prev;
509 else
510 buf->b_next->b_prev = buf->b_prev;
511 free_buffer(buf);
512 }
513 else
514 {
515 if (del_buf)
516 {
517 /* Free all internal variables and reset option values, to make
518 * ":bdel" compatible with Vim 5.7. */
519 free_buffer_stuff(buf, TRUE);
520
521 /* Make it look like a new buffer. */
522 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
523
524 /* Init the options when loaded again. */
525 buf->b_p_initialized = FALSE;
526 }
527 buf_clear_file(buf);
528 if (del_buf)
529 buf->b_p_bl = FALSE;
530 }
531}
532
533/*
534 * Make buffer not contain a file.
535 */
536 void
537buf_clear_file(buf)
538 buf_T *buf;
539{
540 buf->b_ml.ml_line_count = 1;
541 unchanged(buf, TRUE);
542#ifndef SHORT_FNAME
543 buf->b_shortname = FALSE;
544#endif
545 buf->b_p_eol = TRUE;
546 buf->b_start_eol = TRUE;
547#ifdef FEAT_MBYTE
548 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000549 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550#endif
551 buf->b_ml.ml_mfp = NULL;
552 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
553#ifdef FEAT_NETBEANS_INTG
554 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555#endif
556}
557
558/*
559 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200560 * the file. flags:
561 * BFA_DEL buffer is going to be deleted
562 * BFA_WIPE buffer is going to be wiped out
563 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200566buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200568 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000569{
570#ifdef FEAT_AUTOCMD
571 int is_curbuf = (buf == curbuf);
572
Bram Moolenaar362ce482012-06-06 19:02:45 +0200573 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
575 if (!buf_valid(buf)) /* autocommands may delete the buffer */
576 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200577 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 {
579 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
580 if (!buf_valid(buf)) /* autocommands may delete the buffer */
581 return;
582 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200583 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 {
585 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
586 FALSE, buf);
587 if (!buf_valid(buf)) /* autocommands may delete the buffer */
588 return;
589 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200590 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000592 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 return;
594# endif
595
596 /*
597 * It's possible that autocommands change curbuf to the one being deleted.
598 * This might cause curbuf to be deleted unexpectedly. But in some cases
599 * it's OK to delete the curbuf, because a new one is obtained anyway.
600 * Therefore only return if curbuf changed to the deleted buffer.
601 */
602 if (buf == curbuf && !is_curbuf)
603 return;
604#endif
605#ifdef FEAT_DIFF
606 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
607#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200608#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100609 /* Remove any ownsyntax, unless exiting. */
610 if (firstwin != NULL && curwin->w_buffer == buf)
611 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200612#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000613
614#ifdef FEAT_FOLDING
615 /* No folds in an empty buffer. */
616# ifdef FEAT_WINDOWS
617 {
618 win_T *win;
619 tabpage_T *tp;
620
621 FOR_ALL_TAB_WINDOWS(tp, win)
622 if (win->w_buffer == buf)
623 clearFolding(win);
624 }
625# else
626 if (curwin->w_buffer == buf)
627 clearFolding(curwin);
628# endif
629#endif
630
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_TCL
632 tcl_buffer_free(buf);
633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 ml_close(buf, TRUE); /* close and delete the memline/memfile */
635 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200636 if ((flags & BFA_KEEP_UNDO) == 0)
637 {
638 u_blockfree(buf); /* free the memory allocated for undo */
639 u_clearall(buf); /* reset all undo information */
640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200642 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000644 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645}
646
647/*
648 * Free a buffer structure and the things it contains related to the buffer
649 * itself (not the file, that must have been done already).
650 */
651 static void
652free_buffer(buf)
653 buf_T *buf;
654{
655 free_buffer_stuff(buf, TRUE);
Bram Moolenaar429fa852013-04-15 12:27:36 +0200656#ifdef FEAT_EVAL
657 unref_var_dict(buf->b_vars);
658#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200659#ifdef FEAT_LUA
660 lua_buffer_free(buf);
661#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000662#ifdef FEAT_MZSCHEME
663 mzscheme_buffer_free(buf);
664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665#ifdef FEAT_PERL
666 perl_buf_free(buf);
667#endif
668#ifdef FEAT_PYTHON
669 python_buffer_free(buf);
670#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200671#ifdef FEAT_PYTHON3
672 python3_buffer_free(buf);
673#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674#ifdef FEAT_RUBY
675 ruby_buffer_free(buf);
676#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000677#ifdef FEAT_AUTOCMD
678 aubuflocal_remove(buf);
679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 vim_free(buf);
681}
682
683/*
684 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
685 */
686 static void
687free_buffer_stuff(buf, free_options)
688 buf_T *buf;
689 int free_options; /* free options as well */
690{
691 if (free_options)
692 {
693 clear_wininfo(buf); /* including window-local options */
694 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200695#ifdef FEAT_SPELL
696 ga_clear(&buf->b_s.b_langp);
697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 }
699#ifdef FEAT_EVAL
Bram Moolenaar429fa852013-04-15 12:27:36 +0200700 vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
701 hash_init(&buf->b_vars->dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702#endif
703#ifdef FEAT_USR_CMDS
704 uc_clear(&buf->b_ucmds); /* clear local user commands */
705#endif
706#ifdef FEAT_SIGNS
707 buf_delete_signs(buf); /* delete any signs */
708#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000709#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200710 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712#ifdef FEAT_LOCALMAP
713 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
714 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
715#endif
716#ifdef FEAT_MBYTE
717 vim_free(buf->b_start_fenc);
718 buf->b_start_fenc = NULL;
719#endif
720}
721
722/*
723 * Free the b_wininfo list for buffer "buf".
724 */
725 static void
726clear_wininfo(buf)
727 buf_T *buf;
728{
729 wininfo_T *wip;
730
731 while (buf->b_wininfo != NULL)
732 {
733 wip = buf->b_wininfo;
734 buf->b_wininfo = wip->wi_next;
735 if (wip->wi_optset)
736 {
737 clear_winopt(&wip->wi_opt);
738#ifdef FEAT_FOLDING
739 deleteFoldRecurse(&wip->wi_folds);
740#endif
741 }
742 vim_free(wip);
743 }
744}
745
746#if defined(FEAT_LISTCMDS) || defined(PROTO)
747/*
748 * Go to another buffer. Handles the result of the ATTENTION dialog.
749 */
750 void
751goto_buffer(eap, start, dir, count)
752 exarg_T *eap;
753 int start;
754 int dir;
755 int count;
756{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000757# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 buf_T *old_curbuf = curbuf;
759
760 swap_exists_action = SEA_DIALOG;
761# endif
762 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
763 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000764# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
766 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000767# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
768 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000769
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000770 /* Reset the error/interrupt/exception state here so that
771 * aborting() returns FALSE when closing a window. */
772 enter_cleanup(&cs);
773# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000774
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000775 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000776 win_close(curwin, TRUE);
777 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000778 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000779
780# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
781 /* Restore the error/interrupt/exception state if not discarded by a
782 * new aborting error, interrupt, or uncaught exception. */
783 leave_cleanup(&cs);
784# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786 else
787 handle_swap_exists(old_curbuf);
788# endif
789}
790#endif
791
Bram Moolenaare64ac772005-12-07 20:54:59 +0000792#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793/*
794 * Handle the situation of swap_exists_action being set.
795 * It is allowed for "old_curbuf" to be NULL or invalid.
796 */
797 void
798handle_swap_exists(old_curbuf)
799 buf_T *old_curbuf;
800{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000801# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
802 cleanup_T cs;
803# endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100804#ifdef FEAT_SYN_HL
805 long old_tw = curbuf->b_p_tw;
806#endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000807
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 if (swap_exists_action == SEA_QUIT)
809 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000810# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
811 /* Reset the error/interrupt/exception state here so that
812 * aborting() returns FALSE when closing a buffer. */
813 enter_cleanup(&cs);
814# endif
815
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 /* User selected Quit at ATTENTION prompt. Go back to previous
817 * buffer. If that buffer is gone or the same as the current one,
818 * open a new, empty buffer. */
819 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000820 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100821 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000823 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 if (old_curbuf != NULL)
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100825 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 enter_buffer(old_curbuf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +0100827#ifdef FEAT_SYN_HL
828 if (old_tw != curbuf->b_p_tw)
829 check_colorcolumn(curwin);
830#endif
831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000833
834# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
835 /* Restore the error/interrupt/exception state if not discarded by a
836 * new aborting error, interrupt, or uncaught exception. */
837 leave_cleanup(&cs);
838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 }
840 else if (swap_exists_action == SEA_RECOVER)
841 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000842# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
843 /* Reset the error/interrupt/exception state here so that
844 * aborting() returns FALSE when closing a buffer. */
845 enter_cleanup(&cs);
846# endif
847
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 /* User selected Recover at ATTENTION prompt. */
849 msg_scroll = TRUE;
850 ml_recover();
851 MSG_PUTS("\n"); /* don't overwrite the last message */
852 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000853 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000854
855# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
856 /* Restore the error/interrupt/exception state if not discarded by a
857 * new aborting error, interrupt, or uncaught exception. */
858 leave_cleanup(&cs);
859# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 }
861 swap_exists_action = SEA_NONE;
862}
863#endif
864
865#if defined(FEAT_LISTCMDS) || defined(PROTO)
866/*
867 * do_bufdel() - delete or unload buffer(s)
868 *
869 * addr_count == 0: ":bdel" - delete current buffer
870 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
871 * buffer "end_bnr", then any other arguments.
872 * addr_count == 2: ":N,N bdel" - delete buffers in range
873 *
874 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
875 * DOBUF_DEL (":bdel")
876 *
877 * Returns error message or NULL
878 */
879 char_u *
880do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
881 int command;
882 char_u *arg; /* pointer to extra arguments */
883 int addr_count;
884 int start_bnr; /* first buffer number in a range */
885 int end_bnr; /* buffer nr or last buffer nr in a range */
886 int forceit;
887{
888 int do_current = 0; /* delete current buffer? */
889 int deleted = 0; /* number of buffers deleted */
890 char_u *errormsg = NULL; /* return value */
891 int bnr; /* buffer number */
892 char_u *p;
893
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (addr_count == 0)
895 {
896 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
897 }
898 else
899 {
900 if (addr_count == 2)
901 {
902 if (*arg) /* both range and argument is not allowed */
903 return (char_u *)_(e_trailing);
904 bnr = start_bnr;
905 }
906 else /* addr_count == 1 */
907 bnr = end_bnr;
908
909 for ( ;!got_int; ui_breakcheck())
910 {
911 /*
912 * delete the current buffer last, otherwise when the
913 * current buffer is deleted, the next buffer becomes
914 * the current one and will be loaded, which may then
915 * also be deleted, etc.
916 */
917 if (bnr == curbuf->b_fnum)
918 do_current = bnr;
919 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
920 forceit) == OK)
921 ++deleted;
922
923 /*
924 * find next buffer number to delete/unload
925 */
926 if (addr_count == 2)
927 {
928 if (++bnr > end_bnr)
929 break;
930 }
931 else /* addr_count == 1 */
932 {
933 arg = skipwhite(arg);
934 if (*arg == NUL)
935 break;
936 if (!VIM_ISDIGIT(*arg))
937 {
938 p = skiptowhite_esc(arg);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +0100939 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
940 FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 if (bnr < 0) /* failed */
942 break;
943 arg = p;
944 }
945 else
946 bnr = getdigits(&arg);
947 }
948 }
949 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
950 FORWARD, do_current, forceit) == OK)
951 ++deleted;
952
953 if (deleted == 0)
954 {
955 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000956 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000958 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000960 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 errormsg = IObuff;
962 }
963 else if (deleted >= p_report)
964 {
965 if (command == DOBUF_UNLOAD)
966 {
967 if (deleted == 1)
968 MSG(_("1 buffer unloaded"));
969 else
970 smsg((char_u *)_("%d buffers unloaded"), deleted);
971 }
972 else if (command == DOBUF_DEL)
973 {
974 if (deleted == 1)
975 MSG(_("1 buffer deleted"));
976 else
977 smsg((char_u *)_("%d buffers deleted"), deleted);
978 }
979 else
980 {
981 if (deleted == 1)
982 MSG(_("1 buffer wiped out"));
983 else
984 smsg((char_u *)_("%d buffers wiped out"), deleted);
985 }
986 }
987 }
988
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989
990 return errormsg;
991}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200992#endif /* FEAT_LISTCMDS */
993
994#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
995 || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996
997/*
998 * Implementation of the commands for the buffer list.
999 *
1000 * action == DOBUF_GOTO go to specified buffer
1001 * action == DOBUF_SPLIT split window and go to specified buffer
1002 * action == DOBUF_UNLOAD unload specified buffer(s)
1003 * action == DOBUF_DEL delete specified buffer(s) from buffer list
1004 * action == DOBUF_WIPE delete specified buffer(s) really
1005 *
1006 * start == DOBUF_CURRENT go to "count" buffer from current buffer
1007 * start == DOBUF_FIRST go to "count" buffer from first buffer
1008 * start == DOBUF_LAST go to "count" buffer from last buffer
1009 * start == DOBUF_MOD go to "count" modified buffer from current buffer
1010 *
1011 * Return FAIL or OK.
1012 */
1013 int
1014do_buffer(action, start, dir, count, forceit)
1015 int action;
1016 int start;
1017 int dir; /* FORWARD or BACKWARD */
1018 int count; /* buffer number or number of buffers */
1019 int forceit; /* TRUE for :...! */
1020{
1021 buf_T *buf;
1022 buf_T *bp;
1023 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1024 || action == DOBUF_WIPE);
1025
1026 switch (start)
1027 {
1028 case DOBUF_FIRST: buf = firstbuf; break;
1029 case DOBUF_LAST: buf = lastbuf; break;
1030 default: buf = curbuf; break;
1031 }
1032 if (start == DOBUF_MOD) /* find next modified buffer */
1033 {
1034 while (count-- > 0)
1035 {
1036 do
1037 {
1038 buf = buf->b_next;
1039 if (buf == NULL)
1040 buf = firstbuf;
1041 }
1042 while (buf != curbuf && !bufIsChanged(buf));
1043 }
1044 if (!bufIsChanged(buf))
1045 {
1046 EMSG(_("E84: No modified buffer found"));
1047 return FAIL;
1048 }
1049 }
1050 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1051 {
1052 while (buf != NULL && buf->b_fnum != count)
1053 buf = buf->b_next;
1054 }
1055 else
1056 {
1057 bp = NULL;
1058 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1059 {
1060 /* remember the buffer where we start, we come back there when all
1061 * buffers are unlisted. */
1062 if (bp == NULL)
1063 bp = buf;
1064 if (dir == FORWARD)
1065 {
1066 buf = buf->b_next;
1067 if (buf == NULL)
1068 buf = firstbuf;
1069 }
1070 else
1071 {
1072 buf = buf->b_prev;
1073 if (buf == NULL)
1074 buf = lastbuf;
1075 }
1076 /* don't count unlisted buffers */
1077 if (unload || buf->b_p_bl)
1078 {
1079 --count;
1080 bp = NULL; /* use this buffer as new starting point */
1081 }
1082 if (bp == buf)
1083 {
1084 /* back where we started, didn't find anything. */
1085 EMSG(_("E85: There is no listed buffer"));
1086 return FAIL;
1087 }
1088 }
1089 }
1090
1091 if (buf == NULL) /* could not find it */
1092 {
1093 if (start == DOBUF_FIRST)
1094 {
1095 /* don't warn when deleting */
1096 if (!unload)
1097 EMSGN(_("E86: Buffer %ld does not exist"), count);
1098 }
1099 else if (dir == FORWARD)
1100 EMSG(_("E87: Cannot go beyond last buffer"));
1101 else
1102 EMSG(_("E88: Cannot go before first buffer"));
1103 return FAIL;
1104 }
1105
1106#ifdef FEAT_GUI
1107 need_mouse_correct = TRUE;
1108#endif
1109
1110#ifdef FEAT_LISTCMDS
1111 /*
1112 * delete buffer buf from memory and/or the list
1113 */
1114 if (unload)
1115 {
1116 int forward;
1117 int retval;
1118
1119 /* When unloading or deleting a buffer that's already unloaded and
1120 * unlisted: fail silently. */
1121 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1122 return FAIL;
1123
1124 if (!forceit && bufIsChanged(buf))
1125 {
1126#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1127 if ((p_confirm || cmdmod.confirm) && p_write)
1128 {
1129 dialog_changed(buf, FALSE);
1130# ifdef FEAT_AUTOCMD
1131 if (!buf_valid(buf))
1132 /* Autocommand deleted buffer, oops! It's not changed
1133 * now. */
1134 return FAIL;
1135# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001136 /* If it's still changed fail silently, the dialog already
1137 * mentioned why it fails. */
1138 if (bufIsChanged(buf))
1139 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001141 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#endif
1143 {
1144 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1145 buf->b_fnum);
1146 return FAIL;
1147 }
1148 }
1149
1150 /*
1151 * If deleting the last (listed) buffer, make it empty.
1152 * The last (listed) buffer cannot be unloaded.
1153 */
1154 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1155 if (bp->b_p_bl && bp != buf)
1156 break;
1157 if (bp == NULL && buf == curbuf)
1158 {
1159 if (action == DOBUF_UNLOAD)
1160 {
1161 EMSG(_("E90: Cannot unload last buffer"));
1162 return FAIL;
1163 }
1164
1165 /* Close any other windows on this buffer, then make it empty. */
1166#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001167 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168#endif
1169 setpcmark();
1170 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001171 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
1173 /*
1174 * do_ecmd() may create a new buffer, then we have to delete
1175 * the old one. But do_ecmd() may have done that already, check
1176 * if the buffer still exists.
1177 */
1178 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001179 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180 return retval;
1181 }
1182
1183#ifdef FEAT_WINDOWS
1184 /*
1185 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001186 * (unless it's the only window). Repeat this so long as we end up in
1187 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001189 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001190# ifdef FEAT_AUTOCMD
1191 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1192# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001193 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaarc93df6b2013-08-14 17:11:20 +02001194 {
1195 if (win_close(curwin, FALSE) == FAIL)
1196 break;
1197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198#endif
1199
1200 /*
1201 * If the buffer to be deleted is not the current one, delete it here.
1202 */
1203 if (buf != curbuf)
1204 {
1205#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001206 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#endif
1208 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001209 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 return OK;
1211 }
1212
1213 /*
1214 * Deleting the current buffer: Need to find another buffer to go to.
1215 * There must be another, otherwise it would have been handled above.
1216 * First use au_new_curbuf, if it is valid.
1217 * Then prefer the buffer we most recently visited.
1218 * Else try to find one that is loaded, after the current buffer,
1219 * then before the current buffer.
1220 * Finally use any buffer.
1221 */
1222 buf = NULL; /* selected buffer */
1223 bp = NULL; /* used when no loaded buffer found */
1224#ifdef FEAT_AUTOCMD
1225 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1226 buf = au_new_curbuf;
1227# ifdef FEAT_JUMPLIST
1228 else
1229# endif
1230#endif
1231#ifdef FEAT_JUMPLIST
1232 if (curwin->w_jumplistlen > 0)
1233 {
1234 int jumpidx;
1235
1236 jumpidx = curwin->w_jumplistidx - 1;
1237 if (jumpidx < 0)
1238 jumpidx = curwin->w_jumplistlen - 1;
1239
1240 forward = jumpidx;
1241 while (jumpidx != curwin->w_jumplistidx)
1242 {
1243 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1244 if (buf != NULL)
1245 {
1246 if (buf == curbuf || !buf->b_p_bl)
1247 buf = NULL; /* skip current and unlisted bufs */
1248 else if (buf->b_ml.ml_mfp == NULL)
1249 {
1250 /* skip unloaded buf, but may keep it for later */
1251 if (bp == NULL)
1252 bp = buf;
1253 buf = NULL;
1254 }
1255 }
1256 if (buf != NULL) /* found a valid buffer: stop searching */
1257 break;
1258 /* advance to older entry in jump list */
1259 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1260 break;
1261 if (--jumpidx < 0)
1262 jumpidx = curwin->w_jumplistlen - 1;
1263 if (jumpidx == forward) /* List exhausted for sure */
1264 break;
1265 }
1266 }
1267#endif
1268
1269 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1270 {
1271 forward = TRUE;
1272 buf = curbuf->b_next;
1273 for (;;)
1274 {
1275 if (buf == NULL)
1276 {
1277 if (!forward) /* tried both directions */
1278 break;
1279 buf = curbuf->b_prev;
1280 forward = FALSE;
1281 continue;
1282 }
1283 /* in non-help buffer, try to skip help buffers, and vv */
1284 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1285 {
1286 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1287 break;
1288 if (bp == NULL) /* remember unloaded buf for later */
1289 bp = buf;
1290 }
1291 if (forward)
1292 buf = buf->b_next;
1293 else
1294 buf = buf->b_prev;
1295 }
1296 }
1297 if (buf == NULL) /* No loaded buffer, use unloaded one */
1298 buf = bp;
1299 if (buf == NULL) /* No loaded buffer, find listed one */
1300 {
1301 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1302 if (buf->b_p_bl && buf != curbuf)
1303 break;
1304 }
1305 if (buf == NULL) /* Still no buffer, just take one */
1306 {
1307 if (curbuf->b_next != NULL)
1308 buf = curbuf->b_next;
1309 else
1310 buf = curbuf->b_prev;
1311 }
1312 }
1313
1314 /*
1315 * make buf current buffer
1316 */
1317 if (action == DOBUF_SPLIT) /* split window first */
1318 {
1319# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001320 /* If 'switchbuf' contains "useopen": jump to first window containing
1321 * "buf" if one exists */
1322 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001323 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001324 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001325 * page containing "buf" if one exists */
1326 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 return OK;
1328 if (win_split(0, 0) == FAIL)
1329# endif
1330 return FAIL;
1331 }
1332#endif
1333
1334 /* go to current buffer - nothing to do */
1335 if (buf == curbuf)
1336 return OK;
1337
1338 /*
1339 * Check if the current buffer may be abandoned.
1340 */
1341 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1342 {
1343#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1344 if ((p_confirm || cmdmod.confirm) && p_write)
1345 {
1346 dialog_changed(curbuf, FALSE);
1347# ifdef FEAT_AUTOCMD
1348 if (!buf_valid(buf))
1349 /* Autocommand deleted buffer, oops! */
1350 return FAIL;
1351# endif
1352 }
1353 if (bufIsChanged(curbuf))
1354#endif
1355 {
1356 EMSG(_(e_nowrtmsg));
1357 return FAIL;
1358 }
1359 }
1360
1361 /* Go to the other buffer. */
1362 set_curbuf(buf, action);
1363
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001364#if defined(FEAT_LISTCMDS) \
1365 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001367 {
1368 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370#endif
1371
1372#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1373 if (aborting()) /* autocmds may abort script processing */
1374 return FAIL;
1375#endif
1376
1377 return OK;
1378}
Bram Moolenaar8c0e3222013-06-16 17:32:40 +02001379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380
1381/*
1382 * Set current buffer to "buf". Executes autocommands and closes current
1383 * buffer. "action" tells how to close the current buffer:
1384 * DOBUF_GOTO free or hide it
1385 * DOBUF_SPLIT nothing
1386 * DOBUF_UNLOAD unload it
1387 * DOBUF_DEL delete it
1388 * DOBUF_WIPE wipe it out
1389 */
1390 void
1391set_curbuf(buf, action)
1392 buf_T *buf;
1393 int action;
1394{
1395 buf_T *prevbuf;
1396 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1397 || action == DOBUF_WIPE);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001398#ifdef FEAT_SYN_HL
1399 long old_tw = curbuf->b_p_tw;
1400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401
1402 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001403 if (!cmdmod.keepalt)
1404 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001405 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406
1407#ifdef FEAT_VISUAL
1408 /* Don't restart Select mode after switching to another buffer. */
1409 VIsual_reselect = FALSE;
1410#endif
1411
1412 /* close_windows() or apply_autocmds() may change curbuf */
1413 prevbuf = curbuf;
1414
1415#ifdef FEAT_AUTOCMD
1416 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1417# ifdef FEAT_EVAL
1418 if (buf_valid(prevbuf) && !aborting())
1419# else
1420 if (buf_valid(prevbuf))
1421# endif
1422#endif
1423 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001424#ifdef FEAT_SYN_HL
1425 if (prevbuf == curwin->w_buffer)
1426 reset_synblock(curwin);
1427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428#ifdef FEAT_WINDOWS
1429 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001430 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431#endif
1432#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1433 if (buf_valid(prevbuf) && !aborting())
1434#else
1435 if (buf_valid(prevbuf))
1436#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001437 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001438#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001439 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001440#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001441 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001442 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1444 unload ? action : (action == DOBUF_GOTO
1445 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001446 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001447#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001448 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001449 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001450 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001451#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001455 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001456 * it did ":bunload") or aborted the script processing!
1457 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1458 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001459# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001460 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001461# endif
1462# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001463 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001464# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001465 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001467 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 enter_buffer(buf);
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001469#ifdef FEAT_SYN_HL
1470 if (old_tw != curbuf->b_p_tw)
1471 check_colorcolumn(curwin);
1472#endif
1473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474}
1475
1476/*
1477 * Enter a new current buffer.
Bram Moolenaar6d47df72013-02-17 15:45:37 +01001478 * Old curbuf must have been abandoned already! This also means "curbuf" may
1479 * be pointing to freed memory.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 */
1481 void
1482enter_buffer(buf)
1483 buf_T *buf;
1484{
1485 /* Copy buffer and window local option values. Not for a help buffer. */
1486 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1487 if (!buf->b_help)
1488 get_winopts(buf);
1489#ifdef FEAT_FOLDING
1490 else
1491 /* Remove all folds in the window. */
1492 clearFolding(curwin);
1493 foldUpdateAll(curwin); /* update folds (later). */
1494#endif
1495
1496 /* Get the buffer in the current window. */
1497 curwin->w_buffer = buf;
1498 curbuf = buf;
1499 ++curbuf->b_nwindows;
1500
1501#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001502 if (curwin->w_p_diff)
1503 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504#endif
1505
Bram Moolenaara971b822011-09-14 14:43:25 +02001506#ifdef FEAT_SYN_HL
1507 curwin->w_s = &(buf->b_s);
1508#endif
1509
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 /* Cursor on first line by default. */
1511 curwin->w_cursor.lnum = 1;
1512 curwin->w_cursor.col = 0;
1513#ifdef FEAT_VIRTUALEDIT
1514 curwin->w_cursor.coladd = 0;
1515#endif
1516 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001517#ifdef FEAT_AUTOCMD
1518 curwin->w_topline_was_set = FALSE;
1519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001521 /* mark cursor position as being invalid */
1522 curwin->w_valid = 0;
1523
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 /* Make sure the buffer is loaded. */
1525 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001526 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001527#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001528 /* If there is no filetype, allow for detecting one. Esp. useful for
1529 * ":ball" used in a autocommand. If there already is a filetype we
1530 * might prefer to keep it. */
1531 if (*curbuf->b_p_ft == NUL)
1532 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001533#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001534
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001535 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001536 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 else
1538 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001539 if (!msg_silent)
1540 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1542#ifdef FEAT_AUTOCMD
1543 curwin->w_topline = 1;
1544# ifdef FEAT_DIFF
1545 curwin->w_topfill = 0;
1546# endif
1547 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1548 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1549#endif
1550 }
1551
1552 /* If autocommands did not change the cursor position, restore cursor lnum
1553 * and possibly cursor col. */
1554 if (curwin->w_cursor.lnum == 1 && inindent(0))
1555 buflist_getfpos();
1556
1557 check_arg_idx(curwin); /* check for valid arg_idx */
1558#ifdef FEAT_TITLE
1559 maketitle();
1560#endif
1561#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001562 /* when autocmds didn't change it */
1563 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564#endif
1565 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1566
Bram Moolenaar009b2592004-10-24 19:18:58 +00001567#ifdef FEAT_NETBEANS_INTG
1568 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001569 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001570#endif
1571
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001572 /* Change directories when the 'acd' option is set. */
1573 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574
1575#ifdef FEAT_KEYMAP
1576 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001577 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001579#ifdef FEAT_SPELL
1580 /* May need to set the spell language. Can only do this after the buffer
1581 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001582 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1583 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001584#endif
1585
Bram Moolenaar071d4272004-06-13 20:20:40 +00001586 redraw_later(NOT_VALID);
1587}
1588
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001589#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1590/*
1591 * Change to the directory of the current buffer.
1592 */
1593 void
1594do_autochdir()
1595{
1596 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1597 shorten_fnames(TRUE);
1598}
1599#endif
1600
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601/*
1602 * functions for dealing with the buffer list
1603 */
1604
1605/*
1606 * Add a file name to the buffer list. Return a pointer to the buffer.
1607 * If the same file name already exists return a pointer to that buffer.
1608 * If it does not exist, or if fname == NULL, a new entry is created.
1609 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1610 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1611 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612 * This is the ONLY way to create a new buffer.
1613 */
1614static int top_file_num = 1; /* highest file number */
1615
1616 buf_T *
1617buflist_new(ffname, sfname, lnum, flags)
1618 char_u *ffname; /* full path of fname or relative */
1619 char_u *sfname; /* short fname or NULL */
1620 linenr_T lnum; /* preferred cursor line */
1621 int flags; /* BLN_ defines */
1622{
1623 buf_T *buf;
1624#ifdef UNIX
1625 struct stat st;
1626#endif
1627
1628 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1629
1630 /*
1631 * If file name already exists in the list, update the entry.
1632 */
1633#ifdef UNIX
1634 /* On Unix we can use inode numbers when the file exists. Works better
1635 * for hard links. */
1636 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1637 st.st_dev = (dev_T)-1;
1638#endif
1639 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1640#ifdef UNIX
1641 buflist_findname_stat(ffname, &st)
1642#else
1643 buflist_findname(ffname)
1644#endif
1645 ) != NULL)
1646 {
1647 vim_free(ffname);
1648 if (lnum != 0)
1649 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1650 /* copy the options now, if 'cpo' doesn't have 's' and not done
1651 * already */
1652 buf_copy_options(buf, 0);
1653 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1654 {
1655 buf->b_p_bl = TRUE;
1656#ifdef FEAT_AUTOCMD
1657 if (!(flags & BLN_DUMMY))
1658 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1659#endif
1660 }
1661 return buf;
1662 }
1663
1664 /*
1665 * If the current buffer has no name and no contents, use the current
1666 * buffer. Otherwise: Need to allocate a new buffer structure.
1667 *
1668 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001669 * (A spell file buffer is allocated in spell.c, but that's not a normal
1670 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 */
1672 buf = NULL;
1673 if ((flags & BLN_CURBUF)
1674 && curbuf != NULL
1675 && curbuf->b_ffname == NULL
1676 && curbuf->b_nwindows <= 1
1677 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1678 {
1679 buf = curbuf;
1680#ifdef FEAT_AUTOCMD
1681 /* It's like this buffer is deleted. Watch out for autocommands that
1682 * change curbuf! If that happens, allocate a new buffer anyway. */
1683 if (curbuf->b_p_bl)
1684 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1685 if (buf == curbuf)
1686 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1687# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001688 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689 return NULL;
1690# endif
1691#endif
1692#ifdef FEAT_QUICKFIX
1693# ifdef FEAT_AUTOCMD
1694 if (buf == curbuf)
1695# endif
1696 {
1697 /* Make sure 'bufhidden' and 'buftype' are empty */
1698 clear_string_option(&buf->b_p_bh);
1699 clear_string_option(&buf->b_p_bt);
1700 }
1701#endif
1702 }
1703 if (buf != curbuf || curbuf == NULL)
1704 {
1705 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1706 if (buf == NULL)
1707 {
1708 vim_free(ffname);
1709 return NULL;
1710 }
Bram Moolenaar429fa852013-04-15 12:27:36 +02001711#ifdef FEAT_EVAL
1712 /* init b: variables */
1713 buf->b_vars = dict_alloc();
1714 if (buf->b_vars == NULL)
1715 {
1716 vim_free(ffname);
1717 vim_free(buf);
1718 return NULL;
1719 }
1720 init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
1721#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 }
1723
1724 if (ffname != NULL)
1725 {
1726 buf->b_ffname = ffname;
1727 buf->b_sfname = vim_strsave(sfname);
1728 }
1729
1730 clear_wininfo(buf);
1731 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1732
1733 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1734 || buf->b_wininfo == NULL)
1735 {
1736 vim_free(buf->b_ffname);
1737 buf->b_ffname = NULL;
1738 vim_free(buf->b_sfname);
1739 buf->b_sfname = NULL;
1740 if (buf != curbuf)
1741 free_buffer(buf);
1742 return NULL;
1743 }
1744
1745 if (buf == curbuf)
1746 {
1747 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001748 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 if (buf != curbuf) /* autocommands deleted the buffer! */
1750 return NULL;
1751#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001752 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 return NULL;
1754#endif
1755 /* buf->b_nwindows = 0; why was this here? */
1756 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001757
1758 /* Init the options. */
1759 buf->b_p_initialized = FALSE;
1760 buf_copy_options(buf, BCO_ENTER);
1761
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762#ifdef FEAT_KEYMAP
1763 /* need to reload lmaps and set b:keymap_name */
1764 curbuf->b_kmap_state |= KEYMAP_INIT;
1765#endif
1766 }
1767 else
1768 {
1769 /*
1770 * put new buffer at the end of the buffer list
1771 */
1772 buf->b_next = NULL;
1773 if (firstbuf == NULL) /* buffer list is empty */
1774 {
1775 buf->b_prev = NULL;
1776 firstbuf = buf;
1777 }
1778 else /* append new buffer at end of list */
1779 {
1780 lastbuf->b_next = buf;
1781 buf->b_prev = lastbuf;
1782 }
1783 lastbuf = buf;
1784
1785 buf->b_fnum = top_file_num++;
1786 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1787 {
1788 EMSG(_("W14: Warning: List of file names overflow"));
1789 if (emsg_silent == 0)
1790 {
1791 out_flush();
1792 ui_delay(3000L, TRUE); /* make sure it is noticed */
1793 }
1794 top_file_num = 1;
1795 }
1796
1797 /*
1798 * Always copy the options from the current buffer.
1799 */
1800 buf_copy_options(buf, BCO_ALWAYS);
1801 }
1802
1803 buf->b_wininfo->wi_fpos.lnum = lnum;
1804 buf->b_wininfo->wi_win = curwin;
1805
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001806#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001807 hash_init(&buf->b_s.b_keywtab);
1808 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#endif
1810
1811 buf->b_fname = buf->b_sfname;
1812#ifdef UNIX
1813 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001814 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 else
1816 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001817 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 buf->b_dev = st.st_dev;
1819 buf->b_ino = st.st_ino;
1820 }
1821#endif
1822 buf->b_u_synced = TRUE;
1823 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001824 if (flags & BLN_DUMMY)
1825 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826 buf_clear_file(buf);
1827 clrallmarks(buf); /* clear marks */
1828 fmarks_check_names(buf); /* check file marks for this file */
1829 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1830#ifdef FEAT_AUTOCMD
1831 if (!(flags & BLN_DUMMY))
1832 {
1833 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1834 if (flags & BLN_LISTED)
1835 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1836# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001837 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 return NULL;
1839# endif
1840 }
1841#endif
1842
1843 return buf;
1844}
1845
1846/*
1847 * Free the memory for the options of a buffer.
1848 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1849 * 'fileencoding'.
1850 */
1851 void
1852free_buf_options(buf, free_p_ff)
1853 buf_T *buf;
1854 int free_p_ff;
1855{
1856 if (free_p_ff)
1857 {
1858#ifdef FEAT_MBYTE
1859 clear_string_option(&buf->b_p_fenc);
1860#endif
1861 clear_string_option(&buf->b_p_ff);
1862#ifdef FEAT_QUICKFIX
1863 clear_string_option(&buf->b_p_bh);
1864 clear_string_option(&buf->b_p_bt);
1865#endif
1866 }
1867#ifdef FEAT_FIND_ID
1868 clear_string_option(&buf->b_p_def);
1869 clear_string_option(&buf->b_p_inc);
1870# ifdef FEAT_EVAL
1871 clear_string_option(&buf->b_p_inex);
1872# endif
1873#endif
1874#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1875 clear_string_option(&buf->b_p_inde);
1876 clear_string_option(&buf->b_p_indk);
1877#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001878#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1879 clear_string_option(&buf->b_p_bexpr);
1880#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001881#if defined(FEAT_CRYPT)
1882 clear_string_option(&buf->b_p_cm);
1883#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001884#if defined(FEAT_EVAL)
1885 clear_string_option(&buf->b_p_fex);
1886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887#ifdef FEAT_CRYPT
1888 clear_string_option(&buf->b_p_key);
1889#endif
1890 clear_string_option(&buf->b_p_kp);
1891 clear_string_option(&buf->b_p_mps);
1892 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001893 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 clear_string_option(&buf->b_p_isk);
1895#ifdef FEAT_KEYMAP
1896 clear_string_option(&buf->b_p_keymap);
1897 ga_clear(&buf->b_kmap_ga);
1898#endif
1899#ifdef FEAT_COMMENTS
1900 clear_string_option(&buf->b_p_com);
1901#endif
1902#ifdef FEAT_FOLDING
1903 clear_string_option(&buf->b_p_cms);
1904#endif
1905 clear_string_option(&buf->b_p_nf);
1906#ifdef FEAT_SYN_HL
1907 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001908#endif
1909#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001910 clear_string_option(&buf->b_s.b_p_spc);
1911 clear_string_option(&buf->b_s.b_p_spf);
Bram Moolenaar473de612013-06-08 18:19:48 +02001912 vim_regfree(buf->b_s.b_cap_prog);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001913 buf->b_s.b_cap_prog = NULL;
1914 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915#endif
1916#ifdef FEAT_SEARCHPATH
1917 clear_string_option(&buf->b_p_sua);
1918#endif
1919#ifdef FEAT_AUTOCMD
1920 clear_string_option(&buf->b_p_ft);
1921#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922#ifdef FEAT_CINDENT
1923 clear_string_option(&buf->b_p_cink);
1924 clear_string_option(&buf->b_p_cino);
1925#endif
1926#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1927 clear_string_option(&buf->b_p_cinw);
1928#endif
1929#ifdef FEAT_INS_EXPAND
1930 clear_string_option(&buf->b_p_cpt);
1931#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001932#ifdef FEAT_COMPL_FUNC
1933 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001934 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936#ifdef FEAT_QUICKFIX
1937 clear_string_option(&buf->b_p_gp);
1938 clear_string_option(&buf->b_p_mp);
1939 clear_string_option(&buf->b_p_efm);
1940#endif
1941 clear_string_option(&buf->b_p_ep);
1942 clear_string_option(&buf->b_p_path);
1943 clear_string_option(&buf->b_p_tags);
1944#ifdef FEAT_INS_EXPAND
1945 clear_string_option(&buf->b_p_dict);
1946 clear_string_option(&buf->b_p_tsr);
1947#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001948#ifdef FEAT_TEXTOBJ
1949 clear_string_option(&buf->b_p_qe);
1950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 buf->b_p_ar = -1;
Bram Moolenaarf5a2fd82013-11-06 05:26:15 +01001952 buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953}
1954
1955/*
1956 * get alternate file n
1957 * set linenr to lnum or altfpos.lnum if lnum == 0
1958 * also set cursor column to altfpos.col if 'startofline' is not set.
1959 * if (options & GETF_SETMARK) call setpcmark()
1960 * if (options & GETF_ALT) we are jumping to an alternate file.
1961 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1962 *
1963 * return FAIL for failure, OK for success
1964 */
1965 int
1966buflist_getfile(n, lnum, options, forceit)
1967 int n;
1968 linenr_T lnum;
1969 int options;
1970 int forceit;
1971{
1972 buf_T *buf;
1973#ifdef FEAT_WINDOWS
1974 win_T *wp = NULL;
1975#endif
1976 pos_T *fpos;
1977 colnr_T col;
1978
1979 buf = buflist_findnr(n);
1980 if (buf == NULL)
1981 {
1982 if ((options & GETF_ALT) && n == 0)
1983 EMSG(_(e_noalt));
1984 else
1985 EMSGN(_("E92: Buffer %ld not found"), n);
1986 return FAIL;
1987 }
1988
1989 /* if alternate file is the current buffer, nothing to do */
1990 if (buf == curbuf)
1991 return OK;
1992
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001993 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001994 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001995 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001997 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001998#ifdef FEAT_AUTOCMD
1999 if (curbuf_locked())
2000 return FAIL;
2001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002
2003 /* altfpos may be changed by getfile(), get it now */
2004 if (lnum == 0)
2005 {
2006 fpos = buflist_findfpos(buf);
2007 lnum = fpos->lnum;
2008 col = fpos->col;
2009 }
2010 else
2011 col = 0;
2012
2013#ifdef FEAT_WINDOWS
2014 if (options & GETF_SWITCH)
2015 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002016 /* If 'switchbuf' contains "useopen": jump to first window containing
2017 * "buf" if one exists */
2018 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 wp = buf_jump_open_win(buf);
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002020 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002021 * page containing "buf" if one exists */
2022 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002023 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00002024 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
2025 * isn't empty: open new window */
2026 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002028 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
2029 tabpage_new();
2030 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002032 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002033 }
2034 }
2035#endif
2036
2037 ++RedrawingDisabled;
2038 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2039 lnum, forceit) <= 0)
2040 {
2041 --RedrawingDisabled;
2042
2043 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2044 if (!p_sol && col != 0)
2045 {
2046 curwin->w_cursor.col = col;
2047 check_cursor_col();
2048#ifdef FEAT_VIRTUALEDIT
2049 curwin->w_cursor.coladd = 0;
2050#endif
2051 curwin->w_set_curswant = TRUE;
2052 }
2053 return OK;
2054 }
2055 --RedrawingDisabled;
2056 return FAIL;
2057}
2058
2059/*
2060 * go to the last know line number for the current buffer
2061 */
2062 void
2063buflist_getfpos()
2064{
2065 pos_T *fpos;
2066
2067 fpos = buflist_findfpos(curbuf);
2068
2069 curwin->w_cursor.lnum = fpos->lnum;
2070 check_cursor_lnum();
2071
2072 if (p_sol)
2073 curwin->w_cursor.col = 0;
2074 else
2075 {
2076 curwin->w_cursor.col = fpos->col;
2077 check_cursor_col();
2078#ifdef FEAT_VIRTUALEDIT
2079 curwin->w_cursor.coladd = 0;
2080#endif
2081 curwin->w_set_curswant = TRUE;
2082 }
2083}
2084
Bram Moolenaar81695252004-12-29 20:58:21 +00002085#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2086/*
2087 * Find file in buffer list by name (it has to be for the current window).
2088 * Returns NULL if not found.
2089 */
2090 buf_T *
2091buflist_findname_exp(fname)
2092 char_u *fname;
2093{
2094 char_u *ffname;
2095 buf_T *buf = NULL;
2096
2097 /* First make the name into a full path name */
2098 ffname = FullName_save(fname,
2099#ifdef UNIX
2100 TRUE /* force expansion, get rid of symbolic links */
2101#else
2102 FALSE
2103#endif
2104 );
2105 if (ffname != NULL)
2106 {
2107 buf = buflist_findname(ffname);
2108 vim_free(ffname);
2109 }
2110 return buf;
2111}
2112#endif
2113
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114/*
2115 * Find file in buffer list by name (it has to be for the current window).
2116 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002117 * Skips dummy buffers.
2118 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 */
2120 buf_T *
2121buflist_findname(ffname)
2122 char_u *ffname;
2123{
2124#ifdef UNIX
2125 struct stat st;
2126
2127 if (mch_stat((char *)ffname, &st) < 0)
2128 st.st_dev = (dev_T)-1;
2129 return buflist_findname_stat(ffname, &st);
2130}
2131
2132/*
2133 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2134 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002135 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 */
2137 static buf_T *
2138buflist_findname_stat(ffname, stp)
2139 char_u *ffname;
2140 struct stat *stp;
2141{
2142#endif
2143 buf_T *buf;
2144
2145 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002146 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147#ifdef UNIX
2148 , stp
2149#endif
2150 ))
2151 return buf;
2152 return NULL;
2153}
2154
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002155#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2156 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157/*
2158 * Find file in buffer list by a regexp pattern.
2159 * Return fnum of the found buffer.
2160 * Return < 0 for error.
2161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002163buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 char_u *pattern;
2165 char_u *pattern_end; /* pointer to first char after pattern */
2166 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002167 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002168 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170 buf_T *buf;
2171 regprog_T *prog;
2172 int match = -1;
2173 int find_listed;
2174 char_u *pat;
2175 char_u *patend;
2176 int attempt;
2177 char_u *p;
2178 int toggledollar;
2179
2180 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2181 {
2182 if (*pattern == '%')
2183 match = curbuf->b_fnum;
2184 else
2185 match = curwin->w_alt_fnum;
2186#ifdef FEAT_DIFF
2187 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2188 match = -1;
2189#endif
2190 }
2191
2192 /*
2193 * Try four ways of matching a listed buffer:
2194 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002195 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 * attempt == 2: with '$' at end (only match at end)
2197 * attempt == 3: with '^' at start and '$' at end (only full match)
2198 * Repeat this for finding an unlisted buffer if there was no matching
2199 * listed buffer.
2200 */
2201 else
2202 {
2203 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2204 if (pat == NULL)
2205 return -1;
2206 patend = pat + STRLEN(pat) - 1;
2207 toggledollar = (patend > pat && *patend == '$');
2208
2209 /* First try finding a listed buffer. If not found and "unlisted"
2210 * is TRUE, try finding an unlisted buffer. */
2211 find_listed = TRUE;
2212 for (;;)
2213 {
2214 for (attempt = 0; attempt <= 3; ++attempt)
2215 {
2216 /* may add '^' and '$' */
2217 if (toggledollar)
2218 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2219 p = pat;
2220 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2221 ++p;
2222 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2223 if (prog == NULL)
2224 {
2225 vim_free(pat);
2226 return -1;
2227 }
2228
2229 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2230 if (buf->b_p_bl == find_listed
2231#ifdef FEAT_DIFF
2232 && (!diffmode || diff_mode_buf(buf))
2233#endif
2234 && buflist_match(prog, buf) != NULL)
2235 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002236 if (curtab_only)
2237 {
2238 /* Ignore the match if the buffer is not open in
2239 * the current tab. */
2240#ifdef FEAT_WINDOWS
2241 win_T *wp;
2242
2243 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2244 if (wp->w_buffer == buf)
2245 break;
2246 if (wp == NULL)
2247 continue;
2248#else
2249 if (curwin->w_buffer != buf)
2250 continue;
2251#endif
2252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 if (match >= 0) /* already found a match */
2254 {
2255 match = -2;
2256 break;
2257 }
2258 match = buf->b_fnum; /* remember first match */
2259 }
2260
Bram Moolenaar473de612013-06-08 18:19:48 +02002261 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 if (match >= 0) /* found one match */
2263 break;
2264 }
2265
2266 /* Only search for unlisted buffers if there was no match with
2267 * a listed buffer. */
2268 if (!unlisted || !find_listed || match != -1)
2269 break;
2270 find_listed = FALSE;
2271 }
2272
2273 vim_free(pat);
2274 }
2275
2276 if (match == -2)
2277 EMSG2(_("E93: More than one match for %s"), pattern);
2278 else if (match < 0)
2279 EMSG2(_("E94: No matching buffer for %s"), pattern);
2280 return match;
2281}
2282#endif
2283
2284#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2285
2286/*
2287 * Find all buffer names that match.
2288 * For command line expansion of ":buf" and ":sbuf".
2289 * Return OK if matches found, FAIL otherwise.
2290 */
2291 int
2292ExpandBufnames(pat, num_file, file, options)
2293 char_u *pat;
2294 int *num_file;
2295 char_u ***file;
2296 int options;
2297{
2298 int count = 0;
2299 buf_T *buf;
2300 int round;
2301 char_u *p;
2302 int attempt;
2303 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002304 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305
2306 *num_file = 0; /* return values in case of FAIL */
2307 *file = NULL;
2308
Bram Moolenaar05159a02005-02-26 23:04:13 +00002309 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2310 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002312 patc = alloc((unsigned)STRLEN(pat) + 11);
2313 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002315 STRCPY(patc, "\\(^\\|[\\/]\\)");
2316 STRCPY(patc + 11, pat + 1);
2317 }
2318 else
2319 patc = pat;
2320
2321 /*
2322 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002323 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002324 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002325 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002326 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002327 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002328 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002329 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002330 if (prog == NULL)
2331 {
2332 if (patc != pat)
2333 vim_free(patc);
2334 return FAIL;
2335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 /*
2338 * round == 1: Count the matches.
2339 * round == 2: Build the array to keep the matches.
2340 */
2341 for (round = 1; round <= 2; ++round)
2342 {
2343 count = 0;
2344 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2345 {
2346 if (!buf->b_p_bl) /* skip unlisted buffers */
2347 continue;
2348 p = buflist_match(prog, buf);
2349 if (p != NULL)
2350 {
2351 if (round == 1)
2352 ++count;
2353 else
2354 {
2355 if (options & WILD_HOME_REPLACE)
2356 p = home_replace_save(buf, p);
2357 else
2358 p = vim_strsave(p);
2359 (*file)[count++] = p;
2360 }
2361 }
2362 }
2363 if (count == 0) /* no match found, break here */
2364 break;
2365 if (round == 1)
2366 {
2367 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2368 if (*file == NULL)
2369 {
Bram Moolenaar473de612013-06-08 18:19:48 +02002370 vim_regfree(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002371 if (patc != pat)
2372 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 return FAIL;
2374 }
2375 }
2376 }
Bram Moolenaar473de612013-06-08 18:19:48 +02002377 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 if (count) /* match(es) found, break here */
2379 break;
2380 }
2381
Bram Moolenaar05159a02005-02-26 23:04:13 +00002382 if (patc != pat)
2383 vim_free(patc);
2384
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 *num_file = count;
2386 return (count == 0 ? FAIL : OK);
2387}
2388
2389#endif /* FEAT_CMDL_COMPL */
2390
2391#ifdef HAVE_BUFLIST_MATCH
2392/*
2393 * Check for a match on the file name for buffer "buf" with regprog "prog".
2394 */
2395 static char_u *
2396buflist_match(prog, buf)
2397 regprog_T *prog;
2398 buf_T *buf;
2399{
2400 char_u *match;
2401
2402 /* First try the short file name, then the long file name. */
2403 match = fname_match(prog, buf->b_sfname);
2404 if (match == NULL)
2405 match = fname_match(prog, buf->b_ffname);
2406
2407 return match;
2408}
2409
2410/*
2411 * Try matching the regexp in "prog" with file name "name".
2412 * Return "name" when there is a match, NULL when not.
2413 */
2414 static char_u *
2415fname_match(prog, name)
2416 regprog_T *prog;
2417 char_u *name;
2418{
2419 char_u *match = NULL;
2420 char_u *p;
2421 regmatch_T regmatch;
2422
2423 if (name != NULL)
2424 {
2425 regmatch.regprog = prog;
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002426 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (vim_regexec(&regmatch, name, (colnr_T)0))
2428 match = name;
2429 else
2430 {
2431 /* Replace $(HOME) with '~' and try matching again. */
2432 p = home_replace_save(NULL, name);
2433 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2434 match = name;
2435 vim_free(p);
2436 }
2437 }
2438
2439 return match;
2440}
2441#endif
2442
2443/*
2444 * find file in buffer list by number
2445 */
2446 buf_T *
2447buflist_findnr(nr)
2448 int nr;
2449{
2450 buf_T *buf;
2451
2452 if (nr == 0)
2453 nr = curwin->w_alt_fnum;
2454 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2455 if (buf->b_fnum == nr)
2456 return (buf);
2457 return NULL;
2458}
2459
2460/*
2461 * Get name of file 'n' in the buffer list.
2462 * When the file has no name an empty string is returned.
2463 * home_replace() is used to shorten the file name (used for marks).
2464 * Returns a pointer to allocated memory, of NULL when failed.
2465 */
2466 char_u *
2467buflist_nr2name(n, fullname, helptail)
2468 int n;
2469 int fullname;
2470 int helptail; /* for help buffers return tail only */
2471{
2472 buf_T *buf;
2473
2474 buf = buflist_findnr(n);
2475 if (buf == NULL)
2476 return NULL;
2477 return home_replace_save(helptail ? buf : NULL,
2478 fullname ? buf->b_ffname : buf->b_fname);
2479}
2480
2481/*
2482 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2483 * When "copy_options" is TRUE save the local window option values.
2484 * When "lnum" is 0 only do the options.
2485 */
2486 static void
2487buflist_setfpos(buf, win, lnum, col, copy_options)
2488 buf_T *buf;
2489 win_T *win;
2490 linenr_T lnum;
2491 colnr_T col;
2492 int copy_options;
2493{
2494 wininfo_T *wip;
2495
2496 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2497 if (wip->wi_win == win)
2498 break;
2499 if (wip == NULL)
2500 {
2501 /* allocate a new entry */
2502 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2503 if (wip == NULL)
2504 return;
2505 wip->wi_win = win;
2506 if (lnum == 0) /* set lnum even when it's 0 */
2507 lnum = 1;
2508 }
2509 else
2510 {
2511 /* remove the entry from the list */
2512 if (wip->wi_prev)
2513 wip->wi_prev->wi_next = wip->wi_next;
2514 else
2515 buf->b_wininfo = wip->wi_next;
2516 if (wip->wi_next)
2517 wip->wi_next->wi_prev = wip->wi_prev;
2518 if (copy_options && wip->wi_optset)
2519 {
2520 clear_winopt(&wip->wi_opt);
2521#ifdef FEAT_FOLDING
2522 deleteFoldRecurse(&wip->wi_folds);
2523#endif
2524 }
2525 }
2526 if (lnum != 0)
2527 {
2528 wip->wi_fpos.lnum = lnum;
2529 wip->wi_fpos.col = col;
2530 }
2531 if (copy_options)
2532 {
2533 /* Save the window-specific option values. */
2534 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2535#ifdef FEAT_FOLDING
2536 wip->wi_fold_manual = win->w_fold_manual;
2537 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2538#endif
2539 wip->wi_optset = TRUE;
2540 }
2541
2542 /* insert the entry in front of the list */
2543 wip->wi_next = buf->b_wininfo;
2544 buf->b_wininfo = wip;
2545 wip->wi_prev = NULL;
2546 if (wip->wi_next)
2547 wip->wi_next->wi_prev = wip;
2548
2549 return;
2550}
2551
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002552#ifdef FEAT_DIFF
2553static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2554
2555/*
2556 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2557 * page. That's because a diff is local to a tab page.
2558 */
2559 static int
2560wininfo_other_tab_diff(wip)
2561 wininfo_T *wip;
2562{
2563 win_T *wp;
2564
2565 if (wip->wi_opt.wo_diff)
2566 {
2567 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2568 /* return FALSE when it's a window in the current tab page, thus
2569 * the buffer was in diff mode here */
2570 if (wip->wi_win == wp)
2571 return FALSE;
2572 return TRUE;
2573 }
2574 return FALSE;
2575}
2576#endif
2577
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578/*
2579 * Find info for the current window in buffer "buf".
2580 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002581 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2582 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 * Returns NULL when there isn't any info.
2584 */
2585 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002586find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002588 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
2590 wininfo_T *wip;
2591
2592 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002593 if (wip->wi_win == curwin
2594#ifdef FEAT_DIFF
2595 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2596#endif
2597 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002599
2600 /* If no wininfo for curwin, use the first in the list (that doesn't have
2601 * 'diff' set and is in another tab page). */
2602 if (wip == NULL)
2603 {
2604#ifdef FEAT_DIFF
2605 if (skip_diff_buffer)
2606 {
2607 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2608 if (!wininfo_other_tab_diff(wip))
2609 break;
2610 }
2611 else
2612#endif
2613 wip = buf->b_wininfo;
2614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 return wip;
2616}
2617
2618/*
2619 * Reset the local window options to the values last used in this window.
2620 * If the buffer wasn't used in this window before, use the values from
2621 * the most recently used window. If the values were never set, use the
2622 * global values for the window.
2623 */
2624 void
2625get_winopts(buf)
2626 buf_T *buf;
2627{
2628 wininfo_T *wip;
2629
2630 clear_winopt(&curwin->w_onebuf_opt);
2631#ifdef FEAT_FOLDING
2632 clearFolding(curwin);
2633#endif
2634
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002635 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 if (wip != NULL && wip->wi_optset)
2637 {
2638 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2639#ifdef FEAT_FOLDING
2640 curwin->w_fold_manual = wip->wi_fold_manual;
2641 curwin->w_foldinvalid = TRUE;
2642 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2643#endif
2644 }
2645 else
2646 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2647
2648#ifdef FEAT_FOLDING
2649 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2650 if (p_fdls >= 0)
2651 curwin->w_p_fdl = p_fdls;
2652#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002653#ifdef FEAT_SYN_HL
2654 check_colorcolumn(curwin);
2655#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656}
2657
2658/*
2659 * Find the position (lnum and col) for the buffer 'buf' for the current
2660 * window.
2661 * Returns a pointer to no_position if no position is found.
2662 */
2663 pos_T *
2664buflist_findfpos(buf)
2665 buf_T *buf;
2666{
2667 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002668 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002670 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 if (wip != NULL)
2672 return &(wip->wi_fpos);
2673 else
2674 return &no_position;
2675}
2676
2677/*
2678 * Find the lnum for the buffer 'buf' for the current window.
2679 */
2680 linenr_T
2681buflist_findlnum(buf)
2682 buf_T *buf;
2683{
2684 return buflist_findfpos(buf)->lnum;
2685}
2686
2687#if defined(FEAT_LISTCMDS) || defined(PROTO)
2688/*
2689 * List all know file names (for :files and :buffers command).
2690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 void
2692buflist_list(eap)
2693 exarg_T *eap;
2694{
2695 buf_T *buf;
2696 int len;
2697 int i;
2698
2699 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2700 {
2701 /* skip unlisted buffers, unless ! was used */
2702 if (!buf->b_p_bl && !eap->forceit)
2703 continue;
2704 msg_putchar('\n');
2705 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002706 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707 else
2708 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2709
Bram Moolenaar50cde822005-06-05 21:54:54 +00002710 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 buf->b_fnum,
2712 buf->b_p_bl ? ' ' : 'u',
2713 buf == curbuf ? '%' :
2714 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2715 buf->b_ml.ml_mfp == NULL ? ' ' :
2716 (buf->b_nwindows == 0 ? 'h' : 'a'),
2717 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2718 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002719 : (bufIsChanged(buf) ? '+' : ' '),
2720 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721
2722 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723 i = 40 - vim_strsize(IObuff);
2724 do
2725 {
2726 IObuff[len++] = ' ';
2727 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002728 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2729 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002730 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 msg_outtrans(IObuff);
2732 out_flush(); /* output one line at a time */
2733 ui_breakcheck();
2734 }
2735}
2736#endif
2737
2738/*
2739 * Get file name and line number for file 'fnum'.
2740 * Used by DoOneCmd() for translating '%' and '#'.
2741 * Used by insert_reg() and cmdline_paste() for '#' register.
2742 * Return FAIL if not found, OK for success.
2743 */
2744 int
2745buflist_name_nr(fnum, fname, lnum)
2746 int fnum;
2747 char_u **fname;
2748 linenr_T *lnum;
2749{
2750 buf_T *buf;
2751
2752 buf = buflist_findnr(fnum);
2753 if (buf == NULL || buf->b_fname == NULL)
2754 return FAIL;
2755
2756 *fname = buf->b_fname;
2757 *lnum = buflist_findlnum(buf);
2758
2759 return OK;
2760}
2761
2762/*
2763 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2764 * The file name with the full path is also remembered, for when :cd is used.
2765 * Returns FAIL for failure (file name already in use by other buffer)
2766 * OK otherwise.
2767 */
2768 int
2769setfname(buf, ffname, sfname, message)
2770 buf_T *buf;
2771 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002772 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773{
Bram Moolenaar81695252004-12-29 20:58:21 +00002774 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775#ifdef UNIX
2776 struct stat st;
2777#endif
2778
2779 if (ffname == NULL || *ffname == NUL)
2780 {
2781 /* Removing the name. */
2782 vim_free(buf->b_ffname);
2783 vim_free(buf->b_sfname);
2784 buf->b_ffname = NULL;
2785 buf->b_sfname = NULL;
2786#ifdef UNIX
2787 st.st_dev = (dev_T)-1;
2788#endif
2789 }
2790 else
2791 {
2792 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2793 if (ffname == NULL) /* out of memory */
2794 return FAIL;
2795
2796 /*
2797 * if the file name is already used in another buffer:
2798 * - if the buffer is loaded, fail
2799 * - if the buffer is not loaded, delete it from the list
2800 */
2801#ifdef UNIX
2802 if (mch_stat((char *)ffname, &st) < 0)
2803 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002804#endif
2805 if (!(buf->b_flags & BF_DUMMY))
2806#ifdef UNIX
2807 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002808#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002809 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002810#endif
2811 if (obuf != NULL && obuf != buf)
2812 {
2813 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2814 {
2815 if (message)
2816 EMSG(_("E95: Buffer with this name already exists"));
2817 vim_free(ffname);
2818 return FAIL;
2819 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002820 /* delete from the list */
2821 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 }
2823 sfname = vim_strsave(sfname);
2824 if (ffname == NULL || sfname == NULL)
2825 {
2826 vim_free(sfname);
2827 vim_free(ffname);
2828 return FAIL;
2829 }
2830#ifdef USE_FNAME_CASE
2831# ifdef USE_LONG_FNAME
2832 if (USE_LONG_FNAME)
2833# endif
2834 fname_case(sfname, 0); /* set correct case for short file name */
2835#endif
2836 vim_free(buf->b_ffname);
2837 vim_free(buf->b_sfname);
2838 buf->b_ffname = ffname;
2839 buf->b_sfname = sfname;
2840 }
2841 buf->b_fname = buf->b_sfname;
2842#ifdef UNIX
2843 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002844 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845 else
2846 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002847 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 buf->b_dev = st.st_dev;
2849 buf->b_ino = st.st_ino;
2850 }
2851#endif
2852
2853#ifndef SHORT_FNAME
2854 buf->b_shortname = FALSE;
2855#endif
2856
2857 buf_name_changed(buf);
2858 return OK;
2859}
2860
2861/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002862 * Crude way of changing the name of a buffer. Use with care!
2863 * The name should be relative to the current directory.
2864 */
2865 void
2866buf_set_name(fnum, name)
2867 int fnum;
2868 char_u *name;
2869{
2870 buf_T *buf;
2871
2872 buf = buflist_findnr(fnum);
2873 if (buf != NULL)
2874 {
2875 vim_free(buf->b_sfname);
2876 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002877 buf->b_ffname = vim_strsave(name);
2878 buf->b_sfname = NULL;
2879 /* Allocate ffname and expand into full path. Also resolves .lnk
2880 * files on Win32. */
2881 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002882 buf->b_fname = buf->b_sfname;
2883 }
2884}
2885
2886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 * Take care of what needs to be done when the name of buffer "buf" has
2888 * changed.
2889 */
2890 void
2891buf_name_changed(buf)
2892 buf_T *buf;
2893{
2894 /*
2895 * If the file name changed, also change the name of the swapfile
2896 */
2897 if (buf->b_ml.ml_mfp != NULL)
2898 ml_setname(buf);
2899
2900 if (curwin->w_buffer == buf)
2901 check_arg_idx(curwin); /* check file name for arg list */
2902#ifdef FEAT_TITLE
2903 maketitle(); /* set window title */
2904#endif
2905#ifdef FEAT_WINDOWS
2906 status_redraw_all(); /* status lines need to be redrawn */
2907#endif
2908 fmarks_check_names(buf); /* check named file marks */
2909 ml_timestamp(buf); /* reset timestamp */
2910}
2911
2912/*
2913 * set alternate file name for current window
2914 *
2915 * Used by do_one_cmd(), do_write() and do_ecmd().
2916 * Return the buffer.
2917 */
2918 buf_T *
2919setaltfname(ffname, sfname, lnum)
2920 char_u *ffname;
2921 char_u *sfname;
2922 linenr_T lnum;
2923{
2924 buf_T *buf;
2925
2926 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2927 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002928 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 curwin->w_alt_fnum = buf->b_fnum;
2930 return buf;
2931}
2932
2933/*
2934 * Get alternate file name for current window.
2935 * Return NULL if there isn't any, and give error message if requested.
2936 */
2937 char_u *
2938getaltfname(errmsg)
2939 int errmsg; /* give error message */
2940{
2941 char_u *fname;
2942 linenr_T dummy;
2943
2944 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2945 {
2946 if (errmsg)
2947 EMSG(_(e_noalt));
2948 return NULL;
2949 }
2950 return fname;
2951}
2952
2953/*
2954 * Add a file name to the buflist and return its number.
2955 * Uses same flags as buflist_new(), except BLN_DUMMY.
2956 *
2957 * used by qf_init(), main() and doarglist()
2958 */
2959 int
2960buflist_add(fname, flags)
2961 char_u *fname;
2962 int flags;
2963{
2964 buf_T *buf;
2965
2966 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2967 if (buf != NULL)
2968 return buf->b_fnum;
2969 return 0;
2970}
2971
2972#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2973/*
2974 * Adjust slashes in file names. Called after 'shellslash' was set.
2975 */
2976 void
2977buflist_slash_adjust()
2978{
2979 buf_T *bp;
2980
2981 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2982 {
2983 if (bp->b_ffname != NULL)
2984 slash_adjust(bp->b_ffname);
2985 if (bp->b_sfname != NULL)
2986 slash_adjust(bp->b_sfname);
2987 }
2988}
2989#endif
2990
2991/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002992 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 * Also save the local window option values.
2994 */
2995 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002996buflist_altfpos(win)
2997 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002999 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000}
3001
3002/*
3003 * Return TRUE if 'ffname' is not the same file as current file.
3004 * Fname must have a full path (expanded by mch_FullName()).
3005 */
3006 int
3007otherfile(ffname)
3008 char_u *ffname;
3009{
3010 return otherfile_buf(curbuf, ffname
3011#ifdef UNIX
3012 , NULL
3013#endif
3014 );
3015}
3016
3017 static int
3018otherfile_buf(buf, ffname
3019#ifdef UNIX
3020 , stp
3021#endif
3022 )
3023 buf_T *buf;
3024 char_u *ffname;
3025#ifdef UNIX
3026 struct stat *stp;
3027#endif
3028{
3029 /* no name is different */
3030 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3031 return TRUE;
3032 if (fnamecmp(ffname, buf->b_ffname) == 0)
3033 return FALSE;
3034#ifdef UNIX
3035 {
3036 struct stat st;
3037
3038 /* If no struct stat given, get it now */
3039 if (stp == NULL)
3040 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003041 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 st.st_dev = (dev_T)-1;
3043 stp = &st;
3044 }
3045 /* Use dev/ino to check if the files are the same, even when the names
3046 * are different (possible with links). Still need to compare the
3047 * name above, for when the file doesn't exist yet.
3048 * Problem: The dev/ino changes when a file is deleted (and created
3049 * again) and remains the same when renamed/moved. We don't want to
3050 * mch_stat() each buffer each time, that would be too slow. Get the
3051 * dev/ino again when they appear to match, but not when they appear
3052 * to be different: Could skip a buffer when it's actually the same
3053 * file. */
3054 if (buf_same_ino(buf, stp))
3055 {
3056 buf_setino(buf);
3057 if (buf_same_ino(buf, stp))
3058 return FALSE;
3059 }
3060 }
3061#endif
3062 return TRUE;
3063}
3064
3065#if defined(UNIX) || defined(PROTO)
3066/*
3067 * Set inode and device number for a buffer.
3068 * Must always be called when b_fname is changed!.
3069 */
3070 void
3071buf_setino(buf)
3072 buf_T *buf;
3073{
3074 struct stat st;
3075
3076 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3077 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003078 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 buf->b_dev = st.st_dev;
3080 buf->b_ino = st.st_ino;
3081 }
3082 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003083 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084}
3085
3086/*
3087 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3088 */
3089 static int
3090buf_same_ino(buf, stp)
3091 buf_T *buf;
3092 struct stat *stp;
3093{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003094 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003095 && stp->st_dev == buf->b_dev
3096 && stp->st_ino == buf->b_ino);
3097}
3098#endif
3099
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003100/*
3101 * Print info about the current buffer.
3102 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 void
3104fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003105 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 int shorthelp;
3107 int dont_truncate;
3108{
3109 char_u *name;
3110 int n;
3111 char_u *p;
3112 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003113 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114
3115 buffer = alloc(IOSIZE);
3116 if (buffer == NULL)
3117 return;
3118
3119 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3120 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003121 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 p = buffer + STRLEN(buffer);
3123 }
3124 else
3125 p = buffer;
3126
3127 *p++ = '"';
3128 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003129 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 else
3131 {
3132 if (!fullname && curbuf->b_fname != NULL)
3133 name = curbuf->b_fname;
3134 else
3135 name = curbuf->b_ffname;
3136 home_replace(shorthelp ? curbuf : NULL, name, p,
3137 (int)(IOSIZE - (p - buffer)), TRUE);
3138 }
3139
Bram Moolenaara800b422010-06-27 01:15:55 +02003140 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 curbufIsChanged() ? (shortmess(SHM_MOD)
3142 ? " [+]" : _(" [Modified]")) : " ",
3143 (curbuf->b_flags & BF_NOTEDITED)
3144#ifdef FEAT_QUICKFIX
3145 && !bt_dontwrite(curbuf)
3146#endif
3147 ? _("[Not edited]") : "",
3148 (curbuf->b_flags & BF_NEW)
3149#ifdef FEAT_QUICKFIX
3150 && !bt_dontwrite(curbuf)
3151#endif
3152 ? _("[New file]") : "",
3153 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003154 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 : _("[readonly]")) : "",
3156 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3157 || curbuf->b_p_ro) ?
3158 " " : "");
3159 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3160 * causes an overflow, thus for large numbers divide instead. */
3161 if (curwin->w_cursor.lnum > 1000000L)
3162 n = (int)(((long)curwin->w_cursor.lnum) /
3163 ((long)curbuf->b_ml.ml_line_count / 100L));
3164 else
3165 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3166 (long)curbuf->b_ml.ml_line_count);
3167 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3168 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003169 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 }
3171#ifdef FEAT_CMDL_INFO
3172 else if (p_ru)
3173 {
3174 /* Current line and column are already on the screen -- webb */
3175 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003176 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003178 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 (long)curbuf->b_ml.ml_line_count, n);
3180 }
3181#endif
3182 else
3183 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003184 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 _("line %ld of %ld --%d%%-- col "),
3186 (long)curwin->w_cursor.lnum,
3187 (long)curbuf->b_ml.ml_line_count,
3188 n);
3189 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003190 len = STRLEN(buffer);
3191 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3193 }
3194
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003195 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196
3197 if (dont_truncate)
3198 {
3199 /* Temporarily set msg_scroll to avoid the message being truncated.
3200 * First call msg_start() to get the message in the right place. */
3201 msg_start();
3202 n = msg_scroll;
3203 msg_scroll = TRUE;
3204 msg(buffer);
3205 msg_scroll = n;
3206 }
3207 else
3208 {
3209 p = msg_trunc_attr(buffer, FALSE, 0);
3210 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 /* Need to repeat the message after redrawing when:
3212 * - When restart_edit is set (otherwise there will be a delay
3213 * before redrawing).
3214 * - When the screen was scrolled but there is no wait-return
3215 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003216 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 }
3218
3219 vim_free(buffer);
3220}
3221
3222 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003223col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003225 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 int col;
3227 int vcol;
3228{
3229 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003230 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003232 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233}
3234
3235#if defined(FEAT_TITLE) || defined(PROTO)
3236/*
3237 * put file name in title bar of window and in icon title
3238 */
3239
3240static char_u *lasttitle = NULL;
3241static char_u *lasticon = NULL;
3242
3243 void
3244maketitle()
3245{
3246 char_u *p;
3247 char_u *t_str = NULL;
3248 char_u *i_name;
3249 char_u *i_str = NULL;
3250 int maxlen = 0;
3251 int len;
3252 int mustset;
3253 char_u buf[IOSIZE];
3254 int off;
3255
3256 if (!redrawing())
3257 {
3258 /* Postpone updating the title when 'lazyredraw' is set. */
3259 need_maketitle = TRUE;
3260 return;
3261 }
3262
3263 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003264 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265 return;
3266
3267 if (p_title)
3268 {
3269 if (p_titlelen > 0)
3270 {
3271 maxlen = p_titlelen * Columns / 100;
3272 if (maxlen < 10)
3273 maxlen = 10;
3274 }
3275
3276 t_str = buf;
3277 if (*p_titlestring != NUL)
3278 {
3279#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003280 if (stl_syntax & STL_IN_TITLE)
3281 {
3282 int use_sandbox = FALSE;
3283 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003284
3285# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003286 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003287# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003288 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003290 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003291 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003292 if (called_emsg)
3293 set_string_option_direct((char_u *)"titlestring", -1,
3294 (char_u *)"", OPT_FREE, SID_ERROR);
3295 called_emsg |= save_called_emsg;
3296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 else
3298#endif
3299 t_str = p_titlestring;
3300 }
3301 else
3302 {
3303 /* format: "fname + (path) (1 of 2) - VIM" */
3304
Bram Moolenaar2c666692012-09-05 13:30:40 +02003305#define SPACE_FOR_FNAME (IOSIZE - 100)
3306#define SPACE_FOR_DIR (IOSIZE - 20)
3307#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003309 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 else
3311 {
3312 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003313 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316
3317 switch (bufIsChanged(curbuf)
3318 + (curbuf->b_p_ro * 2)
3319 + (!curbuf->b_p_ma * 4))
3320 {
3321 case 1: STRCAT(buf, " +"); break;
3322 case 2: STRCAT(buf, " ="); break;
3323 case 3: STRCAT(buf, " =+"); break;
3324 case 4:
3325 case 6: STRCAT(buf, " -"); break;
3326 case 5:
3327 case 7: STRCAT(buf, " -+"); break;
3328 }
3329
3330 if (curbuf->b_fname != NULL)
3331 {
3332 /* Get path of file, replace home dir with ~ */
3333 off = (int)STRLEN(buf);
3334 buf[off++] = ' ';
3335 buf[off++] = '(';
3336 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003337 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#ifdef BACKSLASH_IN_FILENAME
3339 /* avoid "c:/name" to be reduced to "c" */
3340 if (isalpha(buf[off]) && buf[off + 1] == ':')
3341 off += 2;
3342#endif
3343 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003344 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003347 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003348 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003351
Bram Moolenaar2c666692012-09-05 13:30:40 +02003352 /* Translate unprintable chars and concatenate. Keep some
3353 * room for the server name. When there is no room (very long
3354 * file name) use (...). */
3355 if (off < SPACE_FOR_DIR)
3356 {
3357 p = transstr(buf + off);
3358 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3359 vim_free(p);
3360 }
3361 else
3362 {
3363 vim_strncpy(buf + off, (char_u *)"...",
3364 (size_t)(SPACE_FOR_ARGNR - off));
3365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 STRCAT(buf, ")");
3367 }
3368
Bram Moolenaar2c666692012-09-05 13:30:40 +02003369 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370
3371#if defined(FEAT_CLIENTSERVER)
3372 if (serverName != NULL)
3373 {
3374 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003375 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 }
3377 else
3378#endif
3379 STRCAT(buf, " - VIM");
3380
3381 if (maxlen > 0)
3382 {
3383 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003384 if (vim_strsize(buf) > maxlen)
3385 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 }
3387 }
3388 }
3389 mustset = ti_change(t_str, &lasttitle);
3390
3391 if (p_icon)
3392 {
3393 i_str = buf;
3394 if (*p_iconstring != NUL)
3395 {
3396#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003397 if (stl_syntax & STL_IN_ICON)
3398 {
3399 int use_sandbox = FALSE;
3400 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003401
3402# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003403 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003404# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003405 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003407 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003408 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003409 if (called_emsg)
3410 set_string_option_direct((char_u *)"iconstring", -1,
3411 (char_u *)"", OPT_FREE, SID_ERROR);
3412 called_emsg |= save_called_emsg;
3413 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 else
3415#endif
3416 i_str = p_iconstring;
3417 }
3418 else
3419 {
3420 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003421 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003422 else /* use file name only in icon */
3423 i_name = gettail(curbuf->b_ffname);
3424 *i_str = NUL;
3425 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003426 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 if (len > 100)
3428 {
3429 len -= 100;
3430#ifdef FEAT_MBYTE
3431 if (has_mbyte)
3432 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3433#endif
3434 i_name += len;
3435 }
3436 STRCPY(i_str, i_name);
3437 trans_characters(i_str, IOSIZE);
3438 }
3439 }
3440
3441 mustset |= ti_change(i_str, &lasticon);
3442
3443 if (mustset)
3444 resettitle();
3445}
3446
3447/*
3448 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3449 * from "str" if it does.
3450 * Return TRUE when "*last" changed.
3451 */
3452 static int
3453ti_change(str, last)
3454 char_u *str;
3455 char_u **last;
3456{
3457 if ((str == NULL) != (*last == NULL)
3458 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3459 {
3460 vim_free(*last);
3461 if (str == NULL)
3462 *last = NULL;
3463 else
3464 *last = vim_strsave(str);
3465 return TRUE;
3466 }
3467 return FALSE;
3468}
3469
3470/*
3471 * Put current window title back (used after calling a shell)
3472 */
3473 void
3474resettitle()
3475{
3476 mch_settitle(lasttitle, lasticon);
3477}
Bram Moolenaarea408852005-06-25 22:49:46 +00003478
3479# if defined(EXITFREE) || defined(PROTO)
3480 void
3481free_titles()
3482{
3483 vim_free(lasttitle);
3484 vim_free(lasticon);
3485}
3486# endif
3487
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488#endif /* FEAT_TITLE */
3489
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003490#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003492 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 * Return length of string in screen cells.
3494 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003495 * Normally works for window "wp", except when working for 'tabline' then it
3496 * is "curwin".
3497 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003498 * Items are drawn interspersed with the text that surrounds it
3499 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3500 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3501 *
3502 * If maxwidth is not zero, the string will be filled at any middle marker
3503 * or truncated if too long, fillchar is used for all whitespace.
3504 */
3505 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003506build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3507 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003509 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 size_t outlen; /* length of out[] */
3511 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003512 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513 int fillchar;
3514 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003515 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3516 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517{
3518 char_u *p;
3519 char_u *s;
3520 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003521 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522#ifdef FEAT_EVAL
3523 win_T *o_curwin;
3524 buf_T *o_curbuf;
3525#endif
3526 int empty_line;
3527 colnr_T virtcol;
3528 long l;
3529 long n;
3530 int prevchar_isflag;
3531 int prevchar_isitem;
3532 int itemisflag;
3533 int fillable;
3534 char_u *str;
3535 long num;
3536 int width;
3537 int itemcnt;
3538 int curitem;
3539 int groupitem[STL_MAX_ITEM];
3540 int groupdepth;
3541 struct stl_item
3542 {
3543 char_u *start;
3544 int minwid;
3545 int maxwid;
3546 enum
3547 {
3548 Normal,
3549 Empty,
3550 Group,
3551 Middle,
3552 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003553 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 Trunc
3555 } type;
3556 } item[STL_MAX_ITEM];
3557 int minwid;
3558 int maxwid;
3559 int zeropad;
3560 char_u base;
3561 char_u opt;
3562#define TMPLEN 70
3563 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003564 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003565 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003566
3567#ifdef FEAT_EVAL
3568 /*
3569 * When the format starts with "%!" then evaluate it as an expression and
3570 * use the result as the actual format string.
3571 */
3572 if (fmt[0] == '%' && fmt[1] == '!')
3573 {
3574 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3575 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003576 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003577 }
3578#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579
3580 if (fillchar == 0)
3581 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003582#ifdef FEAT_MBYTE
3583 /* Can't handle a multi-byte fill character yet. */
3584 else if (mb_char2len(fillchar) > 1)
3585 fillchar = '-';
3586#endif
3587
Bram Moolenaar567199b2013-04-24 16:52:36 +02003588 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3589 * p will become invalid when getting another buffer line. */
3590 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3591 empty_line = (*p == NUL);
3592
3593 /* Get the byte value now, in case we need it below. This is more
3594 * efficient than making a copy of the line. */
3595 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3596 byteval = 0;
3597 else
3598#ifdef FEAT_MBYTE
3599 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3600#else
3601 byteval = p[wp->w_cursor.col];
3602#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603
3604 groupdepth = 0;
3605 p = out;
3606 curitem = 0;
3607 prevchar_isflag = TRUE;
3608 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003609 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003611 if (curitem == STL_MAX_ITEM)
3612 {
3613 /* There are too many items. Add the error code to the statusline
3614 * to give the user a hint about what went wrong. */
3615 if (p + 6 < out + outlen)
3616 {
3617 mch_memmove(p, " E541", (size_t)5);
3618 p += 5;
3619 }
3620 break;
3621 }
3622
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 if (*s != NUL && *s != '%')
3624 prevchar_isflag = prevchar_isitem = FALSE;
3625
3626 /*
3627 * Handle up to the next '%' or the end.
3628 */
3629 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3630 *p++ = *s++;
3631 if (*s == NUL || p + 1 >= out + outlen)
3632 break;
3633
3634 /*
3635 * Handle one '%' item.
3636 */
3637 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003638 if (*s == NUL) /* ignore trailing % */
3639 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (*s == '%')
3641 {
3642 if (p + 1 >= out + outlen)
3643 break;
3644 *p++ = *s++;
3645 prevchar_isflag = prevchar_isitem = FALSE;
3646 continue;
3647 }
3648 if (*s == STL_MIDDLEMARK)
3649 {
3650 s++;
3651 if (groupdepth > 0)
3652 continue;
3653 item[curitem].type = Middle;
3654 item[curitem++].start = p;
3655 continue;
3656 }
3657 if (*s == STL_TRUNCMARK)
3658 {
3659 s++;
3660 item[curitem].type = Trunc;
3661 item[curitem++].start = p;
3662 continue;
3663 }
3664 if (*s == ')')
3665 {
3666 s++;
3667 if (groupdepth < 1)
3668 continue;
3669 groupdepth--;
3670
3671 t = item[groupitem[groupdepth]].start;
3672 *p = NUL;
3673 l = vim_strsize(t);
3674 if (curitem > groupitem[groupdepth] + 1
3675 && item[groupitem[groupdepth]].minwid == 0)
3676 {
3677 /* remove group if all items are empty */
3678 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3679 if (item[n].type == Normal)
3680 break;
3681 if (n == curitem)
3682 {
3683 p = t;
3684 l = 0;
3685 }
3686 }
3687 if (l > item[groupitem[groupdepth]].maxwid)
3688 {
3689 /* truncate, remove n bytes of text at the start */
3690#ifdef FEAT_MBYTE
3691 if (has_mbyte)
3692 {
3693 /* Find the first character that should be included. */
3694 n = 0;
3695 while (l >= item[groupitem[groupdepth]].maxwid)
3696 {
3697 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003698 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 }
3700 }
3701 else
3702#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003703 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704
3705 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003706 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 p = p - n + 1;
3708#ifdef FEAT_MBYTE
3709 /* Fill up space left over by half a double-wide char. */
3710 while (++l < item[groupitem[groupdepth]].minwid)
3711 *p++ = fillchar;
3712#endif
3713
3714 /* correct the start of the items for the truncation */
3715 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3716 {
3717 item[l].start -= n;
3718 if (item[l].start < t)
3719 item[l].start = t;
3720 }
3721 }
3722 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3723 {
3724 /* fill */
3725 n = item[groupitem[groupdepth]].minwid;
3726 if (n < 0)
3727 {
3728 /* fill by appending characters */
3729 n = 0 - n;
3730 while (l++ < n && p + 1 < out + outlen)
3731 *p++ = fillchar;
3732 }
3733 else
3734 {
3735 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003736 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 l = n - l;
3738 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003739 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 p += l;
3741 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3742 item[n].start += l;
3743 for ( ; l > 0; l--)
3744 *t++ = fillchar;
3745 }
3746 }
3747 continue;
3748 }
3749 minwid = 0;
3750 maxwid = 9999;
3751 zeropad = FALSE;
3752 l = 1;
3753 if (*s == '0')
3754 {
3755 s++;
3756 zeropad = TRUE;
3757 }
3758 if (*s == '-')
3759 {
3760 s++;
3761 l = -1;
3762 }
3763 if (VIM_ISDIGIT(*s))
3764 {
3765 minwid = (int)getdigits(&s);
3766 if (minwid < 0) /* overflow */
3767 minwid = 0;
3768 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003769 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 {
3771 item[curitem].type = Highlight;
3772 item[curitem].start = p;
3773 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3774 s++;
3775 curitem++;
3776 continue;
3777 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003778 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3779 {
3780 if (*s == STL_TABCLOSENR)
3781 {
3782 if (minwid == 0)
3783 {
3784 /* %X ends the close label, go back to the previously
3785 * define tab label nr. */
3786 for (n = curitem - 1; n >= 0; --n)
3787 if (item[n].type == TabPage && item[n].minwid >= 0)
3788 {
3789 minwid = item[n].minwid;
3790 break;
3791 }
3792 }
3793 else
3794 /* close nrs are stored as negative values */
3795 minwid = - minwid;
3796 }
3797 item[curitem].type = TabPage;
3798 item[curitem].start = p;
3799 item[curitem].minwid = minwid;
3800 s++;
3801 curitem++;
3802 continue;
3803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 if (*s == '.')
3805 {
3806 s++;
3807 if (VIM_ISDIGIT(*s))
3808 {
3809 maxwid = (int)getdigits(&s);
3810 if (maxwid <= 0) /* overflow */
3811 maxwid = 50;
3812 }
3813 }
3814 minwid = (minwid > 50 ? 50 : minwid) * l;
3815 if (*s == '(')
3816 {
3817 groupitem[groupdepth++] = curitem;
3818 item[curitem].type = Group;
3819 item[curitem].start = p;
3820 item[curitem].minwid = minwid;
3821 item[curitem].maxwid = maxwid;
3822 s++;
3823 curitem++;
3824 continue;
3825 }
3826 if (vim_strchr(STL_ALL, *s) == NULL)
3827 {
3828 s++;
3829 continue;
3830 }
3831 opt = *s++;
3832
3833 /* OK - now for the real work */
3834 base = 'D';
3835 itemisflag = FALSE;
3836 fillable = TRUE;
3837 num = -1;
3838 str = NULL;
3839 switch (opt)
3840 {
3841 case STL_FILEPATH:
3842 case STL_FULLPATH:
3843 case STL_FILENAME:
3844 fillable = FALSE; /* don't change ' ' to fillchar */
3845 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003846 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 else
3848 {
3849 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003850 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3852 }
3853 trans_characters(NameBuff, MAXPATHL);
3854 if (opt != STL_FILENAME)
3855 str = NameBuff;
3856 else
3857 str = gettail(NameBuff);
3858 break;
3859
3860 case STL_VIM_EXPR: /* '{' */
3861 itemisflag = TRUE;
3862 t = p;
3863 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3864 *p++ = *s++;
3865 if (*s != '}') /* missing '}' or out of space */
3866 break;
3867 s++;
3868 *p = 0;
3869 p = t;
3870
3871#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003872 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3874
3875 o_curbuf = curbuf;
3876 o_curwin = curwin;
3877 curwin = wp;
3878 curbuf = wp->w_buffer;
3879
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003880 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881
3882 curwin = o_curwin;
3883 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003884 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885
3886 if (str != NULL && *str != 0)
3887 {
3888 if (*skipdigits(str) == NUL)
3889 {
3890 num = atoi((char *)str);
3891 vim_free(str);
3892 str = NULL;
3893 itemisflag = FALSE;
3894 }
3895 }
3896#endif
3897 break;
3898
3899 case STL_LINE:
3900 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3901 ? 0L : (long)(wp->w_cursor.lnum);
3902 break;
3903
3904 case STL_NUMLINES:
3905 num = wp->w_buffer->b_ml.ml_line_count;
3906 break;
3907
3908 case STL_COLUMN:
3909 num = !(State & INSERT) && empty_line
3910 ? 0 : (int)wp->w_cursor.col + 1;
3911 break;
3912
3913 case STL_VIRTCOL:
3914 case STL_VIRTCOL_ALT:
3915 /* In list mode virtcol needs to be recomputed */
3916 virtcol = wp->w_virtcol;
3917 if (wp->w_p_list && lcs_tab1 == NUL)
3918 {
3919 wp->w_p_list = FALSE;
3920 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3921 wp->w_p_list = TRUE;
3922 }
3923 ++virtcol;
3924 /* Don't display %V if it's the same as %c. */
3925 if (opt == STL_VIRTCOL_ALT
3926 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3927 ? 0 : (int)wp->w_cursor.col + 1)))
3928 break;
3929 num = (long)virtcol;
3930 break;
3931
3932 case STL_PERCENTAGE:
3933 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3934 (long)wp->w_buffer->b_ml.ml_line_count);
3935 break;
3936
3937 case STL_ALTPERCENT:
3938 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003939 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 break;
3941
3942 case STL_ARGLISTSTAT:
3943 fillable = FALSE;
3944 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003945 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 str = tmp;
3947 break;
3948
3949 case STL_KEYMAP:
3950 fillable = FALSE;
3951 if (get_keymap_str(wp, tmp, TMPLEN))
3952 str = tmp;
3953 break;
3954 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003955#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003956 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957#else
3958 num = 0;
3959#endif
3960 break;
3961
3962 case STL_BUFNO:
3963 num = wp->w_buffer->b_fnum;
3964 break;
3965
3966 case STL_OFFSET_X:
3967 base = 'X';
3968 case STL_OFFSET:
3969#ifdef FEAT_BYTEOFF
3970 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3971 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3972 0L : l + 1 + (!(State & INSERT) && empty_line ?
3973 0 : (int)wp->w_cursor.col);
3974#endif
3975 break;
3976
3977 case STL_BYTEVAL_X:
3978 base = 'X';
3979 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02003980 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 if (num == NL)
3982 num = 0;
3983 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3984 num = NL;
3985 break;
3986
3987 case STL_ROFLAG:
3988 case STL_ROFLAG_ALT:
3989 itemisflag = TRUE;
3990 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02003991 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992 break;
3993
3994 case STL_HELPFLAG:
3995 case STL_HELPFLAG_ALT:
3996 itemisflag = TRUE;
3997 if (wp->w_buffer->b_help)
3998 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003999 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 break;
4001
4002#ifdef FEAT_AUTOCMD
4003 case STL_FILETYPE:
4004 if (*wp->w_buffer->b_p_ft != NUL
4005 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4006 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004007 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4008 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 str = tmp;
4010 }
4011 break;
4012
4013 case STL_FILETYPE_ALT:
4014 itemisflag = TRUE;
4015 if (*wp->w_buffer->b_p_ft != NUL
4016 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4017 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004018 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4019 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 for (t = tmp; *t != 0; t++)
4021 *t = TOUPPER_LOC(*t);
4022 str = tmp;
4023 }
4024 break;
4025#endif
4026
4027#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4028 case STL_PREVIEWFLAG:
4029 case STL_PREVIEWFLAG_ALT:
4030 itemisflag = TRUE;
4031 if (wp->w_p_pvw)
4032 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4033 : _("[Preview]"));
4034 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004035
4036 case STL_QUICKFIX:
4037 if (bt_quickfix(wp->w_buffer))
4038 str = (char_u *)(wp->w_llist_ref
4039 ? _(msg_loclist)
4040 : _(msg_qflist));
4041 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042#endif
4043
4044 case STL_MODIFIED:
4045 case STL_MODIFIED_ALT:
4046 itemisflag = TRUE;
4047 switch ((opt == STL_MODIFIED_ALT)
4048 + bufIsChanged(wp->w_buffer) * 2
4049 + (!wp->w_buffer->b_p_ma) * 4)
4050 {
4051 case 2: str = (char_u *)"[+]"; break;
4052 case 3: str = (char_u *)",+"; break;
4053 case 4: str = (char_u *)"[-]"; break;
4054 case 5: str = (char_u *)",-"; break;
4055 case 6: str = (char_u *)"[+-]"; break;
4056 case 7: str = (char_u *)",+-"; break;
4057 }
4058 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004059
4060 case STL_HIGHLIGHT:
4061 t = s;
4062 while (*s != '#' && *s != NUL)
4063 ++s;
4064 if (*s == '#')
4065 {
4066 item[curitem].type = Highlight;
4067 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004068 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004069 curitem++;
4070 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004071 if (*s != NUL)
4072 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004073 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
4075
4076 item[curitem].start = p;
4077 item[curitem].type = Normal;
4078 if (str != NULL && *str)
4079 {
4080 t = str;
4081 if (itemisflag)
4082 {
4083 if ((t[0] && t[1])
4084 && ((!prevchar_isitem && *t == ',')
4085 || (prevchar_isflag && *t == ' ')))
4086 t++;
4087 prevchar_isflag = TRUE;
4088 }
4089 l = vim_strsize(t);
4090 if (l > 0)
4091 prevchar_isitem = TRUE;
4092 if (l > maxwid)
4093 {
4094 while (l >= maxwid)
4095#ifdef FEAT_MBYTE
4096 if (has_mbyte)
4097 {
4098 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004099 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 }
4101 else
4102#endif
4103 l -= byte2cells(*t++);
4104 if (p + 1 >= out + outlen)
4105 break;
4106 *p++ = '<';
4107 }
4108 if (minwid > 0)
4109 {
4110 for (; l < minwid && p + 1 < out + outlen; l++)
4111 {
4112 /* Don't put a "-" in front of a digit. */
4113 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4114 *p++ = ' ';
4115 else
4116 *p++ = fillchar;
4117 }
4118 minwid = 0;
4119 }
4120 else
4121 minwid *= -1;
4122 while (*t && p + 1 < out + outlen)
4123 {
4124 *p++ = *t++;
4125 /* Change a space by fillchar, unless fillchar is '-' and a
4126 * digit follows. */
4127 if (fillable && p[-1] == ' '
4128 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4129 p[-1] = fillchar;
4130 }
4131 for (; l < minwid && p + 1 < out + outlen; l++)
4132 *p++ = fillchar;
4133 }
4134 else if (num >= 0)
4135 {
4136 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4137 char_u nstr[20];
4138
4139 if (p + 20 >= out + outlen)
4140 break; /* not sufficient space */
4141 prevchar_isitem = TRUE;
4142 t = nstr;
4143 if (opt == STL_VIRTCOL_ALT)
4144 {
4145 *t++ = '-';
4146 minwid--;
4147 }
4148 *t++ = '%';
4149 if (zeropad)
4150 *t++ = '0';
4151 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004152 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 *t = 0;
4154
4155 for (n = num, l = 1; n >= nbase; n /= nbase)
4156 l++;
4157 if (opt == STL_VIRTCOL_ALT)
4158 l++;
4159 if (l > maxwid)
4160 {
4161 l += 2;
4162 n = l - maxwid;
4163 while (l-- > maxwid)
4164 num /= nbase;
4165 *t++ = '>';
4166 *t++ = '%';
4167 *t = t[-3];
4168 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004169 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4170 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 }
4172 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004173 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4174 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 p += STRLEN(p);
4176 }
4177 else
4178 item[curitem].type = Empty;
4179
4180 if (opt == STL_VIM_EXPR)
4181 vim_free(str);
4182
4183 if (num >= 0 || (!itemisflag && str && *str))
4184 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4185 curitem++;
4186 }
4187 *p = NUL;
4188 itemcnt = curitem;
4189
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004190#ifdef FEAT_EVAL
4191 if (usefmt != fmt)
4192 vim_free(usefmt);
4193#endif
4194
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 width = vim_strsize(out);
4196 if (maxwidth > 0 && width > maxwidth)
4197 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004198 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 l = 0;
4200 if (itemcnt == 0)
4201 s = out;
4202 else
4203 {
4204 for ( ; l < itemcnt; l++)
4205 if (item[l].type == Trunc)
4206 {
4207 /* Truncate at %< item. */
4208 s = item[l].start;
4209 break;
4210 }
4211 if (l == itemcnt)
4212 {
4213 /* No %< item, truncate first item. */
4214 s = item[0].start;
4215 l = 0;
4216 }
4217 }
4218
4219 if (width - vim_strsize(s) >= maxwidth)
4220 {
4221 /* Truncation mark is beyond max length */
4222#ifdef FEAT_MBYTE
4223 if (has_mbyte)
4224 {
4225 s = out;
4226 width = 0;
4227 for (;;)
4228 {
4229 width += ptr2cells(s);
4230 if (width >= maxwidth)
4231 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004232 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234 /* Fill up for half a double-wide character. */
4235 while (++width < maxwidth)
4236 *s++ = fillchar;
4237 }
4238 else
4239#endif
4240 s = out + maxwidth - 1;
4241 for (l = 0; l < itemcnt; l++)
4242 if (item[l].start > s)
4243 break;
4244 itemcnt = l;
4245 *s++ = '>';
4246 *s = 0;
4247 }
4248 else
4249 {
4250#ifdef FEAT_MBYTE
4251 if (has_mbyte)
4252 {
4253 n = 0;
4254 while (width >= maxwidth)
4255 {
4256 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004257 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 }
4259 }
4260 else
4261#endif
4262 n = width - maxwidth + 1;
4263 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004264 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 *s = '<';
4266
4267 /* Fill up for half a double-wide character. */
4268 while (++width < maxwidth)
4269 {
4270 s = s + STRLEN(s);
4271 *s++ = fillchar;
4272 *s = NUL;
4273 }
4274
4275 --n; /* count the '<' */
4276 for (; l < itemcnt; l++)
4277 {
4278 if (item[l].start - n >= s)
4279 item[l].start -= n;
4280 else
4281 item[l].start = s;
4282 }
4283 }
4284 width = maxwidth;
4285 }
4286 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4287 {
4288 /* Apply STL_MIDDLE if any */
4289 for (l = 0; l < itemcnt; l++)
4290 if (item[l].type == Middle)
4291 break;
4292 if (l < itemcnt)
4293 {
4294 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004295 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 for (s = item[l].start; s < p; s++)
4297 *s = fillchar;
4298 for (l++; l < itemcnt; l++)
4299 item[l].start += maxwidth - width;
4300 width = maxwidth;
4301 }
4302 }
4303
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004304 /* Store the info about highlighting. */
4305 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004307 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 for (l = 0; l < itemcnt; l++)
4309 {
4310 if (item[l].type == Highlight)
4311 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004312 sp->start = item[l].start;
4313 sp->userhl = item[l].minwid;
4314 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 }
4316 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004317 sp->start = NULL;
4318 sp->userhl = 0;
4319 }
4320
4321 /* Store the info about tab pages labels. */
4322 if (tabtab != NULL)
4323 {
4324 sp = tabtab;
4325 for (l = 0; l < itemcnt; l++)
4326 {
4327 if (item[l].type == TabPage)
4328 {
4329 sp->start = item[l].start;
4330 sp->userhl = item[l].minwid;
4331 sp++;
4332 }
4333 }
4334 sp->start = NULL;
4335 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 }
4337
4338 return width;
4339}
4340#endif /* FEAT_STL_OPT */
4341
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004342#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4343 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004345 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4346 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 */
4348 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004349get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004351 char_u *buf;
4352 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353{
4354 long above; /* number of lines above window */
4355 long below; /* number of lines below window */
4356
4357 above = wp->w_topline - 1;
4358#ifdef FEAT_DIFF
4359 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4360#endif
4361 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4362 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004363 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4364 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004366 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004368 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 ? (int)(above / ((above + below) / 100L))
4370 : (int)(above * 100L / (above + below)));
4371}
4372#endif
4373
4374/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004375 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 * Return TRUE if it was appended.
4377 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004378 static int
4379append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 win_T *wp;
4381 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004382 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
4385 char_u *p;
4386
4387 if (ARGCOUNT <= 1) /* nothing to do */
4388 return FALSE;
4389
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004390 p = buf + STRLEN(buf); /* go to the end of the buffer */
4391 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 return FALSE;
4393 *p++ = ' ';
4394 *p++ = '(';
4395 if (add_file)
4396 {
4397 STRCPY(p, "file ");
4398 p += 5;
4399 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004400 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4401 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4403 return TRUE;
4404}
4405
4406/*
4407 * If fname is not a full path, make it a full path.
4408 * Returns pointer to allocated memory (NULL for failure).
4409 */
4410 char_u *
4411fix_fname(fname)
4412 char_u *fname;
4413{
4414 /*
4415 * Force expanding the path always for Unix, because symbolic links may
4416 * mess up the full path name, even though it starts with a '/'.
4417 * Also expand when there is ".." in the file name, try to remove it,
4418 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004419 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 * For MS-Windows also expand names like "longna~1" to "longname".
4421 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004422#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 return FullName_save(fname, TRUE);
4424#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004425 if (!vim_isAbsName(fname)
4426 || strstr((char *)fname, "..") != NULL
4427 || strstr((char *)fname, "//") != NULL
4428# ifdef BACKSLASH_IN_FILENAME
4429 || strstr((char *)fname, "\\\\") != NULL
4430# endif
4431# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004433# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 )
4435 return FullName_save(fname, FALSE);
4436
4437 fname = vim_strsave(fname);
4438
Bram Moolenaar9b942202007-10-03 12:31:33 +00004439# ifdef USE_FNAME_CASE
4440# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004442# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 {
4444 if (fname != NULL)
4445 fname_case(fname, 0); /* set correct case for file name */
4446 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004447# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448
4449 return fname;
4450#endif
4451}
4452
4453/*
4454 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4455 * "ffname" becomes a pointer to allocated memory (or NULL).
4456 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 void
4458fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004459 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 char_u **ffname;
4461 char_u **sfname;
4462{
4463 if (*ffname == NULL) /* if no file name given, nothing to do */
4464 return;
4465 if (*sfname == NULL) /* if no short file name given, use ffname */
4466 *sfname = *ffname;
4467 *ffname = fix_fname(*ffname); /* expand to full path */
4468
4469#ifdef FEAT_SHORTCUT
4470 if (!buf->b_p_bin)
4471 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004472 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
4474 /* If the file name is a shortcut file, use the file it links to. */
4475 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004476 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
4478 vim_free(*ffname);
4479 *ffname = rfname;
4480 *sfname = rfname;
4481 }
4482 }
4483#endif
4484}
4485
4486/*
4487 * Get the file name for an argument list entry.
4488 */
4489 char_u *
4490alist_name(aep)
4491 aentry_T *aep;
4492{
4493 buf_T *bp;
4494
4495 /* Use the name from the associated buffer if it exists. */
4496 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004497 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 return aep->ae_fname;
4499 return bp->b_fname;
4500}
4501
4502#if defined(FEAT_WINDOWS) || defined(PROTO)
4503/*
4504 * do_arg_all(): Open up to 'count' windows, one for each argument.
4505 */
4506 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004507do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 int count;
4509 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004510 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511{
4512 int i;
4513 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004514 char_u *opened; /* Array of weight for which args are open:
4515 * 0: not opened
4516 * 1: opened in other tab
4517 * 2: opened in curtab
4518 * 3: opened in curtab and curwin
4519 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004520 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 int use_firstwin = FALSE; /* use first window for arglist */
4522 int split_ret = OK;
4523 int p_ea_save;
4524 alist_T *alist; /* argument list to be used */
4525 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004526 tabpage_T *tpnext;
4527 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004528 win_T *old_curwin, *last_curwin;
4529 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004530 win_T *new_curwin = NULL;
4531 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532
4533 if (ARGCOUNT <= 0)
4534 {
4535 /* Don't give an error message. We don't want it when the ":all"
4536 * command is in the .vimrc. */
4537 return;
4538 }
4539 setpcmark();
4540
4541 opened_len = ARGCOUNT;
4542 opened = alloc_clear((unsigned)opened_len);
4543 if (opened == NULL)
4544 return;
4545
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004546 /* Autocommands may do anything to the argument list. Make sure it's not
4547 * freed while we are working here by "locking" it. We still have to
4548 * watch out for its size to be changed. */
4549 alist = curwin->w_alist;
4550 ++alist->al_refcount;
4551
4552 old_curwin = curwin;
4553 old_curtab = curtab;
4554
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555#ifdef FEAT_GUI
4556 need_mouse_correct = TRUE;
4557#endif
4558
4559 /*
4560 * Try closing all windows that are not in the argument list.
4561 * Also close windows that are not full width;
4562 * When 'hidden' or "forceit" set the buffer becomes hidden.
4563 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004564 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004566 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004567 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004568 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004570 tpnext = curtab->tp_next;
4571 for (wp = firstwin; wp != NULL; wp = wpnext)
4572 {
4573 wpnext = wp->w_next;
4574 buf = wp->w_buffer;
4575 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004576 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004578 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004580 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004581 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004582 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004584 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004585 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004587 if (i < alist->al_ga.ga_len
4588 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4589 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4590 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004592 int weight = 1;
4593
4594 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004595 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004596 ++weight;
4597 if (old_curwin == wp)
4598 ++weight;
4599 }
4600
4601 if (weight > (int)opened[i])
4602 {
4603 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004604 if (i == 0)
4605 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004606 if (new_curwin != NULL)
4607 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004608 new_curwin = wp;
4609 new_curtab = curtab;
4610 }
4611 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004612 else if (keep_tabs)
4613 i = opened_len;
4614
4615 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004616 {
4617 /* Use the current argument list for all windows
4618 * containing a file from it. */
4619 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004620 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004621 ++wp->w_alist->al_refcount;
4622 }
4623 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
4626 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004627 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004629 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004631 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004632 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004634 /* If the buffer was changed, and we would like to hide it,
4635 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004636 if (!P_HID(buf) && buf->b_nwindows <= 1
4637 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004639 (void)autowrite(buf, FALSE);
4640#ifdef FEAT_AUTOCMD
4641 /* check if autocommands removed the window */
4642 if (!win_valid(wp) || !buf_valid(buf))
4643 {
4644 wpnext = firstwin; /* start all over... */
4645 continue;
4646 }
4647#endif
4648 }
4649#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004650 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004651 if (firstwin == lastwin
4652 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004653#endif
4654 use_firstwin = TRUE;
4655#ifdef FEAT_WINDOWS
4656 else
4657 {
4658 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4659# ifdef FEAT_AUTOCMD
4660 /* check if autocommands removed the next window */
4661 if (!win_valid(wpnext))
4662 wpnext = firstwin; /* start all over... */
4663# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 }
4665#endif
4666 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004669
4670 /* Without the ":tab" modifier only do the current tab page. */
4671 if (had_tab == 0 || tpnext == NULL)
4672 break;
4673
4674# ifdef FEAT_AUTOCMD
4675 /* check if autocommands removed the next tab page */
4676 if (!valid_tabpage(tpnext))
4677 tpnext = first_tabpage; /* start all over...*/
4678# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004679 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681
4682 /*
4683 * Open a window for files in the argument list that don't have one.
4684 * ARGCOUNT may change while doing this, because of autocommands.
4685 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004686 if (count > opened_len || count <= 0)
4687 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688
4689#ifdef FEAT_AUTOCMD
4690 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4691 ++autocmd_no_enter;
4692 ++autocmd_no_leave;
4693#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004694 last_curwin = curwin;
4695 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004697#ifdef FEAT_WINDOWS
4698 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4699 * leaving an empty tab page when executed locally. */
4700 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4701 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4702 use_firstwin = TRUE;
4703#endif
4704
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004705 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 {
4707 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4708 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004709 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 /* Move the already present window to below the current window */
4712 if (curwin->w_arg_idx != i)
4713 {
4714 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4715 {
4716 if (wpnext->w_arg_idx == i)
4717 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004718 if (keep_tabs)
4719 {
4720 new_curwin = wpnext;
4721 new_curtab = curtab;
4722 }
4723 else
4724 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 break;
4726 }
4727 }
4728 }
4729 }
4730 else if (split_ret == OK)
4731 {
4732 if (!use_firstwin) /* split current window */
4733 {
4734 p_ea_save = p_ea;
4735 p_ea = TRUE; /* use space from all windows */
4736 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4737 p_ea = p_ea_save;
4738 if (split_ret == FAIL)
4739 continue;
4740 }
4741#ifdef FEAT_AUTOCMD
4742 else /* first window: do autocmd for leaving this buffer */
4743 --autocmd_no_leave;
4744#endif
4745
4746 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004747 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 */
4749 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004750 if (i == 0)
4751 {
4752 new_curwin = curwin;
4753 new_curtab = curtab;
4754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4756 ECMD_ONE,
4757 ((P_HID(curwin->w_buffer)
4758 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004759 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760#ifdef FEAT_AUTOCMD
4761 if (use_firstwin)
4762 ++autocmd_no_leave;
4763#endif
4764 use_firstwin = FALSE;
4765 }
4766 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004767
4768 /* When ":tab" was used open a new tab for a new window repeatedly. */
4769 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4770 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772
4773 /* Remove the "lock" on the argument list. */
4774 alist_unlink(alist);
4775
4776#ifdef FEAT_AUTOCMD
4777 --autocmd_no_enter;
4778#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004779 /* restore last referenced tabpage's curwin */
4780 if (last_curtab != new_curtab)
4781 {
4782 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004783 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004784 if (win_valid(last_curwin))
4785 win_enter(last_curwin, FALSE);
4786 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004787 /* to window with first arg */
4788 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004789 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004790 if (win_valid(new_curwin))
4791 win_enter(new_curwin, FALSE);
4792
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793#ifdef FEAT_AUTOCMD
4794 --autocmd_no_leave;
4795#endif
4796 vim_free(opened);
4797}
4798
4799# if defined(FEAT_LISTCMDS) || defined(PROTO)
4800/*
4801 * Open a window for a number of buffers.
4802 */
4803 void
4804ex_buffer_all(eap)
4805 exarg_T *eap;
4806{
4807 buf_T *buf;
4808 win_T *wp, *wpnext;
4809 int split_ret = OK;
4810 int p_ea_save;
4811 int open_wins = 0;
4812 int r;
4813 int count; /* Maximum number of windows to open. */
4814 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004815#ifdef FEAT_WINDOWS
4816 int had_tab = cmdmod.tab;
4817 tabpage_T *tpnext;
4818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
4820 if (eap->addr_count == 0) /* make as many windows as possible */
4821 count = 9999;
4822 else
4823 count = eap->line2; /* make as many windows as specified */
4824 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4825 all = FALSE;
4826 else
4827 all = TRUE;
4828
4829 setpcmark();
4830
4831#ifdef FEAT_GUI
4832 need_mouse_correct = TRUE;
4833#endif
4834
4835 /*
4836 * Close superfluous windows (two windows for the same buffer).
4837 * Also close windows that are not full-width.
4838 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004839#ifdef FEAT_WINDOWS
4840 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004841 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004842 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004845 tpnext = curtab->tp_next;
4846 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004848 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004849 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004850#ifdef FEAT_VERTSPLIT
4851 || ((cmdmod.split & WSP_VERT)
4852 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004853 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004854 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004856#ifdef FEAT_WINDOWS
4857 || (had_tab > 0 && wp != firstwin)
4858#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004859 ) && firstwin != lastwin
4860#ifdef FEAT_AUTOCMD
4861 && !(wp->w_closing || wp->w_buffer->b_closing)
4862#endif
4863 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004864 {
4865 win_close(wp, FALSE);
4866#ifdef FEAT_AUTOCMD
4867 wpnext = firstwin; /* just in case an autocommand does
4868 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004869 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004870 open_wins = 0;
4871#endif
4872 }
4873 else
4874 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004876
4877#ifdef FEAT_WINDOWS
4878 /* Without the ":tab" modifier only do the current tab page. */
4879 if (had_tab == 0 || tpnext == NULL)
4880 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004881 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004883#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884
4885 /*
4886 * Go through the buffer list. When a buffer doesn't have a window yet,
4887 * open one. Otherwise move the window to the right position.
4888 * Watch out for autocommands that delete buffers or windows!
4889 */
4890#ifdef FEAT_AUTOCMD
4891 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4892 ++autocmd_no_enter;
4893#endif
4894 win_enter(lastwin, FALSE);
4895#ifdef FEAT_AUTOCMD
4896 ++autocmd_no_leave;
4897#endif
4898 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4899 {
4900 /* Check if this buffer needs a window */
4901 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4902 continue;
4903
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004904#ifdef FEAT_WINDOWS
4905 if (had_tab != 0)
4906 {
4907 /* With the ":tab" modifier don't move the window. */
4908 if (buf->b_nwindows > 0)
4909 wp = lastwin; /* buffer has a window, skip it */
4910 else
4911 wp = NULL;
4912 }
4913 else
4914#endif
4915 {
4916 /* Check if this buffer already has a window */
4917 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4918 if (wp->w_buffer == buf)
4919 break;
4920 /* If the buffer already has a window, move it */
4921 if (wp != NULL)
4922 win_move_after(wp, curwin);
4923 }
4924
4925 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 {
4927 /* Split the window and put the buffer in it */
4928 p_ea_save = p_ea;
4929 p_ea = TRUE; /* use space from all windows */
4930 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4931 ++open_wins;
4932 p_ea = p_ea_save;
4933 if (split_ret == FAIL)
4934 continue;
4935
4936 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004937#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 swap_exists_action = SEA_DIALOG;
4939#endif
4940 set_curbuf(buf, DOBUF_GOTO);
4941#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004943 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004944#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 break;
4948 }
4949#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004950#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 if (swap_exists_action == SEA_QUIT)
4952 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004953# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4954 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004955
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004956 /* Reset the error/interrupt/exception state here so that
4957 * aborting() returns FALSE when closing a window. */
4958 enter_cleanup(&cs);
4959# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004960
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004961 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 win_close(curwin, TRUE);
4963 --open_wins;
4964 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004965 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004966
4967# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4968 /* Restore the error/interrupt/exception state if not
4969 * discarded by a new aborting error, interrupt, or uncaught
4970 * exception. */
4971 leave_cleanup(&cs);
4972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 }
4974 else
4975 handle_swap_exists(NULL);
4976#endif
4977 }
4978
4979 ui_breakcheck();
4980 if (got_int)
4981 {
4982 (void)vgetc(); /* only break the file loading, not the rest */
4983 break;
4984 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004985#ifdef FEAT_EVAL
4986 /* Autocommands deleted the buffer or aborted script processing!!! */
4987 if (aborting())
4988 break;
4989#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004990#ifdef FEAT_WINDOWS
4991 /* When ":tab" was used open a new tab for a new window repeatedly. */
4992 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4993 cmdmod.tab = 9999;
4994#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996#ifdef FEAT_AUTOCMD
4997 --autocmd_no_enter;
4998#endif
4999 win_enter(firstwin, FALSE); /* back to first window */
5000#ifdef FEAT_AUTOCMD
5001 --autocmd_no_leave;
5002#endif
5003
5004 /*
5005 * Close superfluous windows.
5006 */
5007 for (wp = lastwin; open_wins > count; )
5008 {
5009 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5010 || autowrite(wp->w_buffer, FALSE) == OK);
5011#ifdef FEAT_AUTOCMD
5012 if (!win_valid(wp))
5013 {
5014 /* BufWrite Autocommands made the window invalid, start over */
5015 wp = lastwin;
5016 }
5017 else
5018#endif
5019 if (r)
5020 {
5021 win_close(wp, !P_HID(wp->w_buffer));
5022 --open_wins;
5023 wp = lastwin;
5024 }
5025 else
5026 {
5027 wp = wp->w_prev;
5028 if (wp == NULL)
5029 break;
5030 }
5031 }
5032}
5033# endif /* FEAT_LISTCMDS */
5034
5035#endif /* FEAT_WINDOWS */
5036
Bram Moolenaara3227e22006-03-08 21:32:40 +00005037static int chk_modeline __ARGS((linenr_T, int));
5038
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039/*
5040 * do_modelines() - process mode lines for the current file
5041 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005042 * "flags" can be:
5043 * OPT_WINONLY only set options local to window
5044 * OPT_NOWIN don't set options local to window
5045 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 * Returns immediately if the "ml" option isn't set.
5047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005049do_modelines(flags)
5050 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005052 linenr_T lnum;
5053 int nmlines;
5054 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
5056 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5057 return;
5058
5059 /* Disallow recursive entry here. Can happen when executing a modeline
5060 * triggers an autocommand, which reloads modelines with a ":do". */
5061 if (entered)
5062 return;
5063
5064 ++entered;
5065 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5066 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005067 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 nmlines = 0;
5069
5070 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5071 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005072 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 nmlines = 0;
5074 --entered;
5075}
5076
5077#include "version.h" /* for version number */
5078
5079/*
5080 * chk_modeline() - check a single line for a mode string
5081 * Return FAIL if an error encountered.
5082 */
5083 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005084chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005086 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087{
5088 char_u *s;
5089 char_u *e;
5090 char_u *linecopy; /* local copy of any modeline found */
5091 int prev;
5092 int vers;
5093 int end;
5094 int retval = OK;
5095 char_u *save_sourcing_name;
5096 linenr_T save_sourcing_lnum;
5097#ifdef FEAT_EVAL
5098 scid_T save_SID;
5099#endif
5100
5101 prev = -1;
5102 for (s = ml_get(lnum); *s != NUL; ++s)
5103 {
5104 if (prev == -1 || vim_isspace(prev))
5105 {
5106 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5107 || STRNCMP(s, "vi:", (size_t)3) == 0)
5108 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005109 /* Accept both "vim" and "Vim". */
5110 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5113 e = s + 4;
5114 else
5115 e = s + 3;
5116 vers = getdigits(&e);
5117 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005118 && (s[0] != 'V'
5119 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 && (s[3] == ':'
5121 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5122 || (VIM_VERSION_100 < vers && s[3] == '<')
5123 || (VIM_VERSION_100 > vers && s[3] == '>')
5124 || (VIM_VERSION_100 == vers && s[3] == '=')))
5125 break;
5126 }
5127 }
5128 prev = *s;
5129 }
5130
5131 if (*s)
5132 {
5133 do /* skip over "ex:", "vi:" or "vim:" */
5134 ++s;
5135 while (s[-1] != ':');
5136
5137 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5138 if (linecopy == NULL)
5139 return FAIL;
5140
5141 save_sourcing_lnum = sourcing_lnum;
5142 save_sourcing_name = sourcing_name;
5143 sourcing_lnum = lnum; /* prepare for emsg() */
5144 sourcing_name = (char_u *)"modelines";
5145
5146 end = FALSE;
5147 while (end == FALSE)
5148 {
5149 s = skipwhite(s);
5150 if (*s == NUL)
5151 break;
5152
5153 /*
5154 * Find end of set command: ':' or end of line.
5155 * Skip over "\:", replacing it with ":".
5156 */
5157 for (e = s; *e != ':' && *e != NUL; ++e)
5158 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005159 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 if (*e == NUL)
5161 end = TRUE;
5162
5163 /*
5164 * If there is a "set" command, require a terminating ':' and
5165 * ignore the stuff after the ':'.
5166 * "vi:set opt opt opt: foo" -- foo not interpreted
5167 * "vi:opt opt opt: foo" -- foo interpreted
5168 * Accept "se" for compatibility with Elvis.
5169 */
5170 if (STRNCMP(s, "set ", (size_t)4) == 0
5171 || STRNCMP(s, "se ", (size_t)3) == 0)
5172 {
5173 if (*e != ':') /* no terminating ':'? */
5174 break;
5175 end = TRUE;
5176 s = vim_strchr(s, ' ') + 1;
5177 }
5178 *e = NUL; /* truncate the set command */
5179
5180 if (*s != NUL) /* skip over an empty "::" */
5181 {
5182#ifdef FEAT_EVAL
5183 save_SID = current_SID;
5184 current_SID = SID_MODELINE;
5185#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005186 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187#ifdef FEAT_EVAL
5188 current_SID = save_SID;
5189#endif
5190 if (retval == FAIL) /* stop if error found */
5191 break;
5192 }
5193 s = e + 1; /* advance to next part */
5194 }
5195
5196 sourcing_lnum = save_sourcing_lnum;
5197 sourcing_name = save_sourcing_name;
5198
5199 vim_free(linecopy);
5200 }
5201 return retval;
5202}
5203
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005204#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 int
5206read_viminfo_bufferlist(virp, writing)
5207 vir_T *virp;
5208 int writing;
5209{
5210 char_u *tab;
5211 linenr_T lnum;
5212 colnr_T col;
5213 buf_T *buf;
5214 char_u *sfname;
5215 char_u *xline;
5216
5217 /* Handle long line and escaped characters. */
5218 xline = viminfo_readstring(virp, 1, FALSE);
5219
5220 /* don't read in if there are files on the command-line or if writing: */
5221 if (xline != NULL && !writing && ARGCOUNT == 0
5222 && find_viminfo_parameter('%') != NULL)
5223 {
5224 /* Format is: <fname> Tab <lnum> Tab <col>.
5225 * Watch out for a Tab in the file name, work from the end. */
5226 lnum = 0;
5227 col = 0;
5228 tab = vim_strrchr(xline, '\t');
5229 if (tab != NULL)
5230 {
5231 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005232 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 tab = vim_strrchr(xline, '\t');
5234 if (tab != NULL)
5235 {
5236 *tab++ = '\0';
5237 lnum = atol((char *)tab);
5238 }
5239 }
5240
5241 /* Expand "~/" in the file name at "line + 1" to a full path.
5242 * Then try shortening it by comparing with the current directory */
5243 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005244 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245
5246 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5247 if (buf != NULL) /* just in case... */
5248 {
5249 buf->b_last_cursor.lnum = lnum;
5250 buf->b_last_cursor.col = col;
5251 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5252 }
5253 }
5254 vim_free(xline);
5255
5256 return viminfo_readline(virp);
5257}
5258
5259 void
5260write_viminfo_bufferlist(fp)
5261 FILE *fp;
5262{
5263 buf_T *buf;
5264#ifdef FEAT_WINDOWS
5265 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005266 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267#endif
5268 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005269 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270
5271 if (find_viminfo_parameter('%') == NULL)
5272 return;
5273
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005274 /* Without a number -1 is returned: do all buffers. */
5275 max_buffers = get_viminfo_parameter('%');
5276
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005278#define LINE_BUF_LEN (MAXPATHL + 40)
5279 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 if (line == NULL)
5281 return;
5282
5283#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005284 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 set_last_cursor(win);
5286#else
5287 set_last_cursor(curwin);
5288#endif
5289
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005290 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5292 {
5293 if (buf->b_fname == NULL
5294 || !buf->b_p_bl
5295#ifdef FEAT_QUICKFIX
5296 || bt_quickfix(buf)
5297#endif
5298 || removable(buf->b_ffname))
5299 continue;
5300
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005301 if (max_buffers-- == 0)
5302 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 putc('%', fp);
5304 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005305 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 (long)buf->b_last_cursor.lnum,
5307 buf->b_last_cursor.col);
5308 viminfo_writestring(fp, line);
5309 }
5310 vim_free(line);
5311}
5312#endif
5313
5314
5315/*
5316 * Return special buffer name.
5317 * Returns NULL when the buffer has a normal file name.
5318 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005319 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320buf_spname(buf)
5321 buf_T *buf;
5322{
5323#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5324 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005325 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005326 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005327 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005328
5329 /*
5330 * For location list window, w_llist_ref points to the location list.
5331 * For quickfix window, w_llist_ref is NULL.
5332 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005333 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005334 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005335 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005336 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338#endif
5339#ifdef FEAT_QUICKFIX
5340 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5341 * contains the name as specified by the user */
5342 if (bt_nofile(buf))
5343 {
5344 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005345 return buf->b_sfname;
5346 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005347 }
5348#endif
5349 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005350 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 return NULL;
5352}
5353
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005354#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5355 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5356 || defined(PROTO)
5357/*
5358 * Find a window for buffer "buf".
5359 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5360 * If not found FAIL is returned.
5361 */
5362 int
5363find_win_for_buf(buf, wp, tp)
5364 buf_T *buf;
5365 win_T **wp;
5366 tabpage_T **tp;
5367{
5368 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5369 if ((*wp)->w_buffer == buf)
5370 goto win_found;
5371 return FAIL;
5372win_found:
5373 return OK;
5374}
5375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376
5377#if defined(FEAT_SIGNS) || defined(PROTO)
5378/*
5379 * Insert the sign into the signlist.
5380 */
5381 static void
5382insert_sign(buf, prev, next, id, lnum, typenr)
5383 buf_T *buf; /* buffer to store sign in */
5384 signlist_T *prev; /* previous sign entry */
5385 signlist_T *next; /* next sign entry */
5386 int id; /* sign ID */
5387 linenr_T lnum; /* line number which gets the mark */
5388 int typenr; /* typenr of sign we are adding */
5389{
5390 signlist_T *newsign;
5391
5392 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5393 if (newsign != NULL)
5394 {
5395 newsign->id = id;
5396 newsign->lnum = lnum;
5397 newsign->typenr = typenr;
5398 newsign->next = next;
5399#ifdef FEAT_NETBEANS_INTG
5400 newsign->prev = prev;
5401 if (next != NULL)
5402 next->prev = newsign;
5403#endif
5404
5405 if (prev == NULL)
5406 {
5407 /* When adding first sign need to redraw the windows to create the
5408 * column for signs. */
5409 if (buf->b_signlist == NULL)
5410 {
5411 redraw_buf_later(buf, NOT_VALID);
5412 changed_cline_bef_curs();
5413 }
5414
5415 /* first sign in signlist */
5416 buf->b_signlist = newsign;
5417 }
5418 else
5419 prev->next = newsign;
5420 }
5421}
5422
5423/*
5424 * Add the sign into the signlist. Find the right spot to do it though.
5425 */
5426 void
5427buf_addsign(buf, id, lnum, typenr)
5428 buf_T *buf; /* buffer to store sign in */
5429 int id; /* sign ID */
5430 linenr_T lnum; /* line number which gets the mark */
5431 int typenr; /* typenr of sign we are adding */
5432{
5433 signlist_T *sign; /* a sign in the signlist */
5434 signlist_T *prev; /* the previous sign */
5435
5436 prev = NULL;
5437 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5438 {
5439 if (lnum == sign->lnum && id == sign->id)
5440 {
5441 sign->typenr = typenr;
5442 return;
5443 }
5444 else if (
5445#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5446 id < 0 &&
5447#endif
5448 lnum < sign->lnum)
5449 {
5450#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5451 /* XXX - GRP: Is this because of sign slide problem? Or is it
5452 * really needed? Or is it because we allow multiple signs per
5453 * line? If so, should I add that feature to FEAT_SIGNS?
5454 */
5455 while (prev != NULL && prev->lnum == lnum)
5456 prev = prev->prev;
5457 if (prev == NULL)
5458 sign = buf->b_signlist;
5459 else
5460 sign = prev->next;
5461#endif
5462 insert_sign(buf, prev, sign, id, lnum, typenr);
5463 return;
5464 }
5465 prev = sign;
5466 }
5467#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5468 /* XXX - GRP: See previous comment */
5469 while (prev != NULL && prev->lnum == lnum)
5470 prev = prev->prev;
5471 if (prev == NULL)
5472 sign = buf->b_signlist;
5473 else
5474 sign = prev->next;
5475#endif
5476 insert_sign(buf, prev, sign, id, lnum, typenr);
5477
5478 return;
5479}
5480
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005481 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482buf_change_sign_type(buf, markId, typenr)
5483 buf_T *buf; /* buffer to store sign in */
5484 int markId; /* sign ID */
5485 int typenr; /* typenr of sign we are adding */
5486{
5487 signlist_T *sign; /* a sign in the signlist */
5488
5489 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5490 {
5491 if (sign->id == markId)
5492 {
5493 sign->typenr = typenr;
5494 return sign->lnum;
5495 }
5496 }
5497
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005498 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499}
5500
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005501 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502buf_getsigntype(buf, lnum, type)
5503 buf_T *buf;
5504 linenr_T lnum;
5505 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5506{
5507 signlist_T *sign; /* a sign in a b_signlist */
5508
5509 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5510 if (sign->lnum == lnum
5511 && (type == SIGN_ANY
5512# ifdef FEAT_SIGN_ICONS
5513 || (type == SIGN_ICON
5514 && sign_get_image(sign->typenr) != NULL)
5515# endif
5516 || (type == SIGN_TEXT
5517 && sign_get_text(sign->typenr) != NULL)
5518 || (type == SIGN_LINEHL
5519 && sign_get_attr(sign->typenr, TRUE) != 0)))
5520 return sign->typenr;
5521 return 0;
5522}
5523
5524
5525 linenr_T
5526buf_delsign(buf, id)
5527 buf_T *buf; /* buffer sign is stored in */
5528 int id; /* sign id */
5529{
5530 signlist_T **lastp; /* pointer to pointer to current sign */
5531 signlist_T *sign; /* a sign in a b_signlist */
5532 signlist_T *next; /* the next sign in a b_signlist */
5533 linenr_T lnum; /* line number whose sign was deleted */
5534
5535 lastp = &buf->b_signlist;
5536 lnum = 0;
5537 for (sign = buf->b_signlist; sign != NULL; sign = next)
5538 {
5539 next = sign->next;
5540 if (sign->id == id)
5541 {
5542 *lastp = next;
5543#ifdef FEAT_NETBEANS_INTG
5544 if (next != NULL)
5545 next->prev = sign->prev;
5546#endif
5547 lnum = sign->lnum;
5548 vim_free(sign);
5549 break;
5550 }
5551 else
5552 lastp = &sign->next;
5553 }
5554
5555 /* When deleted the last sign need to redraw the windows to remove the
5556 * sign column. */
5557 if (buf->b_signlist == NULL)
5558 {
5559 redraw_buf_later(buf, NOT_VALID);
5560 changed_cline_bef_curs();
5561 }
5562
5563 return lnum;
5564}
5565
5566
5567/*
5568 * Find the line number of the sign with the requested id. If the sign does
5569 * not exist, return 0 as the line number. This will still let the correct file
5570 * get loaded.
5571 */
5572 int
5573buf_findsign(buf, id)
5574 buf_T *buf; /* buffer to store sign in */
5575 int id; /* sign ID */
5576{
5577 signlist_T *sign; /* a sign in the signlist */
5578
5579 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5580 if (sign->id == id)
5581 return sign->lnum;
5582
5583 return 0;
5584}
5585
5586 int
5587buf_findsign_id(buf, lnum)
5588 buf_T *buf; /* buffer whose sign we are searching for */
5589 linenr_T lnum; /* line number of sign */
5590{
5591 signlist_T *sign; /* a sign in the signlist */
5592
5593 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5594 if (sign->lnum == lnum)
5595 return sign->id;
5596
5597 return 0;
5598}
5599
5600
5601# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5602/* see if a given type of sign exists on a specific line */
5603 int
5604buf_findsigntype_id(buf, lnum, typenr)
5605 buf_T *buf; /* buffer whose sign we are searching for */
5606 linenr_T lnum; /* line number of sign */
5607 int typenr; /* sign type number */
5608{
5609 signlist_T *sign; /* a sign in the signlist */
5610
5611 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5612 if (sign->lnum == lnum && sign->typenr == typenr)
5613 return sign->id;
5614
5615 return 0;
5616}
5617
5618
5619# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5620/* return the number of icons on the given line */
5621 int
5622buf_signcount(buf, lnum)
5623 buf_T *buf;
5624 linenr_T lnum;
5625{
5626 signlist_T *sign; /* a sign in the signlist */
5627 int count = 0;
5628
5629 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5630 if (sign->lnum == lnum)
5631 if (sign_get_image(sign->typenr) != NULL)
5632 count++;
5633
5634 return count;
5635}
5636# endif /* FEAT_SIGN_ICONS */
5637# endif /* FEAT_NETBEANS_INTG */
5638
5639
5640/*
5641 * Delete signs in buffer "buf".
5642 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005643 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644buf_delete_signs(buf)
5645 buf_T *buf;
5646{
5647 signlist_T *next;
5648
5649 while (buf->b_signlist != NULL)
5650 {
5651 next = buf->b_signlist->next;
5652 vim_free(buf->b_signlist);
5653 buf->b_signlist = next;
5654 }
5655}
5656
5657/*
5658 * Delete all signs in all buffers.
5659 */
5660 void
5661buf_delete_all_signs()
5662{
5663 buf_T *buf; /* buffer we are checking for signs */
5664
5665 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5666 if (buf->b_signlist != NULL)
5667 {
5668 /* Need to redraw the windows to remove the sign column. */
5669 redraw_buf_later(buf, NOT_VALID);
5670 buf_delete_signs(buf);
5671 }
5672}
5673
5674/*
5675 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5676 */
5677 void
5678sign_list_placed(rbuf)
5679 buf_T *rbuf;
5680{
5681 buf_T *buf;
5682 signlist_T *p;
5683 char lbuf[BUFSIZ];
5684
5685 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5686 msg_putchar('\n');
5687 if (rbuf == NULL)
5688 buf = firstbuf;
5689 else
5690 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005691 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 {
5693 if (buf->b_signlist != NULL)
5694 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005695 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5697 msg_putchar('\n');
5698 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005699 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005701 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5703 MSG_PUTS(lbuf);
5704 msg_putchar('\n');
5705 }
5706 if (rbuf != NULL)
5707 break;
5708 buf = buf->b_next;
5709 }
5710}
5711
5712/*
5713 * Adjust a placed sign for inserted/deleted lines.
5714 */
5715 void
5716sign_mark_adjust(line1, line2, amount, amount_after)
5717 linenr_T line1;
5718 linenr_T line2;
5719 long amount;
5720 long amount_after;
5721{
5722 signlist_T *sign; /* a sign in a b_signlist */
5723
5724 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5725 {
5726 if (sign->lnum >= line1 && sign->lnum <= line2)
5727 {
5728 if (amount == MAXLNUM)
5729 sign->lnum = line1;
5730 else
5731 sign->lnum += amount;
5732 }
5733 else if (sign->lnum > line2)
5734 sign->lnum += amount_after;
5735 }
5736}
5737#endif /* FEAT_SIGNS */
5738
5739/*
5740 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5741 */
5742 void
5743set_buflisted(on)
5744 int on;
5745{
5746 if (on != curbuf->b_p_bl)
5747 {
5748 curbuf->b_p_bl = on;
5749#ifdef FEAT_AUTOCMD
5750 if (on)
5751 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5752 else
5753 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5754#endif
5755 }
5756}
5757
5758/*
5759 * Read the file for "buf" again and check if the contents changed.
5760 * Return TRUE if it changed or this could not be checked.
5761 */
5762 int
5763buf_contents_changed(buf)
5764 buf_T *buf;
5765{
5766 buf_T *newbuf;
5767 int differ = TRUE;
5768 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 exarg_T ea;
5771
5772 /* Allocate a buffer without putting it in the buffer list. */
5773 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5774 if (newbuf == NULL)
5775 return TRUE;
5776
5777 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5778 if (prep_exarg(&ea, buf) == FAIL)
5779 {
5780 wipe_buffer(newbuf, FALSE);
5781 return TRUE;
5782 }
5783
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 /* set curwin/curbuf to buf and save a few things */
5785 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786
Bram Moolenaar4770d092006-01-12 23:22:24 +00005787 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 && readfile(buf->b_ffname, buf->b_fname,
5789 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5790 &ea, READ_NEW | READ_DUMMY) == OK)
5791 {
5792 /* compare the two files line by line */
5793 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5794 {
5795 differ = FALSE;
5796 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5797 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5798 {
5799 differ = TRUE;
5800 break;
5801 }
5802 }
5803 }
5804 vim_free(ea.cmd);
5805
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 /* restore curwin/curbuf and a few other things */
5807 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808
5809 if (curbuf != newbuf) /* safety check */
5810 wipe_buffer(newbuf, FALSE);
5811
5812 return differ;
5813}
5814
5815/*
5816 * Wipe out a buffer and decrement the last buffer number if it was used for
5817 * this buffer. Call this to wipe out a temp buffer that does not contain any
5818 * marks.
5819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 void
5821wipe_buffer(buf, aucmd)
5822 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005823 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824{
5825 if (buf->b_fnum == top_file_num - 1)
5826 --top_file_num;
5827
5828#ifdef FEAT_AUTOCMD
5829 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005830 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005832 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833#ifdef FEAT_AUTOCMD
5834 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005835 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005836#endif
5837}