blob: ddbcbc4b505e9084b0dbf2b1241367453af8cce5 [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;
1952}
1953
1954/*
1955 * get alternate file n
1956 * set linenr to lnum or altfpos.lnum if lnum == 0
1957 * also set cursor column to altfpos.col if 'startofline' is not set.
1958 * if (options & GETF_SETMARK) call setpcmark()
1959 * if (options & GETF_ALT) we are jumping to an alternate file.
1960 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1961 *
1962 * return FAIL for failure, OK for success
1963 */
1964 int
1965buflist_getfile(n, lnum, options, forceit)
1966 int n;
1967 linenr_T lnum;
1968 int options;
1969 int forceit;
1970{
1971 buf_T *buf;
1972#ifdef FEAT_WINDOWS
1973 win_T *wp = NULL;
1974#endif
1975 pos_T *fpos;
1976 colnr_T col;
1977
1978 buf = buflist_findnr(n);
1979 if (buf == NULL)
1980 {
1981 if ((options & GETF_ALT) && n == 0)
1982 EMSG(_(e_noalt));
1983 else
1984 EMSGN(_("E92: Buffer %ld not found"), n);
1985 return FAIL;
1986 }
1987
1988 /* if alternate file is the current buffer, nothing to do */
1989 if (buf == curbuf)
1990 return OK;
1991
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001992 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001993 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001994 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001996 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001997#ifdef FEAT_AUTOCMD
1998 if (curbuf_locked())
1999 return FAIL;
2000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002 /* altfpos may be changed by getfile(), get it now */
2003 if (lnum == 0)
2004 {
2005 fpos = buflist_findfpos(buf);
2006 lnum = fpos->lnum;
2007 col = fpos->col;
2008 }
2009 else
2010 col = 0;
2011
2012#ifdef FEAT_WINDOWS
2013 if (options & GETF_SWITCH)
2014 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002015 /* If 'switchbuf' contains "useopen": jump to first window containing
2016 * "buf" if one exists */
2017 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 wp = buf_jump_open_win(buf);
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02002019 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00002020 * page containing "buf" if one exists */
2021 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002022 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00002023 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
2024 * isn't empty: open new window */
2025 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00002027 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
2028 tabpage_new();
2029 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02002031 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002032 }
2033 }
2034#endif
2035
2036 ++RedrawingDisabled;
2037 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2038 lnum, forceit) <= 0)
2039 {
2040 --RedrawingDisabled;
2041
2042 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
2043 if (!p_sol && col != 0)
2044 {
2045 curwin->w_cursor.col = col;
2046 check_cursor_col();
2047#ifdef FEAT_VIRTUALEDIT
2048 curwin->w_cursor.coladd = 0;
2049#endif
2050 curwin->w_set_curswant = TRUE;
2051 }
2052 return OK;
2053 }
2054 --RedrawingDisabled;
2055 return FAIL;
2056}
2057
2058/*
2059 * go to the last know line number for the current buffer
2060 */
2061 void
2062buflist_getfpos()
2063{
2064 pos_T *fpos;
2065
2066 fpos = buflist_findfpos(curbuf);
2067
2068 curwin->w_cursor.lnum = fpos->lnum;
2069 check_cursor_lnum();
2070
2071 if (p_sol)
2072 curwin->w_cursor.col = 0;
2073 else
2074 {
2075 curwin->w_cursor.col = fpos->col;
2076 check_cursor_col();
2077#ifdef FEAT_VIRTUALEDIT
2078 curwin->w_cursor.coladd = 0;
2079#endif
2080 curwin->w_set_curswant = TRUE;
2081 }
2082}
2083
Bram Moolenaar81695252004-12-29 20:58:21 +00002084#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2085/*
2086 * Find file in buffer list by name (it has to be for the current window).
2087 * Returns NULL if not found.
2088 */
2089 buf_T *
2090buflist_findname_exp(fname)
2091 char_u *fname;
2092{
2093 char_u *ffname;
2094 buf_T *buf = NULL;
2095
2096 /* First make the name into a full path name */
2097 ffname = FullName_save(fname,
2098#ifdef UNIX
2099 TRUE /* force expansion, get rid of symbolic links */
2100#else
2101 FALSE
2102#endif
2103 );
2104 if (ffname != NULL)
2105 {
2106 buf = buflist_findname(ffname);
2107 vim_free(ffname);
2108 }
2109 return buf;
2110}
2111#endif
2112
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113/*
2114 * Find file in buffer list by name (it has to be for the current window).
2115 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002116 * Skips dummy buffers.
2117 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 */
2119 buf_T *
2120buflist_findname(ffname)
2121 char_u *ffname;
2122{
2123#ifdef UNIX
2124 struct stat st;
2125
2126 if (mch_stat((char *)ffname, &st) < 0)
2127 st.st_dev = (dev_T)-1;
2128 return buflist_findname_stat(ffname, &st);
2129}
2130
2131/*
2132 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2133 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002134 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 */
2136 static buf_T *
2137buflist_findname_stat(ffname, stp)
2138 char_u *ffname;
2139 struct stat *stp;
2140{
2141#endif
2142 buf_T *buf;
2143
2144 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002145 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146#ifdef UNIX
2147 , stp
2148#endif
2149 ))
2150 return buf;
2151 return NULL;
2152}
2153
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002154#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
2155 || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156/*
2157 * Find file in buffer list by a regexp pattern.
2158 * Return fnum of the found buffer.
2159 * Return < 0 for error.
2160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161 int
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002162buflist_findpat(pattern, pattern_end, unlisted, diffmode, curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 char_u *pattern;
2164 char_u *pattern_end; /* pointer to first char after pattern */
2165 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002166 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002167 int curtab_only; /* find buffers in current tab only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168{
2169 buf_T *buf;
2170 regprog_T *prog;
2171 int match = -1;
2172 int find_listed;
2173 char_u *pat;
2174 char_u *patend;
2175 int attempt;
2176 char_u *p;
2177 int toggledollar;
2178
2179 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2180 {
2181 if (*pattern == '%')
2182 match = curbuf->b_fnum;
2183 else
2184 match = curwin->w_alt_fnum;
2185#ifdef FEAT_DIFF
2186 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2187 match = -1;
2188#endif
2189 }
2190
2191 /*
2192 * Try four ways of matching a listed buffer:
2193 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002194 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 * attempt == 2: with '$' at end (only match at end)
2196 * attempt == 3: with '^' at start and '$' at end (only full match)
2197 * Repeat this for finding an unlisted buffer if there was no matching
2198 * listed buffer.
2199 */
2200 else
2201 {
2202 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2203 if (pat == NULL)
2204 return -1;
2205 patend = pat + STRLEN(pat) - 1;
2206 toggledollar = (patend > pat && *patend == '$');
2207
2208 /* First try finding a listed buffer. If not found and "unlisted"
2209 * is TRUE, try finding an unlisted buffer. */
2210 find_listed = TRUE;
2211 for (;;)
2212 {
2213 for (attempt = 0; attempt <= 3; ++attempt)
2214 {
2215 /* may add '^' and '$' */
2216 if (toggledollar)
2217 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2218 p = pat;
2219 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2220 ++p;
2221 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2222 if (prog == NULL)
2223 {
2224 vim_free(pat);
2225 return -1;
2226 }
2227
2228 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2229 if (buf->b_p_bl == find_listed
2230#ifdef FEAT_DIFF
2231 && (!diffmode || diff_mode_buf(buf))
2232#endif
2233 && buflist_match(prog, buf) != NULL)
2234 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002235 if (curtab_only)
2236 {
2237 /* Ignore the match if the buffer is not open in
2238 * the current tab. */
2239#ifdef FEAT_WINDOWS
2240 win_T *wp;
2241
2242 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2243 if (wp->w_buffer == buf)
2244 break;
2245 if (wp == NULL)
2246 continue;
2247#else
2248 if (curwin->w_buffer != buf)
2249 continue;
2250#endif
2251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 if (match >= 0) /* already found a match */
2253 {
2254 match = -2;
2255 break;
2256 }
2257 match = buf->b_fnum; /* remember first match */
2258 }
2259
Bram Moolenaar473de612013-06-08 18:19:48 +02002260 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 if (match >= 0) /* found one match */
2262 break;
2263 }
2264
2265 /* Only search for unlisted buffers if there was no match with
2266 * a listed buffer. */
2267 if (!unlisted || !find_listed || match != -1)
2268 break;
2269 find_listed = FALSE;
2270 }
2271
2272 vim_free(pat);
2273 }
2274
2275 if (match == -2)
2276 EMSG2(_("E93: More than one match for %s"), pattern);
2277 else if (match < 0)
2278 EMSG2(_("E94: No matching buffer for %s"), pattern);
2279 return match;
2280}
2281#endif
2282
2283#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2284
2285/*
2286 * Find all buffer names that match.
2287 * For command line expansion of ":buf" and ":sbuf".
2288 * Return OK if matches found, FAIL otherwise.
2289 */
2290 int
2291ExpandBufnames(pat, num_file, file, options)
2292 char_u *pat;
2293 int *num_file;
2294 char_u ***file;
2295 int options;
2296{
2297 int count = 0;
2298 buf_T *buf;
2299 int round;
2300 char_u *p;
2301 int attempt;
2302 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002303 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
2305 *num_file = 0; /* return values in case of FAIL */
2306 *file = NULL;
2307
Bram Moolenaar05159a02005-02-26 23:04:13 +00002308 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2309 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002311 patc = alloc((unsigned)STRLEN(pat) + 11);
2312 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002314 STRCPY(patc, "\\(^\\|[\\/]\\)");
2315 STRCPY(patc + 11, pat + 1);
2316 }
2317 else
2318 patc = pat;
2319
2320 /*
2321 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002322 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002323 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002324 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002325 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002326 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002327 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002328 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002329 if (prog == NULL)
2330 {
2331 if (patc != pat)
2332 vim_free(patc);
2333 return FAIL;
2334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335
2336 /*
2337 * round == 1: Count the matches.
2338 * round == 2: Build the array to keep the matches.
2339 */
2340 for (round = 1; round <= 2; ++round)
2341 {
2342 count = 0;
2343 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2344 {
2345 if (!buf->b_p_bl) /* skip unlisted buffers */
2346 continue;
2347 p = buflist_match(prog, buf);
2348 if (p != NULL)
2349 {
2350 if (round == 1)
2351 ++count;
2352 else
2353 {
2354 if (options & WILD_HOME_REPLACE)
2355 p = home_replace_save(buf, p);
2356 else
2357 p = vim_strsave(p);
2358 (*file)[count++] = p;
2359 }
2360 }
2361 }
2362 if (count == 0) /* no match found, break here */
2363 break;
2364 if (round == 1)
2365 {
2366 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2367 if (*file == NULL)
2368 {
Bram Moolenaar473de612013-06-08 18:19:48 +02002369 vim_regfree(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002370 if (patc != pat)
2371 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 return FAIL;
2373 }
2374 }
2375 }
Bram Moolenaar473de612013-06-08 18:19:48 +02002376 vim_regfree(prog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 if (count) /* match(es) found, break here */
2378 break;
2379 }
2380
Bram Moolenaar05159a02005-02-26 23:04:13 +00002381 if (patc != pat)
2382 vim_free(patc);
2383
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 *num_file = count;
2385 return (count == 0 ? FAIL : OK);
2386}
2387
2388#endif /* FEAT_CMDL_COMPL */
2389
2390#ifdef HAVE_BUFLIST_MATCH
2391/*
2392 * Check for a match on the file name for buffer "buf" with regprog "prog".
2393 */
2394 static char_u *
2395buflist_match(prog, buf)
2396 regprog_T *prog;
2397 buf_T *buf;
2398{
2399 char_u *match;
2400
2401 /* First try the short file name, then the long file name. */
2402 match = fname_match(prog, buf->b_sfname);
2403 if (match == NULL)
2404 match = fname_match(prog, buf->b_ffname);
2405
2406 return match;
2407}
2408
2409/*
2410 * Try matching the regexp in "prog" with file name "name".
2411 * Return "name" when there is a match, NULL when not.
2412 */
2413 static char_u *
2414fname_match(prog, name)
2415 regprog_T *prog;
2416 char_u *name;
2417{
2418 char_u *match = NULL;
2419 char_u *p;
2420 regmatch_T regmatch;
2421
2422 if (name != NULL)
2423 {
2424 regmatch.regprog = prog;
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01002425 regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (vim_regexec(&regmatch, name, (colnr_T)0))
2427 match = name;
2428 else
2429 {
2430 /* Replace $(HOME) with '~' and try matching again. */
2431 p = home_replace_save(NULL, name);
2432 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2433 match = name;
2434 vim_free(p);
2435 }
2436 }
2437
2438 return match;
2439}
2440#endif
2441
2442/*
2443 * find file in buffer list by number
2444 */
2445 buf_T *
2446buflist_findnr(nr)
2447 int nr;
2448{
2449 buf_T *buf;
2450
2451 if (nr == 0)
2452 nr = curwin->w_alt_fnum;
2453 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2454 if (buf->b_fnum == nr)
2455 return (buf);
2456 return NULL;
2457}
2458
2459/*
2460 * Get name of file 'n' in the buffer list.
2461 * When the file has no name an empty string is returned.
2462 * home_replace() is used to shorten the file name (used for marks).
2463 * Returns a pointer to allocated memory, of NULL when failed.
2464 */
2465 char_u *
2466buflist_nr2name(n, fullname, helptail)
2467 int n;
2468 int fullname;
2469 int helptail; /* for help buffers return tail only */
2470{
2471 buf_T *buf;
2472
2473 buf = buflist_findnr(n);
2474 if (buf == NULL)
2475 return NULL;
2476 return home_replace_save(helptail ? buf : NULL,
2477 fullname ? buf->b_ffname : buf->b_fname);
2478}
2479
2480/*
2481 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2482 * When "copy_options" is TRUE save the local window option values.
2483 * When "lnum" is 0 only do the options.
2484 */
2485 static void
2486buflist_setfpos(buf, win, lnum, col, copy_options)
2487 buf_T *buf;
2488 win_T *win;
2489 linenr_T lnum;
2490 colnr_T col;
2491 int copy_options;
2492{
2493 wininfo_T *wip;
2494
2495 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2496 if (wip->wi_win == win)
2497 break;
2498 if (wip == NULL)
2499 {
2500 /* allocate a new entry */
2501 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2502 if (wip == NULL)
2503 return;
2504 wip->wi_win = win;
2505 if (lnum == 0) /* set lnum even when it's 0 */
2506 lnum = 1;
2507 }
2508 else
2509 {
2510 /* remove the entry from the list */
2511 if (wip->wi_prev)
2512 wip->wi_prev->wi_next = wip->wi_next;
2513 else
2514 buf->b_wininfo = wip->wi_next;
2515 if (wip->wi_next)
2516 wip->wi_next->wi_prev = wip->wi_prev;
2517 if (copy_options && wip->wi_optset)
2518 {
2519 clear_winopt(&wip->wi_opt);
2520#ifdef FEAT_FOLDING
2521 deleteFoldRecurse(&wip->wi_folds);
2522#endif
2523 }
2524 }
2525 if (lnum != 0)
2526 {
2527 wip->wi_fpos.lnum = lnum;
2528 wip->wi_fpos.col = col;
2529 }
2530 if (copy_options)
2531 {
2532 /* Save the window-specific option values. */
2533 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2534#ifdef FEAT_FOLDING
2535 wip->wi_fold_manual = win->w_fold_manual;
2536 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2537#endif
2538 wip->wi_optset = TRUE;
2539 }
2540
2541 /* insert the entry in front of the list */
2542 wip->wi_next = buf->b_wininfo;
2543 buf->b_wininfo = wip;
2544 wip->wi_prev = NULL;
2545 if (wip->wi_next)
2546 wip->wi_next->wi_prev = wip;
2547
2548 return;
2549}
2550
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002551#ifdef FEAT_DIFF
2552static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2553
2554/*
2555 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2556 * page. That's because a diff is local to a tab page.
2557 */
2558 static int
2559wininfo_other_tab_diff(wip)
2560 wininfo_T *wip;
2561{
2562 win_T *wp;
2563
2564 if (wip->wi_opt.wo_diff)
2565 {
2566 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2567 /* return FALSE when it's a window in the current tab page, thus
2568 * the buffer was in diff mode here */
2569 if (wip->wi_win == wp)
2570 return FALSE;
2571 return TRUE;
2572 }
2573 return FALSE;
2574}
2575#endif
2576
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577/*
2578 * Find info for the current window in buffer "buf".
2579 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002580 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2581 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 * Returns NULL when there isn't any info.
2583 */
2584 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002585find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002587 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589 wininfo_T *wip;
2590
2591 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002592 if (wip->wi_win == curwin
2593#ifdef FEAT_DIFF
2594 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2595#endif
2596 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002598
2599 /* If no wininfo for curwin, use the first in the list (that doesn't have
2600 * 'diff' set and is in another tab page). */
2601 if (wip == NULL)
2602 {
2603#ifdef FEAT_DIFF
2604 if (skip_diff_buffer)
2605 {
2606 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2607 if (!wininfo_other_tab_diff(wip))
2608 break;
2609 }
2610 else
2611#endif
2612 wip = buf->b_wininfo;
2613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 return wip;
2615}
2616
2617/*
2618 * Reset the local window options to the values last used in this window.
2619 * If the buffer wasn't used in this window before, use the values from
2620 * the most recently used window. If the values were never set, use the
2621 * global values for the window.
2622 */
2623 void
2624get_winopts(buf)
2625 buf_T *buf;
2626{
2627 wininfo_T *wip;
2628
2629 clear_winopt(&curwin->w_onebuf_opt);
2630#ifdef FEAT_FOLDING
2631 clearFolding(curwin);
2632#endif
2633
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002634 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (wip != NULL && wip->wi_optset)
2636 {
2637 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2638#ifdef FEAT_FOLDING
2639 curwin->w_fold_manual = wip->wi_fold_manual;
2640 curwin->w_foldinvalid = TRUE;
2641 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2642#endif
2643 }
2644 else
2645 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2646
2647#ifdef FEAT_FOLDING
2648 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2649 if (p_fdls >= 0)
2650 curwin->w_p_fdl = p_fdls;
2651#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002652#ifdef FEAT_SYN_HL
2653 check_colorcolumn(curwin);
2654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655}
2656
2657/*
2658 * Find the position (lnum and col) for the buffer 'buf' for the current
2659 * window.
2660 * Returns a pointer to no_position if no position is found.
2661 */
2662 pos_T *
2663buflist_findfpos(buf)
2664 buf_T *buf;
2665{
2666 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002667 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002669 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 if (wip != NULL)
2671 return &(wip->wi_fpos);
2672 else
2673 return &no_position;
2674}
2675
2676/*
2677 * Find the lnum for the buffer 'buf' for the current window.
2678 */
2679 linenr_T
2680buflist_findlnum(buf)
2681 buf_T *buf;
2682{
2683 return buflist_findfpos(buf)->lnum;
2684}
2685
2686#if defined(FEAT_LISTCMDS) || defined(PROTO)
2687/*
2688 * List all know file names (for :files and :buffers command).
2689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002690 void
2691buflist_list(eap)
2692 exarg_T *eap;
2693{
2694 buf_T *buf;
2695 int len;
2696 int i;
2697
2698 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2699 {
2700 /* skip unlisted buffers, unless ! was used */
2701 if (!buf->b_p_bl && !eap->forceit)
2702 continue;
2703 msg_putchar('\n');
2704 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002705 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706 else
2707 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2708
Bram Moolenaar50cde822005-06-05 21:54:54 +00002709 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 buf->b_fnum,
2711 buf->b_p_bl ? ' ' : 'u',
2712 buf == curbuf ? '%' :
2713 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2714 buf->b_ml.ml_mfp == NULL ? ' ' :
2715 (buf->b_nwindows == 0 ? 'h' : 'a'),
2716 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2717 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002718 : (bufIsChanged(buf) ? '+' : ' '),
2719 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720
2721 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 i = 40 - vim_strsize(IObuff);
2723 do
2724 {
2725 IObuff[len++] = ' ';
2726 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002727 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2728 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002729 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 msg_outtrans(IObuff);
2731 out_flush(); /* output one line at a time */
2732 ui_breakcheck();
2733 }
2734}
2735#endif
2736
2737/*
2738 * Get file name and line number for file 'fnum'.
2739 * Used by DoOneCmd() for translating '%' and '#'.
2740 * Used by insert_reg() and cmdline_paste() for '#' register.
2741 * Return FAIL if not found, OK for success.
2742 */
2743 int
2744buflist_name_nr(fnum, fname, lnum)
2745 int fnum;
2746 char_u **fname;
2747 linenr_T *lnum;
2748{
2749 buf_T *buf;
2750
2751 buf = buflist_findnr(fnum);
2752 if (buf == NULL || buf->b_fname == NULL)
2753 return FAIL;
2754
2755 *fname = buf->b_fname;
2756 *lnum = buflist_findlnum(buf);
2757
2758 return OK;
2759}
2760
2761/*
2762 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2763 * The file name with the full path is also remembered, for when :cd is used.
2764 * Returns FAIL for failure (file name already in use by other buffer)
2765 * OK otherwise.
2766 */
2767 int
2768setfname(buf, ffname, sfname, message)
2769 buf_T *buf;
2770 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002771 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002772{
Bram Moolenaar81695252004-12-29 20:58:21 +00002773 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774#ifdef UNIX
2775 struct stat st;
2776#endif
2777
2778 if (ffname == NULL || *ffname == NUL)
2779 {
2780 /* Removing the name. */
2781 vim_free(buf->b_ffname);
2782 vim_free(buf->b_sfname);
2783 buf->b_ffname = NULL;
2784 buf->b_sfname = NULL;
2785#ifdef UNIX
2786 st.st_dev = (dev_T)-1;
2787#endif
2788 }
2789 else
2790 {
2791 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2792 if (ffname == NULL) /* out of memory */
2793 return FAIL;
2794
2795 /*
2796 * if the file name is already used in another buffer:
2797 * - if the buffer is loaded, fail
2798 * - if the buffer is not loaded, delete it from the list
2799 */
2800#ifdef UNIX
2801 if (mch_stat((char *)ffname, &st) < 0)
2802 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002803#endif
2804 if (!(buf->b_flags & BF_DUMMY))
2805#ifdef UNIX
2806 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002808 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002809#endif
2810 if (obuf != NULL && obuf != buf)
2811 {
2812 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2813 {
2814 if (message)
2815 EMSG(_("E95: Buffer with this name already exists"));
2816 vim_free(ffname);
2817 return FAIL;
2818 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002819 /* delete from the list */
2820 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002821 }
2822 sfname = vim_strsave(sfname);
2823 if (ffname == NULL || sfname == NULL)
2824 {
2825 vim_free(sfname);
2826 vim_free(ffname);
2827 return FAIL;
2828 }
2829#ifdef USE_FNAME_CASE
2830# ifdef USE_LONG_FNAME
2831 if (USE_LONG_FNAME)
2832# endif
2833 fname_case(sfname, 0); /* set correct case for short file name */
2834#endif
2835 vim_free(buf->b_ffname);
2836 vim_free(buf->b_sfname);
2837 buf->b_ffname = ffname;
2838 buf->b_sfname = sfname;
2839 }
2840 buf->b_fname = buf->b_sfname;
2841#ifdef UNIX
2842 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002843 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002844 else
2845 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002846 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002847 buf->b_dev = st.st_dev;
2848 buf->b_ino = st.st_ino;
2849 }
2850#endif
2851
2852#ifndef SHORT_FNAME
2853 buf->b_shortname = FALSE;
2854#endif
2855
2856 buf_name_changed(buf);
2857 return OK;
2858}
2859
2860/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002861 * Crude way of changing the name of a buffer. Use with care!
2862 * The name should be relative to the current directory.
2863 */
2864 void
2865buf_set_name(fnum, name)
2866 int fnum;
2867 char_u *name;
2868{
2869 buf_T *buf;
2870
2871 buf = buflist_findnr(fnum);
2872 if (buf != NULL)
2873 {
2874 vim_free(buf->b_sfname);
2875 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002876 buf->b_ffname = vim_strsave(name);
2877 buf->b_sfname = NULL;
2878 /* Allocate ffname and expand into full path. Also resolves .lnk
2879 * files on Win32. */
2880 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002881 buf->b_fname = buf->b_sfname;
2882 }
2883}
2884
2885/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886 * Take care of what needs to be done when the name of buffer "buf" has
2887 * changed.
2888 */
2889 void
2890buf_name_changed(buf)
2891 buf_T *buf;
2892{
2893 /*
2894 * If the file name changed, also change the name of the swapfile
2895 */
2896 if (buf->b_ml.ml_mfp != NULL)
2897 ml_setname(buf);
2898
2899 if (curwin->w_buffer == buf)
2900 check_arg_idx(curwin); /* check file name for arg list */
2901#ifdef FEAT_TITLE
2902 maketitle(); /* set window title */
2903#endif
2904#ifdef FEAT_WINDOWS
2905 status_redraw_all(); /* status lines need to be redrawn */
2906#endif
2907 fmarks_check_names(buf); /* check named file marks */
2908 ml_timestamp(buf); /* reset timestamp */
2909}
2910
2911/*
2912 * set alternate file name for current window
2913 *
2914 * Used by do_one_cmd(), do_write() and do_ecmd().
2915 * Return the buffer.
2916 */
2917 buf_T *
2918setaltfname(ffname, sfname, lnum)
2919 char_u *ffname;
2920 char_u *sfname;
2921 linenr_T lnum;
2922{
2923 buf_T *buf;
2924
2925 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2926 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002927 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 curwin->w_alt_fnum = buf->b_fnum;
2929 return buf;
2930}
2931
2932/*
2933 * Get alternate file name for current window.
2934 * Return NULL if there isn't any, and give error message if requested.
2935 */
2936 char_u *
2937getaltfname(errmsg)
2938 int errmsg; /* give error message */
2939{
2940 char_u *fname;
2941 linenr_T dummy;
2942
2943 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2944 {
2945 if (errmsg)
2946 EMSG(_(e_noalt));
2947 return NULL;
2948 }
2949 return fname;
2950}
2951
2952/*
2953 * Add a file name to the buflist and return its number.
2954 * Uses same flags as buflist_new(), except BLN_DUMMY.
2955 *
2956 * used by qf_init(), main() and doarglist()
2957 */
2958 int
2959buflist_add(fname, flags)
2960 char_u *fname;
2961 int flags;
2962{
2963 buf_T *buf;
2964
2965 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2966 if (buf != NULL)
2967 return buf->b_fnum;
2968 return 0;
2969}
2970
2971#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2972/*
2973 * Adjust slashes in file names. Called after 'shellslash' was set.
2974 */
2975 void
2976buflist_slash_adjust()
2977{
2978 buf_T *bp;
2979
2980 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2981 {
2982 if (bp->b_ffname != NULL)
2983 slash_adjust(bp->b_ffname);
2984 if (bp->b_sfname != NULL)
2985 slash_adjust(bp->b_sfname);
2986 }
2987}
2988#endif
2989
2990/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002991 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 * Also save the local window option values.
2993 */
2994 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002995buflist_altfpos(win)
2996 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002998 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999}
3000
3001/*
3002 * Return TRUE if 'ffname' is not the same file as current file.
3003 * Fname must have a full path (expanded by mch_FullName()).
3004 */
3005 int
3006otherfile(ffname)
3007 char_u *ffname;
3008{
3009 return otherfile_buf(curbuf, ffname
3010#ifdef UNIX
3011 , NULL
3012#endif
3013 );
3014}
3015
3016 static int
3017otherfile_buf(buf, ffname
3018#ifdef UNIX
3019 , stp
3020#endif
3021 )
3022 buf_T *buf;
3023 char_u *ffname;
3024#ifdef UNIX
3025 struct stat *stp;
3026#endif
3027{
3028 /* no name is different */
3029 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
3030 return TRUE;
3031 if (fnamecmp(ffname, buf->b_ffname) == 0)
3032 return FALSE;
3033#ifdef UNIX
3034 {
3035 struct stat st;
3036
3037 /* If no struct stat given, get it now */
3038 if (stp == NULL)
3039 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003040 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 st.st_dev = (dev_T)-1;
3042 stp = &st;
3043 }
3044 /* Use dev/ino to check if the files are the same, even when the names
3045 * are different (possible with links). Still need to compare the
3046 * name above, for when the file doesn't exist yet.
3047 * Problem: The dev/ino changes when a file is deleted (and created
3048 * again) and remains the same when renamed/moved. We don't want to
3049 * mch_stat() each buffer each time, that would be too slow. Get the
3050 * dev/ino again when they appear to match, but not when they appear
3051 * to be different: Could skip a buffer when it's actually the same
3052 * file. */
3053 if (buf_same_ino(buf, stp))
3054 {
3055 buf_setino(buf);
3056 if (buf_same_ino(buf, stp))
3057 return FALSE;
3058 }
3059 }
3060#endif
3061 return TRUE;
3062}
3063
3064#if defined(UNIX) || defined(PROTO)
3065/*
3066 * Set inode and device number for a buffer.
3067 * Must always be called when b_fname is changed!.
3068 */
3069 void
3070buf_setino(buf)
3071 buf_T *buf;
3072{
3073 struct stat st;
3074
3075 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3076 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003077 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 buf->b_dev = st.st_dev;
3079 buf->b_ino = st.st_ino;
3080 }
3081 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003082 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083}
3084
3085/*
3086 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3087 */
3088 static int
3089buf_same_ino(buf, stp)
3090 buf_T *buf;
3091 struct stat *stp;
3092{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003093 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 && stp->st_dev == buf->b_dev
3095 && stp->st_ino == buf->b_ino);
3096}
3097#endif
3098
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003099/*
3100 * Print info about the current buffer.
3101 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 void
3103fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003104 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 int shorthelp;
3106 int dont_truncate;
3107{
3108 char_u *name;
3109 int n;
3110 char_u *p;
3111 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003112 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113
3114 buffer = alloc(IOSIZE);
3115 if (buffer == NULL)
3116 return;
3117
3118 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3119 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003120 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 p = buffer + STRLEN(buffer);
3122 }
3123 else
3124 p = buffer;
3125
3126 *p++ = '"';
3127 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003128 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129 else
3130 {
3131 if (!fullname && curbuf->b_fname != NULL)
3132 name = curbuf->b_fname;
3133 else
3134 name = curbuf->b_ffname;
3135 home_replace(shorthelp ? curbuf : NULL, name, p,
3136 (int)(IOSIZE - (p - buffer)), TRUE);
3137 }
3138
Bram Moolenaara800b422010-06-27 01:15:55 +02003139 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 curbufIsChanged() ? (shortmess(SHM_MOD)
3141 ? " [+]" : _(" [Modified]")) : " ",
3142 (curbuf->b_flags & BF_NOTEDITED)
3143#ifdef FEAT_QUICKFIX
3144 && !bt_dontwrite(curbuf)
3145#endif
3146 ? _("[Not edited]") : "",
3147 (curbuf->b_flags & BF_NEW)
3148#ifdef FEAT_QUICKFIX
3149 && !bt_dontwrite(curbuf)
3150#endif
3151 ? _("[New file]") : "",
3152 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
Bram Moolenaar23584032013-06-07 20:17:11 +02003153 curbuf->b_p_ro ? (shortmess(SHM_RO) ? _("[RO]")
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 : _("[readonly]")) : "",
3155 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3156 || curbuf->b_p_ro) ?
3157 " " : "");
3158 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3159 * causes an overflow, thus for large numbers divide instead. */
3160 if (curwin->w_cursor.lnum > 1000000L)
3161 n = (int)(((long)curwin->w_cursor.lnum) /
3162 ((long)curbuf->b_ml.ml_line_count / 100L));
3163 else
3164 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3165 (long)curbuf->b_ml.ml_line_count);
3166 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3167 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003168 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
3170#ifdef FEAT_CMDL_INFO
3171 else if (p_ru)
3172 {
3173 /* Current line and column are already on the screen -- webb */
3174 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003175 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003177 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 (long)curbuf->b_ml.ml_line_count, n);
3179 }
3180#endif
3181 else
3182 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003183 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 _("line %ld of %ld --%d%%-- col "),
3185 (long)curwin->w_cursor.lnum,
3186 (long)curbuf->b_ml.ml_line_count,
3187 n);
3188 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003189 len = STRLEN(buffer);
3190 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3192 }
3193
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003194 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195
3196 if (dont_truncate)
3197 {
3198 /* Temporarily set msg_scroll to avoid the message being truncated.
3199 * First call msg_start() to get the message in the right place. */
3200 msg_start();
3201 n = msg_scroll;
3202 msg_scroll = TRUE;
3203 msg(buffer);
3204 msg_scroll = n;
3205 }
3206 else
3207 {
3208 p = msg_trunc_attr(buffer, FALSE, 0);
3209 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 /* Need to repeat the message after redrawing when:
3211 * - When restart_edit is set (otherwise there will be a delay
3212 * before redrawing).
3213 * - When the screen was scrolled but there is no wait-return
3214 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003215 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 }
3217
3218 vim_free(buffer);
3219}
3220
3221 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003222col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003224 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 int col;
3226 int vcol;
3227{
3228 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003229 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003231 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232}
3233
3234#if defined(FEAT_TITLE) || defined(PROTO)
3235/*
3236 * put file name in title bar of window and in icon title
3237 */
3238
3239static char_u *lasttitle = NULL;
3240static char_u *lasticon = NULL;
3241
3242 void
3243maketitle()
3244{
3245 char_u *p;
3246 char_u *t_str = NULL;
3247 char_u *i_name;
3248 char_u *i_str = NULL;
3249 int maxlen = 0;
3250 int len;
3251 int mustset;
3252 char_u buf[IOSIZE];
3253 int off;
3254
3255 if (!redrawing())
3256 {
3257 /* Postpone updating the title when 'lazyredraw' is set. */
3258 need_maketitle = TRUE;
3259 return;
3260 }
3261
3262 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003263 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264 return;
3265
3266 if (p_title)
3267 {
3268 if (p_titlelen > 0)
3269 {
3270 maxlen = p_titlelen * Columns / 100;
3271 if (maxlen < 10)
3272 maxlen = 10;
3273 }
3274
3275 t_str = buf;
3276 if (*p_titlestring != NUL)
3277 {
3278#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003279 if (stl_syntax & STL_IN_TITLE)
3280 {
3281 int use_sandbox = FALSE;
3282 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003283
3284# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003285 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003286# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003287 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003289 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003290 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003291 if (called_emsg)
3292 set_string_option_direct((char_u *)"titlestring", -1,
3293 (char_u *)"", OPT_FREE, SID_ERROR);
3294 called_emsg |= save_called_emsg;
3295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 else
3297#endif
3298 t_str = p_titlestring;
3299 }
3300 else
3301 {
3302 /* format: "fname + (path) (1 of 2) - VIM" */
3303
Bram Moolenaar2c666692012-09-05 13:30:40 +02003304#define SPACE_FOR_FNAME (IOSIZE - 100)
3305#define SPACE_FOR_DIR (IOSIZE - 20)
3306#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003308 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003309 else
3310 {
3311 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003312 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 }
3315
3316 switch (bufIsChanged(curbuf)
3317 + (curbuf->b_p_ro * 2)
3318 + (!curbuf->b_p_ma * 4))
3319 {
3320 case 1: STRCAT(buf, " +"); break;
3321 case 2: STRCAT(buf, " ="); break;
3322 case 3: STRCAT(buf, " =+"); break;
3323 case 4:
3324 case 6: STRCAT(buf, " -"); break;
3325 case 5:
3326 case 7: STRCAT(buf, " -+"); break;
3327 }
3328
3329 if (curbuf->b_fname != NULL)
3330 {
3331 /* Get path of file, replace home dir with ~ */
3332 off = (int)STRLEN(buf);
3333 buf[off++] = ' ';
3334 buf[off++] = '(';
3335 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003336 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337#ifdef BACKSLASH_IN_FILENAME
3338 /* avoid "c:/name" to be reduced to "c" */
3339 if (isalpha(buf[off]) && buf[off + 1] == ':')
3340 off += 2;
3341#endif
3342 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003343 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003344 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003346 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003347 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003350
Bram Moolenaar2c666692012-09-05 13:30:40 +02003351 /* Translate unprintable chars and concatenate. Keep some
3352 * room for the server name. When there is no room (very long
3353 * file name) use (...). */
3354 if (off < SPACE_FOR_DIR)
3355 {
3356 p = transstr(buf + off);
3357 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3358 vim_free(p);
3359 }
3360 else
3361 {
3362 vim_strncpy(buf + off, (char_u *)"...",
3363 (size_t)(SPACE_FOR_ARGNR - off));
3364 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 STRCAT(buf, ")");
3366 }
3367
Bram Moolenaar2c666692012-09-05 13:30:40 +02003368 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369
3370#if defined(FEAT_CLIENTSERVER)
3371 if (serverName != NULL)
3372 {
3373 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003374 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 }
3376 else
3377#endif
3378 STRCAT(buf, " - VIM");
3379
3380 if (maxlen > 0)
3381 {
3382 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003383 if (vim_strsize(buf) > maxlen)
3384 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 }
3386 }
3387 }
3388 mustset = ti_change(t_str, &lasttitle);
3389
3390 if (p_icon)
3391 {
3392 i_str = buf;
3393 if (*p_iconstring != NUL)
3394 {
3395#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003396 if (stl_syntax & STL_IN_ICON)
3397 {
3398 int use_sandbox = FALSE;
3399 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003400
3401# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003402 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003403# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003404 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003406 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003407 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003408 if (called_emsg)
3409 set_string_option_direct((char_u *)"iconstring", -1,
3410 (char_u *)"", OPT_FREE, SID_ERROR);
3411 called_emsg |= save_called_emsg;
3412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 else
3414#endif
3415 i_str = p_iconstring;
3416 }
3417 else
3418 {
3419 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003420 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 else /* use file name only in icon */
3422 i_name = gettail(curbuf->b_ffname);
3423 *i_str = NUL;
3424 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003425 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 if (len > 100)
3427 {
3428 len -= 100;
3429#ifdef FEAT_MBYTE
3430 if (has_mbyte)
3431 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3432#endif
3433 i_name += len;
3434 }
3435 STRCPY(i_str, i_name);
3436 trans_characters(i_str, IOSIZE);
3437 }
3438 }
3439
3440 mustset |= ti_change(i_str, &lasticon);
3441
3442 if (mustset)
3443 resettitle();
3444}
3445
3446/*
3447 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3448 * from "str" if it does.
3449 * Return TRUE when "*last" changed.
3450 */
3451 static int
3452ti_change(str, last)
3453 char_u *str;
3454 char_u **last;
3455{
3456 if ((str == NULL) != (*last == NULL)
3457 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3458 {
3459 vim_free(*last);
3460 if (str == NULL)
3461 *last = NULL;
3462 else
3463 *last = vim_strsave(str);
3464 return TRUE;
3465 }
3466 return FALSE;
3467}
3468
3469/*
3470 * Put current window title back (used after calling a shell)
3471 */
3472 void
3473resettitle()
3474{
3475 mch_settitle(lasttitle, lasticon);
3476}
Bram Moolenaarea408852005-06-25 22:49:46 +00003477
3478# if defined(EXITFREE) || defined(PROTO)
3479 void
3480free_titles()
3481{
3482 vim_free(lasttitle);
3483 vim_free(lasticon);
3484}
3485# endif
3486
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#endif /* FEAT_TITLE */
3488
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003489#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003491 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 * Return length of string in screen cells.
3493 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003494 * Normally works for window "wp", except when working for 'tabline' then it
3495 * is "curwin".
3496 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 * Items are drawn interspersed with the text that surrounds it
3498 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3499 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3500 *
3501 * If maxwidth is not zero, the string will be filled at any middle marker
3502 * or truncated if too long, fillchar is used for all whitespace.
3503 */
3504 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003505build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3506 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003508 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 size_t outlen; /* length of out[] */
3510 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003511 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 int fillchar;
3513 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003514 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3515 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516{
3517 char_u *p;
3518 char_u *s;
3519 char_u *t;
Bram Moolenaar567199b2013-04-24 16:52:36 +02003520 int byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521#ifdef FEAT_EVAL
3522 win_T *o_curwin;
3523 buf_T *o_curbuf;
3524#endif
3525 int empty_line;
3526 colnr_T virtcol;
3527 long l;
3528 long n;
3529 int prevchar_isflag;
3530 int prevchar_isitem;
3531 int itemisflag;
3532 int fillable;
3533 char_u *str;
3534 long num;
3535 int width;
3536 int itemcnt;
3537 int curitem;
3538 int groupitem[STL_MAX_ITEM];
3539 int groupdepth;
3540 struct stl_item
3541 {
3542 char_u *start;
3543 int minwid;
3544 int maxwid;
3545 enum
3546 {
3547 Normal,
3548 Empty,
3549 Group,
3550 Middle,
3551 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003552 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 Trunc
3554 } type;
3555 } item[STL_MAX_ITEM];
3556 int minwid;
3557 int maxwid;
3558 int zeropad;
3559 char_u base;
3560 char_u opt;
3561#define TMPLEN 70
3562 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003563 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003564 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003565
3566#ifdef FEAT_EVAL
3567 /*
3568 * When the format starts with "%!" then evaluate it as an expression and
3569 * use the result as the actual format string.
3570 */
3571 if (fmt[0] == '%' && fmt[1] == '!')
3572 {
3573 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3574 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003575 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003576 }
3577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578
3579 if (fillchar == 0)
3580 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003581#ifdef FEAT_MBYTE
3582 /* Can't handle a multi-byte fill character yet. */
3583 else if (mb_char2len(fillchar) > 1)
3584 fillchar = '-';
3585#endif
3586
Bram Moolenaar567199b2013-04-24 16:52:36 +02003587 /* Get line & check if empty (cursorpos will show "0-1"). Note that
3588 * p will become invalid when getting another buffer line. */
3589 p = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3590 empty_line = (*p == NUL);
3591
3592 /* Get the byte value now, in case we need it below. This is more
3593 * efficient than making a copy of the line. */
3594 if (wp->w_cursor.col > (colnr_T)STRLEN(p))
3595 byteval = 0;
3596 else
3597#ifdef FEAT_MBYTE
3598 byteval = (*mb_ptr2char)(p + wp->w_cursor.col);
3599#else
3600 byteval = p[wp->w_cursor.col];
3601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 groupdepth = 0;
3604 p = out;
3605 curitem = 0;
3606 prevchar_isflag = TRUE;
3607 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003608 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003610 if (curitem == STL_MAX_ITEM)
3611 {
3612 /* There are too many items. Add the error code to the statusline
3613 * to give the user a hint about what went wrong. */
3614 if (p + 6 < out + outlen)
3615 {
3616 mch_memmove(p, " E541", (size_t)5);
3617 p += 5;
3618 }
3619 break;
3620 }
3621
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 if (*s != NUL && *s != '%')
3623 prevchar_isflag = prevchar_isitem = FALSE;
3624
3625 /*
3626 * Handle up to the next '%' or the end.
3627 */
3628 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3629 *p++ = *s++;
3630 if (*s == NUL || p + 1 >= out + outlen)
3631 break;
3632
3633 /*
3634 * Handle one '%' item.
3635 */
3636 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003637 if (*s == NUL) /* ignore trailing % */
3638 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 if (*s == '%')
3640 {
3641 if (p + 1 >= out + outlen)
3642 break;
3643 *p++ = *s++;
3644 prevchar_isflag = prevchar_isitem = FALSE;
3645 continue;
3646 }
3647 if (*s == STL_MIDDLEMARK)
3648 {
3649 s++;
3650 if (groupdepth > 0)
3651 continue;
3652 item[curitem].type = Middle;
3653 item[curitem++].start = p;
3654 continue;
3655 }
3656 if (*s == STL_TRUNCMARK)
3657 {
3658 s++;
3659 item[curitem].type = Trunc;
3660 item[curitem++].start = p;
3661 continue;
3662 }
3663 if (*s == ')')
3664 {
3665 s++;
3666 if (groupdepth < 1)
3667 continue;
3668 groupdepth--;
3669
3670 t = item[groupitem[groupdepth]].start;
3671 *p = NUL;
3672 l = vim_strsize(t);
3673 if (curitem > groupitem[groupdepth] + 1
3674 && item[groupitem[groupdepth]].minwid == 0)
3675 {
3676 /* remove group if all items are empty */
3677 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3678 if (item[n].type == Normal)
3679 break;
3680 if (n == curitem)
3681 {
3682 p = t;
3683 l = 0;
3684 }
3685 }
3686 if (l > item[groupitem[groupdepth]].maxwid)
3687 {
3688 /* truncate, remove n bytes of text at the start */
3689#ifdef FEAT_MBYTE
3690 if (has_mbyte)
3691 {
3692 /* Find the first character that should be included. */
3693 n = 0;
3694 while (l >= item[groupitem[groupdepth]].maxwid)
3695 {
3696 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003697 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 }
3700 else
3701#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003702 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703
3704 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003705 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 p = p - n + 1;
3707#ifdef FEAT_MBYTE
3708 /* Fill up space left over by half a double-wide char. */
3709 while (++l < item[groupitem[groupdepth]].minwid)
3710 *p++ = fillchar;
3711#endif
3712
3713 /* correct the start of the items for the truncation */
3714 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3715 {
3716 item[l].start -= n;
3717 if (item[l].start < t)
3718 item[l].start = t;
3719 }
3720 }
3721 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3722 {
3723 /* fill */
3724 n = item[groupitem[groupdepth]].minwid;
3725 if (n < 0)
3726 {
3727 /* fill by appending characters */
3728 n = 0 - n;
3729 while (l++ < n && p + 1 < out + outlen)
3730 *p++ = fillchar;
3731 }
3732 else
3733 {
3734 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003735 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 l = n - l;
3737 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003738 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 p += l;
3740 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3741 item[n].start += l;
3742 for ( ; l > 0; l--)
3743 *t++ = fillchar;
3744 }
3745 }
3746 continue;
3747 }
3748 minwid = 0;
3749 maxwid = 9999;
3750 zeropad = FALSE;
3751 l = 1;
3752 if (*s == '0')
3753 {
3754 s++;
3755 zeropad = TRUE;
3756 }
3757 if (*s == '-')
3758 {
3759 s++;
3760 l = -1;
3761 }
3762 if (VIM_ISDIGIT(*s))
3763 {
3764 minwid = (int)getdigits(&s);
3765 if (minwid < 0) /* overflow */
3766 minwid = 0;
3767 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003768 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
3770 item[curitem].type = Highlight;
3771 item[curitem].start = p;
3772 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3773 s++;
3774 curitem++;
3775 continue;
3776 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003777 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3778 {
3779 if (*s == STL_TABCLOSENR)
3780 {
3781 if (minwid == 0)
3782 {
3783 /* %X ends the close label, go back to the previously
3784 * define tab label nr. */
3785 for (n = curitem - 1; n >= 0; --n)
3786 if (item[n].type == TabPage && item[n].minwid >= 0)
3787 {
3788 minwid = item[n].minwid;
3789 break;
3790 }
3791 }
3792 else
3793 /* close nrs are stored as negative values */
3794 minwid = - minwid;
3795 }
3796 item[curitem].type = TabPage;
3797 item[curitem].start = p;
3798 item[curitem].minwid = minwid;
3799 s++;
3800 curitem++;
3801 continue;
3802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 if (*s == '.')
3804 {
3805 s++;
3806 if (VIM_ISDIGIT(*s))
3807 {
3808 maxwid = (int)getdigits(&s);
3809 if (maxwid <= 0) /* overflow */
3810 maxwid = 50;
3811 }
3812 }
3813 minwid = (minwid > 50 ? 50 : minwid) * l;
3814 if (*s == '(')
3815 {
3816 groupitem[groupdepth++] = curitem;
3817 item[curitem].type = Group;
3818 item[curitem].start = p;
3819 item[curitem].minwid = minwid;
3820 item[curitem].maxwid = maxwid;
3821 s++;
3822 curitem++;
3823 continue;
3824 }
3825 if (vim_strchr(STL_ALL, *s) == NULL)
3826 {
3827 s++;
3828 continue;
3829 }
3830 opt = *s++;
3831
3832 /* OK - now for the real work */
3833 base = 'D';
3834 itemisflag = FALSE;
3835 fillable = TRUE;
3836 num = -1;
3837 str = NULL;
3838 switch (opt)
3839 {
3840 case STL_FILEPATH:
3841 case STL_FULLPATH:
3842 case STL_FILENAME:
3843 fillable = FALSE; /* don't change ' ' to fillchar */
3844 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003845 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003846 else
3847 {
3848 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003849 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3851 }
3852 trans_characters(NameBuff, MAXPATHL);
3853 if (opt != STL_FILENAME)
3854 str = NameBuff;
3855 else
3856 str = gettail(NameBuff);
3857 break;
3858
3859 case STL_VIM_EXPR: /* '{' */
3860 itemisflag = TRUE;
3861 t = p;
3862 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3863 *p++ = *s++;
3864 if (*s != '}') /* missing '}' or out of space */
3865 break;
3866 s++;
3867 *p = 0;
3868 p = t;
3869
3870#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003871 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3873
3874 o_curbuf = curbuf;
3875 o_curwin = curwin;
3876 curwin = wp;
3877 curbuf = wp->w_buffer;
3878
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003879 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880
3881 curwin = o_curwin;
3882 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003883 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
3885 if (str != NULL && *str != 0)
3886 {
3887 if (*skipdigits(str) == NUL)
3888 {
3889 num = atoi((char *)str);
3890 vim_free(str);
3891 str = NULL;
3892 itemisflag = FALSE;
3893 }
3894 }
3895#endif
3896 break;
3897
3898 case STL_LINE:
3899 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3900 ? 0L : (long)(wp->w_cursor.lnum);
3901 break;
3902
3903 case STL_NUMLINES:
3904 num = wp->w_buffer->b_ml.ml_line_count;
3905 break;
3906
3907 case STL_COLUMN:
3908 num = !(State & INSERT) && empty_line
3909 ? 0 : (int)wp->w_cursor.col + 1;
3910 break;
3911
3912 case STL_VIRTCOL:
3913 case STL_VIRTCOL_ALT:
3914 /* In list mode virtcol needs to be recomputed */
3915 virtcol = wp->w_virtcol;
3916 if (wp->w_p_list && lcs_tab1 == NUL)
3917 {
3918 wp->w_p_list = FALSE;
3919 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3920 wp->w_p_list = TRUE;
3921 }
3922 ++virtcol;
3923 /* Don't display %V if it's the same as %c. */
3924 if (opt == STL_VIRTCOL_ALT
3925 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3926 ? 0 : (int)wp->w_cursor.col + 1)))
3927 break;
3928 num = (long)virtcol;
3929 break;
3930
3931 case STL_PERCENTAGE:
3932 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3933 (long)wp->w_buffer->b_ml.ml_line_count);
3934 break;
3935
3936 case STL_ALTPERCENT:
3937 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003938 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003939 break;
3940
3941 case STL_ARGLISTSTAT:
3942 fillable = FALSE;
3943 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003944 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 str = tmp;
3946 break;
3947
3948 case STL_KEYMAP:
3949 fillable = FALSE;
3950 if (get_keymap_str(wp, tmp, TMPLEN))
3951 str = tmp;
3952 break;
3953 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003954#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003955 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956#else
3957 num = 0;
3958#endif
3959 break;
3960
3961 case STL_BUFNO:
3962 num = wp->w_buffer->b_fnum;
3963 break;
3964
3965 case STL_OFFSET_X:
3966 base = 'X';
3967 case STL_OFFSET:
3968#ifdef FEAT_BYTEOFF
3969 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3970 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3971 0L : l + 1 + (!(State & INSERT) && empty_line ?
3972 0 : (int)wp->w_cursor.col);
3973#endif
3974 break;
3975
3976 case STL_BYTEVAL_X:
3977 base = 'X';
3978 case STL_BYTEVAL:
Bram Moolenaar567199b2013-04-24 16:52:36 +02003979 num = byteval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 if (num == NL)
3981 num = 0;
3982 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3983 num = NL;
3984 break;
3985
3986 case STL_ROFLAG:
3987 case STL_ROFLAG_ALT:
3988 itemisflag = TRUE;
3989 if (wp->w_buffer->b_p_ro)
Bram Moolenaar23584032013-06-07 20:17:11 +02003990 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : _("[RO]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 break;
3992
3993 case STL_HELPFLAG:
3994 case STL_HELPFLAG_ALT:
3995 itemisflag = TRUE;
3996 if (wp->w_buffer->b_help)
3997 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003998 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 break;
4000
4001#ifdef FEAT_AUTOCMD
4002 case STL_FILETYPE:
4003 if (*wp->w_buffer->b_p_ft != NUL
4004 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
4005 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004006 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
4007 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004008 str = tmp;
4009 }
4010 break;
4011
4012 case STL_FILETYPE_ALT:
4013 itemisflag = TRUE;
4014 if (*wp->w_buffer->b_p_ft != NUL
4015 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
4016 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004017 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
4018 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 for (t = tmp; *t != 0; t++)
4020 *t = TOUPPER_LOC(*t);
4021 str = tmp;
4022 }
4023 break;
4024#endif
4025
4026#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4027 case STL_PREVIEWFLAG:
4028 case STL_PREVIEWFLAG_ALT:
4029 itemisflag = TRUE;
4030 if (wp->w_p_pvw)
4031 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
4032 : _("[Preview]"));
4033 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02004034
4035 case STL_QUICKFIX:
4036 if (bt_quickfix(wp->w_buffer))
4037 str = (char_u *)(wp->w_llist_ref
4038 ? _(msg_loclist)
4039 : _(msg_qflist));
4040 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041#endif
4042
4043 case STL_MODIFIED:
4044 case STL_MODIFIED_ALT:
4045 itemisflag = TRUE;
4046 switch ((opt == STL_MODIFIED_ALT)
4047 + bufIsChanged(wp->w_buffer) * 2
4048 + (!wp->w_buffer->b_p_ma) * 4)
4049 {
4050 case 2: str = (char_u *)"[+]"; break;
4051 case 3: str = (char_u *)",+"; break;
4052 case 4: str = (char_u *)"[-]"; break;
4053 case 5: str = (char_u *)",-"; break;
4054 case 6: str = (char_u *)"[+-]"; break;
4055 case 7: str = (char_u *)",+-"; break;
4056 }
4057 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004058
4059 case STL_HIGHLIGHT:
4060 t = s;
4061 while (*s != '#' && *s != NUL)
4062 ++s;
4063 if (*s == '#')
4064 {
4065 item[curitem].type = Highlight;
4066 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004067 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004068 curitem++;
4069 }
Bram Moolenaar11808222013-11-02 04:39:38 +01004070 if (*s != NUL)
4071 ++s;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004072 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
4074
4075 item[curitem].start = p;
4076 item[curitem].type = Normal;
4077 if (str != NULL && *str)
4078 {
4079 t = str;
4080 if (itemisflag)
4081 {
4082 if ((t[0] && t[1])
4083 && ((!prevchar_isitem && *t == ',')
4084 || (prevchar_isflag && *t == ' ')))
4085 t++;
4086 prevchar_isflag = TRUE;
4087 }
4088 l = vim_strsize(t);
4089 if (l > 0)
4090 prevchar_isitem = TRUE;
4091 if (l > maxwid)
4092 {
4093 while (l >= maxwid)
4094#ifdef FEAT_MBYTE
4095 if (has_mbyte)
4096 {
4097 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004098 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 else
4101#endif
4102 l -= byte2cells(*t++);
4103 if (p + 1 >= out + outlen)
4104 break;
4105 *p++ = '<';
4106 }
4107 if (minwid > 0)
4108 {
4109 for (; l < minwid && p + 1 < out + outlen; l++)
4110 {
4111 /* Don't put a "-" in front of a digit. */
4112 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4113 *p++ = ' ';
4114 else
4115 *p++ = fillchar;
4116 }
4117 minwid = 0;
4118 }
4119 else
4120 minwid *= -1;
4121 while (*t && p + 1 < out + outlen)
4122 {
4123 *p++ = *t++;
4124 /* Change a space by fillchar, unless fillchar is '-' and a
4125 * digit follows. */
4126 if (fillable && p[-1] == ' '
4127 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4128 p[-1] = fillchar;
4129 }
4130 for (; l < minwid && p + 1 < out + outlen; l++)
4131 *p++ = fillchar;
4132 }
4133 else if (num >= 0)
4134 {
4135 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4136 char_u nstr[20];
4137
4138 if (p + 20 >= out + outlen)
4139 break; /* not sufficient space */
4140 prevchar_isitem = TRUE;
4141 t = nstr;
4142 if (opt == STL_VIRTCOL_ALT)
4143 {
4144 *t++ = '-';
4145 minwid--;
4146 }
4147 *t++ = '%';
4148 if (zeropad)
4149 *t++ = '0';
4150 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004151 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 *t = 0;
4153
4154 for (n = num, l = 1; n >= nbase; n /= nbase)
4155 l++;
4156 if (opt == STL_VIRTCOL_ALT)
4157 l++;
4158 if (l > maxwid)
4159 {
4160 l += 2;
4161 n = l - maxwid;
4162 while (l-- > maxwid)
4163 num /= nbase;
4164 *t++ = '>';
4165 *t++ = '%';
4166 *t = t[-3];
4167 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004168 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4169 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 }
4171 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004172 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4173 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 p += STRLEN(p);
4175 }
4176 else
4177 item[curitem].type = Empty;
4178
4179 if (opt == STL_VIM_EXPR)
4180 vim_free(str);
4181
4182 if (num >= 0 || (!itemisflag && str && *str))
4183 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4184 curitem++;
4185 }
4186 *p = NUL;
4187 itemcnt = curitem;
4188
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004189#ifdef FEAT_EVAL
4190 if (usefmt != fmt)
4191 vim_free(usefmt);
4192#endif
4193
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 width = vim_strsize(out);
4195 if (maxwidth > 0 && width > maxwidth)
4196 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004197 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 l = 0;
4199 if (itemcnt == 0)
4200 s = out;
4201 else
4202 {
4203 for ( ; l < itemcnt; l++)
4204 if (item[l].type == Trunc)
4205 {
4206 /* Truncate at %< item. */
4207 s = item[l].start;
4208 break;
4209 }
4210 if (l == itemcnt)
4211 {
4212 /* No %< item, truncate first item. */
4213 s = item[0].start;
4214 l = 0;
4215 }
4216 }
4217
4218 if (width - vim_strsize(s) >= maxwidth)
4219 {
4220 /* Truncation mark is beyond max length */
4221#ifdef FEAT_MBYTE
4222 if (has_mbyte)
4223 {
4224 s = out;
4225 width = 0;
4226 for (;;)
4227 {
4228 width += ptr2cells(s);
4229 if (width >= maxwidth)
4230 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004231 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 }
4233 /* Fill up for half a double-wide character. */
4234 while (++width < maxwidth)
4235 *s++ = fillchar;
4236 }
4237 else
4238#endif
4239 s = out + maxwidth - 1;
4240 for (l = 0; l < itemcnt; l++)
4241 if (item[l].start > s)
4242 break;
4243 itemcnt = l;
4244 *s++ = '>';
4245 *s = 0;
4246 }
4247 else
4248 {
4249#ifdef FEAT_MBYTE
4250 if (has_mbyte)
4251 {
4252 n = 0;
4253 while (width >= maxwidth)
4254 {
4255 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004256 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258 }
4259 else
4260#endif
4261 n = width - maxwidth + 1;
4262 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004263 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 *s = '<';
4265
4266 /* Fill up for half a double-wide character. */
4267 while (++width < maxwidth)
4268 {
4269 s = s + STRLEN(s);
4270 *s++ = fillchar;
4271 *s = NUL;
4272 }
4273
4274 --n; /* count the '<' */
4275 for (; l < itemcnt; l++)
4276 {
4277 if (item[l].start - n >= s)
4278 item[l].start -= n;
4279 else
4280 item[l].start = s;
4281 }
4282 }
4283 width = maxwidth;
4284 }
4285 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4286 {
4287 /* Apply STL_MIDDLE if any */
4288 for (l = 0; l < itemcnt; l++)
4289 if (item[l].type == Middle)
4290 break;
4291 if (l < itemcnt)
4292 {
4293 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004294 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 for (s = item[l].start; s < p; s++)
4296 *s = fillchar;
4297 for (l++; l < itemcnt; l++)
4298 item[l].start += maxwidth - width;
4299 width = maxwidth;
4300 }
4301 }
4302
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004303 /* Store the info about highlighting. */
4304 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004306 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 for (l = 0; l < itemcnt; l++)
4308 {
4309 if (item[l].type == Highlight)
4310 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004311 sp->start = item[l].start;
4312 sp->userhl = item[l].minwid;
4313 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 }
4315 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004316 sp->start = NULL;
4317 sp->userhl = 0;
4318 }
4319
4320 /* Store the info about tab pages labels. */
4321 if (tabtab != NULL)
4322 {
4323 sp = tabtab;
4324 for (l = 0; l < itemcnt; l++)
4325 {
4326 if (item[l].type == TabPage)
4327 {
4328 sp->start = item[l].start;
4329 sp->userhl = item[l].minwid;
4330 sp++;
4331 }
4332 }
4333 sp->start = NULL;
4334 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336
4337 return width;
4338}
4339#endif /* FEAT_STL_OPT */
4340
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004341#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4342 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004344 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4345 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 */
4347 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004348get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004350 char_u *buf;
4351 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352{
4353 long above; /* number of lines above window */
4354 long below; /* number of lines below window */
4355
4356 above = wp->w_topline - 1;
4357#ifdef FEAT_DIFF
4358 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4359#endif
4360 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4361 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004362 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4363 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004365 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004367 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 ? (int)(above / ((above + below) / 100L))
4369 : (int)(above * 100L / (above + below)));
4370}
4371#endif
4372
4373/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004374 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 * Return TRUE if it was appended.
4376 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004377 static int
4378append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 win_T *wp;
4380 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004381 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383{
4384 char_u *p;
4385
4386 if (ARGCOUNT <= 1) /* nothing to do */
4387 return FALSE;
4388
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004389 p = buf + STRLEN(buf); /* go to the end of the buffer */
4390 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 return FALSE;
4392 *p++ = ' ';
4393 *p++ = '(';
4394 if (add_file)
4395 {
4396 STRCPY(p, "file ");
4397 p += 5;
4398 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004399 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4400 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4402 return TRUE;
4403}
4404
4405/*
4406 * If fname is not a full path, make it a full path.
4407 * Returns pointer to allocated memory (NULL for failure).
4408 */
4409 char_u *
4410fix_fname(fname)
4411 char_u *fname;
4412{
4413 /*
4414 * Force expanding the path always for Unix, because symbolic links may
4415 * mess up the full path name, even though it starts with a '/'.
4416 * Also expand when there is ".." in the file name, try to remove it,
4417 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004418 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 * For MS-Windows also expand names like "longna~1" to "longname".
4420 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004421#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 return FullName_save(fname, TRUE);
4423#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004424 if (!vim_isAbsName(fname)
4425 || strstr((char *)fname, "..") != NULL
4426 || strstr((char *)fname, "//") != NULL
4427# ifdef BACKSLASH_IN_FILENAME
4428 || strstr((char *)fname, "\\\\") != NULL
4429# endif
4430# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004432# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 )
4434 return FullName_save(fname, FALSE);
4435
4436 fname = vim_strsave(fname);
4437
Bram Moolenaar9b942202007-10-03 12:31:33 +00004438# ifdef USE_FNAME_CASE
4439# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004441# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 {
4443 if (fname != NULL)
4444 fname_case(fname, 0); /* set correct case for file name */
4445 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004446# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 return fname;
4449#endif
4450}
4451
4452/*
4453 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4454 * "ffname" becomes a pointer to allocated memory (or NULL).
4455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 void
4457fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004458 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 char_u **ffname;
4460 char_u **sfname;
4461{
4462 if (*ffname == NULL) /* if no file name given, nothing to do */
4463 return;
4464 if (*sfname == NULL) /* if no short file name given, use ffname */
4465 *sfname = *ffname;
4466 *ffname = fix_fname(*ffname); /* expand to full path */
4467
4468#ifdef FEAT_SHORTCUT
4469 if (!buf->b_p_bin)
4470 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004471 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 /* If the file name is a shortcut file, use the file it links to. */
4474 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004475 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 vim_free(*ffname);
4478 *ffname = rfname;
4479 *sfname = rfname;
4480 }
4481 }
4482#endif
4483}
4484
4485/*
4486 * Get the file name for an argument list entry.
4487 */
4488 char_u *
4489alist_name(aep)
4490 aentry_T *aep;
4491{
4492 buf_T *bp;
4493
4494 /* Use the name from the associated buffer if it exists. */
4495 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004496 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 return aep->ae_fname;
4498 return bp->b_fname;
4499}
4500
4501#if defined(FEAT_WINDOWS) || defined(PROTO)
4502/*
4503 * do_arg_all(): Open up to 'count' windows, one for each argument.
4504 */
4505 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004506do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 int count;
4508 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004509 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510{
4511 int i;
4512 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004513 char_u *opened; /* Array of weight for which args are open:
4514 * 0: not opened
4515 * 1: opened in other tab
4516 * 2: opened in curtab
4517 * 3: opened in curtab and curwin
4518 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004519 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 int use_firstwin = FALSE; /* use first window for arglist */
4521 int split_ret = OK;
4522 int p_ea_save;
4523 alist_T *alist; /* argument list to be used */
4524 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004525 tabpage_T *tpnext;
4526 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004527 win_T *old_curwin, *last_curwin;
4528 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004529 win_T *new_curwin = NULL;
4530 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531
4532 if (ARGCOUNT <= 0)
4533 {
4534 /* Don't give an error message. We don't want it when the ":all"
4535 * command is in the .vimrc. */
4536 return;
4537 }
4538 setpcmark();
4539
4540 opened_len = ARGCOUNT;
4541 opened = alloc_clear((unsigned)opened_len);
4542 if (opened == NULL)
4543 return;
4544
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004545 /* Autocommands may do anything to the argument list. Make sure it's not
4546 * freed while we are working here by "locking" it. We still have to
4547 * watch out for its size to be changed. */
4548 alist = curwin->w_alist;
4549 ++alist->al_refcount;
4550
4551 old_curwin = curwin;
4552 old_curtab = curtab;
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554#ifdef FEAT_GUI
4555 need_mouse_correct = TRUE;
4556#endif
4557
4558 /*
4559 * Try closing all windows that are not in the argument list.
4560 * Also close windows that are not full width;
4561 * When 'hidden' or "forceit" set the buffer becomes hidden.
4562 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004563 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004565 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004566 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004567 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004569 tpnext = curtab->tp_next;
4570 for (wp = firstwin; wp != NULL; wp = wpnext)
4571 {
4572 wpnext = wp->w_next;
4573 buf = wp->w_buffer;
4574 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004575 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004577 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004579 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004580 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004581 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004583 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004584 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004586 if (i < alist->al_ga.ga_len
4587 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4588 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4589 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004591 int weight = 1;
4592
4593 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004594 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004595 ++weight;
4596 if (old_curwin == wp)
4597 ++weight;
4598 }
4599
4600 if (weight > (int)opened[i])
4601 {
4602 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004603 if (i == 0)
4604 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004605 if (new_curwin != NULL)
4606 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004607 new_curwin = wp;
4608 new_curtab = curtab;
4609 }
4610 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004611 else if (keep_tabs)
4612 i = opened_len;
4613
4614 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004615 {
4616 /* Use the current argument list for all windows
4617 * containing a file from it. */
4618 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004619 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004620 ++wp->w_alist->al_refcount;
4621 }
4622 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 }
4625 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004626 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004628 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004630 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004631 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004633 /* If the buffer was changed, and we would like to hide it,
4634 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004635 if (!P_HID(buf) && buf->b_nwindows <= 1
4636 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004638 (void)autowrite(buf, FALSE);
4639#ifdef FEAT_AUTOCMD
4640 /* check if autocommands removed the window */
4641 if (!win_valid(wp) || !buf_valid(buf))
4642 {
4643 wpnext = firstwin; /* start all over... */
4644 continue;
4645 }
4646#endif
4647 }
4648#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004649 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004650 if (firstwin == lastwin
4651 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004652#endif
4653 use_firstwin = TRUE;
4654#ifdef FEAT_WINDOWS
4655 else
4656 {
4657 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4658# ifdef FEAT_AUTOCMD
4659 /* check if autocommands removed the next window */
4660 if (!win_valid(wpnext))
4661 wpnext = firstwin; /* start all over... */
4662# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 }
4664#endif
4665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666 }
4667 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004668
4669 /* Without the ":tab" modifier only do the current tab page. */
4670 if (had_tab == 0 || tpnext == NULL)
4671 break;
4672
4673# ifdef FEAT_AUTOCMD
4674 /* check if autocommands removed the next tab page */
4675 if (!valid_tabpage(tpnext))
4676 tpnext = first_tabpage; /* start all over...*/
4677# endif
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004678 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 }
4680
4681 /*
4682 * Open a window for files in the argument list that don't have one.
4683 * ARGCOUNT may change while doing this, because of autocommands.
4684 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004685 if (count > opened_len || count <= 0)
4686 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687
4688#ifdef FEAT_AUTOCMD
4689 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4690 ++autocmd_no_enter;
4691 ++autocmd_no_leave;
4692#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004693 last_curwin = curwin;
4694 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004696#ifdef FEAT_WINDOWS
4697 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4698 * leaving an empty tab page when executed locally. */
4699 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4700 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4701 use_firstwin = TRUE;
4702#endif
4703
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004704 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
4706 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4707 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004708 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
4710 /* Move the already present window to below the current window */
4711 if (curwin->w_arg_idx != i)
4712 {
4713 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4714 {
4715 if (wpnext->w_arg_idx == i)
4716 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004717 if (keep_tabs)
4718 {
4719 new_curwin = wpnext;
4720 new_curtab = curtab;
4721 }
4722 else
4723 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 break;
4725 }
4726 }
4727 }
4728 }
4729 else if (split_ret == OK)
4730 {
4731 if (!use_firstwin) /* split current window */
4732 {
4733 p_ea_save = p_ea;
4734 p_ea = TRUE; /* use space from all windows */
4735 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4736 p_ea = p_ea_save;
4737 if (split_ret == FAIL)
4738 continue;
4739 }
4740#ifdef FEAT_AUTOCMD
4741 else /* first window: do autocmd for leaving this buffer */
4742 --autocmd_no_leave;
4743#endif
4744
4745 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004746 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 */
4748 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004749 if (i == 0)
4750 {
4751 new_curwin = curwin;
4752 new_curtab = curtab;
4753 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4755 ECMD_ONE,
4756 ((P_HID(curwin->w_buffer)
4757 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004758 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#ifdef FEAT_AUTOCMD
4760 if (use_firstwin)
4761 ++autocmd_no_leave;
4762#endif
4763 use_firstwin = FALSE;
4764 }
4765 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004766
4767 /* When ":tab" was used open a new tab for a new window repeatedly. */
4768 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4769 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771
4772 /* Remove the "lock" on the argument list. */
4773 alist_unlink(alist);
4774
4775#ifdef FEAT_AUTOCMD
4776 --autocmd_no_enter;
4777#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004778 /* restore last referenced tabpage's curwin */
4779 if (last_curtab != new_curtab)
4780 {
4781 if (valid_tabpage(last_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004782 goto_tabpage_tp(last_curtab, TRUE, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004783 if (win_valid(last_curwin))
4784 win_enter(last_curwin, FALSE);
4785 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004786 /* to window with first arg */
4787 if (valid_tabpage(new_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004788 goto_tabpage_tp(new_curtab, TRUE, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004789 if (win_valid(new_curwin))
4790 win_enter(new_curwin, FALSE);
4791
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792#ifdef FEAT_AUTOCMD
4793 --autocmd_no_leave;
4794#endif
4795 vim_free(opened);
4796}
4797
4798# if defined(FEAT_LISTCMDS) || defined(PROTO)
4799/*
4800 * Open a window for a number of buffers.
4801 */
4802 void
4803ex_buffer_all(eap)
4804 exarg_T *eap;
4805{
4806 buf_T *buf;
4807 win_T *wp, *wpnext;
4808 int split_ret = OK;
4809 int p_ea_save;
4810 int open_wins = 0;
4811 int r;
4812 int count; /* Maximum number of windows to open. */
4813 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004814#ifdef FEAT_WINDOWS
4815 int had_tab = cmdmod.tab;
4816 tabpage_T *tpnext;
4817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
4819 if (eap->addr_count == 0) /* make as many windows as possible */
4820 count = 9999;
4821 else
4822 count = eap->line2; /* make as many windows as specified */
4823 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4824 all = FALSE;
4825 else
4826 all = TRUE;
4827
4828 setpcmark();
4829
4830#ifdef FEAT_GUI
4831 need_mouse_correct = TRUE;
4832#endif
4833
4834 /*
4835 * Close superfluous windows (two windows for the same buffer).
4836 * Also close windows that are not full-width.
4837 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004838#ifdef FEAT_WINDOWS
4839 if (had_tab > 0)
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004840 goto_tabpage_tp(first_tabpage, TRUE, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004841 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004844 tpnext = curtab->tp_next;
4845 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004847 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004848 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004849#ifdef FEAT_VERTSPLIT
4850 || ((cmdmod.split & WSP_VERT)
4851 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004852 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004853 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004855#ifdef FEAT_WINDOWS
4856 || (had_tab > 0 && wp != firstwin)
4857#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004858 ) && firstwin != lastwin
4859#ifdef FEAT_AUTOCMD
4860 && !(wp->w_closing || wp->w_buffer->b_closing)
4861#endif
4862 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004863 {
4864 win_close(wp, FALSE);
4865#ifdef FEAT_AUTOCMD
4866 wpnext = firstwin; /* just in case an autocommand does
4867 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004868 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004869 open_wins = 0;
4870#endif
4871 }
4872 else
4873 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004875
4876#ifdef FEAT_WINDOWS
4877 /* Without the ":tab" modifier only do the current tab page. */
4878 if (had_tab == 0 || tpnext == NULL)
4879 break;
Bram Moolenaar49e649f2013-05-06 04:50:35 +02004880 goto_tabpage_tp(tpnext, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 /*
4885 * Go through the buffer list. When a buffer doesn't have a window yet,
4886 * open one. Otherwise move the window to the right position.
4887 * Watch out for autocommands that delete buffers or windows!
4888 */
4889#ifdef FEAT_AUTOCMD
4890 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4891 ++autocmd_no_enter;
4892#endif
4893 win_enter(lastwin, FALSE);
4894#ifdef FEAT_AUTOCMD
4895 ++autocmd_no_leave;
4896#endif
4897 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4898 {
4899 /* Check if this buffer needs a window */
4900 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4901 continue;
4902
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004903#ifdef FEAT_WINDOWS
4904 if (had_tab != 0)
4905 {
4906 /* With the ":tab" modifier don't move the window. */
4907 if (buf->b_nwindows > 0)
4908 wp = lastwin; /* buffer has a window, skip it */
4909 else
4910 wp = NULL;
4911 }
4912 else
4913#endif
4914 {
4915 /* Check if this buffer already has a window */
4916 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4917 if (wp->w_buffer == buf)
4918 break;
4919 /* If the buffer already has a window, move it */
4920 if (wp != NULL)
4921 win_move_after(wp, curwin);
4922 }
4923
4924 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 {
4926 /* Split the window and put the buffer in it */
4927 p_ea_save = p_ea;
4928 p_ea = TRUE; /* use space from all windows */
4929 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4930 ++open_wins;
4931 p_ea = p_ea_save;
4932 if (split_ret == FAIL)
4933 continue;
4934
4935 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004936#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 swap_exists_action = SEA_DIALOG;
4938#endif
4939 set_curbuf(buf, DOBUF_GOTO);
4940#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004943#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004945# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 break;
4947 }
4948#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004949#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (swap_exists_action == SEA_QUIT)
4951 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004952# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4953 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004954
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004955 /* Reset the error/interrupt/exception state here so that
4956 * aborting() returns FALSE when closing a window. */
4957 enter_cleanup(&cs);
4958# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004959
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004960 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 win_close(curwin, TRUE);
4962 --open_wins;
4963 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004964 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004965
4966# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4967 /* Restore the error/interrupt/exception state if not
4968 * discarded by a new aborting error, interrupt, or uncaught
4969 * exception. */
4970 leave_cleanup(&cs);
4971# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
4973 else
4974 handle_swap_exists(NULL);
4975#endif
4976 }
4977
4978 ui_breakcheck();
4979 if (got_int)
4980 {
4981 (void)vgetc(); /* only break the file loading, not the rest */
4982 break;
4983 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004984#ifdef FEAT_EVAL
4985 /* Autocommands deleted the buffer or aborted script processing!!! */
4986 if (aborting())
4987 break;
4988#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004989#ifdef FEAT_WINDOWS
4990 /* When ":tab" was used open a new tab for a new window repeatedly. */
4991 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4992 cmdmod.tab = 9999;
4993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995#ifdef FEAT_AUTOCMD
4996 --autocmd_no_enter;
4997#endif
4998 win_enter(firstwin, FALSE); /* back to first window */
4999#ifdef FEAT_AUTOCMD
5000 --autocmd_no_leave;
5001#endif
5002
5003 /*
5004 * Close superfluous windows.
5005 */
5006 for (wp = lastwin; open_wins > count; )
5007 {
5008 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
5009 || autowrite(wp->w_buffer, FALSE) == OK);
5010#ifdef FEAT_AUTOCMD
5011 if (!win_valid(wp))
5012 {
5013 /* BufWrite Autocommands made the window invalid, start over */
5014 wp = lastwin;
5015 }
5016 else
5017#endif
5018 if (r)
5019 {
5020 win_close(wp, !P_HID(wp->w_buffer));
5021 --open_wins;
5022 wp = lastwin;
5023 }
5024 else
5025 {
5026 wp = wp->w_prev;
5027 if (wp == NULL)
5028 break;
5029 }
5030 }
5031}
5032# endif /* FEAT_LISTCMDS */
5033
5034#endif /* FEAT_WINDOWS */
5035
Bram Moolenaara3227e22006-03-08 21:32:40 +00005036static int chk_modeline __ARGS((linenr_T, int));
5037
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038/*
5039 * do_modelines() - process mode lines for the current file
5040 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00005041 * "flags" can be:
5042 * OPT_WINONLY only set options local to window
5043 * OPT_NOWIN don't set options local to window
5044 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 * Returns immediately if the "ml" option isn't set.
5046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00005048do_modelines(flags)
5049 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005051 linenr_T lnum;
5052 int nmlines;
5053 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
5055 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
5056 return;
5057
5058 /* Disallow recursive entry here. Can happen when executing a modeline
5059 * triggers an autocommand, which reloads modelines with a ":do". */
5060 if (entered)
5061 return;
5062
5063 ++entered;
5064 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5065 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005066 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 nmlines = 0;
5068
5069 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5070 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005071 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 nmlines = 0;
5073 --entered;
5074}
5075
5076#include "version.h" /* for version number */
5077
5078/*
5079 * chk_modeline() - check a single line for a mode string
5080 * Return FAIL if an error encountered.
5081 */
5082 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005083chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005085 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086{
5087 char_u *s;
5088 char_u *e;
5089 char_u *linecopy; /* local copy of any modeline found */
5090 int prev;
5091 int vers;
5092 int end;
5093 int retval = OK;
5094 char_u *save_sourcing_name;
5095 linenr_T save_sourcing_lnum;
5096#ifdef FEAT_EVAL
5097 scid_T save_SID;
5098#endif
5099
5100 prev = -1;
5101 for (s = ml_get(lnum); *s != NUL; ++s)
5102 {
5103 if (prev == -1 || vim_isspace(prev))
5104 {
5105 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5106 || STRNCMP(s, "vi:", (size_t)3) == 0)
5107 break;
Bram Moolenaarc14621e2013-06-26 20:04:35 +02005108 /* Accept both "vim" and "Vim". */
5109 if ((s[0] == 'v' || s[0] == 'V') && s[1] == 'i' && s[2] == 'm')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5112 e = s + 4;
5113 else
5114 e = s + 3;
5115 vers = getdigits(&e);
5116 if (*e == ':'
Bram Moolenaar630a7302013-06-29 15:07:22 +02005117 && (s[0] != 'V'
5118 || STRNCMP(skipwhite(e + 1), "set", 3) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 && (s[3] == ':'
5120 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5121 || (VIM_VERSION_100 < vers && s[3] == '<')
5122 || (VIM_VERSION_100 > vers && s[3] == '>')
5123 || (VIM_VERSION_100 == vers && s[3] == '=')))
5124 break;
5125 }
5126 }
5127 prev = *s;
5128 }
5129
5130 if (*s)
5131 {
5132 do /* skip over "ex:", "vi:" or "vim:" */
5133 ++s;
5134 while (s[-1] != ':');
5135
5136 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5137 if (linecopy == NULL)
5138 return FAIL;
5139
5140 save_sourcing_lnum = sourcing_lnum;
5141 save_sourcing_name = sourcing_name;
5142 sourcing_lnum = lnum; /* prepare for emsg() */
5143 sourcing_name = (char_u *)"modelines";
5144
5145 end = FALSE;
5146 while (end == FALSE)
5147 {
5148 s = skipwhite(s);
5149 if (*s == NUL)
5150 break;
5151
5152 /*
5153 * Find end of set command: ':' or end of line.
5154 * Skip over "\:", replacing it with ":".
5155 */
5156 for (e = s; *e != ':' && *e != NUL; ++e)
5157 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005158 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (*e == NUL)
5160 end = TRUE;
5161
5162 /*
5163 * If there is a "set" command, require a terminating ':' and
5164 * ignore the stuff after the ':'.
5165 * "vi:set opt opt opt: foo" -- foo not interpreted
5166 * "vi:opt opt opt: foo" -- foo interpreted
5167 * Accept "se" for compatibility with Elvis.
5168 */
5169 if (STRNCMP(s, "set ", (size_t)4) == 0
5170 || STRNCMP(s, "se ", (size_t)3) == 0)
5171 {
5172 if (*e != ':') /* no terminating ':'? */
5173 break;
5174 end = TRUE;
5175 s = vim_strchr(s, ' ') + 1;
5176 }
5177 *e = NUL; /* truncate the set command */
5178
5179 if (*s != NUL) /* skip over an empty "::" */
5180 {
5181#ifdef FEAT_EVAL
5182 save_SID = current_SID;
5183 current_SID = SID_MODELINE;
5184#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005185 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#ifdef FEAT_EVAL
5187 current_SID = save_SID;
5188#endif
5189 if (retval == FAIL) /* stop if error found */
5190 break;
5191 }
5192 s = e + 1; /* advance to next part */
5193 }
5194
5195 sourcing_lnum = save_sourcing_lnum;
5196 sourcing_name = save_sourcing_name;
5197
5198 vim_free(linecopy);
5199 }
5200 return retval;
5201}
5202
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005203#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 int
5205read_viminfo_bufferlist(virp, writing)
5206 vir_T *virp;
5207 int writing;
5208{
5209 char_u *tab;
5210 linenr_T lnum;
5211 colnr_T col;
5212 buf_T *buf;
5213 char_u *sfname;
5214 char_u *xline;
5215
5216 /* Handle long line and escaped characters. */
5217 xline = viminfo_readstring(virp, 1, FALSE);
5218
5219 /* don't read in if there are files on the command-line or if writing: */
5220 if (xline != NULL && !writing && ARGCOUNT == 0
5221 && find_viminfo_parameter('%') != NULL)
5222 {
5223 /* Format is: <fname> Tab <lnum> Tab <col>.
5224 * Watch out for a Tab in the file name, work from the end. */
5225 lnum = 0;
5226 col = 0;
5227 tab = vim_strrchr(xline, '\t');
5228 if (tab != NULL)
5229 {
5230 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005231 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 tab = vim_strrchr(xline, '\t');
5233 if (tab != NULL)
5234 {
5235 *tab++ = '\0';
5236 lnum = atol((char *)tab);
5237 }
5238 }
5239
5240 /* Expand "~/" in the file name at "line + 1" to a full path.
5241 * Then try shortening it by comparing with the current directory */
5242 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005243 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244
5245 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5246 if (buf != NULL) /* just in case... */
5247 {
5248 buf->b_last_cursor.lnum = lnum;
5249 buf->b_last_cursor.col = col;
5250 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5251 }
5252 }
5253 vim_free(xline);
5254
5255 return viminfo_readline(virp);
5256}
5257
5258 void
5259write_viminfo_bufferlist(fp)
5260 FILE *fp;
5261{
5262 buf_T *buf;
5263#ifdef FEAT_WINDOWS
5264 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005265 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266#endif
5267 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005268 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269
5270 if (find_viminfo_parameter('%') == NULL)
5271 return;
5272
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005273 /* Without a number -1 is returned: do all buffers. */
5274 max_buffers = get_viminfo_parameter('%');
5275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005277#define LINE_BUF_LEN (MAXPATHL + 40)
5278 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279 if (line == NULL)
5280 return;
5281
5282#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005283 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 set_last_cursor(win);
5285#else
5286 set_last_cursor(curwin);
5287#endif
5288
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005289 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5291 {
5292 if (buf->b_fname == NULL
5293 || !buf->b_p_bl
5294#ifdef FEAT_QUICKFIX
5295 || bt_quickfix(buf)
5296#endif
5297 || removable(buf->b_ffname))
5298 continue;
5299
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005300 if (max_buffers-- == 0)
5301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 putc('%', fp);
5303 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005304 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 (long)buf->b_last_cursor.lnum,
5306 buf->b_last_cursor.col);
5307 viminfo_writestring(fp, line);
5308 }
5309 vim_free(line);
5310}
5311#endif
5312
5313
5314/*
5315 * Return special buffer name.
5316 * Returns NULL when the buffer has a normal file name.
5317 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005318 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319buf_spname(buf)
5320 buf_T *buf;
5321{
5322#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5323 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005324 {
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005325 win_T *win;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005326 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005327
5328 /*
5329 * For location list window, w_llist_ref points to the location list.
5330 * For quickfix window, w_llist_ref is NULL.
5331 */
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005332 if (find_win_for_buf(buf, &win, &tp) == OK && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005333 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005334 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005335 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337#endif
5338#ifdef FEAT_QUICKFIX
5339 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5340 * contains the name as specified by the user */
5341 if (bt_nofile(buf))
5342 {
5343 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005344 return buf->b_sfname;
5345 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
5347#endif
5348 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005349 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 return NULL;
5351}
5352
Bram Moolenaar4a3aef72013-07-17 19:12:57 +02005353#if (defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)) \
5354 || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
5355 || defined(PROTO)
5356/*
5357 * Find a window for buffer "buf".
5358 * If found OK is returned and "wp" and "tp" are set to the window and tabpage.
5359 * If not found FAIL is returned.
5360 */
5361 int
5362find_win_for_buf(buf, wp, tp)
5363 buf_T *buf;
5364 win_T **wp;
5365 tabpage_T **tp;
5366{
5367 FOR_ALL_TAB_WINDOWS(*tp, *wp)
5368 if ((*wp)->w_buffer == buf)
5369 goto win_found;
5370 return FAIL;
5371win_found:
5372 return OK;
5373}
5374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376#if defined(FEAT_SIGNS) || defined(PROTO)
5377/*
5378 * Insert the sign into the signlist.
5379 */
5380 static void
5381insert_sign(buf, prev, next, id, lnum, typenr)
5382 buf_T *buf; /* buffer to store sign in */
5383 signlist_T *prev; /* previous sign entry */
5384 signlist_T *next; /* next sign entry */
5385 int id; /* sign ID */
5386 linenr_T lnum; /* line number which gets the mark */
5387 int typenr; /* typenr of sign we are adding */
5388{
5389 signlist_T *newsign;
5390
5391 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5392 if (newsign != NULL)
5393 {
5394 newsign->id = id;
5395 newsign->lnum = lnum;
5396 newsign->typenr = typenr;
5397 newsign->next = next;
5398#ifdef FEAT_NETBEANS_INTG
5399 newsign->prev = prev;
5400 if (next != NULL)
5401 next->prev = newsign;
5402#endif
5403
5404 if (prev == NULL)
5405 {
5406 /* When adding first sign need to redraw the windows to create the
5407 * column for signs. */
5408 if (buf->b_signlist == NULL)
5409 {
5410 redraw_buf_later(buf, NOT_VALID);
5411 changed_cline_bef_curs();
5412 }
5413
5414 /* first sign in signlist */
5415 buf->b_signlist = newsign;
5416 }
5417 else
5418 prev->next = newsign;
5419 }
5420}
5421
5422/*
5423 * Add the sign into the signlist. Find the right spot to do it though.
5424 */
5425 void
5426buf_addsign(buf, id, lnum, typenr)
5427 buf_T *buf; /* buffer to store sign in */
5428 int id; /* sign ID */
5429 linenr_T lnum; /* line number which gets the mark */
5430 int typenr; /* typenr of sign we are adding */
5431{
5432 signlist_T *sign; /* a sign in the signlist */
5433 signlist_T *prev; /* the previous sign */
5434
5435 prev = NULL;
5436 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5437 {
5438 if (lnum == sign->lnum && id == sign->id)
5439 {
5440 sign->typenr = typenr;
5441 return;
5442 }
5443 else if (
5444#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5445 id < 0 &&
5446#endif
5447 lnum < sign->lnum)
5448 {
5449#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5450 /* XXX - GRP: Is this because of sign slide problem? Or is it
5451 * really needed? Or is it because we allow multiple signs per
5452 * line? If so, should I add that feature to FEAT_SIGNS?
5453 */
5454 while (prev != NULL && prev->lnum == lnum)
5455 prev = prev->prev;
5456 if (prev == NULL)
5457 sign = buf->b_signlist;
5458 else
5459 sign = prev->next;
5460#endif
5461 insert_sign(buf, prev, sign, id, lnum, typenr);
5462 return;
5463 }
5464 prev = sign;
5465 }
5466#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5467 /* XXX - GRP: See previous comment */
5468 while (prev != NULL && prev->lnum == lnum)
5469 prev = prev->prev;
5470 if (prev == NULL)
5471 sign = buf->b_signlist;
5472 else
5473 sign = prev->next;
5474#endif
5475 insert_sign(buf, prev, sign, id, lnum, typenr);
5476
5477 return;
5478}
5479
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005480 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481buf_change_sign_type(buf, markId, typenr)
5482 buf_T *buf; /* buffer to store sign in */
5483 int markId; /* sign ID */
5484 int typenr; /* typenr of sign we are adding */
5485{
5486 signlist_T *sign; /* a sign in the signlist */
5487
5488 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5489 {
5490 if (sign->id == markId)
5491 {
5492 sign->typenr = typenr;
5493 return sign->lnum;
5494 }
5495 }
5496
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005497 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498}
5499
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005500 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501buf_getsigntype(buf, lnum, type)
5502 buf_T *buf;
5503 linenr_T lnum;
5504 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5505{
5506 signlist_T *sign; /* a sign in a b_signlist */
5507
5508 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5509 if (sign->lnum == lnum
5510 && (type == SIGN_ANY
5511# ifdef FEAT_SIGN_ICONS
5512 || (type == SIGN_ICON
5513 && sign_get_image(sign->typenr) != NULL)
5514# endif
5515 || (type == SIGN_TEXT
5516 && sign_get_text(sign->typenr) != NULL)
5517 || (type == SIGN_LINEHL
5518 && sign_get_attr(sign->typenr, TRUE) != 0)))
5519 return sign->typenr;
5520 return 0;
5521}
5522
5523
5524 linenr_T
5525buf_delsign(buf, id)
5526 buf_T *buf; /* buffer sign is stored in */
5527 int id; /* sign id */
5528{
5529 signlist_T **lastp; /* pointer to pointer to current sign */
5530 signlist_T *sign; /* a sign in a b_signlist */
5531 signlist_T *next; /* the next sign in a b_signlist */
5532 linenr_T lnum; /* line number whose sign was deleted */
5533
5534 lastp = &buf->b_signlist;
5535 lnum = 0;
5536 for (sign = buf->b_signlist; sign != NULL; sign = next)
5537 {
5538 next = sign->next;
5539 if (sign->id == id)
5540 {
5541 *lastp = next;
5542#ifdef FEAT_NETBEANS_INTG
5543 if (next != NULL)
5544 next->prev = sign->prev;
5545#endif
5546 lnum = sign->lnum;
5547 vim_free(sign);
5548 break;
5549 }
5550 else
5551 lastp = &sign->next;
5552 }
5553
5554 /* When deleted the last sign need to redraw the windows to remove the
5555 * sign column. */
5556 if (buf->b_signlist == NULL)
5557 {
5558 redraw_buf_later(buf, NOT_VALID);
5559 changed_cline_bef_curs();
5560 }
5561
5562 return lnum;
5563}
5564
5565
5566/*
5567 * Find the line number of the sign with the requested id. If the sign does
5568 * not exist, return 0 as the line number. This will still let the correct file
5569 * get loaded.
5570 */
5571 int
5572buf_findsign(buf, id)
5573 buf_T *buf; /* buffer to store sign in */
5574 int id; /* sign ID */
5575{
5576 signlist_T *sign; /* a sign in the signlist */
5577
5578 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5579 if (sign->id == id)
5580 return sign->lnum;
5581
5582 return 0;
5583}
5584
5585 int
5586buf_findsign_id(buf, lnum)
5587 buf_T *buf; /* buffer whose sign we are searching for */
5588 linenr_T lnum; /* line number of sign */
5589{
5590 signlist_T *sign; /* a sign in the signlist */
5591
5592 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5593 if (sign->lnum == lnum)
5594 return sign->id;
5595
5596 return 0;
5597}
5598
5599
5600# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5601/* see if a given type of sign exists on a specific line */
5602 int
5603buf_findsigntype_id(buf, lnum, typenr)
5604 buf_T *buf; /* buffer whose sign we are searching for */
5605 linenr_T lnum; /* line number of sign */
5606 int typenr; /* sign type number */
5607{
5608 signlist_T *sign; /* a sign in the signlist */
5609
5610 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5611 if (sign->lnum == lnum && sign->typenr == typenr)
5612 return sign->id;
5613
5614 return 0;
5615}
5616
5617
5618# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5619/* return the number of icons on the given line */
5620 int
5621buf_signcount(buf, lnum)
5622 buf_T *buf;
5623 linenr_T lnum;
5624{
5625 signlist_T *sign; /* a sign in the signlist */
5626 int count = 0;
5627
5628 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5629 if (sign->lnum == lnum)
5630 if (sign_get_image(sign->typenr) != NULL)
5631 count++;
5632
5633 return count;
5634}
5635# endif /* FEAT_SIGN_ICONS */
5636# endif /* FEAT_NETBEANS_INTG */
5637
5638
5639/*
5640 * Delete signs in buffer "buf".
5641 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005642 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643buf_delete_signs(buf)
5644 buf_T *buf;
5645{
5646 signlist_T *next;
5647
5648 while (buf->b_signlist != NULL)
5649 {
5650 next = buf->b_signlist->next;
5651 vim_free(buf->b_signlist);
5652 buf->b_signlist = next;
5653 }
5654}
5655
5656/*
5657 * Delete all signs in all buffers.
5658 */
5659 void
5660buf_delete_all_signs()
5661{
5662 buf_T *buf; /* buffer we are checking for signs */
5663
5664 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5665 if (buf->b_signlist != NULL)
5666 {
5667 /* Need to redraw the windows to remove the sign column. */
5668 redraw_buf_later(buf, NOT_VALID);
5669 buf_delete_signs(buf);
5670 }
5671}
5672
5673/*
5674 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5675 */
5676 void
5677sign_list_placed(rbuf)
5678 buf_T *rbuf;
5679{
5680 buf_T *buf;
5681 signlist_T *p;
5682 char lbuf[BUFSIZ];
5683
5684 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5685 msg_putchar('\n');
5686 if (rbuf == NULL)
5687 buf = firstbuf;
5688 else
5689 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005690 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 {
5692 if (buf->b_signlist != NULL)
5693 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005694 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5696 msg_putchar('\n');
5697 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005698 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005700 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5702 MSG_PUTS(lbuf);
5703 msg_putchar('\n');
5704 }
5705 if (rbuf != NULL)
5706 break;
5707 buf = buf->b_next;
5708 }
5709}
5710
5711/*
5712 * Adjust a placed sign for inserted/deleted lines.
5713 */
5714 void
5715sign_mark_adjust(line1, line2, amount, amount_after)
5716 linenr_T line1;
5717 linenr_T line2;
5718 long amount;
5719 long amount_after;
5720{
5721 signlist_T *sign; /* a sign in a b_signlist */
5722
5723 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5724 {
5725 if (sign->lnum >= line1 && sign->lnum <= line2)
5726 {
5727 if (amount == MAXLNUM)
5728 sign->lnum = line1;
5729 else
5730 sign->lnum += amount;
5731 }
5732 else if (sign->lnum > line2)
5733 sign->lnum += amount_after;
5734 }
5735}
5736#endif /* FEAT_SIGNS */
5737
5738/*
5739 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5740 */
5741 void
5742set_buflisted(on)
5743 int on;
5744{
5745 if (on != curbuf->b_p_bl)
5746 {
5747 curbuf->b_p_bl = on;
5748#ifdef FEAT_AUTOCMD
5749 if (on)
5750 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5751 else
5752 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5753#endif
5754 }
5755}
5756
5757/*
5758 * Read the file for "buf" again and check if the contents changed.
5759 * Return TRUE if it changed or this could not be checked.
5760 */
5761 int
5762buf_contents_changed(buf)
5763 buf_T *buf;
5764{
5765 buf_T *newbuf;
5766 int differ = TRUE;
5767 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005768 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769 exarg_T ea;
5770
5771 /* Allocate a buffer without putting it in the buffer list. */
5772 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5773 if (newbuf == NULL)
5774 return TRUE;
5775
5776 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5777 if (prep_exarg(&ea, buf) == FAIL)
5778 {
5779 wipe_buffer(newbuf, FALSE);
5780 return TRUE;
5781 }
5782
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 /* set curwin/curbuf to buf and save a few things */
5784 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785
Bram Moolenaar4770d092006-01-12 23:22:24 +00005786 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 && readfile(buf->b_ffname, buf->b_fname,
5788 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5789 &ea, READ_NEW | READ_DUMMY) == OK)
5790 {
5791 /* compare the two files line by line */
5792 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5793 {
5794 differ = FALSE;
5795 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5796 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5797 {
5798 differ = TRUE;
5799 break;
5800 }
5801 }
5802 }
5803 vim_free(ea.cmd);
5804
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 /* restore curwin/curbuf and a few other things */
5806 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807
5808 if (curbuf != newbuf) /* safety check */
5809 wipe_buffer(newbuf, FALSE);
5810
5811 return differ;
5812}
5813
5814/*
5815 * Wipe out a buffer and decrement the last buffer number if it was used for
5816 * this buffer. Call this to wipe out a temp buffer that does not contain any
5817 * marks.
5818 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 void
5820wipe_buffer(buf, aucmd)
5821 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005822 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823{
5824 if (buf->b_fnum == top_file_num - 1)
5825 --top_file_num;
5826
5827#ifdef FEAT_AUTOCMD
5828 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005829 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005831 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832#ifdef FEAT_AUTOCMD
5833 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005834 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835#endif
5836}