blob: d4a9c1eeb16a36a2219dea635d10851089188460 [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
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020069 * Open current buffer, that is: open the memfile and read the file into
70 * memory.
71 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 */
73 int
Bram Moolenaar59f931e2010-07-24 20:27:03 +020074open_buffer(read_stdin, eap, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int read_stdin; /* read file from stdin */
76 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
Bram Moolenaar59f931e2010-07-24 20:27:03 +020077 int flags; /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078{
79 int retval = OK;
80#ifdef FEAT_AUTOCMD
81 buf_T *old_curbuf;
82#endif
83
84 /*
85 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
86 * When re-entering the same buffer, it should not change, because the
87 * user may have reset the flag by hand.
88 */
89 if (readonlymode && curbuf->b_ffname != NULL
90 && (curbuf->b_flags & BF_NEVERLOADED))
91 curbuf->b_p_ro = TRUE;
92
Bram Moolenaar4770d092006-01-12 23:22:24 +000093 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 {
95 /*
96 * There MUST be a memfile, otherwise we can't do anything
97 * If we can't create one for the current buffer, take another buffer
98 */
99 close_buffer(NULL, curbuf, 0);
100 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
101 if (curbuf->b_ml.ml_mfp != NULL)
102 break;
103 /*
104 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000105 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 */
107 if (curbuf == NULL)
108 {
109 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
110 getout(2);
111 }
112 EMSG(_("E83: Cannot allocate buffer, using other one..."));
113 enter_buffer(curbuf);
114 return FAIL;
115 }
116
117#ifdef FEAT_AUTOCMD
118 /* The autocommands in readfile() may change the buffer, but only AFTER
119 * reading the file. */
120 old_curbuf = curbuf;
121 modified_was_set = FALSE;
122#endif
123
124 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100125 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127 if (curbuf->b_ffname != NULL
128#ifdef FEAT_NETBEANS_INTG
129 && netbeansReadFile
130#endif
131 )
132 {
133#ifdef FEAT_NETBEANS_INTG
134 int oldFire = netbeansFireChanges;
135
136 netbeansFireChanges = 0;
137#endif
138 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200139 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
140 flags | READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_NETBEANS_INTG
142 netbeansFireChanges = oldFire;
143#endif
144 /* Help buffer is filtered. */
145 if (curbuf->b_help)
146 fix_help_buffer();
147 }
148 else if (read_stdin)
149 {
150 int save_bin = curbuf->b_p_bin;
151 linenr_T line_count;
152
153 /*
154 * First read the text in binary mode into the buffer.
155 * Then read from that same buffer and append at the end. This makes
156 * it possible to retry when 'fileformat' or 'fileencoding' was
157 * guessed wrong.
158 */
159 curbuf->b_p_bin = TRUE;
160 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200161 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
162 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 curbuf->b_p_bin = save_bin;
164 if (retval == OK)
165 {
166 line_count = curbuf->b_ml.ml_line_count;
167 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200168 (linenr_T)0, (linenr_T)MAXLNUM, eap,
169 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (retval == OK)
171 {
172 /* Delete the binary lines. */
173 while (--line_count >= 0)
174 ml_delete((linenr_T)1, FALSE);
175 }
176 else
177 {
178 /* Delete the converted lines. */
179 while (curbuf->b_ml.ml_line_count > line_count)
180 ml_delete(line_count, FALSE);
181 }
182 /* Put the cursor on the first line. */
183 curwin->w_cursor.lnum = 1;
184 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000185
186 /* Set or reset 'modified' before executing autocommands, so that
187 * it can be changed there. */
188 if (!readonlymode && !bufempty())
189 changed();
190 else if (retval != FAIL)
191 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#ifdef FEAT_AUTOCMD
193# ifdef FEAT_EVAL
194 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
195 curbuf, &retval);
196# else
197 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
198# endif
199#endif
200 }
201 }
202
203 /* if first time loading this buffer, init b_chartab[] */
204 if (curbuf->b_flags & BF_NEVERLOADED)
205 (void)buf_init_chartab(curbuf, FALSE);
206
207 /*
208 * Set/reset the Changed flag first, autocmds may change the buffer.
209 * Apply the automatic commands, before processing the modelines.
210 * So the modelines have priority over auto commands.
211 */
212 /* When reading stdin, the buffer contents always needs writing, so set
213 * the changed flag. Unless in readonly mode: "ls | gview -".
214 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000215 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifdef FEAT_AUTOCMD
217 || modified_was_set /* ":set modified" used in autocmd */
218# ifdef FEAT_EVAL
219 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
220# endif
221#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000222 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000224 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 unchanged(curbuf, FALSE);
226 save_file_ff(curbuf); /* keep this fileformat */
227
228 /* require "!" to overwrite the file, because it wasn't read completely */
229#ifdef FEAT_EVAL
230 if (aborting())
231#else
232 if (got_int)
233#endif
234 curbuf->b_flags |= BF_READERR;
235
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000236#ifdef FEAT_FOLDING
237 /* Need to update automatic folding. Do this before the autocommands,
238 * they may use the fold info. */
239 foldUpdateAll(curwin);
240#endif
241
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#ifdef FEAT_AUTOCMD
243 /* need to set w_topline, unless some autocommand already did that. */
244 if (!(curwin->w_valid & VALID_TOPLINE))
245 {
246 curwin->w_topline = 1;
247# ifdef FEAT_DIFF
248 curwin->w_topfill = 0;
249# endif
250 }
251# ifdef FEAT_EVAL
252 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
253# else
254 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
255# endif
256#endif
257
258 if (retval != FAIL)
259 {
260#ifdef FEAT_AUTOCMD
261 /*
262 * The autocommands may have changed the current buffer. Apply the
263 * modelines to the correct buffer, if it still exists and is loaded.
264 */
265 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
266 {
267 aco_save_T aco;
268
269 /* Go to the buffer that was opened. */
270 aucmd_prepbuf(&aco, old_curbuf);
271#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000272 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
274
275#ifdef FEAT_AUTOCMD
276# ifdef FEAT_EVAL
277 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
278 &retval);
279# else
280 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
281# endif
282
283 /* restore curwin/curbuf and a few other things */
284 aucmd_restbuf(&aco);
285 }
286#endif
287 }
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 return retval;
290}
291
292/*
293 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
294 */
295 int
296buf_valid(buf)
297 buf_T *buf;
298{
299 buf_T *bp;
300
301 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
302 if (bp == buf)
303 return TRUE;
304 return FALSE;
305}
306
307/*
308 * Close the link to a buffer.
309 * "action" is used when there is no longer a window for the buffer.
310 * It can be:
311 * 0 buffer becomes hidden
312 * DOBUF_UNLOAD buffer is unloaded
313 * DOBUF_DELETE buffer is unloaded and removed from buffer list
314 * DOBUF_WIPE buffer is unloaded and really deleted
315 * When doing all but the first one on the current buffer, the caller should
316 * get a new buffer very soon!
317 *
318 * The 'bufhidden' option can force freeing and deleting.
319 */
320 void
321close_buffer(win, buf, action)
322 win_T *win; /* if not NULL, set b_last_cursor */
323 buf_T *buf;
324 int action;
325{
326#ifdef FEAT_AUTOCMD
327 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100328 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#endif
330 int unload_buf = (action != 0);
331 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
332 int wipe_buf = (action == DOBUF_WIPE);
333
334#ifdef FEAT_QUICKFIX
335 /*
336 * Force unloading or deleting when 'bufhidden' says so.
337 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
338 * "hide" (otherwise we could never free or delete a buffer).
339 */
340 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
341 {
342 del_buf = TRUE;
343 unload_buf = TRUE;
344 }
345 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
346 {
347 del_buf = TRUE;
348 unload_buf = TRUE;
349 wipe_buf = TRUE;
350 }
351 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
352 unload_buf = TRUE;
353#endif
354
355 if (win != NULL)
356 {
357 /* Set b_last_cursor when closing the last window for the buffer.
358 * Remember the last cursor position and window options of the buffer.
359 * This used to be only for the current window, but then options like
360 * 'foldmethod' may be lost with a ":only" command. */
361 if (buf->b_nwindows == 1)
362 set_last_cursor(win);
363 buflist_setfpos(buf, win,
364 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
365 win->w_cursor.col, TRUE);
366 }
367
368#ifdef FEAT_AUTOCMD
369 /* When the buffer is no longer in a window, trigger BufWinLeave */
370 if (buf->b_nwindows == 1)
371 {
372 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
373 FALSE, buf);
374 if (!buf_valid(buf)) /* autocommands may delete the buffer */
375 return;
376
377 /* When the buffer becomes hidden, but is not unloaded, trigger
378 * BufHidden */
379 if (!unload_buf)
380 {
381 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
382 FALSE, buf);
383 if (!buf_valid(buf)) /* autocmds may delete the buffer */
384 return;
385 }
386# ifdef FEAT_EVAL
387 if (aborting()) /* autocmds may abort script processing */
388 return;
389# endif
390 }
391 nwindows = buf->b_nwindows;
392#endif
393
394 /* decrease the link count from windows (unless not in any window) */
395 if (buf->b_nwindows > 0)
396 --buf->b_nwindows;
397
398 /* Return when a window is displaying the buffer or when it's not
399 * unloaded. */
400 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
403 /* Always remove the buffer when there is no file name. */
404 if (buf->b_ffname == NULL)
405 del_buf = TRUE;
406
407 /*
408 * Free all things allocated for this buffer.
409 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
410 */
411#ifdef FEAT_AUTOCMD
412 /* Remember if we are closing the current buffer. Restore the number of
413 * windows, so that autocommands in buf_freeall() don't get confused. */
414 is_curbuf = (buf == curbuf);
415 buf->b_nwindows = nwindows;
416#endif
417
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200418 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200419 if (
420#ifdef FEAT_WINDOWS
421 win_valid(win) &&
422#endif
423 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200424 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425
426#ifdef FEAT_AUTOCMD
427 /* Autocommands may have deleted the buffer. */
428 if (!buf_valid(buf))
429 return;
430# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000431 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 return;
433# endif
434
435 /* Autocommands may have opened or closed windows for this buffer.
436 * Decrement the count for the close we do here. */
437 if (buf->b_nwindows > 0)
438 --buf->b_nwindows;
439
440 /*
441 * It's possible that autocommands change curbuf to the one being deleted.
442 * This might cause the previous curbuf to be deleted unexpectedly. But
443 * in some cases it's OK to delete the curbuf, because a new one is
444 * obtained anyway. Therefore only return if curbuf changed to the
445 * deleted buffer.
446 */
447 if (buf == curbuf && !is_curbuf)
448 return;
449#endif
450
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000451 /* Change directories when the 'acd' option is set. */
452 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453
454 /*
455 * Remove the buffer from the list.
456 */
457 if (wipe_buf)
458 {
459#ifdef FEAT_SUN_WORKSHOP
460 if (usingSunWorkShop)
461 workshop_file_closed_lineno((char *)buf->b_ffname,
462 (int)buf->b_last_cursor.lnum);
463#endif
464 vim_free(buf->b_ffname);
465 vim_free(buf->b_sfname);
466 if (buf->b_prev == NULL)
467 firstbuf = buf->b_next;
468 else
469 buf->b_prev->b_next = buf->b_next;
470 if (buf->b_next == NULL)
471 lastbuf = buf->b_prev;
472 else
473 buf->b_next->b_prev = buf->b_prev;
474 free_buffer(buf);
475 }
476 else
477 {
478 if (del_buf)
479 {
480 /* Free all internal variables and reset option values, to make
481 * ":bdel" compatible with Vim 5.7. */
482 free_buffer_stuff(buf, TRUE);
483
484 /* Make it look like a new buffer. */
485 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
486
487 /* Init the options when loaded again. */
488 buf->b_p_initialized = FALSE;
489 }
490 buf_clear_file(buf);
491 if (del_buf)
492 buf->b_p_bl = FALSE;
493 }
494}
495
496/*
497 * Make buffer not contain a file.
498 */
499 void
500buf_clear_file(buf)
501 buf_T *buf;
502{
503 buf->b_ml.ml_line_count = 1;
504 unchanged(buf, TRUE);
505#ifndef SHORT_FNAME
506 buf->b_shortname = FALSE;
507#endif
508 buf->b_p_eol = TRUE;
509 buf->b_start_eol = TRUE;
510#ifdef FEAT_MBYTE
511 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000512 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513#endif
514 buf->b_ml.ml_mfp = NULL;
515 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
516#ifdef FEAT_NETBEANS_INTG
517 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#endif
519}
520
521/*
522 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200523 * the file. flags:
524 * BFA_DEL buffer is going to be deleted
525 * BFA_WIPE buffer is going to be wiped out
526 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200529buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200531 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532{
533#ifdef FEAT_AUTOCMD
534 int is_curbuf = (buf == curbuf);
535
536 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
537 if (!buf_valid(buf)) /* autocommands may delete the buffer */
538 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200539 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
541 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
542 if (!buf_valid(buf)) /* autocommands may delete the buffer */
543 return;
544 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200545 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 {
547 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
548 FALSE, buf);
549 if (!buf_valid(buf)) /* autocommands may delete the buffer */
550 return;
551 }
552# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000553 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 return;
555# endif
556
557 /*
558 * It's possible that autocommands change curbuf to the one being deleted.
559 * This might cause curbuf to be deleted unexpectedly. But in some cases
560 * it's OK to delete the curbuf, because a new one is obtained anyway.
561 * Therefore only return if curbuf changed to the deleted buffer.
562 */
563 if (buf == curbuf && !is_curbuf)
564 return;
565#endif
566#ifdef FEAT_DIFF
567 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
568#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200569#ifdef FEAT_SYN_HL
570 if (curwin->w_buffer == buf)
571 reset_synblock(curwin); /* remove any ownsyntax */
572#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000573
574#ifdef FEAT_FOLDING
575 /* No folds in an empty buffer. */
576# ifdef FEAT_WINDOWS
577 {
578 win_T *win;
579 tabpage_T *tp;
580
581 FOR_ALL_TAB_WINDOWS(tp, win)
582 if (win->w_buffer == buf)
583 clearFolding(win);
584 }
585# else
586 if (curwin->w_buffer == buf)
587 clearFolding(curwin);
588# endif
589#endif
590
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#ifdef FEAT_TCL
592 tcl_buffer_free(buf);
593#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 ml_close(buf, TRUE); /* close and delete the memline/memfile */
595 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200596 if ((flags & BFA_KEEP_UNDO) == 0)
597 {
598 u_blockfree(buf); /* free the memory allocated for undo */
599 u_clearall(buf); /* reset all undo information */
600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200602 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000604 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605}
606
607/*
608 * Free a buffer structure and the things it contains related to the buffer
609 * itself (not the file, that must have been done already).
610 */
611 static void
612free_buffer(buf)
613 buf_T *buf;
614{
615 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200616#ifdef FEAT_LUA
617 lua_buffer_free(buf);
618#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000619#ifdef FEAT_MZSCHEME
620 mzscheme_buffer_free(buf);
621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622#ifdef FEAT_PERL
623 perl_buf_free(buf);
624#endif
625#ifdef FEAT_PYTHON
626 python_buffer_free(buf);
627#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200628#ifdef FEAT_PYTHON3
629 python3_buffer_free(buf);
630#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000631#ifdef FEAT_RUBY
632 ruby_buffer_free(buf);
633#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000634#ifdef FEAT_AUTOCMD
635 aubuflocal_remove(buf);
636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637 vim_free(buf);
638}
639
640/*
641 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
642 */
643 static void
644free_buffer_stuff(buf, free_options)
645 buf_T *buf;
646 int free_options; /* free options as well */
647{
648 if (free_options)
649 {
650 clear_wininfo(buf); /* including window-local options */
651 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200652#ifdef FEAT_SPELL
653 ga_clear(&buf->b_s.b_langp);
654#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655 }
656#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000657 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
658 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#endif
660#ifdef FEAT_USR_CMDS
661 uc_clear(&buf->b_ucmds); /* clear local user commands */
662#endif
663#ifdef FEAT_SIGNS
664 buf_delete_signs(buf); /* delete any signs */
665#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000666#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200667 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000669#ifdef FEAT_LOCALMAP
670 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
671 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
672#endif
673#ifdef FEAT_MBYTE
674 vim_free(buf->b_start_fenc);
675 buf->b_start_fenc = NULL;
676#endif
677}
678
679/*
680 * Free the b_wininfo list for buffer "buf".
681 */
682 static void
683clear_wininfo(buf)
684 buf_T *buf;
685{
686 wininfo_T *wip;
687
688 while (buf->b_wininfo != NULL)
689 {
690 wip = buf->b_wininfo;
691 buf->b_wininfo = wip->wi_next;
692 if (wip->wi_optset)
693 {
694 clear_winopt(&wip->wi_opt);
695#ifdef FEAT_FOLDING
696 deleteFoldRecurse(&wip->wi_folds);
697#endif
698 }
699 vim_free(wip);
700 }
701}
702
703#if defined(FEAT_LISTCMDS) || defined(PROTO)
704/*
705 * Go to another buffer. Handles the result of the ATTENTION dialog.
706 */
707 void
708goto_buffer(eap, start, dir, count)
709 exarg_T *eap;
710 int start;
711 int dir;
712 int count;
713{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000714# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 buf_T *old_curbuf = curbuf;
716
717 swap_exists_action = SEA_DIALOG;
718# endif
719 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
720 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000721# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
723 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000724# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
725 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000726
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000727 /* Reset the error/interrupt/exception state here so that
728 * aborting() returns FALSE when closing a window. */
729 enter_cleanup(&cs);
730# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000731
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000732 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 win_close(curwin, TRUE);
734 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000735 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000736
737# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
738 /* Restore the error/interrupt/exception state if not discarded by a
739 * new aborting error, interrupt, or uncaught exception. */
740 leave_cleanup(&cs);
741# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 }
743 else
744 handle_swap_exists(old_curbuf);
745# endif
746}
747#endif
748
Bram Moolenaare64ac772005-12-07 20:54:59 +0000749#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750/*
751 * Handle the situation of swap_exists_action being set.
752 * It is allowed for "old_curbuf" to be NULL or invalid.
753 */
754 void
755handle_swap_exists(old_curbuf)
756 buf_T *old_curbuf;
757{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000758# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
759 cleanup_T cs;
760# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000761
Bram Moolenaar071d4272004-06-13 20:20:40 +0000762 if (swap_exists_action == SEA_QUIT)
763 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000764# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
765 /* Reset the error/interrupt/exception state here so that
766 * aborting() returns FALSE when closing a buffer. */
767 enter_cleanup(&cs);
768# endif
769
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770 /* User selected Quit at ATTENTION prompt. Go back to previous
771 * buffer. If that buffer is gone or the same as the current one,
772 * open a new, empty buffer. */
773 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000774 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000775 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
776 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000777 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 if (old_curbuf != NULL)
779 enter_buffer(old_curbuf);
780 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000781
782# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
783 /* Restore the error/interrupt/exception state if not discarded by a
784 * new aborting error, interrupt, or uncaught exception. */
785 leave_cleanup(&cs);
786# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 }
788 else if (swap_exists_action == SEA_RECOVER)
789 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000790# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
791 /* Reset the error/interrupt/exception state here so that
792 * aborting() returns FALSE when closing a buffer. */
793 enter_cleanup(&cs);
794# endif
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 /* User selected Recover at ATTENTION prompt. */
797 msg_scroll = TRUE;
798 ml_recover();
799 MSG_PUTS("\n"); /* don't overwrite the last message */
800 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000801 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000802
803# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
804 /* Restore the error/interrupt/exception state if not discarded by a
805 * new aborting error, interrupt, or uncaught exception. */
806 leave_cleanup(&cs);
807# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809 swap_exists_action = SEA_NONE;
810}
811#endif
812
813#if defined(FEAT_LISTCMDS) || defined(PROTO)
814/*
815 * do_bufdel() - delete or unload buffer(s)
816 *
817 * addr_count == 0: ":bdel" - delete current buffer
818 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
819 * buffer "end_bnr", then any other arguments.
820 * addr_count == 2: ":N,N bdel" - delete buffers in range
821 *
822 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
823 * DOBUF_DEL (":bdel")
824 *
825 * Returns error message or NULL
826 */
827 char_u *
828do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
829 int command;
830 char_u *arg; /* pointer to extra arguments */
831 int addr_count;
832 int start_bnr; /* first buffer number in a range */
833 int end_bnr; /* buffer nr or last buffer nr in a range */
834 int forceit;
835{
836 int do_current = 0; /* delete current buffer? */
837 int deleted = 0; /* number of buffers deleted */
838 char_u *errormsg = NULL; /* return value */
839 int bnr; /* buffer number */
840 char_u *p;
841
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (addr_count == 0)
843 {
844 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
845 }
846 else
847 {
848 if (addr_count == 2)
849 {
850 if (*arg) /* both range and argument is not allowed */
851 return (char_u *)_(e_trailing);
852 bnr = start_bnr;
853 }
854 else /* addr_count == 1 */
855 bnr = end_bnr;
856
857 for ( ;!got_int; ui_breakcheck())
858 {
859 /*
860 * delete the current buffer last, otherwise when the
861 * current buffer is deleted, the next buffer becomes
862 * the current one and will be loaded, which may then
863 * also be deleted, etc.
864 */
865 if (bnr == curbuf->b_fnum)
866 do_current = bnr;
867 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
868 forceit) == OK)
869 ++deleted;
870
871 /*
872 * find next buffer number to delete/unload
873 */
874 if (addr_count == 2)
875 {
876 if (++bnr > end_bnr)
877 break;
878 }
879 else /* addr_count == 1 */
880 {
881 arg = skipwhite(arg);
882 if (*arg == NUL)
883 break;
884 if (!VIM_ISDIGIT(*arg))
885 {
886 p = skiptowhite_esc(arg);
887 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
888 if (bnr < 0) /* failed */
889 break;
890 arg = p;
891 }
892 else
893 bnr = getdigits(&arg);
894 }
895 }
896 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
897 FORWARD, do_current, forceit) == OK)
898 ++deleted;
899
900 if (deleted == 0)
901 {
902 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000903 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000905 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000907 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 errormsg = IObuff;
909 }
910 else if (deleted >= p_report)
911 {
912 if (command == DOBUF_UNLOAD)
913 {
914 if (deleted == 1)
915 MSG(_("1 buffer unloaded"));
916 else
917 smsg((char_u *)_("%d buffers unloaded"), deleted);
918 }
919 else if (command == DOBUF_DEL)
920 {
921 if (deleted == 1)
922 MSG(_("1 buffer deleted"));
923 else
924 smsg((char_u *)_("%d buffers deleted"), deleted);
925 }
926 else
927 {
928 if (deleted == 1)
929 MSG(_("1 buffer wiped out"));
930 else
931 smsg((char_u *)_("%d buffers wiped out"), deleted);
932 }
933 }
934 }
935
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936
937 return errormsg;
938}
939
940/*
941 * Implementation of the commands for the buffer list.
942 *
943 * action == DOBUF_GOTO go to specified buffer
944 * action == DOBUF_SPLIT split window and go to specified buffer
945 * action == DOBUF_UNLOAD unload specified buffer(s)
946 * action == DOBUF_DEL delete specified buffer(s) from buffer list
947 * action == DOBUF_WIPE delete specified buffer(s) really
948 *
949 * start == DOBUF_CURRENT go to "count" buffer from current buffer
950 * start == DOBUF_FIRST go to "count" buffer from first buffer
951 * start == DOBUF_LAST go to "count" buffer from last buffer
952 * start == DOBUF_MOD go to "count" modified buffer from current buffer
953 *
954 * Return FAIL or OK.
955 */
956 int
957do_buffer(action, start, dir, count, forceit)
958 int action;
959 int start;
960 int dir; /* FORWARD or BACKWARD */
961 int count; /* buffer number or number of buffers */
962 int forceit; /* TRUE for :...! */
963{
964 buf_T *buf;
965 buf_T *bp;
966 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
967 || action == DOBUF_WIPE);
968
969 switch (start)
970 {
971 case DOBUF_FIRST: buf = firstbuf; break;
972 case DOBUF_LAST: buf = lastbuf; break;
973 default: buf = curbuf; break;
974 }
975 if (start == DOBUF_MOD) /* find next modified buffer */
976 {
977 while (count-- > 0)
978 {
979 do
980 {
981 buf = buf->b_next;
982 if (buf == NULL)
983 buf = firstbuf;
984 }
985 while (buf != curbuf && !bufIsChanged(buf));
986 }
987 if (!bufIsChanged(buf))
988 {
989 EMSG(_("E84: No modified buffer found"));
990 return FAIL;
991 }
992 }
993 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
994 {
995 while (buf != NULL && buf->b_fnum != count)
996 buf = buf->b_next;
997 }
998 else
999 {
1000 bp = NULL;
1001 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1002 {
1003 /* remember the buffer where we start, we come back there when all
1004 * buffers are unlisted. */
1005 if (bp == NULL)
1006 bp = buf;
1007 if (dir == FORWARD)
1008 {
1009 buf = buf->b_next;
1010 if (buf == NULL)
1011 buf = firstbuf;
1012 }
1013 else
1014 {
1015 buf = buf->b_prev;
1016 if (buf == NULL)
1017 buf = lastbuf;
1018 }
1019 /* don't count unlisted buffers */
1020 if (unload || buf->b_p_bl)
1021 {
1022 --count;
1023 bp = NULL; /* use this buffer as new starting point */
1024 }
1025 if (bp == buf)
1026 {
1027 /* back where we started, didn't find anything. */
1028 EMSG(_("E85: There is no listed buffer"));
1029 return FAIL;
1030 }
1031 }
1032 }
1033
1034 if (buf == NULL) /* could not find it */
1035 {
1036 if (start == DOBUF_FIRST)
1037 {
1038 /* don't warn when deleting */
1039 if (!unload)
1040 EMSGN(_("E86: Buffer %ld does not exist"), count);
1041 }
1042 else if (dir == FORWARD)
1043 EMSG(_("E87: Cannot go beyond last buffer"));
1044 else
1045 EMSG(_("E88: Cannot go before first buffer"));
1046 return FAIL;
1047 }
1048
1049#ifdef FEAT_GUI
1050 need_mouse_correct = TRUE;
1051#endif
1052
1053#ifdef FEAT_LISTCMDS
1054 /*
1055 * delete buffer buf from memory and/or the list
1056 */
1057 if (unload)
1058 {
1059 int forward;
1060 int retval;
1061
1062 /* When unloading or deleting a buffer that's already unloaded and
1063 * unlisted: fail silently. */
1064 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1065 return FAIL;
1066
1067 if (!forceit && bufIsChanged(buf))
1068 {
1069#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1070 if ((p_confirm || cmdmod.confirm) && p_write)
1071 {
1072 dialog_changed(buf, FALSE);
1073# ifdef FEAT_AUTOCMD
1074 if (!buf_valid(buf))
1075 /* Autocommand deleted buffer, oops! It's not changed
1076 * now. */
1077 return FAIL;
1078# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001079 /* If it's still changed fail silently, the dialog already
1080 * mentioned why it fails. */
1081 if (bufIsChanged(buf))
1082 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001083 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001084 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085#endif
1086 {
1087 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1088 buf->b_fnum);
1089 return FAIL;
1090 }
1091 }
1092
1093 /*
1094 * If deleting the last (listed) buffer, make it empty.
1095 * The last (listed) buffer cannot be unloaded.
1096 */
1097 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1098 if (bp->b_p_bl && bp != buf)
1099 break;
1100 if (bp == NULL && buf == curbuf)
1101 {
1102 if (action == DOBUF_UNLOAD)
1103 {
1104 EMSG(_("E90: Cannot unload last buffer"));
1105 return FAIL;
1106 }
1107
1108 /* Close any other windows on this buffer, then make it empty. */
1109#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001110 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#endif
1112 setpcmark();
1113 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001114 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115
1116 /*
1117 * do_ecmd() may create a new buffer, then we have to delete
1118 * the old one. But do_ecmd() may have done that already, check
1119 * if the buffer still exists.
1120 */
1121 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1122 close_buffer(NULL, buf, action);
1123 return retval;
1124 }
1125
1126#ifdef FEAT_WINDOWS
1127 /*
1128 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001129 * (unless it's the only window). Repeat this so long as we end up in
1130 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001132 while (buf == curbuf
1133 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 win_close(curwin, FALSE);
1135#endif
1136
1137 /*
1138 * If the buffer to be deleted is not the current one, delete it here.
1139 */
1140 if (buf != curbuf)
1141 {
1142#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001143 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
1145 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1146 close_buffer(NULL, buf, action);
1147 return OK;
1148 }
1149
1150 /*
1151 * Deleting the current buffer: Need to find another buffer to go to.
1152 * There must be another, otherwise it would have been handled above.
1153 * First use au_new_curbuf, if it is valid.
1154 * Then prefer the buffer we most recently visited.
1155 * Else try to find one that is loaded, after the current buffer,
1156 * then before the current buffer.
1157 * Finally use any buffer.
1158 */
1159 buf = NULL; /* selected buffer */
1160 bp = NULL; /* used when no loaded buffer found */
1161#ifdef FEAT_AUTOCMD
1162 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1163 buf = au_new_curbuf;
1164# ifdef FEAT_JUMPLIST
1165 else
1166# endif
1167#endif
1168#ifdef FEAT_JUMPLIST
1169 if (curwin->w_jumplistlen > 0)
1170 {
1171 int jumpidx;
1172
1173 jumpidx = curwin->w_jumplistidx - 1;
1174 if (jumpidx < 0)
1175 jumpidx = curwin->w_jumplistlen - 1;
1176
1177 forward = jumpidx;
1178 while (jumpidx != curwin->w_jumplistidx)
1179 {
1180 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1181 if (buf != NULL)
1182 {
1183 if (buf == curbuf || !buf->b_p_bl)
1184 buf = NULL; /* skip current and unlisted bufs */
1185 else if (buf->b_ml.ml_mfp == NULL)
1186 {
1187 /* skip unloaded buf, but may keep it for later */
1188 if (bp == NULL)
1189 bp = buf;
1190 buf = NULL;
1191 }
1192 }
1193 if (buf != NULL) /* found a valid buffer: stop searching */
1194 break;
1195 /* advance to older entry in jump list */
1196 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1197 break;
1198 if (--jumpidx < 0)
1199 jumpidx = curwin->w_jumplistlen - 1;
1200 if (jumpidx == forward) /* List exhausted for sure */
1201 break;
1202 }
1203 }
1204#endif
1205
1206 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1207 {
1208 forward = TRUE;
1209 buf = curbuf->b_next;
1210 for (;;)
1211 {
1212 if (buf == NULL)
1213 {
1214 if (!forward) /* tried both directions */
1215 break;
1216 buf = curbuf->b_prev;
1217 forward = FALSE;
1218 continue;
1219 }
1220 /* in non-help buffer, try to skip help buffers, and vv */
1221 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1222 {
1223 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1224 break;
1225 if (bp == NULL) /* remember unloaded buf for later */
1226 bp = buf;
1227 }
1228 if (forward)
1229 buf = buf->b_next;
1230 else
1231 buf = buf->b_prev;
1232 }
1233 }
1234 if (buf == NULL) /* No loaded buffer, use unloaded one */
1235 buf = bp;
1236 if (buf == NULL) /* No loaded buffer, find listed one */
1237 {
1238 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1239 if (buf->b_p_bl && buf != curbuf)
1240 break;
1241 }
1242 if (buf == NULL) /* Still no buffer, just take one */
1243 {
1244 if (curbuf->b_next != NULL)
1245 buf = curbuf->b_next;
1246 else
1247 buf = curbuf->b_prev;
1248 }
1249 }
1250
1251 /*
1252 * make buf current buffer
1253 */
1254 if (action == DOBUF_SPLIT) /* split window first */
1255 {
1256# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001257 /* If 'switchbuf' contains "useopen": jump to first window containing
1258 * "buf" if one exists */
1259 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001260 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001261 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001262 * page containing "buf" if one exists */
1263 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 return OK;
1265 if (win_split(0, 0) == FAIL)
1266# endif
1267 return FAIL;
1268 }
1269#endif
1270
1271 /* go to current buffer - nothing to do */
1272 if (buf == curbuf)
1273 return OK;
1274
1275 /*
1276 * Check if the current buffer may be abandoned.
1277 */
1278 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1279 {
1280#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1281 if ((p_confirm || cmdmod.confirm) && p_write)
1282 {
1283 dialog_changed(curbuf, FALSE);
1284# ifdef FEAT_AUTOCMD
1285 if (!buf_valid(buf))
1286 /* Autocommand deleted buffer, oops! */
1287 return FAIL;
1288# endif
1289 }
1290 if (bufIsChanged(curbuf))
1291#endif
1292 {
1293 EMSG(_(e_nowrtmsg));
1294 return FAIL;
1295 }
1296 }
1297
1298 /* Go to the other buffer. */
1299 set_curbuf(buf, action);
1300
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001301#if defined(FEAT_LISTCMDS) \
1302 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001304 {
1305 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307#endif
1308
1309#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1310 if (aborting()) /* autocmds may abort script processing */
1311 return FAIL;
1312#endif
1313
1314 return OK;
1315}
1316
1317#endif /* FEAT_LISTCMDS */
1318
1319/*
1320 * Set current buffer to "buf". Executes autocommands and closes current
1321 * buffer. "action" tells how to close the current buffer:
1322 * DOBUF_GOTO free or hide it
1323 * DOBUF_SPLIT nothing
1324 * DOBUF_UNLOAD unload it
1325 * DOBUF_DEL delete it
1326 * DOBUF_WIPE wipe it out
1327 */
1328 void
1329set_curbuf(buf, action)
1330 buf_T *buf;
1331 int action;
1332{
1333 buf_T *prevbuf;
1334 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1335 || action == DOBUF_WIPE);
1336
1337 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001338 if (!cmdmod.keepalt)
1339 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001340 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341
1342#ifdef FEAT_VISUAL
1343 /* Don't restart Select mode after switching to another buffer. */
1344 VIsual_reselect = FALSE;
1345#endif
1346
1347 /* close_windows() or apply_autocmds() may change curbuf */
1348 prevbuf = curbuf;
1349
1350#ifdef FEAT_AUTOCMD
1351 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1352# ifdef FEAT_EVAL
1353 if (buf_valid(prevbuf) && !aborting())
1354# else
1355 if (buf_valid(prevbuf))
1356# endif
1357#endif
1358 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001359#ifdef FEAT_SYN_HL
1360 if (prevbuf == curwin->w_buffer)
1361 reset_synblock(curwin);
1362#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363#ifdef FEAT_WINDOWS
1364 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001365 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#endif
1367#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1368 if (buf_valid(prevbuf) && !aborting())
1369#else
1370 if (buf_valid(prevbuf))
1371#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001372 {
1373 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001374 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1376 unload ? action : (action == DOBUF_GOTO
1377 && !P_HID(prevbuf)
1378 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 }
1381#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001382 /* An autocommand may have deleted "buf", already entered it (e.g., when
1383 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001385 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001387 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388# endif
1389#endif
1390 enter_buffer(buf);
1391}
1392
1393/*
1394 * Enter a new current buffer.
1395 * Old curbuf must have been abandoned already!
1396 */
1397 void
1398enter_buffer(buf)
1399 buf_T *buf;
1400{
1401 /* Copy buffer and window local option values. Not for a help buffer. */
1402 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1403 if (!buf->b_help)
1404 get_winopts(buf);
1405#ifdef FEAT_FOLDING
1406 else
1407 /* Remove all folds in the window. */
1408 clearFolding(curwin);
1409 foldUpdateAll(curwin); /* update folds (later). */
1410#endif
1411
1412 /* Get the buffer in the current window. */
1413 curwin->w_buffer = buf;
1414 curbuf = buf;
1415 ++curbuf->b_nwindows;
1416
1417#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001418 if (curwin->w_p_diff)
1419 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420#endif
1421
Bram Moolenaara971b822011-09-14 14:43:25 +02001422#ifdef FEAT_SYN_HL
1423 curwin->w_s = &(buf->b_s);
1424#endif
1425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 /* Cursor on first line by default. */
1427 curwin->w_cursor.lnum = 1;
1428 curwin->w_cursor.col = 0;
1429#ifdef FEAT_VIRTUALEDIT
1430 curwin->w_cursor.coladd = 0;
1431#endif
1432 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001433#ifdef FEAT_AUTOCMD
1434 curwin->w_topline_was_set = FALSE;
1435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001437 /* mark cursor position as being invalid */
1438 curwin->w_valid = 0;
1439
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /* Make sure the buffer is loaded. */
1441 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001442 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001443#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001444 /* If there is no filetype, allow for detecting one. Esp. useful for
1445 * ":ball" used in a autocommand. If there already is a filetype we
1446 * might prefer to keep it. */
1447 if (*curbuf->b_p_ft == NUL)
1448 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001449#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001450
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001451 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 else
1454 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001455 if (!msg_silent)
1456 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1458#ifdef FEAT_AUTOCMD
1459 curwin->w_topline = 1;
1460# ifdef FEAT_DIFF
1461 curwin->w_topfill = 0;
1462# endif
1463 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1464 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1465#endif
1466 }
1467
1468 /* If autocommands did not change the cursor position, restore cursor lnum
1469 * and possibly cursor col. */
1470 if (curwin->w_cursor.lnum == 1 && inindent(0))
1471 buflist_getfpos();
1472
1473 check_arg_idx(curwin); /* check for valid arg_idx */
1474#ifdef FEAT_TITLE
1475 maketitle();
1476#endif
1477#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001478 /* when autocmds didn't change it */
1479 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#endif
1481 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1482
Bram Moolenaar009b2592004-10-24 19:18:58 +00001483#ifdef FEAT_NETBEANS_INTG
1484 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001485 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001486#endif
1487
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001488 /* Change directories when the 'acd' option is set. */
1489 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490
1491#ifdef FEAT_KEYMAP
1492 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001493 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001495#ifdef FEAT_SPELL
1496 /* May need to set the spell language. Can only do this after the buffer
1497 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001498 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1499 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001500#endif
1501
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 redraw_later(NOT_VALID);
1503}
1504
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001505#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1506/*
1507 * Change to the directory of the current buffer.
1508 */
1509 void
1510do_autochdir()
1511{
1512 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1513 shorten_fnames(TRUE);
1514}
1515#endif
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517/*
1518 * functions for dealing with the buffer list
1519 */
1520
1521/*
1522 * Add a file name to the buffer list. Return a pointer to the buffer.
1523 * If the same file name already exists return a pointer to that buffer.
1524 * If it does not exist, or if fname == NULL, a new entry is created.
1525 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1526 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1527 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001528 * This is the ONLY way to create a new buffer.
1529 */
1530static int top_file_num = 1; /* highest file number */
1531
1532 buf_T *
1533buflist_new(ffname, sfname, lnum, flags)
1534 char_u *ffname; /* full path of fname or relative */
1535 char_u *sfname; /* short fname or NULL */
1536 linenr_T lnum; /* preferred cursor line */
1537 int flags; /* BLN_ defines */
1538{
1539 buf_T *buf;
1540#ifdef UNIX
1541 struct stat st;
1542#endif
1543
1544 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1545
1546 /*
1547 * If file name already exists in the list, update the entry.
1548 */
1549#ifdef UNIX
1550 /* On Unix we can use inode numbers when the file exists. Works better
1551 * for hard links. */
1552 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1553 st.st_dev = (dev_T)-1;
1554#endif
1555 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1556#ifdef UNIX
1557 buflist_findname_stat(ffname, &st)
1558#else
1559 buflist_findname(ffname)
1560#endif
1561 ) != NULL)
1562 {
1563 vim_free(ffname);
1564 if (lnum != 0)
1565 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1566 /* copy the options now, if 'cpo' doesn't have 's' and not done
1567 * already */
1568 buf_copy_options(buf, 0);
1569 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1570 {
1571 buf->b_p_bl = TRUE;
1572#ifdef FEAT_AUTOCMD
1573 if (!(flags & BLN_DUMMY))
1574 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1575#endif
1576 }
1577 return buf;
1578 }
1579
1580 /*
1581 * If the current buffer has no name and no contents, use the current
1582 * buffer. Otherwise: Need to allocate a new buffer structure.
1583 *
1584 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001585 * (A spell file buffer is allocated in spell.c, but that's not a normal
1586 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001587 */
1588 buf = NULL;
1589 if ((flags & BLN_CURBUF)
1590 && curbuf != NULL
1591 && curbuf->b_ffname == NULL
1592 && curbuf->b_nwindows <= 1
1593 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1594 {
1595 buf = curbuf;
1596#ifdef FEAT_AUTOCMD
1597 /* It's like this buffer is deleted. Watch out for autocommands that
1598 * change curbuf! If that happens, allocate a new buffer anyway. */
1599 if (curbuf->b_p_bl)
1600 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1601 if (buf == curbuf)
1602 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1603# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001604 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 return NULL;
1606# endif
1607#endif
1608#ifdef FEAT_QUICKFIX
1609# ifdef FEAT_AUTOCMD
1610 if (buf == curbuf)
1611# endif
1612 {
1613 /* Make sure 'bufhidden' and 'buftype' are empty */
1614 clear_string_option(&buf->b_p_bh);
1615 clear_string_option(&buf->b_p_bt);
1616 }
1617#endif
1618 }
1619 if (buf != curbuf || curbuf == NULL)
1620 {
1621 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1622 if (buf == NULL)
1623 {
1624 vim_free(ffname);
1625 return NULL;
1626 }
1627 }
1628
1629 if (ffname != NULL)
1630 {
1631 buf->b_ffname = ffname;
1632 buf->b_sfname = vim_strsave(sfname);
1633 }
1634
1635 clear_wininfo(buf);
1636 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1637
1638 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1639 || buf->b_wininfo == NULL)
1640 {
1641 vim_free(buf->b_ffname);
1642 buf->b_ffname = NULL;
1643 vim_free(buf->b_sfname);
1644 buf->b_sfname = NULL;
1645 if (buf != curbuf)
1646 free_buffer(buf);
1647 return NULL;
1648 }
1649
1650 if (buf == curbuf)
1651 {
1652 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001653 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 if (buf != curbuf) /* autocommands deleted the buffer! */
1655 return NULL;
1656#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001657 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 return NULL;
1659#endif
1660 /* buf->b_nwindows = 0; why was this here? */
1661 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1662#ifdef FEAT_KEYMAP
1663 /* need to reload lmaps and set b:keymap_name */
1664 curbuf->b_kmap_state |= KEYMAP_INIT;
1665#endif
1666 }
1667 else
1668 {
1669 /*
1670 * put new buffer at the end of the buffer list
1671 */
1672 buf->b_next = NULL;
1673 if (firstbuf == NULL) /* buffer list is empty */
1674 {
1675 buf->b_prev = NULL;
1676 firstbuf = buf;
1677 }
1678 else /* append new buffer at end of list */
1679 {
1680 lastbuf->b_next = buf;
1681 buf->b_prev = lastbuf;
1682 }
1683 lastbuf = buf;
1684
1685 buf->b_fnum = top_file_num++;
1686 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1687 {
1688 EMSG(_("W14: Warning: List of file names overflow"));
1689 if (emsg_silent == 0)
1690 {
1691 out_flush();
1692 ui_delay(3000L, TRUE); /* make sure it is noticed */
1693 }
1694 top_file_num = 1;
1695 }
1696
1697 /*
1698 * Always copy the options from the current buffer.
1699 */
1700 buf_copy_options(buf, BCO_ALWAYS);
1701 }
1702
1703 buf->b_wininfo->wi_fpos.lnum = lnum;
1704 buf->b_wininfo->wi_win = curwin;
1705
1706#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001707 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1708#endif
1709#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001710 hash_init(&buf->b_s.b_keywtab);
1711 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712#endif
1713
1714 buf->b_fname = buf->b_sfname;
1715#ifdef UNIX
1716 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001717 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 else
1719 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001720 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 buf->b_dev = st.st_dev;
1722 buf->b_ino = st.st_ino;
1723 }
1724#endif
1725 buf->b_u_synced = TRUE;
1726 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001727 if (flags & BLN_DUMMY)
1728 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 buf_clear_file(buf);
1730 clrallmarks(buf); /* clear marks */
1731 fmarks_check_names(buf); /* check file marks for this file */
1732 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1733#ifdef FEAT_AUTOCMD
1734 if (!(flags & BLN_DUMMY))
1735 {
1736 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1737 if (flags & BLN_LISTED)
1738 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1739# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001740 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 return NULL;
1742# endif
1743 }
1744#endif
1745
1746 return buf;
1747}
1748
1749/*
1750 * Free the memory for the options of a buffer.
1751 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1752 * 'fileencoding'.
1753 */
1754 void
1755free_buf_options(buf, free_p_ff)
1756 buf_T *buf;
1757 int free_p_ff;
1758{
1759 if (free_p_ff)
1760 {
1761#ifdef FEAT_MBYTE
1762 clear_string_option(&buf->b_p_fenc);
1763#endif
1764 clear_string_option(&buf->b_p_ff);
1765#ifdef FEAT_QUICKFIX
1766 clear_string_option(&buf->b_p_bh);
1767 clear_string_option(&buf->b_p_bt);
1768#endif
1769 }
1770#ifdef FEAT_FIND_ID
1771 clear_string_option(&buf->b_p_def);
1772 clear_string_option(&buf->b_p_inc);
1773# ifdef FEAT_EVAL
1774 clear_string_option(&buf->b_p_inex);
1775# endif
1776#endif
1777#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1778 clear_string_option(&buf->b_p_inde);
1779 clear_string_option(&buf->b_p_indk);
1780#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001781#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1782 clear_string_option(&buf->b_p_bexpr);
1783#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001784#if defined(FEAT_CRYPT)
1785 clear_string_option(&buf->b_p_cm);
1786#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001787#if defined(FEAT_EVAL)
1788 clear_string_option(&buf->b_p_fex);
1789#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790#ifdef FEAT_CRYPT
1791 clear_string_option(&buf->b_p_key);
1792#endif
1793 clear_string_option(&buf->b_p_kp);
1794 clear_string_option(&buf->b_p_mps);
1795 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001796 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 clear_string_option(&buf->b_p_isk);
1798#ifdef FEAT_KEYMAP
1799 clear_string_option(&buf->b_p_keymap);
1800 ga_clear(&buf->b_kmap_ga);
1801#endif
1802#ifdef FEAT_COMMENTS
1803 clear_string_option(&buf->b_p_com);
1804#endif
1805#ifdef FEAT_FOLDING
1806 clear_string_option(&buf->b_p_cms);
1807#endif
1808 clear_string_option(&buf->b_p_nf);
1809#ifdef FEAT_SYN_HL
1810 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001811#endif
1812#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001813 clear_string_option(&buf->b_s.b_p_spc);
1814 clear_string_option(&buf->b_s.b_p_spf);
1815 vim_free(buf->b_s.b_cap_prog);
1816 buf->b_s.b_cap_prog = NULL;
1817 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818#endif
1819#ifdef FEAT_SEARCHPATH
1820 clear_string_option(&buf->b_p_sua);
1821#endif
1822#ifdef FEAT_AUTOCMD
1823 clear_string_option(&buf->b_p_ft);
1824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825#ifdef FEAT_CINDENT
1826 clear_string_option(&buf->b_p_cink);
1827 clear_string_option(&buf->b_p_cino);
1828#endif
1829#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1830 clear_string_option(&buf->b_p_cinw);
1831#endif
1832#ifdef FEAT_INS_EXPAND
1833 clear_string_option(&buf->b_p_cpt);
1834#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001835#ifdef FEAT_COMPL_FUNC
1836 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001837 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839#ifdef FEAT_QUICKFIX
1840 clear_string_option(&buf->b_p_gp);
1841 clear_string_option(&buf->b_p_mp);
1842 clear_string_option(&buf->b_p_efm);
1843#endif
1844 clear_string_option(&buf->b_p_ep);
1845 clear_string_option(&buf->b_p_path);
1846 clear_string_option(&buf->b_p_tags);
1847#ifdef FEAT_INS_EXPAND
1848 clear_string_option(&buf->b_p_dict);
1849 clear_string_option(&buf->b_p_tsr);
1850#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001851#ifdef FEAT_TEXTOBJ
1852 clear_string_option(&buf->b_p_qe);
1853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 buf->b_p_ar = -1;
1855}
1856
1857/*
1858 * get alternate file n
1859 * set linenr to lnum or altfpos.lnum if lnum == 0
1860 * also set cursor column to altfpos.col if 'startofline' is not set.
1861 * if (options & GETF_SETMARK) call setpcmark()
1862 * if (options & GETF_ALT) we are jumping to an alternate file.
1863 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1864 *
1865 * return FAIL for failure, OK for success
1866 */
1867 int
1868buflist_getfile(n, lnum, options, forceit)
1869 int n;
1870 linenr_T lnum;
1871 int options;
1872 int forceit;
1873{
1874 buf_T *buf;
1875#ifdef FEAT_WINDOWS
1876 win_T *wp = NULL;
1877#endif
1878 pos_T *fpos;
1879 colnr_T col;
1880
1881 buf = buflist_findnr(n);
1882 if (buf == NULL)
1883 {
1884 if ((options & GETF_ALT) && n == 0)
1885 EMSG(_(e_noalt));
1886 else
1887 EMSGN(_("E92: Buffer %ld not found"), n);
1888 return FAIL;
1889 }
1890
1891 /* if alternate file is the current buffer, nothing to do */
1892 if (buf == curbuf)
1893 return OK;
1894
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001895 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001896 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001897 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001899 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001900#ifdef FEAT_AUTOCMD
1901 if (curbuf_locked())
1902 return FAIL;
1903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 /* altfpos may be changed by getfile(), get it now */
1906 if (lnum == 0)
1907 {
1908 fpos = buflist_findfpos(buf);
1909 lnum = fpos->lnum;
1910 col = fpos->col;
1911 }
1912 else
1913 col = 0;
1914
1915#ifdef FEAT_WINDOWS
1916 if (options & GETF_SWITCH)
1917 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001918 /* If 'switchbuf' contains "useopen": jump to first window containing
1919 * "buf" if one exists */
1920 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001922 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1923 * page containing "buf" if one exists */
1924 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001925 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001926 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1927 * isn't empty: open new window */
1928 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001930 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1931 tabpage_new();
1932 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001934 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 }
1936 }
1937#endif
1938
1939 ++RedrawingDisabled;
1940 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1941 lnum, forceit) <= 0)
1942 {
1943 --RedrawingDisabled;
1944
1945 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1946 if (!p_sol && col != 0)
1947 {
1948 curwin->w_cursor.col = col;
1949 check_cursor_col();
1950#ifdef FEAT_VIRTUALEDIT
1951 curwin->w_cursor.coladd = 0;
1952#endif
1953 curwin->w_set_curswant = TRUE;
1954 }
1955 return OK;
1956 }
1957 --RedrawingDisabled;
1958 return FAIL;
1959}
1960
1961/*
1962 * go to the last know line number for the current buffer
1963 */
1964 void
1965buflist_getfpos()
1966{
1967 pos_T *fpos;
1968
1969 fpos = buflist_findfpos(curbuf);
1970
1971 curwin->w_cursor.lnum = fpos->lnum;
1972 check_cursor_lnum();
1973
1974 if (p_sol)
1975 curwin->w_cursor.col = 0;
1976 else
1977 {
1978 curwin->w_cursor.col = fpos->col;
1979 check_cursor_col();
1980#ifdef FEAT_VIRTUALEDIT
1981 curwin->w_cursor.coladd = 0;
1982#endif
1983 curwin->w_set_curswant = TRUE;
1984 }
1985}
1986
Bram Moolenaar81695252004-12-29 20:58:21 +00001987#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1988/*
1989 * Find file in buffer list by name (it has to be for the current window).
1990 * Returns NULL if not found.
1991 */
1992 buf_T *
1993buflist_findname_exp(fname)
1994 char_u *fname;
1995{
1996 char_u *ffname;
1997 buf_T *buf = NULL;
1998
1999 /* First make the name into a full path name */
2000 ffname = FullName_save(fname,
2001#ifdef UNIX
2002 TRUE /* force expansion, get rid of symbolic links */
2003#else
2004 FALSE
2005#endif
2006 );
2007 if (ffname != NULL)
2008 {
2009 buf = buflist_findname(ffname);
2010 vim_free(ffname);
2011 }
2012 return buf;
2013}
2014#endif
2015
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016/*
2017 * Find file in buffer list by name (it has to be for the current window).
2018 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002019 * Skips dummy buffers.
2020 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 */
2022 buf_T *
2023buflist_findname(ffname)
2024 char_u *ffname;
2025{
2026#ifdef UNIX
2027 struct stat st;
2028
2029 if (mch_stat((char *)ffname, &st) < 0)
2030 st.st_dev = (dev_T)-1;
2031 return buflist_findname_stat(ffname, &st);
2032}
2033
2034/*
2035 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2036 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002037 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 */
2039 static buf_T *
2040buflist_findname_stat(ffname, stp)
2041 char_u *ffname;
2042 struct stat *stp;
2043{
2044#endif
2045 buf_T *buf;
2046
2047 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002048 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049#ifdef UNIX
2050 , stp
2051#endif
2052 ))
2053 return buf;
2054 return NULL;
2055}
2056
2057#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2058/*
2059 * Find file in buffer list by a regexp pattern.
2060 * Return fnum of the found buffer.
2061 * Return < 0 for error.
2062 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 int
2064buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2065 char_u *pattern;
2066 char_u *pattern_end; /* pointer to first char after pattern */
2067 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002068 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069{
2070 buf_T *buf;
2071 regprog_T *prog;
2072 int match = -1;
2073 int find_listed;
2074 char_u *pat;
2075 char_u *patend;
2076 int attempt;
2077 char_u *p;
2078 int toggledollar;
2079
2080 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2081 {
2082 if (*pattern == '%')
2083 match = curbuf->b_fnum;
2084 else
2085 match = curwin->w_alt_fnum;
2086#ifdef FEAT_DIFF
2087 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2088 match = -1;
2089#endif
2090 }
2091
2092 /*
2093 * Try four ways of matching a listed buffer:
2094 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002095 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 * attempt == 2: with '$' at end (only match at end)
2097 * attempt == 3: with '^' at start and '$' at end (only full match)
2098 * Repeat this for finding an unlisted buffer if there was no matching
2099 * listed buffer.
2100 */
2101 else
2102 {
2103 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2104 if (pat == NULL)
2105 return -1;
2106 patend = pat + STRLEN(pat) - 1;
2107 toggledollar = (patend > pat && *patend == '$');
2108
2109 /* First try finding a listed buffer. If not found and "unlisted"
2110 * is TRUE, try finding an unlisted buffer. */
2111 find_listed = TRUE;
2112 for (;;)
2113 {
2114 for (attempt = 0; attempt <= 3; ++attempt)
2115 {
2116 /* may add '^' and '$' */
2117 if (toggledollar)
2118 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2119 p = pat;
2120 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2121 ++p;
2122 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2123 if (prog == NULL)
2124 {
2125 vim_free(pat);
2126 return -1;
2127 }
2128
2129 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2130 if (buf->b_p_bl == find_listed
2131#ifdef FEAT_DIFF
2132 && (!diffmode || diff_mode_buf(buf))
2133#endif
2134 && buflist_match(prog, buf) != NULL)
2135 {
2136 if (match >= 0) /* already found a match */
2137 {
2138 match = -2;
2139 break;
2140 }
2141 match = buf->b_fnum; /* remember first match */
2142 }
2143
2144 vim_free(prog);
2145 if (match >= 0) /* found one match */
2146 break;
2147 }
2148
2149 /* Only search for unlisted buffers if there was no match with
2150 * a listed buffer. */
2151 if (!unlisted || !find_listed || match != -1)
2152 break;
2153 find_listed = FALSE;
2154 }
2155
2156 vim_free(pat);
2157 }
2158
2159 if (match == -2)
2160 EMSG2(_("E93: More than one match for %s"), pattern);
2161 else if (match < 0)
2162 EMSG2(_("E94: No matching buffer for %s"), pattern);
2163 return match;
2164}
2165#endif
2166
2167#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2168
2169/*
2170 * Find all buffer names that match.
2171 * For command line expansion of ":buf" and ":sbuf".
2172 * Return OK if matches found, FAIL otherwise.
2173 */
2174 int
2175ExpandBufnames(pat, num_file, file, options)
2176 char_u *pat;
2177 int *num_file;
2178 char_u ***file;
2179 int options;
2180{
2181 int count = 0;
2182 buf_T *buf;
2183 int round;
2184 char_u *p;
2185 int attempt;
2186 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002187 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188
2189 *num_file = 0; /* return values in case of FAIL */
2190 *file = NULL;
2191
Bram Moolenaar05159a02005-02-26 23:04:13 +00002192 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2193 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002195 patc = alloc((unsigned)STRLEN(pat) + 11);
2196 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002198 STRCPY(patc, "\\(^\\|[\\/]\\)");
2199 STRCPY(patc + 11, pat + 1);
2200 }
2201 else
2202 patc = pat;
2203
2204 /*
2205 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002206 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002207 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002208 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002209 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002210 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002211 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002212 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002213 if (prog == NULL)
2214 {
2215 if (patc != pat)
2216 vim_free(patc);
2217 return FAIL;
2218 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219
2220 /*
2221 * round == 1: Count the matches.
2222 * round == 2: Build the array to keep the matches.
2223 */
2224 for (round = 1; round <= 2; ++round)
2225 {
2226 count = 0;
2227 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2228 {
2229 if (!buf->b_p_bl) /* skip unlisted buffers */
2230 continue;
2231 p = buflist_match(prog, buf);
2232 if (p != NULL)
2233 {
2234 if (round == 1)
2235 ++count;
2236 else
2237 {
2238 if (options & WILD_HOME_REPLACE)
2239 p = home_replace_save(buf, p);
2240 else
2241 p = vim_strsave(p);
2242 (*file)[count++] = p;
2243 }
2244 }
2245 }
2246 if (count == 0) /* no match found, break here */
2247 break;
2248 if (round == 1)
2249 {
2250 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2251 if (*file == NULL)
2252 {
2253 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002254 if (patc != pat)
2255 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 return FAIL;
2257 }
2258 }
2259 }
2260 vim_free(prog);
2261 if (count) /* match(es) found, break here */
2262 break;
2263 }
2264
Bram Moolenaar05159a02005-02-26 23:04:13 +00002265 if (patc != pat)
2266 vim_free(patc);
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 *num_file = count;
2269 return (count == 0 ? FAIL : OK);
2270}
2271
2272#endif /* FEAT_CMDL_COMPL */
2273
2274#ifdef HAVE_BUFLIST_MATCH
2275/*
2276 * Check for a match on the file name for buffer "buf" with regprog "prog".
2277 */
2278 static char_u *
2279buflist_match(prog, buf)
2280 regprog_T *prog;
2281 buf_T *buf;
2282{
2283 char_u *match;
2284
2285 /* First try the short file name, then the long file name. */
2286 match = fname_match(prog, buf->b_sfname);
2287 if (match == NULL)
2288 match = fname_match(prog, buf->b_ffname);
2289
2290 return match;
2291}
2292
2293/*
2294 * Try matching the regexp in "prog" with file name "name".
2295 * Return "name" when there is a match, NULL when not.
2296 */
2297 static char_u *
2298fname_match(prog, name)
2299 regprog_T *prog;
2300 char_u *name;
2301{
2302 char_u *match = NULL;
2303 char_u *p;
2304 regmatch_T regmatch;
2305
2306 if (name != NULL)
2307 {
2308 regmatch.regprog = prog;
2309#ifdef CASE_INSENSITIVE_FILENAME
2310 regmatch.rm_ic = TRUE; /* Always ignore case */
2311#else
2312 regmatch.rm_ic = FALSE; /* Never ignore case */
2313#endif
2314
2315 if (vim_regexec(&regmatch, name, (colnr_T)0))
2316 match = name;
2317 else
2318 {
2319 /* Replace $(HOME) with '~' and try matching again. */
2320 p = home_replace_save(NULL, name);
2321 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2322 match = name;
2323 vim_free(p);
2324 }
2325 }
2326
2327 return match;
2328}
2329#endif
2330
2331/*
2332 * find file in buffer list by number
2333 */
2334 buf_T *
2335buflist_findnr(nr)
2336 int nr;
2337{
2338 buf_T *buf;
2339
2340 if (nr == 0)
2341 nr = curwin->w_alt_fnum;
2342 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2343 if (buf->b_fnum == nr)
2344 return (buf);
2345 return NULL;
2346}
2347
2348/*
2349 * Get name of file 'n' in the buffer list.
2350 * When the file has no name an empty string is returned.
2351 * home_replace() is used to shorten the file name (used for marks).
2352 * Returns a pointer to allocated memory, of NULL when failed.
2353 */
2354 char_u *
2355buflist_nr2name(n, fullname, helptail)
2356 int n;
2357 int fullname;
2358 int helptail; /* for help buffers return tail only */
2359{
2360 buf_T *buf;
2361
2362 buf = buflist_findnr(n);
2363 if (buf == NULL)
2364 return NULL;
2365 return home_replace_save(helptail ? buf : NULL,
2366 fullname ? buf->b_ffname : buf->b_fname);
2367}
2368
2369/*
2370 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2371 * When "copy_options" is TRUE save the local window option values.
2372 * When "lnum" is 0 only do the options.
2373 */
2374 static void
2375buflist_setfpos(buf, win, lnum, col, copy_options)
2376 buf_T *buf;
2377 win_T *win;
2378 linenr_T lnum;
2379 colnr_T col;
2380 int copy_options;
2381{
2382 wininfo_T *wip;
2383
2384 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2385 if (wip->wi_win == win)
2386 break;
2387 if (wip == NULL)
2388 {
2389 /* allocate a new entry */
2390 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2391 if (wip == NULL)
2392 return;
2393 wip->wi_win = win;
2394 if (lnum == 0) /* set lnum even when it's 0 */
2395 lnum = 1;
2396 }
2397 else
2398 {
2399 /* remove the entry from the list */
2400 if (wip->wi_prev)
2401 wip->wi_prev->wi_next = wip->wi_next;
2402 else
2403 buf->b_wininfo = wip->wi_next;
2404 if (wip->wi_next)
2405 wip->wi_next->wi_prev = wip->wi_prev;
2406 if (copy_options && wip->wi_optset)
2407 {
2408 clear_winopt(&wip->wi_opt);
2409#ifdef FEAT_FOLDING
2410 deleteFoldRecurse(&wip->wi_folds);
2411#endif
2412 }
2413 }
2414 if (lnum != 0)
2415 {
2416 wip->wi_fpos.lnum = lnum;
2417 wip->wi_fpos.col = col;
2418 }
2419 if (copy_options)
2420 {
2421 /* Save the window-specific option values. */
2422 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2423#ifdef FEAT_FOLDING
2424 wip->wi_fold_manual = win->w_fold_manual;
2425 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2426#endif
2427 wip->wi_optset = TRUE;
2428 }
2429
2430 /* insert the entry in front of the list */
2431 wip->wi_next = buf->b_wininfo;
2432 buf->b_wininfo = wip;
2433 wip->wi_prev = NULL;
2434 if (wip->wi_next)
2435 wip->wi_next->wi_prev = wip;
2436
2437 return;
2438}
2439
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002440#ifdef FEAT_DIFF
2441static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2442
2443/*
2444 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2445 * page. That's because a diff is local to a tab page.
2446 */
2447 static int
2448wininfo_other_tab_diff(wip)
2449 wininfo_T *wip;
2450{
2451 win_T *wp;
2452
2453 if (wip->wi_opt.wo_diff)
2454 {
2455 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2456 /* return FALSE when it's a window in the current tab page, thus
2457 * the buffer was in diff mode here */
2458 if (wip->wi_win == wp)
2459 return FALSE;
2460 return TRUE;
2461 }
2462 return FALSE;
2463}
2464#endif
2465
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466/*
2467 * Find info for the current window in buffer "buf".
2468 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002469 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2470 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 * Returns NULL when there isn't any info.
2472 */
2473 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002474find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002476 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477{
2478 wininfo_T *wip;
2479
2480 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002481 if (wip->wi_win == curwin
2482#ifdef FEAT_DIFF
2483 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2484#endif
2485 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002487
2488 /* If no wininfo for curwin, use the first in the list (that doesn't have
2489 * 'diff' set and is in another tab page). */
2490 if (wip == NULL)
2491 {
2492#ifdef FEAT_DIFF
2493 if (skip_diff_buffer)
2494 {
2495 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2496 if (!wininfo_other_tab_diff(wip))
2497 break;
2498 }
2499 else
2500#endif
2501 wip = buf->b_wininfo;
2502 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 return wip;
2504}
2505
2506/*
2507 * Reset the local window options to the values last used in this window.
2508 * If the buffer wasn't used in this window before, use the values from
2509 * the most recently used window. If the values were never set, use the
2510 * global values for the window.
2511 */
2512 void
2513get_winopts(buf)
2514 buf_T *buf;
2515{
2516 wininfo_T *wip;
2517
2518 clear_winopt(&curwin->w_onebuf_opt);
2519#ifdef FEAT_FOLDING
2520 clearFolding(curwin);
2521#endif
2522
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002523 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 if (wip != NULL && wip->wi_optset)
2525 {
2526 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2527#ifdef FEAT_FOLDING
2528 curwin->w_fold_manual = wip->wi_fold_manual;
2529 curwin->w_foldinvalid = TRUE;
2530 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2531#endif
2532 }
2533 else
2534 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2535
2536#ifdef FEAT_FOLDING
2537 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2538 if (p_fdls >= 0)
2539 curwin->w_p_fdl = p_fdls;
2540#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002541#ifdef FEAT_SYN_HL
2542 check_colorcolumn(curwin);
2543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544}
2545
2546/*
2547 * Find the position (lnum and col) for the buffer 'buf' for the current
2548 * window.
2549 * Returns a pointer to no_position if no position is found.
2550 */
2551 pos_T *
2552buflist_findfpos(buf)
2553 buf_T *buf;
2554{
2555 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002556 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002558 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 if (wip != NULL)
2560 return &(wip->wi_fpos);
2561 else
2562 return &no_position;
2563}
2564
2565/*
2566 * Find the lnum for the buffer 'buf' for the current window.
2567 */
2568 linenr_T
2569buflist_findlnum(buf)
2570 buf_T *buf;
2571{
2572 return buflist_findfpos(buf)->lnum;
2573}
2574
2575#if defined(FEAT_LISTCMDS) || defined(PROTO)
2576/*
2577 * List all know file names (for :files and :buffers command).
2578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 void
2580buflist_list(eap)
2581 exarg_T *eap;
2582{
2583 buf_T *buf;
2584 int len;
2585 int i;
2586
2587 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2588 {
2589 /* skip unlisted buffers, unless ! was used */
2590 if (!buf->b_p_bl && !eap->forceit)
2591 continue;
2592 msg_putchar('\n');
2593 if (buf_spname(buf) != NULL)
2594 STRCPY(NameBuff, buf_spname(buf));
2595 else
2596 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2597
Bram Moolenaar50cde822005-06-05 21:54:54 +00002598 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 buf->b_fnum,
2600 buf->b_p_bl ? ' ' : 'u',
2601 buf == curbuf ? '%' :
2602 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2603 buf->b_ml.ml_mfp == NULL ? ' ' :
2604 (buf->b_nwindows == 0 ? 'h' : 'a'),
2605 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2606 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002607 : (bufIsChanged(buf) ? '+' : ' '),
2608 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
2610 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 i = 40 - vim_strsize(IObuff);
2612 do
2613 {
2614 IObuff[len++] = ' ';
2615 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002616 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2617 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002618 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002619 msg_outtrans(IObuff);
2620 out_flush(); /* output one line at a time */
2621 ui_breakcheck();
2622 }
2623}
2624#endif
2625
2626/*
2627 * Get file name and line number for file 'fnum'.
2628 * Used by DoOneCmd() for translating '%' and '#'.
2629 * Used by insert_reg() and cmdline_paste() for '#' register.
2630 * Return FAIL if not found, OK for success.
2631 */
2632 int
2633buflist_name_nr(fnum, fname, lnum)
2634 int fnum;
2635 char_u **fname;
2636 linenr_T *lnum;
2637{
2638 buf_T *buf;
2639
2640 buf = buflist_findnr(fnum);
2641 if (buf == NULL || buf->b_fname == NULL)
2642 return FAIL;
2643
2644 *fname = buf->b_fname;
2645 *lnum = buflist_findlnum(buf);
2646
2647 return OK;
2648}
2649
2650/*
2651 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2652 * The file name with the full path is also remembered, for when :cd is used.
2653 * Returns FAIL for failure (file name already in use by other buffer)
2654 * OK otherwise.
2655 */
2656 int
2657setfname(buf, ffname, sfname, message)
2658 buf_T *buf;
2659 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002660 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661{
Bram Moolenaar81695252004-12-29 20:58:21 +00002662 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663#ifdef UNIX
2664 struct stat st;
2665#endif
2666
2667 if (ffname == NULL || *ffname == NUL)
2668 {
2669 /* Removing the name. */
2670 vim_free(buf->b_ffname);
2671 vim_free(buf->b_sfname);
2672 buf->b_ffname = NULL;
2673 buf->b_sfname = NULL;
2674#ifdef UNIX
2675 st.st_dev = (dev_T)-1;
2676#endif
2677 }
2678 else
2679 {
2680 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2681 if (ffname == NULL) /* out of memory */
2682 return FAIL;
2683
2684 /*
2685 * if the file name is already used in another buffer:
2686 * - if the buffer is loaded, fail
2687 * - if the buffer is not loaded, delete it from the list
2688 */
2689#ifdef UNIX
2690 if (mch_stat((char *)ffname, &st) < 0)
2691 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002692#endif
2693 if (!(buf->b_flags & BF_DUMMY))
2694#ifdef UNIX
2695 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002697 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698#endif
2699 if (obuf != NULL && obuf != buf)
2700 {
2701 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2702 {
2703 if (message)
2704 EMSG(_("E95: Buffer with this name already exists"));
2705 vim_free(ffname);
2706 return FAIL;
2707 }
2708 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2709 }
2710 sfname = vim_strsave(sfname);
2711 if (ffname == NULL || sfname == NULL)
2712 {
2713 vim_free(sfname);
2714 vim_free(ffname);
2715 return FAIL;
2716 }
2717#ifdef USE_FNAME_CASE
2718# ifdef USE_LONG_FNAME
2719 if (USE_LONG_FNAME)
2720# endif
2721 fname_case(sfname, 0); /* set correct case for short file name */
2722#endif
2723 vim_free(buf->b_ffname);
2724 vim_free(buf->b_sfname);
2725 buf->b_ffname = ffname;
2726 buf->b_sfname = sfname;
2727 }
2728 buf->b_fname = buf->b_sfname;
2729#ifdef UNIX
2730 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002731 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 else
2733 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002734 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 buf->b_dev = st.st_dev;
2736 buf->b_ino = st.st_ino;
2737 }
2738#endif
2739
2740#ifndef SHORT_FNAME
2741 buf->b_shortname = FALSE;
2742#endif
2743
2744 buf_name_changed(buf);
2745 return OK;
2746}
2747
2748/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002749 * Crude way of changing the name of a buffer. Use with care!
2750 * The name should be relative to the current directory.
2751 */
2752 void
2753buf_set_name(fnum, name)
2754 int fnum;
2755 char_u *name;
2756{
2757 buf_T *buf;
2758
2759 buf = buflist_findnr(fnum);
2760 if (buf != NULL)
2761 {
2762 vim_free(buf->b_sfname);
2763 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002764 buf->b_ffname = vim_strsave(name);
2765 buf->b_sfname = NULL;
2766 /* Allocate ffname and expand into full path. Also resolves .lnk
2767 * files on Win32. */
2768 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002769 buf->b_fname = buf->b_sfname;
2770 }
2771}
2772
2773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 * Take care of what needs to be done when the name of buffer "buf" has
2775 * changed.
2776 */
2777 void
2778buf_name_changed(buf)
2779 buf_T *buf;
2780{
2781 /*
2782 * If the file name changed, also change the name of the swapfile
2783 */
2784 if (buf->b_ml.ml_mfp != NULL)
2785 ml_setname(buf);
2786
2787 if (curwin->w_buffer == buf)
2788 check_arg_idx(curwin); /* check file name for arg list */
2789#ifdef FEAT_TITLE
2790 maketitle(); /* set window title */
2791#endif
2792#ifdef FEAT_WINDOWS
2793 status_redraw_all(); /* status lines need to be redrawn */
2794#endif
2795 fmarks_check_names(buf); /* check named file marks */
2796 ml_timestamp(buf); /* reset timestamp */
2797}
2798
2799/*
2800 * set alternate file name for current window
2801 *
2802 * Used by do_one_cmd(), do_write() and do_ecmd().
2803 * Return the buffer.
2804 */
2805 buf_T *
2806setaltfname(ffname, sfname, lnum)
2807 char_u *ffname;
2808 char_u *sfname;
2809 linenr_T lnum;
2810{
2811 buf_T *buf;
2812
2813 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2814 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002815 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002816 curwin->w_alt_fnum = buf->b_fnum;
2817 return buf;
2818}
2819
2820/*
2821 * Get alternate file name for current window.
2822 * Return NULL if there isn't any, and give error message if requested.
2823 */
2824 char_u *
2825getaltfname(errmsg)
2826 int errmsg; /* give error message */
2827{
2828 char_u *fname;
2829 linenr_T dummy;
2830
2831 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2832 {
2833 if (errmsg)
2834 EMSG(_(e_noalt));
2835 return NULL;
2836 }
2837 return fname;
2838}
2839
2840/*
2841 * Add a file name to the buflist and return its number.
2842 * Uses same flags as buflist_new(), except BLN_DUMMY.
2843 *
2844 * used by qf_init(), main() and doarglist()
2845 */
2846 int
2847buflist_add(fname, flags)
2848 char_u *fname;
2849 int flags;
2850{
2851 buf_T *buf;
2852
2853 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2854 if (buf != NULL)
2855 return buf->b_fnum;
2856 return 0;
2857}
2858
2859#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2860/*
2861 * Adjust slashes in file names. Called after 'shellslash' was set.
2862 */
2863 void
2864buflist_slash_adjust()
2865{
2866 buf_T *bp;
2867
2868 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2869 {
2870 if (bp->b_ffname != NULL)
2871 slash_adjust(bp->b_ffname);
2872 if (bp->b_sfname != NULL)
2873 slash_adjust(bp->b_sfname);
2874 }
2875}
2876#endif
2877
2878/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002879 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002880 * Also save the local window option values.
2881 */
2882 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002883buflist_altfpos(win)
2884 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002886 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887}
2888
2889/*
2890 * Return TRUE if 'ffname' is not the same file as current file.
2891 * Fname must have a full path (expanded by mch_FullName()).
2892 */
2893 int
2894otherfile(ffname)
2895 char_u *ffname;
2896{
2897 return otherfile_buf(curbuf, ffname
2898#ifdef UNIX
2899 , NULL
2900#endif
2901 );
2902}
2903
2904 static int
2905otherfile_buf(buf, ffname
2906#ifdef UNIX
2907 , stp
2908#endif
2909 )
2910 buf_T *buf;
2911 char_u *ffname;
2912#ifdef UNIX
2913 struct stat *stp;
2914#endif
2915{
2916 /* no name is different */
2917 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2918 return TRUE;
2919 if (fnamecmp(ffname, buf->b_ffname) == 0)
2920 return FALSE;
2921#ifdef UNIX
2922 {
2923 struct stat st;
2924
2925 /* If no struct stat given, get it now */
2926 if (stp == NULL)
2927 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002928 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002929 st.st_dev = (dev_T)-1;
2930 stp = &st;
2931 }
2932 /* Use dev/ino to check if the files are the same, even when the names
2933 * are different (possible with links). Still need to compare the
2934 * name above, for when the file doesn't exist yet.
2935 * Problem: The dev/ino changes when a file is deleted (and created
2936 * again) and remains the same when renamed/moved. We don't want to
2937 * mch_stat() each buffer each time, that would be too slow. Get the
2938 * dev/ino again when they appear to match, but not when they appear
2939 * to be different: Could skip a buffer when it's actually the same
2940 * file. */
2941 if (buf_same_ino(buf, stp))
2942 {
2943 buf_setino(buf);
2944 if (buf_same_ino(buf, stp))
2945 return FALSE;
2946 }
2947 }
2948#endif
2949 return TRUE;
2950}
2951
2952#if defined(UNIX) || defined(PROTO)
2953/*
2954 * Set inode and device number for a buffer.
2955 * Must always be called when b_fname is changed!.
2956 */
2957 void
2958buf_setino(buf)
2959 buf_T *buf;
2960{
2961 struct stat st;
2962
2963 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2964 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002965 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002966 buf->b_dev = st.st_dev;
2967 buf->b_ino = st.st_ino;
2968 }
2969 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002970 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971}
2972
2973/*
2974 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2975 */
2976 static int
2977buf_same_ino(buf, stp)
2978 buf_T *buf;
2979 struct stat *stp;
2980{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002981 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 && stp->st_dev == buf->b_dev
2983 && stp->st_ino == buf->b_ino);
2984}
2985#endif
2986
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002987/*
2988 * Print info about the current buffer.
2989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 void
2991fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002992 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 int shorthelp;
2994 int dont_truncate;
2995{
2996 char_u *name;
2997 int n;
2998 char_u *p;
2999 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003000 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003001
3002 buffer = alloc(IOSIZE);
3003 if (buffer == NULL)
3004 return;
3005
3006 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3007 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003008 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 p = buffer + STRLEN(buffer);
3010 }
3011 else
3012 p = buffer;
3013
3014 *p++ = '"';
3015 if (buf_spname(curbuf) != NULL)
3016 STRCPY(p, buf_spname(curbuf));
3017 else
3018 {
3019 if (!fullname && curbuf->b_fname != NULL)
3020 name = curbuf->b_fname;
3021 else
3022 name = curbuf->b_ffname;
3023 home_replace(shorthelp ? curbuf : NULL, name, p,
3024 (int)(IOSIZE - (p - buffer)), TRUE);
3025 }
3026
Bram Moolenaara800b422010-06-27 01:15:55 +02003027 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 curbufIsChanged() ? (shortmess(SHM_MOD)
3029 ? " [+]" : _(" [Modified]")) : " ",
3030 (curbuf->b_flags & BF_NOTEDITED)
3031#ifdef FEAT_QUICKFIX
3032 && !bt_dontwrite(curbuf)
3033#endif
3034 ? _("[Not edited]") : "",
3035 (curbuf->b_flags & BF_NEW)
3036#ifdef FEAT_QUICKFIX
3037 && !bt_dontwrite(curbuf)
3038#endif
3039 ? _("[New file]") : "",
3040 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3041 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3042 : _("[readonly]")) : "",
3043 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3044 || curbuf->b_p_ro) ?
3045 " " : "");
3046 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3047 * causes an overflow, thus for large numbers divide instead. */
3048 if (curwin->w_cursor.lnum > 1000000L)
3049 n = (int)(((long)curwin->w_cursor.lnum) /
3050 ((long)curbuf->b_ml.ml_line_count / 100L));
3051 else
3052 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3053 (long)curbuf->b_ml.ml_line_count);
3054 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3055 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003056 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 }
3058#ifdef FEAT_CMDL_INFO
3059 else if (p_ru)
3060 {
3061 /* Current line and column are already on the screen -- webb */
3062 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003063 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003065 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066 (long)curbuf->b_ml.ml_line_count, n);
3067 }
3068#endif
3069 else
3070 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003071 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 _("line %ld of %ld --%d%%-- col "),
3073 (long)curwin->w_cursor.lnum,
3074 (long)curbuf->b_ml.ml_line_count,
3075 n);
3076 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003077 len = STRLEN(buffer);
3078 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3080 }
3081
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003082 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
3084 if (dont_truncate)
3085 {
3086 /* Temporarily set msg_scroll to avoid the message being truncated.
3087 * First call msg_start() to get the message in the right place. */
3088 msg_start();
3089 n = msg_scroll;
3090 msg_scroll = TRUE;
3091 msg(buffer);
3092 msg_scroll = n;
3093 }
3094 else
3095 {
3096 p = msg_trunc_attr(buffer, FALSE, 0);
3097 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 /* Need to repeat the message after redrawing when:
3099 * - When restart_edit is set (otherwise there will be a delay
3100 * before redrawing).
3101 * - When the screen was scrolled but there is no wait-return
3102 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003103 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 }
3105
3106 vim_free(buffer);
3107}
3108
3109 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003110col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003112 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 int col;
3114 int vcol;
3115{
3116 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003117 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003119 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120}
3121
3122#if defined(FEAT_TITLE) || defined(PROTO)
3123/*
3124 * put file name in title bar of window and in icon title
3125 */
3126
3127static char_u *lasttitle = NULL;
3128static char_u *lasticon = NULL;
3129
3130 void
3131maketitle()
3132{
3133 char_u *p;
3134 char_u *t_str = NULL;
3135 char_u *i_name;
3136 char_u *i_str = NULL;
3137 int maxlen = 0;
3138 int len;
3139 int mustset;
3140 char_u buf[IOSIZE];
3141 int off;
3142
3143 if (!redrawing())
3144 {
3145 /* Postpone updating the title when 'lazyredraw' is set. */
3146 need_maketitle = TRUE;
3147 return;
3148 }
3149
3150 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003151 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003152 return;
3153
3154 if (p_title)
3155 {
3156 if (p_titlelen > 0)
3157 {
3158 maxlen = p_titlelen * Columns / 100;
3159 if (maxlen < 10)
3160 maxlen = 10;
3161 }
3162
3163 t_str = buf;
3164 if (*p_titlestring != NUL)
3165 {
3166#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003167 if (stl_syntax & STL_IN_TITLE)
3168 {
3169 int use_sandbox = FALSE;
3170 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003171
3172# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003173 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003174# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003175 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003177 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003178 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003179 if (called_emsg)
3180 set_string_option_direct((char_u *)"titlestring", -1,
3181 (char_u *)"", OPT_FREE, SID_ERROR);
3182 called_emsg |= save_called_emsg;
3183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 else
3185#endif
3186 t_str = p_titlestring;
3187 }
3188 else
3189 {
3190 /* format: "fname + (path) (1 of 2) - VIM" */
3191
3192 if (curbuf->b_fname == NULL)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003193 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 else
3195 {
3196 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003197 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 }
3200
3201 switch (bufIsChanged(curbuf)
3202 + (curbuf->b_p_ro * 2)
3203 + (!curbuf->b_p_ma * 4))
3204 {
3205 case 1: STRCAT(buf, " +"); break;
3206 case 2: STRCAT(buf, " ="); break;
3207 case 3: STRCAT(buf, " =+"); break;
3208 case 4:
3209 case 6: STRCAT(buf, " -"); break;
3210 case 5:
3211 case 7: STRCAT(buf, " -+"); break;
3212 }
3213
3214 if (curbuf->b_fname != NULL)
3215 {
3216 /* Get path of file, replace home dir with ~ */
3217 off = (int)STRLEN(buf);
3218 buf[off++] = ' ';
3219 buf[off++] = '(';
3220 home_replace(curbuf, curbuf->b_ffname,
3221 buf + off, IOSIZE - off, TRUE);
3222#ifdef BACKSLASH_IN_FILENAME
3223 /* avoid "c:/name" to be reduced to "c" */
3224 if (isalpha(buf[off]) && buf[off + 1] == ':')
3225 off += 2;
3226#endif
3227 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003228 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003231 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003232 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003235
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 /* translate unprintable chars */
3237 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003238 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 STRCAT(buf, ")");
3241 }
3242
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003243 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244
3245#if defined(FEAT_CLIENTSERVER)
3246 if (serverName != NULL)
3247 {
3248 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003249 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 }
3251 else
3252#endif
3253 STRCAT(buf, " - VIM");
3254
3255 if (maxlen > 0)
3256 {
3257 /* make it shorter by removing a bit in the middle */
3258 len = vim_strsize(buf);
3259 if (len > maxlen)
3260 trunc_string(buf, buf, maxlen);
3261 }
3262 }
3263 }
3264 mustset = ti_change(t_str, &lasttitle);
3265
3266 if (p_icon)
3267 {
3268 i_str = buf;
3269 if (*p_iconstring != NUL)
3270 {
3271#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003272 if (stl_syntax & STL_IN_ICON)
3273 {
3274 int use_sandbox = FALSE;
3275 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003276
3277# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003278 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003279# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003280 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003281 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003282 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003283 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003284 if (called_emsg)
3285 set_string_option_direct((char_u *)"iconstring", -1,
3286 (char_u *)"", OPT_FREE, SID_ERROR);
3287 called_emsg |= save_called_emsg;
3288 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289 else
3290#endif
3291 i_str = p_iconstring;
3292 }
3293 else
3294 {
3295 if (buf_spname(curbuf) != NULL)
3296 i_name = (char_u *)buf_spname(curbuf);
3297 else /* use file name only in icon */
3298 i_name = gettail(curbuf->b_ffname);
3299 *i_str = NUL;
3300 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003301 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 if (len > 100)
3303 {
3304 len -= 100;
3305#ifdef FEAT_MBYTE
3306 if (has_mbyte)
3307 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3308#endif
3309 i_name += len;
3310 }
3311 STRCPY(i_str, i_name);
3312 trans_characters(i_str, IOSIZE);
3313 }
3314 }
3315
3316 mustset |= ti_change(i_str, &lasticon);
3317
3318 if (mustset)
3319 resettitle();
3320}
3321
3322/*
3323 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3324 * from "str" if it does.
3325 * Return TRUE when "*last" changed.
3326 */
3327 static int
3328ti_change(str, last)
3329 char_u *str;
3330 char_u **last;
3331{
3332 if ((str == NULL) != (*last == NULL)
3333 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3334 {
3335 vim_free(*last);
3336 if (str == NULL)
3337 *last = NULL;
3338 else
3339 *last = vim_strsave(str);
3340 return TRUE;
3341 }
3342 return FALSE;
3343}
3344
3345/*
3346 * Put current window title back (used after calling a shell)
3347 */
3348 void
3349resettitle()
3350{
3351 mch_settitle(lasttitle, lasticon);
3352}
Bram Moolenaarea408852005-06-25 22:49:46 +00003353
3354# if defined(EXITFREE) || defined(PROTO)
3355 void
3356free_titles()
3357{
3358 vim_free(lasttitle);
3359 vim_free(lasticon);
3360}
3361# endif
3362
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363#endif /* FEAT_TITLE */
3364
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003365#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003367 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 * Return length of string in screen cells.
3369 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003370 * Normally works for window "wp", except when working for 'tabline' then it
3371 * is "curwin".
3372 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 * Items are drawn interspersed with the text that surrounds it
3374 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3375 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3376 *
3377 * If maxwidth is not zero, the string will be filled at any middle marker
3378 * or truncated if too long, fillchar is used for all whitespace.
3379 */
3380 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003381build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3382 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003384 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 size_t outlen; /* length of out[] */
3386 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003387 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 int fillchar;
3389 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003390 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3391 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392{
3393 char_u *p;
3394 char_u *s;
3395 char_u *t;
3396 char_u *linecont;
3397#ifdef FEAT_EVAL
3398 win_T *o_curwin;
3399 buf_T *o_curbuf;
3400#endif
3401 int empty_line;
3402 colnr_T virtcol;
3403 long l;
3404 long n;
3405 int prevchar_isflag;
3406 int prevchar_isitem;
3407 int itemisflag;
3408 int fillable;
3409 char_u *str;
3410 long num;
3411 int width;
3412 int itemcnt;
3413 int curitem;
3414 int groupitem[STL_MAX_ITEM];
3415 int groupdepth;
3416 struct stl_item
3417 {
3418 char_u *start;
3419 int minwid;
3420 int maxwid;
3421 enum
3422 {
3423 Normal,
3424 Empty,
3425 Group,
3426 Middle,
3427 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003428 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429 Trunc
3430 } type;
3431 } item[STL_MAX_ITEM];
3432 int minwid;
3433 int maxwid;
3434 int zeropad;
3435 char_u base;
3436 char_u opt;
3437#define TMPLEN 70
3438 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003439 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003440 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003441
3442#ifdef FEAT_EVAL
3443 /*
3444 * When the format starts with "%!" then evaluate it as an expression and
3445 * use the result as the actual format string.
3446 */
3447 if (fmt[0] == '%' && fmt[1] == '!')
3448 {
3449 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3450 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003451 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003452 }
3453#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454
3455 if (fillchar == 0)
3456 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003457#ifdef FEAT_MBYTE
3458 /* Can't handle a multi-byte fill character yet. */
3459 else if (mb_char2len(fillchar) > 1)
3460 fillchar = '-';
3461#endif
3462
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 /*
3464 * Get line & check if empty (cursorpos will show "0-1").
3465 * If inversion is possible we use it. Else '=' characters are used.
3466 */
3467 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3468 empty_line = (*linecont == NUL);
3469
3470 groupdepth = 0;
3471 p = out;
3472 curitem = 0;
3473 prevchar_isflag = TRUE;
3474 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003475 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003477 if (curitem == STL_MAX_ITEM)
3478 {
3479 /* There are too many items. Add the error code to the statusline
3480 * to give the user a hint about what went wrong. */
3481 if (p + 6 < out + outlen)
3482 {
3483 mch_memmove(p, " E541", (size_t)5);
3484 p += 5;
3485 }
3486 break;
3487 }
3488
Bram Moolenaar071d4272004-06-13 20:20:40 +00003489 if (*s != NUL && *s != '%')
3490 prevchar_isflag = prevchar_isitem = FALSE;
3491
3492 /*
3493 * Handle up to the next '%' or the end.
3494 */
3495 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3496 *p++ = *s++;
3497 if (*s == NUL || p + 1 >= out + outlen)
3498 break;
3499
3500 /*
3501 * Handle one '%' item.
3502 */
3503 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003504 if (*s == NUL) /* ignore trailing % */
3505 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 if (*s == '%')
3507 {
3508 if (p + 1 >= out + outlen)
3509 break;
3510 *p++ = *s++;
3511 prevchar_isflag = prevchar_isitem = FALSE;
3512 continue;
3513 }
3514 if (*s == STL_MIDDLEMARK)
3515 {
3516 s++;
3517 if (groupdepth > 0)
3518 continue;
3519 item[curitem].type = Middle;
3520 item[curitem++].start = p;
3521 continue;
3522 }
3523 if (*s == STL_TRUNCMARK)
3524 {
3525 s++;
3526 item[curitem].type = Trunc;
3527 item[curitem++].start = p;
3528 continue;
3529 }
3530 if (*s == ')')
3531 {
3532 s++;
3533 if (groupdepth < 1)
3534 continue;
3535 groupdepth--;
3536
3537 t = item[groupitem[groupdepth]].start;
3538 *p = NUL;
3539 l = vim_strsize(t);
3540 if (curitem > groupitem[groupdepth] + 1
3541 && item[groupitem[groupdepth]].minwid == 0)
3542 {
3543 /* remove group if all items are empty */
3544 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3545 if (item[n].type == Normal)
3546 break;
3547 if (n == curitem)
3548 {
3549 p = t;
3550 l = 0;
3551 }
3552 }
3553 if (l > item[groupitem[groupdepth]].maxwid)
3554 {
3555 /* truncate, remove n bytes of text at the start */
3556#ifdef FEAT_MBYTE
3557 if (has_mbyte)
3558 {
3559 /* Find the first character that should be included. */
3560 n = 0;
3561 while (l >= item[groupitem[groupdepth]].maxwid)
3562 {
3563 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003564 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003565 }
3566 }
3567 else
3568#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003569 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570
3571 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003572 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 p = p - n + 1;
3574#ifdef FEAT_MBYTE
3575 /* Fill up space left over by half a double-wide char. */
3576 while (++l < item[groupitem[groupdepth]].minwid)
3577 *p++ = fillchar;
3578#endif
3579
3580 /* correct the start of the items for the truncation */
3581 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3582 {
3583 item[l].start -= n;
3584 if (item[l].start < t)
3585 item[l].start = t;
3586 }
3587 }
3588 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3589 {
3590 /* fill */
3591 n = item[groupitem[groupdepth]].minwid;
3592 if (n < 0)
3593 {
3594 /* fill by appending characters */
3595 n = 0 - n;
3596 while (l++ < n && p + 1 < out + outlen)
3597 *p++ = fillchar;
3598 }
3599 else
3600 {
3601 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003602 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 l = n - l;
3604 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003605 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 p += l;
3607 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3608 item[n].start += l;
3609 for ( ; l > 0; l--)
3610 *t++ = fillchar;
3611 }
3612 }
3613 continue;
3614 }
3615 minwid = 0;
3616 maxwid = 9999;
3617 zeropad = FALSE;
3618 l = 1;
3619 if (*s == '0')
3620 {
3621 s++;
3622 zeropad = TRUE;
3623 }
3624 if (*s == '-')
3625 {
3626 s++;
3627 l = -1;
3628 }
3629 if (VIM_ISDIGIT(*s))
3630 {
3631 minwid = (int)getdigits(&s);
3632 if (minwid < 0) /* overflow */
3633 minwid = 0;
3634 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003635 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
3637 item[curitem].type = Highlight;
3638 item[curitem].start = p;
3639 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3640 s++;
3641 curitem++;
3642 continue;
3643 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003644 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3645 {
3646 if (*s == STL_TABCLOSENR)
3647 {
3648 if (minwid == 0)
3649 {
3650 /* %X ends the close label, go back to the previously
3651 * define tab label nr. */
3652 for (n = curitem - 1; n >= 0; --n)
3653 if (item[n].type == TabPage && item[n].minwid >= 0)
3654 {
3655 minwid = item[n].minwid;
3656 break;
3657 }
3658 }
3659 else
3660 /* close nrs are stored as negative values */
3661 minwid = - minwid;
3662 }
3663 item[curitem].type = TabPage;
3664 item[curitem].start = p;
3665 item[curitem].minwid = minwid;
3666 s++;
3667 curitem++;
3668 continue;
3669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 if (*s == '.')
3671 {
3672 s++;
3673 if (VIM_ISDIGIT(*s))
3674 {
3675 maxwid = (int)getdigits(&s);
3676 if (maxwid <= 0) /* overflow */
3677 maxwid = 50;
3678 }
3679 }
3680 minwid = (minwid > 50 ? 50 : minwid) * l;
3681 if (*s == '(')
3682 {
3683 groupitem[groupdepth++] = curitem;
3684 item[curitem].type = Group;
3685 item[curitem].start = p;
3686 item[curitem].minwid = minwid;
3687 item[curitem].maxwid = maxwid;
3688 s++;
3689 curitem++;
3690 continue;
3691 }
3692 if (vim_strchr(STL_ALL, *s) == NULL)
3693 {
3694 s++;
3695 continue;
3696 }
3697 opt = *s++;
3698
3699 /* OK - now for the real work */
3700 base = 'D';
3701 itemisflag = FALSE;
3702 fillable = TRUE;
3703 num = -1;
3704 str = NULL;
3705 switch (opt)
3706 {
3707 case STL_FILEPATH:
3708 case STL_FULLPATH:
3709 case STL_FILENAME:
3710 fillable = FALSE; /* don't change ' ' to fillchar */
3711 if (buf_spname(wp->w_buffer) != NULL)
3712 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3713 else
3714 {
3715 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003716 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3718 }
3719 trans_characters(NameBuff, MAXPATHL);
3720 if (opt != STL_FILENAME)
3721 str = NameBuff;
3722 else
3723 str = gettail(NameBuff);
3724 break;
3725
3726 case STL_VIM_EXPR: /* '{' */
3727 itemisflag = TRUE;
3728 t = p;
3729 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3730 *p++ = *s++;
3731 if (*s != '}') /* missing '}' or out of space */
3732 break;
3733 s++;
3734 *p = 0;
3735 p = t;
3736
3737#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003738 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003739 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3740
3741 o_curbuf = curbuf;
3742 o_curwin = curwin;
3743 curwin = wp;
3744 curbuf = wp->w_buffer;
3745
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003746 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
3748 curwin = o_curwin;
3749 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003750 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
3752 if (str != NULL && *str != 0)
3753 {
3754 if (*skipdigits(str) == NUL)
3755 {
3756 num = atoi((char *)str);
3757 vim_free(str);
3758 str = NULL;
3759 itemisflag = FALSE;
3760 }
3761 }
3762#endif
3763 break;
3764
3765 case STL_LINE:
3766 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3767 ? 0L : (long)(wp->w_cursor.lnum);
3768 break;
3769
3770 case STL_NUMLINES:
3771 num = wp->w_buffer->b_ml.ml_line_count;
3772 break;
3773
3774 case STL_COLUMN:
3775 num = !(State & INSERT) && empty_line
3776 ? 0 : (int)wp->w_cursor.col + 1;
3777 break;
3778
3779 case STL_VIRTCOL:
3780 case STL_VIRTCOL_ALT:
3781 /* In list mode virtcol needs to be recomputed */
3782 virtcol = wp->w_virtcol;
3783 if (wp->w_p_list && lcs_tab1 == NUL)
3784 {
3785 wp->w_p_list = FALSE;
3786 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3787 wp->w_p_list = TRUE;
3788 }
3789 ++virtcol;
3790 /* Don't display %V if it's the same as %c. */
3791 if (opt == STL_VIRTCOL_ALT
3792 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3793 ? 0 : (int)wp->w_cursor.col + 1)))
3794 break;
3795 num = (long)virtcol;
3796 break;
3797
3798 case STL_PERCENTAGE:
3799 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3800 (long)wp->w_buffer->b_ml.ml_line_count);
3801 break;
3802
3803 case STL_ALTPERCENT:
3804 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003805 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 break;
3807
3808 case STL_ARGLISTSTAT:
3809 fillable = FALSE;
3810 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003811 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 str = tmp;
3813 break;
3814
3815 case STL_KEYMAP:
3816 fillable = FALSE;
3817 if (get_keymap_str(wp, tmp, TMPLEN))
3818 str = tmp;
3819 break;
3820 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003821#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003822 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823#else
3824 num = 0;
3825#endif
3826 break;
3827
3828 case STL_BUFNO:
3829 num = wp->w_buffer->b_fnum;
3830 break;
3831
3832 case STL_OFFSET_X:
3833 base = 'X';
3834 case STL_OFFSET:
3835#ifdef FEAT_BYTEOFF
3836 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3837 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3838 0L : l + 1 + (!(State & INSERT) && empty_line ?
3839 0 : (int)wp->w_cursor.col);
3840#endif
3841 break;
3842
3843 case STL_BYTEVAL_X:
3844 base = 'X';
3845 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003846 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 num = 0;
3848 else
3849 {
3850#ifdef FEAT_MBYTE
3851 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3852#else
3853 num = linecont[wp->w_cursor.col];
3854#endif
3855 }
3856 if (num == NL)
3857 num = 0;
3858 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3859 num = NL;
3860 break;
3861
3862 case STL_ROFLAG:
3863 case STL_ROFLAG_ALT:
3864 itemisflag = TRUE;
3865 if (wp->w_buffer->b_p_ro)
3866 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3867 break;
3868
3869 case STL_HELPFLAG:
3870 case STL_HELPFLAG_ALT:
3871 itemisflag = TRUE;
3872 if (wp->w_buffer->b_help)
3873 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003874 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 break;
3876
3877#ifdef FEAT_AUTOCMD
3878 case STL_FILETYPE:
3879 if (*wp->w_buffer->b_p_ft != NUL
3880 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3881 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003882 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3883 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 str = tmp;
3885 }
3886 break;
3887
3888 case STL_FILETYPE_ALT:
3889 itemisflag = TRUE;
3890 if (*wp->w_buffer->b_p_ft != NUL
3891 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3892 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003893 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3894 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 for (t = tmp; *t != 0; t++)
3896 *t = TOUPPER_LOC(*t);
3897 str = tmp;
3898 }
3899 break;
3900#endif
3901
3902#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3903 case STL_PREVIEWFLAG:
3904 case STL_PREVIEWFLAG_ALT:
3905 itemisflag = TRUE;
3906 if (wp->w_p_pvw)
3907 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3908 : _("[Preview]"));
3909 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003910
3911 case STL_QUICKFIX:
3912 if (bt_quickfix(wp->w_buffer))
3913 str = (char_u *)(wp->w_llist_ref
3914 ? _(msg_loclist)
3915 : _(msg_qflist));
3916 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917#endif
3918
3919 case STL_MODIFIED:
3920 case STL_MODIFIED_ALT:
3921 itemisflag = TRUE;
3922 switch ((opt == STL_MODIFIED_ALT)
3923 + bufIsChanged(wp->w_buffer) * 2
3924 + (!wp->w_buffer->b_p_ma) * 4)
3925 {
3926 case 2: str = (char_u *)"[+]"; break;
3927 case 3: str = (char_u *)",+"; break;
3928 case 4: str = (char_u *)"[-]"; break;
3929 case 5: str = (char_u *)",-"; break;
3930 case 6: str = (char_u *)"[+-]"; break;
3931 case 7: str = (char_u *)",+-"; break;
3932 }
3933 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003934
3935 case STL_HIGHLIGHT:
3936 t = s;
3937 while (*s != '#' && *s != NUL)
3938 ++s;
3939 if (*s == '#')
3940 {
3941 item[curitem].type = Highlight;
3942 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003943 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003944 curitem++;
3945 }
3946 ++s;
3947 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
3949
3950 item[curitem].start = p;
3951 item[curitem].type = Normal;
3952 if (str != NULL && *str)
3953 {
3954 t = str;
3955 if (itemisflag)
3956 {
3957 if ((t[0] && t[1])
3958 && ((!prevchar_isitem && *t == ',')
3959 || (prevchar_isflag && *t == ' ')))
3960 t++;
3961 prevchar_isflag = TRUE;
3962 }
3963 l = vim_strsize(t);
3964 if (l > 0)
3965 prevchar_isitem = TRUE;
3966 if (l > maxwid)
3967 {
3968 while (l >= maxwid)
3969#ifdef FEAT_MBYTE
3970 if (has_mbyte)
3971 {
3972 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003973 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 else
3976#endif
3977 l -= byte2cells(*t++);
3978 if (p + 1 >= out + outlen)
3979 break;
3980 *p++ = '<';
3981 }
3982 if (minwid > 0)
3983 {
3984 for (; l < minwid && p + 1 < out + outlen; l++)
3985 {
3986 /* Don't put a "-" in front of a digit. */
3987 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3988 *p++ = ' ';
3989 else
3990 *p++ = fillchar;
3991 }
3992 minwid = 0;
3993 }
3994 else
3995 minwid *= -1;
3996 while (*t && p + 1 < out + outlen)
3997 {
3998 *p++ = *t++;
3999 /* Change a space by fillchar, unless fillchar is '-' and a
4000 * digit follows. */
4001 if (fillable && p[-1] == ' '
4002 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4003 p[-1] = fillchar;
4004 }
4005 for (; l < minwid && p + 1 < out + outlen; l++)
4006 *p++ = fillchar;
4007 }
4008 else if (num >= 0)
4009 {
4010 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4011 char_u nstr[20];
4012
4013 if (p + 20 >= out + outlen)
4014 break; /* not sufficient space */
4015 prevchar_isitem = TRUE;
4016 t = nstr;
4017 if (opt == STL_VIRTCOL_ALT)
4018 {
4019 *t++ = '-';
4020 minwid--;
4021 }
4022 *t++ = '%';
4023 if (zeropad)
4024 *t++ = '0';
4025 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004026 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 *t = 0;
4028
4029 for (n = num, l = 1; n >= nbase; n /= nbase)
4030 l++;
4031 if (opt == STL_VIRTCOL_ALT)
4032 l++;
4033 if (l > maxwid)
4034 {
4035 l += 2;
4036 n = l - maxwid;
4037 while (l-- > maxwid)
4038 num /= nbase;
4039 *t++ = '>';
4040 *t++ = '%';
4041 *t = t[-3];
4042 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004043 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4044 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004047 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4048 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 p += STRLEN(p);
4050 }
4051 else
4052 item[curitem].type = Empty;
4053
4054 if (opt == STL_VIM_EXPR)
4055 vim_free(str);
4056
4057 if (num >= 0 || (!itemisflag && str && *str))
4058 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4059 curitem++;
4060 }
4061 *p = NUL;
4062 itemcnt = curitem;
4063
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004064#ifdef FEAT_EVAL
4065 if (usefmt != fmt)
4066 vim_free(usefmt);
4067#endif
4068
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 width = vim_strsize(out);
4070 if (maxwidth > 0 && width > maxwidth)
4071 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004072 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 l = 0;
4074 if (itemcnt == 0)
4075 s = out;
4076 else
4077 {
4078 for ( ; l < itemcnt; l++)
4079 if (item[l].type == Trunc)
4080 {
4081 /* Truncate at %< item. */
4082 s = item[l].start;
4083 break;
4084 }
4085 if (l == itemcnt)
4086 {
4087 /* No %< item, truncate first item. */
4088 s = item[0].start;
4089 l = 0;
4090 }
4091 }
4092
4093 if (width - vim_strsize(s) >= maxwidth)
4094 {
4095 /* Truncation mark is beyond max length */
4096#ifdef FEAT_MBYTE
4097 if (has_mbyte)
4098 {
4099 s = out;
4100 width = 0;
4101 for (;;)
4102 {
4103 width += ptr2cells(s);
4104 if (width >= maxwidth)
4105 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004106 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108 /* Fill up for half a double-wide character. */
4109 while (++width < maxwidth)
4110 *s++ = fillchar;
4111 }
4112 else
4113#endif
4114 s = out + maxwidth - 1;
4115 for (l = 0; l < itemcnt; l++)
4116 if (item[l].start > s)
4117 break;
4118 itemcnt = l;
4119 *s++ = '>';
4120 *s = 0;
4121 }
4122 else
4123 {
4124#ifdef FEAT_MBYTE
4125 if (has_mbyte)
4126 {
4127 n = 0;
4128 while (width >= maxwidth)
4129 {
4130 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004131 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 }
4133 }
4134 else
4135#endif
4136 n = width - maxwidth + 1;
4137 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004138 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 *s = '<';
4140
4141 /* Fill up for half a double-wide character. */
4142 while (++width < maxwidth)
4143 {
4144 s = s + STRLEN(s);
4145 *s++ = fillchar;
4146 *s = NUL;
4147 }
4148
4149 --n; /* count the '<' */
4150 for (; l < itemcnt; l++)
4151 {
4152 if (item[l].start - n >= s)
4153 item[l].start -= n;
4154 else
4155 item[l].start = s;
4156 }
4157 }
4158 width = maxwidth;
4159 }
4160 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4161 {
4162 /* Apply STL_MIDDLE if any */
4163 for (l = 0; l < itemcnt; l++)
4164 if (item[l].type == Middle)
4165 break;
4166 if (l < itemcnt)
4167 {
4168 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004169 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 for (s = item[l].start; s < p; s++)
4171 *s = fillchar;
4172 for (l++; l < itemcnt; l++)
4173 item[l].start += maxwidth - width;
4174 width = maxwidth;
4175 }
4176 }
4177
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004178 /* Store the info about highlighting. */
4179 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004181 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 for (l = 0; l < itemcnt; l++)
4183 {
4184 if (item[l].type == Highlight)
4185 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004186 sp->start = item[l].start;
4187 sp->userhl = item[l].minwid;
4188 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004191 sp->start = NULL;
4192 sp->userhl = 0;
4193 }
4194
4195 /* Store the info about tab pages labels. */
4196 if (tabtab != NULL)
4197 {
4198 sp = tabtab;
4199 for (l = 0; l < itemcnt; l++)
4200 {
4201 if (item[l].type == TabPage)
4202 {
4203 sp->start = item[l].start;
4204 sp->userhl = item[l].minwid;
4205 sp++;
4206 }
4207 }
4208 sp->start = NULL;
4209 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 }
4211
4212 return width;
4213}
4214#endif /* FEAT_STL_OPT */
4215
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004216#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4217 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004219 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4220 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 */
4222 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004223get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004225 char_u *buf;
4226 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227{
4228 long above; /* number of lines above window */
4229 long below; /* number of lines below window */
4230
4231 above = wp->w_topline - 1;
4232#ifdef FEAT_DIFF
4233 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4234#endif
4235 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4236 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004237 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4238 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004240 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004242 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 ? (int)(above / ((above + below) / 100L))
4244 : (int)(above * 100L / (above + below)));
4245}
4246#endif
4247
4248/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004249 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 * Return TRUE if it was appended.
4251 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004252 static int
4253append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 win_T *wp;
4255 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004256 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258{
4259 char_u *p;
4260
4261 if (ARGCOUNT <= 1) /* nothing to do */
4262 return FALSE;
4263
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004264 p = buf + STRLEN(buf); /* go to the end of the buffer */
4265 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 return FALSE;
4267 *p++ = ' ';
4268 *p++ = '(';
4269 if (add_file)
4270 {
4271 STRCPY(p, "file ");
4272 p += 5;
4273 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004274 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4275 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4277 return TRUE;
4278}
4279
4280/*
4281 * If fname is not a full path, make it a full path.
4282 * Returns pointer to allocated memory (NULL for failure).
4283 */
4284 char_u *
4285fix_fname(fname)
4286 char_u *fname;
4287{
4288 /*
4289 * Force expanding the path always for Unix, because symbolic links may
4290 * mess up the full path name, even though it starts with a '/'.
4291 * Also expand when there is ".." in the file name, try to remove it,
4292 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004293 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 * For MS-Windows also expand names like "longna~1" to "longname".
4295 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004296#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 return FullName_save(fname, TRUE);
4298#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004299 if (!vim_isAbsName(fname)
4300 || strstr((char *)fname, "..") != NULL
4301 || strstr((char *)fname, "//") != NULL
4302# ifdef BACKSLASH_IN_FILENAME
4303 || strstr((char *)fname, "\\\\") != NULL
4304# endif
4305# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 )
4309 return FullName_save(fname, FALSE);
4310
4311 fname = vim_strsave(fname);
4312
Bram Moolenaar9b942202007-10-03 12:31:33 +00004313# ifdef USE_FNAME_CASE
4314# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004316# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 {
4318 if (fname != NULL)
4319 fname_case(fname, 0); /* set correct case for file name */
4320 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004321# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
4323 return fname;
4324#endif
4325}
4326
4327/*
4328 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4329 * "ffname" becomes a pointer to allocated memory (or NULL).
4330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 void
4332fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004333 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 char_u **ffname;
4335 char_u **sfname;
4336{
4337 if (*ffname == NULL) /* if no file name given, nothing to do */
4338 return;
4339 if (*sfname == NULL) /* if no short file name given, use ffname */
4340 *sfname = *ffname;
4341 *ffname = fix_fname(*ffname); /* expand to full path */
4342
4343#ifdef FEAT_SHORTCUT
4344 if (!buf->b_p_bin)
4345 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004346 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
4348 /* If the file name is a shortcut file, use the file it links to. */
4349 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004350 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
4352 vim_free(*ffname);
4353 *ffname = rfname;
4354 *sfname = rfname;
4355 }
4356 }
4357#endif
4358}
4359
4360/*
4361 * Get the file name for an argument list entry.
4362 */
4363 char_u *
4364alist_name(aep)
4365 aentry_T *aep;
4366{
4367 buf_T *bp;
4368
4369 /* Use the name from the associated buffer if it exists. */
4370 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004371 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 return aep->ae_fname;
4373 return bp->b_fname;
4374}
4375
4376#if defined(FEAT_WINDOWS) || defined(PROTO)
4377/*
4378 * do_arg_all(): Open up to 'count' windows, one for each argument.
4379 */
4380 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004381do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 int count;
4383 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004384 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385{
4386 int i;
4387 win_T *wp, *wpnext;
4388 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004389 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 int use_firstwin = FALSE; /* use first window for arglist */
4391 int split_ret = OK;
4392 int p_ea_save;
4393 alist_T *alist; /* argument list to be used */
4394 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004395 tabpage_T *tpnext;
4396 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004397 win_T *new_curwin = NULL;
4398 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399
4400 if (ARGCOUNT <= 0)
4401 {
4402 /* Don't give an error message. We don't want it when the ":all"
4403 * command is in the .vimrc. */
4404 return;
4405 }
4406 setpcmark();
4407
4408 opened_len = ARGCOUNT;
4409 opened = alloc_clear((unsigned)opened_len);
4410 if (opened == NULL)
4411 return;
4412
4413#ifdef FEAT_GUI
4414 need_mouse_correct = TRUE;
4415#endif
4416
4417 /*
4418 * Try closing all windows that are not in the argument list.
4419 * Also close windows that are not full width;
4420 * When 'hidden' or "forceit" set the buffer becomes hidden.
4421 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004422 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004424 if (had_tab > 0)
4425 goto_tabpage_tp(first_tabpage);
4426 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004428 tpnext = curtab->tp_next;
4429 for (wp = firstwin; wp != NULL; wp = wpnext)
4430 {
4431 wpnext = wp->w_next;
4432 buf = wp->w_buffer;
4433 if (buf->b_ffname == NULL
4434 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004436 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004438 )
4439 i = ARGCOUNT;
4440 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004442 /* check if the buffer in this window is in the arglist */
4443 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004445 if (ARGLIST[i].ae_fnum == buf->b_fnum
4446 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004447 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004449 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004450 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004451 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004452 if (i == 0)
4453 {
4454 new_curwin = wp;
4455 new_curtab = curtab;
4456 }
4457 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004458 if (wp->w_alist != curwin->w_alist)
4459 {
4460 /* Use the current argument list for all windows
4461 * containing a file from it. */
4462 alist_unlink(wp->w_alist);
4463 wp->w_alist = curwin->w_alist;
4464 ++wp->w_alist->al_refcount;
4465 }
4466 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004470 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004472 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004474 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004475 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004477 /* If the buffer was changed, and we would like to hide it,
4478 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004479 if (!P_HID(buf) && buf->b_nwindows <= 1
4480 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004482 (void)autowrite(buf, FALSE);
4483#ifdef FEAT_AUTOCMD
4484 /* check if autocommands removed the window */
4485 if (!win_valid(wp) || !buf_valid(buf))
4486 {
4487 wpnext = firstwin; /* start all over... */
4488 continue;
4489 }
4490#endif
4491 }
4492#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004493 /* don't close last window */
4494 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004495#endif
4496 use_firstwin = TRUE;
4497#ifdef FEAT_WINDOWS
4498 else
4499 {
4500 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4501# ifdef FEAT_AUTOCMD
4502 /* check if autocommands removed the next window */
4503 if (!win_valid(wpnext))
4504 wpnext = firstwin; /* start all over... */
4505# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 }
4507#endif
4508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004511
4512 /* Without the ":tab" modifier only do the current tab page. */
4513 if (had_tab == 0 || tpnext == NULL)
4514 break;
4515
4516# ifdef FEAT_AUTOCMD
4517 /* check if autocommands removed the next tab page */
4518 if (!valid_tabpage(tpnext))
4519 tpnext = first_tabpage; /* start all over...*/
4520# endif
4521 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 }
4523
4524 /*
4525 * Open a window for files in the argument list that don't have one.
4526 * ARGCOUNT may change while doing this, because of autocommands.
4527 */
4528 if (count > ARGCOUNT || count <= 0)
4529 count = ARGCOUNT;
4530
4531 /* Autocommands may do anything to the argument list. Make sure it's not
4532 * freed while we are working here by "locking" it. We still have to
4533 * watch out for its size to be changed. */
4534 alist = curwin->w_alist;
4535 ++alist->al_refcount;
4536
4537#ifdef FEAT_AUTOCMD
4538 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4539 ++autocmd_no_enter;
4540 ++autocmd_no_leave;
4541#endif
4542 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004543#ifdef FEAT_WINDOWS
4544 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4545 * leaving an empty tab page when executed locally. */
4546 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4547 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4548 use_firstwin = TRUE;
4549#endif
4550
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4552 {
4553 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4554 arg_had_last = TRUE;
4555 if (i < opened_len && opened[i])
4556 {
4557 /* Move the already present window to below the current window */
4558 if (curwin->w_arg_idx != i)
4559 {
4560 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4561 {
4562 if (wpnext->w_arg_idx == i)
4563 {
4564 win_move_after(wpnext, curwin);
4565 break;
4566 }
4567 }
4568 }
4569 }
4570 else if (split_ret == OK)
4571 {
4572 if (!use_firstwin) /* split current window */
4573 {
4574 p_ea_save = p_ea;
4575 p_ea = TRUE; /* use space from all windows */
4576 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4577 p_ea = p_ea_save;
4578 if (split_ret == FAIL)
4579 continue;
4580 }
4581#ifdef FEAT_AUTOCMD
4582 else /* first window: do autocmd for leaving this buffer */
4583 --autocmd_no_leave;
4584#endif
4585
4586 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004587 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 */
4589 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004590 if (i == 0)
4591 {
4592 new_curwin = curwin;
4593 new_curtab = curtab;
4594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4596 ECMD_ONE,
4597 ((P_HID(curwin->w_buffer)
4598 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004599 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600#ifdef FEAT_AUTOCMD
4601 if (use_firstwin)
4602 ++autocmd_no_leave;
4603#endif
4604 use_firstwin = FALSE;
4605 }
4606 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004607
4608 /* When ":tab" was used open a new tab for a new window repeatedly. */
4609 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4610 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 }
4612
4613 /* Remove the "lock" on the argument list. */
4614 alist_unlink(alist);
4615
4616#ifdef FEAT_AUTOCMD
4617 --autocmd_no_enter;
4618#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004619 /* to window with first arg */
4620 if (valid_tabpage(new_curtab))
4621 goto_tabpage_tp(new_curtab);
4622 if (win_valid(new_curwin))
4623 win_enter(new_curwin, FALSE);
4624
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625#ifdef FEAT_AUTOCMD
4626 --autocmd_no_leave;
4627#endif
4628 vim_free(opened);
4629}
4630
4631# if defined(FEAT_LISTCMDS) || defined(PROTO)
4632/*
4633 * Open a window for a number of buffers.
4634 */
4635 void
4636ex_buffer_all(eap)
4637 exarg_T *eap;
4638{
4639 buf_T *buf;
4640 win_T *wp, *wpnext;
4641 int split_ret = OK;
4642 int p_ea_save;
4643 int open_wins = 0;
4644 int r;
4645 int count; /* Maximum number of windows to open. */
4646 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004647#ifdef FEAT_WINDOWS
4648 int had_tab = cmdmod.tab;
4649 tabpage_T *tpnext;
4650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651
4652 if (eap->addr_count == 0) /* make as many windows as possible */
4653 count = 9999;
4654 else
4655 count = eap->line2; /* make as many windows as specified */
4656 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4657 all = FALSE;
4658 else
4659 all = TRUE;
4660
4661 setpcmark();
4662
4663#ifdef FEAT_GUI
4664 need_mouse_correct = TRUE;
4665#endif
4666
4667 /*
4668 * Close superfluous windows (two windows for the same buffer).
4669 * Also close windows that are not full-width.
4670 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004671#ifdef FEAT_WINDOWS
4672 if (had_tab > 0)
4673 goto_tabpage_tp(first_tabpage);
4674 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004677 tpnext = curtab->tp_next;
4678 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004680 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004681 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004682#ifdef FEAT_VERTSPLIT
4683 || ((cmdmod.split & WSP_VERT)
4684 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004685 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004686 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004688#ifdef FEAT_WINDOWS
4689 || (had_tab > 0 && wp != firstwin)
4690#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004691 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004692 {
4693 win_close(wp, FALSE);
4694#ifdef FEAT_AUTOCMD
4695 wpnext = firstwin; /* just in case an autocommand does
4696 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004697 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004698 open_wins = 0;
4699#endif
4700 }
4701 else
4702 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004704
4705#ifdef FEAT_WINDOWS
4706 /* Without the ":tab" modifier only do the current tab page. */
4707 if (had_tab == 0 || tpnext == NULL)
4708 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004709 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
4713 /*
4714 * Go through the buffer list. When a buffer doesn't have a window yet,
4715 * open one. Otherwise move the window to the right position.
4716 * Watch out for autocommands that delete buffers or windows!
4717 */
4718#ifdef FEAT_AUTOCMD
4719 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4720 ++autocmd_no_enter;
4721#endif
4722 win_enter(lastwin, FALSE);
4723#ifdef FEAT_AUTOCMD
4724 ++autocmd_no_leave;
4725#endif
4726 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4727 {
4728 /* Check if this buffer needs a window */
4729 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4730 continue;
4731
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004732#ifdef FEAT_WINDOWS
4733 if (had_tab != 0)
4734 {
4735 /* With the ":tab" modifier don't move the window. */
4736 if (buf->b_nwindows > 0)
4737 wp = lastwin; /* buffer has a window, skip it */
4738 else
4739 wp = NULL;
4740 }
4741 else
4742#endif
4743 {
4744 /* Check if this buffer already has a window */
4745 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4746 if (wp->w_buffer == buf)
4747 break;
4748 /* If the buffer already has a window, move it */
4749 if (wp != NULL)
4750 win_move_after(wp, curwin);
4751 }
4752
4753 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755 /* Split the window and put the buffer in it */
4756 p_ea_save = p_ea;
4757 p_ea = TRUE; /* use space from all windows */
4758 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4759 ++open_wins;
4760 p_ea = p_ea_save;
4761 if (split_ret == FAIL)
4762 continue;
4763
4764 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004765#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 swap_exists_action = SEA_DIALOG;
4767#endif
4768 set_curbuf(buf, DOBUF_GOTO);
4769#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004772#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004774# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 break;
4776 }
4777#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004778#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 if (swap_exists_action == SEA_QUIT)
4780 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004781# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4782 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004783
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004784 /* Reset the error/interrupt/exception state here so that
4785 * aborting() returns FALSE when closing a window. */
4786 enter_cleanup(&cs);
4787# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004788
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004789 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 win_close(curwin, TRUE);
4791 --open_wins;
4792 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004793 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004794
4795# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4796 /* Restore the error/interrupt/exception state if not
4797 * discarded by a new aborting error, interrupt, or uncaught
4798 * exception. */
4799 leave_cleanup(&cs);
4800# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
4802 else
4803 handle_swap_exists(NULL);
4804#endif
4805 }
4806
4807 ui_breakcheck();
4808 if (got_int)
4809 {
4810 (void)vgetc(); /* only break the file loading, not the rest */
4811 break;
4812 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004813#ifdef FEAT_EVAL
4814 /* Autocommands deleted the buffer or aborted script processing!!! */
4815 if (aborting())
4816 break;
4817#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004818#ifdef FEAT_WINDOWS
4819 /* When ":tab" was used open a new tab for a new window repeatedly. */
4820 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4821 cmdmod.tab = 9999;
4822#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
4824#ifdef FEAT_AUTOCMD
4825 --autocmd_no_enter;
4826#endif
4827 win_enter(firstwin, FALSE); /* back to first window */
4828#ifdef FEAT_AUTOCMD
4829 --autocmd_no_leave;
4830#endif
4831
4832 /*
4833 * Close superfluous windows.
4834 */
4835 for (wp = lastwin; open_wins > count; )
4836 {
4837 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4838 || autowrite(wp->w_buffer, FALSE) == OK);
4839#ifdef FEAT_AUTOCMD
4840 if (!win_valid(wp))
4841 {
4842 /* BufWrite Autocommands made the window invalid, start over */
4843 wp = lastwin;
4844 }
4845 else
4846#endif
4847 if (r)
4848 {
4849 win_close(wp, !P_HID(wp->w_buffer));
4850 --open_wins;
4851 wp = lastwin;
4852 }
4853 else
4854 {
4855 wp = wp->w_prev;
4856 if (wp == NULL)
4857 break;
4858 }
4859 }
4860}
4861# endif /* FEAT_LISTCMDS */
4862
4863#endif /* FEAT_WINDOWS */
4864
Bram Moolenaara3227e22006-03-08 21:32:40 +00004865static int chk_modeline __ARGS((linenr_T, int));
4866
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867/*
4868 * do_modelines() - process mode lines for the current file
4869 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004870 * "flags" can be:
4871 * OPT_WINONLY only set options local to window
4872 * OPT_NOWIN don't set options local to window
4873 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 * Returns immediately if the "ml" option isn't set.
4875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004877do_modelines(flags)
4878 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004880 linenr_T lnum;
4881 int nmlines;
4882 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883
4884 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4885 return;
4886
4887 /* Disallow recursive entry here. Can happen when executing a modeline
4888 * triggers an autocommand, which reloads modelines with a ":do". */
4889 if (entered)
4890 return;
4891
4892 ++entered;
4893 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4894 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004895 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 nmlines = 0;
4897
4898 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4899 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004900 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 nmlines = 0;
4902 --entered;
4903}
4904
4905#include "version.h" /* for version number */
4906
4907/*
4908 * chk_modeline() - check a single line for a mode string
4909 * Return FAIL if an error encountered.
4910 */
4911 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004912chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004914 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915{
4916 char_u *s;
4917 char_u *e;
4918 char_u *linecopy; /* local copy of any modeline found */
4919 int prev;
4920 int vers;
4921 int end;
4922 int retval = OK;
4923 char_u *save_sourcing_name;
4924 linenr_T save_sourcing_lnum;
4925#ifdef FEAT_EVAL
4926 scid_T save_SID;
4927#endif
4928
4929 prev = -1;
4930 for (s = ml_get(lnum); *s != NUL; ++s)
4931 {
4932 if (prev == -1 || vim_isspace(prev))
4933 {
4934 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4935 || STRNCMP(s, "vi:", (size_t)3) == 0)
4936 break;
4937 if (STRNCMP(s, "vim", 3) == 0)
4938 {
4939 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4940 e = s + 4;
4941 else
4942 e = s + 3;
4943 vers = getdigits(&e);
4944 if (*e == ':'
4945 && (s[3] == ':'
4946 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4947 || (VIM_VERSION_100 < vers && s[3] == '<')
4948 || (VIM_VERSION_100 > vers && s[3] == '>')
4949 || (VIM_VERSION_100 == vers && s[3] == '=')))
4950 break;
4951 }
4952 }
4953 prev = *s;
4954 }
4955
4956 if (*s)
4957 {
4958 do /* skip over "ex:", "vi:" or "vim:" */
4959 ++s;
4960 while (s[-1] != ':');
4961
4962 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4963 if (linecopy == NULL)
4964 return FAIL;
4965
4966 save_sourcing_lnum = sourcing_lnum;
4967 save_sourcing_name = sourcing_name;
4968 sourcing_lnum = lnum; /* prepare for emsg() */
4969 sourcing_name = (char_u *)"modelines";
4970
4971 end = FALSE;
4972 while (end == FALSE)
4973 {
4974 s = skipwhite(s);
4975 if (*s == NUL)
4976 break;
4977
4978 /*
4979 * Find end of set command: ':' or end of line.
4980 * Skip over "\:", replacing it with ":".
4981 */
4982 for (e = s; *e != ':' && *e != NUL; ++e)
4983 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004984 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 if (*e == NUL)
4986 end = TRUE;
4987
4988 /*
4989 * If there is a "set" command, require a terminating ':' and
4990 * ignore the stuff after the ':'.
4991 * "vi:set opt opt opt: foo" -- foo not interpreted
4992 * "vi:opt opt opt: foo" -- foo interpreted
4993 * Accept "se" for compatibility with Elvis.
4994 */
4995 if (STRNCMP(s, "set ", (size_t)4) == 0
4996 || STRNCMP(s, "se ", (size_t)3) == 0)
4997 {
4998 if (*e != ':') /* no terminating ':'? */
4999 break;
5000 end = TRUE;
5001 s = vim_strchr(s, ' ') + 1;
5002 }
5003 *e = NUL; /* truncate the set command */
5004
5005 if (*s != NUL) /* skip over an empty "::" */
5006 {
5007#ifdef FEAT_EVAL
5008 save_SID = current_SID;
5009 current_SID = SID_MODELINE;
5010#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005011 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012#ifdef FEAT_EVAL
5013 current_SID = save_SID;
5014#endif
5015 if (retval == FAIL) /* stop if error found */
5016 break;
5017 }
5018 s = e + 1; /* advance to next part */
5019 }
5020
5021 sourcing_lnum = save_sourcing_lnum;
5022 sourcing_name = save_sourcing_name;
5023
5024 vim_free(linecopy);
5025 }
5026 return retval;
5027}
5028
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005029#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 int
5031read_viminfo_bufferlist(virp, writing)
5032 vir_T *virp;
5033 int writing;
5034{
5035 char_u *tab;
5036 linenr_T lnum;
5037 colnr_T col;
5038 buf_T *buf;
5039 char_u *sfname;
5040 char_u *xline;
5041
5042 /* Handle long line and escaped characters. */
5043 xline = viminfo_readstring(virp, 1, FALSE);
5044
5045 /* don't read in if there are files on the command-line or if writing: */
5046 if (xline != NULL && !writing && ARGCOUNT == 0
5047 && find_viminfo_parameter('%') != NULL)
5048 {
5049 /* Format is: <fname> Tab <lnum> Tab <col>.
5050 * Watch out for a Tab in the file name, work from the end. */
5051 lnum = 0;
5052 col = 0;
5053 tab = vim_strrchr(xline, '\t');
5054 if (tab != NULL)
5055 {
5056 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005057 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 tab = vim_strrchr(xline, '\t');
5059 if (tab != NULL)
5060 {
5061 *tab++ = '\0';
5062 lnum = atol((char *)tab);
5063 }
5064 }
5065
5066 /* Expand "~/" in the file name at "line + 1" to a full path.
5067 * Then try shortening it by comparing with the current directory */
5068 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005069 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070
5071 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5072 if (buf != NULL) /* just in case... */
5073 {
5074 buf->b_last_cursor.lnum = lnum;
5075 buf->b_last_cursor.col = col;
5076 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5077 }
5078 }
5079 vim_free(xline);
5080
5081 return viminfo_readline(virp);
5082}
5083
5084 void
5085write_viminfo_bufferlist(fp)
5086 FILE *fp;
5087{
5088 buf_T *buf;
5089#ifdef FEAT_WINDOWS
5090 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005091 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092#endif
5093 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005094 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095
5096 if (find_viminfo_parameter('%') == NULL)
5097 return;
5098
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005099 /* Without a number -1 is returned: do all buffers. */
5100 max_buffers = get_viminfo_parameter('%');
5101
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005103#define LINE_BUF_LEN (MAXPATHL + 40)
5104 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 if (line == NULL)
5106 return;
5107
5108#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005109 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 set_last_cursor(win);
5111#else
5112 set_last_cursor(curwin);
5113#endif
5114
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005115 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5117 {
5118 if (buf->b_fname == NULL
5119 || !buf->b_p_bl
5120#ifdef FEAT_QUICKFIX
5121 || bt_quickfix(buf)
5122#endif
5123 || removable(buf->b_ffname))
5124 continue;
5125
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005126 if (max_buffers-- == 0)
5127 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 putc('%', fp);
5129 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005130 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 (long)buf->b_last_cursor.lnum,
5132 buf->b_last_cursor.col);
5133 viminfo_writestring(fp, line);
5134 }
5135 vim_free(line);
5136}
5137#endif
5138
5139
5140/*
5141 * Return special buffer name.
5142 * Returns NULL when the buffer has a normal file name.
5143 */
5144 char *
5145buf_spname(buf)
5146 buf_T *buf;
5147{
5148#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5149 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005150 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005151 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005152 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005153
5154 /*
5155 * For location list window, w_llist_ref points to the location list.
5156 * For quickfix window, w_llist_ref is NULL.
5157 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005158 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005159 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005160 goto win_found;
5161win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005162 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005163 return _(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005164 else
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005165 return _(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167#endif
5168#ifdef FEAT_QUICKFIX
5169 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5170 * contains the name as specified by the user */
5171 if (bt_nofile(buf))
5172 {
5173 if (buf->b_sfname != NULL)
5174 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005175 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177#endif
5178 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005179 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 return NULL;
5181}
5182
5183
5184#if defined(FEAT_SIGNS) || defined(PROTO)
5185/*
5186 * Insert the sign into the signlist.
5187 */
5188 static void
5189insert_sign(buf, prev, next, id, lnum, typenr)
5190 buf_T *buf; /* buffer to store sign in */
5191 signlist_T *prev; /* previous sign entry */
5192 signlist_T *next; /* next sign entry */
5193 int id; /* sign ID */
5194 linenr_T lnum; /* line number which gets the mark */
5195 int typenr; /* typenr of sign we are adding */
5196{
5197 signlist_T *newsign;
5198
5199 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5200 if (newsign != NULL)
5201 {
5202 newsign->id = id;
5203 newsign->lnum = lnum;
5204 newsign->typenr = typenr;
5205 newsign->next = next;
5206#ifdef FEAT_NETBEANS_INTG
5207 newsign->prev = prev;
5208 if (next != NULL)
5209 next->prev = newsign;
5210#endif
5211
5212 if (prev == NULL)
5213 {
5214 /* When adding first sign need to redraw the windows to create the
5215 * column for signs. */
5216 if (buf->b_signlist == NULL)
5217 {
5218 redraw_buf_later(buf, NOT_VALID);
5219 changed_cline_bef_curs();
5220 }
5221
5222 /* first sign in signlist */
5223 buf->b_signlist = newsign;
5224 }
5225 else
5226 prev->next = newsign;
5227 }
5228}
5229
5230/*
5231 * Add the sign into the signlist. Find the right spot to do it though.
5232 */
5233 void
5234buf_addsign(buf, id, lnum, typenr)
5235 buf_T *buf; /* buffer to store sign in */
5236 int id; /* sign ID */
5237 linenr_T lnum; /* line number which gets the mark */
5238 int typenr; /* typenr of sign we are adding */
5239{
5240 signlist_T *sign; /* a sign in the signlist */
5241 signlist_T *prev; /* the previous sign */
5242
5243 prev = NULL;
5244 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5245 {
5246 if (lnum == sign->lnum && id == sign->id)
5247 {
5248 sign->typenr = typenr;
5249 return;
5250 }
5251 else if (
5252#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5253 id < 0 &&
5254#endif
5255 lnum < sign->lnum)
5256 {
5257#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5258 /* XXX - GRP: Is this because of sign slide problem? Or is it
5259 * really needed? Or is it because we allow multiple signs per
5260 * line? If so, should I add that feature to FEAT_SIGNS?
5261 */
5262 while (prev != NULL && prev->lnum == lnum)
5263 prev = prev->prev;
5264 if (prev == NULL)
5265 sign = buf->b_signlist;
5266 else
5267 sign = prev->next;
5268#endif
5269 insert_sign(buf, prev, sign, id, lnum, typenr);
5270 return;
5271 }
5272 prev = sign;
5273 }
5274#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5275 /* XXX - GRP: See previous comment */
5276 while (prev != NULL && prev->lnum == lnum)
5277 prev = prev->prev;
5278 if (prev == NULL)
5279 sign = buf->b_signlist;
5280 else
5281 sign = prev->next;
5282#endif
5283 insert_sign(buf, prev, sign, id, lnum, typenr);
5284
5285 return;
5286}
5287
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005288 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289buf_change_sign_type(buf, markId, typenr)
5290 buf_T *buf; /* buffer to store sign in */
5291 int markId; /* sign ID */
5292 int typenr; /* typenr of sign we are adding */
5293{
5294 signlist_T *sign; /* a sign in the signlist */
5295
5296 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5297 {
5298 if (sign->id == markId)
5299 {
5300 sign->typenr = typenr;
5301 return sign->lnum;
5302 }
5303 }
5304
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005305 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306}
5307
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005308 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309buf_getsigntype(buf, lnum, type)
5310 buf_T *buf;
5311 linenr_T lnum;
5312 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5313{
5314 signlist_T *sign; /* a sign in a b_signlist */
5315
5316 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5317 if (sign->lnum == lnum
5318 && (type == SIGN_ANY
5319# ifdef FEAT_SIGN_ICONS
5320 || (type == SIGN_ICON
5321 && sign_get_image(sign->typenr) != NULL)
5322# endif
5323 || (type == SIGN_TEXT
5324 && sign_get_text(sign->typenr) != NULL)
5325 || (type == SIGN_LINEHL
5326 && sign_get_attr(sign->typenr, TRUE) != 0)))
5327 return sign->typenr;
5328 return 0;
5329}
5330
5331
5332 linenr_T
5333buf_delsign(buf, id)
5334 buf_T *buf; /* buffer sign is stored in */
5335 int id; /* sign id */
5336{
5337 signlist_T **lastp; /* pointer to pointer to current sign */
5338 signlist_T *sign; /* a sign in a b_signlist */
5339 signlist_T *next; /* the next sign in a b_signlist */
5340 linenr_T lnum; /* line number whose sign was deleted */
5341
5342 lastp = &buf->b_signlist;
5343 lnum = 0;
5344 for (sign = buf->b_signlist; sign != NULL; sign = next)
5345 {
5346 next = sign->next;
5347 if (sign->id == id)
5348 {
5349 *lastp = next;
5350#ifdef FEAT_NETBEANS_INTG
5351 if (next != NULL)
5352 next->prev = sign->prev;
5353#endif
5354 lnum = sign->lnum;
5355 vim_free(sign);
5356 break;
5357 }
5358 else
5359 lastp = &sign->next;
5360 }
5361
5362 /* When deleted the last sign need to redraw the windows to remove the
5363 * sign column. */
5364 if (buf->b_signlist == NULL)
5365 {
5366 redraw_buf_later(buf, NOT_VALID);
5367 changed_cline_bef_curs();
5368 }
5369
5370 return lnum;
5371}
5372
5373
5374/*
5375 * Find the line number of the sign with the requested id. If the sign does
5376 * not exist, return 0 as the line number. This will still let the correct file
5377 * get loaded.
5378 */
5379 int
5380buf_findsign(buf, id)
5381 buf_T *buf; /* buffer to store sign in */
5382 int id; /* sign ID */
5383{
5384 signlist_T *sign; /* a sign in the signlist */
5385
5386 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5387 if (sign->id == id)
5388 return sign->lnum;
5389
5390 return 0;
5391}
5392
5393 int
5394buf_findsign_id(buf, lnum)
5395 buf_T *buf; /* buffer whose sign we are searching for */
5396 linenr_T lnum; /* line number of sign */
5397{
5398 signlist_T *sign; /* a sign in the signlist */
5399
5400 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5401 if (sign->lnum == lnum)
5402 return sign->id;
5403
5404 return 0;
5405}
5406
5407
5408# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5409/* see if a given type of sign exists on a specific line */
5410 int
5411buf_findsigntype_id(buf, lnum, typenr)
5412 buf_T *buf; /* buffer whose sign we are searching for */
5413 linenr_T lnum; /* line number of sign */
5414 int typenr; /* sign type number */
5415{
5416 signlist_T *sign; /* a sign in the signlist */
5417
5418 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5419 if (sign->lnum == lnum && sign->typenr == typenr)
5420 return sign->id;
5421
5422 return 0;
5423}
5424
5425
5426# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5427/* return the number of icons on the given line */
5428 int
5429buf_signcount(buf, lnum)
5430 buf_T *buf;
5431 linenr_T lnum;
5432{
5433 signlist_T *sign; /* a sign in the signlist */
5434 int count = 0;
5435
5436 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5437 if (sign->lnum == lnum)
5438 if (sign_get_image(sign->typenr) != NULL)
5439 count++;
5440
5441 return count;
5442}
5443# endif /* FEAT_SIGN_ICONS */
5444# endif /* FEAT_NETBEANS_INTG */
5445
5446
5447/*
5448 * Delete signs in buffer "buf".
5449 */
5450 static void
5451buf_delete_signs(buf)
5452 buf_T *buf;
5453{
5454 signlist_T *next;
5455
5456 while (buf->b_signlist != NULL)
5457 {
5458 next = buf->b_signlist->next;
5459 vim_free(buf->b_signlist);
5460 buf->b_signlist = next;
5461 }
5462}
5463
5464/*
5465 * Delete all signs in all buffers.
5466 */
5467 void
5468buf_delete_all_signs()
5469{
5470 buf_T *buf; /* buffer we are checking for signs */
5471
5472 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5473 if (buf->b_signlist != NULL)
5474 {
5475 /* Need to redraw the windows to remove the sign column. */
5476 redraw_buf_later(buf, NOT_VALID);
5477 buf_delete_signs(buf);
5478 }
5479}
5480
5481/*
5482 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5483 */
5484 void
5485sign_list_placed(rbuf)
5486 buf_T *rbuf;
5487{
5488 buf_T *buf;
5489 signlist_T *p;
5490 char lbuf[BUFSIZ];
5491
5492 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5493 msg_putchar('\n');
5494 if (rbuf == NULL)
5495 buf = firstbuf;
5496 else
5497 buf = rbuf;
5498 while (buf != NULL)
5499 {
5500 if (buf->b_signlist != NULL)
5501 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005502 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5504 msg_putchar('\n');
5505 }
5506 for (p = buf->b_signlist; p != NULL; p = p->next)
5507 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005508 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5510 MSG_PUTS(lbuf);
5511 msg_putchar('\n');
5512 }
5513 if (rbuf != NULL)
5514 break;
5515 buf = buf->b_next;
5516 }
5517}
5518
5519/*
5520 * Adjust a placed sign for inserted/deleted lines.
5521 */
5522 void
5523sign_mark_adjust(line1, line2, amount, amount_after)
5524 linenr_T line1;
5525 linenr_T line2;
5526 long amount;
5527 long amount_after;
5528{
5529 signlist_T *sign; /* a sign in a b_signlist */
5530
5531 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5532 {
5533 if (sign->lnum >= line1 && sign->lnum <= line2)
5534 {
5535 if (amount == MAXLNUM)
5536 sign->lnum = line1;
5537 else
5538 sign->lnum += amount;
5539 }
5540 else if (sign->lnum > line2)
5541 sign->lnum += amount_after;
5542 }
5543}
5544#endif /* FEAT_SIGNS */
5545
5546/*
5547 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5548 */
5549 void
5550set_buflisted(on)
5551 int on;
5552{
5553 if (on != curbuf->b_p_bl)
5554 {
5555 curbuf->b_p_bl = on;
5556#ifdef FEAT_AUTOCMD
5557 if (on)
5558 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5559 else
5560 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5561#endif
5562 }
5563}
5564
5565/*
5566 * Read the file for "buf" again and check if the contents changed.
5567 * Return TRUE if it changed or this could not be checked.
5568 */
5569 int
5570buf_contents_changed(buf)
5571 buf_T *buf;
5572{
5573 buf_T *newbuf;
5574 int differ = TRUE;
5575 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 exarg_T ea;
5578
5579 /* Allocate a buffer without putting it in the buffer list. */
5580 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5581 if (newbuf == NULL)
5582 return TRUE;
5583
5584 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5585 if (prep_exarg(&ea, buf) == FAIL)
5586 {
5587 wipe_buffer(newbuf, FALSE);
5588 return TRUE;
5589 }
5590
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 /* set curwin/curbuf to buf and save a few things */
5592 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593
Bram Moolenaar4770d092006-01-12 23:22:24 +00005594 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 && readfile(buf->b_ffname, buf->b_fname,
5596 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5597 &ea, READ_NEW | READ_DUMMY) == OK)
5598 {
5599 /* compare the two files line by line */
5600 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5601 {
5602 differ = FALSE;
5603 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5604 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5605 {
5606 differ = TRUE;
5607 break;
5608 }
5609 }
5610 }
5611 vim_free(ea.cmd);
5612
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 /* restore curwin/curbuf and a few other things */
5614 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005615
5616 if (curbuf != newbuf) /* safety check */
5617 wipe_buffer(newbuf, FALSE);
5618
5619 return differ;
5620}
5621
5622/*
5623 * Wipe out a buffer and decrement the last buffer number if it was used for
5624 * this buffer. Call this to wipe out a temp buffer that does not contain any
5625 * marks.
5626 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 void
5628wipe_buffer(buf, aucmd)
5629 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005630 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631{
5632 if (buf->b_fnum == top_file_num - 1)
5633 --top_file_num;
5634
5635#ifdef FEAT_AUTOCMD
5636 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005637 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005638#endif
5639 close_buffer(NULL, buf, DOBUF_WIPE);
5640#ifdef FEAT_AUTOCMD
5641 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005642 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643#endif
5644}