blob: 00d1f3535106a838bc6e91f232108c32b64b4f23 [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;
1366 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1367 || action == DOBUF_WIPE);
1368
1369 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001370 if (!cmdmod.keepalt)
1371 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001372 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373
1374#ifdef FEAT_VISUAL
1375 /* Don't restart Select mode after switching to another buffer. */
1376 VIsual_reselect = FALSE;
1377#endif
1378
1379 /* close_windows() or apply_autocmds() may change curbuf */
1380 prevbuf = curbuf;
1381
1382#ifdef FEAT_AUTOCMD
1383 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1384# ifdef FEAT_EVAL
1385 if (buf_valid(prevbuf) && !aborting())
1386# else
1387 if (buf_valid(prevbuf))
1388# endif
1389#endif
1390 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001391#ifdef FEAT_SYN_HL
1392 if (prevbuf == curwin->w_buffer)
1393 reset_synblock(curwin);
1394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#ifdef FEAT_WINDOWS
1396 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001397 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398#endif
1399#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1400 if (buf_valid(prevbuf) && !aborting())
1401#else
1402 if (buf_valid(prevbuf))
1403#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001404 {
1405 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001406 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1408 unload ? action : (action == DOBUF_GOTO
1409 && !P_HID(prevbuf)
Bram Moolenaar42ec6562012-02-22 14:58:37 +01001410 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001411 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 }
1413#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001414 /* An autocommand may have deleted "buf", already entered it (e.g., when
1415 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001417 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001419 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420# endif
1421#endif
1422 enter_buffer(buf);
1423}
1424
1425/*
1426 * Enter a new current buffer.
1427 * Old curbuf must have been abandoned already!
1428 */
1429 void
1430enter_buffer(buf)
1431 buf_T *buf;
1432{
1433 /* Copy buffer and window local option values. Not for a help buffer. */
1434 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1435 if (!buf->b_help)
1436 get_winopts(buf);
1437#ifdef FEAT_FOLDING
1438 else
1439 /* Remove all folds in the window. */
1440 clearFolding(curwin);
1441 foldUpdateAll(curwin); /* update folds (later). */
1442#endif
1443
1444 /* Get the buffer in the current window. */
1445 curwin->w_buffer = buf;
1446 curbuf = buf;
1447 ++curbuf->b_nwindows;
1448
1449#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001450 if (curwin->w_p_diff)
1451 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452#endif
1453
Bram Moolenaara971b822011-09-14 14:43:25 +02001454#ifdef FEAT_SYN_HL
1455 curwin->w_s = &(buf->b_s);
1456#endif
1457
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 /* Cursor on first line by default. */
1459 curwin->w_cursor.lnum = 1;
1460 curwin->w_cursor.col = 0;
1461#ifdef FEAT_VIRTUALEDIT
1462 curwin->w_cursor.coladd = 0;
1463#endif
1464 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001465#ifdef FEAT_AUTOCMD
1466 curwin->w_topline_was_set = FALSE;
1467#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001469 /* mark cursor position as being invalid */
1470 curwin->w_valid = 0;
1471
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 /* Make sure the buffer is loaded. */
1473 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001474 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001475#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001476 /* If there is no filetype, allow for detecting one. Esp. useful for
1477 * ":ball" used in a autocommand. If there already is a filetype we
1478 * might prefer to keep it. */
1479 if (*curbuf->b_p_ft == NUL)
1480 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001481#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001482
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001483 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485 else
1486 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001487 if (!msg_silent)
1488 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1490#ifdef FEAT_AUTOCMD
1491 curwin->w_topline = 1;
1492# ifdef FEAT_DIFF
1493 curwin->w_topfill = 0;
1494# endif
1495 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1496 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1497#endif
1498 }
1499
1500 /* If autocommands did not change the cursor position, restore cursor lnum
1501 * and possibly cursor col. */
1502 if (curwin->w_cursor.lnum == 1 && inindent(0))
1503 buflist_getfpos();
1504
1505 check_arg_idx(curwin); /* check for valid arg_idx */
1506#ifdef FEAT_TITLE
1507 maketitle();
1508#endif
1509#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001510 /* when autocmds didn't change it */
1511 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512#endif
1513 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1514
Bram Moolenaar009b2592004-10-24 19:18:58 +00001515#ifdef FEAT_NETBEANS_INTG
1516 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001517 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001518#endif
1519
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001520 /* Change directories when the 'acd' option is set. */
1521 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522
1523#ifdef FEAT_KEYMAP
1524 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001525 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001527#ifdef FEAT_SPELL
1528 /* May need to set the spell language. Can only do this after the buffer
1529 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001530 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1531 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001532#endif
1533
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534 redraw_later(NOT_VALID);
1535}
1536
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001537#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1538/*
1539 * Change to the directory of the current buffer.
1540 */
1541 void
1542do_autochdir()
1543{
1544 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1545 shorten_fnames(TRUE);
1546}
1547#endif
1548
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549/*
1550 * functions for dealing with the buffer list
1551 */
1552
1553/*
1554 * Add a file name to the buffer list. Return a pointer to the buffer.
1555 * If the same file name already exists return a pointer to that buffer.
1556 * If it does not exist, or if fname == NULL, a new entry is created.
1557 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1558 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1559 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 * This is the ONLY way to create a new buffer.
1561 */
1562static int top_file_num = 1; /* highest file number */
1563
1564 buf_T *
1565buflist_new(ffname, sfname, lnum, flags)
1566 char_u *ffname; /* full path of fname or relative */
1567 char_u *sfname; /* short fname or NULL */
1568 linenr_T lnum; /* preferred cursor line */
1569 int flags; /* BLN_ defines */
1570{
1571 buf_T *buf;
1572#ifdef UNIX
1573 struct stat st;
1574#endif
1575
1576 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1577
1578 /*
1579 * If file name already exists in the list, update the entry.
1580 */
1581#ifdef UNIX
1582 /* On Unix we can use inode numbers when the file exists. Works better
1583 * for hard links. */
1584 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1585 st.st_dev = (dev_T)-1;
1586#endif
1587 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1588#ifdef UNIX
1589 buflist_findname_stat(ffname, &st)
1590#else
1591 buflist_findname(ffname)
1592#endif
1593 ) != NULL)
1594 {
1595 vim_free(ffname);
1596 if (lnum != 0)
1597 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1598 /* copy the options now, if 'cpo' doesn't have 's' and not done
1599 * already */
1600 buf_copy_options(buf, 0);
1601 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1602 {
1603 buf->b_p_bl = TRUE;
1604#ifdef FEAT_AUTOCMD
1605 if (!(flags & BLN_DUMMY))
1606 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1607#endif
1608 }
1609 return buf;
1610 }
1611
1612 /*
1613 * If the current buffer has no name and no contents, use the current
1614 * buffer. Otherwise: Need to allocate a new buffer structure.
1615 *
1616 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001617 * (A spell file buffer is allocated in spell.c, but that's not a normal
1618 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 */
1620 buf = NULL;
1621 if ((flags & BLN_CURBUF)
1622 && curbuf != NULL
1623 && curbuf->b_ffname == NULL
1624 && curbuf->b_nwindows <= 1
1625 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1626 {
1627 buf = curbuf;
1628#ifdef FEAT_AUTOCMD
1629 /* It's like this buffer is deleted. Watch out for autocommands that
1630 * change curbuf! If that happens, allocate a new buffer anyway. */
1631 if (curbuf->b_p_bl)
1632 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1633 if (buf == curbuf)
1634 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1635# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001636 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 return NULL;
1638# endif
1639#endif
1640#ifdef FEAT_QUICKFIX
1641# ifdef FEAT_AUTOCMD
1642 if (buf == curbuf)
1643# endif
1644 {
1645 /* Make sure 'bufhidden' and 'buftype' are empty */
1646 clear_string_option(&buf->b_p_bh);
1647 clear_string_option(&buf->b_p_bt);
1648 }
1649#endif
1650 }
1651 if (buf != curbuf || curbuf == NULL)
1652 {
1653 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1654 if (buf == NULL)
1655 {
1656 vim_free(ffname);
1657 return NULL;
1658 }
1659 }
1660
1661 if (ffname != NULL)
1662 {
1663 buf->b_ffname = ffname;
1664 buf->b_sfname = vim_strsave(sfname);
1665 }
1666
1667 clear_wininfo(buf);
1668 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1669
1670 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1671 || buf->b_wininfo == NULL)
1672 {
1673 vim_free(buf->b_ffname);
1674 buf->b_ffname = NULL;
1675 vim_free(buf->b_sfname);
1676 buf->b_sfname = NULL;
1677 if (buf != curbuf)
1678 free_buffer(buf);
1679 return NULL;
1680 }
1681
1682 if (buf == curbuf)
1683 {
1684 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001685 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (buf != curbuf) /* autocommands deleted the buffer! */
1687 return NULL;
1688#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001689 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 return NULL;
1691#endif
1692 /* buf->b_nwindows = 0; why was this here? */
1693 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1694#ifdef FEAT_KEYMAP
1695 /* need to reload lmaps and set b:keymap_name */
1696 curbuf->b_kmap_state |= KEYMAP_INIT;
1697#endif
1698 }
1699 else
1700 {
1701 /*
1702 * put new buffer at the end of the buffer list
1703 */
1704 buf->b_next = NULL;
1705 if (firstbuf == NULL) /* buffer list is empty */
1706 {
1707 buf->b_prev = NULL;
1708 firstbuf = buf;
1709 }
1710 else /* append new buffer at end of list */
1711 {
1712 lastbuf->b_next = buf;
1713 buf->b_prev = lastbuf;
1714 }
1715 lastbuf = buf;
1716
1717 buf->b_fnum = top_file_num++;
1718 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1719 {
1720 EMSG(_("W14: Warning: List of file names overflow"));
1721 if (emsg_silent == 0)
1722 {
1723 out_flush();
1724 ui_delay(3000L, TRUE); /* make sure it is noticed */
1725 }
1726 top_file_num = 1;
1727 }
1728
1729 /*
1730 * Always copy the options from the current buffer.
1731 */
1732 buf_copy_options(buf, BCO_ALWAYS);
1733 }
1734
1735 buf->b_wininfo->wi_fpos.lnum = lnum;
1736 buf->b_wininfo->wi_win = curwin;
1737
1738#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001739 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1740#endif
1741#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001742 hash_init(&buf->b_s.b_keywtab);
1743 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744#endif
1745
1746 buf->b_fname = buf->b_sfname;
1747#ifdef UNIX
1748 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001749 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 else
1751 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001752 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001753 buf->b_dev = st.st_dev;
1754 buf->b_ino = st.st_ino;
1755 }
1756#endif
1757 buf->b_u_synced = TRUE;
1758 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001759 if (flags & BLN_DUMMY)
1760 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001761 buf_clear_file(buf);
1762 clrallmarks(buf); /* clear marks */
1763 fmarks_check_names(buf); /* check file marks for this file */
1764 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1765#ifdef FEAT_AUTOCMD
1766 if (!(flags & BLN_DUMMY))
1767 {
1768 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1769 if (flags & BLN_LISTED)
1770 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1771# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001772 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 return NULL;
1774# endif
1775 }
1776#endif
1777
1778 return buf;
1779}
1780
1781/*
1782 * Free the memory for the options of a buffer.
1783 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1784 * 'fileencoding'.
1785 */
1786 void
1787free_buf_options(buf, free_p_ff)
1788 buf_T *buf;
1789 int free_p_ff;
1790{
1791 if (free_p_ff)
1792 {
1793#ifdef FEAT_MBYTE
1794 clear_string_option(&buf->b_p_fenc);
1795#endif
1796 clear_string_option(&buf->b_p_ff);
1797#ifdef FEAT_QUICKFIX
1798 clear_string_option(&buf->b_p_bh);
1799 clear_string_option(&buf->b_p_bt);
1800#endif
1801 }
1802#ifdef FEAT_FIND_ID
1803 clear_string_option(&buf->b_p_def);
1804 clear_string_option(&buf->b_p_inc);
1805# ifdef FEAT_EVAL
1806 clear_string_option(&buf->b_p_inex);
1807# endif
1808#endif
1809#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1810 clear_string_option(&buf->b_p_inde);
1811 clear_string_option(&buf->b_p_indk);
1812#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001813#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1814 clear_string_option(&buf->b_p_bexpr);
1815#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001816#if defined(FEAT_CRYPT)
1817 clear_string_option(&buf->b_p_cm);
1818#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001819#if defined(FEAT_EVAL)
1820 clear_string_option(&buf->b_p_fex);
1821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822#ifdef FEAT_CRYPT
1823 clear_string_option(&buf->b_p_key);
1824#endif
1825 clear_string_option(&buf->b_p_kp);
1826 clear_string_option(&buf->b_p_mps);
1827 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001828 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 clear_string_option(&buf->b_p_isk);
1830#ifdef FEAT_KEYMAP
1831 clear_string_option(&buf->b_p_keymap);
1832 ga_clear(&buf->b_kmap_ga);
1833#endif
1834#ifdef FEAT_COMMENTS
1835 clear_string_option(&buf->b_p_com);
1836#endif
1837#ifdef FEAT_FOLDING
1838 clear_string_option(&buf->b_p_cms);
1839#endif
1840 clear_string_option(&buf->b_p_nf);
1841#ifdef FEAT_SYN_HL
1842 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001843#endif
1844#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001845 clear_string_option(&buf->b_s.b_p_spc);
1846 clear_string_option(&buf->b_s.b_p_spf);
1847 vim_free(buf->b_s.b_cap_prog);
1848 buf->b_s.b_cap_prog = NULL;
1849 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850#endif
1851#ifdef FEAT_SEARCHPATH
1852 clear_string_option(&buf->b_p_sua);
1853#endif
1854#ifdef FEAT_AUTOCMD
1855 clear_string_option(&buf->b_p_ft);
1856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857#ifdef FEAT_CINDENT
1858 clear_string_option(&buf->b_p_cink);
1859 clear_string_option(&buf->b_p_cino);
1860#endif
1861#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1862 clear_string_option(&buf->b_p_cinw);
1863#endif
1864#ifdef FEAT_INS_EXPAND
1865 clear_string_option(&buf->b_p_cpt);
1866#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001867#ifdef FEAT_COMPL_FUNC
1868 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001869 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#ifdef FEAT_QUICKFIX
1872 clear_string_option(&buf->b_p_gp);
1873 clear_string_option(&buf->b_p_mp);
1874 clear_string_option(&buf->b_p_efm);
1875#endif
1876 clear_string_option(&buf->b_p_ep);
1877 clear_string_option(&buf->b_p_path);
1878 clear_string_option(&buf->b_p_tags);
1879#ifdef FEAT_INS_EXPAND
1880 clear_string_option(&buf->b_p_dict);
1881 clear_string_option(&buf->b_p_tsr);
1882#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001883#ifdef FEAT_TEXTOBJ
1884 clear_string_option(&buf->b_p_qe);
1885#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 buf->b_p_ar = -1;
1887}
1888
1889/*
1890 * get alternate file n
1891 * set linenr to lnum or altfpos.lnum if lnum == 0
1892 * also set cursor column to altfpos.col if 'startofline' is not set.
1893 * if (options & GETF_SETMARK) call setpcmark()
1894 * if (options & GETF_ALT) we are jumping to an alternate file.
1895 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1896 *
1897 * return FAIL for failure, OK for success
1898 */
1899 int
1900buflist_getfile(n, lnum, options, forceit)
1901 int n;
1902 linenr_T lnum;
1903 int options;
1904 int forceit;
1905{
1906 buf_T *buf;
1907#ifdef FEAT_WINDOWS
1908 win_T *wp = NULL;
1909#endif
1910 pos_T *fpos;
1911 colnr_T col;
1912
1913 buf = buflist_findnr(n);
1914 if (buf == NULL)
1915 {
1916 if ((options & GETF_ALT) && n == 0)
1917 EMSG(_(e_noalt));
1918 else
1919 EMSGN(_("E92: Buffer %ld not found"), n);
1920 return FAIL;
1921 }
1922
1923 /* if alternate file is the current buffer, nothing to do */
1924 if (buf == curbuf)
1925 return OK;
1926
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001927 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001928 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001929 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001931 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001932#ifdef FEAT_AUTOCMD
1933 if (curbuf_locked())
1934 return FAIL;
1935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936
1937 /* altfpos may be changed by getfile(), get it now */
1938 if (lnum == 0)
1939 {
1940 fpos = buflist_findfpos(buf);
1941 lnum = fpos->lnum;
1942 col = fpos->col;
1943 }
1944 else
1945 col = 0;
1946
1947#ifdef FEAT_WINDOWS
1948 if (options & GETF_SWITCH)
1949 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001950 /* If 'switchbuf' contains "useopen": jump to first window containing
1951 * "buf" if one exists */
1952 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001954 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1955 * page containing "buf" if one exists */
1956 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001957 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001958 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1959 * isn't empty: open new window */
1960 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001962 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1963 tabpage_new();
1964 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001966 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 }
1969#endif
1970
1971 ++RedrawingDisabled;
1972 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1973 lnum, forceit) <= 0)
1974 {
1975 --RedrawingDisabled;
1976
1977 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1978 if (!p_sol && col != 0)
1979 {
1980 curwin->w_cursor.col = col;
1981 check_cursor_col();
1982#ifdef FEAT_VIRTUALEDIT
1983 curwin->w_cursor.coladd = 0;
1984#endif
1985 curwin->w_set_curswant = TRUE;
1986 }
1987 return OK;
1988 }
1989 --RedrawingDisabled;
1990 return FAIL;
1991}
1992
1993/*
1994 * go to the last know line number for the current buffer
1995 */
1996 void
1997buflist_getfpos()
1998{
1999 pos_T *fpos;
2000
2001 fpos = buflist_findfpos(curbuf);
2002
2003 curwin->w_cursor.lnum = fpos->lnum;
2004 check_cursor_lnum();
2005
2006 if (p_sol)
2007 curwin->w_cursor.col = 0;
2008 else
2009 {
2010 curwin->w_cursor.col = fpos->col;
2011 check_cursor_col();
2012#ifdef FEAT_VIRTUALEDIT
2013 curwin->w_cursor.coladd = 0;
2014#endif
2015 curwin->w_set_curswant = TRUE;
2016 }
2017}
2018
Bram Moolenaar81695252004-12-29 20:58:21 +00002019#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
2020/*
2021 * Find file in buffer list by name (it has to be for the current window).
2022 * Returns NULL if not found.
2023 */
2024 buf_T *
2025buflist_findname_exp(fname)
2026 char_u *fname;
2027{
2028 char_u *ffname;
2029 buf_T *buf = NULL;
2030
2031 /* First make the name into a full path name */
2032 ffname = FullName_save(fname,
2033#ifdef UNIX
2034 TRUE /* force expansion, get rid of symbolic links */
2035#else
2036 FALSE
2037#endif
2038 );
2039 if (ffname != NULL)
2040 {
2041 buf = buflist_findname(ffname);
2042 vim_free(ffname);
2043 }
2044 return buf;
2045}
2046#endif
2047
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048/*
2049 * Find file in buffer list by name (it has to be for the current window).
2050 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002051 * Skips dummy buffers.
2052 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 */
2054 buf_T *
2055buflist_findname(ffname)
2056 char_u *ffname;
2057{
2058#ifdef UNIX
2059 struct stat st;
2060
2061 if (mch_stat((char *)ffname, &st) < 0)
2062 st.st_dev = (dev_T)-1;
2063 return buflist_findname_stat(ffname, &st);
2064}
2065
2066/*
2067 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2068 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002069 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 */
2071 static buf_T *
2072buflist_findname_stat(ffname, stp)
2073 char_u *ffname;
2074 struct stat *stp;
2075{
2076#endif
2077 buf_T *buf;
2078
2079 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002080 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081#ifdef UNIX
2082 , stp
2083#endif
2084 ))
2085 return buf;
2086 return NULL;
2087}
2088
2089#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2090/*
2091 * Find file in buffer list by a regexp pattern.
2092 * Return fnum of the found buffer.
2093 * Return < 0 for error.
2094 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 int
2096buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2097 char_u *pattern;
2098 char_u *pattern_end; /* pointer to first char after pattern */
2099 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002100 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101{
2102 buf_T *buf;
2103 regprog_T *prog;
2104 int match = -1;
2105 int find_listed;
2106 char_u *pat;
2107 char_u *patend;
2108 int attempt;
2109 char_u *p;
2110 int toggledollar;
2111
2112 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2113 {
2114 if (*pattern == '%')
2115 match = curbuf->b_fnum;
2116 else
2117 match = curwin->w_alt_fnum;
2118#ifdef FEAT_DIFF
2119 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2120 match = -1;
2121#endif
2122 }
2123
2124 /*
2125 * Try four ways of matching a listed buffer:
2126 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002127 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002128 * attempt == 2: with '$' at end (only match at end)
2129 * attempt == 3: with '^' at start and '$' at end (only full match)
2130 * Repeat this for finding an unlisted buffer if there was no matching
2131 * listed buffer.
2132 */
2133 else
2134 {
2135 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2136 if (pat == NULL)
2137 return -1;
2138 patend = pat + STRLEN(pat) - 1;
2139 toggledollar = (patend > pat && *patend == '$');
2140
2141 /* First try finding a listed buffer. If not found and "unlisted"
2142 * is TRUE, try finding an unlisted buffer. */
2143 find_listed = TRUE;
2144 for (;;)
2145 {
2146 for (attempt = 0; attempt <= 3; ++attempt)
2147 {
2148 /* may add '^' and '$' */
2149 if (toggledollar)
2150 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2151 p = pat;
2152 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2153 ++p;
2154 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2155 if (prog == NULL)
2156 {
2157 vim_free(pat);
2158 return -1;
2159 }
2160
2161 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2162 if (buf->b_p_bl == find_listed
2163#ifdef FEAT_DIFF
2164 && (!diffmode || diff_mode_buf(buf))
2165#endif
2166 && buflist_match(prog, buf) != NULL)
2167 {
2168 if (match >= 0) /* already found a match */
2169 {
2170 match = -2;
2171 break;
2172 }
2173 match = buf->b_fnum; /* remember first match */
2174 }
2175
2176 vim_free(prog);
2177 if (match >= 0) /* found one match */
2178 break;
2179 }
2180
2181 /* Only search for unlisted buffers if there was no match with
2182 * a listed buffer. */
2183 if (!unlisted || !find_listed || match != -1)
2184 break;
2185 find_listed = FALSE;
2186 }
2187
2188 vim_free(pat);
2189 }
2190
2191 if (match == -2)
2192 EMSG2(_("E93: More than one match for %s"), pattern);
2193 else if (match < 0)
2194 EMSG2(_("E94: No matching buffer for %s"), pattern);
2195 return match;
2196}
2197#endif
2198
2199#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2200
2201/*
2202 * Find all buffer names that match.
2203 * For command line expansion of ":buf" and ":sbuf".
2204 * Return OK if matches found, FAIL otherwise.
2205 */
2206 int
2207ExpandBufnames(pat, num_file, file, options)
2208 char_u *pat;
2209 int *num_file;
2210 char_u ***file;
2211 int options;
2212{
2213 int count = 0;
2214 buf_T *buf;
2215 int round;
2216 char_u *p;
2217 int attempt;
2218 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002219 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
2221 *num_file = 0; /* return values in case of FAIL */
2222 *file = NULL;
2223
Bram Moolenaar05159a02005-02-26 23:04:13 +00002224 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2225 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002227 patc = alloc((unsigned)STRLEN(pat) + 11);
2228 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002230 STRCPY(patc, "\\(^\\|[\\/]\\)");
2231 STRCPY(patc + 11, pat + 1);
2232 }
2233 else
2234 patc = pat;
2235
2236 /*
2237 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002238 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002239 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002240 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002241 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002242 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002243 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002244 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002245 if (prog == NULL)
2246 {
2247 if (patc != pat)
2248 vim_free(patc);
2249 return FAIL;
2250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
2252 /*
2253 * round == 1: Count the matches.
2254 * round == 2: Build the array to keep the matches.
2255 */
2256 for (round = 1; round <= 2; ++round)
2257 {
2258 count = 0;
2259 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2260 {
2261 if (!buf->b_p_bl) /* skip unlisted buffers */
2262 continue;
2263 p = buflist_match(prog, buf);
2264 if (p != NULL)
2265 {
2266 if (round == 1)
2267 ++count;
2268 else
2269 {
2270 if (options & WILD_HOME_REPLACE)
2271 p = home_replace_save(buf, p);
2272 else
2273 p = vim_strsave(p);
2274 (*file)[count++] = p;
2275 }
2276 }
2277 }
2278 if (count == 0) /* no match found, break here */
2279 break;
2280 if (round == 1)
2281 {
2282 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2283 if (*file == NULL)
2284 {
2285 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002286 if (patc != pat)
2287 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 return FAIL;
2289 }
2290 }
2291 }
2292 vim_free(prog);
2293 if (count) /* match(es) found, break here */
2294 break;
2295 }
2296
Bram Moolenaar05159a02005-02-26 23:04:13 +00002297 if (patc != pat)
2298 vim_free(patc);
2299
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 *num_file = count;
2301 return (count == 0 ? FAIL : OK);
2302}
2303
2304#endif /* FEAT_CMDL_COMPL */
2305
2306#ifdef HAVE_BUFLIST_MATCH
2307/*
2308 * Check for a match on the file name for buffer "buf" with regprog "prog".
2309 */
2310 static char_u *
2311buflist_match(prog, buf)
2312 regprog_T *prog;
2313 buf_T *buf;
2314{
2315 char_u *match;
2316
2317 /* First try the short file name, then the long file name. */
2318 match = fname_match(prog, buf->b_sfname);
2319 if (match == NULL)
2320 match = fname_match(prog, buf->b_ffname);
2321
2322 return match;
2323}
2324
2325/*
2326 * Try matching the regexp in "prog" with file name "name".
2327 * Return "name" when there is a match, NULL when not.
2328 */
2329 static char_u *
2330fname_match(prog, name)
2331 regprog_T *prog;
2332 char_u *name;
2333{
2334 char_u *match = NULL;
2335 char_u *p;
2336 regmatch_T regmatch;
2337
2338 if (name != NULL)
2339 {
2340 regmatch.regprog = prog;
2341#ifdef CASE_INSENSITIVE_FILENAME
2342 regmatch.rm_ic = TRUE; /* Always ignore case */
2343#else
2344 regmatch.rm_ic = FALSE; /* Never ignore case */
2345#endif
2346
2347 if (vim_regexec(&regmatch, name, (colnr_T)0))
2348 match = name;
2349 else
2350 {
2351 /* Replace $(HOME) with '~' and try matching again. */
2352 p = home_replace_save(NULL, name);
2353 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2354 match = name;
2355 vim_free(p);
2356 }
2357 }
2358
2359 return match;
2360}
2361#endif
2362
2363/*
2364 * find file in buffer list by number
2365 */
2366 buf_T *
2367buflist_findnr(nr)
2368 int nr;
2369{
2370 buf_T *buf;
2371
2372 if (nr == 0)
2373 nr = curwin->w_alt_fnum;
2374 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2375 if (buf->b_fnum == nr)
2376 return (buf);
2377 return NULL;
2378}
2379
2380/*
2381 * Get name of file 'n' in the buffer list.
2382 * When the file has no name an empty string is returned.
2383 * home_replace() is used to shorten the file name (used for marks).
2384 * Returns a pointer to allocated memory, of NULL when failed.
2385 */
2386 char_u *
2387buflist_nr2name(n, fullname, helptail)
2388 int n;
2389 int fullname;
2390 int helptail; /* for help buffers return tail only */
2391{
2392 buf_T *buf;
2393
2394 buf = buflist_findnr(n);
2395 if (buf == NULL)
2396 return NULL;
2397 return home_replace_save(helptail ? buf : NULL,
2398 fullname ? buf->b_ffname : buf->b_fname);
2399}
2400
2401/*
2402 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2403 * When "copy_options" is TRUE save the local window option values.
2404 * When "lnum" is 0 only do the options.
2405 */
2406 static void
2407buflist_setfpos(buf, win, lnum, col, copy_options)
2408 buf_T *buf;
2409 win_T *win;
2410 linenr_T lnum;
2411 colnr_T col;
2412 int copy_options;
2413{
2414 wininfo_T *wip;
2415
2416 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2417 if (wip->wi_win == win)
2418 break;
2419 if (wip == NULL)
2420 {
2421 /* allocate a new entry */
2422 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2423 if (wip == NULL)
2424 return;
2425 wip->wi_win = win;
2426 if (lnum == 0) /* set lnum even when it's 0 */
2427 lnum = 1;
2428 }
2429 else
2430 {
2431 /* remove the entry from the list */
2432 if (wip->wi_prev)
2433 wip->wi_prev->wi_next = wip->wi_next;
2434 else
2435 buf->b_wininfo = wip->wi_next;
2436 if (wip->wi_next)
2437 wip->wi_next->wi_prev = wip->wi_prev;
2438 if (copy_options && wip->wi_optset)
2439 {
2440 clear_winopt(&wip->wi_opt);
2441#ifdef FEAT_FOLDING
2442 deleteFoldRecurse(&wip->wi_folds);
2443#endif
2444 }
2445 }
2446 if (lnum != 0)
2447 {
2448 wip->wi_fpos.lnum = lnum;
2449 wip->wi_fpos.col = col;
2450 }
2451 if (copy_options)
2452 {
2453 /* Save the window-specific option values. */
2454 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2455#ifdef FEAT_FOLDING
2456 wip->wi_fold_manual = win->w_fold_manual;
2457 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2458#endif
2459 wip->wi_optset = TRUE;
2460 }
2461
2462 /* insert the entry in front of the list */
2463 wip->wi_next = buf->b_wininfo;
2464 buf->b_wininfo = wip;
2465 wip->wi_prev = NULL;
2466 if (wip->wi_next)
2467 wip->wi_next->wi_prev = wip;
2468
2469 return;
2470}
2471
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002472#ifdef FEAT_DIFF
2473static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2474
2475/*
2476 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2477 * page. That's because a diff is local to a tab page.
2478 */
2479 static int
2480wininfo_other_tab_diff(wip)
2481 wininfo_T *wip;
2482{
2483 win_T *wp;
2484
2485 if (wip->wi_opt.wo_diff)
2486 {
2487 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2488 /* return FALSE when it's a window in the current tab page, thus
2489 * the buffer was in diff mode here */
2490 if (wip->wi_win == wp)
2491 return FALSE;
2492 return TRUE;
2493 }
2494 return FALSE;
2495}
2496#endif
2497
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498/*
2499 * Find info for the current window in buffer "buf".
2500 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002501 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2502 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * Returns NULL when there isn't any info.
2504 */
2505 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002506find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002508 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509{
2510 wininfo_T *wip;
2511
2512 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002513 if (wip->wi_win == curwin
2514#ifdef FEAT_DIFF
2515 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2516#endif
2517 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002519
2520 /* If no wininfo for curwin, use the first in the list (that doesn't have
2521 * 'diff' set and is in another tab page). */
2522 if (wip == NULL)
2523 {
2524#ifdef FEAT_DIFF
2525 if (skip_diff_buffer)
2526 {
2527 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2528 if (!wininfo_other_tab_diff(wip))
2529 break;
2530 }
2531 else
2532#endif
2533 wip = buf->b_wininfo;
2534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 return wip;
2536}
2537
2538/*
2539 * Reset the local window options to the values last used in this window.
2540 * If the buffer wasn't used in this window before, use the values from
2541 * the most recently used window. If the values were never set, use the
2542 * global values for the window.
2543 */
2544 void
2545get_winopts(buf)
2546 buf_T *buf;
2547{
2548 wininfo_T *wip;
2549
2550 clear_winopt(&curwin->w_onebuf_opt);
2551#ifdef FEAT_FOLDING
2552 clearFolding(curwin);
2553#endif
2554
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002555 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 if (wip != NULL && wip->wi_optset)
2557 {
2558 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2559#ifdef FEAT_FOLDING
2560 curwin->w_fold_manual = wip->wi_fold_manual;
2561 curwin->w_foldinvalid = TRUE;
2562 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2563#endif
2564 }
2565 else
2566 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2567
2568#ifdef FEAT_FOLDING
2569 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2570 if (p_fdls >= 0)
2571 curwin->w_p_fdl = p_fdls;
2572#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002573#ifdef FEAT_SYN_HL
2574 check_colorcolumn(curwin);
2575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576}
2577
2578/*
2579 * Find the position (lnum and col) for the buffer 'buf' for the current
2580 * window.
2581 * Returns a pointer to no_position if no position is found.
2582 */
2583 pos_T *
2584buflist_findfpos(buf)
2585 buf_T *buf;
2586{
2587 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002588 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002590 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 if (wip != NULL)
2592 return &(wip->wi_fpos);
2593 else
2594 return &no_position;
2595}
2596
2597/*
2598 * Find the lnum for the buffer 'buf' for the current window.
2599 */
2600 linenr_T
2601buflist_findlnum(buf)
2602 buf_T *buf;
2603{
2604 return buflist_findfpos(buf)->lnum;
2605}
2606
2607#if defined(FEAT_LISTCMDS) || defined(PROTO)
2608/*
2609 * List all know file names (for :files and :buffers command).
2610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 void
2612buflist_list(eap)
2613 exarg_T *eap;
2614{
2615 buf_T *buf;
2616 int len;
2617 int i;
2618
2619 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2620 {
2621 /* skip unlisted buffers, unless ! was used */
2622 if (!buf->b_p_bl && !eap->forceit)
2623 continue;
2624 msg_putchar('\n');
2625 if (buf_spname(buf) != NULL)
2626 STRCPY(NameBuff, buf_spname(buf));
2627 else
2628 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2629
Bram Moolenaar50cde822005-06-05 21:54:54 +00002630 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002631 buf->b_fnum,
2632 buf->b_p_bl ? ' ' : 'u',
2633 buf == curbuf ? '%' :
2634 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2635 buf->b_ml.ml_mfp == NULL ? ' ' :
2636 (buf->b_nwindows == 0 ? 'h' : 'a'),
2637 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2638 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002639 : (bufIsChanged(buf) ? '+' : ' '),
2640 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002641
2642 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 i = 40 - vim_strsize(IObuff);
2644 do
2645 {
2646 IObuff[len++] = ' ';
2647 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002648 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2649 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002650 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 msg_outtrans(IObuff);
2652 out_flush(); /* output one line at a time */
2653 ui_breakcheck();
2654 }
2655}
2656#endif
2657
2658/*
2659 * Get file name and line number for file 'fnum'.
2660 * Used by DoOneCmd() for translating '%' and '#'.
2661 * Used by insert_reg() and cmdline_paste() for '#' register.
2662 * Return FAIL if not found, OK for success.
2663 */
2664 int
2665buflist_name_nr(fnum, fname, lnum)
2666 int fnum;
2667 char_u **fname;
2668 linenr_T *lnum;
2669{
2670 buf_T *buf;
2671
2672 buf = buflist_findnr(fnum);
2673 if (buf == NULL || buf->b_fname == NULL)
2674 return FAIL;
2675
2676 *fname = buf->b_fname;
2677 *lnum = buflist_findlnum(buf);
2678
2679 return OK;
2680}
2681
2682/*
2683 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2684 * The file name with the full path is also remembered, for when :cd is used.
2685 * Returns FAIL for failure (file name already in use by other buffer)
2686 * OK otherwise.
2687 */
2688 int
2689setfname(buf, ffname, sfname, message)
2690 buf_T *buf;
2691 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002692 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002693{
Bram Moolenaar81695252004-12-29 20:58:21 +00002694 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695#ifdef UNIX
2696 struct stat st;
2697#endif
2698
2699 if (ffname == NULL || *ffname == NUL)
2700 {
2701 /* Removing the name. */
2702 vim_free(buf->b_ffname);
2703 vim_free(buf->b_sfname);
2704 buf->b_ffname = NULL;
2705 buf->b_sfname = NULL;
2706#ifdef UNIX
2707 st.st_dev = (dev_T)-1;
2708#endif
2709 }
2710 else
2711 {
2712 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2713 if (ffname == NULL) /* out of memory */
2714 return FAIL;
2715
2716 /*
2717 * if the file name is already used in another buffer:
2718 * - if the buffer is loaded, fail
2719 * - if the buffer is not loaded, delete it from the list
2720 */
2721#ifdef UNIX
2722 if (mch_stat((char *)ffname, &st) < 0)
2723 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002724#endif
2725 if (!(buf->b_flags & BF_DUMMY))
2726#ifdef UNIX
2727 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002729 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730#endif
2731 if (obuf != NULL && obuf != buf)
2732 {
2733 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2734 {
2735 if (message)
2736 EMSG(_("E95: Buffer with this name already exists"));
2737 vim_free(ffname);
2738 return FAIL;
2739 }
Bram Moolenaar42ec6562012-02-22 14:58:37 +01002740 /* delete from the list */
2741 close_buffer(NULL, obuf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002742 }
2743 sfname = vim_strsave(sfname);
2744 if (ffname == NULL || sfname == NULL)
2745 {
2746 vim_free(sfname);
2747 vim_free(ffname);
2748 return FAIL;
2749 }
2750#ifdef USE_FNAME_CASE
2751# ifdef USE_LONG_FNAME
2752 if (USE_LONG_FNAME)
2753# endif
2754 fname_case(sfname, 0); /* set correct case for short file name */
2755#endif
2756 vim_free(buf->b_ffname);
2757 vim_free(buf->b_sfname);
2758 buf->b_ffname = ffname;
2759 buf->b_sfname = sfname;
2760 }
2761 buf->b_fname = buf->b_sfname;
2762#ifdef UNIX
2763 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002764 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002765 else
2766 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002767 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 buf->b_dev = st.st_dev;
2769 buf->b_ino = st.st_ino;
2770 }
2771#endif
2772
2773#ifndef SHORT_FNAME
2774 buf->b_shortname = FALSE;
2775#endif
2776
2777 buf_name_changed(buf);
2778 return OK;
2779}
2780
2781/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002782 * Crude way of changing the name of a buffer. Use with care!
2783 * The name should be relative to the current directory.
2784 */
2785 void
2786buf_set_name(fnum, name)
2787 int fnum;
2788 char_u *name;
2789{
2790 buf_T *buf;
2791
2792 buf = buflist_findnr(fnum);
2793 if (buf != NULL)
2794 {
2795 vim_free(buf->b_sfname);
2796 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002797 buf->b_ffname = vim_strsave(name);
2798 buf->b_sfname = NULL;
2799 /* Allocate ffname and expand into full path. Also resolves .lnk
2800 * files on Win32. */
2801 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002802 buf->b_fname = buf->b_sfname;
2803 }
2804}
2805
2806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002807 * Take care of what needs to be done when the name of buffer "buf" has
2808 * changed.
2809 */
2810 void
2811buf_name_changed(buf)
2812 buf_T *buf;
2813{
2814 /*
2815 * If the file name changed, also change the name of the swapfile
2816 */
2817 if (buf->b_ml.ml_mfp != NULL)
2818 ml_setname(buf);
2819
2820 if (curwin->w_buffer == buf)
2821 check_arg_idx(curwin); /* check file name for arg list */
2822#ifdef FEAT_TITLE
2823 maketitle(); /* set window title */
2824#endif
2825#ifdef FEAT_WINDOWS
2826 status_redraw_all(); /* status lines need to be redrawn */
2827#endif
2828 fmarks_check_names(buf); /* check named file marks */
2829 ml_timestamp(buf); /* reset timestamp */
2830}
2831
2832/*
2833 * set alternate file name for current window
2834 *
2835 * Used by do_one_cmd(), do_write() and do_ecmd().
2836 * Return the buffer.
2837 */
2838 buf_T *
2839setaltfname(ffname, sfname, lnum)
2840 char_u *ffname;
2841 char_u *sfname;
2842 linenr_T lnum;
2843{
2844 buf_T *buf;
2845
2846 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2847 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002848 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 curwin->w_alt_fnum = buf->b_fnum;
2850 return buf;
2851}
2852
2853/*
2854 * Get alternate file name for current window.
2855 * Return NULL if there isn't any, and give error message if requested.
2856 */
2857 char_u *
2858getaltfname(errmsg)
2859 int errmsg; /* give error message */
2860{
2861 char_u *fname;
2862 linenr_T dummy;
2863
2864 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2865 {
2866 if (errmsg)
2867 EMSG(_(e_noalt));
2868 return NULL;
2869 }
2870 return fname;
2871}
2872
2873/*
2874 * Add a file name to the buflist and return its number.
2875 * Uses same flags as buflist_new(), except BLN_DUMMY.
2876 *
2877 * used by qf_init(), main() and doarglist()
2878 */
2879 int
2880buflist_add(fname, flags)
2881 char_u *fname;
2882 int flags;
2883{
2884 buf_T *buf;
2885
2886 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2887 if (buf != NULL)
2888 return buf->b_fnum;
2889 return 0;
2890}
2891
2892#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2893/*
2894 * Adjust slashes in file names. Called after 'shellslash' was set.
2895 */
2896 void
2897buflist_slash_adjust()
2898{
2899 buf_T *bp;
2900
2901 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2902 {
2903 if (bp->b_ffname != NULL)
2904 slash_adjust(bp->b_ffname);
2905 if (bp->b_sfname != NULL)
2906 slash_adjust(bp->b_sfname);
2907 }
2908}
2909#endif
2910
2911/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002912 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 * Also save the local window option values.
2914 */
2915 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002916buflist_altfpos(win)
2917 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002919 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920}
2921
2922/*
2923 * Return TRUE if 'ffname' is not the same file as current file.
2924 * Fname must have a full path (expanded by mch_FullName()).
2925 */
2926 int
2927otherfile(ffname)
2928 char_u *ffname;
2929{
2930 return otherfile_buf(curbuf, ffname
2931#ifdef UNIX
2932 , NULL
2933#endif
2934 );
2935}
2936
2937 static int
2938otherfile_buf(buf, ffname
2939#ifdef UNIX
2940 , stp
2941#endif
2942 )
2943 buf_T *buf;
2944 char_u *ffname;
2945#ifdef UNIX
2946 struct stat *stp;
2947#endif
2948{
2949 /* no name is different */
2950 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2951 return TRUE;
2952 if (fnamecmp(ffname, buf->b_ffname) == 0)
2953 return FALSE;
2954#ifdef UNIX
2955 {
2956 struct stat st;
2957
2958 /* If no struct stat given, get it now */
2959 if (stp == NULL)
2960 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002961 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 st.st_dev = (dev_T)-1;
2963 stp = &st;
2964 }
2965 /* Use dev/ino to check if the files are the same, even when the names
2966 * are different (possible with links). Still need to compare the
2967 * name above, for when the file doesn't exist yet.
2968 * Problem: The dev/ino changes when a file is deleted (and created
2969 * again) and remains the same when renamed/moved. We don't want to
2970 * mch_stat() each buffer each time, that would be too slow. Get the
2971 * dev/ino again when they appear to match, but not when they appear
2972 * to be different: Could skip a buffer when it's actually the same
2973 * file. */
2974 if (buf_same_ino(buf, stp))
2975 {
2976 buf_setino(buf);
2977 if (buf_same_ino(buf, stp))
2978 return FALSE;
2979 }
2980 }
2981#endif
2982 return TRUE;
2983}
2984
2985#if defined(UNIX) || defined(PROTO)
2986/*
2987 * Set inode and device number for a buffer.
2988 * Must always be called when b_fname is changed!.
2989 */
2990 void
2991buf_setino(buf)
2992 buf_T *buf;
2993{
2994 struct stat st;
2995
2996 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2997 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002998 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999 buf->b_dev = st.st_dev;
3000 buf->b_ino = st.st_ino;
3001 }
3002 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003003 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004}
3005
3006/*
3007 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
3008 */
3009 static int
3010buf_same_ino(buf, stp)
3011 buf_T *buf;
3012 struct stat *stp;
3013{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00003014 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 && stp->st_dev == buf->b_dev
3016 && stp->st_ino == buf->b_ino);
3017}
3018#endif
3019
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003020/*
3021 * Print info about the current buffer.
3022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023 void
3024fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003025 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 int shorthelp;
3027 int dont_truncate;
3028{
3029 char_u *name;
3030 int n;
3031 char_u *p;
3032 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003033 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
3035 buffer = alloc(IOSIZE);
3036 if (buffer == NULL)
3037 return;
3038
3039 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3040 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003041 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 p = buffer + STRLEN(buffer);
3043 }
3044 else
3045 p = buffer;
3046
3047 *p++ = '"';
3048 if (buf_spname(curbuf) != NULL)
3049 STRCPY(p, buf_spname(curbuf));
3050 else
3051 {
3052 if (!fullname && curbuf->b_fname != NULL)
3053 name = curbuf->b_fname;
3054 else
3055 name = curbuf->b_ffname;
3056 home_replace(shorthelp ? curbuf : NULL, name, p,
3057 (int)(IOSIZE - (p - buffer)), TRUE);
3058 }
3059
Bram Moolenaara800b422010-06-27 01:15:55 +02003060 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 curbufIsChanged() ? (shortmess(SHM_MOD)
3062 ? " [+]" : _(" [Modified]")) : " ",
3063 (curbuf->b_flags & BF_NOTEDITED)
3064#ifdef FEAT_QUICKFIX
3065 && !bt_dontwrite(curbuf)
3066#endif
3067 ? _("[Not edited]") : "",
3068 (curbuf->b_flags & BF_NEW)
3069#ifdef FEAT_QUICKFIX
3070 && !bt_dontwrite(curbuf)
3071#endif
3072 ? _("[New file]") : "",
3073 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3074 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3075 : _("[readonly]")) : "",
3076 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3077 || curbuf->b_p_ro) ?
3078 " " : "");
3079 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3080 * causes an overflow, thus for large numbers divide instead. */
3081 if (curwin->w_cursor.lnum > 1000000L)
3082 n = (int)(((long)curwin->w_cursor.lnum) /
3083 ((long)curbuf->b_ml.ml_line_count / 100L));
3084 else
3085 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3086 (long)curbuf->b_ml.ml_line_count);
3087 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3088 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003089 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 }
3091#ifdef FEAT_CMDL_INFO
3092 else if (p_ru)
3093 {
3094 /* Current line and column are already on the screen -- webb */
3095 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003096 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003097 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003098 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 (long)curbuf->b_ml.ml_line_count, n);
3100 }
3101#endif
3102 else
3103 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003104 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 _("line %ld of %ld --%d%%-- col "),
3106 (long)curwin->w_cursor.lnum,
3107 (long)curbuf->b_ml.ml_line_count,
3108 n);
3109 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003110 len = STRLEN(buffer);
3111 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003112 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3113 }
3114
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003115 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116
3117 if (dont_truncate)
3118 {
3119 /* Temporarily set msg_scroll to avoid the message being truncated.
3120 * First call msg_start() to get the message in the right place. */
3121 msg_start();
3122 n = msg_scroll;
3123 msg_scroll = TRUE;
3124 msg(buffer);
3125 msg_scroll = n;
3126 }
3127 else
3128 {
3129 p = msg_trunc_attr(buffer, FALSE, 0);
3130 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131 /* Need to repeat the message after redrawing when:
3132 * - When restart_edit is set (otherwise there will be a delay
3133 * before redrawing).
3134 * - When the screen was scrolled but there is no wait-return
3135 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003136 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 }
3138
3139 vim_free(buffer);
3140}
3141
3142 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003143col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003145 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 int col;
3147 int vcol;
3148{
3149 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003150 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003152 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003153}
3154
3155#if defined(FEAT_TITLE) || defined(PROTO)
3156/*
3157 * put file name in title bar of window and in icon title
3158 */
3159
3160static char_u *lasttitle = NULL;
3161static char_u *lasticon = NULL;
3162
3163 void
3164maketitle()
3165{
3166 char_u *p;
3167 char_u *t_str = NULL;
3168 char_u *i_name;
3169 char_u *i_str = NULL;
3170 int maxlen = 0;
3171 int len;
3172 int mustset;
3173 char_u buf[IOSIZE];
3174 int off;
3175
3176 if (!redrawing())
3177 {
3178 /* Postpone updating the title when 'lazyredraw' is set. */
3179 need_maketitle = TRUE;
3180 return;
3181 }
3182
3183 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003184 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003185 return;
3186
3187 if (p_title)
3188 {
3189 if (p_titlelen > 0)
3190 {
3191 maxlen = p_titlelen * Columns / 100;
3192 if (maxlen < 10)
3193 maxlen = 10;
3194 }
3195
3196 t_str = buf;
3197 if (*p_titlestring != NUL)
3198 {
3199#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003200 if (stl_syntax & STL_IN_TITLE)
3201 {
3202 int use_sandbox = FALSE;
3203 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003204
3205# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003206 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003207# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003208 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003210 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003211 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003212 if (called_emsg)
3213 set_string_option_direct((char_u *)"titlestring", -1,
3214 (char_u *)"", OPT_FREE, SID_ERROR);
3215 called_emsg |= save_called_emsg;
3216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003217 else
3218#endif
3219 t_str = p_titlestring;
3220 }
3221 else
3222 {
3223 /* format: "fname + (path) (1 of 2) - VIM" */
3224
3225 if (curbuf->b_fname == NULL)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003226 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003227 else
3228 {
3229 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003230 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003231 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 }
3233
3234 switch (bufIsChanged(curbuf)
3235 + (curbuf->b_p_ro * 2)
3236 + (!curbuf->b_p_ma * 4))
3237 {
3238 case 1: STRCAT(buf, " +"); break;
3239 case 2: STRCAT(buf, " ="); break;
3240 case 3: STRCAT(buf, " =+"); break;
3241 case 4:
3242 case 6: STRCAT(buf, " -"); break;
3243 case 5:
3244 case 7: STRCAT(buf, " -+"); break;
3245 }
3246
3247 if (curbuf->b_fname != NULL)
3248 {
3249 /* Get path of file, replace home dir with ~ */
3250 off = (int)STRLEN(buf);
3251 buf[off++] = ' ';
3252 buf[off++] = '(';
3253 home_replace(curbuf, curbuf->b_ffname,
3254 buf + off, IOSIZE - off, TRUE);
3255#ifdef BACKSLASH_IN_FILENAME
3256 /* avoid "c:/name" to be reduced to "c" */
3257 if (isalpha(buf[off]) && buf[off + 1] == ':')
3258 off += 2;
3259#endif
3260 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003261 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003264 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003265 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003268
Bram Moolenaar071d4272004-06-13 20:20:40 +00003269 /* translate unprintable chars */
3270 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003271 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 STRCAT(buf, ")");
3274 }
3275
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003276 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277
3278#if defined(FEAT_CLIENTSERVER)
3279 if (serverName != NULL)
3280 {
3281 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003282 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 }
3284 else
3285#endif
3286 STRCAT(buf, " - VIM");
3287
3288 if (maxlen > 0)
3289 {
3290 /* make it shorter by removing a bit in the middle */
Bram Moolenaarf31b7642012-01-20 20:44:43 +01003291 if (vim_strsize(buf) > maxlen)
3292 trunc_string(buf, buf, maxlen, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 }
3294 }
3295 }
3296 mustset = ti_change(t_str, &lasttitle);
3297
3298 if (p_icon)
3299 {
3300 i_str = buf;
3301 if (*p_iconstring != NUL)
3302 {
3303#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003304 if (stl_syntax & STL_IN_ICON)
3305 {
3306 int use_sandbox = FALSE;
3307 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003308
3309# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003310 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003311# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003312 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003314 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003315 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003316 if (called_emsg)
3317 set_string_option_direct((char_u *)"iconstring", -1,
3318 (char_u *)"", OPT_FREE, SID_ERROR);
3319 called_emsg |= save_called_emsg;
3320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321 else
3322#endif
3323 i_str = p_iconstring;
3324 }
3325 else
3326 {
3327 if (buf_spname(curbuf) != NULL)
3328 i_name = (char_u *)buf_spname(curbuf);
3329 else /* use file name only in icon */
3330 i_name = gettail(curbuf->b_ffname);
3331 *i_str = NUL;
3332 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003333 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 if (len > 100)
3335 {
3336 len -= 100;
3337#ifdef FEAT_MBYTE
3338 if (has_mbyte)
3339 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3340#endif
3341 i_name += len;
3342 }
3343 STRCPY(i_str, i_name);
3344 trans_characters(i_str, IOSIZE);
3345 }
3346 }
3347
3348 mustset |= ti_change(i_str, &lasticon);
3349
3350 if (mustset)
3351 resettitle();
3352}
3353
3354/*
3355 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3356 * from "str" if it does.
3357 * Return TRUE when "*last" changed.
3358 */
3359 static int
3360ti_change(str, last)
3361 char_u *str;
3362 char_u **last;
3363{
3364 if ((str == NULL) != (*last == NULL)
3365 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3366 {
3367 vim_free(*last);
3368 if (str == NULL)
3369 *last = NULL;
3370 else
3371 *last = vim_strsave(str);
3372 return TRUE;
3373 }
3374 return FALSE;
3375}
3376
3377/*
3378 * Put current window title back (used after calling a shell)
3379 */
3380 void
3381resettitle()
3382{
3383 mch_settitle(lasttitle, lasticon);
3384}
Bram Moolenaarea408852005-06-25 22:49:46 +00003385
3386# if defined(EXITFREE) || defined(PROTO)
3387 void
3388free_titles()
3389{
3390 vim_free(lasttitle);
3391 vim_free(lasticon);
3392}
3393# endif
3394
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395#endif /* FEAT_TITLE */
3396
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003397#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003399 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 * Return length of string in screen cells.
3401 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003402 * Normally works for window "wp", except when working for 'tabline' then it
3403 * is "curwin".
3404 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 * Items are drawn interspersed with the text that surrounds it
3406 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3407 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3408 *
3409 * If maxwidth is not zero, the string will be filled at any middle marker
3410 * or truncated if too long, fillchar is used for all whitespace.
3411 */
3412 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003413build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3414 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003416 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 size_t outlen; /* length of out[] */
3418 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003419 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 int fillchar;
3421 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003422 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3423 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424{
3425 char_u *p;
3426 char_u *s;
3427 char_u *t;
3428 char_u *linecont;
3429#ifdef FEAT_EVAL
3430 win_T *o_curwin;
3431 buf_T *o_curbuf;
3432#endif
3433 int empty_line;
3434 colnr_T virtcol;
3435 long l;
3436 long n;
3437 int prevchar_isflag;
3438 int prevchar_isitem;
3439 int itemisflag;
3440 int fillable;
3441 char_u *str;
3442 long num;
3443 int width;
3444 int itemcnt;
3445 int curitem;
3446 int groupitem[STL_MAX_ITEM];
3447 int groupdepth;
3448 struct stl_item
3449 {
3450 char_u *start;
3451 int minwid;
3452 int maxwid;
3453 enum
3454 {
3455 Normal,
3456 Empty,
3457 Group,
3458 Middle,
3459 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003460 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 Trunc
3462 } type;
3463 } item[STL_MAX_ITEM];
3464 int minwid;
3465 int maxwid;
3466 int zeropad;
3467 char_u base;
3468 char_u opt;
3469#define TMPLEN 70
3470 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003471 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003472 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003473
3474#ifdef FEAT_EVAL
3475 /*
3476 * When the format starts with "%!" then evaluate it as an expression and
3477 * use the result as the actual format string.
3478 */
3479 if (fmt[0] == '%' && fmt[1] == '!')
3480 {
3481 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3482 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003483 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003484 }
3485#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486
3487 if (fillchar == 0)
3488 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003489#ifdef FEAT_MBYTE
3490 /* Can't handle a multi-byte fill character yet. */
3491 else if (mb_char2len(fillchar) > 1)
3492 fillchar = '-';
3493#endif
3494
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 /*
3496 * Get line & check if empty (cursorpos will show "0-1").
3497 * If inversion is possible we use it. Else '=' characters are used.
3498 */
3499 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3500 empty_line = (*linecont == NUL);
3501
3502 groupdepth = 0;
3503 p = out;
3504 curitem = 0;
3505 prevchar_isflag = TRUE;
3506 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003507 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003509 if (curitem == STL_MAX_ITEM)
3510 {
3511 /* There are too many items. Add the error code to the statusline
3512 * to give the user a hint about what went wrong. */
3513 if (p + 6 < out + outlen)
3514 {
3515 mch_memmove(p, " E541", (size_t)5);
3516 p += 5;
3517 }
3518 break;
3519 }
3520
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 if (*s != NUL && *s != '%')
3522 prevchar_isflag = prevchar_isitem = FALSE;
3523
3524 /*
3525 * Handle up to the next '%' or the end.
3526 */
3527 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3528 *p++ = *s++;
3529 if (*s == NUL || p + 1 >= out + outlen)
3530 break;
3531
3532 /*
3533 * Handle one '%' item.
3534 */
3535 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003536 if (*s == NUL) /* ignore trailing % */
3537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 if (*s == '%')
3539 {
3540 if (p + 1 >= out + outlen)
3541 break;
3542 *p++ = *s++;
3543 prevchar_isflag = prevchar_isitem = FALSE;
3544 continue;
3545 }
3546 if (*s == STL_MIDDLEMARK)
3547 {
3548 s++;
3549 if (groupdepth > 0)
3550 continue;
3551 item[curitem].type = Middle;
3552 item[curitem++].start = p;
3553 continue;
3554 }
3555 if (*s == STL_TRUNCMARK)
3556 {
3557 s++;
3558 item[curitem].type = Trunc;
3559 item[curitem++].start = p;
3560 continue;
3561 }
3562 if (*s == ')')
3563 {
3564 s++;
3565 if (groupdepth < 1)
3566 continue;
3567 groupdepth--;
3568
3569 t = item[groupitem[groupdepth]].start;
3570 *p = NUL;
3571 l = vim_strsize(t);
3572 if (curitem > groupitem[groupdepth] + 1
3573 && item[groupitem[groupdepth]].minwid == 0)
3574 {
3575 /* remove group if all items are empty */
3576 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3577 if (item[n].type == Normal)
3578 break;
3579 if (n == curitem)
3580 {
3581 p = t;
3582 l = 0;
3583 }
3584 }
3585 if (l > item[groupitem[groupdepth]].maxwid)
3586 {
3587 /* truncate, remove n bytes of text at the start */
3588#ifdef FEAT_MBYTE
3589 if (has_mbyte)
3590 {
3591 /* Find the first character that should be included. */
3592 n = 0;
3593 while (l >= item[groupitem[groupdepth]].maxwid)
3594 {
3595 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003596 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 }
3598 }
3599 else
3600#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003601 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602
3603 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003604 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 p = p - n + 1;
3606#ifdef FEAT_MBYTE
3607 /* Fill up space left over by half a double-wide char. */
3608 while (++l < item[groupitem[groupdepth]].minwid)
3609 *p++ = fillchar;
3610#endif
3611
3612 /* correct the start of the items for the truncation */
3613 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3614 {
3615 item[l].start -= n;
3616 if (item[l].start < t)
3617 item[l].start = t;
3618 }
3619 }
3620 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3621 {
3622 /* fill */
3623 n = item[groupitem[groupdepth]].minwid;
3624 if (n < 0)
3625 {
3626 /* fill by appending characters */
3627 n = 0 - n;
3628 while (l++ < n && p + 1 < out + outlen)
3629 *p++ = fillchar;
3630 }
3631 else
3632 {
3633 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003634 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 l = n - l;
3636 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003637 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 p += l;
3639 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3640 item[n].start += l;
3641 for ( ; l > 0; l--)
3642 *t++ = fillchar;
3643 }
3644 }
3645 continue;
3646 }
3647 minwid = 0;
3648 maxwid = 9999;
3649 zeropad = FALSE;
3650 l = 1;
3651 if (*s == '0')
3652 {
3653 s++;
3654 zeropad = TRUE;
3655 }
3656 if (*s == '-')
3657 {
3658 s++;
3659 l = -1;
3660 }
3661 if (VIM_ISDIGIT(*s))
3662 {
3663 minwid = (int)getdigits(&s);
3664 if (minwid < 0) /* overflow */
3665 minwid = 0;
3666 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003667 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 {
3669 item[curitem].type = Highlight;
3670 item[curitem].start = p;
3671 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3672 s++;
3673 curitem++;
3674 continue;
3675 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003676 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3677 {
3678 if (*s == STL_TABCLOSENR)
3679 {
3680 if (minwid == 0)
3681 {
3682 /* %X ends the close label, go back to the previously
3683 * define tab label nr. */
3684 for (n = curitem - 1; n >= 0; --n)
3685 if (item[n].type == TabPage && item[n].minwid >= 0)
3686 {
3687 minwid = item[n].minwid;
3688 break;
3689 }
3690 }
3691 else
3692 /* close nrs are stored as negative values */
3693 minwid = - minwid;
3694 }
3695 item[curitem].type = TabPage;
3696 item[curitem].start = p;
3697 item[curitem].minwid = minwid;
3698 s++;
3699 curitem++;
3700 continue;
3701 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 if (*s == '.')
3703 {
3704 s++;
3705 if (VIM_ISDIGIT(*s))
3706 {
3707 maxwid = (int)getdigits(&s);
3708 if (maxwid <= 0) /* overflow */
3709 maxwid = 50;
3710 }
3711 }
3712 minwid = (minwid > 50 ? 50 : minwid) * l;
3713 if (*s == '(')
3714 {
3715 groupitem[groupdepth++] = curitem;
3716 item[curitem].type = Group;
3717 item[curitem].start = p;
3718 item[curitem].minwid = minwid;
3719 item[curitem].maxwid = maxwid;
3720 s++;
3721 curitem++;
3722 continue;
3723 }
3724 if (vim_strchr(STL_ALL, *s) == NULL)
3725 {
3726 s++;
3727 continue;
3728 }
3729 opt = *s++;
3730
3731 /* OK - now for the real work */
3732 base = 'D';
3733 itemisflag = FALSE;
3734 fillable = TRUE;
3735 num = -1;
3736 str = NULL;
3737 switch (opt)
3738 {
3739 case STL_FILEPATH:
3740 case STL_FULLPATH:
3741 case STL_FILENAME:
3742 fillable = FALSE; /* don't change ' ' to fillchar */
3743 if (buf_spname(wp->w_buffer) != NULL)
3744 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3745 else
3746 {
3747 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003748 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3750 }
3751 trans_characters(NameBuff, MAXPATHL);
3752 if (opt != STL_FILENAME)
3753 str = NameBuff;
3754 else
3755 str = gettail(NameBuff);
3756 break;
3757
3758 case STL_VIM_EXPR: /* '{' */
3759 itemisflag = TRUE;
3760 t = p;
3761 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3762 *p++ = *s++;
3763 if (*s != '}') /* missing '}' or out of space */
3764 break;
3765 s++;
3766 *p = 0;
3767 p = t;
3768
3769#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003770 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3772
3773 o_curbuf = curbuf;
3774 o_curwin = curwin;
3775 curwin = wp;
3776 curbuf = wp->w_buffer;
3777
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003778 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779
3780 curwin = o_curwin;
3781 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003782 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783
3784 if (str != NULL && *str != 0)
3785 {
3786 if (*skipdigits(str) == NUL)
3787 {
3788 num = atoi((char *)str);
3789 vim_free(str);
3790 str = NULL;
3791 itemisflag = FALSE;
3792 }
3793 }
3794#endif
3795 break;
3796
3797 case STL_LINE:
3798 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3799 ? 0L : (long)(wp->w_cursor.lnum);
3800 break;
3801
3802 case STL_NUMLINES:
3803 num = wp->w_buffer->b_ml.ml_line_count;
3804 break;
3805
3806 case STL_COLUMN:
3807 num = !(State & INSERT) && empty_line
3808 ? 0 : (int)wp->w_cursor.col + 1;
3809 break;
3810
3811 case STL_VIRTCOL:
3812 case STL_VIRTCOL_ALT:
3813 /* In list mode virtcol needs to be recomputed */
3814 virtcol = wp->w_virtcol;
3815 if (wp->w_p_list && lcs_tab1 == NUL)
3816 {
3817 wp->w_p_list = FALSE;
3818 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3819 wp->w_p_list = TRUE;
3820 }
3821 ++virtcol;
3822 /* Don't display %V if it's the same as %c. */
3823 if (opt == STL_VIRTCOL_ALT
3824 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3825 ? 0 : (int)wp->w_cursor.col + 1)))
3826 break;
3827 num = (long)virtcol;
3828 break;
3829
3830 case STL_PERCENTAGE:
3831 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3832 (long)wp->w_buffer->b_ml.ml_line_count);
3833 break;
3834
3835 case STL_ALTPERCENT:
3836 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003837 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 break;
3839
3840 case STL_ARGLISTSTAT:
3841 fillable = FALSE;
3842 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003843 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 str = tmp;
3845 break;
3846
3847 case STL_KEYMAP:
3848 fillable = FALSE;
3849 if (get_keymap_str(wp, tmp, TMPLEN))
3850 str = tmp;
3851 break;
3852 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003853#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003854 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855#else
3856 num = 0;
3857#endif
3858 break;
3859
3860 case STL_BUFNO:
3861 num = wp->w_buffer->b_fnum;
3862 break;
3863
3864 case STL_OFFSET_X:
3865 base = 'X';
3866 case STL_OFFSET:
3867#ifdef FEAT_BYTEOFF
3868 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3869 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3870 0L : l + 1 + (!(State & INSERT) && empty_line ?
3871 0 : (int)wp->w_cursor.col);
3872#endif
3873 break;
3874
3875 case STL_BYTEVAL_X:
3876 base = 'X';
3877 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003878 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 num = 0;
3880 else
3881 {
3882#ifdef FEAT_MBYTE
3883 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3884#else
3885 num = linecont[wp->w_cursor.col];
3886#endif
3887 }
3888 if (num == NL)
3889 num = 0;
3890 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3891 num = NL;
3892 break;
3893
3894 case STL_ROFLAG:
3895 case STL_ROFLAG_ALT:
3896 itemisflag = TRUE;
3897 if (wp->w_buffer->b_p_ro)
3898 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3899 break;
3900
3901 case STL_HELPFLAG:
3902 case STL_HELPFLAG_ALT:
3903 itemisflag = TRUE;
3904 if (wp->w_buffer->b_help)
3905 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003906 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 break;
3908
3909#ifdef FEAT_AUTOCMD
3910 case STL_FILETYPE:
3911 if (*wp->w_buffer->b_p_ft != NUL
3912 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3913 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003914 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3915 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 str = tmp;
3917 }
3918 break;
3919
3920 case STL_FILETYPE_ALT:
3921 itemisflag = TRUE;
3922 if (*wp->w_buffer->b_p_ft != NUL
3923 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3924 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003925 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3926 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 for (t = tmp; *t != 0; t++)
3928 *t = TOUPPER_LOC(*t);
3929 str = tmp;
3930 }
3931 break;
3932#endif
3933
3934#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3935 case STL_PREVIEWFLAG:
3936 case STL_PREVIEWFLAG_ALT:
3937 itemisflag = TRUE;
3938 if (wp->w_p_pvw)
3939 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3940 : _("[Preview]"));
3941 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003942
3943 case STL_QUICKFIX:
3944 if (bt_quickfix(wp->w_buffer))
3945 str = (char_u *)(wp->w_llist_ref
3946 ? _(msg_loclist)
3947 : _(msg_qflist));
3948 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#endif
3950
3951 case STL_MODIFIED:
3952 case STL_MODIFIED_ALT:
3953 itemisflag = TRUE;
3954 switch ((opt == STL_MODIFIED_ALT)
3955 + bufIsChanged(wp->w_buffer) * 2
3956 + (!wp->w_buffer->b_p_ma) * 4)
3957 {
3958 case 2: str = (char_u *)"[+]"; break;
3959 case 3: str = (char_u *)",+"; break;
3960 case 4: str = (char_u *)"[-]"; break;
3961 case 5: str = (char_u *)",-"; break;
3962 case 6: str = (char_u *)"[+-]"; break;
3963 case 7: str = (char_u *)",+-"; break;
3964 }
3965 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003966
3967 case STL_HIGHLIGHT:
3968 t = s;
3969 while (*s != '#' && *s != NUL)
3970 ++s;
3971 if (*s == '#')
3972 {
3973 item[curitem].type = Highlight;
3974 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003975 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003976 curitem++;
3977 }
3978 ++s;
3979 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981
3982 item[curitem].start = p;
3983 item[curitem].type = Normal;
3984 if (str != NULL && *str)
3985 {
3986 t = str;
3987 if (itemisflag)
3988 {
3989 if ((t[0] && t[1])
3990 && ((!prevchar_isitem && *t == ',')
3991 || (prevchar_isflag && *t == ' ')))
3992 t++;
3993 prevchar_isflag = TRUE;
3994 }
3995 l = vim_strsize(t);
3996 if (l > 0)
3997 prevchar_isitem = TRUE;
3998 if (l > maxwid)
3999 {
4000 while (l >= maxwid)
4001#ifdef FEAT_MBYTE
4002 if (has_mbyte)
4003 {
4004 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004005 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 else
4008#endif
4009 l -= byte2cells(*t++);
4010 if (p + 1 >= out + outlen)
4011 break;
4012 *p++ = '<';
4013 }
4014 if (minwid > 0)
4015 {
4016 for (; l < minwid && p + 1 < out + outlen; l++)
4017 {
4018 /* Don't put a "-" in front of a digit. */
4019 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
4020 *p++ = ' ';
4021 else
4022 *p++ = fillchar;
4023 }
4024 minwid = 0;
4025 }
4026 else
4027 minwid *= -1;
4028 while (*t && p + 1 < out + outlen)
4029 {
4030 *p++ = *t++;
4031 /* Change a space by fillchar, unless fillchar is '-' and a
4032 * digit follows. */
4033 if (fillable && p[-1] == ' '
4034 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4035 p[-1] = fillchar;
4036 }
4037 for (; l < minwid && p + 1 < out + outlen; l++)
4038 *p++ = fillchar;
4039 }
4040 else if (num >= 0)
4041 {
4042 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4043 char_u nstr[20];
4044
4045 if (p + 20 >= out + outlen)
4046 break; /* not sufficient space */
4047 prevchar_isitem = TRUE;
4048 t = nstr;
4049 if (opt == STL_VIRTCOL_ALT)
4050 {
4051 *t++ = '-';
4052 minwid--;
4053 }
4054 *t++ = '%';
4055 if (zeropad)
4056 *t++ = '0';
4057 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004058 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 *t = 0;
4060
4061 for (n = num, l = 1; n >= nbase; n /= nbase)
4062 l++;
4063 if (opt == STL_VIRTCOL_ALT)
4064 l++;
4065 if (l > maxwid)
4066 {
4067 l += 2;
4068 n = l - maxwid;
4069 while (l-- > maxwid)
4070 num /= nbase;
4071 *t++ = '>';
4072 *t++ = '%';
4073 *t = t[-3];
4074 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004075 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4076 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 }
4078 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004079 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4080 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 p += STRLEN(p);
4082 }
4083 else
4084 item[curitem].type = Empty;
4085
4086 if (opt == STL_VIM_EXPR)
4087 vim_free(str);
4088
4089 if (num >= 0 || (!itemisflag && str && *str))
4090 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4091 curitem++;
4092 }
4093 *p = NUL;
4094 itemcnt = curitem;
4095
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004096#ifdef FEAT_EVAL
4097 if (usefmt != fmt)
4098 vim_free(usefmt);
4099#endif
4100
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 width = vim_strsize(out);
4102 if (maxwidth > 0 && width > maxwidth)
4103 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004104 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 l = 0;
4106 if (itemcnt == 0)
4107 s = out;
4108 else
4109 {
4110 for ( ; l < itemcnt; l++)
4111 if (item[l].type == Trunc)
4112 {
4113 /* Truncate at %< item. */
4114 s = item[l].start;
4115 break;
4116 }
4117 if (l == itemcnt)
4118 {
4119 /* No %< item, truncate first item. */
4120 s = item[0].start;
4121 l = 0;
4122 }
4123 }
4124
4125 if (width - vim_strsize(s) >= maxwidth)
4126 {
4127 /* Truncation mark is beyond max length */
4128#ifdef FEAT_MBYTE
4129 if (has_mbyte)
4130 {
4131 s = out;
4132 width = 0;
4133 for (;;)
4134 {
4135 width += ptr2cells(s);
4136 if (width >= maxwidth)
4137 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004138 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 }
4140 /* Fill up for half a double-wide character. */
4141 while (++width < maxwidth)
4142 *s++ = fillchar;
4143 }
4144 else
4145#endif
4146 s = out + maxwidth - 1;
4147 for (l = 0; l < itemcnt; l++)
4148 if (item[l].start > s)
4149 break;
4150 itemcnt = l;
4151 *s++ = '>';
4152 *s = 0;
4153 }
4154 else
4155 {
4156#ifdef FEAT_MBYTE
4157 if (has_mbyte)
4158 {
4159 n = 0;
4160 while (width >= maxwidth)
4161 {
4162 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004163 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 }
4165 }
4166 else
4167#endif
4168 n = width - maxwidth + 1;
4169 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004170 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 *s = '<';
4172
4173 /* Fill up for half a double-wide character. */
4174 while (++width < maxwidth)
4175 {
4176 s = s + STRLEN(s);
4177 *s++ = fillchar;
4178 *s = NUL;
4179 }
4180
4181 --n; /* count the '<' */
4182 for (; l < itemcnt; l++)
4183 {
4184 if (item[l].start - n >= s)
4185 item[l].start -= n;
4186 else
4187 item[l].start = s;
4188 }
4189 }
4190 width = maxwidth;
4191 }
4192 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4193 {
4194 /* Apply STL_MIDDLE if any */
4195 for (l = 0; l < itemcnt; l++)
4196 if (item[l].type == Middle)
4197 break;
4198 if (l < itemcnt)
4199 {
4200 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004201 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 for (s = item[l].start; s < p; s++)
4203 *s = fillchar;
4204 for (l++; l < itemcnt; l++)
4205 item[l].start += maxwidth - width;
4206 width = maxwidth;
4207 }
4208 }
4209
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004210 /* Store the info about highlighting. */
4211 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004213 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 for (l = 0; l < itemcnt; l++)
4215 {
4216 if (item[l].type == Highlight)
4217 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004218 sp->start = item[l].start;
4219 sp->userhl = item[l].minwid;
4220 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 }
4222 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004223 sp->start = NULL;
4224 sp->userhl = 0;
4225 }
4226
4227 /* Store the info about tab pages labels. */
4228 if (tabtab != NULL)
4229 {
4230 sp = tabtab;
4231 for (l = 0; l < itemcnt; l++)
4232 {
4233 if (item[l].type == TabPage)
4234 {
4235 sp->start = item[l].start;
4236 sp->userhl = item[l].minwid;
4237 sp++;
4238 }
4239 }
4240 sp->start = NULL;
4241 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 }
4243
4244 return width;
4245}
4246#endif /* FEAT_STL_OPT */
4247
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004248#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4249 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004251 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4252 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 */
4254 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004255get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004257 char_u *buf;
4258 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259{
4260 long above; /* number of lines above window */
4261 long below; /* number of lines below window */
4262
4263 above = wp->w_topline - 1;
4264#ifdef FEAT_DIFF
4265 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4266#endif
4267 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4268 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004269 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4270 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004272 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004274 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004275 ? (int)(above / ((above + below) / 100L))
4276 : (int)(above * 100L / (above + below)));
4277}
4278#endif
4279
4280/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004281 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 * Return TRUE if it was appended.
4283 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004284 static int
4285append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 win_T *wp;
4287 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004288 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290{
4291 char_u *p;
4292
4293 if (ARGCOUNT <= 1) /* nothing to do */
4294 return FALSE;
4295
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004296 p = buf + STRLEN(buf); /* go to the end of the buffer */
4297 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 return FALSE;
4299 *p++ = ' ';
4300 *p++ = '(';
4301 if (add_file)
4302 {
4303 STRCPY(p, "file ");
4304 p += 5;
4305 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004306 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4307 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4309 return TRUE;
4310}
4311
4312/*
4313 * If fname is not a full path, make it a full path.
4314 * Returns pointer to allocated memory (NULL for failure).
4315 */
4316 char_u *
4317fix_fname(fname)
4318 char_u *fname;
4319{
4320 /*
4321 * Force expanding the path always for Unix, because symbolic links may
4322 * mess up the full path name, even though it starts with a '/'.
4323 * Also expand when there is ".." in the file name, try to remove it,
4324 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004325 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 * For MS-Windows also expand names like "longna~1" to "longname".
4327 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004328#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 return FullName_save(fname, TRUE);
4330#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004331 if (!vim_isAbsName(fname)
4332 || strstr((char *)fname, "..") != NULL
4333 || strstr((char *)fname, "//") != NULL
4334# ifdef BACKSLASH_IN_FILENAME
4335 || strstr((char *)fname, "\\\\") != NULL
4336# endif
4337# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004339# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 )
4341 return FullName_save(fname, FALSE);
4342
4343 fname = vim_strsave(fname);
4344
Bram Moolenaar9b942202007-10-03 12:31:33 +00004345# ifdef USE_FNAME_CASE
4346# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004348# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 {
4350 if (fname != NULL)
4351 fname_case(fname, 0); /* set correct case for file name */
4352 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 return fname;
4356#endif
4357}
4358
4359/*
4360 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4361 * "ffname" becomes a pointer to allocated memory (or NULL).
4362 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 void
4364fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004365 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 char_u **ffname;
4367 char_u **sfname;
4368{
4369 if (*ffname == NULL) /* if no file name given, nothing to do */
4370 return;
4371 if (*sfname == NULL) /* if no short file name given, use ffname */
4372 *sfname = *ffname;
4373 *ffname = fix_fname(*ffname); /* expand to full path */
4374
4375#ifdef FEAT_SHORTCUT
4376 if (!buf->b_p_bin)
4377 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004378 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379
4380 /* If the file name is a shortcut file, use the file it links to. */
4381 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004382 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
4384 vim_free(*ffname);
4385 *ffname = rfname;
4386 *sfname = rfname;
4387 }
4388 }
4389#endif
4390}
4391
4392/*
4393 * Get the file name for an argument list entry.
4394 */
4395 char_u *
4396alist_name(aep)
4397 aentry_T *aep;
4398{
4399 buf_T *bp;
4400
4401 /* Use the name from the associated buffer if it exists. */
4402 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004403 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 return aep->ae_fname;
4405 return bp->b_fname;
4406}
4407
4408#if defined(FEAT_WINDOWS) || defined(PROTO)
4409/*
4410 * do_arg_all(): Open up to 'count' windows, one for each argument.
4411 */
4412 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004413do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 int count;
4415 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004416 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417{
4418 int i;
4419 win_T *wp, *wpnext;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004420 char_u *opened; /* Array of weight for which args are open:
4421 * 0: not opened
4422 * 1: opened in other tab
4423 * 2: opened in curtab
4424 * 3: opened in curtab and curwin
4425 */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004426 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 int use_firstwin = FALSE; /* use first window for arglist */
4428 int split_ret = OK;
4429 int p_ea_save;
4430 alist_T *alist; /* argument list to be used */
4431 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004432 tabpage_T *tpnext;
4433 int had_tab = cmdmod.tab;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004434 win_T *old_curwin, *last_curwin;
4435 tabpage_T *old_curtab, *last_curtab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004436 win_T *new_curwin = NULL;
4437 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438
4439 if (ARGCOUNT <= 0)
4440 {
4441 /* Don't give an error message. We don't want it when the ":all"
4442 * command is in the .vimrc. */
4443 return;
4444 }
4445 setpcmark();
4446
4447 opened_len = ARGCOUNT;
4448 opened = alloc_clear((unsigned)opened_len);
4449 if (opened == NULL)
4450 return;
4451
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004452 /* Autocommands may do anything to the argument list. Make sure it's not
4453 * freed while we are working here by "locking" it. We still have to
4454 * watch out for its size to be changed. */
4455 alist = curwin->w_alist;
4456 ++alist->al_refcount;
4457
4458 old_curwin = curwin;
4459 old_curtab = curtab;
4460
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461#ifdef FEAT_GUI
4462 need_mouse_correct = TRUE;
4463#endif
4464
4465 /*
4466 * Try closing all windows that are not in the argument list.
4467 * Also close windows that are not full width;
4468 * When 'hidden' or "forceit" set the buffer becomes hidden.
4469 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004470 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004472 if (had_tab > 0)
4473 goto_tabpage_tp(first_tabpage);
4474 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004476 tpnext = curtab->tp_next;
4477 for (wp = firstwin; wp != NULL; wp = wpnext)
4478 {
4479 wpnext = wp->w_next;
4480 buf = wp->w_buffer;
4481 if (buf->b_ffname == NULL
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004482 || (!keep_tabs && buf->b_nwindows > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004484 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004486 )
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004487 i = opened_len;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004488 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004490 /* check if the buffer in this window is in the arglist */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004491 for (i = 0; i < opened_len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004493 if (i < alist->al_ga.ga_len
4494 && (AARGLIST(alist)[i].ae_fnum == buf->b_fnum
4495 || fullpathcmp(alist_name(&AARGLIST(alist)[i]),
4496 buf->b_ffname, TRUE) & FPC_SAME))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004498 int weight = 1;
4499
4500 if (old_curtab == curtab)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004501 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004502 ++weight;
4503 if (old_curwin == wp)
4504 ++weight;
4505 }
4506
4507 if (weight > (int)opened[i])
4508 {
4509 opened[i] = (char_u)weight;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004510 if (i == 0)
4511 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004512 if (new_curwin != NULL)
4513 new_curwin->w_arg_idx = opened_len;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004514 new_curwin = wp;
4515 new_curtab = curtab;
4516 }
4517 }
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004518 else if (keep_tabs)
4519 i = opened_len;
4520
4521 if (wp->w_alist != alist)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004522 {
4523 /* Use the current argument list for all windows
4524 * containing a file from it. */
4525 alist_unlink(wp->w_alist);
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004526 wp->w_alist = alist;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004527 ++wp->w_alist->al_refcount;
4528 }
4529 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 }
4532 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004533 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004535 if (i == opened_len && !keep_tabs)/* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004537 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004538 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004540 /* If the buffer was changed, and we would like to hide it,
4541 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004542 if (!P_HID(buf) && buf->b_nwindows <= 1
4543 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004545 (void)autowrite(buf, FALSE);
4546#ifdef FEAT_AUTOCMD
4547 /* check if autocommands removed the window */
4548 if (!win_valid(wp) || !buf_valid(buf))
4549 {
4550 wpnext = firstwin; /* start all over... */
4551 continue;
4552 }
4553#endif
4554 }
4555#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004556 /* don't close last window */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004557 if (firstwin == lastwin
4558 && (first_tabpage->tp_next == NULL || !had_tab))
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004559#endif
4560 use_firstwin = TRUE;
4561#ifdef FEAT_WINDOWS
4562 else
4563 {
4564 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4565# ifdef FEAT_AUTOCMD
4566 /* check if autocommands removed the next window */
4567 if (!win_valid(wpnext))
4568 wpnext = firstwin; /* start all over... */
4569# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 }
4571#endif
4572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 }
4574 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004575
4576 /* Without the ":tab" modifier only do the current tab page. */
4577 if (had_tab == 0 || tpnext == NULL)
4578 break;
4579
4580# ifdef FEAT_AUTOCMD
4581 /* check if autocommands removed the next tab page */
4582 if (!valid_tabpage(tpnext))
4583 tpnext = first_tabpage; /* start all over...*/
4584# endif
4585 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
4587
4588 /*
4589 * Open a window for files in the argument list that don't have one.
4590 * ARGCOUNT may change while doing this, because of autocommands.
4591 */
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004592 if (count > opened_len || count <= 0)
4593 count = opened_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
4595#ifdef FEAT_AUTOCMD
4596 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4597 ++autocmd_no_enter;
4598 ++autocmd_no_leave;
4599#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004600 last_curwin = curwin;
4601 last_curtab = curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004603#ifdef FEAT_WINDOWS
4604 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4605 * leaving an empty tab page when executed locally. */
4606 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4607 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4608 use_firstwin = TRUE;
4609#endif
4610
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004611 for (i = 0; i < count && i < opened_len && !got_int; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 {
4613 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4614 arg_had_last = TRUE;
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004615 if (opened[i] > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
4617 /* Move the already present window to below the current window */
4618 if (curwin->w_arg_idx != i)
4619 {
4620 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4621 {
4622 if (wpnext->w_arg_idx == i)
4623 {
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004624 if (keep_tabs)
4625 {
4626 new_curwin = wpnext;
4627 new_curtab = curtab;
4628 }
4629 else
4630 win_move_after(wpnext, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 break;
4632 }
4633 }
4634 }
4635 }
4636 else if (split_ret == OK)
4637 {
4638 if (!use_firstwin) /* split current window */
4639 {
4640 p_ea_save = p_ea;
4641 p_ea = TRUE; /* use space from all windows */
4642 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4643 p_ea = p_ea_save;
4644 if (split_ret == FAIL)
4645 continue;
4646 }
4647#ifdef FEAT_AUTOCMD
4648 else /* first window: do autocmd for leaving this buffer */
4649 --autocmd_no_leave;
4650#endif
4651
4652 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004653 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 */
4655 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004656 if (i == 0)
4657 {
4658 new_curwin = curwin;
4659 new_curtab = curtab;
4660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4662 ECMD_ONE,
4663 ((P_HID(curwin->w_buffer)
4664 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004665 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004666#ifdef FEAT_AUTOCMD
4667 if (use_firstwin)
4668 ++autocmd_no_leave;
4669#endif
4670 use_firstwin = FALSE;
4671 }
4672 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004673
4674 /* When ":tab" was used open a new tab for a new window repeatedly. */
4675 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4676 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 }
4678
4679 /* Remove the "lock" on the argument list. */
4680 alist_unlink(alist);
4681
4682#ifdef FEAT_AUTOCMD
4683 --autocmd_no_enter;
4684#endif
Bram Moolenaar52379ea2012-02-22 19:13:08 +01004685 /* restore last referenced tabpage's curwin */
4686 if (last_curtab != new_curtab)
4687 {
4688 if (valid_tabpage(last_curtab))
4689 goto_tabpage_tp(last_curtab);
4690 if (win_valid(last_curwin))
4691 win_enter(last_curwin, FALSE);
4692 }
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004693 /* to window with first arg */
4694 if (valid_tabpage(new_curtab))
4695 goto_tabpage_tp(new_curtab);
4696 if (win_valid(new_curwin))
4697 win_enter(new_curwin, FALSE);
4698
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#ifdef FEAT_AUTOCMD
4700 --autocmd_no_leave;
4701#endif
4702 vim_free(opened);
4703}
4704
4705# if defined(FEAT_LISTCMDS) || defined(PROTO)
4706/*
4707 * Open a window for a number of buffers.
4708 */
4709 void
4710ex_buffer_all(eap)
4711 exarg_T *eap;
4712{
4713 buf_T *buf;
4714 win_T *wp, *wpnext;
4715 int split_ret = OK;
4716 int p_ea_save;
4717 int open_wins = 0;
4718 int r;
4719 int count; /* Maximum number of windows to open. */
4720 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004721#ifdef FEAT_WINDOWS
4722 int had_tab = cmdmod.tab;
4723 tabpage_T *tpnext;
4724#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725
4726 if (eap->addr_count == 0) /* make as many windows as possible */
4727 count = 9999;
4728 else
4729 count = eap->line2; /* make as many windows as specified */
4730 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4731 all = FALSE;
4732 else
4733 all = TRUE;
4734
4735 setpcmark();
4736
4737#ifdef FEAT_GUI
4738 need_mouse_correct = TRUE;
4739#endif
4740
4741 /*
4742 * Close superfluous windows (two windows for the same buffer).
4743 * Also close windows that are not full-width.
4744 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004745#ifdef FEAT_WINDOWS
4746 if (had_tab > 0)
4747 goto_tabpage_tp(first_tabpage);
4748 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004751 tpnext = curtab->tp_next;
4752 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004754 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004755 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004756#ifdef FEAT_VERTSPLIT
4757 || ((cmdmod.split & WSP_VERT)
4758 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004759 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004760 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004762#ifdef FEAT_WINDOWS
4763 || (had_tab > 0 && wp != firstwin)
4764#endif
Bram Moolenaar362ce482012-06-06 19:02:45 +02004765 ) && firstwin != lastwin
4766#ifdef FEAT_AUTOCMD
4767 && !(wp->w_closing || wp->w_buffer->b_closing)
4768#endif
4769 )
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004770 {
4771 win_close(wp, FALSE);
4772#ifdef FEAT_AUTOCMD
4773 wpnext = firstwin; /* just in case an autocommand does
4774 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004775 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004776 open_wins = 0;
4777#endif
4778 }
4779 else
4780 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004782
4783#ifdef FEAT_WINDOWS
4784 /* Without the ":tab" modifier only do the current tab page. */
4785 if (had_tab == 0 || tpnext == NULL)
4786 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004787 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790
4791 /*
4792 * Go through the buffer list. When a buffer doesn't have a window yet,
4793 * open one. Otherwise move the window to the right position.
4794 * Watch out for autocommands that delete buffers or windows!
4795 */
4796#ifdef FEAT_AUTOCMD
4797 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4798 ++autocmd_no_enter;
4799#endif
4800 win_enter(lastwin, FALSE);
4801#ifdef FEAT_AUTOCMD
4802 ++autocmd_no_leave;
4803#endif
4804 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4805 {
4806 /* Check if this buffer needs a window */
4807 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4808 continue;
4809
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004810#ifdef FEAT_WINDOWS
4811 if (had_tab != 0)
4812 {
4813 /* With the ":tab" modifier don't move the window. */
4814 if (buf->b_nwindows > 0)
4815 wp = lastwin; /* buffer has a window, skip it */
4816 else
4817 wp = NULL;
4818 }
4819 else
4820#endif
4821 {
4822 /* Check if this buffer already has a window */
4823 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4824 if (wp->w_buffer == buf)
4825 break;
4826 /* If the buffer already has a window, move it */
4827 if (wp != NULL)
4828 win_move_after(wp, curwin);
4829 }
4830
4831 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 {
4833 /* Split the window and put the buffer in it */
4834 p_ea_save = p_ea;
4835 p_ea = TRUE; /* use space from all windows */
4836 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4837 ++open_wins;
4838 p_ea = p_ea_save;
4839 if (split_ret == FAIL)
4840 continue;
4841
4842 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004843#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 swap_exists_action = SEA_DIALOG;
4845#endif
4846 set_curbuf(buf, DOBUF_GOTO);
4847#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004850#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004852# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 break;
4854 }
4855#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004856#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 if (swap_exists_action == SEA_QUIT)
4858 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004859# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4860 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004861
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004862 /* Reset the error/interrupt/exception state here so that
4863 * aborting() returns FALSE when closing a window. */
4864 enter_cleanup(&cs);
4865# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004866
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004867 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 win_close(curwin, TRUE);
4869 --open_wins;
4870 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004871 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004872
4873# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4874 /* Restore the error/interrupt/exception state if not
4875 * discarded by a new aborting error, interrupt, or uncaught
4876 * exception. */
4877 leave_cleanup(&cs);
4878# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
4880 else
4881 handle_swap_exists(NULL);
4882#endif
4883 }
4884
4885 ui_breakcheck();
4886 if (got_int)
4887 {
4888 (void)vgetc(); /* only break the file loading, not the rest */
4889 break;
4890 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004891#ifdef FEAT_EVAL
4892 /* Autocommands deleted the buffer or aborted script processing!!! */
4893 if (aborting())
4894 break;
4895#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004896#ifdef FEAT_WINDOWS
4897 /* When ":tab" was used open a new tab for a new window repeatedly. */
4898 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4899 cmdmod.tab = 9999;
4900#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902#ifdef FEAT_AUTOCMD
4903 --autocmd_no_enter;
4904#endif
4905 win_enter(firstwin, FALSE); /* back to first window */
4906#ifdef FEAT_AUTOCMD
4907 --autocmd_no_leave;
4908#endif
4909
4910 /*
4911 * Close superfluous windows.
4912 */
4913 for (wp = lastwin; open_wins > count; )
4914 {
4915 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4916 || autowrite(wp->w_buffer, FALSE) == OK);
4917#ifdef FEAT_AUTOCMD
4918 if (!win_valid(wp))
4919 {
4920 /* BufWrite Autocommands made the window invalid, start over */
4921 wp = lastwin;
4922 }
4923 else
4924#endif
4925 if (r)
4926 {
4927 win_close(wp, !P_HID(wp->w_buffer));
4928 --open_wins;
4929 wp = lastwin;
4930 }
4931 else
4932 {
4933 wp = wp->w_prev;
4934 if (wp == NULL)
4935 break;
4936 }
4937 }
4938}
4939# endif /* FEAT_LISTCMDS */
4940
4941#endif /* FEAT_WINDOWS */
4942
Bram Moolenaara3227e22006-03-08 21:32:40 +00004943static int chk_modeline __ARGS((linenr_T, int));
4944
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945/*
4946 * do_modelines() - process mode lines for the current file
4947 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004948 * "flags" can be:
4949 * OPT_WINONLY only set options local to window
4950 * OPT_NOWIN don't set options local to window
4951 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 * Returns immediately if the "ml" option isn't set.
4953 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004955do_modelines(flags)
4956 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004958 linenr_T lnum;
4959 int nmlines;
4960 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4963 return;
4964
4965 /* Disallow recursive entry here. Can happen when executing a modeline
4966 * triggers an autocommand, which reloads modelines with a ":do". */
4967 if (entered)
4968 return;
4969
4970 ++entered;
4971 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4972 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004973 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 nmlines = 0;
4975
4976 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4977 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004978 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 nmlines = 0;
4980 --entered;
4981}
4982
4983#include "version.h" /* for version number */
4984
4985/*
4986 * chk_modeline() - check a single line for a mode string
4987 * Return FAIL if an error encountered.
4988 */
4989 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004990chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004992 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993{
4994 char_u *s;
4995 char_u *e;
4996 char_u *linecopy; /* local copy of any modeline found */
4997 int prev;
4998 int vers;
4999 int end;
5000 int retval = OK;
5001 char_u *save_sourcing_name;
5002 linenr_T save_sourcing_lnum;
5003#ifdef FEAT_EVAL
5004 scid_T save_SID;
5005#endif
5006
5007 prev = -1;
5008 for (s = ml_get(lnum); *s != NUL; ++s)
5009 {
5010 if (prev == -1 || vim_isspace(prev))
5011 {
5012 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
5013 || STRNCMP(s, "vi:", (size_t)3) == 0)
5014 break;
5015 if (STRNCMP(s, "vim", 3) == 0)
5016 {
5017 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
5018 e = s + 4;
5019 else
5020 e = s + 3;
5021 vers = getdigits(&e);
5022 if (*e == ':'
5023 && (s[3] == ':'
5024 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
5025 || (VIM_VERSION_100 < vers && s[3] == '<')
5026 || (VIM_VERSION_100 > vers && s[3] == '>')
5027 || (VIM_VERSION_100 == vers && s[3] == '=')))
5028 break;
5029 }
5030 }
5031 prev = *s;
5032 }
5033
5034 if (*s)
5035 {
5036 do /* skip over "ex:", "vi:" or "vim:" */
5037 ++s;
5038 while (s[-1] != ':');
5039
5040 s = linecopy = vim_strsave(s); /* copy the line, it will change */
5041 if (linecopy == NULL)
5042 return FAIL;
5043
5044 save_sourcing_lnum = sourcing_lnum;
5045 save_sourcing_name = sourcing_name;
5046 sourcing_lnum = lnum; /* prepare for emsg() */
5047 sourcing_name = (char_u *)"modelines";
5048
5049 end = FALSE;
5050 while (end == FALSE)
5051 {
5052 s = skipwhite(s);
5053 if (*s == NUL)
5054 break;
5055
5056 /*
5057 * Find end of set command: ':' or end of line.
5058 * Skip over "\:", replacing it with ":".
5059 */
5060 for (e = s; *e != ':' && *e != NUL; ++e)
5061 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00005062 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (*e == NUL)
5064 end = TRUE;
5065
5066 /*
5067 * If there is a "set" command, require a terminating ':' and
5068 * ignore the stuff after the ':'.
5069 * "vi:set opt opt opt: foo" -- foo not interpreted
5070 * "vi:opt opt opt: foo" -- foo interpreted
5071 * Accept "se" for compatibility with Elvis.
5072 */
5073 if (STRNCMP(s, "set ", (size_t)4) == 0
5074 || STRNCMP(s, "se ", (size_t)3) == 0)
5075 {
5076 if (*e != ':') /* no terminating ':'? */
5077 break;
5078 end = TRUE;
5079 s = vim_strchr(s, ' ') + 1;
5080 }
5081 *e = NUL; /* truncate the set command */
5082
5083 if (*s != NUL) /* skip over an empty "::" */
5084 {
5085#ifdef FEAT_EVAL
5086 save_SID = current_SID;
5087 current_SID = SID_MODELINE;
5088#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005089 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090#ifdef FEAT_EVAL
5091 current_SID = save_SID;
5092#endif
5093 if (retval == FAIL) /* stop if error found */
5094 break;
5095 }
5096 s = e + 1; /* advance to next part */
5097 }
5098
5099 sourcing_lnum = save_sourcing_lnum;
5100 sourcing_name = save_sourcing_name;
5101
5102 vim_free(linecopy);
5103 }
5104 return retval;
5105}
5106
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005107#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 int
5109read_viminfo_bufferlist(virp, writing)
5110 vir_T *virp;
5111 int writing;
5112{
5113 char_u *tab;
5114 linenr_T lnum;
5115 colnr_T col;
5116 buf_T *buf;
5117 char_u *sfname;
5118 char_u *xline;
5119
5120 /* Handle long line and escaped characters. */
5121 xline = viminfo_readstring(virp, 1, FALSE);
5122
5123 /* don't read in if there are files on the command-line or if writing: */
5124 if (xline != NULL && !writing && ARGCOUNT == 0
5125 && find_viminfo_parameter('%') != NULL)
5126 {
5127 /* Format is: <fname> Tab <lnum> Tab <col>.
5128 * Watch out for a Tab in the file name, work from the end. */
5129 lnum = 0;
5130 col = 0;
5131 tab = vim_strrchr(xline, '\t');
5132 if (tab != NULL)
5133 {
5134 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005135 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 tab = vim_strrchr(xline, '\t');
5137 if (tab != NULL)
5138 {
5139 *tab++ = '\0';
5140 lnum = atol((char *)tab);
5141 }
5142 }
5143
5144 /* Expand "~/" in the file name at "line + 1" to a full path.
5145 * Then try shortening it by comparing with the current directory */
5146 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005147 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
5149 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5150 if (buf != NULL) /* just in case... */
5151 {
5152 buf->b_last_cursor.lnum = lnum;
5153 buf->b_last_cursor.col = col;
5154 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5155 }
5156 }
5157 vim_free(xline);
5158
5159 return viminfo_readline(virp);
5160}
5161
5162 void
5163write_viminfo_bufferlist(fp)
5164 FILE *fp;
5165{
5166 buf_T *buf;
5167#ifdef FEAT_WINDOWS
5168 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005169 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170#endif
5171 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005172 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173
5174 if (find_viminfo_parameter('%') == NULL)
5175 return;
5176
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005177 /* Without a number -1 is returned: do all buffers. */
5178 max_buffers = get_viminfo_parameter('%');
5179
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005181#define LINE_BUF_LEN (MAXPATHL + 40)
5182 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 if (line == NULL)
5184 return;
5185
5186#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005187 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 set_last_cursor(win);
5189#else
5190 set_last_cursor(curwin);
5191#endif
5192
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005193 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5195 {
5196 if (buf->b_fname == NULL
5197 || !buf->b_p_bl
5198#ifdef FEAT_QUICKFIX
5199 || bt_quickfix(buf)
5200#endif
5201 || removable(buf->b_ffname))
5202 continue;
5203
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005204 if (max_buffers-- == 0)
5205 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 putc('%', fp);
5207 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005208 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 (long)buf->b_last_cursor.lnum,
5210 buf->b_last_cursor.col);
5211 viminfo_writestring(fp, line);
5212 }
5213 vim_free(line);
5214}
5215#endif
5216
5217
5218/*
5219 * Return special buffer name.
5220 * Returns NULL when the buffer has a normal file name.
5221 */
5222 char *
5223buf_spname(buf)
5224 buf_T *buf;
5225{
5226#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5227 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005228 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005229 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005230 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005231
5232 /*
5233 * For location list window, w_llist_ref points to the location list.
5234 * For quickfix window, w_llist_ref is NULL.
5235 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005236 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005237 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005238 goto win_found;
5239win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005240 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005241 return _(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005242 else
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005243 return _(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245#endif
5246#ifdef FEAT_QUICKFIX
5247 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5248 * contains the name as specified by the user */
5249 if (bt_nofile(buf))
5250 {
5251 if (buf->b_sfname != NULL)
5252 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005253 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255#endif
5256 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005257 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 return NULL;
5259}
5260
5261
5262#if defined(FEAT_SIGNS) || defined(PROTO)
5263/*
5264 * Insert the sign into the signlist.
5265 */
5266 static void
5267insert_sign(buf, prev, next, id, lnum, typenr)
5268 buf_T *buf; /* buffer to store sign in */
5269 signlist_T *prev; /* previous sign entry */
5270 signlist_T *next; /* next sign entry */
5271 int id; /* sign ID */
5272 linenr_T lnum; /* line number which gets the mark */
5273 int typenr; /* typenr of sign we are adding */
5274{
5275 signlist_T *newsign;
5276
5277 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5278 if (newsign != NULL)
5279 {
5280 newsign->id = id;
5281 newsign->lnum = lnum;
5282 newsign->typenr = typenr;
5283 newsign->next = next;
5284#ifdef FEAT_NETBEANS_INTG
5285 newsign->prev = prev;
5286 if (next != NULL)
5287 next->prev = newsign;
5288#endif
5289
5290 if (prev == NULL)
5291 {
5292 /* When adding first sign need to redraw the windows to create the
5293 * column for signs. */
5294 if (buf->b_signlist == NULL)
5295 {
5296 redraw_buf_later(buf, NOT_VALID);
5297 changed_cline_bef_curs();
5298 }
5299
5300 /* first sign in signlist */
5301 buf->b_signlist = newsign;
5302 }
5303 else
5304 prev->next = newsign;
5305 }
5306}
5307
5308/*
5309 * Add the sign into the signlist. Find the right spot to do it though.
5310 */
5311 void
5312buf_addsign(buf, id, lnum, typenr)
5313 buf_T *buf; /* buffer to store sign in */
5314 int id; /* sign ID */
5315 linenr_T lnum; /* line number which gets the mark */
5316 int typenr; /* typenr of sign we are adding */
5317{
5318 signlist_T *sign; /* a sign in the signlist */
5319 signlist_T *prev; /* the previous sign */
5320
5321 prev = NULL;
5322 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5323 {
5324 if (lnum == sign->lnum && id == sign->id)
5325 {
5326 sign->typenr = typenr;
5327 return;
5328 }
5329 else if (
5330#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5331 id < 0 &&
5332#endif
5333 lnum < sign->lnum)
5334 {
5335#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5336 /* XXX - GRP: Is this because of sign slide problem? Or is it
5337 * really needed? Or is it because we allow multiple signs per
5338 * line? If so, should I add that feature to FEAT_SIGNS?
5339 */
5340 while (prev != NULL && prev->lnum == lnum)
5341 prev = prev->prev;
5342 if (prev == NULL)
5343 sign = buf->b_signlist;
5344 else
5345 sign = prev->next;
5346#endif
5347 insert_sign(buf, prev, sign, id, lnum, typenr);
5348 return;
5349 }
5350 prev = sign;
5351 }
5352#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5353 /* XXX - GRP: See previous comment */
5354 while (prev != NULL && prev->lnum == lnum)
5355 prev = prev->prev;
5356 if (prev == NULL)
5357 sign = buf->b_signlist;
5358 else
5359 sign = prev->next;
5360#endif
5361 insert_sign(buf, prev, sign, id, lnum, typenr);
5362
5363 return;
5364}
5365
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005366 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367buf_change_sign_type(buf, markId, typenr)
5368 buf_T *buf; /* buffer to store sign in */
5369 int markId; /* sign ID */
5370 int typenr; /* typenr of sign we are adding */
5371{
5372 signlist_T *sign; /* a sign in the signlist */
5373
5374 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5375 {
5376 if (sign->id == markId)
5377 {
5378 sign->typenr = typenr;
5379 return sign->lnum;
5380 }
5381 }
5382
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005383 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384}
5385
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005386 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005387buf_getsigntype(buf, lnum, type)
5388 buf_T *buf;
5389 linenr_T lnum;
5390 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5391{
5392 signlist_T *sign; /* a sign in a b_signlist */
5393
5394 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5395 if (sign->lnum == lnum
5396 && (type == SIGN_ANY
5397# ifdef FEAT_SIGN_ICONS
5398 || (type == SIGN_ICON
5399 && sign_get_image(sign->typenr) != NULL)
5400# endif
5401 || (type == SIGN_TEXT
5402 && sign_get_text(sign->typenr) != NULL)
5403 || (type == SIGN_LINEHL
5404 && sign_get_attr(sign->typenr, TRUE) != 0)))
5405 return sign->typenr;
5406 return 0;
5407}
5408
5409
5410 linenr_T
5411buf_delsign(buf, id)
5412 buf_T *buf; /* buffer sign is stored in */
5413 int id; /* sign id */
5414{
5415 signlist_T **lastp; /* pointer to pointer to current sign */
5416 signlist_T *sign; /* a sign in a b_signlist */
5417 signlist_T *next; /* the next sign in a b_signlist */
5418 linenr_T lnum; /* line number whose sign was deleted */
5419
5420 lastp = &buf->b_signlist;
5421 lnum = 0;
5422 for (sign = buf->b_signlist; sign != NULL; sign = next)
5423 {
5424 next = sign->next;
5425 if (sign->id == id)
5426 {
5427 *lastp = next;
5428#ifdef FEAT_NETBEANS_INTG
5429 if (next != NULL)
5430 next->prev = sign->prev;
5431#endif
5432 lnum = sign->lnum;
5433 vim_free(sign);
5434 break;
5435 }
5436 else
5437 lastp = &sign->next;
5438 }
5439
5440 /* When deleted the last sign need to redraw the windows to remove the
5441 * sign column. */
5442 if (buf->b_signlist == NULL)
5443 {
5444 redraw_buf_later(buf, NOT_VALID);
5445 changed_cline_bef_curs();
5446 }
5447
5448 return lnum;
5449}
5450
5451
5452/*
5453 * Find the line number of the sign with the requested id. If the sign does
5454 * not exist, return 0 as the line number. This will still let the correct file
5455 * get loaded.
5456 */
5457 int
5458buf_findsign(buf, id)
5459 buf_T *buf; /* buffer to store sign in */
5460 int id; /* sign ID */
5461{
5462 signlist_T *sign; /* a sign in the signlist */
5463
5464 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5465 if (sign->id == id)
5466 return sign->lnum;
5467
5468 return 0;
5469}
5470
5471 int
5472buf_findsign_id(buf, lnum)
5473 buf_T *buf; /* buffer whose sign we are searching for */
5474 linenr_T lnum; /* line number of sign */
5475{
5476 signlist_T *sign; /* a sign in the signlist */
5477
5478 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5479 if (sign->lnum == lnum)
5480 return sign->id;
5481
5482 return 0;
5483}
5484
5485
5486# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5487/* see if a given type of sign exists on a specific line */
5488 int
5489buf_findsigntype_id(buf, lnum, typenr)
5490 buf_T *buf; /* buffer whose sign we are searching for */
5491 linenr_T lnum; /* line number of sign */
5492 int typenr; /* sign type number */
5493{
5494 signlist_T *sign; /* a sign in the signlist */
5495
5496 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5497 if (sign->lnum == lnum && sign->typenr == typenr)
5498 return sign->id;
5499
5500 return 0;
5501}
5502
5503
5504# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5505/* return the number of icons on the given line */
5506 int
5507buf_signcount(buf, lnum)
5508 buf_T *buf;
5509 linenr_T lnum;
5510{
5511 signlist_T *sign; /* a sign in the signlist */
5512 int count = 0;
5513
5514 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5515 if (sign->lnum == lnum)
5516 if (sign_get_image(sign->typenr) != NULL)
5517 count++;
5518
5519 return count;
5520}
5521# endif /* FEAT_SIGN_ICONS */
5522# endif /* FEAT_NETBEANS_INTG */
5523
5524
5525/*
5526 * Delete signs in buffer "buf".
5527 */
5528 static void
5529buf_delete_signs(buf)
5530 buf_T *buf;
5531{
5532 signlist_T *next;
5533
5534 while (buf->b_signlist != NULL)
5535 {
5536 next = buf->b_signlist->next;
5537 vim_free(buf->b_signlist);
5538 buf->b_signlist = next;
5539 }
5540}
5541
5542/*
5543 * Delete all signs in all buffers.
5544 */
5545 void
5546buf_delete_all_signs()
5547{
5548 buf_T *buf; /* buffer we are checking for signs */
5549
5550 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5551 if (buf->b_signlist != NULL)
5552 {
5553 /* Need to redraw the windows to remove the sign column. */
5554 redraw_buf_later(buf, NOT_VALID);
5555 buf_delete_signs(buf);
5556 }
5557}
5558
5559/*
5560 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5561 */
5562 void
5563sign_list_placed(rbuf)
5564 buf_T *rbuf;
5565{
5566 buf_T *buf;
5567 signlist_T *p;
5568 char lbuf[BUFSIZ];
5569
5570 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5571 msg_putchar('\n');
5572 if (rbuf == NULL)
5573 buf = firstbuf;
5574 else
5575 buf = rbuf;
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005576 while (buf != NULL && !got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 {
5578 if (buf->b_signlist != NULL)
5579 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005580 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5582 msg_putchar('\n');
5583 }
Bram Moolenaar1c0b03e2012-03-16 14:32:15 +01005584 for (p = buf->b_signlist; p != NULL && !got_int; p = p->next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005586 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5588 MSG_PUTS(lbuf);
5589 msg_putchar('\n');
5590 }
5591 if (rbuf != NULL)
5592 break;
5593 buf = buf->b_next;
5594 }
5595}
5596
5597/*
5598 * Adjust a placed sign for inserted/deleted lines.
5599 */
5600 void
5601sign_mark_adjust(line1, line2, amount, amount_after)
5602 linenr_T line1;
5603 linenr_T line2;
5604 long amount;
5605 long amount_after;
5606{
5607 signlist_T *sign; /* a sign in a b_signlist */
5608
5609 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5610 {
5611 if (sign->lnum >= line1 && sign->lnum <= line2)
5612 {
5613 if (amount == MAXLNUM)
5614 sign->lnum = line1;
5615 else
5616 sign->lnum += amount;
5617 }
5618 else if (sign->lnum > line2)
5619 sign->lnum += amount_after;
5620 }
5621}
5622#endif /* FEAT_SIGNS */
5623
5624/*
5625 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5626 */
5627 void
5628set_buflisted(on)
5629 int on;
5630{
5631 if (on != curbuf->b_p_bl)
5632 {
5633 curbuf->b_p_bl = on;
5634#ifdef FEAT_AUTOCMD
5635 if (on)
5636 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5637 else
5638 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5639#endif
5640 }
5641}
5642
5643/*
5644 * Read the file for "buf" again and check if the contents changed.
5645 * Return TRUE if it changed or this could not be checked.
5646 */
5647 int
5648buf_contents_changed(buf)
5649 buf_T *buf;
5650{
5651 buf_T *newbuf;
5652 int differ = TRUE;
5653 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005654 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 exarg_T ea;
5656
5657 /* Allocate a buffer without putting it in the buffer list. */
5658 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5659 if (newbuf == NULL)
5660 return TRUE;
5661
5662 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5663 if (prep_exarg(&ea, buf) == FAIL)
5664 {
5665 wipe_buffer(newbuf, FALSE);
5666 return TRUE;
5667 }
5668
Bram Moolenaar071d4272004-06-13 20:20:40 +00005669 /* set curwin/curbuf to buf and save a few things */
5670 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
Bram Moolenaar4770d092006-01-12 23:22:24 +00005672 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 && readfile(buf->b_ffname, buf->b_fname,
5674 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5675 &ea, READ_NEW | READ_DUMMY) == OK)
5676 {
5677 /* compare the two files line by line */
5678 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5679 {
5680 differ = FALSE;
5681 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5682 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5683 {
5684 differ = TRUE;
5685 break;
5686 }
5687 }
5688 }
5689 vim_free(ea.cmd);
5690
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 /* restore curwin/curbuf and a few other things */
5692 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694 if (curbuf != newbuf) /* safety check */
5695 wipe_buffer(newbuf, FALSE);
5696
5697 return differ;
5698}
5699
5700/*
5701 * Wipe out a buffer and decrement the last buffer number if it was used for
5702 * this buffer. Call this to wipe out a temp buffer that does not contain any
5703 * marks.
5704 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 void
5706wipe_buffer(buf, aucmd)
5707 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005708 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709{
5710 if (buf->b_fnum == top_file_num - 1)
5711 --top_file_num;
5712
5713#ifdef FEAT_AUTOCMD
5714 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005715 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716#endif
Bram Moolenaar42ec6562012-02-22 14:58:37 +01005717 close_buffer(NULL, buf, DOBUF_WIPE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718#ifdef FEAT_AUTOCMD
5719 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005720 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721#endif
5722}