blob: f4713a1f790104f198609a82806d43e7100a6326 [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
85
86 /*
87 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
88 * When re-entering the same buffer, it should not change, because the
89 * user may have reset the flag by hand.
90 */
91 if (readonlymode && curbuf->b_ffname != NULL
92 && (curbuf->b_flags & BF_NEVERLOADED))
93 curbuf->b_p_ro = TRUE;
94
Bram Moolenaar4770d092006-01-12 23:22:24 +000095 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000096 {
97 /*
98 * There MUST be a memfile, otherwise we can't do anything
99 * If we can't create one for the current buffer, take another buffer
100 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100101 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
103 if (curbuf->b_ml.ml_mfp != NULL)
104 break;
105 /*
106 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000107 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000108 */
109 if (curbuf == NULL)
110 {
111 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
112 getout(2);
113 }
114 EMSG(_("E83: Cannot allocate buffer, using other one..."));
115 enter_buffer(curbuf);
116 return FAIL;
117 }
118
119#ifdef FEAT_AUTOCMD
120 /* The autocommands in readfile() may change the buffer, but only AFTER
121 * reading the file. */
122 old_curbuf = curbuf;
123 modified_was_set = FALSE;
124#endif
125
126 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100127 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128
129 if (curbuf->b_ffname != NULL
130#ifdef FEAT_NETBEANS_INTG
131 && netbeansReadFile
132#endif
133 )
134 {
135#ifdef FEAT_NETBEANS_INTG
136 int oldFire = netbeansFireChanges;
137
138 netbeansFireChanges = 0;
139#endif
140 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200141 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
142 flags | READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143#ifdef FEAT_NETBEANS_INTG
144 netbeansFireChanges = oldFire;
145#endif
146 /* Help buffer is filtered. */
147 if (curbuf->b_help)
148 fix_help_buffer();
149 }
150 else if (read_stdin)
151 {
152 int save_bin = curbuf->b_p_bin;
153 linenr_T line_count;
154
155 /*
156 * First read the text in binary mode into the buffer.
157 * Then read from that same buffer and append at the end. This makes
158 * it possible to retry when 'fileformat' or 'fileencoding' was
159 * guessed wrong.
160 */
161 curbuf->b_p_bin = TRUE;
162 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200163 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
164 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 curbuf->b_p_bin = save_bin;
166 if (retval == OK)
167 {
168 line_count = curbuf->b_ml.ml_line_count;
169 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200170 (linenr_T)0, (linenr_T)MAXLNUM, eap,
171 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 if (retval == OK)
173 {
174 /* Delete the binary lines. */
175 while (--line_count >= 0)
176 ml_delete((linenr_T)1, FALSE);
177 }
178 else
179 {
180 /* Delete the converted lines. */
181 while (curbuf->b_ml.ml_line_count > line_count)
182 ml_delete(line_count, FALSE);
183 }
184 /* Put the cursor on the first line. */
185 curwin->w_cursor.lnum = 1;
186 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000187
188 /* Set or reset 'modified' before executing autocommands, so that
189 * it can be changed there. */
190 if (!readonlymode && !bufempty())
191 changed();
192 else if (retval != FAIL)
193 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194#ifdef FEAT_AUTOCMD
195# ifdef FEAT_EVAL
196 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
197 curbuf, &retval);
198# else
199 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
200# endif
201#endif
202 }
203 }
204
205 /* if first time loading this buffer, init b_chartab[] */
206 if (curbuf->b_flags & BF_NEVERLOADED)
207 (void)buf_init_chartab(curbuf, FALSE);
208
209 /*
210 * Set/reset the Changed flag first, autocmds may change the buffer.
211 * Apply the automatic commands, before processing the modelines.
212 * So the modelines have priority over auto commands.
213 */
214 /* When reading stdin, the buffer contents always needs writing, so set
215 * the changed flag. Unless in readonly mode: "ls | gview -".
216 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000217 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_AUTOCMD
219 || modified_was_set /* ":set modified" used in autocmd */
220# ifdef FEAT_EVAL
221 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
222# endif
223#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000224 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000226 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227 unchanged(curbuf, FALSE);
228 save_file_ff(curbuf); /* keep this fileformat */
229
230 /* require "!" to overwrite the file, because it wasn't read completely */
231#ifdef FEAT_EVAL
232 if (aborting())
233#else
234 if (got_int)
235#endif
236 curbuf->b_flags |= BF_READERR;
237
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000238#ifdef FEAT_FOLDING
239 /* Need to update automatic folding. Do this before the autocommands,
240 * they may use the fold info. */
241 foldUpdateAll(curwin);
242#endif
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244#ifdef FEAT_AUTOCMD
245 /* need to set w_topline, unless some autocommand already did that. */
246 if (!(curwin->w_valid & VALID_TOPLINE))
247 {
248 curwin->w_topline = 1;
249# ifdef FEAT_DIFF
250 curwin->w_topfill = 0;
251# endif
252 }
253# ifdef FEAT_EVAL
254 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
255# else
256 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
257# endif
258#endif
259
260 if (retval != FAIL)
261 {
262#ifdef FEAT_AUTOCMD
263 /*
264 * The autocommands may have changed the current buffer. Apply the
265 * modelines to the correct buffer, if it still exists and is loaded.
266 */
267 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
268 {
269 aco_save_T aco;
270
271 /* Go to the buffer that was opened. */
272 aucmd_prepbuf(&aco, old_curbuf);
273#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000274 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
276
277#ifdef FEAT_AUTOCMD
278# ifdef FEAT_EVAL
279 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
280 &retval);
281# else
282 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
283# endif
284
285 /* restore curwin/curbuf and a few other things */
286 aucmd_restbuf(&aco);
287 }
288#endif
289 }
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 return retval;
292}
293
294/*
295 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
296 */
297 int
298buf_valid(buf)
299 buf_T *buf;
300{
301 buf_T *bp;
302
303 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
304 if (bp == buf)
305 return TRUE;
306 return FALSE;
307}
308
309/*
310 * Close the link to a buffer.
311 * "action" is used when there is no longer a window for the buffer.
312 * It can be:
313 * 0 buffer becomes hidden
314 * DOBUF_UNLOAD buffer is unloaded
315 * DOBUF_DELETE buffer is unloaded and removed from buffer list
316 * DOBUF_WIPE buffer is unloaded and really deleted
317 * When doing all but the first one on the current buffer, the caller should
318 * get a new buffer very soon!
319 *
320 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100321 *
322 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
323 * cause there to be only one window with this buffer. e.g. when ":quit" is
324 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000325 */
326 void
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100327close_buffer(win, buf, action, abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328 win_T *win; /* if not NULL, set b_last_cursor */
329 buf_T *buf;
330 int action;
Bram Moolenaar5fbe6992012-03-07 22:52:36 +0100331 int abort_if_last UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332{
333#ifdef FEAT_AUTOCMD
334 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100335 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336#endif
337 int unload_buf = (action != 0);
338 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
339 int wipe_buf = (action == DOBUF_WIPE);
340
341#ifdef FEAT_QUICKFIX
342 /*
343 * Force unloading or deleting when 'bufhidden' says so.
344 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
345 * "hide" (otherwise we could never free or delete a buffer).
346 */
347 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
348 {
349 del_buf = TRUE;
350 unload_buf = TRUE;
351 }
352 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
353 {
354 del_buf = TRUE;
355 unload_buf = TRUE;
356 wipe_buf = TRUE;
357 }
358 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
359 unload_buf = TRUE;
360#endif
361
362 if (win != NULL)
363 {
364 /* Set b_last_cursor when closing the last window for the buffer.
365 * Remember the last cursor position and window options of the buffer.
366 * This used to be only for the current window, but then options like
367 * 'foldmethod' may be lost with a ":only" command. */
368 if (buf->b_nwindows == 1)
369 set_last_cursor(win);
370 buflist_setfpos(buf, win,
371 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
372 win->w_cursor.col, TRUE);
373 }
374
375#ifdef FEAT_AUTOCMD
376 /* When the buffer is no longer in a window, trigger BufWinLeave */
377 if (buf->b_nwindows == 1)
378 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200379 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
381 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200382 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100383 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200384 /* Autocommands deleted the buffer. */
385aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100386 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100388 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200389 buf->b_closing = FALSE;
390 if (abort_if_last && one_window())
391 /* Autocommands made this the only window. */
392 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393
394 /* When the buffer becomes hidden, but is not unloaded, trigger
395 * BufHidden */
396 if (!unload_buf)
397 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200398 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000399 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
400 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200401 if (!buf_valid(buf))
402 /* Autocommands deleted the buffer. */
403 goto aucmd_abort;
404 buf->b_closing = FALSE;
405 if (abort_if_last && one_window())
406 /* Autocommands made this the only window. */
407 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408 }
409# ifdef FEAT_EVAL
410 if (aborting()) /* autocmds may abort script processing */
411 return;
412# endif
413 }
414 nwindows = buf->b_nwindows;
415#endif
416
417 /* decrease the link count from windows (unless not in any window) */
418 if (buf->b_nwindows > 0)
419 --buf->b_nwindows;
420
421 /* Return when a window is displaying the buffer or when it's not
422 * unloaded. */
423 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425
426 /* Always remove the buffer when there is no file name. */
427 if (buf->b_ffname == NULL)
428 del_buf = TRUE;
429
430 /*
431 * Free all things allocated for this buffer.
432 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
433 */
434#ifdef FEAT_AUTOCMD
435 /* Remember if we are closing the current buffer. Restore the number of
436 * windows, so that autocommands in buf_freeall() don't get confused. */
437 is_curbuf = (buf == curbuf);
438 buf->b_nwindows = nwindows;
439#endif
440
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200441 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200442 if (
443#ifdef FEAT_WINDOWS
444 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100445#else
446 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200447#endif
448 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200449 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000450
451#ifdef FEAT_AUTOCMD
452 /* Autocommands may have deleted the buffer. */
453 if (!buf_valid(buf))
454 return;
455# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000456 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000457 return;
458# endif
459
460 /* Autocommands may have opened or closed windows for this buffer.
461 * Decrement the count for the close we do here. */
462 if (buf->b_nwindows > 0)
463 --buf->b_nwindows;
464
465 /*
466 * It's possible that autocommands change curbuf to the one being deleted.
467 * This might cause the previous curbuf to be deleted unexpectedly. But
468 * in some cases it's OK to delete the curbuf, because a new one is
469 * obtained anyway. Therefore only return if curbuf changed to the
470 * deleted buffer.
471 */
472 if (buf == curbuf && !is_curbuf)
473 return;
474#endif
475
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000476 /* Change directories when the 'acd' option is set. */
477 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478
479 /*
480 * Remove the buffer from the list.
481 */
482 if (wipe_buf)
483 {
484#ifdef FEAT_SUN_WORKSHOP
485 if (usingSunWorkShop)
486 workshop_file_closed_lineno((char *)buf->b_ffname,
487 (int)buf->b_last_cursor.lnum);
488#endif
489 vim_free(buf->b_ffname);
490 vim_free(buf->b_sfname);
491 if (buf->b_prev == NULL)
492 firstbuf = buf->b_next;
493 else
494 buf->b_prev->b_next = buf->b_next;
495 if (buf->b_next == NULL)
496 lastbuf = buf->b_prev;
497 else
498 buf->b_next->b_prev = buf->b_prev;
499 free_buffer(buf);
500 }
501 else
502 {
503 if (del_buf)
504 {
505 /* Free all internal variables and reset option values, to make
506 * ":bdel" compatible with Vim 5.7. */
507 free_buffer_stuff(buf, TRUE);
508
509 /* Make it look like a new buffer. */
510 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
511
512 /* Init the options when loaded again. */
513 buf->b_p_initialized = FALSE;
514 }
515 buf_clear_file(buf);
516 if (del_buf)
517 buf->b_p_bl = FALSE;
518 }
519}
520
521/*
522 * Make buffer not contain a file.
523 */
524 void
525buf_clear_file(buf)
526 buf_T *buf;
527{
528 buf->b_ml.ml_line_count = 1;
529 unchanged(buf, TRUE);
530#ifndef SHORT_FNAME
531 buf->b_shortname = FALSE;
532#endif
533 buf->b_p_eol = TRUE;
534 buf->b_start_eol = TRUE;
535#ifdef FEAT_MBYTE
536 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000537 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538#endif
539 buf->b_ml.ml_mfp = NULL;
540 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
541#ifdef FEAT_NETBEANS_INTG
542 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543#endif
544}
545
546/*
547 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200548 * the file. flags:
549 * BFA_DEL buffer is going to be deleted
550 * BFA_WIPE buffer is going to be wiped out
551 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200554buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200556 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557{
558#ifdef FEAT_AUTOCMD
559 int is_curbuf = (buf == curbuf);
560
Bram Moolenaar362ce482012-06-06 19:02:45 +0200561 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
563 if (!buf_valid(buf)) /* autocommands may delete the buffer */
564 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200565 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566 {
567 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
568 if (!buf_valid(buf)) /* autocommands may delete the buffer */
569 return;
570 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200571 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 {
573 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
574 FALSE, buf);
575 if (!buf_valid(buf)) /* autocommands may delete the buffer */
576 return;
577 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200578 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000580 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581 return;
582# endif
583
584 /*
585 * It's possible that autocommands change curbuf to the one being deleted.
586 * This might cause curbuf to be deleted unexpectedly. But in some cases
587 * it's OK to delete the curbuf, because a new one is obtained anyway.
588 * Therefore only return if curbuf changed to the deleted buffer.
589 */
590 if (buf == curbuf && !is_curbuf)
591 return;
592#endif
593#ifdef FEAT_DIFF
594 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
595#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200596#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100597 /* Remove any ownsyntax, unless exiting. */
598 if (firstwin != NULL && curwin->w_buffer == buf)
599 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200600#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000601
602#ifdef FEAT_FOLDING
603 /* No folds in an empty buffer. */
604# ifdef FEAT_WINDOWS
605 {
606 win_T *win;
607 tabpage_T *tp;
608
609 FOR_ALL_TAB_WINDOWS(tp, win)
610 if (win->w_buffer == buf)
611 clearFolding(win);
612 }
613# else
614 if (curwin->w_buffer == buf)
615 clearFolding(curwin);
616# endif
617#endif
618
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619#ifdef FEAT_TCL
620 tcl_buffer_free(buf);
621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 ml_close(buf, TRUE); /* close and delete the memline/memfile */
623 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200624 if ((flags & BFA_KEEP_UNDO) == 0)
625 {
626 u_blockfree(buf); /* free the memory allocated for undo */
627 u_clearall(buf); /* reset all undo information */
628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000629#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200630 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000632 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633}
634
635/*
636 * Free a buffer structure and the things it contains related to the buffer
637 * itself (not the file, that must have been done already).
638 */
639 static void
640free_buffer(buf)
641 buf_T *buf;
642{
643 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200644#ifdef FEAT_LUA
645 lua_buffer_free(buf);
646#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000647#ifdef FEAT_MZSCHEME
648 mzscheme_buffer_free(buf);
649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650#ifdef FEAT_PERL
651 perl_buf_free(buf);
652#endif
653#ifdef FEAT_PYTHON
654 python_buffer_free(buf);
655#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200656#ifdef FEAT_PYTHON3
657 python3_buffer_free(buf);
658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#ifdef FEAT_RUBY
660 ruby_buffer_free(buf);
661#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000662#ifdef FEAT_AUTOCMD
663 aubuflocal_remove(buf);
664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665 vim_free(buf);
666}
667
668/*
669 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
670 */
671 static void
672free_buffer_stuff(buf, free_options)
673 buf_T *buf;
674 int free_options; /* free options as well */
675{
676 if (free_options)
677 {
678 clear_wininfo(buf); /* including window-local options */
679 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200680#ifdef FEAT_SPELL
681 ga_clear(&buf->b_s.b_langp);
682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000683 }
684#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000685 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
686 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687#endif
688#ifdef FEAT_USR_CMDS
689 uc_clear(&buf->b_ucmds); /* clear local user commands */
690#endif
691#ifdef FEAT_SIGNS
692 buf_delete_signs(buf); /* delete any signs */
693#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000694#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200695 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000696#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697#ifdef FEAT_LOCALMAP
698 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
699 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
700#endif
701#ifdef FEAT_MBYTE
702 vim_free(buf->b_start_fenc);
703 buf->b_start_fenc = NULL;
704#endif
705}
706
707/*
708 * Free the b_wininfo list for buffer "buf".
709 */
710 static void
711clear_wininfo(buf)
712 buf_T *buf;
713{
714 wininfo_T *wip;
715
716 while (buf->b_wininfo != NULL)
717 {
718 wip = buf->b_wininfo;
719 buf->b_wininfo = wip->wi_next;
720 if (wip->wi_optset)
721 {
722 clear_winopt(&wip->wi_opt);
723#ifdef FEAT_FOLDING
724 deleteFoldRecurse(&wip->wi_folds);
725#endif
726 }
727 vim_free(wip);
728 }
729}
730
731#if defined(FEAT_LISTCMDS) || defined(PROTO)
732/*
733 * Go to another buffer. Handles the result of the ATTENTION dialog.
734 */
735 void
736goto_buffer(eap, start, dir, count)
737 exarg_T *eap;
738 int start;
739 int dir;
740 int count;
741{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000742# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743 buf_T *old_curbuf = curbuf;
744
745 swap_exists_action = SEA_DIALOG;
746# endif
747 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
748 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000749# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
751 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000752# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
753 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000754
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000755 /* Reset the error/interrupt/exception state here so that
756 * aborting() returns FALSE when closing a window. */
757 enter_cleanup(&cs);
758# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000759
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000760 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 win_close(curwin, TRUE);
762 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000763 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000764
765# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
766 /* Restore the error/interrupt/exception state if not discarded by a
767 * new aborting error, interrupt, or uncaught exception. */
768 leave_cleanup(&cs);
769# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 }
771 else
772 handle_swap_exists(old_curbuf);
773# endif
774}
775#endif
776
Bram Moolenaare64ac772005-12-07 20:54:59 +0000777#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778/*
779 * Handle the situation of swap_exists_action being set.
780 * It is allowed for "old_curbuf" to be NULL or invalid.
781 */
782 void
783handle_swap_exists(old_curbuf)
784 buf_T *old_curbuf;
785{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000786# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
787 cleanup_T cs;
788# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 if (swap_exists_action == SEA_QUIT)
791 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000792# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
793 /* Reset the error/interrupt/exception state here so that
794 * aborting() returns FALSE when closing a buffer. */
795 enter_cleanup(&cs);
796# endif
797
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 /* User selected Quit at ATTENTION prompt. Go back to previous
799 * buffer. If that buffer is gone or the same as the current one,
800 * open a new, empty buffer. */
801 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000802 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100803 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000805 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (old_curbuf != NULL)
807 enter_buffer(old_curbuf);
808 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000809
810# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
811 /* Restore the error/interrupt/exception state if not discarded by a
812 * new aborting error, interrupt, or uncaught exception. */
813 leave_cleanup(&cs);
814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000815 }
816 else if (swap_exists_action == SEA_RECOVER)
817 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000818# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
819 /* Reset the error/interrupt/exception state here so that
820 * aborting() returns FALSE when closing a buffer. */
821 enter_cleanup(&cs);
822# endif
823
Bram Moolenaar071d4272004-06-13 20:20:40 +0000824 /* User selected Recover at ATTENTION prompt. */
825 msg_scroll = TRUE;
826 ml_recover();
827 MSG_PUTS("\n"); /* don't overwrite the last message */
828 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000829 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000830
831# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
832 /* Restore the error/interrupt/exception state if not discarded by a
833 * new aborting error, interrupt, or uncaught exception. */
834 leave_cleanup(&cs);
835# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000836 }
837 swap_exists_action = SEA_NONE;
838}
839#endif
840
841#if defined(FEAT_LISTCMDS) || defined(PROTO)
842/*
843 * do_bufdel() - delete or unload buffer(s)
844 *
845 * addr_count == 0: ":bdel" - delete current buffer
846 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
847 * buffer "end_bnr", then any other arguments.
848 * addr_count == 2: ":N,N bdel" - delete buffers in range
849 *
850 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
851 * DOBUF_DEL (":bdel")
852 *
853 * Returns error message or NULL
854 */
855 char_u *
856do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
857 int command;
858 char_u *arg; /* pointer to extra arguments */
859 int addr_count;
860 int start_bnr; /* first buffer number in a range */
861 int end_bnr; /* buffer nr or last buffer nr in a range */
862 int forceit;
863{
864 int do_current = 0; /* delete current buffer? */
865 int deleted = 0; /* number of buffers deleted */
866 char_u *errormsg = NULL; /* return value */
867 int bnr; /* buffer number */
868 char_u *p;
869
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870 if (addr_count == 0)
871 {
872 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
873 }
874 else
875 {
876 if (addr_count == 2)
877 {
878 if (*arg) /* both range and argument is not allowed */
879 return (char_u *)_(e_trailing);
880 bnr = start_bnr;
881 }
882 else /* addr_count == 1 */
883 bnr = end_bnr;
884
885 for ( ;!got_int; ui_breakcheck())
886 {
887 /*
888 * delete the current buffer last, otherwise when the
889 * current buffer is deleted, the next buffer becomes
890 * the current one and will be loaded, which may then
891 * also be deleted, etc.
892 */
893 if (bnr == curbuf->b_fnum)
894 do_current = bnr;
895 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
896 forceit) == OK)
897 ++deleted;
898
899 /*
900 * find next buffer number to delete/unload
901 */
902 if (addr_count == 2)
903 {
904 if (++bnr > end_bnr)
905 break;
906 }
907 else /* addr_count == 1 */
908 {
909 arg = skipwhite(arg);
910 if (*arg == NUL)
911 break;
912 if (!VIM_ISDIGIT(*arg))
913 {
914 p = skiptowhite_esc(arg);
915 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
916 if (bnr < 0) /* failed */
917 break;
918 arg = p;
919 }
920 else
921 bnr = getdigits(&arg);
922 }
923 }
924 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
925 FORWARD, do_current, forceit) == OK)
926 ++deleted;
927
928 if (deleted == 0)
929 {
930 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000931 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000933 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000935 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 errormsg = IObuff;
937 }
938 else if (deleted >= p_report)
939 {
940 if (command == DOBUF_UNLOAD)
941 {
942 if (deleted == 1)
943 MSG(_("1 buffer unloaded"));
944 else
945 smsg((char_u *)_("%d buffers unloaded"), deleted);
946 }
947 else if (command == DOBUF_DEL)
948 {
949 if (deleted == 1)
950 MSG(_("1 buffer deleted"));
951 else
952 smsg((char_u *)_("%d buffers deleted"), deleted);
953 }
954 else
955 {
956 if (deleted == 1)
957 MSG(_("1 buffer wiped out"));
958 else
959 smsg((char_u *)_("%d buffers wiped out"), deleted);
960 }
961 }
962 }
963
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964
965 return errormsg;
966}
967
968/*
969 * Implementation of the commands for the buffer list.
970 *
971 * action == DOBUF_GOTO go to specified buffer
972 * action == DOBUF_SPLIT split window and go to specified buffer
973 * action == DOBUF_UNLOAD unload specified buffer(s)
974 * action == DOBUF_DEL delete specified buffer(s) from buffer list
975 * action == DOBUF_WIPE delete specified buffer(s) really
976 *
977 * start == DOBUF_CURRENT go to "count" buffer from current buffer
978 * start == DOBUF_FIRST go to "count" buffer from first buffer
979 * start == DOBUF_LAST go to "count" buffer from last buffer
980 * start == DOBUF_MOD go to "count" modified buffer from current buffer
981 *
982 * Return FAIL or OK.
983 */
984 int
985do_buffer(action, start, dir, count, forceit)
986 int action;
987 int start;
988 int dir; /* FORWARD or BACKWARD */
989 int count; /* buffer number or number of buffers */
990 int forceit; /* TRUE for :...! */
991{
992 buf_T *buf;
993 buf_T *bp;
994 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
995 || action == DOBUF_WIPE);
996
997 switch (start)
998 {
999 case DOBUF_FIRST: buf = firstbuf; break;
1000 case DOBUF_LAST: buf = lastbuf; break;
1001 default: buf = curbuf; break;
1002 }
1003 if (start == DOBUF_MOD) /* find next modified buffer */
1004 {
1005 while (count-- > 0)
1006 {
1007 do
1008 {
1009 buf = buf->b_next;
1010 if (buf == NULL)
1011 buf = firstbuf;
1012 }
1013 while (buf != curbuf && !bufIsChanged(buf));
1014 }
1015 if (!bufIsChanged(buf))
1016 {
1017 EMSG(_("E84: No modified buffer found"));
1018 return FAIL;
1019 }
1020 }
1021 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1022 {
1023 while (buf != NULL && buf->b_fnum != count)
1024 buf = buf->b_next;
1025 }
1026 else
1027 {
1028 bp = NULL;
1029 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1030 {
1031 /* remember the buffer where we start, we come back there when all
1032 * buffers are unlisted. */
1033 if (bp == NULL)
1034 bp = buf;
1035 if (dir == FORWARD)
1036 {
1037 buf = buf->b_next;
1038 if (buf == NULL)
1039 buf = firstbuf;
1040 }
1041 else
1042 {
1043 buf = buf->b_prev;
1044 if (buf == NULL)
1045 buf = lastbuf;
1046 }
1047 /* don't count unlisted buffers */
1048 if (unload || buf->b_p_bl)
1049 {
1050 --count;
1051 bp = NULL; /* use this buffer as new starting point */
1052 }
1053 if (bp == buf)
1054 {
1055 /* back where we started, didn't find anything. */
1056 EMSG(_("E85: There is no listed buffer"));
1057 return FAIL;
1058 }
1059 }
1060 }
1061
1062 if (buf == NULL) /* could not find it */
1063 {
1064 if (start == DOBUF_FIRST)
1065 {
1066 /* don't warn when deleting */
1067 if (!unload)
1068 EMSGN(_("E86: Buffer %ld does not exist"), count);
1069 }
1070 else if (dir == FORWARD)
1071 EMSG(_("E87: Cannot go beyond last buffer"));
1072 else
1073 EMSG(_("E88: Cannot go before first buffer"));
1074 return FAIL;
1075 }
1076
1077#ifdef FEAT_GUI
1078 need_mouse_correct = TRUE;
1079#endif
1080
1081#ifdef FEAT_LISTCMDS
1082 /*
1083 * delete buffer buf from memory and/or the list
1084 */
1085 if (unload)
1086 {
1087 int forward;
1088 int retval;
1089
1090 /* When unloading or deleting a buffer that's already unloaded and
1091 * unlisted: fail silently. */
1092 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1093 return FAIL;
1094
1095 if (!forceit && bufIsChanged(buf))
1096 {
1097#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1098 if ((p_confirm || cmdmod.confirm) && p_write)
1099 {
1100 dialog_changed(buf, FALSE);
1101# ifdef FEAT_AUTOCMD
1102 if (!buf_valid(buf))
1103 /* Autocommand deleted buffer, oops! It's not changed
1104 * now. */
1105 return FAIL;
1106# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001107 /* If it's still changed fail silently, the dialog already
1108 * mentioned why it fails. */
1109 if (bufIsChanged(buf))
1110 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001112 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#endif
1114 {
1115 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1116 buf->b_fnum);
1117 return FAIL;
1118 }
1119 }
1120
1121 /*
1122 * If deleting the last (listed) buffer, make it empty.
1123 * The last (listed) buffer cannot be unloaded.
1124 */
1125 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1126 if (bp->b_p_bl && bp != buf)
1127 break;
1128 if (bp == NULL && buf == curbuf)
1129 {
1130 if (action == DOBUF_UNLOAD)
1131 {
1132 EMSG(_("E90: Cannot unload last buffer"));
1133 return FAIL;
1134 }
1135
1136 /* Close any other windows on this buffer, then make it empty. */
1137#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001138 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139#endif
1140 setpcmark();
1141 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001142 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143
1144 /*
1145 * do_ecmd() may create a new buffer, then we have to delete
1146 * the old one. But do_ecmd() may have done that already, check
1147 * if the buffer still exists.
1148 */
1149 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001150 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 return retval;
1152 }
1153
1154#ifdef FEAT_WINDOWS
1155 /*
1156 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001157 * (unless it's the only window). Repeat this so long as we end up in
1158 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001160 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001161# ifdef FEAT_AUTOCMD
1162 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1163# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001164 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 win_close(curwin, FALSE);
1166#endif
1167
1168 /*
1169 * If the buffer to be deleted is not the current one, delete it here.
1170 */
1171 if (buf != curbuf)
1172 {
1173#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001174 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175#endif
1176 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001177 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 return OK;
1179 }
1180
1181 /*
1182 * Deleting the current buffer: Need to find another buffer to go to.
1183 * There must be another, otherwise it would have been handled above.
1184 * First use au_new_curbuf, if it is valid.
1185 * Then prefer the buffer we most recently visited.
1186 * Else try to find one that is loaded, after the current buffer,
1187 * then before the current buffer.
1188 * Finally use any buffer.
1189 */
1190 buf = NULL; /* selected buffer */
1191 bp = NULL; /* used when no loaded buffer found */
1192#ifdef FEAT_AUTOCMD
1193 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1194 buf = au_new_curbuf;
1195# ifdef FEAT_JUMPLIST
1196 else
1197# endif
1198#endif
1199#ifdef FEAT_JUMPLIST
1200 if (curwin->w_jumplistlen > 0)
1201 {
1202 int jumpidx;
1203
1204 jumpidx = curwin->w_jumplistidx - 1;
1205 if (jumpidx < 0)
1206 jumpidx = curwin->w_jumplistlen - 1;
1207
1208 forward = jumpidx;
1209 while (jumpidx != curwin->w_jumplistidx)
1210 {
1211 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1212 if (buf != NULL)
1213 {
1214 if (buf == curbuf || !buf->b_p_bl)
1215 buf = NULL; /* skip current and unlisted bufs */
1216 else if (buf->b_ml.ml_mfp == NULL)
1217 {
1218 /* skip unloaded buf, but may keep it for later */
1219 if (bp == NULL)
1220 bp = buf;
1221 buf = NULL;
1222 }
1223 }
1224 if (buf != NULL) /* found a valid buffer: stop searching */
1225 break;
1226 /* advance to older entry in jump list */
1227 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1228 break;
1229 if (--jumpidx < 0)
1230 jumpidx = curwin->w_jumplistlen - 1;
1231 if (jumpidx == forward) /* List exhausted for sure */
1232 break;
1233 }
1234 }
1235#endif
1236
1237 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1238 {
1239 forward = TRUE;
1240 buf = curbuf->b_next;
1241 for (;;)
1242 {
1243 if (buf == NULL)
1244 {
1245 if (!forward) /* tried both directions */
1246 break;
1247 buf = curbuf->b_prev;
1248 forward = FALSE;
1249 continue;
1250 }
1251 /* in non-help buffer, try to skip help buffers, and vv */
1252 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1253 {
1254 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1255 break;
1256 if (bp == NULL) /* remember unloaded buf for later */
1257 bp = buf;
1258 }
1259 if (forward)
1260 buf = buf->b_next;
1261 else
1262 buf = buf->b_prev;
1263 }
1264 }
1265 if (buf == NULL) /* No loaded buffer, use unloaded one */
1266 buf = bp;
1267 if (buf == NULL) /* No loaded buffer, find listed one */
1268 {
1269 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1270 if (buf->b_p_bl && buf != curbuf)
1271 break;
1272 }
1273 if (buf == NULL) /* Still no buffer, just take one */
1274 {
1275 if (curbuf->b_next != NULL)
1276 buf = curbuf->b_next;
1277 else
1278 buf = curbuf->b_prev;
1279 }
1280 }
1281
1282 /*
1283 * make buf current buffer
1284 */
1285 if (action == DOBUF_SPLIT) /* split window first */
1286 {
1287# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001288 /* If 'switchbuf' contains "useopen": jump to first window containing
1289 * "buf" if one exists */
1290 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001291 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001292 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001293 * page containing "buf" if one exists */
1294 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 return OK;
1296 if (win_split(0, 0) == FAIL)
1297# endif
1298 return FAIL;
1299 }
1300#endif
1301
1302 /* go to current buffer - nothing to do */
1303 if (buf == curbuf)
1304 return OK;
1305
1306 /*
1307 * Check if the current buffer may be abandoned.
1308 */
1309 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1310 {
1311#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1312 if ((p_confirm || cmdmod.confirm) && p_write)
1313 {
1314 dialog_changed(curbuf, FALSE);
1315# ifdef FEAT_AUTOCMD
1316 if (!buf_valid(buf))
1317 /* Autocommand deleted buffer, oops! */
1318 return FAIL;
1319# endif
1320 }
1321 if (bufIsChanged(curbuf))
1322#endif
1323 {
1324 EMSG(_(e_nowrtmsg));
1325 return FAIL;
1326 }
1327 }
1328
1329 /* Go to the other buffer. */
1330 set_curbuf(buf, action);
1331
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001332#if defined(FEAT_LISTCMDS) \
1333 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001335 {
1336 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1337 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338#endif
1339
1340#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1341 if (aborting()) /* autocmds may abort script processing */
1342 return FAIL;
1343#endif
1344
1345 return OK;
1346}
1347
1348#endif /* FEAT_LISTCMDS */
1349
1350/*
1351 * Set current buffer to "buf". Executes autocommands and closes current
1352 * buffer. "action" tells how to close the current buffer:
1353 * DOBUF_GOTO free or hide it
1354 * DOBUF_SPLIT nothing
1355 * DOBUF_UNLOAD unload it
1356 * DOBUF_DEL delete it
1357 * DOBUF_WIPE wipe it out
1358 */
1359 void
1360set_curbuf(buf, action)
1361 buf_T *buf;
1362 int action;
1363{
1364 buf_T *prevbuf;
1365 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1366 || action == DOBUF_WIPE);
1367
1368 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001369 if (!cmdmod.keepalt)
1370 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001371 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372
1373#ifdef FEAT_VISUAL
1374 /* Don't restart Select mode after switching to another buffer. */
1375 VIsual_reselect = FALSE;
1376#endif
1377
1378 /* close_windows() or apply_autocmds() may change curbuf */
1379 prevbuf = curbuf;
1380
1381#ifdef FEAT_AUTOCMD
1382 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1383# ifdef FEAT_EVAL
1384 if (buf_valid(prevbuf) && !aborting())
1385# else
1386 if (buf_valid(prevbuf))
1387# endif
1388#endif
1389 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001390#ifdef FEAT_SYN_HL
1391 if (prevbuf == curwin->w_buffer)
1392 reset_synblock(curwin);
1393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394#ifdef FEAT_WINDOWS
1395 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001396 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#endif
1398#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1399 if (buf_valid(prevbuf) && !aborting())
1400#else
1401 if (buf_valid(prevbuf))
1402#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001403 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001404#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001405 win_T *previouswin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001406#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001407 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001408 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1410 unload ? action : (action == DOBUF_GOTO
1411 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001412 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001413#ifdef FEAT_WINDOWS
Bram Moolenaare25865a2012-07-06 16:22:02 +02001414 if (curwin != previouswin && win_valid(previouswin))
Bram Moolenaar9e931222012-06-20 11:55:01 +02001415 /* autocommands changed curwin, Grr! */
Bram Moolenaare25865a2012-07-06 16:22:02 +02001416 curwin = previouswin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001417#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 }
1420#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001421 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001422 * it did ":bunload") or aborted the script processing!
1423 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1424 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001425# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001426 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001427# endif
1428# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001429 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001430# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001431 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432#endif
1433 enter_buffer(buf);
1434}
1435
1436/*
1437 * Enter a new current buffer.
1438 * Old curbuf must have been abandoned already!
1439 */
1440 void
1441enter_buffer(buf)
1442 buf_T *buf;
1443{
Bram Moolenaar9c2e6cc2013-02-06 12:14:48 +01001444 long old_tw = curbuf->b_p_tw;
1445
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 /* Copy buffer and window local option values. Not for a help buffer. */
1447 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1448 if (!buf->b_help)
1449 get_winopts(buf);
1450#ifdef FEAT_FOLDING
1451 else
1452 /* Remove all folds in the window. */
1453 clearFolding(curwin);
1454 foldUpdateAll(curwin); /* update folds (later). */
1455#endif
1456
1457 /* Get the buffer in the current window. */
1458 curwin->w_buffer = buf;
1459 curbuf = buf;
1460 ++curbuf->b_nwindows;
1461
1462#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001463 if (curwin->w_p_diff)
1464 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001465#endif
1466
Bram Moolenaara971b822011-09-14 14:43:25 +02001467#ifdef FEAT_SYN_HL
1468 curwin->w_s = &(buf->b_s);
Bram Moolenaar9c2e6cc2013-02-06 12:14:48 +01001469 if (old_tw != buf->b_p_tw)
1470 check_colorcolumn(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +02001471#endif
1472
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 /* Cursor on first line by default. */
1474 curwin->w_cursor.lnum = 1;
1475 curwin->w_cursor.col = 0;
1476#ifdef FEAT_VIRTUALEDIT
1477 curwin->w_cursor.coladd = 0;
1478#endif
1479 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001480#ifdef FEAT_AUTOCMD
1481 curwin->w_topline_was_set = FALSE;
1482#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001484 /* mark cursor position as being invalid */
1485 curwin->w_valid = 0;
1486
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 /* Make sure the buffer is loaded. */
1488 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001489 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001490#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001491 /* If there is no filetype, allow for detecting one. Esp. useful for
1492 * ":ball" used in a autocommand. If there already is a filetype we
1493 * might prefer to keep it. */
1494 if (*curbuf->b_p_ft == NUL)
1495 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001496#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001497
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001498 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 else
1501 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001502 if (!msg_silent)
1503 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1505#ifdef FEAT_AUTOCMD
1506 curwin->w_topline = 1;
1507# ifdef FEAT_DIFF
1508 curwin->w_topfill = 0;
1509# endif
1510 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1511 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1512#endif
1513 }
1514
1515 /* If autocommands did not change the cursor position, restore cursor lnum
1516 * and possibly cursor col. */
1517 if (curwin->w_cursor.lnum == 1 && inindent(0))
1518 buflist_getfpos();
1519
1520 check_arg_idx(curwin); /* check for valid arg_idx */
1521#ifdef FEAT_TITLE
1522 maketitle();
1523#endif
1524#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001525 /* when autocmds didn't change it */
1526 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001527#endif
1528 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1529
Bram Moolenaar009b2592004-10-24 19:18:58 +00001530#ifdef FEAT_NETBEANS_INTG
1531 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001532 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001533#endif
1534
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001535 /* Change directories when the 'acd' option is set. */
1536 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537
1538#ifdef FEAT_KEYMAP
1539 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001540 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001542#ifdef FEAT_SPELL
1543 /* May need to set the spell language. Can only do this after the buffer
1544 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001545 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1546 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001547#endif
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 redraw_later(NOT_VALID);
1550}
1551
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001552#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1553/*
1554 * Change to the directory of the current buffer.
1555 */
1556 void
1557do_autochdir()
1558{
1559 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1560 shorten_fnames(TRUE);
1561}
1562#endif
1563
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564/*
1565 * functions for dealing with the buffer list
1566 */
1567
1568/*
1569 * Add a file name to the buffer list. Return a pointer to the buffer.
1570 * If the same file name already exists return a pointer to that buffer.
1571 * If it does not exist, or if fname == NULL, a new entry is created.
1572 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1573 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1574 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 * This is the ONLY way to create a new buffer.
1576 */
1577static int top_file_num = 1; /* highest file number */
1578
1579 buf_T *
1580buflist_new(ffname, sfname, lnum, flags)
1581 char_u *ffname; /* full path of fname or relative */
1582 char_u *sfname; /* short fname or NULL */
1583 linenr_T lnum; /* preferred cursor line */
1584 int flags; /* BLN_ defines */
1585{
1586 buf_T *buf;
1587#ifdef UNIX
1588 struct stat st;
1589#endif
1590
1591 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1592
1593 /*
1594 * If file name already exists in the list, update the entry.
1595 */
1596#ifdef UNIX
1597 /* On Unix we can use inode numbers when the file exists. Works better
1598 * for hard links. */
1599 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1600 st.st_dev = (dev_T)-1;
1601#endif
1602 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1603#ifdef UNIX
1604 buflist_findname_stat(ffname, &st)
1605#else
1606 buflist_findname(ffname)
1607#endif
1608 ) != NULL)
1609 {
1610 vim_free(ffname);
1611 if (lnum != 0)
1612 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1613 /* copy the options now, if 'cpo' doesn't have 's' and not done
1614 * already */
1615 buf_copy_options(buf, 0);
1616 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1617 {
1618 buf->b_p_bl = TRUE;
1619#ifdef FEAT_AUTOCMD
1620 if (!(flags & BLN_DUMMY))
1621 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1622#endif
1623 }
1624 return buf;
1625 }
1626
1627 /*
1628 * If the current buffer has no name and no contents, use the current
1629 * buffer. Otherwise: Need to allocate a new buffer structure.
1630 *
1631 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001632 * (A spell file buffer is allocated in spell.c, but that's not a normal
1633 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 */
1635 buf = NULL;
1636 if ((flags & BLN_CURBUF)
1637 && curbuf != NULL
1638 && curbuf->b_ffname == NULL
1639 && curbuf->b_nwindows <= 1
1640 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1641 {
1642 buf = curbuf;
1643#ifdef FEAT_AUTOCMD
1644 /* It's like this buffer is deleted. Watch out for autocommands that
1645 * change curbuf! If that happens, allocate a new buffer anyway. */
1646 if (curbuf->b_p_bl)
1647 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1648 if (buf == curbuf)
1649 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1650# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001651 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 return NULL;
1653# endif
1654#endif
1655#ifdef FEAT_QUICKFIX
1656# ifdef FEAT_AUTOCMD
1657 if (buf == curbuf)
1658# endif
1659 {
1660 /* Make sure 'bufhidden' and 'buftype' are empty */
1661 clear_string_option(&buf->b_p_bh);
1662 clear_string_option(&buf->b_p_bt);
1663 }
1664#endif
1665 }
1666 if (buf != curbuf || curbuf == NULL)
1667 {
1668 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1669 if (buf == NULL)
1670 {
1671 vim_free(ffname);
1672 return NULL;
1673 }
1674 }
1675
1676 if (ffname != NULL)
1677 {
1678 buf->b_ffname = ffname;
1679 buf->b_sfname = vim_strsave(sfname);
1680 }
1681
1682 clear_wininfo(buf);
1683 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1684
1685 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1686 || buf->b_wininfo == NULL)
1687 {
1688 vim_free(buf->b_ffname);
1689 buf->b_ffname = NULL;
1690 vim_free(buf->b_sfname);
1691 buf->b_sfname = NULL;
1692 if (buf != curbuf)
1693 free_buffer(buf);
1694 return NULL;
1695 }
1696
1697 if (buf == curbuf)
1698 {
1699 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001700 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001701 if (buf != curbuf) /* autocommands deleted the buffer! */
1702 return NULL;
1703#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001704 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705 return NULL;
1706#endif
1707 /* buf->b_nwindows = 0; why was this here? */
1708 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
Bram Moolenaar0ac24e12012-11-20 12:16:58 +01001709
1710 /* Init the options. */
1711 buf->b_p_initialized = FALSE;
1712 buf_copy_options(buf, BCO_ENTER);
1713
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714#ifdef FEAT_KEYMAP
1715 /* need to reload lmaps and set b:keymap_name */
1716 curbuf->b_kmap_state |= KEYMAP_INIT;
1717#endif
1718 }
1719 else
1720 {
1721 /*
1722 * put new buffer at the end of the buffer list
1723 */
1724 buf->b_next = NULL;
1725 if (firstbuf == NULL) /* buffer list is empty */
1726 {
1727 buf->b_prev = NULL;
1728 firstbuf = buf;
1729 }
1730 else /* append new buffer at end of list */
1731 {
1732 lastbuf->b_next = buf;
1733 buf->b_prev = lastbuf;
1734 }
1735 lastbuf = buf;
1736
1737 buf->b_fnum = top_file_num++;
1738 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1739 {
1740 EMSG(_("W14: Warning: List of file names overflow"));
1741 if (emsg_silent == 0)
1742 {
1743 out_flush();
1744 ui_delay(3000L, TRUE); /* make sure it is noticed */
1745 }
1746 top_file_num = 1;
1747 }
1748
1749 /*
1750 * Always copy the options from the current buffer.
1751 */
1752 buf_copy_options(buf, BCO_ALWAYS);
1753 }
1754
1755 buf->b_wininfo->wi_fpos.lnum = lnum;
1756 buf->b_wininfo->wi_win = curwin;
1757
1758#ifdef FEAT_EVAL
Bram Moolenaarbdb62052012-07-16 17:31:53 +02001759 /* init b: variables */
1760 init_var_dict(&buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001761#endif
1762#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001763 hash_init(&buf->b_s.b_keywtab);
1764 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765#endif
1766
1767 buf->b_fname = buf->b_sfname;
1768#ifdef UNIX
1769 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001770 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771 else
1772 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001773 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 buf->b_dev = st.st_dev;
1775 buf->b_ino = st.st_ino;
1776 }
1777#endif
1778 buf->b_u_synced = TRUE;
1779 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001780 if (flags & BLN_DUMMY)
1781 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 buf_clear_file(buf);
1783 clrallmarks(buf); /* clear marks */
1784 fmarks_check_names(buf); /* check file marks for this file */
1785 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1786#ifdef FEAT_AUTOCMD
1787 if (!(flags & BLN_DUMMY))
1788 {
1789 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1790 if (flags & BLN_LISTED)
1791 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1792# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001793 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 return NULL;
1795# endif
1796 }
1797#endif
1798
1799 return buf;
1800}
1801
1802/*
1803 * Free the memory for the options of a buffer.
1804 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1805 * 'fileencoding'.
1806 */
1807 void
1808free_buf_options(buf, free_p_ff)
1809 buf_T *buf;
1810 int free_p_ff;
1811{
1812 if (free_p_ff)
1813 {
1814#ifdef FEAT_MBYTE
1815 clear_string_option(&buf->b_p_fenc);
1816#endif
1817 clear_string_option(&buf->b_p_ff);
1818#ifdef FEAT_QUICKFIX
1819 clear_string_option(&buf->b_p_bh);
1820 clear_string_option(&buf->b_p_bt);
1821#endif
1822 }
1823#ifdef FEAT_FIND_ID
1824 clear_string_option(&buf->b_p_def);
1825 clear_string_option(&buf->b_p_inc);
1826# ifdef FEAT_EVAL
1827 clear_string_option(&buf->b_p_inex);
1828# endif
1829#endif
1830#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1831 clear_string_option(&buf->b_p_inde);
1832 clear_string_option(&buf->b_p_indk);
1833#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001834#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1835 clear_string_option(&buf->b_p_bexpr);
1836#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001837#if defined(FEAT_CRYPT)
1838 clear_string_option(&buf->b_p_cm);
1839#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001840#if defined(FEAT_EVAL)
1841 clear_string_option(&buf->b_p_fex);
1842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843#ifdef FEAT_CRYPT
1844 clear_string_option(&buf->b_p_key);
1845#endif
1846 clear_string_option(&buf->b_p_kp);
1847 clear_string_option(&buf->b_p_mps);
1848 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001849 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 clear_string_option(&buf->b_p_isk);
1851#ifdef FEAT_KEYMAP
1852 clear_string_option(&buf->b_p_keymap);
1853 ga_clear(&buf->b_kmap_ga);
1854#endif
1855#ifdef FEAT_COMMENTS
1856 clear_string_option(&buf->b_p_com);
1857#endif
1858#ifdef FEAT_FOLDING
1859 clear_string_option(&buf->b_p_cms);
1860#endif
1861 clear_string_option(&buf->b_p_nf);
1862#ifdef FEAT_SYN_HL
1863 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001864#endif
1865#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001866 clear_string_option(&buf->b_s.b_p_spc);
1867 clear_string_option(&buf->b_s.b_p_spf);
1868 vim_free(buf->b_s.b_cap_prog);
1869 buf->b_s.b_cap_prog = NULL;
1870 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#endif
1872#ifdef FEAT_SEARCHPATH
1873 clear_string_option(&buf->b_p_sua);
1874#endif
1875#ifdef FEAT_AUTOCMD
1876 clear_string_option(&buf->b_p_ft);
1877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878#ifdef FEAT_CINDENT
1879 clear_string_option(&buf->b_p_cink);
1880 clear_string_option(&buf->b_p_cino);
1881#endif
1882#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1883 clear_string_option(&buf->b_p_cinw);
1884#endif
1885#ifdef FEAT_INS_EXPAND
1886 clear_string_option(&buf->b_p_cpt);
1887#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001888#ifdef FEAT_COMPL_FUNC
1889 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001890 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892#ifdef FEAT_QUICKFIX
1893 clear_string_option(&buf->b_p_gp);
1894 clear_string_option(&buf->b_p_mp);
1895 clear_string_option(&buf->b_p_efm);
1896#endif
1897 clear_string_option(&buf->b_p_ep);
1898 clear_string_option(&buf->b_p_path);
1899 clear_string_option(&buf->b_p_tags);
1900#ifdef FEAT_INS_EXPAND
1901 clear_string_option(&buf->b_p_dict);
1902 clear_string_option(&buf->b_p_tsr);
1903#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001904#ifdef FEAT_TEXTOBJ
1905 clear_string_option(&buf->b_p_qe);
1906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907 buf->b_p_ar = -1;
1908}
1909
1910/*
1911 * get alternate file n
1912 * set linenr to lnum or altfpos.lnum if lnum == 0
1913 * also set cursor column to altfpos.col if 'startofline' is not set.
1914 * if (options & GETF_SETMARK) call setpcmark()
1915 * if (options & GETF_ALT) we are jumping to an alternate file.
1916 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1917 *
1918 * return FAIL for failure, OK for success
1919 */
1920 int
1921buflist_getfile(n, lnum, options, forceit)
1922 int n;
1923 linenr_T lnum;
1924 int options;
1925 int forceit;
1926{
1927 buf_T *buf;
1928#ifdef FEAT_WINDOWS
1929 win_T *wp = NULL;
1930#endif
1931 pos_T *fpos;
1932 colnr_T col;
1933
1934 buf = buflist_findnr(n);
1935 if (buf == NULL)
1936 {
1937 if ((options & GETF_ALT) && n == 0)
1938 EMSG(_(e_noalt));
1939 else
1940 EMSGN(_("E92: Buffer %ld not found"), n);
1941 return FAIL;
1942 }
1943
1944 /* if alternate file is the current buffer, nothing to do */
1945 if (buf == curbuf)
1946 return OK;
1947
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001948 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001949 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001950 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001952 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001953#ifdef FEAT_AUTOCMD
1954 if (curbuf_locked())
1955 return FAIL;
1956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957
1958 /* altfpos may be changed by getfile(), get it now */
1959 if (lnum == 0)
1960 {
1961 fpos = buflist_findfpos(buf);
1962 lnum = fpos->lnum;
1963 col = fpos->col;
1964 }
1965 else
1966 col = 0;
1967
1968#ifdef FEAT_WINDOWS
1969 if (options & GETF_SWITCH)
1970 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001971 /* If 'switchbuf' contains "useopen": jump to first window containing
1972 * "buf" if one exists */
1973 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001975 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1976 * page containing "buf" if one exists */
1977 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001978 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001979 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1980 * isn't empty: open new window */
1981 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001983 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1984 tabpage_new();
1985 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001987 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 }
1989 }
1990#endif
1991
1992 ++RedrawingDisabled;
1993 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1994 lnum, forceit) <= 0)
1995 {
1996 --RedrawingDisabled;
1997
1998 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1999 if (!p_sol && col != 0)
2000 {
2001 curwin->w_cursor.col = col;
2002 check_cursor_col();
2003#ifdef FEAT_VIRTUALEDIT
2004 curwin->w_cursor.coladd = 0;
2005#endif
2006 curwin->w_set_curswant = TRUE;
2007 }
2008 return OK;
2009 }
2010 --RedrawingDisabled;
2011 return FAIL;
2012}
2013
2014/*
2015 * go to the last know line number for the current buffer
2016 */
2017 void
2018buflist_getfpos()
2019{
2020 pos_T *fpos;
2021
2022 fpos = buflist_findfpos(curbuf);
2023
2024 curwin->w_cursor.lnum = fpos->lnum;
2025 check_cursor_lnum();
2026
2027 if (p_sol)
2028 curwin->w_cursor.col = 0;
2029 else
2030 {
2031 curwin->w_cursor.col = fpos->col;
2032 check_cursor_col();
2033#ifdef FEAT_VIRTUALEDIT
2034 curwin->w_cursor.coladd = 0;
2035#endif
2036 curwin->w_set_curswant = TRUE;
2037 }
2038}
2039
Bram Moolenaar81695252004-12-29 20:58:21 +00002040#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2041/*
2042 * Find file in buffer list by name (it has to be for the current window).
2043 * Returns NULL if not found.
2044 */
2045 buf_T *
2046buflist_findname_exp(fname)
2047 char_u *fname;
2048{
2049 char_u *ffname;
2050 buf_T *buf = NULL;
2051
2052 /* First make the name into a full path name */
2053 ffname = FullName_save(fname,
2054#ifdef UNIX
2055 TRUE /* force expansion, get rid of symbolic links */
2056#else
2057 FALSE
2058#endif
2059 );
2060 if (ffname != NULL)
2061 {
2062 buf = buflist_findname(ffname);
2063 vim_free(ffname);
2064 }
2065 return buf;
2066}
2067#endif
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069/*
2070 * Find file in buffer list by name (it has to be for the current window).
2071 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002072 * Skips dummy buffers.
2073 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002074 */
2075 buf_T *
2076buflist_findname(ffname)
2077 char_u *ffname;
2078{
2079#ifdef UNIX
2080 struct stat st;
2081
2082 if (mch_stat((char *)ffname, &st) < 0)
2083 st.st_dev = (dev_T)-1;
2084 return buflist_findname_stat(ffname, &st);
2085}
2086
2087/*
2088 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2089 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002090 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 */
2092 static buf_T *
2093buflist_findname_stat(ffname, stp)
2094 char_u *ffname;
2095 struct stat *stp;
2096{
2097#endif
2098 buf_T *buf;
2099
2100 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002101 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102#ifdef UNIX
2103 , stp
2104#endif
2105 ))
2106 return buf;
2107 return NULL;
2108}
2109
2110#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2111/*
2112 * Find file in buffer list by a regexp pattern.
2113 * Return fnum of the found buffer.
2114 * Return < 0 for error.
2115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116 int
2117buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2118 char_u *pattern;
2119 char_u *pattern_end; /* pointer to first char after pattern */
2120 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002121 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122{
2123 buf_T *buf;
2124 regprog_T *prog;
2125 int match = -1;
2126 int find_listed;
2127 char_u *pat;
2128 char_u *patend;
2129 int attempt;
2130 char_u *p;
2131 int toggledollar;
2132
2133 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2134 {
2135 if (*pattern == '%')
2136 match = curbuf->b_fnum;
2137 else
2138 match = curwin->w_alt_fnum;
2139#ifdef FEAT_DIFF
2140 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2141 match = -1;
2142#endif
2143 }
2144
2145 /*
2146 * Try four ways of matching a listed buffer:
2147 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002148 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 * attempt == 2: with '$' at end (only match at end)
2150 * attempt == 3: with '^' at start and '$' at end (only full match)
2151 * Repeat this for finding an unlisted buffer if there was no matching
2152 * listed buffer.
2153 */
2154 else
2155 {
2156 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2157 if (pat == NULL)
2158 return -1;
2159 patend = pat + STRLEN(pat) - 1;
2160 toggledollar = (patend > pat && *patend == '$');
2161
2162 /* First try finding a listed buffer. If not found and "unlisted"
2163 * is TRUE, try finding an unlisted buffer. */
2164 find_listed = TRUE;
2165 for (;;)
2166 {
2167 for (attempt = 0; attempt <= 3; ++attempt)
2168 {
2169 /* may add '^' and '$' */
2170 if (toggledollar)
2171 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2172 p = pat;
2173 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2174 ++p;
2175 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2176 if (prog == NULL)
2177 {
2178 vim_free(pat);
2179 return -1;
2180 }
2181
2182 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2183 if (buf->b_p_bl == find_listed
2184#ifdef FEAT_DIFF
2185 && (!diffmode || diff_mode_buf(buf))
2186#endif
2187 && buflist_match(prog, buf) != NULL)
2188 {
2189 if (match >= 0) /* already found a match */
2190 {
2191 match = -2;
2192 break;
2193 }
2194 match = buf->b_fnum; /* remember first match */
2195 }
2196
2197 vim_free(prog);
2198 if (match >= 0) /* found one match */
2199 break;
2200 }
2201
2202 /* Only search for unlisted buffers if there was no match with
2203 * a listed buffer. */
2204 if (!unlisted || !find_listed || match != -1)
2205 break;
2206 find_listed = FALSE;
2207 }
2208
2209 vim_free(pat);
2210 }
2211
2212 if (match == -2)
2213 EMSG2(_("E93: More than one match for %s"), pattern);
2214 else if (match < 0)
2215 EMSG2(_("E94: No matching buffer for %s"), pattern);
2216 return match;
2217}
2218#endif
2219
2220#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2221
2222/*
2223 * Find all buffer names that match.
2224 * For command line expansion of ":buf" and ":sbuf".
2225 * Return OK if matches found, FAIL otherwise.
2226 */
2227 int
2228ExpandBufnames(pat, num_file, file, options)
2229 char_u *pat;
2230 int *num_file;
2231 char_u ***file;
2232 int options;
2233{
2234 int count = 0;
2235 buf_T *buf;
2236 int round;
2237 char_u *p;
2238 int attempt;
2239 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002240 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241
2242 *num_file = 0; /* return values in case of FAIL */
2243 *file = NULL;
2244
Bram Moolenaar05159a02005-02-26 23:04:13 +00002245 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2246 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002248 patc = alloc((unsigned)STRLEN(pat) + 11);
2249 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002251 STRCPY(patc, "\\(^\\|[\\/]\\)");
2252 STRCPY(patc + 11, pat + 1);
2253 }
2254 else
2255 patc = pat;
2256
2257 /*
2258 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002259 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002260 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002261 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002262 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002263 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002264 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002265 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002266 if (prog == NULL)
2267 {
2268 if (patc != pat)
2269 vim_free(patc);
2270 return FAIL;
2271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
2273 /*
2274 * round == 1: Count the matches.
2275 * round == 2: Build the array to keep the matches.
2276 */
2277 for (round = 1; round <= 2; ++round)
2278 {
2279 count = 0;
2280 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2281 {
2282 if (!buf->b_p_bl) /* skip unlisted buffers */
2283 continue;
2284 p = buflist_match(prog, buf);
2285 if (p != NULL)
2286 {
2287 if (round == 1)
2288 ++count;
2289 else
2290 {
2291 if (options & WILD_HOME_REPLACE)
2292 p = home_replace_save(buf, p);
2293 else
2294 p = vim_strsave(p);
2295 (*file)[count++] = p;
2296 }
2297 }
2298 }
2299 if (count == 0) /* no match found, break here */
2300 break;
2301 if (round == 1)
2302 {
2303 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2304 if (*file == NULL)
2305 {
2306 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002307 if (patc != pat)
2308 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 return FAIL;
2310 }
2311 }
2312 }
2313 vim_free(prog);
2314 if (count) /* match(es) found, break here */
2315 break;
2316 }
2317
Bram Moolenaar05159a02005-02-26 23:04:13 +00002318 if (patc != pat)
2319 vim_free(patc);
2320
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 *num_file = count;
2322 return (count == 0 ? FAIL : OK);
2323}
2324
2325#endif /* FEAT_CMDL_COMPL */
2326
2327#ifdef HAVE_BUFLIST_MATCH
2328/*
2329 * Check for a match on the file name for buffer "buf" with regprog "prog".
2330 */
2331 static char_u *
2332buflist_match(prog, buf)
2333 regprog_T *prog;
2334 buf_T *buf;
2335{
2336 char_u *match;
2337
2338 /* First try the short file name, then the long file name. */
2339 match = fname_match(prog, buf->b_sfname);
2340 if (match == NULL)
2341 match = fname_match(prog, buf->b_ffname);
2342
2343 return match;
2344}
2345
2346/*
2347 * Try matching the regexp in "prog" with file name "name".
2348 * Return "name" when there is a match, NULL when not.
2349 */
2350 static char_u *
2351fname_match(prog, name)
2352 regprog_T *prog;
2353 char_u *name;
2354{
2355 char_u *match = NULL;
2356 char_u *p;
2357 regmatch_T regmatch;
2358
2359 if (name != NULL)
2360 {
2361 regmatch.regprog = prog;
2362#ifdef CASE_INSENSITIVE_FILENAME
2363 regmatch.rm_ic = TRUE; /* Always ignore case */
2364#else
2365 regmatch.rm_ic = FALSE; /* Never ignore case */
2366#endif
2367
2368 if (vim_regexec(&regmatch, name, (colnr_T)0))
2369 match = name;
2370 else
2371 {
2372 /* Replace $(HOME) with '~' and try matching again. */
2373 p = home_replace_save(NULL, name);
2374 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2375 match = name;
2376 vim_free(p);
2377 }
2378 }
2379
2380 return match;
2381}
2382#endif
2383
2384/*
2385 * find file in buffer list by number
2386 */
2387 buf_T *
2388buflist_findnr(nr)
2389 int nr;
2390{
2391 buf_T *buf;
2392
2393 if (nr == 0)
2394 nr = curwin->w_alt_fnum;
2395 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2396 if (buf->b_fnum == nr)
2397 return (buf);
2398 return NULL;
2399}
2400
2401/*
2402 * Get name of file 'n' in the buffer list.
2403 * When the file has no name an empty string is returned.
2404 * home_replace() is used to shorten the file name (used for marks).
2405 * Returns a pointer to allocated memory, of NULL when failed.
2406 */
2407 char_u *
2408buflist_nr2name(n, fullname, helptail)
2409 int n;
2410 int fullname;
2411 int helptail; /* for help buffers return tail only */
2412{
2413 buf_T *buf;
2414
2415 buf = buflist_findnr(n);
2416 if (buf == NULL)
2417 return NULL;
2418 return home_replace_save(helptail ? buf : NULL,
2419 fullname ? buf->b_ffname : buf->b_fname);
2420}
2421
2422/*
2423 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2424 * When "copy_options" is TRUE save the local window option values.
2425 * When "lnum" is 0 only do the options.
2426 */
2427 static void
2428buflist_setfpos(buf, win, lnum, col, copy_options)
2429 buf_T *buf;
2430 win_T *win;
2431 linenr_T lnum;
2432 colnr_T col;
2433 int copy_options;
2434{
2435 wininfo_T *wip;
2436
2437 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2438 if (wip->wi_win == win)
2439 break;
2440 if (wip == NULL)
2441 {
2442 /* allocate a new entry */
2443 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2444 if (wip == NULL)
2445 return;
2446 wip->wi_win = win;
2447 if (lnum == 0) /* set lnum even when it's 0 */
2448 lnum = 1;
2449 }
2450 else
2451 {
2452 /* remove the entry from the list */
2453 if (wip->wi_prev)
2454 wip->wi_prev->wi_next = wip->wi_next;
2455 else
2456 buf->b_wininfo = wip->wi_next;
2457 if (wip->wi_next)
2458 wip->wi_next->wi_prev = wip->wi_prev;
2459 if (copy_options && wip->wi_optset)
2460 {
2461 clear_winopt(&wip->wi_opt);
2462#ifdef FEAT_FOLDING
2463 deleteFoldRecurse(&wip->wi_folds);
2464#endif
2465 }
2466 }
2467 if (lnum != 0)
2468 {
2469 wip->wi_fpos.lnum = lnum;
2470 wip->wi_fpos.col = col;
2471 }
2472 if (copy_options)
2473 {
2474 /* Save the window-specific option values. */
2475 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2476#ifdef FEAT_FOLDING
2477 wip->wi_fold_manual = win->w_fold_manual;
2478 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2479#endif
2480 wip->wi_optset = TRUE;
2481 }
2482
2483 /* insert the entry in front of the list */
2484 wip->wi_next = buf->b_wininfo;
2485 buf->b_wininfo = wip;
2486 wip->wi_prev = NULL;
2487 if (wip->wi_next)
2488 wip->wi_next->wi_prev = wip;
2489
2490 return;
2491}
2492
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002493#ifdef FEAT_DIFF
2494static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2495
2496/*
2497 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2498 * page. That's because a diff is local to a tab page.
2499 */
2500 static int
2501wininfo_other_tab_diff(wip)
2502 wininfo_T *wip;
2503{
2504 win_T *wp;
2505
2506 if (wip->wi_opt.wo_diff)
2507 {
2508 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2509 /* return FALSE when it's a window in the current tab page, thus
2510 * the buffer was in diff mode here */
2511 if (wip->wi_win == wp)
2512 return FALSE;
2513 return TRUE;
2514 }
2515 return FALSE;
2516}
2517#endif
2518
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519/*
2520 * Find info for the current window in buffer "buf".
2521 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002522 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2523 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 * Returns NULL when there isn't any info.
2525 */
2526 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002527find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002529 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530{
2531 wininfo_T *wip;
2532
2533 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002534 if (wip->wi_win == curwin
2535#ifdef FEAT_DIFF
2536 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2537#endif
2538 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002540
2541 /* If no wininfo for curwin, use the first in the list (that doesn't have
2542 * 'diff' set and is in another tab page). */
2543 if (wip == NULL)
2544 {
2545#ifdef FEAT_DIFF
2546 if (skip_diff_buffer)
2547 {
2548 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2549 if (!wininfo_other_tab_diff(wip))
2550 break;
2551 }
2552 else
2553#endif
2554 wip = buf->b_wininfo;
2555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 return wip;
2557}
2558
2559/*
2560 * Reset the local window options to the values last used in this window.
2561 * If the buffer wasn't used in this window before, use the values from
2562 * the most recently used window. If the values were never set, use the
2563 * global values for the window.
2564 */
2565 void
2566get_winopts(buf)
2567 buf_T *buf;
2568{
2569 wininfo_T *wip;
2570
2571 clear_winopt(&curwin->w_onebuf_opt);
2572#ifdef FEAT_FOLDING
2573 clearFolding(curwin);
2574#endif
2575
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002576 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577 if (wip != NULL && wip->wi_optset)
2578 {
2579 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2580#ifdef FEAT_FOLDING
2581 curwin->w_fold_manual = wip->wi_fold_manual;
2582 curwin->w_foldinvalid = TRUE;
2583 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2584#endif
2585 }
2586 else
2587 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2588
2589#ifdef FEAT_FOLDING
2590 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2591 if (p_fdls >= 0)
2592 curwin->w_p_fdl = p_fdls;
2593#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002594#ifdef FEAT_SYN_HL
2595 check_colorcolumn(curwin);
2596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597}
2598
2599/*
2600 * Find the position (lnum and col) for the buffer 'buf' for the current
2601 * window.
2602 * Returns a pointer to no_position if no position is found.
2603 */
2604 pos_T *
2605buflist_findfpos(buf)
2606 buf_T *buf;
2607{
2608 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002609 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002611 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612 if (wip != NULL)
2613 return &(wip->wi_fpos);
2614 else
2615 return &no_position;
2616}
2617
2618/*
2619 * Find the lnum for the buffer 'buf' for the current window.
2620 */
2621 linenr_T
2622buflist_findlnum(buf)
2623 buf_T *buf;
2624{
2625 return buflist_findfpos(buf)->lnum;
2626}
2627
2628#if defined(FEAT_LISTCMDS) || defined(PROTO)
2629/*
2630 * List all know file names (for :files and :buffers command).
2631 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 void
2633buflist_list(eap)
2634 exarg_T *eap;
2635{
2636 buf_T *buf;
2637 int len;
2638 int i;
2639
2640 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2641 {
2642 /* skip unlisted buffers, unless ! was used */
2643 if (!buf->b_p_bl && !eap->forceit)
2644 continue;
2645 msg_putchar('\n');
2646 if (buf_spname(buf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02002647 vim_strncpy(NameBuff, buf_spname(buf), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002648 else
2649 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2650
Bram Moolenaar50cde822005-06-05 21:54:54 +00002651 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 buf->b_fnum,
2653 buf->b_p_bl ? ' ' : 'u',
2654 buf == curbuf ? '%' :
2655 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2656 buf->b_ml.ml_mfp == NULL ? ' ' :
2657 (buf->b_nwindows == 0 ? 'h' : 'a'),
2658 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2659 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002660 : (bufIsChanged(buf) ? '+' : ' '),
2661 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662
2663 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664 i = 40 - vim_strsize(IObuff);
2665 do
2666 {
2667 IObuff[len++] = ' ';
2668 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002669 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2670 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002671 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 msg_outtrans(IObuff);
2673 out_flush(); /* output one line at a time */
2674 ui_breakcheck();
2675 }
2676}
2677#endif
2678
2679/*
2680 * Get file name and line number for file 'fnum'.
2681 * Used by DoOneCmd() for translating '%' and '#'.
2682 * Used by insert_reg() and cmdline_paste() for '#' register.
2683 * Return FAIL if not found, OK for success.
2684 */
2685 int
2686buflist_name_nr(fnum, fname, lnum)
2687 int fnum;
2688 char_u **fname;
2689 linenr_T *lnum;
2690{
2691 buf_T *buf;
2692
2693 buf = buflist_findnr(fnum);
2694 if (buf == NULL || buf->b_fname == NULL)
2695 return FAIL;
2696
2697 *fname = buf->b_fname;
2698 *lnum = buflist_findlnum(buf);
2699
2700 return OK;
2701}
2702
2703/*
2704 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2705 * The file name with the full path is also remembered, for when :cd is used.
2706 * Returns FAIL for failure (file name already in use by other buffer)
2707 * OK otherwise.
2708 */
2709 int
2710setfname(buf, ffname, sfname, message)
2711 buf_T *buf;
2712 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002713 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714{
Bram Moolenaar81695252004-12-29 20:58:21 +00002715 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002716#ifdef UNIX
2717 struct stat st;
2718#endif
2719
2720 if (ffname == NULL || *ffname == NUL)
2721 {
2722 /* Removing the name. */
2723 vim_free(buf->b_ffname);
2724 vim_free(buf->b_sfname);
2725 buf->b_ffname = NULL;
2726 buf->b_sfname = NULL;
2727#ifdef UNIX
2728 st.st_dev = (dev_T)-1;
2729#endif
2730 }
2731 else
2732 {
2733 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2734 if (ffname == NULL) /* out of memory */
2735 return FAIL;
2736
2737 /*
2738 * if the file name is already used in another buffer:
2739 * - if the buffer is loaded, fail
2740 * - if the buffer is not loaded, delete it from the list
2741 */
2742#ifdef UNIX
2743 if (mch_stat((char *)ffname, &st) < 0)
2744 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002745#endif
2746 if (!(buf->b_flags & BF_DUMMY))
2747#ifdef UNIX
2748 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002750 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751#endif
2752 if (obuf != NULL && obuf != buf)
2753 {
2754 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2755 {
2756 if (message)
2757 EMSG(_("E95: Buffer with this name already exists"));
2758 vim_free(ffname);
2759 return FAIL;
2760 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002761 /* delete from the list */
2762 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 sfname = vim_strsave(sfname);
2765 if (ffname == NULL || sfname == NULL)
2766 {
2767 vim_free(sfname);
2768 vim_free(ffname);
2769 return FAIL;
2770 }
2771#ifdef USE_FNAME_CASE
2772# ifdef USE_LONG_FNAME
2773 if (USE_LONG_FNAME)
2774# endif
2775 fname_case(sfname, 0); /* set correct case for short file name */
2776#endif
2777 vim_free(buf->b_ffname);
2778 vim_free(buf->b_sfname);
2779 buf->b_ffname = ffname;
2780 buf->b_sfname = sfname;
2781 }
2782 buf->b_fname = buf->b_sfname;
2783#ifdef UNIX
2784 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002785 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 else
2787 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002788 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002789 buf->b_dev = st.st_dev;
2790 buf->b_ino = st.st_ino;
2791 }
2792#endif
2793
2794#ifndef SHORT_FNAME
2795 buf->b_shortname = FALSE;
2796#endif
2797
2798 buf_name_changed(buf);
2799 return OK;
2800}
2801
2802/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002803 * Crude way of changing the name of a buffer. Use with care!
2804 * The name should be relative to the current directory.
2805 */
2806 void
2807buf_set_name(fnum, name)
2808 int fnum;
2809 char_u *name;
2810{
2811 buf_T *buf;
2812
2813 buf = buflist_findnr(fnum);
2814 if (buf != NULL)
2815 {
2816 vim_free(buf->b_sfname);
2817 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002818 buf->b_ffname = vim_strsave(name);
2819 buf->b_sfname = NULL;
2820 /* Allocate ffname and expand into full path. Also resolves .lnk
2821 * files on Win32. */
2822 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002823 buf->b_fname = buf->b_sfname;
2824 }
2825}
2826
2827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002828 * Take care of what needs to be done when the name of buffer "buf" has
2829 * changed.
2830 */
2831 void
2832buf_name_changed(buf)
2833 buf_T *buf;
2834{
2835 /*
2836 * If the file name changed, also change the name of the swapfile
2837 */
2838 if (buf->b_ml.ml_mfp != NULL)
2839 ml_setname(buf);
2840
2841 if (curwin->w_buffer == buf)
2842 check_arg_idx(curwin); /* check file name for arg list */
2843#ifdef FEAT_TITLE
2844 maketitle(); /* set window title */
2845#endif
2846#ifdef FEAT_WINDOWS
2847 status_redraw_all(); /* status lines need to be redrawn */
2848#endif
2849 fmarks_check_names(buf); /* check named file marks */
2850 ml_timestamp(buf); /* reset timestamp */
2851}
2852
2853/*
2854 * set alternate file name for current window
2855 *
2856 * Used by do_one_cmd(), do_write() and do_ecmd().
2857 * Return the buffer.
2858 */
2859 buf_T *
2860setaltfname(ffname, sfname, lnum)
2861 char_u *ffname;
2862 char_u *sfname;
2863 linenr_T lnum;
2864{
2865 buf_T *buf;
2866
2867 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2868 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002869 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 curwin->w_alt_fnum = buf->b_fnum;
2871 return buf;
2872}
2873
2874/*
2875 * Get alternate file name for current window.
2876 * Return NULL if there isn't any, and give error message if requested.
2877 */
2878 char_u *
2879getaltfname(errmsg)
2880 int errmsg; /* give error message */
2881{
2882 char_u *fname;
2883 linenr_T dummy;
2884
2885 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2886 {
2887 if (errmsg)
2888 EMSG(_(e_noalt));
2889 return NULL;
2890 }
2891 return fname;
2892}
2893
2894/*
2895 * Add a file name to the buflist and return its number.
2896 * Uses same flags as buflist_new(), except BLN_DUMMY.
2897 *
2898 * used by qf_init(), main() and doarglist()
2899 */
2900 int
2901buflist_add(fname, flags)
2902 char_u *fname;
2903 int flags;
2904{
2905 buf_T *buf;
2906
2907 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2908 if (buf != NULL)
2909 return buf->b_fnum;
2910 return 0;
2911}
2912
2913#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2914/*
2915 * Adjust slashes in file names. Called after 'shellslash' was set.
2916 */
2917 void
2918buflist_slash_adjust()
2919{
2920 buf_T *bp;
2921
2922 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2923 {
2924 if (bp->b_ffname != NULL)
2925 slash_adjust(bp->b_ffname);
2926 if (bp->b_sfname != NULL)
2927 slash_adjust(bp->b_sfname);
2928 }
2929}
2930#endif
2931
2932/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002933 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 * Also save the local window option values.
2935 */
2936 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002937buflist_altfpos(win)
2938 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002940 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941}
2942
2943/*
2944 * Return TRUE if 'ffname' is not the same file as current file.
2945 * Fname must have a full path (expanded by mch_FullName()).
2946 */
2947 int
2948otherfile(ffname)
2949 char_u *ffname;
2950{
2951 return otherfile_buf(curbuf, ffname
2952#ifdef UNIX
2953 , NULL
2954#endif
2955 );
2956}
2957
2958 static int
2959otherfile_buf(buf, ffname
2960#ifdef UNIX
2961 , stp
2962#endif
2963 )
2964 buf_T *buf;
2965 char_u *ffname;
2966#ifdef UNIX
2967 struct stat *stp;
2968#endif
2969{
2970 /* no name is different */
2971 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2972 return TRUE;
2973 if (fnamecmp(ffname, buf->b_ffname) == 0)
2974 return FALSE;
2975#ifdef UNIX
2976 {
2977 struct stat st;
2978
2979 /* If no struct stat given, get it now */
2980 if (stp == NULL)
2981 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002982 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 st.st_dev = (dev_T)-1;
2984 stp = &st;
2985 }
2986 /* Use dev/ino to check if the files are the same, even when the names
2987 * are different (possible with links). Still need to compare the
2988 * name above, for when the file doesn't exist yet.
2989 * Problem: The dev/ino changes when a file is deleted (and created
2990 * again) and remains the same when renamed/moved. We don't want to
2991 * mch_stat() each buffer each time, that would be too slow. Get the
2992 * dev/ino again when they appear to match, but not when they appear
2993 * to be different: Could skip a buffer when it's actually the same
2994 * file. */
2995 if (buf_same_ino(buf, stp))
2996 {
2997 buf_setino(buf);
2998 if (buf_same_ino(buf, stp))
2999 return FALSE;
3000 }
3001 }
3002#endif
3003 return TRUE;
3004}
3005
3006#if defined(UNIX) || defined(PROTO)
3007/*
3008 * Set inode and device number for a buffer.
3009 * Must always be called when b_fname is changed!.
3010 */
3011 void
3012buf_setino(buf)
3013 buf_T *buf;
3014{
3015 struct stat st;
3016
3017 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3018 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003019 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 buf->b_dev = st.st_dev;
3021 buf->b_ino = st.st_ino;
3022 }
3023 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003024 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025}
3026
3027/*
3028 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3029 */
3030 static int
3031buf_same_ino(buf, stp)
3032 buf_T *buf;
3033 struct stat *stp;
3034{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003035 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 && stp->st_dev == buf->b_dev
3037 && stp->st_ino == buf->b_ino);
3038}
3039#endif
3040
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003041/*
3042 * Print info about the current buffer.
3043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 void
3045fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003046 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 int shorthelp;
3048 int dont_truncate;
3049{
3050 char_u *name;
3051 int n;
3052 char_u *p;
3053 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003054 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055
3056 buffer = alloc(IOSIZE);
3057 if (buffer == NULL)
3058 return;
3059
3060 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3061 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003062 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 p = buffer + STRLEN(buffer);
3064 }
3065 else
3066 p = buffer;
3067
3068 *p++ = '"';
3069 if (buf_spname(curbuf) != NULL)
Bram Moolenaarec3cfeb2012-10-03 17:12:47 +02003070 vim_strncpy(p, buf_spname(curbuf), IOSIZE - (p - buffer) - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071 else
3072 {
3073 if (!fullname && curbuf->b_fname != NULL)
3074 name = curbuf->b_fname;
3075 else
3076 name = curbuf->b_ffname;
3077 home_replace(shorthelp ? curbuf : NULL, name, p,
3078 (int)(IOSIZE - (p - buffer)), TRUE);
3079 }
3080
Bram Moolenaara800b422010-06-27 01:15:55 +02003081 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 curbufIsChanged() ? (shortmess(SHM_MOD)
3083 ? " [+]" : _(" [Modified]")) : " ",
3084 (curbuf->b_flags & BF_NOTEDITED)
3085#ifdef FEAT_QUICKFIX
3086 && !bt_dontwrite(curbuf)
3087#endif
3088 ? _("[Not edited]") : "",
3089 (curbuf->b_flags & BF_NEW)
3090#ifdef FEAT_QUICKFIX
3091 && !bt_dontwrite(curbuf)
3092#endif
3093 ? _("[New file]") : "",
3094 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3095 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3096 : _("[readonly]")) : "",
3097 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3098 || curbuf->b_p_ro) ?
3099 " " : "");
3100 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3101 * causes an overflow, thus for large numbers divide instead. */
3102 if (curwin->w_cursor.lnum > 1000000L)
3103 n = (int)(((long)curwin->w_cursor.lnum) /
3104 ((long)curbuf->b_ml.ml_line_count / 100L));
3105 else
3106 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3107 (long)curbuf->b_ml.ml_line_count);
3108 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3109 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003110 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 }
3112#ifdef FEAT_CMDL_INFO
3113 else if (p_ru)
3114 {
3115 /* Current line and column are already on the screen -- webb */
3116 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003117 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003119 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 (long)curbuf->b_ml.ml_line_count, n);
3121 }
3122#endif
3123 else
3124 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003125 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 _("line %ld of %ld --%d%%-- col "),
3127 (long)curwin->w_cursor.lnum,
3128 (long)curbuf->b_ml.ml_line_count,
3129 n);
3130 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003131 len = STRLEN(buffer);
3132 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3134 }
3135
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003136 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137
3138 if (dont_truncate)
3139 {
3140 /* Temporarily set msg_scroll to avoid the message being truncated.
3141 * First call msg_start() to get the message in the right place. */
3142 msg_start();
3143 n = msg_scroll;
3144 msg_scroll = TRUE;
3145 msg(buffer);
3146 msg_scroll = n;
3147 }
3148 else
3149 {
3150 p = msg_trunc_attr(buffer, FALSE, 0);
3151 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 /* Need to repeat the message after redrawing when:
3153 * - When restart_edit is set (otherwise there will be a delay
3154 * before redrawing).
3155 * - When the screen was scrolled but there is no wait-return
3156 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003157 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 }
3159
3160 vim_free(buffer);
3161}
3162
3163 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003164col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003166 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 int col;
3168 int vcol;
3169{
3170 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003171 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003173 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174}
3175
3176#if defined(FEAT_TITLE) || defined(PROTO)
3177/*
3178 * put file name in title bar of window and in icon title
3179 */
3180
3181static char_u *lasttitle = NULL;
3182static char_u *lasticon = NULL;
3183
3184 void
3185maketitle()
3186{
3187 char_u *p;
3188 char_u *t_str = NULL;
3189 char_u *i_name;
3190 char_u *i_str = NULL;
3191 int maxlen = 0;
3192 int len;
3193 int mustset;
3194 char_u buf[IOSIZE];
3195 int off;
3196
3197 if (!redrawing())
3198 {
3199 /* Postpone updating the title when 'lazyredraw' is set. */
3200 need_maketitle = TRUE;
3201 return;
3202 }
3203
3204 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003205 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206 return;
3207
3208 if (p_title)
3209 {
3210 if (p_titlelen > 0)
3211 {
3212 maxlen = p_titlelen * Columns / 100;
3213 if (maxlen < 10)
3214 maxlen = 10;
3215 }
3216
3217 t_str = buf;
3218 if (*p_titlestring != NUL)
3219 {
3220#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003221 if (stl_syntax & STL_IN_TITLE)
3222 {
3223 int use_sandbox = FALSE;
3224 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003225
3226# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003227 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003228# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003229 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003231 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003232 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003233 if (called_emsg)
3234 set_string_option_direct((char_u *)"titlestring", -1,
3235 (char_u *)"", OPT_FREE, SID_ERROR);
3236 called_emsg |= save_called_emsg;
3237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238 else
3239#endif
3240 t_str = p_titlestring;
3241 }
3242 else
3243 {
3244 /* format: "fname + (path) (1 of 2) - VIM" */
3245
Bram Moolenaar2c666692012-09-05 13:30:40 +02003246#define SPACE_FOR_FNAME (IOSIZE - 100)
3247#define SPACE_FOR_DIR (IOSIZE - 20)
3248#define SPACE_FOR_ARGNR (IOSIZE - 10) /* at least room for " - VIM" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if (curbuf->b_fname == NULL)
Bram Moolenaar2c666692012-09-05 13:30:40 +02003250 vim_strncpy(buf, (char_u *)_("[No Name]"), SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251 else
3252 {
3253 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaar2c666692012-09-05 13:30:40 +02003254 vim_strncpy(buf, p, SPACE_FOR_FNAME);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003256 }
3257
3258 switch (bufIsChanged(curbuf)
3259 + (curbuf->b_p_ro * 2)
3260 + (!curbuf->b_p_ma * 4))
3261 {
3262 case 1: STRCAT(buf, " +"); break;
3263 case 2: STRCAT(buf, " ="); break;
3264 case 3: STRCAT(buf, " =+"); break;
3265 case 4:
3266 case 6: STRCAT(buf, " -"); break;
3267 case 5:
3268 case 7: STRCAT(buf, " -+"); break;
3269 }
3270
3271 if (curbuf->b_fname != NULL)
3272 {
3273 /* Get path of file, replace home dir with ~ */
3274 off = (int)STRLEN(buf);
3275 buf[off++] = ' ';
3276 buf[off++] = '(';
3277 home_replace(curbuf, curbuf->b_ffname,
Bram Moolenaar2c666692012-09-05 13:30:40 +02003278 buf + off, SPACE_FOR_DIR - off, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279#ifdef BACKSLASH_IN_FILENAME
3280 /* avoid "c:/name" to be reduced to "c" */
3281 if (isalpha(buf[off]) && buf[off + 1] == ':')
3282 off += 2;
3283#endif
3284 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003285 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003288 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar2c666692012-09-05 13:30:40 +02003289 (size_t)(SPACE_FOR_DIR - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003292
Bram Moolenaar2c666692012-09-05 13:30:40 +02003293 /* Translate unprintable chars and concatenate. Keep some
3294 * room for the server name. When there is no room (very long
3295 * file name) use (...). */
3296 if (off < SPACE_FOR_DIR)
3297 {
3298 p = transstr(buf + off);
3299 vim_strncpy(buf + off, p, (size_t)(SPACE_FOR_DIR - off));
3300 vim_free(p);
3301 }
3302 else
3303 {
3304 vim_strncpy(buf + off, (char_u *)"...",
3305 (size_t)(SPACE_FOR_ARGNR - off));
3306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 STRCAT(buf, ")");
3308 }
3309
Bram Moolenaar2c666692012-09-05 13:30:40 +02003310 append_arg_number(curwin, buf, SPACE_FOR_ARGNR, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311
3312#if defined(FEAT_CLIENTSERVER)
3313 if (serverName != NULL)
3314 {
3315 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003316 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 }
3318 else
3319#endif
3320 STRCAT(buf, " - VIM");
3321
3322 if (maxlen > 0)
3323 {
3324 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003325 if (vim_strsize(buf) > maxlen)
3326 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 }
3328 }
3329 }
3330 mustset = ti_change(t_str, &lasttitle);
3331
3332 if (p_icon)
3333 {
3334 i_str = buf;
3335 if (*p_iconstring != NUL)
3336 {
3337#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003338 if (stl_syntax & STL_IN_ICON)
3339 {
3340 int use_sandbox = FALSE;
3341 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003342
3343# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003344 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003345# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003346 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003348 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003349 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003350 if (called_emsg)
3351 set_string_option_direct((char_u *)"iconstring", -1,
3352 (char_u *)"", OPT_FREE, SID_ERROR);
3353 called_emsg |= save_called_emsg;
3354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 else
3356#endif
3357 i_str = p_iconstring;
3358 }
3359 else
3360 {
3361 if (buf_spname(curbuf) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003362 i_name = buf_spname(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 else /* use file name only in icon */
3364 i_name = gettail(curbuf->b_ffname);
3365 *i_str = NUL;
3366 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003367 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (len > 100)
3369 {
3370 len -= 100;
3371#ifdef FEAT_MBYTE
3372 if (has_mbyte)
3373 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3374#endif
3375 i_name += len;
3376 }
3377 STRCPY(i_str, i_name);
3378 trans_characters(i_str, IOSIZE);
3379 }
3380 }
3381
3382 mustset |= ti_change(i_str, &lasticon);
3383
3384 if (mustset)
3385 resettitle();
3386}
3387
3388/*
3389 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3390 * from "str" if it does.
3391 * Return TRUE when "*last" changed.
3392 */
3393 static int
3394ti_change(str, last)
3395 char_u *str;
3396 char_u **last;
3397{
3398 if ((str == NULL) != (*last == NULL)
3399 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3400 {
3401 vim_free(*last);
3402 if (str == NULL)
3403 *last = NULL;
3404 else
3405 *last = vim_strsave(str);
3406 return TRUE;
3407 }
3408 return FALSE;
3409}
3410
3411/*
3412 * Put current window title back (used after calling a shell)
3413 */
3414 void
3415resettitle()
3416{
3417 mch_settitle(lasttitle, lasticon);
3418}
Bram Moolenaarea408852005-06-25 22:49:46 +00003419
3420# if defined(EXITFREE) || defined(PROTO)
3421 void
3422free_titles()
3423{
3424 vim_free(lasttitle);
3425 vim_free(lasticon);
3426}
3427# endif
3428
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429#endif /* FEAT_TITLE */
3430
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003431#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003433 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 * Return length of string in screen cells.
3435 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003436 * Normally works for window "wp", except when working for 'tabline' then it
3437 * is "curwin".
3438 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 * Items are drawn interspersed with the text that surrounds it
3440 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3441 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3442 *
3443 * If maxwidth is not zero, the string will be filled at any middle marker
3444 * or truncated if too long, fillchar is used for all whitespace.
3445 */
3446 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003447build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3448 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003450 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 size_t outlen; /* length of out[] */
3452 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003453 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int fillchar;
3455 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003456 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3457 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458{
3459 char_u *p;
3460 char_u *s;
3461 char_u *t;
3462 char_u *linecont;
3463#ifdef FEAT_EVAL
3464 win_T *o_curwin;
3465 buf_T *o_curbuf;
3466#endif
3467 int empty_line;
3468 colnr_T virtcol;
3469 long l;
3470 long n;
3471 int prevchar_isflag;
3472 int prevchar_isitem;
3473 int itemisflag;
3474 int fillable;
3475 char_u *str;
3476 long num;
3477 int width;
3478 int itemcnt;
3479 int curitem;
3480 int groupitem[STL_MAX_ITEM];
3481 int groupdepth;
3482 struct stl_item
3483 {
3484 char_u *start;
3485 int minwid;
3486 int maxwid;
3487 enum
3488 {
3489 Normal,
3490 Empty,
3491 Group,
3492 Middle,
3493 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003494 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 Trunc
3496 } type;
3497 } item[STL_MAX_ITEM];
3498 int minwid;
3499 int maxwid;
3500 int zeropad;
3501 char_u base;
3502 char_u opt;
3503#define TMPLEN 70
3504 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003505 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003506 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003507
3508#ifdef FEAT_EVAL
3509 /*
3510 * When the format starts with "%!" then evaluate it as an expression and
3511 * use the result as the actual format string.
3512 */
3513 if (fmt[0] == '%' && fmt[1] == '!')
3514 {
3515 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3516 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003517 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003518 }
3519#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 if (fillchar == 0)
3522 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003523#ifdef FEAT_MBYTE
3524 /* Can't handle a multi-byte fill character yet. */
3525 else if (mb_char2len(fillchar) > 1)
3526 fillchar = '-';
3527#endif
3528
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529 /*
3530 * Get line & check if empty (cursorpos will show "0-1").
3531 * If inversion is possible we use it. Else '=' characters are used.
3532 */
3533 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3534 empty_line = (*linecont == NUL);
3535
3536 groupdepth = 0;
3537 p = out;
3538 curitem = 0;
3539 prevchar_isflag = TRUE;
3540 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003541 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003542 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003543 if (curitem == STL_MAX_ITEM)
3544 {
3545 /* There are too many items. Add the error code to the statusline
3546 * to give the user a hint about what went wrong. */
3547 if (p + 6 < out + outlen)
3548 {
3549 mch_memmove(p, " E541", (size_t)5);
3550 p += 5;
3551 }
3552 break;
3553 }
3554
Bram Moolenaar071d4272004-06-13 20:20:40 +00003555 if (*s != NUL && *s != '%')
3556 prevchar_isflag = prevchar_isitem = FALSE;
3557
3558 /*
3559 * Handle up to the next '%' or the end.
3560 */
3561 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3562 *p++ = *s++;
3563 if (*s == NUL || p + 1 >= out + outlen)
3564 break;
3565
3566 /*
3567 * Handle one '%' item.
3568 */
3569 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003570 if (*s == NUL) /* ignore trailing % */
3571 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003572 if (*s == '%')
3573 {
3574 if (p + 1 >= out + outlen)
3575 break;
3576 *p++ = *s++;
3577 prevchar_isflag = prevchar_isitem = FALSE;
3578 continue;
3579 }
3580 if (*s == STL_MIDDLEMARK)
3581 {
3582 s++;
3583 if (groupdepth > 0)
3584 continue;
3585 item[curitem].type = Middle;
3586 item[curitem++].start = p;
3587 continue;
3588 }
3589 if (*s == STL_TRUNCMARK)
3590 {
3591 s++;
3592 item[curitem].type = Trunc;
3593 item[curitem++].start = p;
3594 continue;
3595 }
3596 if (*s == ')')
3597 {
3598 s++;
3599 if (groupdepth < 1)
3600 continue;
3601 groupdepth--;
3602
3603 t = item[groupitem[groupdepth]].start;
3604 *p = NUL;
3605 l = vim_strsize(t);
3606 if (curitem > groupitem[groupdepth] + 1
3607 && item[groupitem[groupdepth]].minwid == 0)
3608 {
3609 /* remove group if all items are empty */
3610 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3611 if (item[n].type == Normal)
3612 break;
3613 if (n == curitem)
3614 {
3615 p = t;
3616 l = 0;
3617 }
3618 }
3619 if (l > item[groupitem[groupdepth]].maxwid)
3620 {
3621 /* truncate, remove n bytes of text at the start */
3622#ifdef FEAT_MBYTE
3623 if (has_mbyte)
3624 {
3625 /* Find the first character that should be included. */
3626 n = 0;
3627 while (l >= item[groupitem[groupdepth]].maxwid)
3628 {
3629 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003630 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
3632 }
3633 else
3634#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003635 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
3637 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003638 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 p = p - n + 1;
3640#ifdef FEAT_MBYTE
3641 /* Fill up space left over by half a double-wide char. */
3642 while (++l < item[groupitem[groupdepth]].minwid)
3643 *p++ = fillchar;
3644#endif
3645
3646 /* correct the start of the items for the truncation */
3647 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3648 {
3649 item[l].start -= n;
3650 if (item[l].start < t)
3651 item[l].start = t;
3652 }
3653 }
3654 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3655 {
3656 /* fill */
3657 n = item[groupitem[groupdepth]].minwid;
3658 if (n < 0)
3659 {
3660 /* fill by appending characters */
3661 n = 0 - n;
3662 while (l++ < n && p + 1 < out + outlen)
3663 *p++ = fillchar;
3664 }
3665 else
3666 {
3667 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003668 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669 l = n - l;
3670 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003671 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 p += l;
3673 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3674 item[n].start += l;
3675 for ( ; l > 0; l--)
3676 *t++ = fillchar;
3677 }
3678 }
3679 continue;
3680 }
3681 minwid = 0;
3682 maxwid = 9999;
3683 zeropad = FALSE;
3684 l = 1;
3685 if (*s == '0')
3686 {
3687 s++;
3688 zeropad = TRUE;
3689 }
3690 if (*s == '-')
3691 {
3692 s++;
3693 l = -1;
3694 }
3695 if (VIM_ISDIGIT(*s))
3696 {
3697 minwid = (int)getdigits(&s);
3698 if (minwid < 0) /* overflow */
3699 minwid = 0;
3700 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003701 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 {
3703 item[curitem].type = Highlight;
3704 item[curitem].start = p;
3705 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3706 s++;
3707 curitem++;
3708 continue;
3709 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003710 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3711 {
3712 if (*s == STL_TABCLOSENR)
3713 {
3714 if (minwid == 0)
3715 {
3716 /* %X ends the close label, go back to the previously
3717 * define tab label nr. */
3718 for (n = curitem - 1; n >= 0; --n)
3719 if (item[n].type == TabPage && item[n].minwid >= 0)
3720 {
3721 minwid = item[n].minwid;
3722 break;
3723 }
3724 }
3725 else
3726 /* close nrs are stored as negative values */
3727 minwid = - minwid;
3728 }
3729 item[curitem].type = TabPage;
3730 item[curitem].start = p;
3731 item[curitem].minwid = minwid;
3732 s++;
3733 curitem++;
3734 continue;
3735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 if (*s == '.')
3737 {
3738 s++;
3739 if (VIM_ISDIGIT(*s))
3740 {
3741 maxwid = (int)getdigits(&s);
3742 if (maxwid <= 0) /* overflow */
3743 maxwid = 50;
3744 }
3745 }
3746 minwid = (minwid > 50 ? 50 : minwid) * l;
3747 if (*s == '(')
3748 {
3749 groupitem[groupdepth++] = curitem;
3750 item[curitem].type = Group;
3751 item[curitem].start = p;
3752 item[curitem].minwid = minwid;
3753 item[curitem].maxwid = maxwid;
3754 s++;
3755 curitem++;
3756 continue;
3757 }
3758 if (vim_strchr(STL_ALL, *s) == NULL)
3759 {
3760 s++;
3761 continue;
3762 }
3763 opt = *s++;
3764
3765 /* OK - now for the real work */
3766 base = 'D';
3767 itemisflag = FALSE;
3768 fillable = TRUE;
3769 num = -1;
3770 str = NULL;
3771 switch (opt)
3772 {
3773 case STL_FILEPATH:
3774 case STL_FULLPATH:
3775 case STL_FILENAME:
3776 fillable = FALSE; /* don't change ' ' to fillchar */
3777 if (buf_spname(wp->w_buffer) != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02003778 vim_strncpy(NameBuff, buf_spname(wp->w_buffer), MAXPATHL - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 else
3780 {
3781 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003782 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3784 }
3785 trans_characters(NameBuff, MAXPATHL);
3786 if (opt != STL_FILENAME)
3787 str = NameBuff;
3788 else
3789 str = gettail(NameBuff);
3790 break;
3791
3792 case STL_VIM_EXPR: /* '{' */
3793 itemisflag = TRUE;
3794 t = p;
3795 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3796 *p++ = *s++;
3797 if (*s != '}') /* missing '}' or out of space */
3798 break;
3799 s++;
3800 *p = 0;
3801 p = t;
3802
3803#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003804 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3806
3807 o_curbuf = curbuf;
3808 o_curwin = curwin;
3809 curwin = wp;
3810 curbuf = wp->w_buffer;
3811
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003812 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813
3814 curwin = o_curwin;
3815 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003816 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817
3818 if (str != NULL && *str != 0)
3819 {
3820 if (*skipdigits(str) == NUL)
3821 {
3822 num = atoi((char *)str);
3823 vim_free(str);
3824 str = NULL;
3825 itemisflag = FALSE;
3826 }
3827 }
3828#endif
3829 break;
3830
3831 case STL_LINE:
3832 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3833 ? 0L : (long)(wp->w_cursor.lnum);
3834 break;
3835
3836 case STL_NUMLINES:
3837 num = wp->w_buffer->b_ml.ml_line_count;
3838 break;
3839
3840 case STL_COLUMN:
3841 num = !(State & INSERT) && empty_line
3842 ? 0 : (int)wp->w_cursor.col + 1;
3843 break;
3844
3845 case STL_VIRTCOL:
3846 case STL_VIRTCOL_ALT:
3847 /* In list mode virtcol needs to be recomputed */
3848 virtcol = wp->w_virtcol;
3849 if (wp->w_p_list && lcs_tab1 == NUL)
3850 {
3851 wp->w_p_list = FALSE;
3852 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3853 wp->w_p_list = TRUE;
3854 }
3855 ++virtcol;
3856 /* Don't display %V if it's the same as %c. */
3857 if (opt == STL_VIRTCOL_ALT
3858 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3859 ? 0 : (int)wp->w_cursor.col + 1)))
3860 break;
3861 num = (long)virtcol;
3862 break;
3863
3864 case STL_PERCENTAGE:
3865 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3866 (long)wp->w_buffer->b_ml.ml_line_count);
3867 break;
3868
3869 case STL_ALTPERCENT:
3870 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003871 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 break;
3873
3874 case STL_ARGLISTSTAT:
3875 fillable = FALSE;
3876 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003877 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 str = tmp;
3879 break;
3880
3881 case STL_KEYMAP:
3882 fillable = FALSE;
3883 if (get_keymap_str(wp, tmp, TMPLEN))
3884 str = tmp;
3885 break;
3886 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003887#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003888 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889#else
3890 num = 0;
3891#endif
3892 break;
3893
3894 case STL_BUFNO:
3895 num = wp->w_buffer->b_fnum;
3896 break;
3897
3898 case STL_OFFSET_X:
3899 base = 'X';
3900 case STL_OFFSET:
3901#ifdef FEAT_BYTEOFF
3902 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3903 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3904 0L : l + 1 + (!(State & INSERT) && empty_line ?
3905 0 : (int)wp->w_cursor.col);
3906#endif
3907 break;
3908
3909 case STL_BYTEVAL_X:
3910 base = 'X';
3911 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003912 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 num = 0;
3914 else
3915 {
3916#ifdef FEAT_MBYTE
3917 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3918#else
3919 num = linecont[wp->w_cursor.col];
3920#endif
3921 }
3922 if (num == NL)
3923 num = 0;
3924 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3925 num = NL;
3926 break;
3927
3928 case STL_ROFLAG:
3929 case STL_ROFLAG_ALT:
3930 itemisflag = TRUE;
3931 if (wp->w_buffer->b_p_ro)
3932 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3933 break;
3934
3935 case STL_HELPFLAG:
3936 case STL_HELPFLAG_ALT:
3937 itemisflag = TRUE;
3938 if (wp->w_buffer->b_help)
3939 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003940 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 break;
3942
3943#ifdef FEAT_AUTOCMD
3944 case STL_FILETYPE:
3945 if (*wp->w_buffer->b_p_ft != NUL
3946 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3947 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003948 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3949 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 str = tmp;
3951 }
3952 break;
3953
3954 case STL_FILETYPE_ALT:
3955 itemisflag = TRUE;
3956 if (*wp->w_buffer->b_p_ft != NUL
3957 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3958 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003959 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3960 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 for (t = tmp; *t != 0; t++)
3962 *t = TOUPPER_LOC(*t);
3963 str = tmp;
3964 }
3965 break;
3966#endif
3967
3968#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3969 case STL_PREVIEWFLAG:
3970 case STL_PREVIEWFLAG_ALT:
3971 itemisflag = TRUE;
3972 if (wp->w_p_pvw)
3973 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3974 : _("[Preview]"));
3975 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003976
3977 case STL_QUICKFIX:
3978 if (bt_quickfix(wp->w_buffer))
3979 str = (char_u *)(wp->w_llist_ref
3980 ? _(msg_loclist)
3981 : _(msg_qflist));
3982 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983#endif
3984
3985 case STL_MODIFIED:
3986 case STL_MODIFIED_ALT:
3987 itemisflag = TRUE;
3988 switch ((opt == STL_MODIFIED_ALT)
3989 + bufIsChanged(wp->w_buffer) * 2
3990 + (!wp->w_buffer->b_p_ma) * 4)
3991 {
3992 case 2: str = (char_u *)"[+]"; break;
3993 case 3: str = (char_u *)",+"; break;
3994 case 4: str = (char_u *)"[-]"; break;
3995 case 5: str = (char_u *)",-"; break;
3996 case 6: str = (char_u *)"[+-]"; break;
3997 case 7: str = (char_u *)",+-"; break;
3998 }
3999 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004000
4001 case STL_HIGHLIGHT:
4002 t = s;
4003 while (*s != '#' && *s != NUL)
4004 ++s;
4005 if (*s == '#')
4006 {
4007 item[curitem].type = Highlight;
4008 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004009 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004010 curitem++;
4011 }
4012 ++s;
4013 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 }
4015
4016 item[curitem].start = p;
4017 item[curitem].type = Normal;
4018 if (str != NULL && *str)
4019 {
4020 t = str;
4021 if (itemisflag)
4022 {
4023 if ((t[0] && t[1])
4024 && ((!prevchar_isitem && *t == ',')
4025 || (prevchar_isflag && *t == ' ')))
4026 t++;
4027 prevchar_isflag = TRUE;
4028 }
4029 l = vim_strsize(t);
4030 if (l > 0)
4031 prevchar_isitem = TRUE;
4032 if (l > maxwid)
4033 {
4034 while (l >= maxwid)
4035#ifdef FEAT_MBYTE
4036 if (has_mbyte)
4037 {
4038 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004039 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040 }
4041 else
4042#endif
4043 l -= byte2cells(*t++);
4044 if (p + 1 >= out + outlen)
4045 break;
4046 *p++ = '<';
4047 }
4048 if (minwid > 0)
4049 {
4050 for (; l < minwid && p + 1 < out + outlen; l++)
4051 {
4052 /* Don't put a "-" in front of a digit. */
4053 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4054 *p++ = ' ';
4055 else
4056 *p++ = fillchar;
4057 }
4058 minwid = 0;
4059 }
4060 else
4061 minwid *= -1;
4062 while (*t && p + 1 < out + outlen)
4063 {
4064 *p++ = *t++;
4065 /* Change a space by fillchar, unless fillchar is '-' and a
4066 * digit follows. */
4067 if (fillable && p[-1] == ' '
4068 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4069 p[-1] = fillchar;
4070 }
4071 for (; l < minwid && p + 1 < out + outlen; l++)
4072 *p++ = fillchar;
4073 }
4074 else if (num >= 0)
4075 {
4076 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4077 char_u nstr[20];
4078
4079 if (p + 20 >= out + outlen)
4080 break; /* not sufficient space */
4081 prevchar_isitem = TRUE;
4082 t = nstr;
4083 if (opt == STL_VIRTCOL_ALT)
4084 {
4085 *t++ = '-';
4086 minwid--;
4087 }
4088 *t++ = '%';
4089 if (zeropad)
4090 *t++ = '0';
4091 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004092 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 *t = 0;
4094
4095 for (n = num, l = 1; n >= nbase; n /= nbase)
4096 l++;
4097 if (opt == STL_VIRTCOL_ALT)
4098 l++;
4099 if (l > maxwid)
4100 {
4101 l += 2;
4102 n = l - maxwid;
4103 while (l-- > maxwid)
4104 num /= nbase;
4105 *t++ = '>';
4106 *t++ = '%';
4107 *t = t[-3];
4108 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004109 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4110 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004113 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4114 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 p += STRLEN(p);
4116 }
4117 else
4118 item[curitem].type = Empty;
4119
4120 if (opt == STL_VIM_EXPR)
4121 vim_free(str);
4122
4123 if (num >= 0 || (!itemisflag && str && *str))
4124 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4125 curitem++;
4126 }
4127 *p = NUL;
4128 itemcnt = curitem;
4129
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004130#ifdef FEAT_EVAL
4131 if (usefmt != fmt)
4132 vim_free(usefmt);
4133#endif
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 width = vim_strsize(out);
4136 if (maxwidth > 0 && width > maxwidth)
4137 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004138 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 l = 0;
4140 if (itemcnt == 0)
4141 s = out;
4142 else
4143 {
4144 for ( ; l < itemcnt; l++)
4145 if (item[l].type == Trunc)
4146 {
4147 /* Truncate at %< item. */
4148 s = item[l].start;
4149 break;
4150 }
4151 if (l == itemcnt)
4152 {
4153 /* No %< item, truncate first item. */
4154 s = item[0].start;
4155 l = 0;
4156 }
4157 }
4158
4159 if (width - vim_strsize(s) >= maxwidth)
4160 {
4161 /* Truncation mark is beyond max length */
4162#ifdef FEAT_MBYTE
4163 if (has_mbyte)
4164 {
4165 s = out;
4166 width = 0;
4167 for (;;)
4168 {
4169 width += ptr2cells(s);
4170 if (width >= maxwidth)
4171 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004172 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 }
4174 /* Fill up for half a double-wide character. */
4175 while (++width < maxwidth)
4176 *s++ = fillchar;
4177 }
4178 else
4179#endif
4180 s = out + maxwidth - 1;
4181 for (l = 0; l < itemcnt; l++)
4182 if (item[l].start > s)
4183 break;
4184 itemcnt = l;
4185 *s++ = '>';
4186 *s = 0;
4187 }
4188 else
4189 {
4190#ifdef FEAT_MBYTE
4191 if (has_mbyte)
4192 {
4193 n = 0;
4194 while (width >= maxwidth)
4195 {
4196 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004197 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 }
4200 else
4201#endif
4202 n = width - maxwidth + 1;
4203 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004204 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 *s = '<';
4206
4207 /* Fill up for half a double-wide character. */
4208 while (++width < maxwidth)
4209 {
4210 s = s + STRLEN(s);
4211 *s++ = fillchar;
4212 *s = NUL;
4213 }
4214
4215 --n; /* count the '<' */
4216 for (; l < itemcnt; l++)
4217 {
4218 if (item[l].start - n >= s)
4219 item[l].start -= n;
4220 else
4221 item[l].start = s;
4222 }
4223 }
4224 width = maxwidth;
4225 }
4226 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4227 {
4228 /* Apply STL_MIDDLE if any */
4229 for (l = 0; l < itemcnt; l++)
4230 if (item[l].type == Middle)
4231 break;
4232 if (l < itemcnt)
4233 {
4234 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004235 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 for (s = item[l].start; s < p; s++)
4237 *s = fillchar;
4238 for (l++; l < itemcnt; l++)
4239 item[l].start += maxwidth - width;
4240 width = maxwidth;
4241 }
4242 }
4243
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004244 /* Store the info about highlighting. */
4245 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004247 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 for (l = 0; l < itemcnt; l++)
4249 {
4250 if (item[l].type == Highlight)
4251 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004252 sp->start = item[l].start;
4253 sp->userhl = item[l].minwid;
4254 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004257 sp->start = NULL;
4258 sp->userhl = 0;
4259 }
4260
4261 /* Store the info about tab pages labels. */
4262 if (tabtab != NULL)
4263 {
4264 sp = tabtab;
4265 for (l = 0; l < itemcnt; l++)
4266 {
4267 if (item[l].type == TabPage)
4268 {
4269 sp->start = item[l].start;
4270 sp->userhl = item[l].minwid;
4271 sp++;
4272 }
4273 }
4274 sp->start = NULL;
4275 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277
4278 return width;
4279}
4280#endif /* FEAT_STL_OPT */
4281
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004282#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4283 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004285 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4286 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 */
4288 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004289get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004291 char_u *buf;
4292 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293{
4294 long above; /* number of lines above window */
4295 long below; /* number of lines below window */
4296
4297 above = wp->w_topline - 1;
4298#ifdef FEAT_DIFF
4299 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4300#endif
4301 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4302 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004303 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4304 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004306 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004308 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 ? (int)(above / ((above + below) / 100L))
4310 : (int)(above * 100L / (above + below)));
4311}
4312#endif
4313
4314/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004315 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 * Return TRUE if it was appended.
4317 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004318 static int
4319append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 win_T *wp;
4321 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004322 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324{
4325 char_u *p;
4326
4327 if (ARGCOUNT <= 1) /* nothing to do */
4328 return FALSE;
4329
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004330 p = buf + STRLEN(buf); /* go to the end of the buffer */
4331 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 return FALSE;
4333 *p++ = ' ';
4334 *p++ = '(';
4335 if (add_file)
4336 {
4337 STRCPY(p, "file ");
4338 p += 5;
4339 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004340 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4341 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4343 return TRUE;
4344}
4345
4346/*
4347 * If fname is not a full path, make it a full path.
4348 * Returns pointer to allocated memory (NULL for failure).
4349 */
4350 char_u *
4351fix_fname(fname)
4352 char_u *fname;
4353{
4354 /*
4355 * Force expanding the path always for Unix, because symbolic links may
4356 * mess up the full path name, even though it starts with a '/'.
4357 * Also expand when there is ".." in the file name, try to remove it,
4358 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004359 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 * For MS-Windows also expand names like "longna~1" to "longname".
4361 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004362#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 return FullName_save(fname, TRUE);
4364#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004365 if (!vim_isAbsName(fname)
4366 || strstr((char *)fname, "..") != NULL
4367 || strstr((char *)fname, "//") != NULL
4368# ifdef BACKSLASH_IN_FILENAME
4369 || strstr((char *)fname, "\\\\") != NULL
4370# endif
4371# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004373# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374 )
4375 return FullName_save(fname, FALSE);
4376
4377 fname = vim_strsave(fname);
4378
Bram Moolenaar9b942202007-10-03 12:31:33 +00004379# ifdef USE_FNAME_CASE
4380# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004382# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
4384 if (fname != NULL)
4385 fname_case(fname, 0); /* set correct case for file name */
4386 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 return fname;
4390#endif
4391}
4392
4393/*
4394 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4395 * "ffname" becomes a pointer to allocated memory (or NULL).
4396 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 void
4398fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004399 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 char_u **ffname;
4401 char_u **sfname;
4402{
4403 if (*ffname == NULL) /* if no file name given, nothing to do */
4404 return;
4405 if (*sfname == NULL) /* if no short file name given, use ffname */
4406 *sfname = *ffname;
4407 *ffname = fix_fname(*ffname); /* expand to full path */
4408
4409#ifdef FEAT_SHORTCUT
4410 if (!buf->b_p_bin)
4411 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004412 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413
4414 /* If the file name is a shortcut file, use the file it links to. */
4415 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004416 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 {
4418 vim_free(*ffname);
4419 *ffname = rfname;
4420 *sfname = rfname;
4421 }
4422 }
4423#endif
4424}
4425
4426/*
4427 * Get the file name for an argument list entry.
4428 */
4429 char_u *
4430alist_name(aep)
4431 aentry_T *aep;
4432{
4433 buf_T *bp;
4434
4435 /* Use the name from the associated buffer if it exists. */
4436 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004437 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 return aep->ae_fname;
4439 return bp->b_fname;
4440}
4441
4442#if defined(FEAT_WINDOWS) || defined(PROTO)
4443/*
4444 * do_arg_all(): Open up to 'count' windows, one for each argument.
4445 */
4446 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004447do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 int count;
4449 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004450 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451{
4452 int i;
4453 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004454 char_u *opened; /* Array of weight for which args are open:
4455 * 0: not opened
4456 * 1: opened in other tab
4457 * 2: opened in curtab
4458 * 3: opened in curtab and curwin
4459 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004460 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 int use_firstwin = FALSE; /* use first window for arglist */
4462 int split_ret = OK;
4463 int p_ea_save;
4464 alist_T *alist; /* argument list to be used */
4465 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004466 tabpage_T *tpnext;
4467 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004468 win_T *old_curwin, *last_curwin;
4469 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004470 win_T *new_curwin = NULL;
4471 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472
4473 if (ARGCOUNT <= 0)
4474 {
4475 /* Don't give an error message. We don't want it when the ":all"
4476 * command is in the .vimrc. */
4477 return;
4478 }
4479 setpcmark();
4480
4481 opened_len = ARGCOUNT;
4482 opened = alloc_clear((unsigned)opened_len);
4483 if (opened == NULL)
4484 return;
4485
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004486 /* Autocommands may do anything to the argument list. Make sure it's not
4487 * freed while we are working here by "locking" it. We still have to
4488 * watch out for its size to be changed. */
4489 alist = curwin->w_alist;
4490 ++alist->al_refcount;
4491
4492 old_curwin = curwin;
4493 old_curtab = curtab;
4494
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495#ifdef FEAT_GUI
4496 need_mouse_correct = TRUE;
4497#endif
4498
4499 /*
4500 * Try closing all windows that are not in the argument list.
4501 * Also close windows that are not full width;
4502 * When 'hidden' or "forceit" set the buffer becomes hidden.
4503 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004504 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004506 if (had_tab > 0)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004507 goto_tabpage_tp(first_tabpage, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004508 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004510 tpnext = curtab->tp_next;
4511 for (wp = firstwin; wp != NULL; wp = wpnext)
4512 {
4513 wpnext = wp->w_next;
4514 buf = wp->w_buffer;
4515 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004516 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004518 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004520 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004521 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004522 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004524 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004525 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004527 if (i < alist->al_ga.ga_len
4528 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4529 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4530 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004532 int weight = 1;
4533
4534 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004535 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004536 ++weight;
4537 if (old_curwin == wp)
4538 ++weight;
4539 }
4540
4541 if (weight > (int)opened[i])
4542 {
4543 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004544 if (i == 0)
4545 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004546 if (new_curwin != NULL)
4547 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004548 new_curwin = wp;
4549 new_curtab = curtab;
4550 }
4551 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004552 else if (keep_tabs)
4553 i = opened_len;
4554
4555 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004556 {
4557 /* Use the current argument list for all windows
4558 * containing a file from it. */
4559 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004560 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004561 ++wp->w_alist->al_refcount;
4562 }
4563 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 }
4566 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004567 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004569 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004571 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004572 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004574 /* If the buffer was changed, and we would like to hide it,
4575 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004576 if (!P_HID(buf) && buf->b_nwindows <= 1
4577 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004579 (void)autowrite(buf, FALSE);
4580#ifdef FEAT_AUTOCMD
4581 /* check if autocommands removed the window */
4582 if (!win_valid(wp) || !buf_valid(buf))
4583 {
4584 wpnext = firstwin; /* start all over... */
4585 continue;
4586 }
4587#endif
4588 }
4589#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004590 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004591 if (firstwin == lastwin
4592 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004593#endif
4594 use_firstwin = TRUE;
4595#ifdef FEAT_WINDOWS
4596 else
4597 {
4598 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4599# ifdef FEAT_AUTOCMD
4600 /* check if autocommands removed the next window */
4601 if (!win_valid(wpnext))
4602 wpnext = firstwin; /* start all over... */
4603# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 }
4605#endif
4606 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004609
4610 /* Without the ":tab" modifier only do the current tab page. */
4611 if (had_tab == 0 || tpnext == NULL)
4612 break;
4613
4614# ifdef FEAT_AUTOCMD
4615 /* check if autocommands removed the next tab page */
4616 if (!valid_tabpage(tpnext))
4617 tpnext = first_tabpage; /* start all over...*/
4618# endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02004619 goto_tabpage_tp(tpnext, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 }
4621
4622 /*
4623 * Open a window for files in the argument list that don't have one.
4624 * ARGCOUNT may change while doing this, because of autocommands.
4625 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004626 if (count > opened_len || count <= 0)
4627 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628
4629#ifdef FEAT_AUTOCMD
4630 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4631 ++autocmd_no_enter;
4632 ++autocmd_no_leave;
4633#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004634 last_curwin = curwin;
4635 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004637#ifdef FEAT_WINDOWS
4638 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4639 * leaving an empty tab page when executed locally. */
4640 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4641 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4642 use_firstwin = TRUE;
4643#endif
4644
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004645 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
4647 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4648 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004649 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004650 {
4651 /* Move the already present window to below the current window */
4652 if (curwin->w_arg_idx != i)
4653 {
4654 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4655 {
4656 if (wpnext->w_arg_idx == i)
4657 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004658 if (keep_tabs)
4659 {
4660 new_curwin = wpnext;
4661 new_curtab = curtab;
4662 }
4663 else
4664 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 break;
4666 }
4667 }
4668 }
4669 }
4670 else if (split_ret == OK)
4671 {
4672 if (!use_firstwin) /* split current window */
4673 {
4674 p_ea_save = p_ea;
4675 p_ea = TRUE; /* use space from all windows */
4676 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4677 p_ea = p_ea_save;
4678 if (split_ret == FAIL)
4679 continue;
4680 }
4681#ifdef FEAT_AUTOCMD
4682 else /* first window: do autocmd for leaving this buffer */
4683 --autocmd_no_leave;
4684#endif
4685
4686 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004687 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 */
4689 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004690 if (i == 0)
4691 {
4692 new_curwin = curwin;
4693 new_curtab = curtab;
4694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4696 ECMD_ONE,
4697 ((P_HID(curwin->w_buffer)
4698 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004699 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700#ifdef FEAT_AUTOCMD
4701 if (use_firstwin)
4702 ++autocmd_no_leave;
4703#endif
4704 use_firstwin = FALSE;
4705 }
4706 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004707
4708 /* When ":tab" was used open a new tab for a new window repeatedly. */
4709 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4710 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 }
4712
4713 /* Remove the "lock" on the argument list. */
4714 alist_unlink(alist);
4715
4716#ifdef FEAT_AUTOCMD
4717 --autocmd_no_enter;
4718#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004719 /* restore last referenced tabpage's curwin */
4720 if (last_curtab != new_curtab)
4721 {
4722 if (valid_tabpage(last_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +02004723 goto_tabpage_tp(last_curtab, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004724 if (win_valid(last_curwin))
4725 win_enter(last_curwin, FALSE);
4726 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004727 /* to window with first arg */
4728 if (valid_tabpage(new_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +02004729 goto_tabpage_tp(new_curtab, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004730 if (win_valid(new_curwin))
4731 win_enter(new_curwin, FALSE);
4732
Bram Moolenaar071d4272004-06-13 20:20:40 +00004733#ifdef FEAT_AUTOCMD
4734 --autocmd_no_leave;
4735#endif
4736 vim_free(opened);
4737}
4738
4739# if defined(FEAT_LISTCMDS) || defined(PROTO)
4740/*
4741 * Open a window for a number of buffers.
4742 */
4743 void
4744ex_buffer_all(eap)
4745 exarg_T *eap;
4746{
4747 buf_T *buf;
4748 win_T *wp, *wpnext;
4749 int split_ret = OK;
4750 int p_ea_save;
4751 int open_wins = 0;
4752 int r;
4753 int count; /* Maximum number of windows to open. */
4754 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004755#ifdef FEAT_WINDOWS
4756 int had_tab = cmdmod.tab;
4757 tabpage_T *tpnext;
4758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759
4760 if (eap->addr_count == 0) /* make as many windows as possible */
4761 count = 9999;
4762 else
4763 count = eap->line2; /* make as many windows as specified */
4764 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4765 all = FALSE;
4766 else
4767 all = TRUE;
4768
4769 setpcmark();
4770
4771#ifdef FEAT_GUI
4772 need_mouse_correct = TRUE;
4773#endif
4774
4775 /*
4776 * Close superfluous windows (two windows for the same buffer).
4777 * Also close windows that are not full-width.
4778 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004779#ifdef FEAT_WINDOWS
4780 if (had_tab > 0)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004781 goto_tabpage_tp(first_tabpage, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004782 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004785 tpnext = curtab->tp_next;
4786 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004788 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004789 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004790#ifdef FEAT_VERTSPLIT
4791 || ((cmdmod.split & WSP_VERT)
4792 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004793 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004794 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004796#ifdef FEAT_WINDOWS
4797 || (had_tab > 0 && wp != firstwin)
4798#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004799 ) && firstwin != lastwin
4800#ifdef FEAT_AUTOCMD
4801 && !(wp->w_closing || wp->w_buffer->b_closing)
4802#endif
4803 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004804 {
4805 win_close(wp, FALSE);
4806#ifdef FEAT_AUTOCMD
4807 wpnext = firstwin; /* just in case an autocommand does
4808 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004809 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004810 open_wins = 0;
4811#endif
4812 }
4813 else
4814 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004816
4817#ifdef FEAT_WINDOWS
4818 /* Without the ":tab" modifier only do the current tab page. */
4819 if (had_tab == 0 || tpnext == NULL)
4820 break;
Bram Moolenaara8596c42012-06-13 14:28:20 +02004821 goto_tabpage_tp(tpnext, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824
4825 /*
4826 * Go through the buffer list. When a buffer doesn't have a window yet,
4827 * open one. Otherwise move the window to the right position.
4828 * Watch out for autocommands that delete buffers or windows!
4829 */
4830#ifdef FEAT_AUTOCMD
4831 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4832 ++autocmd_no_enter;
4833#endif
4834 win_enter(lastwin, FALSE);
4835#ifdef FEAT_AUTOCMD
4836 ++autocmd_no_leave;
4837#endif
4838 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4839 {
4840 /* Check if this buffer needs a window */
4841 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4842 continue;
4843
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004844#ifdef FEAT_WINDOWS
4845 if (had_tab != 0)
4846 {
4847 /* With the ":tab" modifier don't move the window. */
4848 if (buf->b_nwindows > 0)
4849 wp = lastwin; /* buffer has a window, skip it */
4850 else
4851 wp = NULL;
4852 }
4853 else
4854#endif
4855 {
4856 /* Check if this buffer already has a window */
4857 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4858 if (wp->w_buffer == buf)
4859 break;
4860 /* If the buffer already has a window, move it */
4861 if (wp != NULL)
4862 win_move_after(wp, curwin);
4863 }
4864
4865 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 {
4867 /* Split the window and put the buffer in it */
4868 p_ea_save = p_ea;
4869 p_ea = TRUE; /* use space from all windows */
4870 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4871 ++open_wins;
4872 p_ea = p_ea_save;
4873 if (split_ret == FAIL)
4874 continue;
4875
4876 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004877#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 swap_exists_action = SEA_DIALOG;
4879#endif
4880 set_curbuf(buf, DOBUF_GOTO);
4881#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004884#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004886# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 break;
4888 }
4889#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004890#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (swap_exists_action == SEA_QUIT)
4892 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004893# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4894 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004895
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004896 /* Reset the error/interrupt/exception state here so that
4897 * aborting() returns FALSE when closing a window. */
4898 enter_cleanup(&cs);
4899# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004900
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004901 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 win_close(curwin, TRUE);
4903 --open_wins;
4904 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004905 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004906
4907# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4908 /* Restore the error/interrupt/exception state if not
4909 * discarded by a new aborting error, interrupt, or uncaught
4910 * exception. */
4911 leave_cleanup(&cs);
4912# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 }
4914 else
4915 handle_swap_exists(NULL);
4916#endif
4917 }
4918
4919 ui_breakcheck();
4920 if (got_int)
4921 {
4922 (void)vgetc(); /* only break the file loading, not the rest */
4923 break;
4924 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004925#ifdef FEAT_EVAL
4926 /* Autocommands deleted the buffer or aborted script processing!!! */
4927 if (aborting())
4928 break;
4929#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004930#ifdef FEAT_WINDOWS
4931 /* When ":tab" was used open a new tab for a new window repeatedly. */
4932 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4933 cmdmod.tab = 9999;
4934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
4936#ifdef FEAT_AUTOCMD
4937 --autocmd_no_enter;
4938#endif
4939 win_enter(firstwin, FALSE); /* back to first window */
4940#ifdef FEAT_AUTOCMD
4941 --autocmd_no_leave;
4942#endif
4943
4944 /*
4945 * Close superfluous windows.
4946 */
4947 for (wp = lastwin; open_wins > count; )
4948 {
4949 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4950 || autowrite(wp->w_buffer, FALSE) == OK);
4951#ifdef FEAT_AUTOCMD
4952 if (!win_valid(wp))
4953 {
4954 /* BufWrite Autocommands made the window invalid, start over */
4955 wp = lastwin;
4956 }
4957 else
4958#endif
4959 if (r)
4960 {
4961 win_close(wp, !P_HID(wp->w_buffer));
4962 --open_wins;
4963 wp = lastwin;
4964 }
4965 else
4966 {
4967 wp = wp->w_prev;
4968 if (wp == NULL)
4969 break;
4970 }
4971 }
4972}
4973# endif /* FEAT_LISTCMDS */
4974
4975#endif /* FEAT_WINDOWS */
4976
Bram Moolenaara3227e22006-03-08 21:32:40 +00004977static int chk_modeline __ARGS((linenr_T, int));
4978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979/*
4980 * do_modelines() - process mode lines for the current file
4981 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004982 * "flags" can be:
4983 * OPT_WINONLY only set options local to window
4984 * OPT_NOWIN don't set options local to window
4985 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 * Returns immediately if the "ml" option isn't set.
4987 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004989do_modelines(flags)
4990 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004992 linenr_T lnum;
4993 int nmlines;
4994 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
4996 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4997 return;
4998
4999 /* Disallow recursive entry here. Can happen when executing a modeline
5000 * triggers an autocommand, which reloads modelines with a ":do". */
5001 if (entered)
5002 return;
5003
5004 ++entered;
5005 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
5006 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005007 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 nmlines = 0;
5009
5010 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
5011 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00005012 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 nmlines = 0;
5014 --entered;
5015}
5016
5017#include "version.h" /* for version number */
5018
5019/*
5020 * chk_modeline() - check a single line for a mode string
5021 * Return FAIL if an error encountered.
5022 */
5023 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005024chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005026 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027{
5028 char_u *s;
5029 char_u *e;
5030 char_u *linecopy; /* local copy of any modeline found */
5031 int prev;
5032 int vers;
5033 int end;
5034 int retval = OK;
5035 char_u *save_sourcing_name;
5036 linenr_T save_sourcing_lnum;
5037#ifdef FEAT_EVAL
5038 scid_T save_SID;
5039#endif
5040
5041 prev = -1;
5042 for (s = ml_get(lnum); *s != NUL; ++s)
5043 {
5044 if (prev == -1 || vim_isspace(prev))
5045 {
5046 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5047 || STRNCMP(s, "vi:", (size_t)3) == 0)
5048 break;
5049 if (STRNCMP(s, "vim", 3) == 0)
5050 {
5051 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5052 e = s + 4;
5053 else
5054 e = s + 3;
5055 vers = getdigits(&e);
5056 if (*e == ':'
5057 && (s[3] == ':'
5058 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5059 || (VIM_VERSION_100 < vers && s[3] == '<')
5060 || (VIM_VERSION_100 > vers && s[3] == '>')
5061 || (VIM_VERSION_100 == vers && s[3] == '=')))
5062 break;
5063 }
5064 }
5065 prev = *s;
5066 }
5067
5068 if (*s)
5069 {
5070 do /* skip over "ex:", "vi:" or "vim:" */
5071 ++s;
5072 while (s[-1] != ':');
5073
5074 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5075 if (linecopy == NULL)
5076 return FAIL;
5077
5078 save_sourcing_lnum = sourcing_lnum;
5079 save_sourcing_name = sourcing_name;
5080 sourcing_lnum = lnum; /* prepare for emsg() */
5081 sourcing_name = (char_u *)"modelines";
5082
5083 end = FALSE;
5084 while (end == FALSE)
5085 {
5086 s = skipwhite(s);
5087 if (*s == NUL)
5088 break;
5089
5090 /*
5091 * Find end of set command: ':' or end of line.
5092 * Skip over "\:", replacing it with ":".
5093 */
5094 for (e = s; *e != ':' && *e != NUL; ++e)
5095 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005096 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 if (*e == NUL)
5098 end = TRUE;
5099
5100 /*
5101 * If there is a "set" command, require a terminating ':' and
5102 * ignore the stuff after the ':'.
5103 * "vi:set opt opt opt: foo" -- foo not interpreted
5104 * "vi:opt opt opt: foo" -- foo interpreted
5105 * Accept "se" for compatibility with Elvis.
5106 */
5107 if (STRNCMP(s, "set ", (size_t)4) == 0
5108 || STRNCMP(s, "se ", (size_t)3) == 0)
5109 {
5110 if (*e != ':') /* no terminating ':'? */
5111 break;
5112 end = TRUE;
5113 s = vim_strchr(s, ' ') + 1;
5114 }
5115 *e = NUL; /* truncate the set command */
5116
5117 if (*s != NUL) /* skip over an empty "::" */
5118 {
5119#ifdef FEAT_EVAL
5120 save_SID = current_SID;
5121 current_SID = SID_MODELINE;
5122#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005123 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124#ifdef FEAT_EVAL
5125 current_SID = save_SID;
5126#endif
5127 if (retval == FAIL) /* stop if error found */
5128 break;
5129 }
5130 s = e + 1; /* advance to next part */
5131 }
5132
5133 sourcing_lnum = save_sourcing_lnum;
5134 sourcing_name = save_sourcing_name;
5135
5136 vim_free(linecopy);
5137 }
5138 return retval;
5139}
5140
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005141#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 int
5143read_viminfo_bufferlist(virp, writing)
5144 vir_T *virp;
5145 int writing;
5146{
5147 char_u *tab;
5148 linenr_T lnum;
5149 colnr_T col;
5150 buf_T *buf;
5151 char_u *sfname;
5152 char_u *xline;
5153
5154 /* Handle long line and escaped characters. */
5155 xline = viminfo_readstring(virp, 1, FALSE);
5156
5157 /* don't read in if there are files on the command-line or if writing: */
5158 if (xline != NULL && !writing && ARGCOUNT == 0
5159 && find_viminfo_parameter('%') != NULL)
5160 {
5161 /* Format is: <fname> Tab <lnum> Tab <col>.
5162 * Watch out for a Tab in the file name, work from the end. */
5163 lnum = 0;
5164 col = 0;
5165 tab = vim_strrchr(xline, '\t');
5166 if (tab != NULL)
5167 {
5168 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005169 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 tab = vim_strrchr(xline, '\t');
5171 if (tab != NULL)
5172 {
5173 *tab++ = '\0';
5174 lnum = atol((char *)tab);
5175 }
5176 }
5177
5178 /* Expand "~/" in the file name at "line + 1" to a full path.
5179 * Then try shortening it by comparing with the current directory */
5180 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005181 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
5183 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5184 if (buf != NULL) /* just in case... */
5185 {
5186 buf->b_last_cursor.lnum = lnum;
5187 buf->b_last_cursor.col = col;
5188 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5189 }
5190 }
5191 vim_free(xline);
5192
5193 return viminfo_readline(virp);
5194}
5195
5196 void
5197write_viminfo_bufferlist(fp)
5198 FILE *fp;
5199{
5200 buf_T *buf;
5201#ifdef FEAT_WINDOWS
5202 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005203 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204#endif
5205 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005206 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207
5208 if (find_viminfo_parameter('%') == NULL)
5209 return;
5210
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005211 /* Without a number -1 is returned: do all buffers. */
5212 max_buffers = get_viminfo_parameter('%');
5213
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005215#define LINE_BUF_LEN (MAXPATHL + 40)
5216 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 if (line == NULL)
5218 return;
5219
5220#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005221 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 set_last_cursor(win);
5223#else
5224 set_last_cursor(curwin);
5225#endif
5226
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005227 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5229 {
5230 if (buf->b_fname == NULL
5231 || !buf->b_p_bl
5232#ifdef FEAT_QUICKFIX
5233 || bt_quickfix(buf)
5234#endif
5235 || removable(buf->b_ffname))
5236 continue;
5237
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005238 if (max_buffers-- == 0)
5239 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 putc('%', fp);
5241 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005242 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 (long)buf->b_last_cursor.lnum,
5244 buf->b_last_cursor.col);
5245 viminfo_writestring(fp, line);
5246 }
5247 vim_free(line);
5248}
5249#endif
5250
5251
5252/*
5253 * Return special buffer name.
5254 * Returns NULL when the buffer has a normal file name.
5255 */
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005256 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257buf_spname(buf)
5258 buf_T *buf;
5259{
5260#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5261 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005262 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005263 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005264 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005265
5266 /*
5267 * For location list window, w_llist_ref points to the location list.
5268 * For quickfix window, w_llist_ref is NULL.
5269 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005270 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005271 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005272 goto win_found;
5273win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005274 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005275 return (char_u *)_(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005276 else
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005277 return (char_u *)_(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005278 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005279#endif
5280#ifdef FEAT_QUICKFIX
5281 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5282 * contains the name as specified by the user */
5283 if (bt_nofile(buf))
5284 {
5285 if (buf->b_sfname != NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005286 return buf->b_sfname;
5287 return (char_u *)_("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
5289#endif
5290 if (buf->b_fname == NULL)
Bram Moolenaare1704ba2012-10-03 18:25:00 +02005291 return (char_u *)_("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 return NULL;
5293}
5294
5295
5296#if defined(FEAT_SIGNS) || defined(PROTO)
5297/*
5298 * Insert the sign into the signlist.
5299 */
5300 static void
5301insert_sign(buf, prev, next, id, lnum, typenr)
5302 buf_T *buf; /* buffer to store sign in */
5303 signlist_T *prev; /* previous sign entry */
5304 signlist_T *next; /* next sign entry */
5305 int id; /* sign ID */
5306 linenr_T lnum; /* line number which gets the mark */
5307 int typenr; /* typenr of sign we are adding */
5308{
5309 signlist_T *newsign;
5310
5311 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5312 if (newsign != NULL)
5313 {
5314 newsign->id = id;
5315 newsign->lnum = lnum;
5316 newsign->typenr = typenr;
5317 newsign->next = next;
5318#ifdef FEAT_NETBEANS_INTG
5319 newsign->prev = prev;
5320 if (next != NULL)
5321 next->prev = newsign;
5322#endif
5323
5324 if (prev == NULL)
5325 {
5326 /* When adding first sign need to redraw the windows to create the
5327 * column for signs. */
5328 if (buf->b_signlist == NULL)
5329 {
5330 redraw_buf_later(buf, NOT_VALID);
5331 changed_cline_bef_curs();
5332 }
5333
5334 /* first sign in signlist */
5335 buf->b_signlist = newsign;
5336 }
5337 else
5338 prev->next = newsign;
5339 }
5340}
5341
5342/*
5343 * Add the sign into the signlist. Find the right spot to do it though.
5344 */
5345 void
5346buf_addsign(buf, id, lnum, typenr)
5347 buf_T *buf; /* buffer to store sign in */
5348 int id; /* sign ID */
5349 linenr_T lnum; /* line number which gets the mark */
5350 int typenr; /* typenr of sign we are adding */
5351{
5352 signlist_T *sign; /* a sign in the signlist */
5353 signlist_T *prev; /* the previous sign */
5354
5355 prev = NULL;
5356 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5357 {
5358 if (lnum == sign->lnum && id == sign->id)
5359 {
5360 sign->typenr = typenr;
5361 return;
5362 }
5363 else if (
5364#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5365 id < 0 &&
5366#endif
5367 lnum < sign->lnum)
5368 {
5369#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5370 /* XXX - GRP: Is this because of sign slide problem? Or is it
5371 * really needed? Or is it because we allow multiple signs per
5372 * line? If so, should I add that feature to FEAT_SIGNS?
5373 */
5374 while (prev != NULL && prev->lnum == lnum)
5375 prev = prev->prev;
5376 if (prev == NULL)
5377 sign = buf->b_signlist;
5378 else
5379 sign = prev->next;
5380#endif
5381 insert_sign(buf, prev, sign, id, lnum, typenr);
5382 return;
5383 }
5384 prev = sign;
5385 }
5386#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5387 /* XXX - GRP: See previous comment */
5388 while (prev != NULL && prev->lnum == lnum)
5389 prev = prev->prev;
5390 if (prev == NULL)
5391 sign = buf->b_signlist;
5392 else
5393 sign = prev->next;
5394#endif
5395 insert_sign(buf, prev, sign, id, lnum, typenr);
5396
5397 return;
5398}
5399
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005400 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005401buf_change_sign_type(buf, markId, typenr)
5402 buf_T *buf; /* buffer to store sign in */
5403 int markId; /* sign ID */
5404 int typenr; /* typenr of sign we are adding */
5405{
5406 signlist_T *sign; /* a sign in the signlist */
5407
5408 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5409 {
5410 if (sign->id == markId)
5411 {
5412 sign->typenr = typenr;
5413 return sign->lnum;
5414 }
5415 }
5416
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005417 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418}
5419
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005420 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421buf_getsigntype(buf, lnum, type)
5422 buf_T *buf;
5423 linenr_T lnum;
5424 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5425{
5426 signlist_T *sign; /* a sign in a b_signlist */
5427
5428 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5429 if (sign->lnum == lnum
5430 && (type == SIGN_ANY
5431# ifdef FEAT_SIGN_ICONS
5432 || (type == SIGN_ICON
5433 && sign_get_image(sign->typenr) != NULL)
5434# endif
5435 || (type == SIGN_TEXT
5436 && sign_get_text(sign->typenr) != NULL)
5437 || (type == SIGN_LINEHL
5438 && sign_get_attr(sign->typenr, TRUE) != 0)))
5439 return sign->typenr;
5440 return 0;
5441}
5442
5443
5444 linenr_T
5445buf_delsign(buf, id)
5446 buf_T *buf; /* buffer sign is stored in */
5447 int id; /* sign id */
5448{
5449 signlist_T **lastp; /* pointer to pointer to current sign */
5450 signlist_T *sign; /* a sign in a b_signlist */
5451 signlist_T *next; /* the next sign in a b_signlist */
5452 linenr_T lnum; /* line number whose sign was deleted */
5453
5454 lastp = &buf->b_signlist;
5455 lnum = 0;
5456 for (sign = buf->b_signlist; sign != NULL; sign = next)
5457 {
5458 next = sign->next;
5459 if (sign->id == id)
5460 {
5461 *lastp = next;
5462#ifdef FEAT_NETBEANS_INTG
5463 if (next != NULL)
5464 next->prev = sign->prev;
5465#endif
5466 lnum = sign->lnum;
5467 vim_free(sign);
5468 break;
5469 }
5470 else
5471 lastp = &sign->next;
5472 }
5473
5474 /* When deleted the last sign need to redraw the windows to remove the
5475 * sign column. */
5476 if (buf->b_signlist == NULL)
5477 {
5478 redraw_buf_later(buf, NOT_VALID);
5479 changed_cline_bef_curs();
5480 }
5481
5482 return lnum;
5483}
5484
5485
5486/*
5487 * Find the line number of the sign with the requested id. If the sign does
5488 * not exist, return 0 as the line number. This will still let the correct file
5489 * get loaded.
5490 */
5491 int
5492buf_findsign(buf, id)
5493 buf_T *buf; /* buffer to store sign in */
5494 int id; /* sign ID */
5495{
5496 signlist_T *sign; /* a sign in the signlist */
5497
5498 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5499 if (sign->id == id)
5500 return sign->lnum;
5501
5502 return 0;
5503}
5504
5505 int
5506buf_findsign_id(buf, lnum)
5507 buf_T *buf; /* buffer whose sign we are searching for */
5508 linenr_T lnum; /* line number of sign */
5509{
5510 signlist_T *sign; /* a sign in the signlist */
5511
5512 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5513 if (sign->lnum == lnum)
5514 return sign->id;
5515
5516 return 0;
5517}
5518
5519
5520# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5521/* see if a given type of sign exists on a specific line */
5522 int
5523buf_findsigntype_id(buf, lnum, typenr)
5524 buf_T *buf; /* buffer whose sign we are searching for */
5525 linenr_T lnum; /* line number of sign */
5526 int typenr; /* sign type number */
5527{
5528 signlist_T *sign; /* a sign in the signlist */
5529
5530 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5531 if (sign->lnum == lnum && sign->typenr == typenr)
5532 return sign->id;
5533
5534 return 0;
5535}
5536
5537
5538# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5539/* return the number of icons on the given line */
5540 int
5541buf_signcount(buf, lnum)
5542 buf_T *buf;
5543 linenr_T lnum;
5544{
5545 signlist_T *sign; /* a sign in the signlist */
5546 int count = 0;
5547
5548 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5549 if (sign->lnum == lnum)
5550 if (sign_get_image(sign->typenr) != NULL)
5551 count++;
5552
5553 return count;
5554}
5555# endif /* FEAT_SIGN_ICONS */
5556# endif /* FEAT_NETBEANS_INTG */
5557
5558
5559/*
5560 * Delete signs in buffer "buf".
5561 */
Bram Moolenaarf65e5662012-07-10 15:18:22 +02005562 void
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563buf_delete_signs(buf)
5564 buf_T *buf;
5565{
5566 signlist_T *next;
5567
5568 while (buf->b_signlist != NULL)
5569 {
5570 next = buf->b_signlist->next;
5571 vim_free(buf->b_signlist);
5572 buf->b_signlist = next;
5573 }
5574}
5575
5576/*
5577 * Delete all signs in all buffers.
5578 */
5579 void
5580buf_delete_all_signs()
5581{
5582 buf_T *buf; /* buffer we are checking for signs */
5583
5584 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5585 if (buf->b_signlist != NULL)
5586 {
5587 /* Need to redraw the windows to remove the sign column. */
5588 redraw_buf_later(buf, NOT_VALID);
5589 buf_delete_signs(buf);
5590 }
5591}
5592
5593/*
5594 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5595 */
5596 void
5597sign_list_placed(rbuf)
5598 buf_T *rbuf;
5599{
5600 buf_T *buf;
5601 signlist_T *p;
5602 char lbuf[BUFSIZ];
5603
5604 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5605 msg_putchar('\n');
5606 if (rbuf == NULL)
5607 buf = firstbuf;
5608 else
5609 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005610 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 {
5612 if (buf->b_signlist != NULL)
5613 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005614 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5616 msg_putchar('\n');
5617 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005618 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005620 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005621 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5622 MSG_PUTS(lbuf);
5623 msg_putchar('\n');
5624 }
5625 if (rbuf != NULL)
5626 break;
5627 buf = buf->b_next;
5628 }
5629}
5630
5631/*
5632 * Adjust a placed sign for inserted/deleted lines.
5633 */
5634 void
5635sign_mark_adjust(line1, line2, amount, amount_after)
5636 linenr_T line1;
5637 linenr_T line2;
5638 long amount;
5639 long amount_after;
5640{
5641 signlist_T *sign; /* a sign in a b_signlist */
5642
5643 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5644 {
5645 if (sign->lnum >= line1 && sign->lnum <= line2)
5646 {
5647 if (amount == MAXLNUM)
5648 sign->lnum = line1;
5649 else
5650 sign->lnum += amount;
5651 }
5652 else if (sign->lnum > line2)
5653 sign->lnum += amount_after;
5654 }
5655}
5656#endif /* FEAT_SIGNS */
5657
5658/*
5659 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5660 */
5661 void
5662set_buflisted(on)
5663 int on;
5664{
5665 if (on != curbuf->b_p_bl)
5666 {
5667 curbuf->b_p_bl = on;
5668#ifdef FEAT_AUTOCMD
5669 if (on)
5670 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5671 else
5672 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5673#endif
5674 }
5675}
5676
5677/*
5678 * Read the file for "buf" again and check if the contents changed.
5679 * Return TRUE if it changed or this could not be checked.
5680 */
5681 int
5682buf_contents_changed(buf)
5683 buf_T *buf;
5684{
5685 buf_T *newbuf;
5686 int differ = TRUE;
5687 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 exarg_T ea;
5690
5691 /* Allocate a buffer without putting it in the buffer list. */
5692 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5693 if (newbuf == NULL)
5694 return TRUE;
5695
5696 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5697 if (prep_exarg(&ea, buf) == FAIL)
5698 {
5699 wipe_buffer(newbuf, FALSE);
5700 return TRUE;
5701 }
5702
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 /* set curwin/curbuf to buf and save a few things */
5704 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705
Bram Moolenaar4770d092006-01-12 23:22:24 +00005706 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 && readfile(buf->b_ffname, buf->b_fname,
5708 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5709 &ea, READ_NEW | READ_DUMMY) == OK)
5710 {
5711 /* compare the two files line by line */
5712 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5713 {
5714 differ = FALSE;
5715 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5716 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5717 {
5718 differ = TRUE;
5719 break;
5720 }
5721 }
5722 }
5723 vim_free(ea.cmd);
5724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 /* restore curwin/curbuf and a few other things */
5726 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
5728 if (curbuf != newbuf) /* safety check */
5729 wipe_buffer(newbuf, FALSE);
5730
5731 return differ;
5732}
5733
5734/*
5735 * Wipe out a buffer and decrement the last buffer number if it was used for
5736 * this buffer. Call this to wipe out a temp buffer that does not contain any
5737 * marks.
5738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 void
5740wipe_buffer(buf, aucmd)
5741 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005742 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743{
5744 if (buf->b_fnum == top_file_num - 1)
5745 --top_file_num;
5746
5747#ifdef FEAT_AUTOCMD
5748 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005749 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005751 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752#ifdef FEAT_AUTOCMD
5753 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005754 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005755#endif
5756}