blob: 183429159d6ce2044a5ca9aafbcf94e4648d4806 [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 Moolenaar9e931222012-06-20 11:55:01 +02001366 win_T *prevwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1368 || action == DOBUF_WIPE);
1369
1370 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001371 if (!cmdmod.keepalt)
1372 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001373 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374
1375#ifdef FEAT_VISUAL
1376 /* Don't restart Select mode after switching to another buffer. */
1377 VIsual_reselect = FALSE;
1378#endif
1379
1380 /* close_windows() or apply_autocmds() may change curbuf */
1381 prevbuf = curbuf;
1382
1383#ifdef FEAT_AUTOCMD
1384 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1385# ifdef FEAT_EVAL
1386 if (buf_valid(prevbuf) && !aborting())
1387# else
1388 if (buf_valid(prevbuf))
1389# endif
1390#endif
1391 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001392#ifdef FEAT_SYN_HL
1393 if (prevbuf == curwin->w_buffer)
1394 reset_synblock(curwin);
1395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396#ifdef FEAT_WINDOWS
1397 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001398 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399#endif
1400#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1401 if (buf_valid(prevbuf) && !aborting())
1402#else
1403 if (buf_valid(prevbuf))
1404#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001405 {
Bram Moolenaar9e931222012-06-20 11:55:01 +02001406 prevwin = curwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001407 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001408 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1410 unload ? action : (action == DOBUF_GOTO
1411 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001412 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar9e931222012-06-20 11:55:01 +02001413 if (curwin != prevwin && win_valid(prevwin))
1414 /* autocommands changed curwin, Grr! */
1415 curwin = prevwin;
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 }
1418#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001419 /* An autocommand may have deleted "buf", already entered it (e.g., when
Bram Moolenaar9e931222012-06-20 11:55:01 +02001420 * it did ":bunload") or aborted the script processing!
1421 * If curwin->w_buffer is null, enter_buffer() will make it valid again */
1422 if ((buf_valid(buf) && buf != curbuf
1423#ifdef FEAT_EVAL
1424 && !aborting()
1425#endif
1426#ifdef FEAT_WINDOWS
1427 ) || curwin->w_buffer == NULL
1428#endif
1429 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430#endif
1431 enter_buffer(buf);
1432}
1433
1434/*
1435 * Enter a new current buffer.
1436 * Old curbuf must have been abandoned already!
1437 */
1438 void
1439enter_buffer(buf)
1440 buf_T *buf;
1441{
1442 /* Copy buffer and window local option values. Not for a help buffer. */
1443 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1444 if (!buf->b_help)
1445 get_winopts(buf);
1446#ifdef FEAT_FOLDING
1447 else
1448 /* Remove all folds in the window. */
1449 clearFolding(curwin);
1450 foldUpdateAll(curwin); /* update folds (later). */
1451#endif
1452
1453 /* Get the buffer in the current window. */
1454 curwin->w_buffer = buf;
1455 curbuf = buf;
1456 ++curbuf->b_nwindows;
1457
1458#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001459 if (curwin->w_p_diff)
1460 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461#endif
1462
Bram Moolenaara971b822011-09-14 14:43:25 +02001463#ifdef FEAT_SYN_HL
1464 curwin->w_s = &(buf->b_s);
1465#endif
1466
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 /* Cursor on first line by default. */
1468 curwin->w_cursor.lnum = 1;
1469 curwin->w_cursor.col = 0;
1470#ifdef FEAT_VIRTUALEDIT
1471 curwin->w_cursor.coladd = 0;
1472#endif
1473 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001474#ifdef FEAT_AUTOCMD
1475 curwin->w_topline_was_set = FALSE;
1476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001478 /* mark cursor position as being invalid */
1479 curwin->w_valid = 0;
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 /* Make sure the buffer is loaded. */
1482 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001483 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001484#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001485 /* If there is no filetype, allow for detecting one. Esp. useful for
1486 * ":ball" used in a autocommand. If there already is a filetype we
1487 * might prefer to keep it. */
1488 if (*curbuf->b_p_ft == NUL)
1489 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001490#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001491
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001492 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 else
1495 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001496 if (!msg_silent)
1497 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1499#ifdef FEAT_AUTOCMD
1500 curwin->w_topline = 1;
1501# ifdef FEAT_DIFF
1502 curwin->w_topfill = 0;
1503# endif
1504 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1505 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1506#endif
1507 }
1508
1509 /* If autocommands did not change the cursor position, restore cursor lnum
1510 * and possibly cursor col. */
1511 if (curwin->w_cursor.lnum == 1 && inindent(0))
1512 buflist_getfpos();
1513
1514 check_arg_idx(curwin); /* check for valid arg_idx */
1515#ifdef FEAT_TITLE
1516 maketitle();
1517#endif
1518#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001519 /* when autocmds didn't change it */
1520 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521#endif
1522 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1523
Bram Moolenaar009b2592004-10-24 19:18:58 +00001524#ifdef FEAT_NETBEANS_INTG
1525 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001526 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001527#endif
1528
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001529 /* Change directories when the 'acd' option is set. */
1530 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531
1532#ifdef FEAT_KEYMAP
1533 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001534 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001536#ifdef FEAT_SPELL
1537 /* May need to set the spell language. Can only do this after the buffer
1538 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001539 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1540 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001541#endif
1542
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 redraw_later(NOT_VALID);
1544}
1545
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001546#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1547/*
1548 * Change to the directory of the current buffer.
1549 */
1550 void
1551do_autochdir()
1552{
1553 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1554 shorten_fnames(TRUE);
1555}
1556#endif
1557
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558/*
1559 * functions for dealing with the buffer list
1560 */
1561
1562/*
1563 * Add a file name to the buffer list. Return a pointer to the buffer.
1564 * If the same file name already exists return a pointer to that buffer.
1565 * If it does not exist, or if fname == NULL, a new entry is created.
1566 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1567 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1568 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 * This is the ONLY way to create a new buffer.
1570 */
1571static int top_file_num = 1; /* highest file number */
1572
1573 buf_T *
1574buflist_new(ffname, sfname, lnum, flags)
1575 char_u *ffname; /* full path of fname or relative */
1576 char_u *sfname; /* short fname or NULL */
1577 linenr_T lnum; /* preferred cursor line */
1578 int flags; /* BLN_ defines */
1579{
1580 buf_T *buf;
1581#ifdef UNIX
1582 struct stat st;
1583#endif
1584
1585 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1586
1587 /*
1588 * If file name already exists in the list, update the entry.
1589 */
1590#ifdef UNIX
1591 /* On Unix we can use inode numbers when the file exists. Works better
1592 * for hard links. */
1593 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1594 st.st_dev = (dev_T)-1;
1595#endif
1596 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1597#ifdef UNIX
1598 buflist_findname_stat(ffname, &st)
1599#else
1600 buflist_findname(ffname)
1601#endif
1602 ) != NULL)
1603 {
1604 vim_free(ffname);
1605 if (lnum != 0)
1606 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1607 /* copy the options now, if 'cpo' doesn't have 's' and not done
1608 * already */
1609 buf_copy_options(buf, 0);
1610 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1611 {
1612 buf->b_p_bl = TRUE;
1613#ifdef FEAT_AUTOCMD
1614 if (!(flags & BLN_DUMMY))
1615 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1616#endif
1617 }
1618 return buf;
1619 }
1620
1621 /*
1622 * If the current buffer has no name and no contents, use the current
1623 * buffer. Otherwise: Need to allocate a new buffer structure.
1624 *
1625 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001626 * (A spell file buffer is allocated in spell.c, but that's not a normal
1627 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 */
1629 buf = NULL;
1630 if ((flags & BLN_CURBUF)
1631 && curbuf != NULL
1632 && curbuf->b_ffname == NULL
1633 && curbuf->b_nwindows <= 1
1634 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1635 {
1636 buf = curbuf;
1637#ifdef FEAT_AUTOCMD
1638 /* It's like this buffer is deleted. Watch out for autocommands that
1639 * change curbuf! If that happens, allocate a new buffer anyway. */
1640 if (curbuf->b_p_bl)
1641 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1642 if (buf == curbuf)
1643 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1644# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001645 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 return NULL;
1647# endif
1648#endif
1649#ifdef FEAT_QUICKFIX
1650# ifdef FEAT_AUTOCMD
1651 if (buf == curbuf)
1652# endif
1653 {
1654 /* Make sure 'bufhidden' and 'buftype' are empty */
1655 clear_string_option(&buf->b_p_bh);
1656 clear_string_option(&buf->b_p_bt);
1657 }
1658#endif
1659 }
1660 if (buf != curbuf || curbuf == NULL)
1661 {
1662 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1663 if (buf == NULL)
1664 {
1665 vim_free(ffname);
1666 return NULL;
1667 }
1668 }
1669
1670 if (ffname != NULL)
1671 {
1672 buf->b_ffname = ffname;
1673 buf->b_sfname = vim_strsave(sfname);
1674 }
1675
1676 clear_wininfo(buf);
1677 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1678
1679 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1680 || buf->b_wininfo == NULL)
1681 {
1682 vim_free(buf->b_ffname);
1683 buf->b_ffname = NULL;
1684 vim_free(buf->b_sfname);
1685 buf->b_sfname = NULL;
1686 if (buf != curbuf)
1687 free_buffer(buf);
1688 return NULL;
1689 }
1690
1691 if (buf == curbuf)
1692 {
1693 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001694 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695 if (buf != curbuf) /* autocommands deleted the buffer! */
1696 return NULL;
1697#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001698 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 return NULL;
1700#endif
1701 /* buf->b_nwindows = 0; why was this here? */
1702 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1703#ifdef FEAT_KEYMAP
1704 /* need to reload lmaps and set b:keymap_name */
1705 curbuf->b_kmap_state |= KEYMAP_INIT;
1706#endif
1707 }
1708 else
1709 {
1710 /*
1711 * put new buffer at the end of the buffer list
1712 */
1713 buf->b_next = NULL;
1714 if (firstbuf == NULL) /* buffer list is empty */
1715 {
1716 buf->b_prev = NULL;
1717 firstbuf = buf;
1718 }
1719 else /* append new buffer at end of list */
1720 {
1721 lastbuf->b_next = buf;
1722 buf->b_prev = lastbuf;
1723 }
1724 lastbuf = buf;
1725
1726 buf->b_fnum = top_file_num++;
1727 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1728 {
1729 EMSG(_("W14: Warning: List of file names overflow"));
1730 if (emsg_silent == 0)
1731 {
1732 out_flush();
1733 ui_delay(3000L, TRUE); /* make sure it is noticed */
1734 }
1735 top_file_num = 1;
1736 }
1737
1738 /*
1739 * Always copy the options from the current buffer.
1740 */
1741 buf_copy_options(buf, BCO_ALWAYS);
1742 }
1743
1744 buf->b_wininfo->wi_fpos.lnum = lnum;
1745 buf->b_wininfo->wi_win = curwin;
1746
1747#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001748 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1749#endif
1750#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001751 hash_init(&buf->b_s.b_keywtab);
1752 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753#endif
1754
1755 buf->b_fname = buf->b_sfname;
1756#ifdef UNIX
1757 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001758 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 else
1760 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001761 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 buf->b_dev = st.st_dev;
1763 buf->b_ino = st.st_ino;
1764 }
1765#endif
1766 buf->b_u_synced = TRUE;
1767 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001768 if (flags & BLN_DUMMY)
1769 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 buf_clear_file(buf);
1771 clrallmarks(buf); /* clear marks */
1772 fmarks_check_names(buf); /* check file marks for this file */
1773 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1774#ifdef FEAT_AUTOCMD
1775 if (!(flags & BLN_DUMMY))
1776 {
1777 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1778 if (flags & BLN_LISTED)
1779 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1780# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001781 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 return NULL;
1783# endif
1784 }
1785#endif
1786
1787 return buf;
1788}
1789
1790/*
1791 * Free the memory for the options of a buffer.
1792 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1793 * 'fileencoding'.
1794 */
1795 void
1796free_buf_options(buf, free_p_ff)
1797 buf_T *buf;
1798 int free_p_ff;
1799{
1800 if (free_p_ff)
1801 {
1802#ifdef FEAT_MBYTE
1803 clear_string_option(&buf->b_p_fenc);
1804#endif
1805 clear_string_option(&buf->b_p_ff);
1806#ifdef FEAT_QUICKFIX
1807 clear_string_option(&buf->b_p_bh);
1808 clear_string_option(&buf->b_p_bt);
1809#endif
1810 }
1811#ifdef FEAT_FIND_ID
1812 clear_string_option(&buf->b_p_def);
1813 clear_string_option(&buf->b_p_inc);
1814# ifdef FEAT_EVAL
1815 clear_string_option(&buf->b_p_inex);
1816# endif
1817#endif
1818#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1819 clear_string_option(&buf->b_p_inde);
1820 clear_string_option(&buf->b_p_indk);
1821#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001822#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1823 clear_string_option(&buf->b_p_bexpr);
1824#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001825#if defined(FEAT_CRYPT)
1826 clear_string_option(&buf->b_p_cm);
1827#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001828#if defined(FEAT_EVAL)
1829 clear_string_option(&buf->b_p_fex);
1830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831#ifdef FEAT_CRYPT
1832 clear_string_option(&buf->b_p_key);
1833#endif
1834 clear_string_option(&buf->b_p_kp);
1835 clear_string_option(&buf->b_p_mps);
1836 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001837 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 clear_string_option(&buf->b_p_isk);
1839#ifdef FEAT_KEYMAP
1840 clear_string_option(&buf->b_p_keymap);
1841 ga_clear(&buf->b_kmap_ga);
1842#endif
1843#ifdef FEAT_COMMENTS
1844 clear_string_option(&buf->b_p_com);
1845#endif
1846#ifdef FEAT_FOLDING
1847 clear_string_option(&buf->b_p_cms);
1848#endif
1849 clear_string_option(&buf->b_p_nf);
1850#ifdef FEAT_SYN_HL
1851 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001852#endif
1853#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001854 clear_string_option(&buf->b_s.b_p_spc);
1855 clear_string_option(&buf->b_s.b_p_spf);
1856 vim_free(buf->b_s.b_cap_prog);
1857 buf->b_s.b_cap_prog = NULL;
1858 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859#endif
1860#ifdef FEAT_SEARCHPATH
1861 clear_string_option(&buf->b_p_sua);
1862#endif
1863#ifdef FEAT_AUTOCMD
1864 clear_string_option(&buf->b_p_ft);
1865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866#ifdef FEAT_CINDENT
1867 clear_string_option(&buf->b_p_cink);
1868 clear_string_option(&buf->b_p_cino);
1869#endif
1870#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1871 clear_string_option(&buf->b_p_cinw);
1872#endif
1873#ifdef FEAT_INS_EXPAND
1874 clear_string_option(&buf->b_p_cpt);
1875#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001876#ifdef FEAT_COMPL_FUNC
1877 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001878 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001879#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880#ifdef FEAT_QUICKFIX
1881 clear_string_option(&buf->b_p_gp);
1882 clear_string_option(&buf->b_p_mp);
1883 clear_string_option(&buf->b_p_efm);
1884#endif
1885 clear_string_option(&buf->b_p_ep);
1886 clear_string_option(&buf->b_p_path);
1887 clear_string_option(&buf->b_p_tags);
1888#ifdef FEAT_INS_EXPAND
1889 clear_string_option(&buf->b_p_dict);
1890 clear_string_option(&buf->b_p_tsr);
1891#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001892#ifdef FEAT_TEXTOBJ
1893 clear_string_option(&buf->b_p_qe);
1894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 buf->b_p_ar = -1;
1896}
1897
1898/*
1899 * get alternate file n
1900 * set linenr to lnum or altfpos.lnum if lnum == 0
1901 * also set cursor column to altfpos.col if 'startofline' is not set.
1902 * if (options & GETF_SETMARK) call setpcmark()
1903 * if (options & GETF_ALT) we are jumping to an alternate file.
1904 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1905 *
1906 * return FAIL for failure, OK for success
1907 */
1908 int
1909buflist_getfile(n, lnum, options, forceit)
1910 int n;
1911 linenr_T lnum;
1912 int options;
1913 int forceit;
1914{
1915 buf_T *buf;
1916#ifdef FEAT_WINDOWS
1917 win_T *wp = NULL;
1918#endif
1919 pos_T *fpos;
1920 colnr_T col;
1921
1922 buf = buflist_findnr(n);
1923 if (buf == NULL)
1924 {
1925 if ((options & GETF_ALT) && n == 0)
1926 EMSG(_(e_noalt));
1927 else
1928 EMSGN(_("E92: Buffer %ld not found"), n);
1929 return FAIL;
1930 }
1931
1932 /* if alternate file is the current buffer, nothing to do */
1933 if (buf == curbuf)
1934 return OK;
1935
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001936 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001937 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001938 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001939 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001940 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001941#ifdef FEAT_AUTOCMD
1942 if (curbuf_locked())
1943 return FAIL;
1944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945
1946 /* altfpos may be changed by getfile(), get it now */
1947 if (lnum == 0)
1948 {
1949 fpos = buflist_findfpos(buf);
1950 lnum = fpos->lnum;
1951 col = fpos->col;
1952 }
1953 else
1954 col = 0;
1955
1956#ifdef FEAT_WINDOWS
1957 if (options & GETF_SWITCH)
1958 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001959 /* If 'switchbuf' contains "useopen": jump to first window containing
1960 * "buf" if one exists */
1961 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001963 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1964 * page containing "buf" if one exists */
1965 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001966 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001967 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1968 * isn't empty: open new window */
1969 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001971 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1972 tabpage_new();
1973 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001975 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 }
1978#endif
1979
1980 ++RedrawingDisabled;
1981 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1982 lnum, forceit) <= 0)
1983 {
1984 --RedrawingDisabled;
1985
1986 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1987 if (!p_sol && col != 0)
1988 {
1989 curwin->w_cursor.col = col;
1990 check_cursor_col();
1991#ifdef FEAT_VIRTUALEDIT
1992 curwin->w_cursor.coladd = 0;
1993#endif
1994 curwin->w_set_curswant = TRUE;
1995 }
1996 return OK;
1997 }
1998 --RedrawingDisabled;
1999 return FAIL;
2000}
2001
2002/*
2003 * go to the last know line number for the current buffer
2004 */
2005 void
2006buflist_getfpos()
2007{
2008 pos_T *fpos;
2009
2010 fpos = buflist_findfpos(curbuf);
2011
2012 curwin->w_cursor.lnum = fpos->lnum;
2013 check_cursor_lnum();
2014
2015 if (p_sol)
2016 curwin->w_cursor.col = 0;
2017 else
2018 {
2019 curwin->w_cursor.col = fpos->col;
2020 check_cursor_col();
2021#ifdef FEAT_VIRTUALEDIT
2022 curwin->w_cursor.coladd = 0;
2023#endif
2024 curwin->w_set_curswant = TRUE;
2025 }
2026}
2027
Bram Moolenaar81695252004-12-29 20:58:21 +00002028#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2029/*
2030 * Find file in buffer list by name (it has to be for the current window).
2031 * Returns NULL if not found.
2032 */
2033 buf_T *
2034buflist_findname_exp(fname)
2035 char_u *fname;
2036{
2037 char_u *ffname;
2038 buf_T *buf = NULL;
2039
2040 /* First make the name into a full path name */
2041 ffname = FullName_save(fname,
2042#ifdef UNIX
2043 TRUE /* force expansion, get rid of symbolic links */
2044#else
2045 FALSE
2046#endif
2047 );
2048 if (ffname != NULL)
2049 {
2050 buf = buflist_findname(ffname);
2051 vim_free(ffname);
2052 }
2053 return buf;
2054}
2055#endif
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057/*
2058 * Find file in buffer list by name (it has to be for the current window).
2059 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002060 * Skips dummy buffers.
2061 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 */
2063 buf_T *
2064buflist_findname(ffname)
2065 char_u *ffname;
2066{
2067#ifdef UNIX
2068 struct stat st;
2069
2070 if (mch_stat((char *)ffname, &st) < 0)
2071 st.st_dev = (dev_T)-1;
2072 return buflist_findname_stat(ffname, &st);
2073}
2074
2075/*
2076 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2077 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002078 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 */
2080 static buf_T *
2081buflist_findname_stat(ffname, stp)
2082 char_u *ffname;
2083 struct stat *stp;
2084{
2085#endif
2086 buf_T *buf;
2087
2088 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002089 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090#ifdef UNIX
2091 , stp
2092#endif
2093 ))
2094 return buf;
2095 return NULL;
2096}
2097
2098#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2099/*
2100 * Find file in buffer list by a regexp pattern.
2101 * Return fnum of the found buffer.
2102 * Return < 0 for error.
2103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 int
2105buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2106 char_u *pattern;
2107 char_u *pattern_end; /* pointer to first char after pattern */
2108 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002109 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 buf_T *buf;
2112 regprog_T *prog;
2113 int match = -1;
2114 int find_listed;
2115 char_u *pat;
2116 char_u *patend;
2117 int attempt;
2118 char_u *p;
2119 int toggledollar;
2120
2121 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2122 {
2123 if (*pattern == '%')
2124 match = curbuf->b_fnum;
2125 else
2126 match = curwin->w_alt_fnum;
2127#ifdef FEAT_DIFF
2128 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2129 match = -1;
2130#endif
2131 }
2132
2133 /*
2134 * Try four ways of matching a listed buffer:
2135 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002136 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 * attempt == 2: with '$' at end (only match at end)
2138 * attempt == 3: with '^' at start and '$' at end (only full match)
2139 * Repeat this for finding an unlisted buffer if there was no matching
2140 * listed buffer.
2141 */
2142 else
2143 {
2144 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2145 if (pat == NULL)
2146 return -1;
2147 patend = pat + STRLEN(pat) - 1;
2148 toggledollar = (patend > pat && *patend == '$');
2149
2150 /* First try finding a listed buffer. If not found and "unlisted"
2151 * is TRUE, try finding an unlisted buffer. */
2152 find_listed = TRUE;
2153 for (;;)
2154 {
2155 for (attempt = 0; attempt <= 3; ++attempt)
2156 {
2157 /* may add '^' and '$' */
2158 if (toggledollar)
2159 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2160 p = pat;
2161 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2162 ++p;
2163 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2164 if (prog == NULL)
2165 {
2166 vim_free(pat);
2167 return -1;
2168 }
2169
2170 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2171 if (buf->b_p_bl == find_listed
2172#ifdef FEAT_DIFF
2173 && (!diffmode || diff_mode_buf(buf))
2174#endif
2175 && buflist_match(prog, buf) != NULL)
2176 {
2177 if (match >= 0) /* already found a match */
2178 {
2179 match = -2;
2180 break;
2181 }
2182 match = buf->b_fnum; /* remember first match */
2183 }
2184
2185 vim_free(prog);
2186 if (match >= 0) /* found one match */
2187 break;
2188 }
2189
2190 /* Only search for unlisted buffers if there was no match with
2191 * a listed buffer. */
2192 if (!unlisted || !find_listed || match != -1)
2193 break;
2194 find_listed = FALSE;
2195 }
2196
2197 vim_free(pat);
2198 }
2199
2200 if (match == -2)
2201 EMSG2(_("E93: More than one match for %s"), pattern);
2202 else if (match < 0)
2203 EMSG2(_("E94: No matching buffer for %s"), pattern);
2204 return match;
2205}
2206#endif
2207
2208#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2209
2210/*
2211 * Find all buffer names that match.
2212 * For command line expansion of ":buf" and ":sbuf".
2213 * Return OK if matches found, FAIL otherwise.
2214 */
2215 int
2216ExpandBufnames(pat, num_file, file, options)
2217 char_u *pat;
2218 int *num_file;
2219 char_u ***file;
2220 int options;
2221{
2222 int count = 0;
2223 buf_T *buf;
2224 int round;
2225 char_u *p;
2226 int attempt;
2227 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002228 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229
2230 *num_file = 0; /* return values in case of FAIL */
2231 *file = NULL;
2232
Bram Moolenaar05159a02005-02-26 23:04:13 +00002233 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2234 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002236 patc = alloc((unsigned)STRLEN(pat) + 11);
2237 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002239 STRCPY(patc, "\\(^\\|[\\/]\\)");
2240 STRCPY(patc + 11, pat + 1);
2241 }
2242 else
2243 patc = pat;
2244
2245 /*
2246 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002247 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002248 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002249 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002250 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002251 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002252 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002253 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002254 if (prog == NULL)
2255 {
2256 if (patc != pat)
2257 vim_free(patc);
2258 return FAIL;
2259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260
2261 /*
2262 * round == 1: Count the matches.
2263 * round == 2: Build the array to keep the matches.
2264 */
2265 for (round = 1; round <= 2; ++round)
2266 {
2267 count = 0;
2268 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2269 {
2270 if (!buf->b_p_bl) /* skip unlisted buffers */
2271 continue;
2272 p = buflist_match(prog, buf);
2273 if (p != NULL)
2274 {
2275 if (round == 1)
2276 ++count;
2277 else
2278 {
2279 if (options & WILD_HOME_REPLACE)
2280 p = home_replace_save(buf, p);
2281 else
2282 p = vim_strsave(p);
2283 (*file)[count++] = p;
2284 }
2285 }
2286 }
2287 if (count == 0) /* no match found, break here */
2288 break;
2289 if (round == 1)
2290 {
2291 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2292 if (*file == NULL)
2293 {
2294 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002295 if (patc != pat)
2296 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 return FAIL;
2298 }
2299 }
2300 }
2301 vim_free(prog);
2302 if (count) /* match(es) found, break here */
2303 break;
2304 }
2305
Bram Moolenaar05159a02005-02-26 23:04:13 +00002306 if (patc != pat)
2307 vim_free(patc);
2308
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 *num_file = count;
2310 return (count == 0 ? FAIL : OK);
2311}
2312
2313#endif /* FEAT_CMDL_COMPL */
2314
2315#ifdef HAVE_BUFLIST_MATCH
2316/*
2317 * Check for a match on the file name for buffer "buf" with regprog "prog".
2318 */
2319 static char_u *
2320buflist_match(prog, buf)
2321 regprog_T *prog;
2322 buf_T *buf;
2323{
2324 char_u *match;
2325
2326 /* First try the short file name, then the long file name. */
2327 match = fname_match(prog, buf->b_sfname);
2328 if (match == NULL)
2329 match = fname_match(prog, buf->b_ffname);
2330
2331 return match;
2332}
2333
2334/*
2335 * Try matching the regexp in "prog" with file name "name".
2336 * Return "name" when there is a match, NULL when not.
2337 */
2338 static char_u *
2339fname_match(prog, name)
2340 regprog_T *prog;
2341 char_u *name;
2342{
2343 char_u *match = NULL;
2344 char_u *p;
2345 regmatch_T regmatch;
2346
2347 if (name != NULL)
2348 {
2349 regmatch.regprog = prog;
2350#ifdef CASE_INSENSITIVE_FILENAME
2351 regmatch.rm_ic = TRUE; /* Always ignore case */
2352#else
2353 regmatch.rm_ic = FALSE; /* Never ignore case */
2354#endif
2355
2356 if (vim_regexec(&regmatch, name, (colnr_T)0))
2357 match = name;
2358 else
2359 {
2360 /* Replace $(HOME) with '~' and try matching again. */
2361 p = home_replace_save(NULL, name);
2362 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2363 match = name;
2364 vim_free(p);
2365 }
2366 }
2367
2368 return match;
2369}
2370#endif
2371
2372/*
2373 * find file in buffer list by number
2374 */
2375 buf_T *
2376buflist_findnr(nr)
2377 int nr;
2378{
2379 buf_T *buf;
2380
2381 if (nr == 0)
2382 nr = curwin->w_alt_fnum;
2383 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2384 if (buf->b_fnum == nr)
2385 return (buf);
2386 return NULL;
2387}
2388
2389/*
2390 * Get name of file 'n' in the buffer list.
2391 * When the file has no name an empty string is returned.
2392 * home_replace() is used to shorten the file name (used for marks).
2393 * Returns a pointer to allocated memory, of NULL when failed.
2394 */
2395 char_u *
2396buflist_nr2name(n, fullname, helptail)
2397 int n;
2398 int fullname;
2399 int helptail; /* for help buffers return tail only */
2400{
2401 buf_T *buf;
2402
2403 buf = buflist_findnr(n);
2404 if (buf == NULL)
2405 return NULL;
2406 return home_replace_save(helptail ? buf : NULL,
2407 fullname ? buf->b_ffname : buf->b_fname);
2408}
2409
2410/*
2411 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2412 * When "copy_options" is TRUE save the local window option values.
2413 * When "lnum" is 0 only do the options.
2414 */
2415 static void
2416buflist_setfpos(buf, win, lnum, col, copy_options)
2417 buf_T *buf;
2418 win_T *win;
2419 linenr_T lnum;
2420 colnr_T col;
2421 int copy_options;
2422{
2423 wininfo_T *wip;
2424
2425 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2426 if (wip->wi_win == win)
2427 break;
2428 if (wip == NULL)
2429 {
2430 /* allocate a new entry */
2431 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2432 if (wip == NULL)
2433 return;
2434 wip->wi_win = win;
2435 if (lnum == 0) /* set lnum even when it's 0 */
2436 lnum = 1;
2437 }
2438 else
2439 {
2440 /* remove the entry from the list */
2441 if (wip->wi_prev)
2442 wip->wi_prev->wi_next = wip->wi_next;
2443 else
2444 buf->b_wininfo = wip->wi_next;
2445 if (wip->wi_next)
2446 wip->wi_next->wi_prev = wip->wi_prev;
2447 if (copy_options && wip->wi_optset)
2448 {
2449 clear_winopt(&wip->wi_opt);
2450#ifdef FEAT_FOLDING
2451 deleteFoldRecurse(&wip->wi_folds);
2452#endif
2453 }
2454 }
2455 if (lnum != 0)
2456 {
2457 wip->wi_fpos.lnum = lnum;
2458 wip->wi_fpos.col = col;
2459 }
2460 if (copy_options)
2461 {
2462 /* Save the window-specific option values. */
2463 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2464#ifdef FEAT_FOLDING
2465 wip->wi_fold_manual = win->w_fold_manual;
2466 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2467#endif
2468 wip->wi_optset = TRUE;
2469 }
2470
2471 /* insert the entry in front of the list */
2472 wip->wi_next = buf->b_wininfo;
2473 buf->b_wininfo = wip;
2474 wip->wi_prev = NULL;
2475 if (wip->wi_next)
2476 wip->wi_next->wi_prev = wip;
2477
2478 return;
2479}
2480
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002481#ifdef FEAT_DIFF
2482static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2483
2484/*
2485 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2486 * page. That's because a diff is local to a tab page.
2487 */
2488 static int
2489wininfo_other_tab_diff(wip)
2490 wininfo_T *wip;
2491{
2492 win_T *wp;
2493
2494 if (wip->wi_opt.wo_diff)
2495 {
2496 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2497 /* return FALSE when it's a window in the current tab page, thus
2498 * the buffer was in diff mode here */
2499 if (wip->wi_win == wp)
2500 return FALSE;
2501 return TRUE;
2502 }
2503 return FALSE;
2504}
2505#endif
2506
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507/*
2508 * Find info for the current window in buffer "buf".
2509 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002510 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2511 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 * Returns NULL when there isn't any info.
2513 */
2514 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002515find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002517 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518{
2519 wininfo_T *wip;
2520
2521 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002522 if (wip->wi_win == curwin
2523#ifdef FEAT_DIFF
2524 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2525#endif
2526 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002528
2529 /* If no wininfo for curwin, use the first in the list (that doesn't have
2530 * 'diff' set and is in another tab page). */
2531 if (wip == NULL)
2532 {
2533#ifdef FEAT_DIFF
2534 if (skip_diff_buffer)
2535 {
2536 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2537 if (!wininfo_other_tab_diff(wip))
2538 break;
2539 }
2540 else
2541#endif
2542 wip = buf->b_wininfo;
2543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 return wip;
2545}
2546
2547/*
2548 * Reset the local window options to the values last used in this window.
2549 * If the buffer wasn't used in this window before, use the values from
2550 * the most recently used window. If the values were never set, use the
2551 * global values for the window.
2552 */
2553 void
2554get_winopts(buf)
2555 buf_T *buf;
2556{
2557 wininfo_T *wip;
2558
2559 clear_winopt(&curwin->w_onebuf_opt);
2560#ifdef FEAT_FOLDING
2561 clearFolding(curwin);
2562#endif
2563
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002564 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 if (wip != NULL && wip->wi_optset)
2566 {
2567 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2568#ifdef FEAT_FOLDING
2569 curwin->w_fold_manual = wip->wi_fold_manual;
2570 curwin->w_foldinvalid = TRUE;
2571 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2572#endif
2573 }
2574 else
2575 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2576
2577#ifdef FEAT_FOLDING
2578 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2579 if (p_fdls >= 0)
2580 curwin->w_p_fdl = p_fdls;
2581#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002582#ifdef FEAT_SYN_HL
2583 check_colorcolumn(curwin);
2584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585}
2586
2587/*
2588 * Find the position (lnum and col) for the buffer 'buf' for the current
2589 * window.
2590 * Returns a pointer to no_position if no position is found.
2591 */
2592 pos_T *
2593buflist_findfpos(buf)
2594 buf_T *buf;
2595{
2596 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002597 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002599 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 if (wip != NULL)
2601 return &(wip->wi_fpos);
2602 else
2603 return &no_position;
2604}
2605
2606/*
2607 * Find the lnum for the buffer 'buf' for the current window.
2608 */
2609 linenr_T
2610buflist_findlnum(buf)
2611 buf_T *buf;
2612{
2613 return buflist_findfpos(buf)->lnum;
2614}
2615
2616#if defined(FEAT_LISTCMDS) || defined(PROTO)
2617/*
2618 * List all know file names (for :files and :buffers command).
2619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 void
2621buflist_list(eap)
2622 exarg_T *eap;
2623{
2624 buf_T *buf;
2625 int len;
2626 int i;
2627
2628 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2629 {
2630 /* skip unlisted buffers, unless ! was used */
2631 if (!buf->b_p_bl && !eap->forceit)
2632 continue;
2633 msg_putchar('\n');
2634 if (buf_spname(buf) != NULL)
2635 STRCPY(NameBuff, buf_spname(buf));
2636 else
2637 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2638
Bram Moolenaar50cde822005-06-05 21:54:54 +00002639 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002640 buf->b_fnum,
2641 buf->b_p_bl ? ' ' : 'u',
2642 buf == curbuf ? '%' :
2643 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2644 buf->b_ml.ml_mfp == NULL ? ' ' :
2645 (buf->b_nwindows == 0 ? 'h' : 'a'),
2646 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2647 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002648 : (bufIsChanged(buf) ? '+' : ' '),
2649 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650
2651 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 i = 40 - vim_strsize(IObuff);
2653 do
2654 {
2655 IObuff[len++] = ' ';
2656 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002657 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2658 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002659 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 msg_outtrans(IObuff);
2661 out_flush(); /* output one line at a time */
2662 ui_breakcheck();
2663 }
2664}
2665#endif
2666
2667/*
2668 * Get file name and line number for file 'fnum'.
2669 * Used by DoOneCmd() for translating '%' and '#'.
2670 * Used by insert_reg() and cmdline_paste() for '#' register.
2671 * Return FAIL if not found, OK for success.
2672 */
2673 int
2674buflist_name_nr(fnum, fname, lnum)
2675 int fnum;
2676 char_u **fname;
2677 linenr_T *lnum;
2678{
2679 buf_T *buf;
2680
2681 buf = buflist_findnr(fnum);
2682 if (buf == NULL || buf->b_fname == NULL)
2683 return FAIL;
2684
2685 *fname = buf->b_fname;
2686 *lnum = buflist_findlnum(buf);
2687
2688 return OK;
2689}
2690
2691/*
2692 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2693 * The file name with the full path is also remembered, for when :cd is used.
2694 * Returns FAIL for failure (file name already in use by other buffer)
2695 * OK otherwise.
2696 */
2697 int
2698setfname(buf, ffname, sfname, message)
2699 buf_T *buf;
2700 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002701 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
Bram Moolenaar81695252004-12-29 20:58:21 +00002703 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704#ifdef UNIX
2705 struct stat st;
2706#endif
2707
2708 if (ffname == NULL || *ffname == NUL)
2709 {
2710 /* Removing the name. */
2711 vim_free(buf->b_ffname);
2712 vim_free(buf->b_sfname);
2713 buf->b_ffname = NULL;
2714 buf->b_sfname = NULL;
2715#ifdef UNIX
2716 st.st_dev = (dev_T)-1;
2717#endif
2718 }
2719 else
2720 {
2721 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2722 if (ffname == NULL) /* out of memory */
2723 return FAIL;
2724
2725 /*
2726 * if the file name is already used in another buffer:
2727 * - if the buffer is loaded, fail
2728 * - if the buffer is not loaded, delete it from the list
2729 */
2730#ifdef UNIX
2731 if (mch_stat((char *)ffname, &st) < 0)
2732 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002733#endif
2734 if (!(buf->b_flags & BF_DUMMY))
2735#ifdef UNIX
2736 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002738 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739#endif
2740 if (obuf != NULL && obuf != buf)
2741 {
2742 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2743 {
2744 if (message)
2745 EMSG(_("E95: Buffer with this name already exists"));
2746 vim_free(ffname);
2747 return FAIL;
2748 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002749 /* delete from the list */
2750 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 }
2752 sfname = vim_strsave(sfname);
2753 if (ffname == NULL || sfname == NULL)
2754 {
2755 vim_free(sfname);
2756 vim_free(ffname);
2757 return FAIL;
2758 }
2759#ifdef USE_FNAME_CASE
2760# ifdef USE_LONG_FNAME
2761 if (USE_LONG_FNAME)
2762# endif
2763 fname_case(sfname, 0); /* set correct case for short file name */
2764#endif
2765 vim_free(buf->b_ffname);
2766 vim_free(buf->b_sfname);
2767 buf->b_ffname = ffname;
2768 buf->b_sfname = sfname;
2769 }
2770 buf->b_fname = buf->b_sfname;
2771#ifdef UNIX
2772 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002773 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 else
2775 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002776 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 buf->b_dev = st.st_dev;
2778 buf->b_ino = st.st_ino;
2779 }
2780#endif
2781
2782#ifndef SHORT_FNAME
2783 buf->b_shortname = FALSE;
2784#endif
2785
2786 buf_name_changed(buf);
2787 return OK;
2788}
2789
2790/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002791 * Crude way of changing the name of a buffer. Use with care!
2792 * The name should be relative to the current directory.
2793 */
2794 void
2795buf_set_name(fnum, name)
2796 int fnum;
2797 char_u *name;
2798{
2799 buf_T *buf;
2800
2801 buf = buflist_findnr(fnum);
2802 if (buf != NULL)
2803 {
2804 vim_free(buf->b_sfname);
2805 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002806 buf->b_ffname = vim_strsave(name);
2807 buf->b_sfname = NULL;
2808 /* Allocate ffname and expand into full path. Also resolves .lnk
2809 * files on Win32. */
2810 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002811 buf->b_fname = buf->b_sfname;
2812 }
2813}
2814
2815/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 * Take care of what needs to be done when the name of buffer "buf" has
2817 * changed.
2818 */
2819 void
2820buf_name_changed(buf)
2821 buf_T *buf;
2822{
2823 /*
2824 * If the file name changed, also change the name of the swapfile
2825 */
2826 if (buf->b_ml.ml_mfp != NULL)
2827 ml_setname(buf);
2828
2829 if (curwin->w_buffer == buf)
2830 check_arg_idx(curwin); /* check file name for arg list */
2831#ifdef FEAT_TITLE
2832 maketitle(); /* set window title */
2833#endif
2834#ifdef FEAT_WINDOWS
2835 status_redraw_all(); /* status lines need to be redrawn */
2836#endif
2837 fmarks_check_names(buf); /* check named file marks */
2838 ml_timestamp(buf); /* reset timestamp */
2839}
2840
2841/*
2842 * set alternate file name for current window
2843 *
2844 * Used by do_one_cmd(), do_write() and do_ecmd().
2845 * Return the buffer.
2846 */
2847 buf_T *
2848setaltfname(ffname, sfname, lnum)
2849 char_u *ffname;
2850 char_u *sfname;
2851 linenr_T lnum;
2852{
2853 buf_T *buf;
2854
2855 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2856 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002857 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 curwin->w_alt_fnum = buf->b_fnum;
2859 return buf;
2860}
2861
2862/*
2863 * Get alternate file name for current window.
2864 * Return NULL if there isn't any, and give error message if requested.
2865 */
2866 char_u *
2867getaltfname(errmsg)
2868 int errmsg; /* give error message */
2869{
2870 char_u *fname;
2871 linenr_T dummy;
2872
2873 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2874 {
2875 if (errmsg)
2876 EMSG(_(e_noalt));
2877 return NULL;
2878 }
2879 return fname;
2880}
2881
2882/*
2883 * Add a file name to the buflist and return its number.
2884 * Uses same flags as buflist_new(), except BLN_DUMMY.
2885 *
2886 * used by qf_init(), main() and doarglist()
2887 */
2888 int
2889buflist_add(fname, flags)
2890 char_u *fname;
2891 int flags;
2892{
2893 buf_T *buf;
2894
2895 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2896 if (buf != NULL)
2897 return buf->b_fnum;
2898 return 0;
2899}
2900
2901#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2902/*
2903 * Adjust slashes in file names. Called after 'shellslash' was set.
2904 */
2905 void
2906buflist_slash_adjust()
2907{
2908 buf_T *bp;
2909
2910 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2911 {
2912 if (bp->b_ffname != NULL)
2913 slash_adjust(bp->b_ffname);
2914 if (bp->b_sfname != NULL)
2915 slash_adjust(bp->b_sfname);
2916 }
2917}
2918#endif
2919
2920/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002921 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002922 * Also save the local window option values.
2923 */
2924 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002925buflist_altfpos(win)
2926 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002928 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929}
2930
2931/*
2932 * Return TRUE if 'ffname' is not the same file as current file.
2933 * Fname must have a full path (expanded by mch_FullName()).
2934 */
2935 int
2936otherfile(ffname)
2937 char_u *ffname;
2938{
2939 return otherfile_buf(curbuf, ffname
2940#ifdef UNIX
2941 , NULL
2942#endif
2943 );
2944}
2945
2946 static int
2947otherfile_buf(buf, ffname
2948#ifdef UNIX
2949 , stp
2950#endif
2951 )
2952 buf_T *buf;
2953 char_u *ffname;
2954#ifdef UNIX
2955 struct stat *stp;
2956#endif
2957{
2958 /* no name is different */
2959 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2960 return TRUE;
2961 if (fnamecmp(ffname, buf->b_ffname) == 0)
2962 return FALSE;
2963#ifdef UNIX
2964 {
2965 struct stat st;
2966
2967 /* If no struct stat given, get it now */
2968 if (stp == NULL)
2969 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002970 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 st.st_dev = (dev_T)-1;
2972 stp = &st;
2973 }
2974 /* Use dev/ino to check if the files are the same, even when the names
2975 * are different (possible with links). Still need to compare the
2976 * name above, for when the file doesn't exist yet.
2977 * Problem: The dev/ino changes when a file is deleted (and created
2978 * again) and remains the same when renamed/moved. We don't want to
2979 * mch_stat() each buffer each time, that would be too slow. Get the
2980 * dev/ino again when they appear to match, but not when they appear
2981 * to be different: Could skip a buffer when it's actually the same
2982 * file. */
2983 if (buf_same_ino(buf, stp))
2984 {
2985 buf_setino(buf);
2986 if (buf_same_ino(buf, stp))
2987 return FALSE;
2988 }
2989 }
2990#endif
2991 return TRUE;
2992}
2993
2994#if defined(UNIX) || defined(PROTO)
2995/*
2996 * Set inode and device number for a buffer.
2997 * Must always be called when b_fname is changed!.
2998 */
2999 void
3000buf_setino(buf)
3001 buf_T *buf;
3002{
3003 struct stat st;
3004
3005 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
3006 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003007 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 buf->b_dev = st.st_dev;
3009 buf->b_ino = st.st_ino;
3010 }
3011 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003012 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013}
3014
3015/*
3016 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3017 */
3018 static int
3019buf_same_ino(buf, stp)
3020 buf_T *buf;
3021 struct stat *stp;
3022{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003023 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 && stp->st_dev == buf->b_dev
3025 && stp->st_ino == buf->b_ino);
3026}
3027#endif
3028
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003029/*
3030 * Print info about the current buffer.
3031 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 void
3033fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003034 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 int shorthelp;
3036 int dont_truncate;
3037{
3038 char_u *name;
3039 int n;
3040 char_u *p;
3041 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003042 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043
3044 buffer = alloc(IOSIZE);
3045 if (buffer == NULL)
3046 return;
3047
3048 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3049 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003050 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 p = buffer + STRLEN(buffer);
3052 }
3053 else
3054 p = buffer;
3055
3056 *p++ = '"';
3057 if (buf_spname(curbuf) != NULL)
3058 STRCPY(p, buf_spname(curbuf));
3059 else
3060 {
3061 if (!fullname && curbuf->b_fname != NULL)
3062 name = curbuf->b_fname;
3063 else
3064 name = curbuf->b_ffname;
3065 home_replace(shorthelp ? curbuf : NULL, name, p,
3066 (int)(IOSIZE - (p - buffer)), TRUE);
3067 }
3068
Bram Moolenaara800b422010-06-27 01:15:55 +02003069 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003070 curbufIsChanged() ? (shortmess(SHM_MOD)
3071 ? " [+]" : _(" [Modified]")) : " ",
3072 (curbuf->b_flags & BF_NOTEDITED)
3073#ifdef FEAT_QUICKFIX
3074 && !bt_dontwrite(curbuf)
3075#endif
3076 ? _("[Not edited]") : "",
3077 (curbuf->b_flags & BF_NEW)
3078#ifdef FEAT_QUICKFIX
3079 && !bt_dontwrite(curbuf)
3080#endif
3081 ? _("[New file]") : "",
3082 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3083 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3084 : _("[readonly]")) : "",
3085 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3086 || curbuf->b_p_ro) ?
3087 " " : "");
3088 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3089 * causes an overflow, thus for large numbers divide instead. */
3090 if (curwin->w_cursor.lnum > 1000000L)
3091 n = (int)(((long)curwin->w_cursor.lnum) /
3092 ((long)curbuf->b_ml.ml_line_count / 100L));
3093 else
3094 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3095 (long)curbuf->b_ml.ml_line_count);
3096 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3097 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003098 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 }
3100#ifdef FEAT_CMDL_INFO
3101 else if (p_ru)
3102 {
3103 /* Current line and column are already on the screen -- webb */
3104 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003105 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003107 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 (long)curbuf->b_ml.ml_line_count, n);
3109 }
3110#endif
3111 else
3112 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003113 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 _("line %ld of %ld --%d%%-- col "),
3115 (long)curwin->w_cursor.lnum,
3116 (long)curbuf->b_ml.ml_line_count,
3117 n);
3118 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003119 len = STRLEN(buffer);
3120 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3122 }
3123
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003124 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003125
3126 if (dont_truncate)
3127 {
3128 /* Temporarily set msg_scroll to avoid the message being truncated.
3129 * First call msg_start() to get the message in the right place. */
3130 msg_start();
3131 n = msg_scroll;
3132 msg_scroll = TRUE;
3133 msg(buffer);
3134 msg_scroll = n;
3135 }
3136 else
3137 {
3138 p = msg_trunc_attr(buffer, FALSE, 0);
3139 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 /* Need to repeat the message after redrawing when:
3141 * - When restart_edit is set (otherwise there will be a delay
3142 * before redrawing).
3143 * - When the screen was scrolled but there is no wait-return
3144 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003145 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 }
3147
3148 vim_free(buffer);
3149}
3150
3151 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003152col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003154 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 int col;
3156 int vcol;
3157{
3158 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003159 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003161 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162}
3163
3164#if defined(FEAT_TITLE) || defined(PROTO)
3165/*
3166 * put file name in title bar of window and in icon title
3167 */
3168
3169static char_u *lasttitle = NULL;
3170static char_u *lasticon = NULL;
3171
3172 void
3173maketitle()
3174{
3175 char_u *p;
3176 char_u *t_str = NULL;
3177 char_u *i_name;
3178 char_u *i_str = NULL;
3179 int maxlen = 0;
3180 int len;
3181 int mustset;
3182 char_u buf[IOSIZE];
3183 int off;
3184
3185 if (!redrawing())
3186 {
3187 /* Postpone updating the title when 'lazyredraw' is set. */
3188 need_maketitle = TRUE;
3189 return;
3190 }
3191
3192 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003193 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 return;
3195
3196 if (p_title)
3197 {
3198 if (p_titlelen > 0)
3199 {
3200 maxlen = p_titlelen * Columns / 100;
3201 if (maxlen < 10)
3202 maxlen = 10;
3203 }
3204
3205 t_str = buf;
3206 if (*p_titlestring != NUL)
3207 {
3208#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003209 if (stl_syntax & STL_IN_TITLE)
3210 {
3211 int use_sandbox = FALSE;
3212 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003213
3214# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003215 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003216# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003217 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003219 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003220 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003221 if (called_emsg)
3222 set_string_option_direct((char_u *)"titlestring", -1,
3223 (char_u *)"", OPT_FREE, SID_ERROR);
3224 called_emsg |= save_called_emsg;
3225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 else
3227#endif
3228 t_str = p_titlestring;
3229 }
3230 else
3231 {
3232 /* format: "fname + (path) (1 of 2) - VIM" */
3233
3234 if (curbuf->b_fname == NULL)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003235 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 else
3237 {
3238 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003239 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 }
3242
3243 switch (bufIsChanged(curbuf)
3244 + (curbuf->b_p_ro * 2)
3245 + (!curbuf->b_p_ma * 4))
3246 {
3247 case 1: STRCAT(buf, " +"); break;
3248 case 2: STRCAT(buf, " ="); break;
3249 case 3: STRCAT(buf, " =+"); break;
3250 case 4:
3251 case 6: STRCAT(buf, " -"); break;
3252 case 5:
3253 case 7: STRCAT(buf, " -+"); break;
3254 }
3255
3256 if (curbuf->b_fname != NULL)
3257 {
3258 /* Get path of file, replace home dir with ~ */
3259 off = (int)STRLEN(buf);
3260 buf[off++] = ' ';
3261 buf[off++] = '(';
3262 home_replace(curbuf, curbuf->b_ffname,
3263 buf + off, IOSIZE - off, TRUE);
3264#ifdef BACKSLASH_IN_FILENAME
3265 /* avoid "c:/name" to be reduced to "c" */
3266 if (isalpha(buf[off]) && buf[off + 1] == ':')
3267 off += 2;
3268#endif
3269 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003270 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003271 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003273 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003274 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003277
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 /* translate unprintable chars */
3279 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003280 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 STRCAT(buf, ")");
3283 }
3284
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003285 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003286
3287#if defined(FEAT_CLIENTSERVER)
3288 if (serverName != NULL)
3289 {
3290 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003291 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 }
3293 else
3294#endif
3295 STRCAT(buf, " - VIM");
3296
3297 if (maxlen > 0)
3298 {
3299 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003300 if (vim_strsize(buf) > maxlen)
3301 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 }
3303 }
3304 }
3305 mustset = ti_change(t_str, &lasttitle);
3306
3307 if (p_icon)
3308 {
3309 i_str = buf;
3310 if (*p_iconstring != NUL)
3311 {
3312#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003313 if (stl_syntax & STL_IN_ICON)
3314 {
3315 int use_sandbox = FALSE;
3316 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003317
3318# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003319 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003320# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003321 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003323 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003324 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003325 if (called_emsg)
3326 set_string_option_direct((char_u *)"iconstring", -1,
3327 (char_u *)"", OPT_FREE, SID_ERROR);
3328 called_emsg |= save_called_emsg;
3329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 else
3331#endif
3332 i_str = p_iconstring;
3333 }
3334 else
3335 {
3336 if (buf_spname(curbuf) != NULL)
3337 i_name = (char_u *)buf_spname(curbuf);
3338 else /* use file name only in icon */
3339 i_name = gettail(curbuf->b_ffname);
3340 *i_str = NUL;
3341 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003342 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 if (len > 100)
3344 {
3345 len -= 100;
3346#ifdef FEAT_MBYTE
3347 if (has_mbyte)
3348 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3349#endif
3350 i_name += len;
3351 }
3352 STRCPY(i_str, i_name);
3353 trans_characters(i_str, IOSIZE);
3354 }
3355 }
3356
3357 mustset |= ti_change(i_str, &lasticon);
3358
3359 if (mustset)
3360 resettitle();
3361}
3362
3363/*
3364 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3365 * from "str" if it does.
3366 * Return TRUE when "*last" changed.
3367 */
3368 static int
3369ti_change(str, last)
3370 char_u *str;
3371 char_u **last;
3372{
3373 if ((str == NULL) != (*last == NULL)
3374 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3375 {
3376 vim_free(*last);
3377 if (str == NULL)
3378 *last = NULL;
3379 else
3380 *last = vim_strsave(str);
3381 return TRUE;
3382 }
3383 return FALSE;
3384}
3385
3386/*
3387 * Put current window title back (used after calling a shell)
3388 */
3389 void
3390resettitle()
3391{
3392 mch_settitle(lasttitle, lasticon);
3393}
Bram Moolenaarea408852005-06-25 22:49:46 +00003394
3395# if defined(EXITFREE) || defined(PROTO)
3396 void
3397free_titles()
3398{
3399 vim_free(lasttitle);
3400 vim_free(lasticon);
3401}
3402# endif
3403
Bram Moolenaar071d4272004-06-13 20:20:40 +00003404#endif /* FEAT_TITLE */
3405
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003406#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003408 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 * Return length of string in screen cells.
3410 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003411 * Normally works for window "wp", except when working for 'tabline' then it
3412 * is "curwin".
3413 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003414 * Items are drawn interspersed with the text that surrounds it
3415 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3416 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3417 *
3418 * If maxwidth is not zero, the string will be filled at any middle marker
3419 * or truncated if too long, fillchar is used for all whitespace.
3420 */
3421 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003422build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3423 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003425 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003426 size_t outlen; /* length of out[] */
3427 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003428 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 int fillchar;
3430 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003431 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3432 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433{
3434 char_u *p;
3435 char_u *s;
3436 char_u *t;
3437 char_u *linecont;
3438#ifdef FEAT_EVAL
3439 win_T *o_curwin;
3440 buf_T *o_curbuf;
3441#endif
3442 int empty_line;
3443 colnr_T virtcol;
3444 long l;
3445 long n;
3446 int prevchar_isflag;
3447 int prevchar_isitem;
3448 int itemisflag;
3449 int fillable;
3450 char_u *str;
3451 long num;
3452 int width;
3453 int itemcnt;
3454 int curitem;
3455 int groupitem[STL_MAX_ITEM];
3456 int groupdepth;
3457 struct stl_item
3458 {
3459 char_u *start;
3460 int minwid;
3461 int maxwid;
3462 enum
3463 {
3464 Normal,
3465 Empty,
3466 Group,
3467 Middle,
3468 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003469 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 Trunc
3471 } type;
3472 } item[STL_MAX_ITEM];
3473 int minwid;
3474 int maxwid;
3475 int zeropad;
3476 char_u base;
3477 char_u opt;
3478#define TMPLEN 70
3479 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003480 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003481 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003482
3483#ifdef FEAT_EVAL
3484 /*
3485 * When the format starts with "%!" then evaluate it as an expression and
3486 * use the result as the actual format string.
3487 */
3488 if (fmt[0] == '%' && fmt[1] == '!')
3489 {
3490 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3491 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003492 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003493 }
3494#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495
3496 if (fillchar == 0)
3497 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003498#ifdef FEAT_MBYTE
3499 /* Can't handle a multi-byte fill character yet. */
3500 else if (mb_char2len(fillchar) > 1)
3501 fillchar = '-';
3502#endif
3503
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504 /*
3505 * Get line & check if empty (cursorpos will show "0-1").
3506 * If inversion is possible we use it. Else '=' characters are used.
3507 */
3508 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3509 empty_line = (*linecont == NUL);
3510
3511 groupdepth = 0;
3512 p = out;
3513 curitem = 0;
3514 prevchar_isflag = TRUE;
3515 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003516 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003518 if (curitem == STL_MAX_ITEM)
3519 {
3520 /* There are too many items. Add the error code to the statusline
3521 * to give the user a hint about what went wrong. */
3522 if (p + 6 < out + outlen)
3523 {
3524 mch_memmove(p, " E541", (size_t)5);
3525 p += 5;
3526 }
3527 break;
3528 }
3529
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 if (*s != NUL && *s != '%')
3531 prevchar_isflag = prevchar_isitem = FALSE;
3532
3533 /*
3534 * Handle up to the next '%' or the end.
3535 */
3536 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3537 *p++ = *s++;
3538 if (*s == NUL || p + 1 >= out + outlen)
3539 break;
3540
3541 /*
3542 * Handle one '%' item.
3543 */
3544 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003545 if (*s == NUL) /* ignore trailing % */
3546 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 if (*s == '%')
3548 {
3549 if (p + 1 >= out + outlen)
3550 break;
3551 *p++ = *s++;
3552 prevchar_isflag = prevchar_isitem = FALSE;
3553 continue;
3554 }
3555 if (*s == STL_MIDDLEMARK)
3556 {
3557 s++;
3558 if (groupdepth > 0)
3559 continue;
3560 item[curitem].type = Middle;
3561 item[curitem++].start = p;
3562 continue;
3563 }
3564 if (*s == STL_TRUNCMARK)
3565 {
3566 s++;
3567 item[curitem].type = Trunc;
3568 item[curitem++].start = p;
3569 continue;
3570 }
3571 if (*s == ')')
3572 {
3573 s++;
3574 if (groupdepth < 1)
3575 continue;
3576 groupdepth--;
3577
3578 t = item[groupitem[groupdepth]].start;
3579 *p = NUL;
3580 l = vim_strsize(t);
3581 if (curitem > groupitem[groupdepth] + 1
3582 && item[groupitem[groupdepth]].minwid == 0)
3583 {
3584 /* remove group if all items are empty */
3585 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3586 if (item[n].type == Normal)
3587 break;
3588 if (n == curitem)
3589 {
3590 p = t;
3591 l = 0;
3592 }
3593 }
3594 if (l > item[groupitem[groupdepth]].maxwid)
3595 {
3596 /* truncate, remove n bytes of text at the start */
3597#ifdef FEAT_MBYTE
3598 if (has_mbyte)
3599 {
3600 /* Find the first character that should be included. */
3601 n = 0;
3602 while (l >= item[groupitem[groupdepth]].maxwid)
3603 {
3604 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003605 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 }
3607 }
3608 else
3609#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003610 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611
3612 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003613 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 p = p - n + 1;
3615#ifdef FEAT_MBYTE
3616 /* Fill up space left over by half a double-wide char. */
3617 while (++l < item[groupitem[groupdepth]].minwid)
3618 *p++ = fillchar;
3619#endif
3620
3621 /* correct the start of the items for the truncation */
3622 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3623 {
3624 item[l].start -= n;
3625 if (item[l].start < t)
3626 item[l].start = t;
3627 }
3628 }
3629 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3630 {
3631 /* fill */
3632 n = item[groupitem[groupdepth]].minwid;
3633 if (n < 0)
3634 {
3635 /* fill by appending characters */
3636 n = 0 - n;
3637 while (l++ < n && p + 1 < out + outlen)
3638 *p++ = fillchar;
3639 }
3640 else
3641 {
3642 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003643 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 l = n - l;
3645 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003646 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 p += l;
3648 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3649 item[n].start += l;
3650 for ( ; l > 0; l--)
3651 *t++ = fillchar;
3652 }
3653 }
3654 continue;
3655 }
3656 minwid = 0;
3657 maxwid = 9999;
3658 zeropad = FALSE;
3659 l = 1;
3660 if (*s == '0')
3661 {
3662 s++;
3663 zeropad = TRUE;
3664 }
3665 if (*s == '-')
3666 {
3667 s++;
3668 l = -1;
3669 }
3670 if (VIM_ISDIGIT(*s))
3671 {
3672 minwid = (int)getdigits(&s);
3673 if (minwid < 0) /* overflow */
3674 minwid = 0;
3675 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003676 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003677 {
3678 item[curitem].type = Highlight;
3679 item[curitem].start = p;
3680 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3681 s++;
3682 curitem++;
3683 continue;
3684 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003685 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3686 {
3687 if (*s == STL_TABCLOSENR)
3688 {
3689 if (minwid == 0)
3690 {
3691 /* %X ends the close label, go back to the previously
3692 * define tab label nr. */
3693 for (n = curitem - 1; n >= 0; --n)
3694 if (item[n].type == TabPage && item[n].minwid >= 0)
3695 {
3696 minwid = item[n].minwid;
3697 break;
3698 }
3699 }
3700 else
3701 /* close nrs are stored as negative values */
3702 minwid = - minwid;
3703 }
3704 item[curitem].type = TabPage;
3705 item[curitem].start = p;
3706 item[curitem].minwid = minwid;
3707 s++;
3708 curitem++;
3709 continue;
3710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003711 if (*s == '.')
3712 {
3713 s++;
3714 if (VIM_ISDIGIT(*s))
3715 {
3716 maxwid = (int)getdigits(&s);
3717 if (maxwid <= 0) /* overflow */
3718 maxwid = 50;
3719 }
3720 }
3721 minwid = (minwid > 50 ? 50 : minwid) * l;
3722 if (*s == '(')
3723 {
3724 groupitem[groupdepth++] = curitem;
3725 item[curitem].type = Group;
3726 item[curitem].start = p;
3727 item[curitem].minwid = minwid;
3728 item[curitem].maxwid = maxwid;
3729 s++;
3730 curitem++;
3731 continue;
3732 }
3733 if (vim_strchr(STL_ALL, *s) == NULL)
3734 {
3735 s++;
3736 continue;
3737 }
3738 opt = *s++;
3739
3740 /* OK - now for the real work */
3741 base = 'D';
3742 itemisflag = FALSE;
3743 fillable = TRUE;
3744 num = -1;
3745 str = NULL;
3746 switch (opt)
3747 {
3748 case STL_FILEPATH:
3749 case STL_FULLPATH:
3750 case STL_FILENAME:
3751 fillable = FALSE; /* don't change ' ' to fillchar */
3752 if (buf_spname(wp->w_buffer) != NULL)
3753 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3754 else
3755 {
3756 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003757 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3759 }
3760 trans_characters(NameBuff, MAXPATHL);
3761 if (opt != STL_FILENAME)
3762 str = NameBuff;
3763 else
3764 str = gettail(NameBuff);
3765 break;
3766
3767 case STL_VIM_EXPR: /* '{' */
3768 itemisflag = TRUE;
3769 t = p;
3770 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3771 *p++ = *s++;
3772 if (*s != '}') /* missing '}' or out of space */
3773 break;
3774 s++;
3775 *p = 0;
3776 p = t;
3777
3778#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003779 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3781
3782 o_curbuf = curbuf;
3783 o_curwin = curwin;
3784 curwin = wp;
3785 curbuf = wp->w_buffer;
3786
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003787 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
3789 curwin = o_curwin;
3790 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003791 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792
3793 if (str != NULL && *str != 0)
3794 {
3795 if (*skipdigits(str) == NUL)
3796 {
3797 num = atoi((char *)str);
3798 vim_free(str);
3799 str = NULL;
3800 itemisflag = FALSE;
3801 }
3802 }
3803#endif
3804 break;
3805
3806 case STL_LINE:
3807 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3808 ? 0L : (long)(wp->w_cursor.lnum);
3809 break;
3810
3811 case STL_NUMLINES:
3812 num = wp->w_buffer->b_ml.ml_line_count;
3813 break;
3814
3815 case STL_COLUMN:
3816 num = !(State & INSERT) && empty_line
3817 ? 0 : (int)wp->w_cursor.col + 1;
3818 break;
3819
3820 case STL_VIRTCOL:
3821 case STL_VIRTCOL_ALT:
3822 /* In list mode virtcol needs to be recomputed */
3823 virtcol = wp->w_virtcol;
3824 if (wp->w_p_list && lcs_tab1 == NUL)
3825 {
3826 wp->w_p_list = FALSE;
3827 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3828 wp->w_p_list = TRUE;
3829 }
3830 ++virtcol;
3831 /* Don't display %V if it's the same as %c. */
3832 if (opt == STL_VIRTCOL_ALT
3833 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3834 ? 0 : (int)wp->w_cursor.col + 1)))
3835 break;
3836 num = (long)virtcol;
3837 break;
3838
3839 case STL_PERCENTAGE:
3840 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3841 (long)wp->w_buffer->b_ml.ml_line_count);
3842 break;
3843
3844 case STL_ALTPERCENT:
3845 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003846 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 break;
3848
3849 case STL_ARGLISTSTAT:
3850 fillable = FALSE;
3851 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003852 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 str = tmp;
3854 break;
3855
3856 case STL_KEYMAP:
3857 fillable = FALSE;
3858 if (get_keymap_str(wp, tmp, TMPLEN))
3859 str = tmp;
3860 break;
3861 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003862#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003863 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864#else
3865 num = 0;
3866#endif
3867 break;
3868
3869 case STL_BUFNO:
3870 num = wp->w_buffer->b_fnum;
3871 break;
3872
3873 case STL_OFFSET_X:
3874 base = 'X';
3875 case STL_OFFSET:
3876#ifdef FEAT_BYTEOFF
3877 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3878 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3879 0L : l + 1 + (!(State & INSERT) && empty_line ?
3880 0 : (int)wp->w_cursor.col);
3881#endif
3882 break;
3883
3884 case STL_BYTEVAL_X:
3885 base = 'X';
3886 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003887 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 num = 0;
3889 else
3890 {
3891#ifdef FEAT_MBYTE
3892 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3893#else
3894 num = linecont[wp->w_cursor.col];
3895#endif
3896 }
3897 if (num == NL)
3898 num = 0;
3899 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3900 num = NL;
3901 break;
3902
3903 case STL_ROFLAG:
3904 case STL_ROFLAG_ALT:
3905 itemisflag = TRUE;
3906 if (wp->w_buffer->b_p_ro)
3907 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3908 break;
3909
3910 case STL_HELPFLAG:
3911 case STL_HELPFLAG_ALT:
3912 itemisflag = TRUE;
3913 if (wp->w_buffer->b_help)
3914 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003915 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 break;
3917
3918#ifdef FEAT_AUTOCMD
3919 case STL_FILETYPE:
3920 if (*wp->w_buffer->b_p_ft != NUL
3921 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3922 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003923 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3924 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 str = tmp;
3926 }
3927 break;
3928
3929 case STL_FILETYPE_ALT:
3930 itemisflag = TRUE;
3931 if (*wp->w_buffer->b_p_ft != NUL
3932 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3933 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003934 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3935 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936 for (t = tmp; *t != 0; t++)
3937 *t = TOUPPER_LOC(*t);
3938 str = tmp;
3939 }
3940 break;
3941#endif
3942
3943#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3944 case STL_PREVIEWFLAG:
3945 case STL_PREVIEWFLAG_ALT:
3946 itemisflag = TRUE;
3947 if (wp->w_p_pvw)
3948 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3949 : _("[Preview]"));
3950 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003951
3952 case STL_QUICKFIX:
3953 if (bt_quickfix(wp->w_buffer))
3954 str = (char_u *)(wp->w_llist_ref
3955 ? _(msg_loclist)
3956 : _(msg_qflist));
3957 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958#endif
3959
3960 case STL_MODIFIED:
3961 case STL_MODIFIED_ALT:
3962 itemisflag = TRUE;
3963 switch ((opt == STL_MODIFIED_ALT)
3964 + bufIsChanged(wp->w_buffer) * 2
3965 + (!wp->w_buffer->b_p_ma) * 4)
3966 {
3967 case 2: str = (char_u *)"[+]"; break;
3968 case 3: str = (char_u *)",+"; break;
3969 case 4: str = (char_u *)"[-]"; break;
3970 case 5: str = (char_u *)",-"; break;
3971 case 6: str = (char_u *)"[+-]"; break;
3972 case 7: str = (char_u *)",+-"; break;
3973 }
3974 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003975
3976 case STL_HIGHLIGHT:
3977 t = s;
3978 while (*s != '#' && *s != NUL)
3979 ++s;
3980 if (*s == '#')
3981 {
3982 item[curitem].type = Highlight;
3983 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003984 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003985 curitem++;
3986 }
3987 ++s;
3988 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 }
3990
3991 item[curitem].start = p;
3992 item[curitem].type = Normal;
3993 if (str != NULL && *str)
3994 {
3995 t = str;
3996 if (itemisflag)
3997 {
3998 if ((t[0] && t[1])
3999 && ((!prevchar_isitem && *t == ',')
4000 || (prevchar_isflag && *t == ' ')))
4001 t++;
4002 prevchar_isflag = TRUE;
4003 }
4004 l = vim_strsize(t);
4005 if (l > 0)
4006 prevchar_isitem = TRUE;
4007 if (l > maxwid)
4008 {
4009 while (l >= maxwid)
4010#ifdef FEAT_MBYTE
4011 if (has_mbyte)
4012 {
4013 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004014 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 }
4016 else
4017#endif
4018 l -= byte2cells(*t++);
4019 if (p + 1 >= out + outlen)
4020 break;
4021 *p++ = '<';
4022 }
4023 if (minwid > 0)
4024 {
4025 for (; l < minwid && p + 1 < out + outlen; l++)
4026 {
4027 /* Don't put a "-" in front of a digit. */
4028 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4029 *p++ = ' ';
4030 else
4031 *p++ = fillchar;
4032 }
4033 minwid = 0;
4034 }
4035 else
4036 minwid *= -1;
4037 while (*t && p + 1 < out + outlen)
4038 {
4039 *p++ = *t++;
4040 /* Change a space by fillchar, unless fillchar is '-' and a
4041 * digit follows. */
4042 if (fillable && p[-1] == ' '
4043 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4044 p[-1] = fillchar;
4045 }
4046 for (; l < minwid && p + 1 < out + outlen; l++)
4047 *p++ = fillchar;
4048 }
4049 else if (num >= 0)
4050 {
4051 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4052 char_u nstr[20];
4053
4054 if (p + 20 >= out + outlen)
4055 break; /* not sufficient space */
4056 prevchar_isitem = TRUE;
4057 t = nstr;
4058 if (opt == STL_VIRTCOL_ALT)
4059 {
4060 *t++ = '-';
4061 minwid--;
4062 }
4063 *t++ = '%';
4064 if (zeropad)
4065 *t++ = '0';
4066 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004067 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 *t = 0;
4069
4070 for (n = num, l = 1; n >= nbase; n /= nbase)
4071 l++;
4072 if (opt == STL_VIRTCOL_ALT)
4073 l++;
4074 if (l > maxwid)
4075 {
4076 l += 2;
4077 n = l - maxwid;
4078 while (l-- > maxwid)
4079 num /= nbase;
4080 *t++ = '>';
4081 *t++ = '%';
4082 *t = t[-3];
4083 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004084 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4085 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
4087 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004088 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4089 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 p += STRLEN(p);
4091 }
4092 else
4093 item[curitem].type = Empty;
4094
4095 if (opt == STL_VIM_EXPR)
4096 vim_free(str);
4097
4098 if (num >= 0 || (!itemisflag && str && *str))
4099 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4100 curitem++;
4101 }
4102 *p = NUL;
4103 itemcnt = curitem;
4104
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004105#ifdef FEAT_EVAL
4106 if (usefmt != fmt)
4107 vim_free(usefmt);
4108#endif
4109
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 width = vim_strsize(out);
4111 if (maxwidth > 0 && width > maxwidth)
4112 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004113 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114 l = 0;
4115 if (itemcnt == 0)
4116 s = out;
4117 else
4118 {
4119 for ( ; l < itemcnt; l++)
4120 if (item[l].type == Trunc)
4121 {
4122 /* Truncate at %< item. */
4123 s = item[l].start;
4124 break;
4125 }
4126 if (l == itemcnt)
4127 {
4128 /* No %< item, truncate first item. */
4129 s = item[0].start;
4130 l = 0;
4131 }
4132 }
4133
4134 if (width - vim_strsize(s) >= maxwidth)
4135 {
4136 /* Truncation mark is beyond max length */
4137#ifdef FEAT_MBYTE
4138 if (has_mbyte)
4139 {
4140 s = out;
4141 width = 0;
4142 for (;;)
4143 {
4144 width += ptr2cells(s);
4145 if (width >= maxwidth)
4146 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004147 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 }
4149 /* Fill up for half a double-wide character. */
4150 while (++width < maxwidth)
4151 *s++ = fillchar;
4152 }
4153 else
4154#endif
4155 s = out + maxwidth - 1;
4156 for (l = 0; l < itemcnt; l++)
4157 if (item[l].start > s)
4158 break;
4159 itemcnt = l;
4160 *s++ = '>';
4161 *s = 0;
4162 }
4163 else
4164 {
4165#ifdef FEAT_MBYTE
4166 if (has_mbyte)
4167 {
4168 n = 0;
4169 while (width >= maxwidth)
4170 {
4171 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004172 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 }
4174 }
4175 else
4176#endif
4177 n = width - maxwidth + 1;
4178 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004179 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 *s = '<';
4181
4182 /* Fill up for half a double-wide character. */
4183 while (++width < maxwidth)
4184 {
4185 s = s + STRLEN(s);
4186 *s++ = fillchar;
4187 *s = NUL;
4188 }
4189
4190 --n; /* count the '<' */
4191 for (; l < itemcnt; l++)
4192 {
4193 if (item[l].start - n >= s)
4194 item[l].start -= n;
4195 else
4196 item[l].start = s;
4197 }
4198 }
4199 width = maxwidth;
4200 }
4201 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4202 {
4203 /* Apply STL_MIDDLE if any */
4204 for (l = 0; l < itemcnt; l++)
4205 if (item[l].type == Middle)
4206 break;
4207 if (l < itemcnt)
4208 {
4209 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004210 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 for (s = item[l].start; s < p; s++)
4212 *s = fillchar;
4213 for (l++; l < itemcnt; l++)
4214 item[l].start += maxwidth - width;
4215 width = maxwidth;
4216 }
4217 }
4218
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004219 /* Store the info about highlighting. */
4220 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004222 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 for (l = 0; l < itemcnt; l++)
4224 {
4225 if (item[l].type == Highlight)
4226 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004227 sp->start = item[l].start;
4228 sp->userhl = item[l].minwid;
4229 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004232 sp->start = NULL;
4233 sp->userhl = 0;
4234 }
4235
4236 /* Store the info about tab pages labels. */
4237 if (tabtab != NULL)
4238 {
4239 sp = tabtab;
4240 for (l = 0; l < itemcnt; l++)
4241 {
4242 if (item[l].type == TabPage)
4243 {
4244 sp->start = item[l].start;
4245 sp->userhl = item[l].minwid;
4246 sp++;
4247 }
4248 }
4249 sp->start = NULL;
4250 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 }
4252
4253 return width;
4254}
4255#endif /* FEAT_STL_OPT */
4256
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004257#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4258 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004260 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4261 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 */
4263 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004264get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004266 char_u *buf;
4267 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268{
4269 long above; /* number of lines above window */
4270 long below; /* number of lines below window */
4271
4272 above = wp->w_topline - 1;
4273#ifdef FEAT_DIFF
4274 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4275#endif
4276 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4277 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004278 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4279 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004281 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004283 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 ? (int)(above / ((above + below) / 100L))
4285 : (int)(above * 100L / (above + below)));
4286}
4287#endif
4288
4289/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004290 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 * Return TRUE if it was appended.
4292 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004293 static int
4294append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 win_T *wp;
4296 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004297 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299{
4300 char_u *p;
4301
4302 if (ARGCOUNT <= 1) /* nothing to do */
4303 return FALSE;
4304
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004305 p = buf + STRLEN(buf); /* go to the end of the buffer */
4306 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 return FALSE;
4308 *p++ = ' ';
4309 *p++ = '(';
4310 if (add_file)
4311 {
4312 STRCPY(p, "file ");
4313 p += 5;
4314 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004315 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4316 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4318 return TRUE;
4319}
4320
4321/*
4322 * If fname is not a full path, make it a full path.
4323 * Returns pointer to allocated memory (NULL for failure).
4324 */
4325 char_u *
4326fix_fname(fname)
4327 char_u *fname;
4328{
4329 /*
4330 * Force expanding the path always for Unix, because symbolic links may
4331 * mess up the full path name, even though it starts with a '/'.
4332 * Also expand when there is ".." in the file name, try to remove it,
4333 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004334 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 * For MS-Windows also expand names like "longna~1" to "longname".
4336 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004337#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 return FullName_save(fname, TRUE);
4339#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004340 if (!vim_isAbsName(fname)
4341 || strstr((char *)fname, "..") != NULL
4342 || strstr((char *)fname, "//") != NULL
4343# ifdef BACKSLASH_IN_FILENAME
4344 || strstr((char *)fname, "\\\\") != NULL
4345# endif
4346# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 )
4350 return FullName_save(fname, FALSE);
4351
4352 fname = vim_strsave(fname);
4353
Bram Moolenaar9b942202007-10-03 12:31:33 +00004354# ifdef USE_FNAME_CASE
4355# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004357# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
4359 if (fname != NULL)
4360 fname_case(fname, 0); /* set correct case for file name */
4361 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004362# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364 return fname;
4365#endif
4366}
4367
4368/*
4369 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4370 * "ffname" becomes a pointer to allocated memory (or NULL).
4371 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 void
4373fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004374 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 char_u **ffname;
4376 char_u **sfname;
4377{
4378 if (*ffname == NULL) /* if no file name given, nothing to do */
4379 return;
4380 if (*sfname == NULL) /* if no short file name given, use ffname */
4381 *sfname = *ffname;
4382 *ffname = fix_fname(*ffname); /* expand to full path */
4383
4384#ifdef FEAT_SHORTCUT
4385 if (!buf->b_p_bin)
4386 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004387 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 /* If the file name is a shortcut file, use the file it links to. */
4390 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004391 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 {
4393 vim_free(*ffname);
4394 *ffname = rfname;
4395 *sfname = rfname;
4396 }
4397 }
4398#endif
4399}
4400
4401/*
4402 * Get the file name for an argument list entry.
4403 */
4404 char_u *
4405alist_name(aep)
4406 aentry_T *aep;
4407{
4408 buf_T *bp;
4409
4410 /* Use the name from the associated buffer if it exists. */
4411 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004412 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 return aep->ae_fname;
4414 return bp->b_fname;
4415}
4416
4417#if defined(FEAT_WINDOWS) || defined(PROTO)
4418/*
4419 * do_arg_all(): Open up to 'count' windows, one for each argument.
4420 */
4421 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004422do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 int count;
4424 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004425 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426{
4427 int i;
4428 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004429 char_u *opened; /* Array of weight for which args are open:
4430 * 0: not opened
4431 * 1: opened in other tab
4432 * 2: opened in curtab
4433 * 3: opened in curtab and curwin
4434 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004435 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 int use_firstwin = FALSE; /* use first window for arglist */
4437 int split_ret = OK;
4438 int p_ea_save;
4439 alist_T *alist; /* argument list to be used */
4440 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004441 tabpage_T *tpnext;
4442 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004443 win_T *old_curwin, *last_curwin;
4444 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004445 win_T *new_curwin = NULL;
4446 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
4448 if (ARGCOUNT <= 0)
4449 {
4450 /* Don't give an error message. We don't want it when the ":all"
4451 * command is in the .vimrc. */
4452 return;
4453 }
4454 setpcmark();
4455
4456 opened_len = ARGCOUNT;
4457 opened = alloc_clear((unsigned)opened_len);
4458 if (opened == NULL)
4459 return;
4460
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004461 /* Autocommands may do anything to the argument list. Make sure it's not
4462 * freed while we are working here by "locking" it. We still have to
4463 * watch out for its size to be changed. */
4464 alist = curwin->w_alist;
4465 ++alist->al_refcount;
4466
4467 old_curwin = curwin;
4468 old_curtab = curtab;
4469
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#ifdef FEAT_GUI
4471 need_mouse_correct = TRUE;
4472#endif
4473
4474 /*
4475 * Try closing all windows that are not in the argument list.
4476 * Also close windows that are not full width;
4477 * When 'hidden' or "forceit" set the buffer becomes hidden.
4478 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004479 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004481 if (had_tab > 0)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004482 goto_tabpage_tp(first_tabpage, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004483 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004485 tpnext = curtab->tp_next;
4486 for (wp = firstwin; wp != NULL; wp = wpnext)
4487 {
4488 wpnext = wp->w_next;
4489 buf = wp->w_buffer;
4490 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004491 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004493 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004495 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004496 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004497 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004499 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004500 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004502 if (i < alist->al_ga.ga_len
4503 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4504 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4505 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004507 int weight = 1;
4508
4509 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004510 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004511 ++weight;
4512 if (old_curwin == wp)
4513 ++weight;
4514 }
4515
4516 if (weight > (int)opened[i])
4517 {
4518 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004519 if (i == 0)
4520 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004521 if (new_curwin != NULL)
4522 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004523 new_curwin = wp;
4524 new_curtab = curtab;
4525 }
4526 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004527 else if (keep_tabs)
4528 i = opened_len;
4529
4530 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004531 {
4532 /* Use the current argument list for all windows
4533 * containing a file from it. */
4534 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004535 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004536 ++wp->w_alist->al_refcount;
4537 }
4538 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 }
4541 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004542 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004544 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004546 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004547 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004549 /* If the buffer was changed, and we would like to hide it,
4550 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004551 if (!P_HID(buf) && buf->b_nwindows <= 1
4552 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004554 (void)autowrite(buf, FALSE);
4555#ifdef FEAT_AUTOCMD
4556 /* check if autocommands removed the window */
4557 if (!win_valid(wp) || !buf_valid(buf))
4558 {
4559 wpnext = firstwin; /* start all over... */
4560 continue;
4561 }
4562#endif
4563 }
4564#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004565 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004566 if (firstwin == lastwin
4567 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004568#endif
4569 use_firstwin = TRUE;
4570#ifdef FEAT_WINDOWS
4571 else
4572 {
4573 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4574# ifdef FEAT_AUTOCMD
4575 /* check if autocommands removed the next window */
4576 if (!win_valid(wpnext))
4577 wpnext = firstwin; /* start all over... */
4578# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 }
4580#endif
4581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 }
4583 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004584
4585 /* Without the ":tab" modifier only do the current tab page. */
4586 if (had_tab == 0 || tpnext == NULL)
4587 break;
4588
4589# ifdef FEAT_AUTOCMD
4590 /* check if autocommands removed the next tab page */
4591 if (!valid_tabpage(tpnext))
4592 tpnext = first_tabpage; /* start all over...*/
4593# endif
Bram Moolenaara8596c42012-06-13 14:28:20 +02004594 goto_tabpage_tp(tpnext, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 }
4596
4597 /*
4598 * Open a window for files in the argument list that don't have one.
4599 * ARGCOUNT may change while doing this, because of autocommands.
4600 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004601 if (count > opened_len || count <= 0)
4602 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
4604#ifdef FEAT_AUTOCMD
4605 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4606 ++autocmd_no_enter;
4607 ++autocmd_no_leave;
4608#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004609 last_curwin = curwin;
4610 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004612#ifdef FEAT_WINDOWS
4613 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4614 * leaving an empty tab page when executed locally. */
4615 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4616 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4617 use_firstwin = TRUE;
4618#endif
4619
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004620 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621 {
4622 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4623 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004624 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
4626 /* Move the already present window to below the current window */
4627 if (curwin->w_arg_idx != i)
4628 {
4629 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4630 {
4631 if (wpnext->w_arg_idx == i)
4632 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004633 if (keep_tabs)
4634 {
4635 new_curwin = wpnext;
4636 new_curtab = curtab;
4637 }
4638 else
4639 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 break;
4641 }
4642 }
4643 }
4644 }
4645 else if (split_ret == OK)
4646 {
4647 if (!use_firstwin) /* split current window */
4648 {
4649 p_ea_save = p_ea;
4650 p_ea = TRUE; /* use space from all windows */
4651 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4652 p_ea = p_ea_save;
4653 if (split_ret == FAIL)
4654 continue;
4655 }
4656#ifdef FEAT_AUTOCMD
4657 else /* first window: do autocmd for leaving this buffer */
4658 --autocmd_no_leave;
4659#endif
4660
4661 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004662 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 */
4664 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004665 if (i == 0)
4666 {
4667 new_curwin = curwin;
4668 new_curtab = curtab;
4669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4671 ECMD_ONE,
4672 ((P_HID(curwin->w_buffer)
4673 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004674 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675#ifdef FEAT_AUTOCMD
4676 if (use_firstwin)
4677 ++autocmd_no_leave;
4678#endif
4679 use_firstwin = FALSE;
4680 }
4681 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004682
4683 /* When ":tab" was used open a new tab for a new window repeatedly. */
4684 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4685 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 }
4687
4688 /* Remove the "lock" on the argument list. */
4689 alist_unlink(alist);
4690
4691#ifdef FEAT_AUTOCMD
4692 --autocmd_no_enter;
4693#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004694 /* restore last referenced tabpage's curwin */
4695 if (last_curtab != new_curtab)
4696 {
4697 if (valid_tabpage(last_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +02004698 goto_tabpage_tp(last_curtab, TRUE);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004699 if (win_valid(last_curwin))
4700 win_enter(last_curwin, FALSE);
4701 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004702 /* to window with first arg */
4703 if (valid_tabpage(new_curtab))
Bram Moolenaara8596c42012-06-13 14:28:20 +02004704 goto_tabpage_tp(new_curtab, TRUE);
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004705 if (win_valid(new_curwin))
4706 win_enter(new_curwin, FALSE);
4707
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708#ifdef FEAT_AUTOCMD
4709 --autocmd_no_leave;
4710#endif
4711 vim_free(opened);
4712}
4713
4714# if defined(FEAT_LISTCMDS) || defined(PROTO)
4715/*
4716 * Open a window for a number of buffers.
4717 */
4718 void
4719ex_buffer_all(eap)
4720 exarg_T *eap;
4721{
4722 buf_T *buf;
4723 win_T *wp, *wpnext;
4724 int split_ret = OK;
4725 int p_ea_save;
4726 int open_wins = 0;
4727 int r;
4728 int count; /* Maximum number of windows to open. */
4729 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004730#ifdef FEAT_WINDOWS
4731 int had_tab = cmdmod.tab;
4732 tabpage_T *tpnext;
4733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
4735 if (eap->addr_count == 0) /* make as many windows as possible */
4736 count = 9999;
4737 else
4738 count = eap->line2; /* make as many windows as specified */
4739 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4740 all = FALSE;
4741 else
4742 all = TRUE;
4743
4744 setpcmark();
4745
4746#ifdef FEAT_GUI
4747 need_mouse_correct = TRUE;
4748#endif
4749
4750 /*
4751 * Close superfluous windows (two windows for the same buffer).
4752 * Also close windows that are not full-width.
4753 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004754#ifdef FEAT_WINDOWS
4755 if (had_tab > 0)
Bram Moolenaara8596c42012-06-13 14:28:20 +02004756 goto_tabpage_tp(first_tabpage, TRUE);
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004757 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004760 tpnext = curtab->tp_next;
4761 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004763 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004764 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004765#ifdef FEAT_VERTSPLIT
4766 || ((cmdmod.split & WSP_VERT)
4767 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004768 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004769 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004771#ifdef FEAT_WINDOWS
4772 || (had_tab > 0 && wp != firstwin)
4773#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004774 ) && firstwin != lastwin
4775#ifdef FEAT_AUTOCMD
4776 && !(wp->w_closing || wp->w_buffer->b_closing)
4777#endif
4778 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004779 {
4780 win_close(wp, FALSE);
4781#ifdef FEAT_AUTOCMD
4782 wpnext = firstwin; /* just in case an autocommand does
4783 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004784 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004785 open_wins = 0;
4786#endif
4787 }
4788 else
4789 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004791
4792#ifdef FEAT_WINDOWS
4793 /* Without the ":tab" modifier only do the current tab page. */
4794 if (had_tab == 0 || tpnext == NULL)
4795 break;
Bram Moolenaara8596c42012-06-13 14:28:20 +02004796 goto_tabpage_tp(tpnext, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799
4800 /*
4801 * Go through the buffer list. When a buffer doesn't have a window yet,
4802 * open one. Otherwise move the window to the right position.
4803 * Watch out for autocommands that delete buffers or windows!
4804 */
4805#ifdef FEAT_AUTOCMD
4806 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4807 ++autocmd_no_enter;
4808#endif
4809 win_enter(lastwin, FALSE);
4810#ifdef FEAT_AUTOCMD
4811 ++autocmd_no_leave;
4812#endif
4813 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4814 {
4815 /* Check if this buffer needs a window */
4816 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4817 continue;
4818
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004819#ifdef FEAT_WINDOWS
4820 if (had_tab != 0)
4821 {
4822 /* With the ":tab" modifier don't move the window. */
4823 if (buf->b_nwindows > 0)
4824 wp = lastwin; /* buffer has a window, skip it */
4825 else
4826 wp = NULL;
4827 }
4828 else
4829#endif
4830 {
4831 /* Check if this buffer already has a window */
4832 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4833 if (wp->w_buffer == buf)
4834 break;
4835 /* If the buffer already has a window, move it */
4836 if (wp != NULL)
4837 win_move_after(wp, curwin);
4838 }
4839
4840 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 {
4842 /* Split the window and put the buffer in it */
4843 p_ea_save = p_ea;
4844 p_ea = TRUE; /* use space from all windows */
4845 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4846 ++open_wins;
4847 p_ea = p_ea_save;
4848 if (split_ret == FAIL)
4849 continue;
4850
4851 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004852#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 swap_exists_action = SEA_DIALOG;
4854#endif
4855 set_curbuf(buf, DOBUF_GOTO);
4856#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004859#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004861# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 break;
4863 }
4864#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004865#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 if (swap_exists_action == SEA_QUIT)
4867 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004868# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4869 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004870
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004871 /* Reset the error/interrupt/exception state here so that
4872 * aborting() returns FALSE when closing a window. */
4873 enter_cleanup(&cs);
4874# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004875
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004876 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 win_close(curwin, TRUE);
4878 --open_wins;
4879 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004880 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004881
4882# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4883 /* Restore the error/interrupt/exception state if not
4884 * discarded by a new aborting error, interrupt, or uncaught
4885 * exception. */
4886 leave_cleanup(&cs);
4887# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 }
4889 else
4890 handle_swap_exists(NULL);
4891#endif
4892 }
4893
4894 ui_breakcheck();
4895 if (got_int)
4896 {
4897 (void)vgetc(); /* only break the file loading, not the rest */
4898 break;
4899 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004900#ifdef FEAT_EVAL
4901 /* Autocommands deleted the buffer or aborted script processing!!! */
4902 if (aborting())
4903 break;
4904#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004905#ifdef FEAT_WINDOWS
4906 /* When ":tab" was used open a new tab for a new window repeatedly. */
4907 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4908 cmdmod.tab = 9999;
4909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
4911#ifdef FEAT_AUTOCMD
4912 --autocmd_no_enter;
4913#endif
4914 win_enter(firstwin, FALSE); /* back to first window */
4915#ifdef FEAT_AUTOCMD
4916 --autocmd_no_leave;
4917#endif
4918
4919 /*
4920 * Close superfluous windows.
4921 */
4922 for (wp = lastwin; open_wins > count; )
4923 {
4924 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4925 || autowrite(wp->w_buffer, FALSE) == OK);
4926#ifdef FEAT_AUTOCMD
4927 if (!win_valid(wp))
4928 {
4929 /* BufWrite Autocommands made the window invalid, start over */
4930 wp = lastwin;
4931 }
4932 else
4933#endif
4934 if (r)
4935 {
4936 win_close(wp, !P_HID(wp->w_buffer));
4937 --open_wins;
4938 wp = lastwin;
4939 }
4940 else
4941 {
4942 wp = wp->w_prev;
4943 if (wp == NULL)
4944 break;
4945 }
4946 }
4947}
4948# endif /* FEAT_LISTCMDS */
4949
4950#endif /* FEAT_WINDOWS */
4951
Bram Moolenaara3227e22006-03-08 21:32:40 +00004952static int chk_modeline __ARGS((linenr_T, int));
4953
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954/*
4955 * do_modelines() - process mode lines for the current file
4956 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004957 * "flags" can be:
4958 * OPT_WINONLY only set options local to window
4959 * OPT_NOWIN don't set options local to window
4960 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961 * Returns immediately if the "ml" option isn't set.
4962 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004964do_modelines(flags)
4965 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004967 linenr_T lnum;
4968 int nmlines;
4969 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970
4971 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4972 return;
4973
4974 /* Disallow recursive entry here. Can happen when executing a modeline
4975 * triggers an autocommand, which reloads modelines with a ":do". */
4976 if (entered)
4977 return;
4978
4979 ++entered;
4980 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4981 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004982 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 nmlines = 0;
4984
4985 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4986 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004987 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 nmlines = 0;
4989 --entered;
4990}
4991
4992#include "version.h" /* for version number */
4993
4994/*
4995 * chk_modeline() - check a single line for a mode string
4996 * Return FAIL if an error encountered.
4997 */
4998 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004999chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00005001 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002{
5003 char_u *s;
5004 char_u *e;
5005 char_u *linecopy; /* local copy of any modeline found */
5006 int prev;
5007 int vers;
5008 int end;
5009 int retval = OK;
5010 char_u *save_sourcing_name;
5011 linenr_T save_sourcing_lnum;
5012#ifdef FEAT_EVAL
5013 scid_T save_SID;
5014#endif
5015
5016 prev = -1;
5017 for (s = ml_get(lnum); *s != NUL; ++s)
5018 {
5019 if (prev == -1 || vim_isspace(prev))
5020 {
5021 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5022 || STRNCMP(s, "vi:", (size_t)3) == 0)
5023 break;
5024 if (STRNCMP(s, "vim", 3) == 0)
5025 {
5026 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5027 e = s + 4;
5028 else
5029 e = s + 3;
5030 vers = getdigits(&e);
5031 if (*e == ':'
5032 && (s[3] == ':'
5033 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5034 || (VIM_VERSION_100 < vers && s[3] == '<')
5035 || (VIM_VERSION_100 > vers && s[3] == '>')
5036 || (VIM_VERSION_100 == vers && s[3] == '=')))
5037 break;
5038 }
5039 }
5040 prev = *s;
5041 }
5042
5043 if (*s)
5044 {
5045 do /* skip over "ex:", "vi:" or "vim:" */
5046 ++s;
5047 while (s[-1] != ':');
5048
5049 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5050 if (linecopy == NULL)
5051 return FAIL;
5052
5053 save_sourcing_lnum = sourcing_lnum;
5054 save_sourcing_name = sourcing_name;
5055 sourcing_lnum = lnum; /* prepare for emsg() */
5056 sourcing_name = (char_u *)"modelines";
5057
5058 end = FALSE;
5059 while (end == FALSE)
5060 {
5061 s = skipwhite(s);
5062 if (*s == NUL)
5063 break;
5064
5065 /*
5066 * Find end of set command: ':' or end of line.
5067 * Skip over "\:", replacing it with ":".
5068 */
5069 for (e = s; *e != ':' && *e != NUL; ++e)
5070 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005071 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 if (*e == NUL)
5073 end = TRUE;
5074
5075 /*
5076 * If there is a "set" command, require a terminating ':' and
5077 * ignore the stuff after the ':'.
5078 * "vi:set opt opt opt: foo" -- foo not interpreted
5079 * "vi:opt opt opt: foo" -- foo interpreted
5080 * Accept "se" for compatibility with Elvis.
5081 */
5082 if (STRNCMP(s, "set ", (size_t)4) == 0
5083 || STRNCMP(s, "se ", (size_t)3) == 0)
5084 {
5085 if (*e != ':') /* no terminating ':'? */
5086 break;
5087 end = TRUE;
5088 s = vim_strchr(s, ' ') + 1;
5089 }
5090 *e = NUL; /* truncate the set command */
5091
5092 if (*s != NUL) /* skip over an empty "::" */
5093 {
5094#ifdef FEAT_EVAL
5095 save_SID = current_SID;
5096 current_SID = SID_MODELINE;
5097#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005098 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099#ifdef FEAT_EVAL
5100 current_SID = save_SID;
5101#endif
5102 if (retval == FAIL) /* stop if error found */
5103 break;
5104 }
5105 s = e + 1; /* advance to next part */
5106 }
5107
5108 sourcing_lnum = save_sourcing_lnum;
5109 sourcing_name = save_sourcing_name;
5110
5111 vim_free(linecopy);
5112 }
5113 return retval;
5114}
5115
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005116#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 int
5118read_viminfo_bufferlist(virp, writing)
5119 vir_T *virp;
5120 int writing;
5121{
5122 char_u *tab;
5123 linenr_T lnum;
5124 colnr_T col;
5125 buf_T *buf;
5126 char_u *sfname;
5127 char_u *xline;
5128
5129 /* Handle long line and escaped characters. */
5130 xline = viminfo_readstring(virp, 1, FALSE);
5131
5132 /* don't read in if there are files on the command-line or if writing: */
5133 if (xline != NULL && !writing && ARGCOUNT == 0
5134 && find_viminfo_parameter('%') != NULL)
5135 {
5136 /* Format is: <fname> Tab <lnum> Tab <col>.
5137 * Watch out for a Tab in the file name, work from the end. */
5138 lnum = 0;
5139 col = 0;
5140 tab = vim_strrchr(xline, '\t');
5141 if (tab != NULL)
5142 {
5143 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005144 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 tab = vim_strrchr(xline, '\t');
5146 if (tab != NULL)
5147 {
5148 *tab++ = '\0';
5149 lnum = atol((char *)tab);
5150 }
5151 }
5152
5153 /* Expand "~/" in the file name at "line + 1" to a full path.
5154 * Then try shortening it by comparing with the current directory */
5155 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005156 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5159 if (buf != NULL) /* just in case... */
5160 {
5161 buf->b_last_cursor.lnum = lnum;
5162 buf->b_last_cursor.col = col;
5163 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5164 }
5165 }
5166 vim_free(xline);
5167
5168 return viminfo_readline(virp);
5169}
5170
5171 void
5172write_viminfo_bufferlist(fp)
5173 FILE *fp;
5174{
5175 buf_T *buf;
5176#ifdef FEAT_WINDOWS
5177 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005178 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179#endif
5180 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005181 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182
5183 if (find_viminfo_parameter('%') == NULL)
5184 return;
5185
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005186 /* Without a number -1 is returned: do all buffers. */
5187 max_buffers = get_viminfo_parameter('%');
5188
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005190#define LINE_BUF_LEN (MAXPATHL + 40)
5191 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (line == NULL)
5193 return;
5194
5195#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005196 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 set_last_cursor(win);
5198#else
5199 set_last_cursor(curwin);
5200#endif
5201
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005202 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5204 {
5205 if (buf->b_fname == NULL
5206 || !buf->b_p_bl
5207#ifdef FEAT_QUICKFIX
5208 || bt_quickfix(buf)
5209#endif
5210 || removable(buf->b_ffname))
5211 continue;
5212
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005213 if (max_buffers-- == 0)
5214 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 putc('%', fp);
5216 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005217 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 (long)buf->b_last_cursor.lnum,
5219 buf->b_last_cursor.col);
5220 viminfo_writestring(fp, line);
5221 }
5222 vim_free(line);
5223}
5224#endif
5225
5226
5227/*
5228 * Return special buffer name.
5229 * Returns NULL when the buffer has a normal file name.
5230 */
5231 char *
5232buf_spname(buf)
5233 buf_T *buf;
5234{
5235#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5236 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005237 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005238 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005239 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005240
5241 /*
5242 * For location list window, w_llist_ref points to the location list.
5243 * For quickfix window, w_llist_ref is NULL.
5244 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005245 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005246 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005247 goto win_found;
5248win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005249 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005250 return _(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005251 else
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005252 return _(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254#endif
5255#ifdef FEAT_QUICKFIX
5256 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5257 * contains the name as specified by the user */
5258 if (bt_nofile(buf))
5259 {
5260 if (buf->b_sfname != NULL)
5261 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005262 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 }
5264#endif
5265 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005266 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005267 return NULL;
5268}
5269
5270
5271#if defined(FEAT_SIGNS) || defined(PROTO)
5272/*
5273 * Insert the sign into the signlist.
5274 */
5275 static void
5276insert_sign(buf, prev, next, id, lnum, typenr)
5277 buf_T *buf; /* buffer to store sign in */
5278 signlist_T *prev; /* previous sign entry */
5279 signlist_T *next; /* next sign entry */
5280 int id; /* sign ID */
5281 linenr_T lnum; /* line number which gets the mark */
5282 int typenr; /* typenr of sign we are adding */
5283{
5284 signlist_T *newsign;
5285
5286 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5287 if (newsign != NULL)
5288 {
5289 newsign->id = id;
5290 newsign->lnum = lnum;
5291 newsign->typenr = typenr;
5292 newsign->next = next;
5293#ifdef FEAT_NETBEANS_INTG
5294 newsign->prev = prev;
5295 if (next != NULL)
5296 next->prev = newsign;
5297#endif
5298
5299 if (prev == NULL)
5300 {
5301 /* When adding first sign need to redraw the windows to create the
5302 * column for signs. */
5303 if (buf->b_signlist == NULL)
5304 {
5305 redraw_buf_later(buf, NOT_VALID);
5306 changed_cline_bef_curs();
5307 }
5308
5309 /* first sign in signlist */
5310 buf->b_signlist = newsign;
5311 }
5312 else
5313 prev->next = newsign;
5314 }
5315}
5316
5317/*
5318 * Add the sign into the signlist. Find the right spot to do it though.
5319 */
5320 void
5321buf_addsign(buf, id, lnum, typenr)
5322 buf_T *buf; /* buffer to store sign in */
5323 int id; /* sign ID */
5324 linenr_T lnum; /* line number which gets the mark */
5325 int typenr; /* typenr of sign we are adding */
5326{
5327 signlist_T *sign; /* a sign in the signlist */
5328 signlist_T *prev; /* the previous sign */
5329
5330 prev = NULL;
5331 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5332 {
5333 if (lnum == sign->lnum && id == sign->id)
5334 {
5335 sign->typenr = typenr;
5336 return;
5337 }
5338 else if (
5339#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5340 id < 0 &&
5341#endif
5342 lnum < sign->lnum)
5343 {
5344#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5345 /* XXX - GRP: Is this because of sign slide problem? Or is it
5346 * really needed? Or is it because we allow multiple signs per
5347 * line? If so, should I add that feature to FEAT_SIGNS?
5348 */
5349 while (prev != NULL && prev->lnum == lnum)
5350 prev = prev->prev;
5351 if (prev == NULL)
5352 sign = buf->b_signlist;
5353 else
5354 sign = prev->next;
5355#endif
5356 insert_sign(buf, prev, sign, id, lnum, typenr);
5357 return;
5358 }
5359 prev = sign;
5360 }
5361#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5362 /* XXX - GRP: See previous comment */
5363 while (prev != NULL && prev->lnum == lnum)
5364 prev = prev->prev;
5365 if (prev == NULL)
5366 sign = buf->b_signlist;
5367 else
5368 sign = prev->next;
5369#endif
5370 insert_sign(buf, prev, sign, id, lnum, typenr);
5371
5372 return;
5373}
5374
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005375 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376buf_change_sign_type(buf, markId, typenr)
5377 buf_T *buf; /* buffer to store sign in */
5378 int markId; /* sign ID */
5379 int typenr; /* typenr of sign we are adding */
5380{
5381 signlist_T *sign; /* a sign in the signlist */
5382
5383 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5384 {
5385 if (sign->id == markId)
5386 {
5387 sign->typenr = typenr;
5388 return sign->lnum;
5389 }
5390 }
5391
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005392 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393}
5394
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005395 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396buf_getsigntype(buf, lnum, type)
5397 buf_T *buf;
5398 linenr_T lnum;
5399 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5400{
5401 signlist_T *sign; /* a sign in a b_signlist */
5402
5403 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5404 if (sign->lnum == lnum
5405 && (type == SIGN_ANY
5406# ifdef FEAT_SIGN_ICONS
5407 || (type == SIGN_ICON
5408 && sign_get_image(sign->typenr) != NULL)
5409# endif
5410 || (type == SIGN_TEXT
5411 && sign_get_text(sign->typenr) != NULL)
5412 || (type == SIGN_LINEHL
5413 && sign_get_attr(sign->typenr, TRUE) != 0)))
5414 return sign->typenr;
5415 return 0;
5416}
5417
5418
5419 linenr_T
5420buf_delsign(buf, id)
5421 buf_T *buf; /* buffer sign is stored in */
5422 int id; /* sign id */
5423{
5424 signlist_T **lastp; /* pointer to pointer to current sign */
5425 signlist_T *sign; /* a sign in a b_signlist */
5426 signlist_T *next; /* the next sign in a b_signlist */
5427 linenr_T lnum; /* line number whose sign was deleted */
5428
5429 lastp = &buf->b_signlist;
5430 lnum = 0;
5431 for (sign = buf->b_signlist; sign != NULL; sign = next)
5432 {
5433 next = sign->next;
5434 if (sign->id == id)
5435 {
5436 *lastp = next;
5437#ifdef FEAT_NETBEANS_INTG
5438 if (next != NULL)
5439 next->prev = sign->prev;
5440#endif
5441 lnum = sign->lnum;
5442 vim_free(sign);
5443 break;
5444 }
5445 else
5446 lastp = &sign->next;
5447 }
5448
5449 /* When deleted the last sign need to redraw the windows to remove the
5450 * sign column. */
5451 if (buf->b_signlist == NULL)
5452 {
5453 redraw_buf_later(buf, NOT_VALID);
5454 changed_cline_bef_curs();
5455 }
5456
5457 return lnum;
5458}
5459
5460
5461/*
5462 * Find the line number of the sign with the requested id. If the sign does
5463 * not exist, return 0 as the line number. This will still let the correct file
5464 * get loaded.
5465 */
5466 int
5467buf_findsign(buf, id)
5468 buf_T *buf; /* buffer to store sign in */
5469 int id; /* sign ID */
5470{
5471 signlist_T *sign; /* a sign in the signlist */
5472
5473 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5474 if (sign->id == id)
5475 return sign->lnum;
5476
5477 return 0;
5478}
5479
5480 int
5481buf_findsign_id(buf, lnum)
5482 buf_T *buf; /* buffer whose sign we are searching for */
5483 linenr_T lnum; /* line number of sign */
5484{
5485 signlist_T *sign; /* a sign in the signlist */
5486
5487 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5488 if (sign->lnum == lnum)
5489 return sign->id;
5490
5491 return 0;
5492}
5493
5494
5495# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5496/* see if a given type of sign exists on a specific line */
5497 int
5498buf_findsigntype_id(buf, lnum, typenr)
5499 buf_T *buf; /* buffer whose sign we are searching for */
5500 linenr_T lnum; /* line number of sign */
5501 int typenr; /* sign type number */
5502{
5503 signlist_T *sign; /* a sign in the signlist */
5504
5505 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5506 if (sign->lnum == lnum && sign->typenr == typenr)
5507 return sign->id;
5508
5509 return 0;
5510}
5511
5512
5513# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5514/* return the number of icons on the given line */
5515 int
5516buf_signcount(buf, lnum)
5517 buf_T *buf;
5518 linenr_T lnum;
5519{
5520 signlist_T *sign; /* a sign in the signlist */
5521 int count = 0;
5522
5523 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5524 if (sign->lnum == lnum)
5525 if (sign_get_image(sign->typenr) != NULL)
5526 count++;
5527
5528 return count;
5529}
5530# endif /* FEAT_SIGN_ICONS */
5531# endif /* FEAT_NETBEANS_INTG */
5532
5533
5534/*
5535 * Delete signs in buffer "buf".
5536 */
5537 static void
5538buf_delete_signs(buf)
5539 buf_T *buf;
5540{
5541 signlist_T *next;
5542
5543 while (buf->b_signlist != NULL)
5544 {
5545 next = buf->b_signlist->next;
5546 vim_free(buf->b_signlist);
5547 buf->b_signlist = next;
5548 }
5549}
5550
5551/*
5552 * Delete all signs in all buffers.
5553 */
5554 void
5555buf_delete_all_signs()
5556{
5557 buf_T *buf; /* buffer we are checking for signs */
5558
5559 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5560 if (buf->b_signlist != NULL)
5561 {
5562 /* Need to redraw the windows to remove the sign column. */
5563 redraw_buf_later(buf, NOT_VALID);
5564 buf_delete_signs(buf);
5565 }
5566}
5567
5568/*
5569 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5570 */
5571 void
5572sign_list_placed(rbuf)
5573 buf_T *rbuf;
5574{
5575 buf_T *buf;
5576 signlist_T *p;
5577 char lbuf[BUFSIZ];
5578
5579 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5580 msg_putchar('\n');
5581 if (rbuf == NULL)
5582 buf = firstbuf;
5583 else
5584 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005585 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586 {
5587 if (buf->b_signlist != NULL)
5588 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005589 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5591 msg_putchar('\n');
5592 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005593 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005595 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5597 MSG_PUTS(lbuf);
5598 msg_putchar('\n');
5599 }
5600 if (rbuf != NULL)
5601 break;
5602 buf = buf->b_next;
5603 }
5604}
5605
5606/*
5607 * Adjust a placed sign for inserted/deleted lines.
5608 */
5609 void
5610sign_mark_adjust(line1, line2, amount, amount_after)
5611 linenr_T line1;
5612 linenr_T line2;
5613 long amount;
5614 long amount_after;
5615{
5616 signlist_T *sign; /* a sign in a b_signlist */
5617
5618 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5619 {
5620 if (sign->lnum >= line1 && sign->lnum <= line2)
5621 {
5622 if (amount == MAXLNUM)
5623 sign->lnum = line1;
5624 else
5625 sign->lnum += amount;
5626 }
5627 else if (sign->lnum > line2)
5628 sign->lnum += amount_after;
5629 }
5630}
5631#endif /* FEAT_SIGNS */
5632
5633/*
5634 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5635 */
5636 void
5637set_buflisted(on)
5638 int on;
5639{
5640 if (on != curbuf->b_p_bl)
5641 {
5642 curbuf->b_p_bl = on;
5643#ifdef FEAT_AUTOCMD
5644 if (on)
5645 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5646 else
5647 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5648#endif
5649 }
5650}
5651
5652/*
5653 * Read the file for "buf" again and check if the contents changed.
5654 * Return TRUE if it changed or this could not be checked.
5655 */
5656 int
5657buf_contents_changed(buf)
5658 buf_T *buf;
5659{
5660 buf_T *newbuf;
5661 int differ = TRUE;
5662 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005664 exarg_T ea;
5665
5666 /* Allocate a buffer without putting it in the buffer list. */
5667 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5668 if (newbuf == NULL)
5669 return TRUE;
5670
5671 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5672 if (prep_exarg(&ea, buf) == FAIL)
5673 {
5674 wipe_buffer(newbuf, FALSE);
5675 return TRUE;
5676 }
5677
Bram Moolenaar071d4272004-06-13 20:20:40 +00005678 /* set curwin/curbuf to buf and save a few things */
5679 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680
Bram Moolenaar4770d092006-01-12 23:22:24 +00005681 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 && readfile(buf->b_ffname, buf->b_fname,
5683 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5684 &ea, READ_NEW | READ_DUMMY) == OK)
5685 {
5686 /* compare the two files line by line */
5687 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5688 {
5689 differ = FALSE;
5690 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5691 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5692 {
5693 differ = TRUE;
5694 break;
5695 }
5696 }
5697 }
5698 vim_free(ea.cmd);
5699
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 /* restore curwin/curbuf and a few other things */
5701 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702
5703 if (curbuf != newbuf) /* safety check */
5704 wipe_buffer(newbuf, FALSE);
5705
5706 return differ;
5707}
5708
5709/*
5710 * Wipe out a buffer and decrement the last buffer number if it was used for
5711 * this buffer. Call this to wipe out a temp buffer that does not contain any
5712 * marks.
5713 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 void
5715wipe_buffer(buf, aucmd)
5716 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005717 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 if (buf->b_fnum == top_file_num - 1)
5720 --top_file_num;
5721
5722#ifdef FEAT_AUTOCMD
5723 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005724 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005726 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727#ifdef FEAT_AUTOCMD
5728 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005729 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730#endif
5731}