blob: c84e6d58502e218735128785e5fad03efa08f31a [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));
60static void buf_delete_signs __ARGS((buf_T *buf));
61#endif
62
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
64static char *msg_loclist = N_("[Location List]");
65static char *msg_qflist = N_("[Quickfix List]");
66#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +010067#ifdef FEAT_AUTOCMD
68static char *e_auabort = N_("E855: Autocommands caused command to abort");
69#endif
Bram Moolenaar7fd73202010-07-25 16:58:46 +020070
Bram Moolenaar071d4272004-06-13 20:20:40 +000071/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020072 * Open current buffer, that is: open the memfile and read the file into
73 * memory.
74 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 */
76 int
Bram Moolenaar59f931e2010-07-24 20:27:03 +020077open_buffer(read_stdin, eap, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 int read_stdin; /* read file from stdin */
79 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
Bram Moolenaar59f931e2010-07-24 20:27:03 +020080 int flags; /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081{
82 int retval = OK;
83#ifdef FEAT_AUTOCMD
84 buf_T *old_curbuf;
85#endif
86
87 /*
88 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
89 * When re-entering the same buffer, it should not change, because the
90 * user may have reset the flag by hand.
91 */
92 if (readonlymode && curbuf->b_ffname != NULL
93 && (curbuf->b_flags & BF_NEVERLOADED))
94 curbuf->b_p_ro = TRUE;
95
Bram Moolenaar4770d092006-01-12 23:22:24 +000096 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 {
98 /*
99 * There MUST be a memfile, otherwise we can't do anything
100 * If we can't create one for the current buffer, take another buffer
101 */
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100102 close_buffer(NULL, curbuf, 0, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
104 if (curbuf->b_ml.ml_mfp != NULL)
105 break;
106 /*
107 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000108 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 */
110 if (curbuf == NULL)
111 {
112 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
113 getout(2);
114 }
115 EMSG(_("E83: Cannot allocate buffer, using other one..."));
116 enter_buffer(curbuf);
117 return FAIL;
118 }
119
120#ifdef FEAT_AUTOCMD
121 /* The autocommands in readfile() may change the buffer, but only AFTER
122 * reading the file. */
123 old_curbuf = curbuf;
124 modified_was_set = FALSE;
125#endif
126
127 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100128 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
130 if (curbuf->b_ffname != NULL
131#ifdef FEAT_NETBEANS_INTG
132 && netbeansReadFile
133#endif
134 )
135 {
136#ifdef FEAT_NETBEANS_INTG
137 int oldFire = netbeansFireChanges;
138
139 netbeansFireChanges = 0;
140#endif
141 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200142 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
143 flags | READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000144#ifdef FEAT_NETBEANS_INTG
145 netbeansFireChanges = oldFire;
146#endif
147 /* Help buffer is filtered. */
148 if (curbuf->b_help)
149 fix_help_buffer();
150 }
151 else if (read_stdin)
152 {
153 int save_bin = curbuf->b_p_bin;
154 linenr_T line_count;
155
156 /*
157 * First read the text in binary mode into the buffer.
158 * Then read from that same buffer and append at the end. This makes
159 * it possible to retry when 'fileformat' or 'fileencoding' was
160 * guessed wrong.
161 */
162 curbuf->b_p_bin = TRUE;
163 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200164 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
165 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000166 curbuf->b_p_bin = save_bin;
167 if (retval == OK)
168 {
169 line_count = curbuf->b_ml.ml_line_count;
170 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200171 (linenr_T)0, (linenr_T)MAXLNUM, eap,
172 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 if (retval == OK)
174 {
175 /* Delete the binary lines. */
176 while (--line_count >= 0)
177 ml_delete((linenr_T)1, FALSE);
178 }
179 else
180 {
181 /* Delete the converted lines. */
182 while (curbuf->b_ml.ml_line_count > line_count)
183 ml_delete(line_count, FALSE);
184 }
185 /* Put the cursor on the first line. */
186 curwin->w_cursor.lnum = 1;
187 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000188
189 /* Set or reset 'modified' before executing autocommands, so that
190 * it can be changed there. */
191 if (!readonlymode && !bufempty())
192 changed();
193 else if (retval != FAIL)
194 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195#ifdef FEAT_AUTOCMD
196# ifdef FEAT_EVAL
197 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
198 curbuf, &retval);
199# else
200 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
201# endif
202#endif
203 }
204 }
205
206 /* if first time loading this buffer, init b_chartab[] */
207 if (curbuf->b_flags & BF_NEVERLOADED)
208 (void)buf_init_chartab(curbuf, FALSE);
209
210 /*
211 * Set/reset the Changed flag first, autocmds may change the buffer.
212 * Apply the automatic commands, before processing the modelines.
213 * So the modelines have priority over auto commands.
214 */
215 /* When reading stdin, the buffer contents always needs writing, so set
216 * the changed flag. Unless in readonly mode: "ls | gview -".
217 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000218 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219#ifdef FEAT_AUTOCMD
220 || modified_was_set /* ":set modified" used in autocmd */
221# ifdef FEAT_EVAL
222 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
223# endif
224#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000225 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000227 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 unchanged(curbuf, FALSE);
229 save_file_ff(curbuf); /* keep this fileformat */
230
231 /* require "!" to overwrite the file, because it wasn't read completely */
232#ifdef FEAT_EVAL
233 if (aborting())
234#else
235 if (got_int)
236#endif
237 curbuf->b_flags |= BF_READERR;
238
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000239#ifdef FEAT_FOLDING
240 /* Need to update automatic folding. Do this before the autocommands,
241 * they may use the fold info. */
242 foldUpdateAll(curwin);
243#endif
244
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245#ifdef FEAT_AUTOCMD
246 /* need to set w_topline, unless some autocommand already did that. */
247 if (!(curwin->w_valid & VALID_TOPLINE))
248 {
249 curwin->w_topline = 1;
250# ifdef FEAT_DIFF
251 curwin->w_topfill = 0;
252# endif
253 }
254# ifdef FEAT_EVAL
255 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
256# else
257 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
258# endif
259#endif
260
261 if (retval != FAIL)
262 {
263#ifdef FEAT_AUTOCMD
264 /*
265 * The autocommands may have changed the current buffer. Apply the
266 * modelines to the correct buffer, if it still exists and is loaded.
267 */
268 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
269 {
270 aco_save_T aco;
271
272 /* Go to the buffer that was opened. */
273 aucmd_prepbuf(&aco, old_curbuf);
274#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000275 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
277
278#ifdef FEAT_AUTOCMD
279# ifdef FEAT_EVAL
280 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
281 &retval);
282# else
283 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
284# endif
285
286 /* restore curwin/curbuf and a few other things */
287 aucmd_restbuf(&aco);
288 }
289#endif
290 }
291
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 return retval;
293}
294
295/*
296 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
297 */
298 int
299buf_valid(buf)
300 buf_T *buf;
301{
302 buf_T *bp;
303
304 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
305 if (bp == buf)
306 return TRUE;
307 return FALSE;
308}
309
310/*
311 * Close the link to a buffer.
312 * "action" is used when there is no longer a window for the buffer.
313 * It can be:
314 * 0 buffer becomes hidden
315 * DOBUF_UNLOAD buffer is unloaded
316 * DOBUF_DELETE buffer is unloaded and removed from buffer list
317 * DOBUF_WIPE buffer is unloaded and really deleted
318 * When doing all but the first one on the current buffer, the caller should
319 * get a new buffer very soon!
320 *
321 * The 'bufhidden' option can force freeing and deleting.
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100322 *
323 * When "abort_if_last" is TRUE then do not close the buffer if autocommands
324 * cause there to be only one window with this buffer. e.g. when ":quit" is
325 * supposed to close the window but autocommands close all other windows.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 */
327 void
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100328close_buffer(win, buf, action, abort_if_last)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 win_T *win; /* if not NULL, set b_last_cursor */
330 buf_T *buf;
331 int action;
Bram Moolenaar5fbe6992012-03-07 22:52:36 +0100332 int abort_if_last UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333{
334#ifdef FEAT_AUTOCMD
335 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100336 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000337#endif
338 int unload_buf = (action != 0);
339 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
340 int wipe_buf = (action == DOBUF_WIPE);
341
342#ifdef FEAT_QUICKFIX
343 /*
344 * Force unloading or deleting when 'bufhidden' says so.
345 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
346 * "hide" (otherwise we could never free or delete a buffer).
347 */
348 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
349 {
350 del_buf = TRUE;
351 unload_buf = TRUE;
352 }
353 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
354 {
355 del_buf = TRUE;
356 unload_buf = TRUE;
357 wipe_buf = TRUE;
358 }
359 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
360 unload_buf = TRUE;
361#endif
362
363 if (win != NULL)
364 {
365 /* Set b_last_cursor when closing the last window for the buffer.
366 * Remember the last cursor position and window options of the buffer.
367 * This used to be only for the current window, but then options like
368 * 'foldmethod' may be lost with a ":only" command. */
369 if (buf->b_nwindows == 1)
370 set_last_cursor(win);
371 buflist_setfpos(buf, win,
372 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
373 win->w_cursor.col, TRUE);
374 }
375
376#ifdef FEAT_AUTOCMD
377 /* When the buffer is no longer in a window, trigger BufWinLeave */
378 if (buf->b_nwindows == 1)
379 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200380 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
382 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200383 if (!buf_valid(buf))
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100384 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200385 /* Autocommands deleted the buffer. */
386aucmd_abort:
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100387 EMSG(_(e_auabort));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 return;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100389 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200390 buf->b_closing = FALSE;
391 if (abort_if_last && one_window())
392 /* Autocommands made this the only window. */
393 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394
395 /* When the buffer becomes hidden, but is not unloaded, trigger
396 * BufHidden */
397 if (!unload_buf)
398 {
Bram Moolenaar362ce482012-06-06 19:02:45 +0200399 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000400 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
401 FALSE, buf);
Bram Moolenaar362ce482012-06-06 19:02:45 +0200402 if (!buf_valid(buf))
403 /* Autocommands deleted the buffer. */
404 goto aucmd_abort;
405 buf->b_closing = FALSE;
406 if (abort_if_last && one_window())
407 /* Autocommands made this the only window. */
408 goto aucmd_abort;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 }
410# ifdef FEAT_EVAL
411 if (aborting()) /* autocmds may abort script processing */
412 return;
413# endif
414 }
415 nwindows = buf->b_nwindows;
416#endif
417
418 /* decrease the link count from windows (unless not in any window) */
419 if (buf->b_nwindows > 0)
420 --buf->b_nwindows;
421
422 /* Return when a window is displaying the buffer or when it's not
423 * unloaded. */
424 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
427 /* Always remove the buffer when there is no file name. */
428 if (buf->b_ffname == NULL)
429 del_buf = TRUE;
430
431 /*
432 * Free all things allocated for this buffer.
433 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
434 */
435#ifdef FEAT_AUTOCMD
436 /* Remember if we are closing the current buffer. Restore the number of
437 * windows, so that autocommands in buf_freeall() don't get confused. */
438 is_curbuf = (buf == curbuf);
439 buf->b_nwindows = nwindows;
440#endif
441
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200442 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200443 if (
444#ifdef FEAT_WINDOWS
445 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100446#else
447 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200448#endif
449 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200450 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451
452#ifdef FEAT_AUTOCMD
453 /* Autocommands may have deleted the buffer. */
454 if (!buf_valid(buf))
455 return;
456# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000457 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 return;
459# endif
460
461 /* Autocommands may have opened or closed windows for this buffer.
462 * Decrement the count for the close we do here. */
463 if (buf->b_nwindows > 0)
464 --buf->b_nwindows;
465
466 /*
467 * It's possible that autocommands change curbuf to the one being deleted.
468 * This might cause the previous curbuf to be deleted unexpectedly. But
469 * in some cases it's OK to delete the curbuf, because a new one is
470 * obtained anyway. Therefore only return if curbuf changed to the
471 * deleted buffer.
472 */
473 if (buf == curbuf && !is_curbuf)
474 return;
475#endif
476
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000477 /* Change directories when the 'acd' option is set. */
478 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000479
480 /*
481 * Remove the buffer from the list.
482 */
483 if (wipe_buf)
484 {
485#ifdef FEAT_SUN_WORKSHOP
486 if (usingSunWorkShop)
487 workshop_file_closed_lineno((char *)buf->b_ffname,
488 (int)buf->b_last_cursor.lnum);
489#endif
490 vim_free(buf->b_ffname);
491 vim_free(buf->b_sfname);
492 if (buf->b_prev == NULL)
493 firstbuf = buf->b_next;
494 else
495 buf->b_prev->b_next = buf->b_next;
496 if (buf->b_next == NULL)
497 lastbuf = buf->b_prev;
498 else
499 buf->b_next->b_prev = buf->b_prev;
500 free_buffer(buf);
501 }
502 else
503 {
504 if (del_buf)
505 {
506 /* Free all internal variables and reset option values, to make
507 * ":bdel" compatible with Vim 5.7. */
508 free_buffer_stuff(buf, TRUE);
509
510 /* Make it look like a new buffer. */
511 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
512
513 /* Init the options when loaded again. */
514 buf->b_p_initialized = FALSE;
515 }
516 buf_clear_file(buf);
517 if (del_buf)
518 buf->b_p_bl = FALSE;
519 }
520}
521
522/*
523 * Make buffer not contain a file.
524 */
525 void
526buf_clear_file(buf)
527 buf_T *buf;
528{
529 buf->b_ml.ml_line_count = 1;
530 unchanged(buf, TRUE);
531#ifndef SHORT_FNAME
532 buf->b_shortname = FALSE;
533#endif
534 buf->b_p_eol = TRUE;
535 buf->b_start_eol = TRUE;
536#ifdef FEAT_MBYTE
537 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000538 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000539#endif
540 buf->b_ml.ml_mfp = NULL;
541 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
542#ifdef FEAT_NETBEANS_INTG
543 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544#endif
545}
546
547/*
548 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200549 * the file. flags:
550 * BFA_DEL buffer is going to be deleted
551 * BFA_WIPE buffer is going to be wiped out
552 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200555buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200557 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558{
559#ifdef FEAT_AUTOCMD
560 int is_curbuf = (buf == curbuf);
561
Bram Moolenaar362ce482012-06-06 19:02:45 +0200562 buf->b_closing = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
564 if (!buf_valid(buf)) /* autocommands may delete the buffer */
565 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200566 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 {
568 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
569 if (!buf_valid(buf)) /* autocommands may delete the buffer */
570 return;
571 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200572 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
575 FALSE, buf);
576 if (!buf_valid(buf)) /* autocommands may delete the buffer */
577 return;
578 }
Bram Moolenaar362ce482012-06-06 19:02:45 +0200579 buf->b_closing = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000581 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 return;
583# endif
584
585 /*
586 * It's possible that autocommands change curbuf to the one being deleted.
587 * This might cause curbuf to be deleted unexpectedly. But in some cases
588 * it's OK to delete the curbuf, because a new one is obtained anyway.
589 * Therefore only return if curbuf changed to the deleted buffer.
590 */
591 if (buf == curbuf && !is_curbuf)
592 return;
593#endif
594#ifdef FEAT_DIFF
595 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
596#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200597#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100598 /* Remove any ownsyntax, unless exiting. */
599 if (firstwin != NULL && curwin->w_buffer == buf)
600 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200601#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000602
603#ifdef FEAT_FOLDING
604 /* No folds in an empty buffer. */
605# ifdef FEAT_WINDOWS
606 {
607 win_T *win;
608 tabpage_T *tp;
609
610 FOR_ALL_TAB_WINDOWS(tp, win)
611 if (win->w_buffer == buf)
612 clearFolding(win);
613 }
614# else
615 if (curwin->w_buffer == buf)
616 clearFolding(curwin);
617# endif
618#endif
619
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620#ifdef FEAT_TCL
621 tcl_buffer_free(buf);
622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 ml_close(buf, TRUE); /* close and delete the memline/memfile */
624 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200625 if ((flags & BFA_KEEP_UNDO) == 0)
626 {
627 u_blockfree(buf); /* free the memory allocated for undo */
628 u_clearall(buf); /* reset all undo information */
629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200631 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000633 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634}
635
636/*
637 * Free a buffer structure and the things it contains related to the buffer
638 * itself (not the file, that must have been done already).
639 */
640 static void
641free_buffer(buf)
642 buf_T *buf;
643{
644 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200645#ifdef FEAT_LUA
646 lua_buffer_free(buf);
647#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000648#ifdef FEAT_MZSCHEME
649 mzscheme_buffer_free(buf);
650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651#ifdef FEAT_PERL
652 perl_buf_free(buf);
653#endif
654#ifdef FEAT_PYTHON
655 python_buffer_free(buf);
656#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200657#ifdef FEAT_PYTHON3
658 python3_buffer_free(buf);
659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660#ifdef FEAT_RUBY
661 ruby_buffer_free(buf);
662#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000663#ifdef FEAT_AUTOCMD
664 aubuflocal_remove(buf);
665#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 vim_free(buf);
667}
668
669/*
670 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
671 */
672 static void
673free_buffer_stuff(buf, free_options)
674 buf_T *buf;
675 int free_options; /* free options as well */
676{
677 if (free_options)
678 {
679 clear_wininfo(buf); /* including window-local options */
680 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200681#ifdef FEAT_SPELL
682 ga_clear(&buf->b_s.b_langp);
683#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000684 }
685#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000686 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
687 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688#endif
689#ifdef FEAT_USR_CMDS
690 uc_clear(&buf->b_ucmds); /* clear local user commands */
691#endif
692#ifdef FEAT_SIGNS
693 buf_delete_signs(buf); /* delete any signs */
694#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000695#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200696 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698#ifdef FEAT_LOCALMAP
699 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
700 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
701#endif
702#ifdef FEAT_MBYTE
703 vim_free(buf->b_start_fenc);
704 buf->b_start_fenc = NULL;
705#endif
706}
707
708/*
709 * Free the b_wininfo list for buffer "buf".
710 */
711 static void
712clear_wininfo(buf)
713 buf_T *buf;
714{
715 wininfo_T *wip;
716
717 while (buf->b_wininfo != NULL)
718 {
719 wip = buf->b_wininfo;
720 buf->b_wininfo = wip->wi_next;
721 if (wip->wi_optset)
722 {
723 clear_winopt(&wip->wi_opt);
724#ifdef FEAT_FOLDING
725 deleteFoldRecurse(&wip->wi_folds);
726#endif
727 }
728 vim_free(wip);
729 }
730}
731
732#if defined(FEAT_LISTCMDS) || defined(PROTO)
733/*
734 * Go to another buffer. Handles the result of the ATTENTION dialog.
735 */
736 void
737goto_buffer(eap, start, dir, count)
738 exarg_T *eap;
739 int start;
740 int dir;
741 int count;
742{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000743# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 buf_T *old_curbuf = curbuf;
745
746 swap_exists_action = SEA_DIALOG;
747# endif
748 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
749 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000750# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000751 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
752 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000753# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
754 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000755
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000756 /* Reset the error/interrupt/exception state here so that
757 * aborting() returns FALSE when closing a window. */
758 enter_cleanup(&cs);
759# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000760
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000761 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 win_close(curwin, TRUE);
763 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000764 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000765
766# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
767 /* Restore the error/interrupt/exception state if not discarded by a
768 * new aborting error, interrupt, or uncaught exception. */
769 leave_cleanup(&cs);
770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 }
772 else
773 handle_swap_exists(old_curbuf);
774# endif
775}
776#endif
777
Bram Moolenaare64ac772005-12-07 20:54:59 +0000778#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779/*
780 * Handle the situation of swap_exists_action being set.
781 * It is allowed for "old_curbuf" to be NULL or invalid.
782 */
783 void
784handle_swap_exists(old_curbuf)
785 buf_T *old_curbuf;
786{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000787# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
788 cleanup_T cs;
789# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791 if (swap_exists_action == SEA_QUIT)
792 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000793# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
794 /* Reset the error/interrupt/exception state here so that
795 * aborting() returns FALSE when closing a buffer. */
796 enter_cleanup(&cs);
797# endif
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 /* User selected Quit at ATTENTION prompt. Go back to previous
800 * buffer. If that buffer is gone or the same as the current one,
801 * open a new, empty buffer. */
802 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000803 swap_exists_did_quit = TRUE;
Bram Moolenaar42ec6562012-02-22 14:58:37 +0100804 close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000806 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (old_curbuf != NULL)
808 enter_buffer(old_curbuf);
809 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000810
811# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
812 /* Restore the error/interrupt/exception state if not discarded by a
813 * new aborting error, interrupt, or uncaught exception. */
814 leave_cleanup(&cs);
815# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 }
817 else if (swap_exists_action == SEA_RECOVER)
818 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000819# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
820 /* Reset the error/interrupt/exception state here so that
821 * aborting() returns FALSE when closing a buffer. */
822 enter_cleanup(&cs);
823# endif
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 /* User selected Recover at ATTENTION prompt. */
826 msg_scroll = TRUE;
827 ml_recover();
828 MSG_PUTS("\n"); /* don't overwrite the last message */
829 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000830 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000831
832# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
833 /* Restore the error/interrupt/exception state if not discarded by a
834 * new aborting error, interrupt, or uncaught exception. */
835 leave_cleanup(&cs);
836# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 }
838 swap_exists_action = SEA_NONE;
839}
840#endif
841
842#if defined(FEAT_LISTCMDS) || defined(PROTO)
843/*
844 * do_bufdel() - delete or unload buffer(s)
845 *
846 * addr_count == 0: ":bdel" - delete current buffer
847 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
848 * buffer "end_bnr", then any other arguments.
849 * addr_count == 2: ":N,N bdel" - delete buffers in range
850 *
851 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
852 * DOBUF_DEL (":bdel")
853 *
854 * Returns error message or NULL
855 */
856 char_u *
857do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
858 int command;
859 char_u *arg; /* pointer to extra arguments */
860 int addr_count;
861 int start_bnr; /* first buffer number in a range */
862 int end_bnr; /* buffer nr or last buffer nr in a range */
863 int forceit;
864{
865 int do_current = 0; /* delete current buffer? */
866 int deleted = 0; /* number of buffers deleted */
867 char_u *errormsg = NULL; /* return value */
868 int bnr; /* buffer number */
869 char_u *p;
870
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 if (addr_count == 0)
872 {
873 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
874 }
875 else
876 {
877 if (addr_count == 2)
878 {
879 if (*arg) /* both range and argument is not allowed */
880 return (char_u *)_(e_trailing);
881 bnr = start_bnr;
882 }
883 else /* addr_count == 1 */
884 bnr = end_bnr;
885
886 for ( ;!got_int; ui_breakcheck())
887 {
888 /*
889 * delete the current buffer last, otherwise when the
890 * current buffer is deleted, the next buffer becomes
891 * the current one and will be loaded, which may then
892 * also be deleted, etc.
893 */
894 if (bnr == curbuf->b_fnum)
895 do_current = bnr;
896 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
897 forceit) == OK)
898 ++deleted;
899
900 /*
901 * find next buffer number to delete/unload
902 */
903 if (addr_count == 2)
904 {
905 if (++bnr > end_bnr)
906 break;
907 }
908 else /* addr_count == 1 */
909 {
910 arg = skipwhite(arg);
911 if (*arg == NUL)
912 break;
913 if (!VIM_ISDIGIT(*arg))
914 {
915 p = skiptowhite_esc(arg);
916 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
917 if (bnr < 0) /* failed */
918 break;
919 arg = p;
920 }
921 else
922 bnr = getdigits(&arg);
923 }
924 }
925 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
926 FORWARD, do_current, forceit) == OK)
927 ++deleted;
928
929 if (deleted == 0)
930 {
931 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000932 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000934 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000936 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 errormsg = IObuff;
938 }
939 else if (deleted >= p_report)
940 {
941 if (command == DOBUF_UNLOAD)
942 {
943 if (deleted == 1)
944 MSG(_("1 buffer unloaded"));
945 else
946 smsg((char_u *)_("%d buffers unloaded"), deleted);
947 }
948 else if (command == DOBUF_DEL)
949 {
950 if (deleted == 1)
951 MSG(_("1 buffer deleted"));
952 else
953 smsg((char_u *)_("%d buffers deleted"), deleted);
954 }
955 else
956 {
957 if (deleted == 1)
958 MSG(_("1 buffer wiped out"));
959 else
960 smsg((char_u *)_("%d buffers wiped out"), deleted);
961 }
962 }
963 }
964
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965
966 return errormsg;
967}
968
969/*
970 * Implementation of the commands for the buffer list.
971 *
972 * action == DOBUF_GOTO go to specified buffer
973 * action == DOBUF_SPLIT split window and go to specified buffer
974 * action == DOBUF_UNLOAD unload specified buffer(s)
975 * action == DOBUF_DEL delete specified buffer(s) from buffer list
976 * action == DOBUF_WIPE delete specified buffer(s) really
977 *
978 * start == DOBUF_CURRENT go to "count" buffer from current buffer
979 * start == DOBUF_FIRST go to "count" buffer from first buffer
980 * start == DOBUF_LAST go to "count" buffer from last buffer
981 * start == DOBUF_MOD go to "count" modified buffer from current buffer
982 *
983 * Return FAIL or OK.
984 */
985 int
986do_buffer(action, start, dir, count, forceit)
987 int action;
988 int start;
989 int dir; /* FORWARD or BACKWARD */
990 int count; /* buffer number or number of buffers */
991 int forceit; /* TRUE for :...! */
992{
993 buf_T *buf;
994 buf_T *bp;
995 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
996 || action == DOBUF_WIPE);
997
998 switch (start)
999 {
1000 case DOBUF_FIRST: buf = firstbuf; break;
1001 case DOBUF_LAST: buf = lastbuf; break;
1002 default: buf = curbuf; break;
1003 }
1004 if (start == DOBUF_MOD) /* find next modified buffer */
1005 {
1006 while (count-- > 0)
1007 {
1008 do
1009 {
1010 buf = buf->b_next;
1011 if (buf == NULL)
1012 buf = firstbuf;
1013 }
1014 while (buf != curbuf && !bufIsChanged(buf));
1015 }
1016 if (!bufIsChanged(buf))
1017 {
1018 EMSG(_("E84: No modified buffer found"));
1019 return FAIL;
1020 }
1021 }
1022 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
1023 {
1024 while (buf != NULL && buf->b_fnum != count)
1025 buf = buf->b_next;
1026 }
1027 else
1028 {
1029 bp = NULL;
1030 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1031 {
1032 /* remember the buffer where we start, we come back there when all
1033 * buffers are unlisted. */
1034 if (bp == NULL)
1035 bp = buf;
1036 if (dir == FORWARD)
1037 {
1038 buf = buf->b_next;
1039 if (buf == NULL)
1040 buf = firstbuf;
1041 }
1042 else
1043 {
1044 buf = buf->b_prev;
1045 if (buf == NULL)
1046 buf = lastbuf;
1047 }
1048 /* don't count unlisted buffers */
1049 if (unload || buf->b_p_bl)
1050 {
1051 --count;
1052 bp = NULL; /* use this buffer as new starting point */
1053 }
1054 if (bp == buf)
1055 {
1056 /* back where we started, didn't find anything. */
1057 EMSG(_("E85: There is no listed buffer"));
1058 return FAIL;
1059 }
1060 }
1061 }
1062
1063 if (buf == NULL) /* could not find it */
1064 {
1065 if (start == DOBUF_FIRST)
1066 {
1067 /* don't warn when deleting */
1068 if (!unload)
1069 EMSGN(_("E86: Buffer %ld does not exist"), count);
1070 }
1071 else if (dir == FORWARD)
1072 EMSG(_("E87: Cannot go beyond last buffer"));
1073 else
1074 EMSG(_("E88: Cannot go before first buffer"));
1075 return FAIL;
1076 }
1077
1078#ifdef FEAT_GUI
1079 need_mouse_correct = TRUE;
1080#endif
1081
1082#ifdef FEAT_LISTCMDS
1083 /*
1084 * delete buffer buf from memory and/or the list
1085 */
1086 if (unload)
1087 {
1088 int forward;
1089 int retval;
1090
1091 /* When unloading or deleting a buffer that's already unloaded and
1092 * unlisted: fail silently. */
1093 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1094 return FAIL;
1095
1096 if (!forceit && bufIsChanged(buf))
1097 {
1098#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1099 if ((p_confirm || cmdmod.confirm) && p_write)
1100 {
1101 dialog_changed(buf, FALSE);
1102# ifdef FEAT_AUTOCMD
1103 if (!buf_valid(buf))
1104 /* Autocommand deleted buffer, oops! It's not changed
1105 * now. */
1106 return FAIL;
1107# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001108 /* If it's still changed fail silently, the dialog already
1109 * mentioned why it fails. */
1110 if (bufIsChanged(buf))
1111 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001113 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#endif
1115 {
1116 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1117 buf->b_fnum);
1118 return FAIL;
1119 }
1120 }
1121
1122 /*
1123 * If deleting the last (listed) buffer, make it empty.
1124 * The last (listed) buffer cannot be unloaded.
1125 */
1126 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1127 if (bp->b_p_bl && bp != buf)
1128 break;
1129 if (bp == NULL && buf == curbuf)
1130 {
1131 if (action == DOBUF_UNLOAD)
1132 {
1133 EMSG(_("E90: Cannot unload last buffer"));
1134 return FAIL;
1135 }
1136
1137 /* Close any other windows on this buffer, then make it empty. */
1138#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001139 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140#endif
1141 setpcmark();
1142 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001143 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144
1145 /*
1146 * do_ecmd() may create a new buffer, then we have to delete
1147 * the old one. But do_ecmd() may have done that already, check
1148 * if the buffer still exists.
1149 */
1150 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001151 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 return retval;
1153 }
1154
1155#ifdef FEAT_WINDOWS
1156 /*
1157 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001158 * (unless it's the only window). Repeat this so long as we end up in
1159 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001161 while (buf == curbuf
Bram Moolenaar362ce482012-06-06 19:02:45 +02001162# ifdef FEAT_AUTOCMD
1163 && !(curwin->w_closing || curwin->w_buffer->b_closing)
1164# endif
Bram Moolenaarf740b292006-02-16 22:11:02 +00001165 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 win_close(curwin, FALSE);
1167#endif
1168
1169 /*
1170 * If the buffer to be deleted is not the current one, delete it here.
1171 */
1172 if (buf != curbuf)
1173 {
1174#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001175 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176#endif
1177 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001178 close_buffer(NULL, buf, action, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 return OK;
1180 }
1181
1182 /*
1183 * Deleting the current buffer: Need to find another buffer to go to.
1184 * There must be another, otherwise it would have been handled above.
1185 * First use au_new_curbuf, if it is valid.
1186 * Then prefer the buffer we most recently visited.
1187 * Else try to find one that is loaded, after the current buffer,
1188 * then before the current buffer.
1189 * Finally use any buffer.
1190 */
1191 buf = NULL; /* selected buffer */
1192 bp = NULL; /* used when no loaded buffer found */
1193#ifdef FEAT_AUTOCMD
1194 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1195 buf = au_new_curbuf;
1196# ifdef FEAT_JUMPLIST
1197 else
1198# endif
1199#endif
1200#ifdef FEAT_JUMPLIST
1201 if (curwin->w_jumplistlen > 0)
1202 {
1203 int jumpidx;
1204
1205 jumpidx = curwin->w_jumplistidx - 1;
1206 if (jumpidx < 0)
1207 jumpidx = curwin->w_jumplistlen - 1;
1208
1209 forward = jumpidx;
1210 while (jumpidx != curwin->w_jumplistidx)
1211 {
1212 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1213 if (buf != NULL)
1214 {
1215 if (buf == curbuf || !buf->b_p_bl)
1216 buf = NULL; /* skip current and unlisted bufs */
1217 else if (buf->b_ml.ml_mfp == NULL)
1218 {
1219 /* skip unloaded buf, but may keep it for later */
1220 if (bp == NULL)
1221 bp = buf;
1222 buf = NULL;
1223 }
1224 }
1225 if (buf != NULL) /* found a valid buffer: stop searching */
1226 break;
1227 /* advance to older entry in jump list */
1228 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1229 break;
1230 if (--jumpidx < 0)
1231 jumpidx = curwin->w_jumplistlen - 1;
1232 if (jumpidx == forward) /* List exhausted for sure */
1233 break;
1234 }
1235 }
1236#endif
1237
1238 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1239 {
1240 forward = TRUE;
1241 buf = curbuf->b_next;
1242 for (;;)
1243 {
1244 if (buf == NULL)
1245 {
1246 if (!forward) /* tried both directions */
1247 break;
1248 buf = curbuf->b_prev;
1249 forward = FALSE;
1250 continue;
1251 }
1252 /* in non-help buffer, try to skip help buffers, and vv */
1253 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1254 {
1255 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1256 break;
1257 if (bp == NULL) /* remember unloaded buf for later */
1258 bp = buf;
1259 }
1260 if (forward)
1261 buf = buf->b_next;
1262 else
1263 buf = buf->b_prev;
1264 }
1265 }
1266 if (buf == NULL) /* No loaded buffer, use unloaded one */
1267 buf = bp;
1268 if (buf == NULL) /* No loaded buffer, find listed one */
1269 {
1270 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1271 if (buf->b_p_bl && buf != curbuf)
1272 break;
1273 }
1274 if (buf == NULL) /* Still no buffer, just take one */
1275 {
1276 if (curbuf->b_next != NULL)
1277 buf = curbuf->b_next;
1278 else
1279 buf = curbuf->b_prev;
1280 }
1281 }
1282
1283 /*
1284 * make buf current buffer
1285 */
1286 if (action == DOBUF_SPLIT) /* split window first */
1287 {
1288# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001289 /* If 'switchbuf' contains "useopen": jump to first window containing
1290 * "buf" if one exists */
1291 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001292 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001293 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001294 * page containing "buf" if one exists */
1295 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 return OK;
1297 if (win_split(0, 0) == FAIL)
1298# endif
1299 return FAIL;
1300 }
1301#endif
1302
1303 /* go to current buffer - nothing to do */
1304 if (buf == curbuf)
1305 return OK;
1306
1307 /*
1308 * Check if the current buffer may be abandoned.
1309 */
1310 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1311 {
1312#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1313 if ((p_confirm || cmdmod.confirm) && p_write)
1314 {
1315 dialog_changed(curbuf, FALSE);
1316# ifdef FEAT_AUTOCMD
1317 if (!buf_valid(buf))
1318 /* Autocommand deleted buffer, oops! */
1319 return FAIL;
1320# endif
1321 }
1322 if (bufIsChanged(curbuf))
1323#endif
1324 {
1325 EMSG(_(e_nowrtmsg));
1326 return FAIL;
1327 }
1328 }
1329
1330 /* Go to the other buffer. */
1331 set_curbuf(buf, action);
1332
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001333#if defined(FEAT_LISTCMDS) \
1334 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001336 {
1337 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#endif
1340
1341#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1342 if (aborting()) /* autocmds may abort script processing */
1343 return FAIL;
1344#endif
1345
1346 return OK;
1347}
1348
1349#endif /* FEAT_LISTCMDS */
1350
1351/*
1352 * Set current buffer to "buf". Executes autocommands and closes current
1353 * buffer. "action" tells how to close the current buffer:
1354 * DOBUF_GOTO free or hide it
1355 * DOBUF_SPLIT nothing
1356 * DOBUF_UNLOAD unload it
1357 * DOBUF_DEL delete it
1358 * DOBUF_WIPE wipe it out
1359 */
1360 void
1361set_curbuf(buf, action)
1362 buf_T *buf;
1363 int action;
1364{
1365 buf_T *prevbuf;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001366#ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001367 win_T *prevwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001368#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1370 || action == DOBUF_WIPE);
1371
1372 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001373 if (!cmdmod.keepalt)
1374 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001375 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377#ifdef FEAT_VISUAL
1378 /* Don't restart Select mode after switching to another buffer. */
1379 VIsual_reselect = FALSE;
1380#endif
1381
1382 /* close_windows() or apply_autocmds() may change curbuf */
1383 prevbuf = curbuf;
1384
1385#ifdef FEAT_AUTOCMD
1386 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1387# ifdef FEAT_EVAL
1388 if (buf_valid(prevbuf) && !aborting())
1389# else
1390 if (buf_valid(prevbuf))
1391# endif
1392#endif
1393 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001394#ifdef FEAT_SYN_HL
1395 if (prevbuf == curwin->w_buffer)
1396 reset_synblock(curwin);
1397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#ifdef FEAT_WINDOWS
1399 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001400 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401#endif
1402#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1403 if (buf_valid(prevbuf) && !aborting())
1404#else
1405 if (buf_valid(prevbuf))
1406#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001407 {
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001408#ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001409 prevwin = curwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001410#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001411 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001412 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1414 unload ? action : (action == DOBUF_GOTO
1415 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001416 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001417#ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001418 if (curwin != prevwin && win_valid(prevwin))
1419 /* autocommands changed curwin, Grr! */
1420 curwin = prevwin;
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001421#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001422 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 }
1424#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001425 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001426 * it did ":bunload") or aborted the script processing!
1427 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1428 if ((buf_valid(buf) && buf != curbuf
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001429# ifdef FEAT_EVAL
Bram Moolenaar9e931222012-06-20 11:55:01 +02001430 && !aborting()
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001431# endif
1432# ifdef FEAT_WINDOWS
Bram Moolenaar9e931222012-06-20 11:55:01 +02001433 ) || curwin->w_buffer == NULL
Bram Moolenaar50a12b42012-06-20 17:54:38 +02001434# endif
Bram Moolenaar9e931222012-06-20 11:55:01 +02001435 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436#endif
1437 enter_buffer(buf);
1438}
1439
1440/*
1441 * Enter a new current buffer.
1442 * Old curbuf must have been abandoned already!
1443 */
1444 void
1445enter_buffer(buf)
1446 buf_T *buf;
1447{
1448 /* Copy buffer and window local option values. Not for a help buffer. */
1449 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1450 if (!buf->b_help)
1451 get_winopts(buf);
1452#ifdef FEAT_FOLDING
1453 else
1454 /* Remove all folds in the window. */
1455 clearFolding(curwin);
1456 foldUpdateAll(curwin); /* update folds (later). */
1457#endif
1458
1459 /* Get the buffer in the current window. */
1460 curwin->w_buffer = buf;
1461 curbuf = buf;
1462 ++curbuf->b_nwindows;
1463
1464#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001465 if (curwin->w_p_diff)
1466 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467#endif
1468
Bram Moolenaara971b822011-09-14 14:43:25 +02001469#ifdef FEAT_SYN_HL
1470 curwin->w_s = &(buf->b_s);
1471#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. */
1709#ifdef FEAT_KEYMAP
1710 /* need to reload lmaps and set b:keymap_name */
1711 curbuf->b_kmap_state |= KEYMAP_INIT;
1712#endif
1713 }
1714 else
1715 {
1716 /*
1717 * put new buffer at the end of the buffer list
1718 */
1719 buf->b_next = NULL;
1720 if (firstbuf == NULL) /* buffer list is empty */
1721 {
1722 buf->b_prev = NULL;
1723 firstbuf = buf;
1724 }
1725 else /* append new buffer at end of list */
1726 {
1727 lastbuf->b_next = buf;
1728 buf->b_prev = lastbuf;
1729 }
1730 lastbuf = buf;
1731
1732 buf->b_fnum = top_file_num++;
1733 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1734 {
1735 EMSG(_("W14: Warning: List of file names overflow"));
1736 if (emsg_silent == 0)
1737 {
1738 out_flush();
1739 ui_delay(3000L, TRUE); /* make sure it is noticed */
1740 }
1741 top_file_num = 1;
1742 }
1743
1744 /*
1745 * Always copy the options from the current buffer.
1746 */
1747 buf_copy_options(buf, BCO_ALWAYS);
1748 }
1749
1750 buf->b_wininfo->wi_fpos.lnum = lnum;
1751 buf->b_wininfo->wi_win = curwin;
1752
1753#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001754 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1755#endif
1756#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001757 hash_init(&buf->b_s.b_keywtab);
1758 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759#endif
1760
1761 buf->b_fname = buf->b_sfname;
1762#ifdef UNIX
1763 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001764 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 else
1766 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001767 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 buf->b_dev = st.st_dev;
1769 buf->b_ino = st.st_ino;
1770 }
1771#endif
1772 buf->b_u_synced = TRUE;
1773 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001774 if (flags & BLN_DUMMY)
1775 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 buf_clear_file(buf);
1777 clrallmarks(buf); /* clear marks */
1778 fmarks_check_names(buf); /* check file marks for this file */
1779 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1780#ifdef FEAT_AUTOCMD
1781 if (!(flags & BLN_DUMMY))
1782 {
1783 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1784 if (flags & BLN_LISTED)
1785 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1786# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001787 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 return NULL;
1789# endif
1790 }
1791#endif
1792
1793 return buf;
1794}
1795
1796/*
1797 * Free the memory for the options of a buffer.
1798 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1799 * 'fileencoding'.
1800 */
1801 void
1802free_buf_options(buf, free_p_ff)
1803 buf_T *buf;
1804 int free_p_ff;
1805{
1806 if (free_p_ff)
1807 {
1808#ifdef FEAT_MBYTE
1809 clear_string_option(&buf->b_p_fenc);
1810#endif
1811 clear_string_option(&buf->b_p_ff);
1812#ifdef FEAT_QUICKFIX
1813 clear_string_option(&buf->b_p_bh);
1814 clear_string_option(&buf->b_p_bt);
1815#endif
1816 }
1817#ifdef FEAT_FIND_ID
1818 clear_string_option(&buf->b_p_def);
1819 clear_string_option(&buf->b_p_inc);
1820# ifdef FEAT_EVAL
1821 clear_string_option(&buf->b_p_inex);
1822# endif
1823#endif
1824#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1825 clear_string_option(&buf->b_p_inde);
1826 clear_string_option(&buf->b_p_indk);
1827#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001828#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1829 clear_string_option(&buf->b_p_bexpr);
1830#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001831#if defined(FEAT_CRYPT)
1832 clear_string_option(&buf->b_p_cm);
1833#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001834#if defined(FEAT_EVAL)
1835 clear_string_option(&buf->b_p_fex);
1836#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837#ifdef FEAT_CRYPT
1838 clear_string_option(&buf->b_p_key);
1839#endif
1840 clear_string_option(&buf->b_p_kp);
1841 clear_string_option(&buf->b_p_mps);
1842 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001843 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 clear_string_option(&buf->b_p_isk);
1845#ifdef FEAT_KEYMAP
1846 clear_string_option(&buf->b_p_keymap);
1847 ga_clear(&buf->b_kmap_ga);
1848#endif
1849#ifdef FEAT_COMMENTS
1850 clear_string_option(&buf->b_p_com);
1851#endif
1852#ifdef FEAT_FOLDING
1853 clear_string_option(&buf->b_p_cms);
1854#endif
1855 clear_string_option(&buf->b_p_nf);
1856#ifdef FEAT_SYN_HL
1857 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001858#endif
1859#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001860 clear_string_option(&buf->b_s.b_p_spc);
1861 clear_string_option(&buf->b_s.b_p_spf);
1862 vim_free(buf->b_s.b_cap_prog);
1863 buf->b_s.b_cap_prog = NULL;
1864 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865#endif
1866#ifdef FEAT_SEARCHPATH
1867 clear_string_option(&buf->b_p_sua);
1868#endif
1869#ifdef FEAT_AUTOCMD
1870 clear_string_option(&buf->b_p_ft);
1871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872#ifdef FEAT_CINDENT
1873 clear_string_option(&buf->b_p_cink);
1874 clear_string_option(&buf->b_p_cino);
1875#endif
1876#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1877 clear_string_option(&buf->b_p_cinw);
1878#endif
1879#ifdef FEAT_INS_EXPAND
1880 clear_string_option(&buf->b_p_cpt);
1881#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001882#ifdef FEAT_COMPL_FUNC
1883 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001884 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886#ifdef FEAT_QUICKFIX
1887 clear_string_option(&buf->b_p_gp);
1888 clear_string_option(&buf->b_p_mp);
1889 clear_string_option(&buf->b_p_efm);
1890#endif
1891 clear_string_option(&buf->b_p_ep);
1892 clear_string_option(&buf->b_p_path);
1893 clear_string_option(&buf->b_p_tags);
1894#ifdef FEAT_INS_EXPAND
1895 clear_string_option(&buf->b_p_dict);
1896 clear_string_option(&buf->b_p_tsr);
1897#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001898#ifdef FEAT_TEXTOBJ
1899 clear_string_option(&buf->b_p_qe);
1900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 buf->b_p_ar = -1;
1902}
1903
1904/*
1905 * get alternate file n
1906 * set linenr to lnum or altfpos.lnum if lnum == 0
1907 * also set cursor column to altfpos.col if 'startofline' is not set.
1908 * if (options & GETF_SETMARK) call setpcmark()
1909 * if (options & GETF_ALT) we are jumping to an alternate file.
1910 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1911 *
1912 * return FAIL for failure, OK for success
1913 */
1914 int
1915buflist_getfile(n, lnum, options, forceit)
1916 int n;
1917 linenr_T lnum;
1918 int options;
1919 int forceit;
1920{
1921 buf_T *buf;
1922#ifdef FEAT_WINDOWS
1923 win_T *wp = NULL;
1924#endif
1925 pos_T *fpos;
1926 colnr_T col;
1927
1928 buf = buflist_findnr(n);
1929 if (buf == NULL)
1930 {
1931 if ((options & GETF_ALT) && n == 0)
1932 EMSG(_(e_noalt));
1933 else
1934 EMSGN(_("E92: Buffer %ld not found"), n);
1935 return FAIL;
1936 }
1937
1938 /* if alternate file is the current buffer, nothing to do */
1939 if (buf == curbuf)
1940 return OK;
1941
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001942 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001943 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001944 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001946 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001947#ifdef FEAT_AUTOCMD
1948 if (curbuf_locked())
1949 return FAIL;
1950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
1952 /* altfpos may be changed by getfile(), get it now */
1953 if (lnum == 0)
1954 {
1955 fpos = buflist_findfpos(buf);
1956 lnum = fpos->lnum;
1957 col = fpos->col;
1958 }
1959 else
1960 col = 0;
1961
1962#ifdef FEAT_WINDOWS
1963 if (options & GETF_SWITCH)
1964 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001965 /* If 'switchbuf' contains "useopen": jump to first window containing
1966 * "buf" if one exists */
1967 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001969 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1970 * page containing "buf" if one exists */
1971 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001972 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001973 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1974 * isn't empty: open new window */
1975 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001977 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1978 tabpage_new();
1979 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001981 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982 }
1983 }
1984#endif
1985
1986 ++RedrawingDisabled;
1987 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1988 lnum, forceit) <= 0)
1989 {
1990 --RedrawingDisabled;
1991
1992 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1993 if (!p_sol && col != 0)
1994 {
1995 curwin->w_cursor.col = col;
1996 check_cursor_col();
1997#ifdef FEAT_VIRTUALEDIT
1998 curwin->w_cursor.coladd = 0;
1999#endif
2000 curwin->w_set_curswant = TRUE;
2001 }
2002 return OK;
2003 }
2004 --RedrawingDisabled;
2005 return FAIL;
2006}
2007
2008/*
2009 * go to the last know line number for the current buffer
2010 */
2011 void
2012buflist_getfpos()
2013{
2014 pos_T *fpos;
2015
2016 fpos = buflist_findfpos(curbuf);
2017
2018 curwin->w_cursor.lnum = fpos->lnum;
2019 check_cursor_lnum();
2020
2021 if (p_sol)
2022 curwin->w_cursor.col = 0;
2023 else
2024 {
2025 curwin->w_cursor.col = fpos->col;
2026 check_cursor_col();
2027#ifdef FEAT_VIRTUALEDIT
2028 curwin->w_cursor.coladd = 0;
2029#endif
2030 curwin->w_set_curswant = TRUE;
2031 }
2032}
2033
Bram Moolenaar81695252004-12-29 20:58:21 +00002034#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2035/*
2036 * Find file in buffer list by name (it has to be for the current window).
2037 * Returns NULL if not found.
2038 */
2039 buf_T *
2040buflist_findname_exp(fname)
2041 char_u *fname;
2042{
2043 char_u *ffname;
2044 buf_T *buf = NULL;
2045
2046 /* First make the name into a full path name */
2047 ffname = FullName_save(fname,
2048#ifdef UNIX
2049 TRUE /* force expansion, get rid of symbolic links */
2050#else
2051 FALSE
2052#endif
2053 );
2054 if (ffname != NULL)
2055 {
2056 buf = buflist_findname(ffname);
2057 vim_free(ffname);
2058 }
2059 return buf;
2060}
2061#endif
2062
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063/*
2064 * Find file in buffer list by name (it has to be for the current window).
2065 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002066 * Skips dummy buffers.
2067 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 */
2069 buf_T *
2070buflist_findname(ffname)
2071 char_u *ffname;
2072{
2073#ifdef UNIX
2074 struct stat st;
2075
2076 if (mch_stat((char *)ffname, &st) < 0)
2077 st.st_dev = (dev_T)-1;
2078 return buflist_findname_stat(ffname, &st);
2079}
2080
2081/*
2082 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2083 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002084 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 */
2086 static buf_T *
2087buflist_findname_stat(ffname, stp)
2088 char_u *ffname;
2089 struct stat *stp;
2090{
2091#endif
2092 buf_T *buf;
2093
2094 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002095 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096#ifdef UNIX
2097 , stp
2098#endif
2099 ))
2100 return buf;
2101 return NULL;
2102}
2103
2104#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2105/*
2106 * Find file in buffer list by a regexp pattern.
2107 * Return fnum of the found buffer.
2108 * Return < 0 for error.
2109 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 int
2111buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2112 char_u *pattern;
2113 char_u *pattern_end; /* pointer to first char after pattern */
2114 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002115 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002116{
2117 buf_T *buf;
2118 regprog_T *prog;
2119 int match = -1;
2120 int find_listed;
2121 char_u *pat;
2122 char_u *patend;
2123 int attempt;
2124 char_u *p;
2125 int toggledollar;
2126
2127 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2128 {
2129 if (*pattern == '%')
2130 match = curbuf->b_fnum;
2131 else
2132 match = curwin->w_alt_fnum;
2133#ifdef FEAT_DIFF
2134 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2135 match = -1;
2136#endif
2137 }
2138
2139 /*
2140 * Try four ways of matching a listed buffer:
2141 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002142 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002143 * attempt == 2: with '$' at end (only match at end)
2144 * attempt == 3: with '^' at start and '$' at end (only full match)
2145 * Repeat this for finding an unlisted buffer if there was no matching
2146 * listed buffer.
2147 */
2148 else
2149 {
2150 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2151 if (pat == NULL)
2152 return -1;
2153 patend = pat + STRLEN(pat) - 1;
2154 toggledollar = (patend > pat && *patend == '$');
2155
2156 /* First try finding a listed buffer. If not found and "unlisted"
2157 * is TRUE, try finding an unlisted buffer. */
2158 find_listed = TRUE;
2159 for (;;)
2160 {
2161 for (attempt = 0; attempt <= 3; ++attempt)
2162 {
2163 /* may add '^' and '$' */
2164 if (toggledollar)
2165 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2166 p = pat;
2167 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2168 ++p;
2169 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2170 if (prog == NULL)
2171 {
2172 vim_free(pat);
2173 return -1;
2174 }
2175
2176 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2177 if (buf->b_p_bl == find_listed
2178#ifdef FEAT_DIFF
2179 && (!diffmode || diff_mode_buf(buf))
2180#endif
2181 && buflist_match(prog, buf) != NULL)
2182 {
2183 if (match >= 0) /* already found a match */
2184 {
2185 match = -2;
2186 break;
2187 }
2188 match = buf->b_fnum; /* remember first match */
2189 }
2190
2191 vim_free(prog);
2192 if (match >= 0) /* found one match */
2193 break;
2194 }
2195
2196 /* Only search for unlisted buffers if there was no match with
2197 * a listed buffer. */
2198 if (!unlisted || !find_listed || match != -1)
2199 break;
2200 find_listed = FALSE;
2201 }
2202
2203 vim_free(pat);
2204 }
2205
2206 if (match == -2)
2207 EMSG2(_("E93: More than one match for %s"), pattern);
2208 else if (match < 0)
2209 EMSG2(_("E94: No matching buffer for %s"), pattern);
2210 return match;
2211}
2212#endif
2213
2214#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2215
2216/*
2217 * Find all buffer names that match.
2218 * For command line expansion of ":buf" and ":sbuf".
2219 * Return OK if matches found, FAIL otherwise.
2220 */
2221 int
2222ExpandBufnames(pat, num_file, file, options)
2223 char_u *pat;
2224 int *num_file;
2225 char_u ***file;
2226 int options;
2227{
2228 int count = 0;
2229 buf_T *buf;
2230 int round;
2231 char_u *p;
2232 int attempt;
2233 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002234 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
2236 *num_file = 0; /* return values in case of FAIL */
2237 *file = NULL;
2238
Bram Moolenaar05159a02005-02-26 23:04:13 +00002239 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2240 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002242 patc = alloc((unsigned)STRLEN(pat) + 11);
2243 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002245 STRCPY(patc, "\\(^\\|[\\/]\\)");
2246 STRCPY(patc + 11, pat + 1);
2247 }
2248 else
2249 patc = pat;
2250
2251 /*
2252 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002253 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002254 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002255 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002256 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002257 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002258 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002259 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002260 if (prog == NULL)
2261 {
2262 if (patc != pat)
2263 vim_free(patc);
2264 return FAIL;
2265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002266
2267 /*
2268 * round == 1: Count the matches.
2269 * round == 2: Build the array to keep the matches.
2270 */
2271 for (round = 1; round <= 2; ++round)
2272 {
2273 count = 0;
2274 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2275 {
2276 if (!buf->b_p_bl) /* skip unlisted buffers */
2277 continue;
2278 p = buflist_match(prog, buf);
2279 if (p != NULL)
2280 {
2281 if (round == 1)
2282 ++count;
2283 else
2284 {
2285 if (options & WILD_HOME_REPLACE)
2286 p = home_replace_save(buf, p);
2287 else
2288 p = vim_strsave(p);
2289 (*file)[count++] = p;
2290 }
2291 }
2292 }
2293 if (count == 0) /* no match found, break here */
2294 break;
2295 if (round == 1)
2296 {
2297 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2298 if (*file == NULL)
2299 {
2300 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002301 if (patc != pat)
2302 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 return FAIL;
2304 }
2305 }
2306 }
2307 vim_free(prog);
2308 if (count) /* match(es) found, break here */
2309 break;
2310 }
2311
Bram Moolenaar05159a02005-02-26 23:04:13 +00002312 if (patc != pat)
2313 vim_free(patc);
2314
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 *num_file = count;
2316 return (count == 0 ? FAIL : OK);
2317}
2318
2319#endif /* FEAT_CMDL_COMPL */
2320
2321#ifdef HAVE_BUFLIST_MATCH
2322/*
2323 * Check for a match on the file name for buffer "buf" with regprog "prog".
2324 */
2325 static char_u *
2326buflist_match(prog, buf)
2327 regprog_T *prog;
2328 buf_T *buf;
2329{
2330 char_u *match;
2331
2332 /* First try the short file name, then the long file name. */
2333 match = fname_match(prog, buf->b_sfname);
2334 if (match == NULL)
2335 match = fname_match(prog, buf->b_ffname);
2336
2337 return match;
2338}
2339
2340/*
2341 * Try matching the regexp in "prog" with file name "name".
2342 * Return "name" when there is a match, NULL when not.
2343 */
2344 static char_u *
2345fname_match(prog, name)
2346 regprog_T *prog;
2347 char_u *name;
2348{
2349 char_u *match = NULL;
2350 char_u *p;
2351 regmatch_T regmatch;
2352
2353 if (name != NULL)
2354 {
2355 regmatch.regprog = prog;
2356#ifdef CASE_INSENSITIVE_FILENAME
2357 regmatch.rm_ic = TRUE; /* Always ignore case */
2358#else
2359 regmatch.rm_ic = FALSE; /* Never ignore case */
2360#endif
2361
2362 if (vim_regexec(&regmatch, name, (colnr_T)0))
2363 match = name;
2364 else
2365 {
2366 /* Replace $(HOME) with '~' and try matching again. */
2367 p = home_replace_save(NULL, name);
2368 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2369 match = name;
2370 vim_free(p);
2371 }
2372 }
2373
2374 return match;
2375}
2376#endif
2377
2378/*
2379 * find file in buffer list by number
2380 */
2381 buf_T *
2382buflist_findnr(nr)
2383 int nr;
2384{
2385 buf_T *buf;
2386
2387 if (nr == 0)
2388 nr = curwin->w_alt_fnum;
2389 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2390 if (buf->b_fnum == nr)
2391 return (buf);
2392 return NULL;
2393}
2394
2395/*
2396 * Get name of file 'n' in the buffer list.
2397 * When the file has no name an empty string is returned.
2398 * home_replace() is used to shorten the file name (used for marks).
2399 * Returns a pointer to allocated memory, of NULL when failed.
2400 */
2401 char_u *
2402buflist_nr2name(n, fullname, helptail)
2403 int n;
2404 int fullname;
2405 int helptail; /* for help buffers return tail only */
2406{
2407 buf_T *buf;
2408
2409 buf = buflist_findnr(n);
2410 if (buf == NULL)
2411 return NULL;
2412 return home_replace_save(helptail ? buf : NULL,
2413 fullname ? buf->b_ffname : buf->b_fname);
2414}
2415
2416/*
2417 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2418 * When "copy_options" is TRUE save the local window option values.
2419 * When "lnum" is 0 only do the options.
2420 */
2421 static void
2422buflist_setfpos(buf, win, lnum, col, copy_options)
2423 buf_T *buf;
2424 win_T *win;
2425 linenr_T lnum;
2426 colnr_T col;
2427 int copy_options;
2428{
2429 wininfo_T *wip;
2430
2431 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2432 if (wip->wi_win == win)
2433 break;
2434 if (wip == NULL)
2435 {
2436 /* allocate a new entry */
2437 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2438 if (wip == NULL)
2439 return;
2440 wip->wi_win = win;
2441 if (lnum == 0) /* set lnum even when it's 0 */
2442 lnum = 1;
2443 }
2444 else
2445 {
2446 /* remove the entry from the list */
2447 if (wip->wi_prev)
2448 wip->wi_prev->wi_next = wip->wi_next;
2449 else
2450 buf->b_wininfo = wip->wi_next;
2451 if (wip->wi_next)
2452 wip->wi_next->wi_prev = wip->wi_prev;
2453 if (copy_options && wip->wi_optset)
2454 {
2455 clear_winopt(&wip->wi_opt);
2456#ifdef FEAT_FOLDING
2457 deleteFoldRecurse(&wip->wi_folds);
2458#endif
2459 }
2460 }
2461 if (lnum != 0)
2462 {
2463 wip->wi_fpos.lnum = lnum;
2464 wip->wi_fpos.col = col;
2465 }
2466 if (copy_options)
2467 {
2468 /* Save the window-specific option values. */
2469 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2470#ifdef FEAT_FOLDING
2471 wip->wi_fold_manual = win->w_fold_manual;
2472 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2473#endif
2474 wip->wi_optset = TRUE;
2475 }
2476
2477 /* insert the entry in front of the list */
2478 wip->wi_next = buf->b_wininfo;
2479 buf->b_wininfo = wip;
2480 wip->wi_prev = NULL;
2481 if (wip->wi_next)
2482 wip->wi_next->wi_prev = wip;
2483
2484 return;
2485}
2486
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002487#ifdef FEAT_DIFF
2488static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2489
2490/*
2491 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2492 * page. That's because a diff is local to a tab page.
2493 */
2494 static int
2495wininfo_other_tab_diff(wip)
2496 wininfo_T *wip;
2497{
2498 win_T *wp;
2499
2500 if (wip->wi_opt.wo_diff)
2501 {
2502 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2503 /* return FALSE when it's a window in the current tab page, thus
2504 * the buffer was in diff mode here */
2505 if (wip->wi_win == wp)
2506 return FALSE;
2507 return TRUE;
2508 }
2509 return FALSE;
2510}
2511#endif
2512
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513/*
2514 * Find info for the current window in buffer "buf".
2515 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002516 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2517 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 * Returns NULL when there isn't any info.
2519 */
2520 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002521find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002523 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524{
2525 wininfo_T *wip;
2526
2527 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002528 if (wip->wi_win == curwin
2529#ifdef FEAT_DIFF
2530 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2531#endif
2532 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002534
2535 /* If no wininfo for curwin, use the first in the list (that doesn't have
2536 * 'diff' set and is in another tab page). */
2537 if (wip == NULL)
2538 {
2539#ifdef FEAT_DIFF
2540 if (skip_diff_buffer)
2541 {
2542 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2543 if (!wininfo_other_tab_diff(wip))
2544 break;
2545 }
2546 else
2547#endif
2548 wip = buf->b_wininfo;
2549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 return wip;
2551}
2552
2553/*
2554 * Reset the local window options to the values last used in this window.
2555 * If the buffer wasn't used in this window before, use the values from
2556 * the most recently used window. If the values were never set, use the
2557 * global values for the window.
2558 */
2559 void
2560get_winopts(buf)
2561 buf_T *buf;
2562{
2563 wininfo_T *wip;
2564
2565 clear_winopt(&curwin->w_onebuf_opt);
2566#ifdef FEAT_FOLDING
2567 clearFolding(curwin);
2568#endif
2569
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002570 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 if (wip != NULL && wip->wi_optset)
2572 {
2573 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2574#ifdef FEAT_FOLDING
2575 curwin->w_fold_manual = wip->wi_fold_manual;
2576 curwin->w_foldinvalid = TRUE;
2577 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2578#endif
2579 }
2580 else
2581 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2582
2583#ifdef FEAT_FOLDING
2584 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2585 if (p_fdls >= 0)
2586 curwin->w_p_fdl = p_fdls;
2587#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002588#ifdef FEAT_SYN_HL
2589 check_colorcolumn(curwin);
2590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591}
2592
2593/*
2594 * Find the position (lnum and col) for the buffer 'buf' for the current
2595 * window.
2596 * Returns a pointer to no_position if no position is found.
2597 */
2598 pos_T *
2599buflist_findfpos(buf)
2600 buf_T *buf;
2601{
2602 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002603 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002605 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 if (wip != NULL)
2607 return &(wip->wi_fpos);
2608 else
2609 return &no_position;
2610}
2611
2612/*
2613 * Find the lnum for the buffer 'buf' for the current window.
2614 */
2615 linenr_T
2616buflist_findlnum(buf)
2617 buf_T *buf;
2618{
2619 return buflist_findfpos(buf)->lnum;
2620}
2621
2622#if defined(FEAT_LISTCMDS) || defined(PROTO)
2623/*
2624 * List all know file names (for :files and :buffers command).
2625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 void
2627buflist_list(eap)
2628 exarg_T *eap;
2629{
2630 buf_T *buf;
2631 int len;
2632 int i;
2633
2634 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2635 {
2636 /* skip unlisted buffers, unless ! was used */
2637 if (!buf->b_p_bl && !eap->forceit)
2638 continue;
2639 msg_putchar('\n');
2640 if (buf_spname(buf) != NULL)
2641 STRCPY(NameBuff, buf_spname(buf));
2642 else
2643 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2644
Bram Moolenaar50cde822005-06-05 21:54:54 +00002645 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002646 buf->b_fnum,
2647 buf->b_p_bl ? ' ' : 'u',
2648 buf == curbuf ? '%' :
2649 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2650 buf->b_ml.ml_mfp == NULL ? ' ' :
2651 (buf->b_nwindows == 0 ? 'h' : 'a'),
2652 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2653 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002654 : (bufIsChanged(buf) ? '+' : ' '),
2655 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656
2657 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 i = 40 - vim_strsize(IObuff);
2659 do
2660 {
2661 IObuff[len++] = ' ';
2662 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002663 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2664 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002665 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 msg_outtrans(IObuff);
2667 out_flush(); /* output one line at a time */
2668 ui_breakcheck();
2669 }
2670}
2671#endif
2672
2673/*
2674 * Get file name and line number for file 'fnum'.
2675 * Used by DoOneCmd() for translating '%' and '#'.
2676 * Used by insert_reg() and cmdline_paste() for '#' register.
2677 * Return FAIL if not found, OK for success.
2678 */
2679 int
2680buflist_name_nr(fnum, fname, lnum)
2681 int fnum;
2682 char_u **fname;
2683 linenr_T *lnum;
2684{
2685 buf_T *buf;
2686
2687 buf = buflist_findnr(fnum);
2688 if (buf == NULL || buf->b_fname == NULL)
2689 return FAIL;
2690
2691 *fname = buf->b_fname;
2692 *lnum = buflist_findlnum(buf);
2693
2694 return OK;
2695}
2696
2697/*
2698 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2699 * The file name with the full path is also remembered, for when :cd is used.
2700 * Returns FAIL for failure (file name already in use by other buffer)
2701 * OK otherwise.
2702 */
2703 int
2704setfname(buf, ffname, sfname, message)
2705 buf_T *buf;
2706 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002707 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708{
Bram Moolenaar81695252004-12-29 20:58:21 +00002709 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710#ifdef UNIX
2711 struct stat st;
2712#endif
2713
2714 if (ffname == NULL || *ffname == NUL)
2715 {
2716 /* Removing the name. */
2717 vim_free(buf->b_ffname);
2718 vim_free(buf->b_sfname);
2719 buf->b_ffname = NULL;
2720 buf->b_sfname = NULL;
2721#ifdef UNIX
2722 st.st_dev = (dev_T)-1;
2723#endif
2724 }
2725 else
2726 {
2727 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2728 if (ffname == NULL) /* out of memory */
2729 return FAIL;
2730
2731 /*
2732 * if the file name is already used in another buffer:
2733 * - if the buffer is loaded, fail
2734 * - if the buffer is not loaded, delete it from the list
2735 */
2736#ifdef UNIX
2737 if (mch_stat((char *)ffname, &st) < 0)
2738 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002739#endif
2740 if (!(buf->b_flags & BF_DUMMY))
2741#ifdef UNIX
2742 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002744 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745#endif
2746 if (obuf != NULL && obuf != buf)
2747 {
2748 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2749 {
2750 if (message)
2751 EMSG(_("E95: Buffer with this name already exists"));
2752 vim_free(ffname);
2753 return FAIL;
2754 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002755 /* delete from the list */
2756 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002757 }
2758 sfname = vim_strsave(sfname);
2759 if (ffname == NULL || sfname == NULL)
2760 {
2761 vim_free(sfname);
2762 vim_free(ffname);
2763 return FAIL;
2764 }
2765#ifdef USE_FNAME_CASE
2766# ifdef USE_LONG_FNAME
2767 if (USE_LONG_FNAME)
2768# endif
2769 fname_case(sfname, 0); /* set correct case for short file name */
2770#endif
2771 vim_free(buf->b_ffname);
2772 vim_free(buf->b_sfname);
2773 buf->b_ffname = ffname;
2774 buf->b_sfname = sfname;
2775 }
2776 buf->b_fname = buf->b_sfname;
2777#ifdef UNIX
2778 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002779 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002780 else
2781 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002782 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 buf->b_dev = st.st_dev;
2784 buf->b_ino = st.st_ino;
2785 }
2786#endif
2787
2788#ifndef SHORT_FNAME
2789 buf->b_shortname = FALSE;
2790#endif
2791
2792 buf_name_changed(buf);
2793 return OK;
2794}
2795
2796/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002797 * Crude way of changing the name of a buffer. Use with care!
2798 * The name should be relative to the current directory.
2799 */
2800 void
2801buf_set_name(fnum, name)
2802 int fnum;
2803 char_u *name;
2804{
2805 buf_T *buf;
2806
2807 buf = buflist_findnr(fnum);
2808 if (buf != NULL)
2809 {
2810 vim_free(buf->b_sfname);
2811 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002812 buf->b_ffname = vim_strsave(name);
2813 buf->b_sfname = NULL;
2814 /* Allocate ffname and expand into full path. Also resolves .lnk
2815 * files on Win32. */
2816 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002817 buf->b_fname = buf->b_sfname;
2818 }
2819}
2820
2821/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002822 * Take care of what needs to be done when the name of buffer "buf" has
2823 * changed.
2824 */
2825 void
2826buf_name_changed(buf)
2827 buf_T *buf;
2828{
2829 /*
2830 * If the file name changed, also change the name of the swapfile
2831 */
2832 if (buf->b_ml.ml_mfp != NULL)
2833 ml_setname(buf);
2834
2835 if (curwin->w_buffer == buf)
2836 check_arg_idx(curwin); /* check file name for arg list */
2837#ifdef FEAT_TITLE
2838 maketitle(); /* set window title */
2839#endif
2840#ifdef FEAT_WINDOWS
2841 status_redraw_all(); /* status lines need to be redrawn */
2842#endif
2843 fmarks_check_names(buf); /* check named file marks */
2844 ml_timestamp(buf); /* reset timestamp */
2845}
2846
2847/*
2848 * set alternate file name for current window
2849 *
2850 * Used by do_one_cmd(), do_write() and do_ecmd().
2851 * Return the buffer.
2852 */
2853 buf_T *
2854setaltfname(ffname, sfname, lnum)
2855 char_u *ffname;
2856 char_u *sfname;
2857 linenr_T lnum;
2858{
2859 buf_T *buf;
2860
2861 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2862 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002863 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 curwin->w_alt_fnum = buf->b_fnum;
2865 return buf;
2866}
2867
2868/*
2869 * Get alternate file name for current window.
2870 * Return NULL if there isn't any, and give error message if requested.
2871 */
2872 char_u *
2873getaltfname(errmsg)
2874 int errmsg; /* give error message */
2875{
2876 char_u *fname;
2877 linenr_T dummy;
2878
2879 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2880 {
2881 if (errmsg)
2882 EMSG(_(e_noalt));
2883 return NULL;
2884 }
2885 return fname;
2886}
2887
2888/*
2889 * Add a file name to the buflist and return its number.
2890 * Uses same flags as buflist_new(), except BLN_DUMMY.
2891 *
2892 * used by qf_init(), main() and doarglist()
2893 */
2894 int
2895buflist_add(fname, flags)
2896 char_u *fname;
2897 int flags;
2898{
2899 buf_T *buf;
2900
2901 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2902 if (buf != NULL)
2903 return buf->b_fnum;
2904 return 0;
2905}
2906
2907#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2908/*
2909 * Adjust slashes in file names. Called after 'shellslash' was set.
2910 */
2911 void
2912buflist_slash_adjust()
2913{
2914 buf_T *bp;
2915
2916 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2917 {
2918 if (bp->b_ffname != NULL)
2919 slash_adjust(bp->b_ffname);
2920 if (bp->b_sfname != NULL)
2921 slash_adjust(bp->b_sfname);
2922 }
2923}
2924#endif
2925
2926/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002927 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 * Also save the local window option values.
2929 */
2930 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002931buflist_altfpos(win)
2932 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002934 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935}
2936
2937/*
2938 * Return TRUE if 'ffname' is not the same file as current file.
2939 * Fname must have a full path (expanded by mch_FullName()).
2940 */
2941 int
2942otherfile(ffname)
2943 char_u *ffname;
2944{
2945 return otherfile_buf(curbuf, ffname
2946#ifdef UNIX
2947 , NULL
2948#endif
2949 );
2950}
2951
2952 static int
2953otherfile_buf(buf, ffname
2954#ifdef UNIX
2955 , stp
2956#endif
2957 )
2958 buf_T *buf;
2959 char_u *ffname;
2960#ifdef UNIX
2961 struct stat *stp;
2962#endif
2963{
2964 /* no name is different */
2965 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2966 return TRUE;
2967 if (fnamecmp(ffname, buf->b_ffname) == 0)
2968 return FALSE;
2969#ifdef UNIX
2970 {
2971 struct stat st;
2972
2973 /* If no struct stat given, get it now */
2974 if (stp == NULL)
2975 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002976 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977 st.st_dev = (dev_T)-1;
2978 stp = &st;
2979 }
2980 /* Use dev/ino to check if the files are the same, even when the names
2981 * are different (possible with links). Still need to compare the
2982 * name above, for when the file doesn't exist yet.
2983 * Problem: The dev/ino changes when a file is deleted (and created
2984 * again) and remains the same when renamed/moved. We don't want to
2985 * mch_stat() each buffer each time, that would be too slow. Get the
2986 * dev/ino again when they appear to match, but not when they appear
2987 * to be different: Could skip a buffer when it's actually the same
2988 * file. */
2989 if (buf_same_ino(buf, stp))
2990 {
2991 buf_setino(buf);
2992 if (buf_same_ino(buf, stp))
2993 return FALSE;
2994 }
2995 }
2996#endif
2997 return TRUE;
2998}
2999
3000#if defined(UNIX) || defined(PROTO)
3001/*
3002 * Set inode and device number for a buffer.
3003 * Must always be called when b_fname is changed!.
3004 */
3005 void
3006buf_setino(buf)
3007 buf_T *buf;
3008{
3009 struct stat st;
3010
3011 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3012 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003013 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003014 buf->b_dev = st.st_dev;
3015 buf->b_ino = st.st_ino;
3016 }
3017 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003018 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019}
3020
3021/*
3022 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3023 */
3024 static int
3025buf_same_ino(buf, stp)
3026 buf_T *buf;
3027 struct stat *stp;
3028{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003029 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 && stp->st_dev == buf->b_dev
3031 && stp->st_ino == buf->b_ino);
3032}
3033#endif
3034
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003035/*
3036 * Print info about the current buffer.
3037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 void
3039fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003040 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 int shorthelp;
3042 int dont_truncate;
3043{
3044 char_u *name;
3045 int n;
3046 char_u *p;
3047 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003048 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
3050 buffer = alloc(IOSIZE);
3051 if (buffer == NULL)
3052 return;
3053
3054 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3055 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003056 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 p = buffer + STRLEN(buffer);
3058 }
3059 else
3060 p = buffer;
3061
3062 *p++ = '"';
3063 if (buf_spname(curbuf) != NULL)
3064 STRCPY(p, buf_spname(curbuf));
3065 else
3066 {
3067 if (!fullname && curbuf->b_fname != NULL)
3068 name = curbuf->b_fname;
3069 else
3070 name = curbuf->b_ffname;
3071 home_replace(shorthelp ? curbuf : NULL, name, p,
3072 (int)(IOSIZE - (p - buffer)), TRUE);
3073 }
3074
Bram Moolenaara800b422010-06-27 01:15:55 +02003075 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 curbufIsChanged() ? (shortmess(SHM_MOD)
3077 ? " [+]" : _(" [Modified]")) : " ",
3078 (curbuf->b_flags & BF_NOTEDITED)
3079#ifdef FEAT_QUICKFIX
3080 && !bt_dontwrite(curbuf)
3081#endif
3082 ? _("[Not edited]") : "",
3083 (curbuf->b_flags & BF_NEW)
3084#ifdef FEAT_QUICKFIX
3085 && !bt_dontwrite(curbuf)
3086#endif
3087 ? _("[New file]") : "",
3088 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3089 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3090 : _("[readonly]")) : "",
3091 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3092 || curbuf->b_p_ro) ?
3093 " " : "");
3094 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3095 * causes an overflow, thus for large numbers divide instead. */
3096 if (curwin->w_cursor.lnum > 1000000L)
3097 n = (int)(((long)curwin->w_cursor.lnum) /
3098 ((long)curbuf->b_ml.ml_line_count / 100L));
3099 else
3100 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3101 (long)curbuf->b_ml.ml_line_count);
3102 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3103 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003104 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 }
3106#ifdef FEAT_CMDL_INFO
3107 else if (p_ru)
3108 {
3109 /* Current line and column are already on the screen -- webb */
3110 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003111 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003113 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 (long)curbuf->b_ml.ml_line_count, n);
3115 }
3116#endif
3117 else
3118 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003119 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 _("line %ld of %ld --%d%%-- col "),
3121 (long)curwin->w_cursor.lnum,
3122 (long)curbuf->b_ml.ml_line_count,
3123 n);
3124 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003125 len = STRLEN(buffer);
3126 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3128 }
3129
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003130 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131
3132 if (dont_truncate)
3133 {
3134 /* Temporarily set msg_scroll to avoid the message being truncated.
3135 * First call msg_start() to get the message in the right place. */
3136 msg_start();
3137 n = msg_scroll;
3138 msg_scroll = TRUE;
3139 msg(buffer);
3140 msg_scroll = n;
3141 }
3142 else
3143 {
3144 p = msg_trunc_attr(buffer, FALSE, 0);
3145 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 /* Need to repeat the message after redrawing when:
3147 * - When restart_edit is set (otherwise there will be a delay
3148 * before redrawing).
3149 * - When the screen was scrolled but there is no wait-return
3150 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003151 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 }
3153
3154 vim_free(buffer);
3155}
3156
3157 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003158col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003159 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003160 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 int col;
3162 int vcol;
3163{
3164 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003165 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003167 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168}
3169
3170#if defined(FEAT_TITLE) || defined(PROTO)
3171/*
3172 * put file name in title bar of window and in icon title
3173 */
3174
3175static char_u *lasttitle = NULL;
3176static char_u *lasticon = NULL;
3177
3178 void
3179maketitle()
3180{
3181 char_u *p;
3182 char_u *t_str = NULL;
3183 char_u *i_name;
3184 char_u *i_str = NULL;
3185 int maxlen = 0;
3186 int len;
3187 int mustset;
3188 char_u buf[IOSIZE];
3189 int off;
3190
3191 if (!redrawing())
3192 {
3193 /* Postpone updating the title when 'lazyredraw' is set. */
3194 need_maketitle = TRUE;
3195 return;
3196 }
3197
3198 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003199 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return;
3201
3202 if (p_title)
3203 {
3204 if (p_titlelen > 0)
3205 {
3206 maxlen = p_titlelen * Columns / 100;
3207 if (maxlen < 10)
3208 maxlen = 10;
3209 }
3210
3211 t_str = buf;
3212 if (*p_titlestring != NUL)
3213 {
3214#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003215 if (stl_syntax & STL_IN_TITLE)
3216 {
3217 int use_sandbox = FALSE;
3218 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003219
3220# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003221 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003222# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003223 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003225 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003226 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003227 if (called_emsg)
3228 set_string_option_direct((char_u *)"titlestring", -1,
3229 (char_u *)"", OPT_FREE, SID_ERROR);
3230 called_emsg |= save_called_emsg;
3231 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 else
3233#endif
3234 t_str = p_titlestring;
3235 }
3236 else
3237 {
3238 /* format: "fname + (path) (1 of 2) - VIM" */
3239
3240 if (curbuf->b_fname == NULL)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003241 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 else
3243 {
3244 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003245 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247 }
3248
3249 switch (bufIsChanged(curbuf)
3250 + (curbuf->b_p_ro * 2)
3251 + (!curbuf->b_p_ma * 4))
3252 {
3253 case 1: STRCAT(buf, " +"); break;
3254 case 2: STRCAT(buf, " ="); break;
3255 case 3: STRCAT(buf, " =+"); break;
3256 case 4:
3257 case 6: STRCAT(buf, " -"); break;
3258 case 5:
3259 case 7: STRCAT(buf, " -+"); break;
3260 }
3261
3262 if (curbuf->b_fname != NULL)
3263 {
3264 /* Get path of file, replace home dir with ~ */
3265 off = (int)STRLEN(buf);
3266 buf[off++] = ' ';
3267 buf[off++] = '(';
3268 home_replace(curbuf, curbuf->b_ffname,
3269 buf + off, IOSIZE - off, TRUE);
3270#ifdef BACKSLASH_IN_FILENAME
3271 /* avoid "c:/name" to be reduced to "c" */
3272 if (isalpha(buf[off]) && buf[off + 1] == ':')
3273 off += 2;
3274#endif
3275 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003276 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003279 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003280 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003283
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 /* translate unprintable chars */
3285 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003286 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 STRCAT(buf, ")");
3289 }
3290
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003291 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292
3293#if defined(FEAT_CLIENTSERVER)
3294 if (serverName != NULL)
3295 {
3296 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003297 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 }
3299 else
3300#endif
3301 STRCAT(buf, " - VIM");
3302
3303 if (maxlen > 0)
3304 {
3305 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003306 if (vim_strsize(buf) > maxlen)
3307 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 }
3309 }
3310 }
3311 mustset = ti_change(t_str, &lasttitle);
3312
3313 if (p_icon)
3314 {
3315 i_str = buf;
3316 if (*p_iconstring != NUL)
3317 {
3318#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003319 if (stl_syntax & STL_IN_ICON)
3320 {
3321 int use_sandbox = FALSE;
3322 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003323
3324# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003325 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003326# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003327 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003329 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003330 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003331 if (called_emsg)
3332 set_string_option_direct((char_u *)"iconstring", -1,
3333 (char_u *)"", OPT_FREE, SID_ERROR);
3334 called_emsg |= save_called_emsg;
3335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 else
3337#endif
3338 i_str = p_iconstring;
3339 }
3340 else
3341 {
3342 if (buf_spname(curbuf) != NULL)
3343 i_name = (char_u *)buf_spname(curbuf);
3344 else /* use file name only in icon */
3345 i_name = gettail(curbuf->b_ffname);
3346 *i_str = NUL;
3347 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003348 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 if (len > 100)
3350 {
3351 len -= 100;
3352#ifdef FEAT_MBYTE
3353 if (has_mbyte)
3354 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3355#endif
3356 i_name += len;
3357 }
3358 STRCPY(i_str, i_name);
3359 trans_characters(i_str, IOSIZE);
3360 }
3361 }
3362
3363 mustset |= ti_change(i_str, &lasticon);
3364
3365 if (mustset)
3366 resettitle();
3367}
3368
3369/*
3370 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3371 * from "str" if it does.
3372 * Return TRUE when "*last" changed.
3373 */
3374 static int
3375ti_change(str, last)
3376 char_u *str;
3377 char_u **last;
3378{
3379 if ((str == NULL) != (*last == NULL)
3380 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3381 {
3382 vim_free(*last);
3383 if (str == NULL)
3384 *last = NULL;
3385 else
3386 *last = vim_strsave(str);
3387 return TRUE;
3388 }
3389 return FALSE;
3390}
3391
3392/*
3393 * Put current window title back (used after calling a shell)
3394 */
3395 void
3396resettitle()
3397{
3398 mch_settitle(lasttitle, lasticon);
3399}
Bram Moolenaarea408852005-06-25 22:49:46 +00003400
3401# if defined(EXITFREE) || defined(PROTO)
3402 void
3403free_titles()
3404{
3405 vim_free(lasttitle);
3406 vim_free(lasticon);
3407}
3408# endif
3409
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410#endif /* FEAT_TITLE */
3411
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003412#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003414 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 * Return length of string in screen cells.
3416 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003417 * Normally works for window "wp", except when working for 'tabline' then it
3418 * is "curwin".
3419 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 * Items are drawn interspersed with the text that surrounds it
3421 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3422 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3423 *
3424 * If maxwidth is not zero, the string will be filled at any middle marker
3425 * or truncated if too long, fillchar is used for all whitespace.
3426 */
3427 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003428build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3429 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003431 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 size_t outlen; /* length of out[] */
3433 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003434 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003435 int fillchar;
3436 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003437 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3438 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439{
3440 char_u *p;
3441 char_u *s;
3442 char_u *t;
3443 char_u *linecont;
3444#ifdef FEAT_EVAL
3445 win_T *o_curwin;
3446 buf_T *o_curbuf;
3447#endif
3448 int empty_line;
3449 colnr_T virtcol;
3450 long l;
3451 long n;
3452 int prevchar_isflag;
3453 int prevchar_isitem;
3454 int itemisflag;
3455 int fillable;
3456 char_u *str;
3457 long num;
3458 int width;
3459 int itemcnt;
3460 int curitem;
3461 int groupitem[STL_MAX_ITEM];
3462 int groupdepth;
3463 struct stl_item
3464 {
3465 char_u *start;
3466 int minwid;
3467 int maxwid;
3468 enum
3469 {
3470 Normal,
3471 Empty,
3472 Group,
3473 Middle,
3474 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003475 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 Trunc
3477 } type;
3478 } item[STL_MAX_ITEM];
3479 int minwid;
3480 int maxwid;
3481 int zeropad;
3482 char_u base;
3483 char_u opt;
3484#define TMPLEN 70
3485 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003486 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003487 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003488
3489#ifdef FEAT_EVAL
3490 /*
3491 * When the format starts with "%!" then evaluate it as an expression and
3492 * use the result as the actual format string.
3493 */
3494 if (fmt[0] == '%' && fmt[1] == '!')
3495 {
3496 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3497 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003498 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003499 }
3500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 if (fillchar == 0)
3503 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003504#ifdef FEAT_MBYTE
3505 /* Can't handle a multi-byte fill character yet. */
3506 else if (mb_char2len(fillchar) > 1)
3507 fillchar = '-';
3508#endif
3509
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510 /*
3511 * Get line & check if empty (cursorpos will show "0-1").
3512 * If inversion is possible we use it. Else '=' characters are used.
3513 */
3514 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3515 empty_line = (*linecont == NUL);
3516
3517 groupdepth = 0;
3518 p = out;
3519 curitem = 0;
3520 prevchar_isflag = TRUE;
3521 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003522 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003524 if (curitem == STL_MAX_ITEM)
3525 {
3526 /* There are too many items. Add the error code to the statusline
3527 * to give the user a hint about what went wrong. */
3528 if (p + 6 < out + outlen)
3529 {
3530 mch_memmove(p, " E541", (size_t)5);
3531 p += 5;
3532 }
3533 break;
3534 }
3535
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 if (*s != NUL && *s != '%')
3537 prevchar_isflag = prevchar_isitem = FALSE;
3538
3539 /*
3540 * Handle up to the next '%' or the end.
3541 */
3542 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3543 *p++ = *s++;
3544 if (*s == NUL || p + 1 >= out + outlen)
3545 break;
3546
3547 /*
3548 * Handle one '%' item.
3549 */
3550 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003551 if (*s == NUL) /* ignore trailing % */
3552 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553 if (*s == '%')
3554 {
3555 if (p + 1 >= out + outlen)
3556 break;
3557 *p++ = *s++;
3558 prevchar_isflag = prevchar_isitem = FALSE;
3559 continue;
3560 }
3561 if (*s == STL_MIDDLEMARK)
3562 {
3563 s++;
3564 if (groupdepth > 0)
3565 continue;
3566 item[curitem].type = Middle;
3567 item[curitem++].start = p;
3568 continue;
3569 }
3570 if (*s == STL_TRUNCMARK)
3571 {
3572 s++;
3573 item[curitem].type = Trunc;
3574 item[curitem++].start = p;
3575 continue;
3576 }
3577 if (*s == ')')
3578 {
3579 s++;
3580 if (groupdepth < 1)
3581 continue;
3582 groupdepth--;
3583
3584 t = item[groupitem[groupdepth]].start;
3585 *p = NUL;
3586 l = vim_strsize(t);
3587 if (curitem > groupitem[groupdepth] + 1
3588 && item[groupitem[groupdepth]].minwid == 0)
3589 {
3590 /* remove group if all items are empty */
3591 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3592 if (item[n].type == Normal)
3593 break;
3594 if (n == curitem)
3595 {
3596 p = t;
3597 l = 0;
3598 }
3599 }
3600 if (l > item[groupitem[groupdepth]].maxwid)
3601 {
3602 /* truncate, remove n bytes of text at the start */
3603#ifdef FEAT_MBYTE
3604 if (has_mbyte)
3605 {
3606 /* Find the first character that should be included. */
3607 n = 0;
3608 while (l >= item[groupitem[groupdepth]].maxwid)
3609 {
3610 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003611 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 }
3613 }
3614 else
3615#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003616 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617
3618 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003619 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 p = p - n + 1;
3621#ifdef FEAT_MBYTE
3622 /* Fill up space left over by half a double-wide char. */
3623 while (++l < item[groupitem[groupdepth]].minwid)
3624 *p++ = fillchar;
3625#endif
3626
3627 /* correct the start of the items for the truncation */
3628 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3629 {
3630 item[l].start -= n;
3631 if (item[l].start < t)
3632 item[l].start = t;
3633 }
3634 }
3635 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3636 {
3637 /* fill */
3638 n = item[groupitem[groupdepth]].minwid;
3639 if (n < 0)
3640 {
3641 /* fill by appending characters */
3642 n = 0 - n;
3643 while (l++ < n && p + 1 < out + outlen)
3644 *p++ = fillchar;
3645 }
3646 else
3647 {
3648 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003649 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 l = n - l;
3651 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003652 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 p += l;
3654 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3655 item[n].start += l;
3656 for ( ; l > 0; l--)
3657 *t++ = fillchar;
3658 }
3659 }
3660 continue;
3661 }
3662 minwid = 0;
3663 maxwid = 9999;
3664 zeropad = FALSE;
3665 l = 1;
3666 if (*s == '0')
3667 {
3668 s++;
3669 zeropad = TRUE;
3670 }
3671 if (*s == '-')
3672 {
3673 s++;
3674 l = -1;
3675 }
3676 if (VIM_ISDIGIT(*s))
3677 {
3678 minwid = (int)getdigits(&s);
3679 if (minwid < 0) /* overflow */
3680 minwid = 0;
3681 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003682 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 {
3684 item[curitem].type = Highlight;
3685 item[curitem].start = p;
3686 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3687 s++;
3688 curitem++;
3689 continue;
3690 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003691 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3692 {
3693 if (*s == STL_TABCLOSENR)
3694 {
3695 if (minwid == 0)
3696 {
3697 /* %X ends the close label, go back to the previously
3698 * define tab label nr. */
3699 for (n = curitem - 1; n >= 0; --n)
3700 if (item[n].type == TabPage && item[n].minwid >= 0)
3701 {
3702 minwid = item[n].minwid;
3703 break;
3704 }
3705 }
3706 else
3707 /* close nrs are stored as negative values */
3708 minwid = - minwid;
3709 }
3710 item[curitem].type = TabPage;
3711 item[curitem].start = p;
3712 item[curitem].minwid = minwid;
3713 s++;
3714 curitem++;
3715 continue;
3716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 if (*s == '.')
3718 {
3719 s++;
3720 if (VIM_ISDIGIT(*s))
3721 {
3722 maxwid = (int)getdigits(&s);
3723 if (maxwid <= 0) /* overflow */
3724 maxwid = 50;
3725 }
3726 }
3727 minwid = (minwid > 50 ? 50 : minwid) * l;
3728 if (*s == '(')
3729 {
3730 groupitem[groupdepth++] = curitem;
3731 item[curitem].type = Group;
3732 item[curitem].start = p;
3733 item[curitem].minwid = minwid;
3734 item[curitem].maxwid = maxwid;
3735 s++;
3736 curitem++;
3737 continue;
3738 }
3739 if (vim_strchr(STL_ALL, *s) == NULL)
3740 {
3741 s++;
3742 continue;
3743 }
3744 opt = *s++;
3745
3746 /* OK - now for the real work */
3747 base = 'D';
3748 itemisflag = FALSE;
3749 fillable = TRUE;
3750 num = -1;
3751 str = NULL;
3752 switch (opt)
3753 {
3754 case STL_FILEPATH:
3755 case STL_FULLPATH:
3756 case STL_FILENAME:
3757 fillable = FALSE; /* don't change ' ' to fillchar */
3758 if (buf_spname(wp->w_buffer) != NULL)
3759 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3760 else
3761 {
3762 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003763 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3765 }
3766 trans_characters(NameBuff, MAXPATHL);
3767 if (opt != STL_FILENAME)
3768 str = NameBuff;
3769 else
3770 str = gettail(NameBuff);
3771 break;
3772
3773 case STL_VIM_EXPR: /* '{' */
3774 itemisflag = TRUE;
3775 t = p;
3776 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3777 *p++ = *s++;
3778 if (*s != '}') /* missing '}' or out of space */
3779 break;
3780 s++;
3781 *p = 0;
3782 p = t;
3783
3784#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003785 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3787
3788 o_curbuf = curbuf;
3789 o_curwin = curwin;
3790 curwin = wp;
3791 curbuf = wp->w_buffer;
3792
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003793 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794
3795 curwin = o_curwin;
3796 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003797 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798
3799 if (str != NULL && *str != 0)
3800 {
3801 if (*skipdigits(str) == NUL)
3802 {
3803 num = atoi((char *)str);
3804 vim_free(str);
3805 str = NULL;
3806 itemisflag = FALSE;
3807 }
3808 }
3809#endif
3810 break;
3811
3812 case STL_LINE:
3813 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3814 ? 0L : (long)(wp->w_cursor.lnum);
3815 break;
3816
3817 case STL_NUMLINES:
3818 num = wp->w_buffer->b_ml.ml_line_count;
3819 break;
3820
3821 case STL_COLUMN:
3822 num = !(State & INSERT) && empty_line
3823 ? 0 : (int)wp->w_cursor.col + 1;
3824 break;
3825
3826 case STL_VIRTCOL:
3827 case STL_VIRTCOL_ALT:
3828 /* In list mode virtcol needs to be recomputed */
3829 virtcol = wp->w_virtcol;
3830 if (wp->w_p_list && lcs_tab1 == NUL)
3831 {
3832 wp->w_p_list = FALSE;
3833 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3834 wp->w_p_list = TRUE;
3835 }
3836 ++virtcol;
3837 /* Don't display %V if it's the same as %c. */
3838 if (opt == STL_VIRTCOL_ALT
3839 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3840 ? 0 : (int)wp->w_cursor.col + 1)))
3841 break;
3842 num = (long)virtcol;
3843 break;
3844
3845 case STL_PERCENTAGE:
3846 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3847 (long)wp->w_buffer->b_ml.ml_line_count);
3848 break;
3849
3850 case STL_ALTPERCENT:
3851 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003852 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 break;
3854
3855 case STL_ARGLISTSTAT:
3856 fillable = FALSE;
3857 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003858 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 str = tmp;
3860 break;
3861
3862 case STL_KEYMAP:
3863 fillable = FALSE;
3864 if (get_keymap_str(wp, tmp, TMPLEN))
3865 str = tmp;
3866 break;
3867 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003868#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003869 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870#else
3871 num = 0;
3872#endif
3873 break;
3874
3875 case STL_BUFNO:
3876 num = wp->w_buffer->b_fnum;
3877 break;
3878
3879 case STL_OFFSET_X:
3880 base = 'X';
3881 case STL_OFFSET:
3882#ifdef FEAT_BYTEOFF
3883 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3884 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3885 0L : l + 1 + (!(State & INSERT) && empty_line ?
3886 0 : (int)wp->w_cursor.col);
3887#endif
3888 break;
3889
3890 case STL_BYTEVAL_X:
3891 base = 'X';
3892 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003893 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 num = 0;
3895 else
3896 {
3897#ifdef FEAT_MBYTE
3898 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3899#else
3900 num = linecont[wp->w_cursor.col];
3901#endif
3902 }
3903 if (num == NL)
3904 num = 0;
3905 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3906 num = NL;
3907 break;
3908
3909 case STL_ROFLAG:
3910 case STL_ROFLAG_ALT:
3911 itemisflag = TRUE;
3912 if (wp->w_buffer->b_p_ro)
3913 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3914 break;
3915
3916 case STL_HELPFLAG:
3917 case STL_HELPFLAG_ALT:
3918 itemisflag = TRUE;
3919 if (wp->w_buffer->b_help)
3920 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003921 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 break;
3923
3924#ifdef FEAT_AUTOCMD
3925 case STL_FILETYPE:
3926 if (*wp->w_buffer->b_p_ft != NUL
3927 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3928 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003929 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3930 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 str = tmp;
3932 }
3933 break;
3934
3935 case STL_FILETYPE_ALT:
3936 itemisflag = TRUE;
3937 if (*wp->w_buffer->b_p_ft != NUL
3938 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3939 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003940 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3941 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 for (t = tmp; *t != 0; t++)
3943 *t = TOUPPER_LOC(*t);
3944 str = tmp;
3945 }
3946 break;
3947#endif
3948
3949#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3950 case STL_PREVIEWFLAG:
3951 case STL_PREVIEWFLAG_ALT:
3952 itemisflag = TRUE;
3953 if (wp->w_p_pvw)
3954 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3955 : _("[Preview]"));
3956 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003957
3958 case STL_QUICKFIX:
3959 if (bt_quickfix(wp->w_buffer))
3960 str = (char_u *)(wp->w_llist_ref
3961 ? _(msg_loclist)
3962 : _(msg_qflist));
3963 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964#endif
3965
3966 case STL_MODIFIED:
3967 case STL_MODIFIED_ALT:
3968 itemisflag = TRUE;
3969 switch ((opt == STL_MODIFIED_ALT)
3970 + bufIsChanged(wp->w_buffer) * 2
3971 + (!wp->w_buffer->b_p_ma) * 4)
3972 {
3973 case 2: str = (char_u *)"[+]"; break;
3974 case 3: str = (char_u *)",+"; break;
3975 case 4: str = (char_u *)"[-]"; break;
3976 case 5: str = (char_u *)",-"; break;
3977 case 6: str = (char_u *)"[+-]"; break;
3978 case 7: str = (char_u *)",+-"; break;
3979 }
3980 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003981
3982 case STL_HIGHLIGHT:
3983 t = s;
3984 while (*s != '#' && *s != NUL)
3985 ++s;
3986 if (*s == '#')
3987 {
3988 item[curitem].type = Highlight;
3989 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003990 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003991 curitem++;
3992 }
3993 ++s;
3994 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 }
3996
3997 item[curitem].start = p;
3998 item[curitem].type = Normal;
3999 if (str != NULL && *str)
4000 {
4001 t = str;
4002 if (itemisflag)
4003 {
4004 if ((t[0] && t[1])
4005 && ((!prevchar_isitem && *t == ',')
4006 || (prevchar_isflag && *t == ' ')))
4007 t++;
4008 prevchar_isflag = TRUE;
4009 }
4010 l = vim_strsize(t);
4011 if (l > 0)
4012 prevchar_isitem = TRUE;
4013 if (l > maxwid)
4014 {
4015 while (l >= maxwid)
4016#ifdef FEAT_MBYTE
4017 if (has_mbyte)
4018 {
4019 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004020 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 }
4022 else
4023#endif
4024 l -= byte2cells(*t++);
4025 if (p + 1 >= out + outlen)
4026 break;
4027 *p++ = '<';
4028 }
4029 if (minwid > 0)
4030 {
4031 for (; l < minwid && p + 1 < out + outlen; l++)
4032 {
4033 /* Don't put a "-" in front of a digit. */
4034 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4035 *p++ = ' ';
4036 else
4037 *p++ = fillchar;
4038 }
4039 minwid = 0;
4040 }
4041 else
4042 minwid *= -1;
4043 while (*t && p + 1 < out + outlen)
4044 {
4045 *p++ = *t++;
4046 /* Change a space by fillchar, unless fillchar is '-' and a
4047 * digit follows. */
4048 if (fillable && p[-1] == ' '
4049 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4050 p[-1] = fillchar;
4051 }
4052 for (; l < minwid && p + 1 < out + outlen; l++)
4053 *p++ = fillchar;
4054 }
4055 else if (num >= 0)
4056 {
4057 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4058 char_u nstr[20];
4059
4060 if (p + 20 >= out + outlen)
4061 break; /* not sufficient space */
4062 prevchar_isitem = TRUE;
4063 t = nstr;
4064 if (opt == STL_VIRTCOL_ALT)
4065 {
4066 *t++ = '-';
4067 minwid--;
4068 }
4069 *t++ = '%';
4070 if (zeropad)
4071 *t++ = '0';
4072 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004073 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 *t = 0;
4075
4076 for (n = num, l = 1; n >= nbase; n /= nbase)
4077 l++;
4078 if (opt == STL_VIRTCOL_ALT)
4079 l++;
4080 if (l > maxwid)
4081 {
4082 l += 2;
4083 n = l - maxwid;
4084 while (l-- > maxwid)
4085 num /= nbase;
4086 *t++ = '>';
4087 *t++ = '%';
4088 *t = t[-3];
4089 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004090 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4091 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
4093 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004094 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4095 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 p += STRLEN(p);
4097 }
4098 else
4099 item[curitem].type = Empty;
4100
4101 if (opt == STL_VIM_EXPR)
4102 vim_free(str);
4103
4104 if (num >= 0 || (!itemisflag && str && *str))
4105 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4106 curitem++;
4107 }
4108 *p = NUL;
4109 itemcnt = curitem;
4110
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004111#ifdef FEAT_EVAL
4112 if (usefmt != fmt)
4113 vim_free(usefmt);
4114#endif
4115
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116 width = vim_strsize(out);
4117 if (maxwidth > 0 && width > maxwidth)
4118 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004119 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120 l = 0;
4121 if (itemcnt == 0)
4122 s = out;
4123 else
4124 {
4125 for ( ; l < itemcnt; l++)
4126 if (item[l].type == Trunc)
4127 {
4128 /* Truncate at %< item. */
4129 s = item[l].start;
4130 break;
4131 }
4132 if (l == itemcnt)
4133 {
4134 /* No %< item, truncate first item. */
4135 s = item[0].start;
4136 l = 0;
4137 }
4138 }
4139
4140 if (width - vim_strsize(s) >= maxwidth)
4141 {
4142 /* Truncation mark is beyond max length */
4143#ifdef FEAT_MBYTE
4144 if (has_mbyte)
4145 {
4146 s = out;
4147 width = 0;
4148 for (;;)
4149 {
4150 width += ptr2cells(s);
4151 if (width >= maxwidth)
4152 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004153 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154 }
4155 /* Fill up for half a double-wide character. */
4156 while (++width < maxwidth)
4157 *s++ = fillchar;
4158 }
4159 else
4160#endif
4161 s = out + maxwidth - 1;
4162 for (l = 0; l < itemcnt; l++)
4163 if (item[l].start > s)
4164 break;
4165 itemcnt = l;
4166 *s++ = '>';
4167 *s = 0;
4168 }
4169 else
4170 {
4171#ifdef FEAT_MBYTE
4172 if (has_mbyte)
4173 {
4174 n = 0;
4175 while (width >= maxwidth)
4176 {
4177 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004178 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 }
4180 }
4181 else
4182#endif
4183 n = width - maxwidth + 1;
4184 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004185 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 *s = '<';
4187
4188 /* Fill up for half a double-wide character. */
4189 while (++width < maxwidth)
4190 {
4191 s = s + STRLEN(s);
4192 *s++ = fillchar;
4193 *s = NUL;
4194 }
4195
4196 --n; /* count the '<' */
4197 for (; l < itemcnt; l++)
4198 {
4199 if (item[l].start - n >= s)
4200 item[l].start -= n;
4201 else
4202 item[l].start = s;
4203 }
4204 }
4205 width = maxwidth;
4206 }
4207 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4208 {
4209 /* Apply STL_MIDDLE if any */
4210 for (l = 0; l < itemcnt; l++)
4211 if (item[l].type == Middle)
4212 break;
4213 if (l < itemcnt)
4214 {
4215 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004216 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 for (s = item[l].start; s < p; s++)
4218 *s = fillchar;
4219 for (l++; l < itemcnt; l++)
4220 item[l].start += maxwidth - width;
4221 width = maxwidth;
4222 }
4223 }
4224
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004225 /* Store the info about highlighting. */
4226 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004228 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 for (l = 0; l < itemcnt; l++)
4230 {
4231 if (item[l].type == Highlight)
4232 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004233 sp->start = item[l].start;
4234 sp->userhl = item[l].minwid;
4235 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 }
4237 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004238 sp->start = NULL;
4239 sp->userhl = 0;
4240 }
4241
4242 /* Store the info about tab pages labels. */
4243 if (tabtab != NULL)
4244 {
4245 sp = tabtab;
4246 for (l = 0; l < itemcnt; l++)
4247 {
4248 if (item[l].type == TabPage)
4249 {
4250 sp->start = item[l].start;
4251 sp->userhl = item[l].minwid;
4252 sp++;
4253 }
4254 }
4255 sp->start = NULL;
4256 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 }
4258
4259 return width;
4260}
4261#endif /* FEAT_STL_OPT */
4262
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004263#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4264 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004266 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4267 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 */
4269 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004270get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004272 char_u *buf;
4273 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274{
4275 long above; /* number of lines above window */
4276 long below; /* number of lines below window */
4277
4278 above = wp->w_topline - 1;
4279#ifdef FEAT_DIFF
4280 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4281#endif
4282 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4283 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004284 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4285 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004287 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004289 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 ? (int)(above / ((above + below) / 100L))
4291 : (int)(above * 100L / (above + below)));
4292}
4293#endif
4294
4295/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004296 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 * Return TRUE if it was appended.
4298 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004299 static int
4300append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 win_T *wp;
4302 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004303 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305{
4306 char_u *p;
4307
4308 if (ARGCOUNT <= 1) /* nothing to do */
4309 return FALSE;
4310
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004311 p = buf + STRLEN(buf); /* go to the end of the buffer */
4312 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 return FALSE;
4314 *p++ = ' ';
4315 *p++ = '(';
4316 if (add_file)
4317 {
4318 STRCPY(p, "file ");
4319 p += 5;
4320 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004321 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4322 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4324 return TRUE;
4325}
4326
4327/*
4328 * If fname is not a full path, make it a full path.
4329 * Returns pointer to allocated memory (NULL for failure).
4330 */
4331 char_u *
4332fix_fname(fname)
4333 char_u *fname;
4334{
4335 /*
4336 * Force expanding the path always for Unix, because symbolic links may
4337 * mess up the full path name, even though it starts with a '/'.
4338 * Also expand when there is ".." in the file name, try to remove it,
4339 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004340 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 * For MS-Windows also expand names like "longna~1" to "longname".
4342 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004343#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 return FullName_save(fname, TRUE);
4345#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004346 if (!vim_isAbsName(fname)
4347 || strstr((char *)fname, "..") != NULL
4348 || strstr((char *)fname, "//") != NULL
4349# ifdef BACKSLASH_IN_FILENAME
4350 || strstr((char *)fname, "\\\\") != NULL
4351# endif
4352# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004354# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 )
4356 return FullName_save(fname, FALSE);
4357
4358 fname = vim_strsave(fname);
4359
Bram Moolenaar9b942202007-10-03 12:31:33 +00004360# ifdef USE_FNAME_CASE
4361# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004363# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 {
4365 if (fname != NULL)
4366 fname_case(fname, 0); /* set correct case for file name */
4367 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004368# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
4370 return fname;
4371#endif
4372}
4373
4374/*
4375 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4376 * "ffname" becomes a pointer to allocated memory (or NULL).
4377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 void
4379fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004380 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 char_u **ffname;
4382 char_u **sfname;
4383{
4384 if (*ffname == NULL) /* if no file name given, nothing to do */
4385 return;
4386 if (*sfname == NULL) /* if no short file name given, use ffname */
4387 *sfname = *ffname;
4388 *ffname = fix_fname(*ffname); /* expand to full path */
4389
4390#ifdef FEAT_SHORTCUT
4391 if (!buf->b_p_bin)
4392 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004393 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394
4395 /* If the file name is a shortcut file, use the file it links to. */
4396 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004397 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
4399 vim_free(*ffname);
4400 *ffname = rfname;
4401 *sfname = rfname;
4402 }
4403 }
4404#endif
4405}
4406
4407/*
4408 * Get the file name for an argument list entry.
4409 */
4410 char_u *
4411alist_name(aep)
4412 aentry_T *aep;
4413{
4414 buf_T *bp;
4415
4416 /* Use the name from the associated buffer if it exists. */
4417 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004418 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 return aep->ae_fname;
4420 return bp->b_fname;
4421}
4422
4423#if defined(FEAT_WINDOWS) || defined(PROTO)
4424/*
4425 * do_arg_all(): Open up to 'count' windows, one for each argument.
4426 */
4427 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004428do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 int count;
4430 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004431 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432{
4433 int i;
4434 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004435 char_u *opened; /* Array of weight for which args are open:
4436 * 0: not opened
4437 * 1: opened in other tab
4438 * 2: opened in curtab
4439 * 3: opened in curtab and curwin
4440 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004441 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 int use_firstwin = FALSE; /* use first window for arglist */
4443 int split_ret = OK;
4444 int p_ea_save;
4445 alist_T *alist; /* argument list to be used */
4446 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004447 tabpage_T *tpnext;
4448 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004449 win_T *old_curwin, *last_curwin;
4450 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004451 win_T *new_curwin = NULL;
4452 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453
4454 if (ARGCOUNT <= 0)
4455 {
4456 /* Don't give an error message. We don't want it when the ":all"
4457 * command is in the .vimrc. */
4458 return;
4459 }
4460 setpcmark();
4461
4462 opened_len = ARGCOUNT;
4463 opened = alloc_clear((unsigned)opened_len);
4464 if (opened == NULL)
4465 return;
4466
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004467 /* Autocommands may do anything to the argument list. Make sure it's not
4468 * freed while we are working here by "locking" it. We still have to
4469 * watch out for its size to be changed. */
4470 alist = curwin->w_alist;
4471 ++alist->al_refcount;
4472
4473 old_curwin = curwin;
4474 old_curtab = curtab;
4475
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476#ifdef FEAT_GUI
4477 need_mouse_correct = TRUE;
4478#endif
4479
4480 /*
4481 * Try closing all windows that are not in the argument list.
4482 * Also close windows that are not full width;
4483 * When 'hidden' or "forceit" set the buffer becomes hidden.
4484 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004485 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004487 if (had_tab > 0)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004488 goto_tabpage_tp(first_tabpage, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004489 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004491 tpnext = curtab->tp_next;
4492 for (wp = firstwin; wp != NULL; wp = wpnext)
4493 {
4494 wpnext = wp->w_next;
4495 buf = wp->w_buffer;
4496 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004497 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004499 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004501 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004502 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004503 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004505 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004506 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004508 if (i < alist->al_ga.ga_len
4509 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4510 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4511 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004513 int weight = 1;
4514
4515 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004516 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004517 ++weight;
4518 if (old_curwin == wp)
4519 ++weight;
4520 }
4521
4522 if (weight > (int)opened[i])
4523 {
4524 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004525 if (i == 0)
4526 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004527 if (new_curwin != NULL)
4528 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004529 new_curwin = wp;
4530 new_curtab = curtab;
4531 }
4532 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004533 else if (keep_tabs)
4534 i = opened_len;
4535
4536 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004537 {
4538 /* Use the current argument list for all windows
4539 * containing a file from it. */
4540 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004541 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004542 ++wp->w_alist->al_refcount;
4543 }
4544 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004548 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004550 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004552 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004553 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004555 /* If the buffer was changed, and we would like to hide it,
4556 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004557 if (!P_HID(buf) && buf->b_nwindows <= 1
4558 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004560 (void)autowrite(buf, FALSE);
4561#ifdef FEAT_AUTOCMD
4562 /* check if autocommands removed the window */
4563 if (!win_valid(wp) || !buf_valid(buf))
4564 {
4565 wpnext = firstwin; /* start all over... */
4566 continue;
4567 }
4568#endif
4569 }
4570#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004571 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004572 if (firstwin == lastwin
4573 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004574#endif
4575 use_firstwin = TRUE;
4576#ifdef FEAT_WINDOWS
4577 else
4578 {
4579 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4580# ifdef FEAT_AUTOCMD
4581 /* check if autocommands removed the next window */
4582 if (!win_valid(wpnext))
4583 wpnext = firstwin; /* start all over... */
4584# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586#endif
4587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
4589 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004590
4591 /* Without the ":tab" modifier only do the current tab page. */
4592 if (had_tab == 0 || tpnext == NULL)
4593 break;
4594
4595# ifdef FEAT_AUTOCMD
4596 /* check if autocommands removed the next tab page */
4597 if (!valid_tabpage(tpnext))
4598 tpnext = first_tabpage; /* start all over...*/
4599# endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02004600 goto_tabpage_tp(tpnext, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
4602
4603 /*
4604 * Open a window for files in the argument list that don't have one.
4605 * ARGCOUNT may change while doing this, because of autocommands.
4606 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004607 if (count > opened_len || count <= 0)
4608 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609
4610#ifdef FEAT_AUTOCMD
4611 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4612 ++autocmd_no_enter;
4613 ++autocmd_no_leave;
4614#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004615 last_curwin = curwin;
4616 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004618#ifdef FEAT_WINDOWS
4619 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4620 * leaving an empty tab page when executed locally. */
4621 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4622 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4623 use_firstwin = TRUE;
4624#endif
4625
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004626 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
4628 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4629 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004630 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
4632 /* Move the already present window to below the current window */
4633 if (curwin->w_arg_idx != i)
4634 {
4635 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4636 {
4637 if (wpnext->w_arg_idx == i)
4638 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004639 if (keep_tabs)
4640 {
4641 new_curwin = wpnext;
4642 new_curtab = curtab;
4643 }
4644 else
4645 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 break;
4647 }
4648 }
4649 }
4650 }
4651 else if (split_ret == OK)
4652 {
4653 if (!use_firstwin) /* split current window */
4654 {
4655 p_ea_save = p_ea;
4656 p_ea = TRUE; /* use space from all windows */
4657 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4658 p_ea = p_ea_save;
4659 if (split_ret == FAIL)
4660 continue;
4661 }
4662#ifdef FEAT_AUTOCMD
4663 else /* first window: do autocmd for leaving this buffer */
4664 --autocmd_no_leave;
4665#endif
4666
4667 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004668 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 */
4670 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004671 if (i == 0)
4672 {
4673 new_curwin = curwin;
4674 new_curtab = curtab;
4675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4677 ECMD_ONE,
4678 ((P_HID(curwin->w_buffer)
4679 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004680 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681#ifdef FEAT_AUTOCMD
4682 if (use_firstwin)
4683 ++autocmd_no_leave;
4684#endif
4685 use_firstwin = FALSE;
4686 }
4687 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004688
4689 /* When ":tab" was used open a new tab for a new window repeatedly. */
4690 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4691 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
4693
4694 /* Remove the "lock" on the argument list. */
4695 alist_unlink(alist);
4696
4697#ifdef FEAT_AUTOCMD
4698 --autocmd_no_enter;
4699#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004700 /* restore last referenced tabpage's curwin */
4701 if (last_curtab != new_curtab)
4702 {
4703 if (valid_tabpage(last_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +02004704 goto_tabpage_tp(last_curtab, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004705 if (win_valid(last_curwin))
4706 win_enter(last_curwin, FALSE);
4707 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004708 /* to window with first arg */
4709 if (valid_tabpage(new_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +02004710 goto_tabpage_tp(new_curtab, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004711 if (win_valid(new_curwin))
4712 win_enter(new_curwin, FALSE);
4713
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714#ifdef FEAT_AUTOCMD
4715 --autocmd_no_leave;
4716#endif
4717 vim_free(opened);
4718}
4719
4720# if defined(FEAT_LISTCMDS) || defined(PROTO)
4721/*
4722 * Open a window for a number of buffers.
4723 */
4724 void
4725ex_buffer_all(eap)
4726 exarg_T *eap;
4727{
4728 buf_T *buf;
4729 win_T *wp, *wpnext;
4730 int split_ret = OK;
4731 int p_ea_save;
4732 int open_wins = 0;
4733 int r;
4734 int count; /* Maximum number of windows to open. */
4735 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004736#ifdef FEAT_WINDOWS
4737 int had_tab = cmdmod.tab;
4738 tabpage_T *tpnext;
4739#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740
4741 if (eap->addr_count == 0) /* make as many windows as possible */
4742 count = 9999;
4743 else
4744 count = eap->line2; /* make as many windows as specified */
4745 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4746 all = FALSE;
4747 else
4748 all = TRUE;
4749
4750 setpcmark();
4751
4752#ifdef FEAT_GUI
4753 need_mouse_correct = TRUE;
4754#endif
4755
4756 /*
4757 * Close superfluous windows (two windows for the same buffer).
4758 * Also close windows that are not full-width.
4759 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004760#ifdef FEAT_WINDOWS
4761 if (had_tab > 0)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004762 goto_tabpage_tp(first_tabpage, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004763 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004766 tpnext = curtab->tp_next;
4767 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004769 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004770 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004771#ifdef FEAT_VERTSPLIT
4772 || ((cmdmod.split & WSP_VERT)
4773 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004774 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004775 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004777#ifdef FEAT_WINDOWS
4778 || (had_tab > 0 && wp != firstwin)
4779#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004780 ) && firstwin != lastwin
4781#ifdef FEAT_AUTOCMD
4782 && !(wp->w_closing || wp->w_buffer->b_closing)
4783#endif
4784 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004785 {
4786 win_close(wp, FALSE);
4787#ifdef FEAT_AUTOCMD
4788 wpnext = firstwin; /* just in case an autocommand does
4789 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004790 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004791 open_wins = 0;
4792#endif
4793 }
4794 else
4795 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004797
4798#ifdef FEAT_WINDOWS
4799 /* Without the ":tab" modifier only do the current tab page. */
4800 if (had_tab == 0 || tpnext == NULL)
4801 break;
Bram Moolenaara8596c42012-06-13 14:28:20 +02004802 goto_tabpage_tp(tpnext, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004804#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805
4806 /*
4807 * Go through the buffer list. When a buffer doesn't have a window yet,
4808 * open one. Otherwise move the window to the right position.
4809 * Watch out for autocommands that delete buffers or windows!
4810 */
4811#ifdef FEAT_AUTOCMD
4812 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4813 ++autocmd_no_enter;
4814#endif
4815 win_enter(lastwin, FALSE);
4816#ifdef FEAT_AUTOCMD
4817 ++autocmd_no_leave;
4818#endif
4819 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4820 {
4821 /* Check if this buffer needs a window */
4822 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4823 continue;
4824
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004825#ifdef FEAT_WINDOWS
4826 if (had_tab != 0)
4827 {
4828 /* With the ":tab" modifier don't move the window. */
4829 if (buf->b_nwindows > 0)
4830 wp = lastwin; /* buffer has a window, skip it */
4831 else
4832 wp = NULL;
4833 }
4834 else
4835#endif
4836 {
4837 /* Check if this buffer already has a window */
4838 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4839 if (wp->w_buffer == buf)
4840 break;
4841 /* If the buffer already has a window, move it */
4842 if (wp != NULL)
4843 win_move_after(wp, curwin);
4844 }
4845
4846 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
4848 /* Split the window and put the buffer in it */
4849 p_ea_save = p_ea;
4850 p_ea = TRUE; /* use space from all windows */
4851 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4852 ++open_wins;
4853 p_ea = p_ea_save;
4854 if (split_ret == FAIL)
4855 continue;
4856
4857 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004858#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 swap_exists_action = SEA_DIALOG;
4860#endif
4861 set_curbuf(buf, DOBUF_GOTO);
4862#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004865#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004867# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 break;
4869 }
4870#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004871#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 if (swap_exists_action == SEA_QUIT)
4873 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004874# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4875 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004876
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004877 /* Reset the error/interrupt/exception state here so that
4878 * aborting() returns FALSE when closing a window. */
4879 enter_cleanup(&cs);
4880# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004881
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004882 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 win_close(curwin, TRUE);
4884 --open_wins;
4885 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004886 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004887
4888# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4889 /* Restore the error/interrupt/exception state if not
4890 * discarded by a new aborting error, interrupt, or uncaught
4891 * exception. */
4892 leave_cleanup(&cs);
4893# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 }
4895 else
4896 handle_swap_exists(NULL);
4897#endif
4898 }
4899
4900 ui_breakcheck();
4901 if (got_int)
4902 {
4903 (void)vgetc(); /* only break the file loading, not the rest */
4904 break;
4905 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004906#ifdef FEAT_EVAL
4907 /* Autocommands deleted the buffer or aborted script processing!!! */
4908 if (aborting())
4909 break;
4910#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004911#ifdef FEAT_WINDOWS
4912 /* When ":tab" was used open a new tab for a new window repeatedly. */
4913 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4914 cmdmod.tab = 9999;
4915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917#ifdef FEAT_AUTOCMD
4918 --autocmd_no_enter;
4919#endif
4920 win_enter(firstwin, FALSE); /* back to first window */
4921#ifdef FEAT_AUTOCMD
4922 --autocmd_no_leave;
4923#endif
4924
4925 /*
4926 * Close superfluous windows.
4927 */
4928 for (wp = lastwin; open_wins > count; )
4929 {
4930 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4931 || autowrite(wp->w_buffer, FALSE) == OK);
4932#ifdef FEAT_AUTOCMD
4933 if (!win_valid(wp))
4934 {
4935 /* BufWrite Autocommands made the window invalid, start over */
4936 wp = lastwin;
4937 }
4938 else
4939#endif
4940 if (r)
4941 {
4942 win_close(wp, !P_HID(wp->w_buffer));
4943 --open_wins;
4944 wp = lastwin;
4945 }
4946 else
4947 {
4948 wp = wp->w_prev;
4949 if (wp == NULL)
4950 break;
4951 }
4952 }
4953}
4954# endif /* FEAT_LISTCMDS */
4955
4956#endif /* FEAT_WINDOWS */
4957
Bram Moolenaara3227e22006-03-08 21:32:40 +00004958static int chk_modeline __ARGS((linenr_T, int));
4959
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960/*
4961 * do_modelines() - process mode lines for the current file
4962 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004963 * "flags" can be:
4964 * OPT_WINONLY only set options local to window
4965 * OPT_NOWIN don't set options local to window
4966 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 * Returns immediately if the "ml" option isn't set.
4968 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004970do_modelines(flags)
4971 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004973 linenr_T lnum;
4974 int nmlines;
4975 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976
4977 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4978 return;
4979
4980 /* Disallow recursive entry here. Can happen when executing a modeline
4981 * triggers an autocommand, which reloads modelines with a ":do". */
4982 if (entered)
4983 return;
4984
4985 ++entered;
4986 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4987 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004988 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 nmlines = 0;
4990
4991 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4992 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004993 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 nmlines = 0;
4995 --entered;
4996}
4997
4998#include "version.h" /* for version number */
4999
5000/*
5001 * chk_modeline() - check a single line for a mode string
5002 * Return FAIL if an error encountered.
5003 */
5004 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00005005chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005007 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008{
5009 char_u *s;
5010 char_u *e;
5011 char_u *linecopy; /* local copy of any modeline found */
5012 int prev;
5013 int vers;
5014 int end;
5015 int retval = OK;
5016 char_u *save_sourcing_name;
5017 linenr_T save_sourcing_lnum;
5018#ifdef FEAT_EVAL
5019 scid_T save_SID;
5020#endif
5021
5022 prev = -1;
5023 for (s = ml_get(lnum); *s != NUL; ++s)
5024 {
5025 if (prev == -1 || vim_isspace(prev))
5026 {
5027 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5028 || STRNCMP(s, "vi:", (size_t)3) == 0)
5029 break;
5030 if (STRNCMP(s, "vim", 3) == 0)
5031 {
5032 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5033 e = s + 4;
5034 else
5035 e = s + 3;
5036 vers = getdigits(&e);
5037 if (*e == ':'
5038 && (s[3] == ':'
5039 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5040 || (VIM_VERSION_100 < vers && s[3] == '<')
5041 || (VIM_VERSION_100 > vers && s[3] == '>')
5042 || (VIM_VERSION_100 == vers && s[3] == '=')))
5043 break;
5044 }
5045 }
5046 prev = *s;
5047 }
5048
5049 if (*s)
5050 {
5051 do /* skip over "ex:", "vi:" or "vim:" */
5052 ++s;
5053 while (s[-1] != ':');
5054
5055 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5056 if (linecopy == NULL)
5057 return FAIL;
5058
5059 save_sourcing_lnum = sourcing_lnum;
5060 save_sourcing_name = sourcing_name;
5061 sourcing_lnum = lnum; /* prepare for emsg() */
5062 sourcing_name = (char_u *)"modelines";
5063
5064 end = FALSE;
5065 while (end == FALSE)
5066 {
5067 s = skipwhite(s);
5068 if (*s == NUL)
5069 break;
5070
5071 /*
5072 * Find end of set command: ':' or end of line.
5073 * Skip over "\:", replacing it with ":".
5074 */
5075 for (e = s; *e != ':' && *e != NUL; ++e)
5076 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005077 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (*e == NUL)
5079 end = TRUE;
5080
5081 /*
5082 * If there is a "set" command, require a terminating ':' and
5083 * ignore the stuff after the ':'.
5084 * "vi:set opt opt opt: foo" -- foo not interpreted
5085 * "vi:opt opt opt: foo" -- foo interpreted
5086 * Accept "se" for compatibility with Elvis.
5087 */
5088 if (STRNCMP(s, "set ", (size_t)4) == 0
5089 || STRNCMP(s, "se ", (size_t)3) == 0)
5090 {
5091 if (*e != ':') /* no terminating ':'? */
5092 break;
5093 end = TRUE;
5094 s = vim_strchr(s, ' ') + 1;
5095 }
5096 *e = NUL; /* truncate the set command */
5097
5098 if (*s != NUL) /* skip over an empty "::" */
5099 {
5100#ifdef FEAT_EVAL
5101 save_SID = current_SID;
5102 current_SID = SID_MODELINE;
5103#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005104 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105#ifdef FEAT_EVAL
5106 current_SID = save_SID;
5107#endif
5108 if (retval == FAIL) /* stop if error found */
5109 break;
5110 }
5111 s = e + 1; /* advance to next part */
5112 }
5113
5114 sourcing_lnum = save_sourcing_lnum;
5115 sourcing_name = save_sourcing_name;
5116
5117 vim_free(linecopy);
5118 }
5119 return retval;
5120}
5121
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005122#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 int
5124read_viminfo_bufferlist(virp, writing)
5125 vir_T *virp;
5126 int writing;
5127{
5128 char_u *tab;
5129 linenr_T lnum;
5130 colnr_T col;
5131 buf_T *buf;
5132 char_u *sfname;
5133 char_u *xline;
5134
5135 /* Handle long line and escaped characters. */
5136 xline = viminfo_readstring(virp, 1, FALSE);
5137
5138 /* don't read in if there are files on the command-line or if writing: */
5139 if (xline != NULL && !writing && ARGCOUNT == 0
5140 && find_viminfo_parameter('%') != NULL)
5141 {
5142 /* Format is: <fname> Tab <lnum> Tab <col>.
5143 * Watch out for a Tab in the file name, work from the end. */
5144 lnum = 0;
5145 col = 0;
5146 tab = vim_strrchr(xline, '\t');
5147 if (tab != NULL)
5148 {
5149 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005150 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 tab = vim_strrchr(xline, '\t');
5152 if (tab != NULL)
5153 {
5154 *tab++ = '\0';
5155 lnum = atol((char *)tab);
5156 }
5157 }
5158
5159 /* Expand "~/" in the file name at "line + 1" to a full path.
5160 * Then try shortening it by comparing with the current directory */
5161 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005162 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5165 if (buf != NULL) /* just in case... */
5166 {
5167 buf->b_last_cursor.lnum = lnum;
5168 buf->b_last_cursor.col = col;
5169 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5170 }
5171 }
5172 vim_free(xline);
5173
5174 return viminfo_readline(virp);
5175}
5176
5177 void
5178write_viminfo_bufferlist(fp)
5179 FILE *fp;
5180{
5181 buf_T *buf;
5182#ifdef FEAT_WINDOWS
5183 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005184 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185#endif
5186 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005187 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188
5189 if (find_viminfo_parameter('%') == NULL)
5190 return;
5191
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005192 /* Without a number -1 is returned: do all buffers. */
5193 max_buffers = get_viminfo_parameter('%');
5194
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005196#define LINE_BUF_LEN (MAXPATHL + 40)
5197 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (line == NULL)
5199 return;
5200
5201#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005202 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 set_last_cursor(win);
5204#else
5205 set_last_cursor(curwin);
5206#endif
5207
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005208 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5210 {
5211 if (buf->b_fname == NULL
5212 || !buf->b_p_bl
5213#ifdef FEAT_QUICKFIX
5214 || bt_quickfix(buf)
5215#endif
5216 || removable(buf->b_ffname))
5217 continue;
5218
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005219 if (max_buffers-- == 0)
5220 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 putc('%', fp);
5222 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005223 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 (long)buf->b_last_cursor.lnum,
5225 buf->b_last_cursor.col);
5226 viminfo_writestring(fp, line);
5227 }
5228 vim_free(line);
5229}
5230#endif
5231
5232
5233/*
5234 * Return special buffer name.
5235 * Returns NULL when the buffer has a normal file name.
5236 */
5237 char *
5238buf_spname(buf)
5239 buf_T *buf;
5240{
5241#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5242 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005243 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005244 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005245 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005246
5247 /*
5248 * For location list window, w_llist_ref points to the location list.
5249 * For quickfix window, w_llist_ref is NULL.
5250 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005251 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005252 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005253 goto win_found;
5254win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005255 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005256 return _(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005257 else
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005258 return _(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260#endif
5261#ifdef FEAT_QUICKFIX
5262 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5263 * contains the name as specified by the user */
5264 if (bt_nofile(buf))
5265 {
5266 if (buf->b_sfname != NULL)
5267 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005268 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 }
5270#endif
5271 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005272 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005273 return NULL;
5274}
5275
5276
5277#if defined(FEAT_SIGNS) || defined(PROTO)
5278/*
5279 * Insert the sign into the signlist.
5280 */
5281 static void
5282insert_sign(buf, prev, next, id, lnum, typenr)
5283 buf_T *buf; /* buffer to store sign in */
5284 signlist_T *prev; /* previous sign entry */
5285 signlist_T *next; /* next sign entry */
5286 int id; /* sign ID */
5287 linenr_T lnum; /* line number which gets the mark */
5288 int typenr; /* typenr of sign we are adding */
5289{
5290 signlist_T *newsign;
5291
5292 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5293 if (newsign != NULL)
5294 {
5295 newsign->id = id;
5296 newsign->lnum = lnum;
5297 newsign->typenr = typenr;
5298 newsign->next = next;
5299#ifdef FEAT_NETBEANS_INTG
5300 newsign->prev = prev;
5301 if (next != NULL)
5302 next->prev = newsign;
5303#endif
5304
5305 if (prev == NULL)
5306 {
5307 /* When adding first sign need to redraw the windows to create the
5308 * column for signs. */
5309 if (buf->b_signlist == NULL)
5310 {
5311 redraw_buf_later(buf, NOT_VALID);
5312 changed_cline_bef_curs();
5313 }
5314
5315 /* first sign in signlist */
5316 buf->b_signlist = newsign;
5317 }
5318 else
5319 prev->next = newsign;
5320 }
5321}
5322
5323/*
5324 * Add the sign into the signlist. Find the right spot to do it though.
5325 */
5326 void
5327buf_addsign(buf, id, lnum, typenr)
5328 buf_T *buf; /* buffer to store sign in */
5329 int id; /* sign ID */
5330 linenr_T lnum; /* line number which gets the mark */
5331 int typenr; /* typenr of sign we are adding */
5332{
5333 signlist_T *sign; /* a sign in the signlist */
5334 signlist_T *prev; /* the previous sign */
5335
5336 prev = NULL;
5337 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5338 {
5339 if (lnum == sign->lnum && id == sign->id)
5340 {
5341 sign->typenr = typenr;
5342 return;
5343 }
5344 else if (
5345#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5346 id < 0 &&
5347#endif
5348 lnum < sign->lnum)
5349 {
5350#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5351 /* XXX - GRP: Is this because of sign slide problem? Or is it
5352 * really needed? Or is it because we allow multiple signs per
5353 * line? If so, should I add that feature to FEAT_SIGNS?
5354 */
5355 while (prev != NULL && prev->lnum == lnum)
5356 prev = prev->prev;
5357 if (prev == NULL)
5358 sign = buf->b_signlist;
5359 else
5360 sign = prev->next;
5361#endif
5362 insert_sign(buf, prev, sign, id, lnum, typenr);
5363 return;
5364 }
5365 prev = sign;
5366 }
5367#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5368 /* XXX - GRP: See previous comment */
5369 while (prev != NULL && prev->lnum == lnum)
5370 prev = prev->prev;
5371 if (prev == NULL)
5372 sign = buf->b_signlist;
5373 else
5374 sign = prev->next;
5375#endif
5376 insert_sign(buf, prev, sign, id, lnum, typenr);
5377
5378 return;
5379}
5380
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005381 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382buf_change_sign_type(buf, markId, typenr)
5383 buf_T *buf; /* buffer to store sign in */
5384 int markId; /* sign ID */
5385 int typenr; /* typenr of sign we are adding */
5386{
5387 signlist_T *sign; /* a sign in the signlist */
5388
5389 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5390 {
5391 if (sign->id == markId)
5392 {
5393 sign->typenr = typenr;
5394 return sign->lnum;
5395 }
5396 }
5397
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005398 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399}
5400
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005401 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402buf_getsigntype(buf, lnum, type)
5403 buf_T *buf;
5404 linenr_T lnum;
5405 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5406{
5407 signlist_T *sign; /* a sign in a b_signlist */
5408
5409 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5410 if (sign->lnum == lnum
5411 && (type == SIGN_ANY
5412# ifdef FEAT_SIGN_ICONS
5413 || (type == SIGN_ICON
5414 && sign_get_image(sign->typenr) != NULL)
5415# endif
5416 || (type == SIGN_TEXT
5417 && sign_get_text(sign->typenr) != NULL)
5418 || (type == SIGN_LINEHL
5419 && sign_get_attr(sign->typenr, TRUE) != 0)))
5420 return sign->typenr;
5421 return 0;
5422}
5423
5424
5425 linenr_T
5426buf_delsign(buf, id)
5427 buf_T *buf; /* buffer sign is stored in */
5428 int id; /* sign id */
5429{
5430 signlist_T **lastp; /* pointer to pointer to current sign */
5431 signlist_T *sign; /* a sign in a b_signlist */
5432 signlist_T *next; /* the next sign in a b_signlist */
5433 linenr_T lnum; /* line number whose sign was deleted */
5434
5435 lastp = &buf->b_signlist;
5436 lnum = 0;
5437 for (sign = buf->b_signlist; sign != NULL; sign = next)
5438 {
5439 next = sign->next;
5440 if (sign->id == id)
5441 {
5442 *lastp = next;
5443#ifdef FEAT_NETBEANS_INTG
5444 if (next != NULL)
5445 next->prev = sign->prev;
5446#endif
5447 lnum = sign->lnum;
5448 vim_free(sign);
5449 break;
5450 }
5451 else
5452 lastp = &sign->next;
5453 }
5454
5455 /* When deleted the last sign need to redraw the windows to remove the
5456 * sign column. */
5457 if (buf->b_signlist == NULL)
5458 {
5459 redraw_buf_later(buf, NOT_VALID);
5460 changed_cline_bef_curs();
5461 }
5462
5463 return lnum;
5464}
5465
5466
5467/*
5468 * Find the line number of the sign with the requested id. If the sign does
5469 * not exist, return 0 as the line number. This will still let the correct file
5470 * get loaded.
5471 */
5472 int
5473buf_findsign(buf, id)
5474 buf_T *buf; /* buffer to store sign in */
5475 int id; /* sign ID */
5476{
5477 signlist_T *sign; /* a sign in the signlist */
5478
5479 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5480 if (sign->id == id)
5481 return sign->lnum;
5482
5483 return 0;
5484}
5485
5486 int
5487buf_findsign_id(buf, lnum)
5488 buf_T *buf; /* buffer whose sign we are searching for */
5489 linenr_T lnum; /* line number of sign */
5490{
5491 signlist_T *sign; /* a sign in the signlist */
5492
5493 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5494 if (sign->lnum == lnum)
5495 return sign->id;
5496
5497 return 0;
5498}
5499
5500
5501# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5502/* see if a given type of sign exists on a specific line */
5503 int
5504buf_findsigntype_id(buf, lnum, typenr)
5505 buf_T *buf; /* buffer whose sign we are searching for */
5506 linenr_T lnum; /* line number of sign */
5507 int typenr; /* sign type number */
5508{
5509 signlist_T *sign; /* a sign in the signlist */
5510
5511 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5512 if (sign->lnum == lnum && sign->typenr == typenr)
5513 return sign->id;
5514
5515 return 0;
5516}
5517
5518
5519# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5520/* return the number of icons on the given line */
5521 int
5522buf_signcount(buf, lnum)
5523 buf_T *buf;
5524 linenr_T lnum;
5525{
5526 signlist_T *sign; /* a sign in the signlist */
5527 int count = 0;
5528
5529 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5530 if (sign->lnum == lnum)
5531 if (sign_get_image(sign->typenr) != NULL)
5532 count++;
5533
5534 return count;
5535}
5536# endif /* FEAT_SIGN_ICONS */
5537# endif /* FEAT_NETBEANS_INTG */
5538
5539
5540/*
5541 * Delete signs in buffer "buf".
5542 */
5543 static void
5544buf_delete_signs(buf)
5545 buf_T *buf;
5546{
5547 signlist_T *next;
5548
5549 while (buf->b_signlist != NULL)
5550 {
5551 next = buf->b_signlist->next;
5552 vim_free(buf->b_signlist);
5553 buf->b_signlist = next;
5554 }
5555}
5556
5557/*
5558 * Delete all signs in all buffers.
5559 */
5560 void
5561buf_delete_all_signs()
5562{
5563 buf_T *buf; /* buffer we are checking for signs */
5564
5565 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5566 if (buf->b_signlist != NULL)
5567 {
5568 /* Need to redraw the windows to remove the sign column. */
5569 redraw_buf_later(buf, NOT_VALID);
5570 buf_delete_signs(buf);
5571 }
5572}
5573
5574/*
5575 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5576 */
5577 void
5578sign_list_placed(rbuf)
5579 buf_T *rbuf;
5580{
5581 buf_T *buf;
5582 signlist_T *p;
5583 char lbuf[BUFSIZ];
5584
5585 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5586 msg_putchar('\n');
5587 if (rbuf == NULL)
5588 buf = firstbuf;
5589 else
5590 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005591 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005592 {
5593 if (buf->b_signlist != NULL)
5594 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005595 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5597 msg_putchar('\n');
5598 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005599 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005601 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5603 MSG_PUTS(lbuf);
5604 msg_putchar('\n');
5605 }
5606 if (rbuf != NULL)
5607 break;
5608 buf = buf->b_next;
5609 }
5610}
5611
5612/*
5613 * Adjust a placed sign for inserted/deleted lines.
5614 */
5615 void
5616sign_mark_adjust(line1, line2, amount, amount_after)
5617 linenr_T line1;
5618 linenr_T line2;
5619 long amount;
5620 long amount_after;
5621{
5622 signlist_T *sign; /* a sign in a b_signlist */
5623
5624 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5625 {
5626 if (sign->lnum >= line1 && sign->lnum <= line2)
5627 {
5628 if (amount == MAXLNUM)
5629 sign->lnum = line1;
5630 else
5631 sign->lnum += amount;
5632 }
5633 else if (sign->lnum > line2)
5634 sign->lnum += amount_after;
5635 }
5636}
5637#endif /* FEAT_SIGNS */
5638
5639/*
5640 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5641 */
5642 void
5643set_buflisted(on)
5644 int on;
5645{
5646 if (on != curbuf->b_p_bl)
5647 {
5648 curbuf->b_p_bl = on;
5649#ifdef FEAT_AUTOCMD
5650 if (on)
5651 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5652 else
5653 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5654#endif
5655 }
5656}
5657
5658/*
5659 * Read the file for "buf" again and check if the contents changed.
5660 * Return TRUE if it changed or this could not be checked.
5661 */
5662 int
5663buf_contents_changed(buf)
5664 buf_T *buf;
5665{
5666 buf_T *newbuf;
5667 int differ = TRUE;
5668 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 exarg_T ea;
5671
5672 /* Allocate a buffer without putting it in the buffer list. */
5673 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5674 if (newbuf == NULL)
5675 return TRUE;
5676
5677 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5678 if (prep_exarg(&ea, buf) == FAIL)
5679 {
5680 wipe_buffer(newbuf, FALSE);
5681 return TRUE;
5682 }
5683
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 /* set curwin/curbuf to buf and save a few things */
5685 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686
Bram Moolenaar4770d092006-01-12 23:22:24 +00005687 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 && readfile(buf->b_ffname, buf->b_fname,
5689 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5690 &ea, READ_NEW | READ_DUMMY) == OK)
5691 {
5692 /* compare the two files line by line */
5693 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5694 {
5695 differ = FALSE;
5696 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5697 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5698 {
5699 differ = TRUE;
5700 break;
5701 }
5702 }
5703 }
5704 vim_free(ea.cmd);
5705
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 /* restore curwin/curbuf and a few other things */
5707 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708
5709 if (curbuf != newbuf) /* safety check */
5710 wipe_buffer(newbuf, FALSE);
5711
5712 return differ;
5713}
5714
5715/*
5716 * Wipe out a buffer and decrement the last buffer number if it was used for
5717 * this buffer. Call this to wipe out a temp buffer that does not contain any
5718 * marks.
5719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 void
5721wipe_buffer(buf, aucmd)
5722 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005723 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724{
5725 if (buf->b_fnum == top_file_num - 1)
5726 --top_file_num;
5727
5728#ifdef FEAT_AUTOCMD
5729 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005730 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005732 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005733#ifdef FEAT_AUTOCMD
5734 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005735 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736#endif
5737}