blob: 254331ef2b8d0c0817d73cd2e6bc1fb61f7636cc [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 Moolenaar071d4272004-06-13 20:20:40 +0000419
420#ifdef FEAT_AUTOCMD
421 /* Autocommands may have deleted the buffer. */
422 if (!buf_valid(buf))
423 return;
424# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000425 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426 return;
427# endif
428
429 /* Autocommands may have opened or closed windows for this buffer.
430 * Decrement the count for the close we do here. */
431 if (buf->b_nwindows > 0)
432 --buf->b_nwindows;
433
434 /*
435 * It's possible that autocommands change curbuf to the one being deleted.
436 * This might cause the previous curbuf to be deleted unexpectedly. But
437 * in some cases it's OK to delete the curbuf, because a new one is
438 * obtained anyway. Therefore only return if curbuf changed to the
439 * deleted buffer.
440 */
441 if (buf == curbuf && !is_curbuf)
442 return;
443#endif
444
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000445 /* Change directories when the 'acd' option is set. */
446 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447
448 /*
449 * Remove the buffer from the list.
450 */
451 if (wipe_buf)
452 {
453#ifdef FEAT_SUN_WORKSHOP
454 if (usingSunWorkShop)
455 workshop_file_closed_lineno((char *)buf->b_ffname,
456 (int)buf->b_last_cursor.lnum);
457#endif
458 vim_free(buf->b_ffname);
459 vim_free(buf->b_sfname);
460 if (buf->b_prev == NULL)
461 firstbuf = buf->b_next;
462 else
463 buf->b_prev->b_next = buf->b_next;
464 if (buf->b_next == NULL)
465 lastbuf = buf->b_prev;
466 else
467 buf->b_next->b_prev = buf->b_prev;
468 free_buffer(buf);
469 }
470 else
471 {
472 if (del_buf)
473 {
474 /* Free all internal variables and reset option values, to make
475 * ":bdel" compatible with Vim 5.7. */
476 free_buffer_stuff(buf, TRUE);
477
478 /* Make it look like a new buffer. */
479 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
480
481 /* Init the options when loaded again. */
482 buf->b_p_initialized = FALSE;
483 }
484 buf_clear_file(buf);
485 if (del_buf)
486 buf->b_p_bl = FALSE;
487 }
488}
489
490/*
491 * Make buffer not contain a file.
492 */
493 void
494buf_clear_file(buf)
495 buf_T *buf;
496{
497 buf->b_ml.ml_line_count = 1;
498 unchanged(buf, TRUE);
499#ifndef SHORT_FNAME
500 buf->b_shortname = FALSE;
501#endif
502 buf->b_p_eol = TRUE;
503 buf->b_start_eol = TRUE;
504#ifdef FEAT_MBYTE
505 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000506 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507#endif
508 buf->b_ml.ml_mfp = NULL;
509 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
510#ifdef FEAT_NETBEANS_INTG
511 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512#endif
513}
514
515/*
516 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200517 * the file. flags:
518 * BFA_DEL buffer is going to be deleted
519 * BFA_WIPE buffer is going to be wiped out
520 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200523buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200525 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
527#ifdef FEAT_AUTOCMD
528 int is_curbuf = (buf == curbuf);
529
530 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
531 if (!buf_valid(buf)) /* autocommands may delete the buffer */
532 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200533 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 {
535 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
536 if (!buf_valid(buf)) /* autocommands may delete the buffer */
537 return;
538 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200539 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 {
541 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
542 FALSE, buf);
543 if (!buf_valid(buf)) /* autocommands may delete the buffer */
544 return;
545 }
546# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000547 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 return;
549# endif
550
551 /*
552 * It's possible that autocommands change curbuf to the one being deleted.
553 * This might cause curbuf to be deleted unexpectedly. But in some cases
554 * it's OK to delete the curbuf, because a new one is obtained anyway.
555 * Therefore only return if curbuf changed to the deleted buffer.
556 */
557 if (buf == curbuf && !is_curbuf)
558 return;
559#endif
560#ifdef FEAT_DIFF
561 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
562#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000563
564#ifdef FEAT_FOLDING
565 /* No folds in an empty buffer. */
566# ifdef FEAT_WINDOWS
567 {
568 win_T *win;
569 tabpage_T *tp;
570
571 FOR_ALL_TAB_WINDOWS(tp, win)
572 if (win->w_buffer == buf)
573 clearFolding(win);
574 }
575# else
576 if (curwin->w_buffer == buf)
577 clearFolding(curwin);
578# endif
579#endif
580
Bram Moolenaar071d4272004-06-13 20:20:40 +0000581#ifdef FEAT_TCL
582 tcl_buffer_free(buf);
583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000584 ml_close(buf, TRUE); /* close and delete the memline/memfile */
585 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200586 if ((flags & BFA_KEEP_UNDO) == 0)
587 {
588 u_blockfree(buf); /* free the memory allocated for undo */
589 u_clearall(buf); /* reset all undo information */
590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200592 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000594 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595}
596
597/*
598 * Free a buffer structure and the things it contains related to the buffer
599 * itself (not the file, that must have been done already).
600 */
601 static void
602free_buffer(buf)
603 buf_T *buf;
604{
605 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200606#ifdef FEAT_LUA
607 lua_buffer_free(buf);
608#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000609#ifdef FEAT_MZSCHEME
610 mzscheme_buffer_free(buf);
611#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612#ifdef FEAT_PERL
613 perl_buf_free(buf);
614#endif
615#ifdef FEAT_PYTHON
616 python_buffer_free(buf);
617#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200618#ifdef FEAT_PYTHON3
619 python3_buffer_free(buf);
620#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000621#ifdef FEAT_RUBY
622 ruby_buffer_free(buf);
623#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000624#ifdef FEAT_AUTOCMD
625 aubuflocal_remove(buf);
626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 vim_free(buf);
628}
629
630/*
631 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
632 */
633 static void
634free_buffer_stuff(buf, free_options)
635 buf_T *buf;
636 int free_options; /* free options as well */
637{
638 if (free_options)
639 {
640 clear_wininfo(buf); /* including window-local options */
641 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200642#ifdef FEAT_SPELL
643 ga_clear(&buf->b_s.b_langp);
644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 }
646#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000647 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
648 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649#endif
650#ifdef FEAT_USR_CMDS
651 uc_clear(&buf->b_ucmds); /* clear local user commands */
652#endif
653#ifdef FEAT_SIGNS
654 buf_delete_signs(buf); /* delete any signs */
655#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000656#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200657 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000658#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#ifdef FEAT_LOCALMAP
660 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
661 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
662#endif
663#ifdef FEAT_MBYTE
664 vim_free(buf->b_start_fenc);
665 buf->b_start_fenc = NULL;
666#endif
667}
668
669/*
670 * Free the b_wininfo list for buffer "buf".
671 */
672 static void
673clear_wininfo(buf)
674 buf_T *buf;
675{
676 wininfo_T *wip;
677
678 while (buf->b_wininfo != NULL)
679 {
680 wip = buf->b_wininfo;
681 buf->b_wininfo = wip->wi_next;
682 if (wip->wi_optset)
683 {
684 clear_winopt(&wip->wi_opt);
685#ifdef FEAT_FOLDING
686 deleteFoldRecurse(&wip->wi_folds);
687#endif
688 }
689 vim_free(wip);
690 }
691}
692
693#if defined(FEAT_LISTCMDS) || defined(PROTO)
694/*
695 * Go to another buffer. Handles the result of the ATTENTION dialog.
696 */
697 void
698goto_buffer(eap, start, dir, count)
699 exarg_T *eap;
700 int start;
701 int dir;
702 int count;
703{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000704# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 buf_T *old_curbuf = curbuf;
706
707 swap_exists_action = SEA_DIALOG;
708# endif
709 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
710 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000711# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
713 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000714# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
715 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000716
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000717 /* Reset the error/interrupt/exception state here so that
718 * aborting() returns FALSE when closing a window. */
719 enter_cleanup(&cs);
720# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000721
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000722 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 win_close(curwin, TRUE);
724 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000725 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000726
727# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
728 /* Restore the error/interrupt/exception state if not discarded by a
729 * new aborting error, interrupt, or uncaught exception. */
730 leave_cleanup(&cs);
731# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 }
733 else
734 handle_swap_exists(old_curbuf);
735# endif
736}
737#endif
738
Bram Moolenaare64ac772005-12-07 20:54:59 +0000739#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740/*
741 * Handle the situation of swap_exists_action being set.
742 * It is allowed for "old_curbuf" to be NULL or invalid.
743 */
744 void
745handle_swap_exists(old_curbuf)
746 buf_T *old_curbuf;
747{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000748# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
749 cleanup_T cs;
750# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000751
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752 if (swap_exists_action == SEA_QUIT)
753 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000754# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
755 /* Reset the error/interrupt/exception state here so that
756 * aborting() returns FALSE when closing a buffer. */
757 enter_cleanup(&cs);
758# endif
759
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 /* User selected Quit at ATTENTION prompt. Go back to previous
761 * buffer. If that buffer is gone or the same as the current one,
762 * open a new, empty buffer. */
763 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000764 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
766 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000767 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 if (old_curbuf != NULL)
769 enter_buffer(old_curbuf);
770 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000771
772# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
773 /* Restore the error/interrupt/exception state if not discarded by a
774 * new aborting error, interrupt, or uncaught exception. */
775 leave_cleanup(&cs);
776# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 }
778 else if (swap_exists_action == SEA_RECOVER)
779 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000780# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
781 /* Reset the error/interrupt/exception state here so that
782 * aborting() returns FALSE when closing a buffer. */
783 enter_cleanup(&cs);
784# endif
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* User selected Recover at ATTENTION prompt. */
787 msg_scroll = TRUE;
788 ml_recover();
789 MSG_PUTS("\n"); /* don't overwrite the last message */
790 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000791 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000792
793# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
794 /* Restore the error/interrupt/exception state if not discarded by a
795 * new aborting error, interrupt, or uncaught exception. */
796 leave_cleanup(&cs);
797# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 }
799 swap_exists_action = SEA_NONE;
800}
801#endif
802
803#if defined(FEAT_LISTCMDS) || defined(PROTO)
804/*
805 * do_bufdel() - delete or unload buffer(s)
806 *
807 * addr_count == 0: ":bdel" - delete current buffer
808 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
809 * buffer "end_bnr", then any other arguments.
810 * addr_count == 2: ":N,N bdel" - delete buffers in range
811 *
812 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
813 * DOBUF_DEL (":bdel")
814 *
815 * Returns error message or NULL
816 */
817 char_u *
818do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
819 int command;
820 char_u *arg; /* pointer to extra arguments */
821 int addr_count;
822 int start_bnr; /* first buffer number in a range */
823 int end_bnr; /* buffer nr or last buffer nr in a range */
824 int forceit;
825{
826 int do_current = 0; /* delete current buffer? */
827 int deleted = 0; /* number of buffers deleted */
828 char_u *errormsg = NULL; /* return value */
829 int bnr; /* buffer number */
830 char_u *p;
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 if (addr_count == 0)
833 {
834 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
835 }
836 else
837 {
838 if (addr_count == 2)
839 {
840 if (*arg) /* both range and argument is not allowed */
841 return (char_u *)_(e_trailing);
842 bnr = start_bnr;
843 }
844 else /* addr_count == 1 */
845 bnr = end_bnr;
846
847 for ( ;!got_int; ui_breakcheck())
848 {
849 /*
850 * delete the current buffer last, otherwise when the
851 * current buffer is deleted, the next buffer becomes
852 * the current one and will be loaded, which may then
853 * also be deleted, etc.
854 */
855 if (bnr == curbuf->b_fnum)
856 do_current = bnr;
857 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
858 forceit) == OK)
859 ++deleted;
860
861 /*
862 * find next buffer number to delete/unload
863 */
864 if (addr_count == 2)
865 {
866 if (++bnr > end_bnr)
867 break;
868 }
869 else /* addr_count == 1 */
870 {
871 arg = skipwhite(arg);
872 if (*arg == NUL)
873 break;
874 if (!VIM_ISDIGIT(*arg))
875 {
876 p = skiptowhite_esc(arg);
877 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
878 if (bnr < 0) /* failed */
879 break;
880 arg = p;
881 }
882 else
883 bnr = getdigits(&arg);
884 }
885 }
886 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
887 FORWARD, do_current, forceit) == OK)
888 ++deleted;
889
890 if (deleted == 0)
891 {
892 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000893 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000895 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000897 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 errormsg = IObuff;
899 }
900 else if (deleted >= p_report)
901 {
902 if (command == DOBUF_UNLOAD)
903 {
904 if (deleted == 1)
905 MSG(_("1 buffer unloaded"));
906 else
907 smsg((char_u *)_("%d buffers unloaded"), deleted);
908 }
909 else if (command == DOBUF_DEL)
910 {
911 if (deleted == 1)
912 MSG(_("1 buffer deleted"));
913 else
914 smsg((char_u *)_("%d buffers deleted"), deleted);
915 }
916 else
917 {
918 if (deleted == 1)
919 MSG(_("1 buffer wiped out"));
920 else
921 smsg((char_u *)_("%d buffers wiped out"), deleted);
922 }
923 }
924 }
925
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926
927 return errormsg;
928}
929
930/*
931 * Implementation of the commands for the buffer list.
932 *
933 * action == DOBUF_GOTO go to specified buffer
934 * action == DOBUF_SPLIT split window and go to specified buffer
935 * action == DOBUF_UNLOAD unload specified buffer(s)
936 * action == DOBUF_DEL delete specified buffer(s) from buffer list
937 * action == DOBUF_WIPE delete specified buffer(s) really
938 *
939 * start == DOBUF_CURRENT go to "count" buffer from current buffer
940 * start == DOBUF_FIRST go to "count" buffer from first buffer
941 * start == DOBUF_LAST go to "count" buffer from last buffer
942 * start == DOBUF_MOD go to "count" modified buffer from current buffer
943 *
944 * Return FAIL or OK.
945 */
946 int
947do_buffer(action, start, dir, count, forceit)
948 int action;
949 int start;
950 int dir; /* FORWARD or BACKWARD */
951 int count; /* buffer number or number of buffers */
952 int forceit; /* TRUE for :...! */
953{
954 buf_T *buf;
955 buf_T *bp;
956 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
957 || action == DOBUF_WIPE);
958
959 switch (start)
960 {
961 case DOBUF_FIRST: buf = firstbuf; break;
962 case DOBUF_LAST: buf = lastbuf; break;
963 default: buf = curbuf; break;
964 }
965 if (start == DOBUF_MOD) /* find next modified buffer */
966 {
967 while (count-- > 0)
968 {
969 do
970 {
971 buf = buf->b_next;
972 if (buf == NULL)
973 buf = firstbuf;
974 }
975 while (buf != curbuf && !bufIsChanged(buf));
976 }
977 if (!bufIsChanged(buf))
978 {
979 EMSG(_("E84: No modified buffer found"));
980 return FAIL;
981 }
982 }
983 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
984 {
985 while (buf != NULL && buf->b_fnum != count)
986 buf = buf->b_next;
987 }
988 else
989 {
990 bp = NULL;
991 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
992 {
993 /* remember the buffer where we start, we come back there when all
994 * buffers are unlisted. */
995 if (bp == NULL)
996 bp = buf;
997 if (dir == FORWARD)
998 {
999 buf = buf->b_next;
1000 if (buf == NULL)
1001 buf = firstbuf;
1002 }
1003 else
1004 {
1005 buf = buf->b_prev;
1006 if (buf == NULL)
1007 buf = lastbuf;
1008 }
1009 /* don't count unlisted buffers */
1010 if (unload || buf->b_p_bl)
1011 {
1012 --count;
1013 bp = NULL; /* use this buffer as new starting point */
1014 }
1015 if (bp == buf)
1016 {
1017 /* back where we started, didn't find anything. */
1018 EMSG(_("E85: There is no listed buffer"));
1019 return FAIL;
1020 }
1021 }
1022 }
1023
1024 if (buf == NULL) /* could not find it */
1025 {
1026 if (start == DOBUF_FIRST)
1027 {
1028 /* don't warn when deleting */
1029 if (!unload)
1030 EMSGN(_("E86: Buffer %ld does not exist"), count);
1031 }
1032 else if (dir == FORWARD)
1033 EMSG(_("E87: Cannot go beyond last buffer"));
1034 else
1035 EMSG(_("E88: Cannot go before first buffer"));
1036 return FAIL;
1037 }
1038
1039#ifdef FEAT_GUI
1040 need_mouse_correct = TRUE;
1041#endif
1042
1043#ifdef FEAT_LISTCMDS
1044 /*
1045 * delete buffer buf from memory and/or the list
1046 */
1047 if (unload)
1048 {
1049 int forward;
1050 int retval;
1051
1052 /* When unloading or deleting a buffer that's already unloaded and
1053 * unlisted: fail silently. */
1054 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1055 return FAIL;
1056
1057 if (!forceit && bufIsChanged(buf))
1058 {
1059#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1060 if ((p_confirm || cmdmod.confirm) && p_write)
1061 {
1062 dialog_changed(buf, FALSE);
1063# ifdef FEAT_AUTOCMD
1064 if (!buf_valid(buf))
1065 /* Autocommand deleted buffer, oops! It's not changed
1066 * now. */
1067 return FAIL;
1068# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001069 /* If it's still changed fail silently, the dialog already
1070 * mentioned why it fails. */
1071 if (bufIsChanged(buf))
1072 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#endif
1076 {
1077 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1078 buf->b_fnum);
1079 return FAIL;
1080 }
1081 }
1082
1083 /*
1084 * If deleting the last (listed) buffer, make it empty.
1085 * The last (listed) buffer cannot be unloaded.
1086 */
1087 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1088 if (bp->b_p_bl && bp != buf)
1089 break;
1090 if (bp == NULL && buf == curbuf)
1091 {
1092 if (action == DOBUF_UNLOAD)
1093 {
1094 EMSG(_("E90: Cannot unload last buffer"));
1095 return FAIL;
1096 }
1097
1098 /* Close any other windows on this buffer, then make it empty. */
1099#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001100 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#endif
1102 setpcmark();
1103 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001104 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105
1106 /*
1107 * do_ecmd() may create a new buffer, then we have to delete
1108 * the old one. But do_ecmd() may have done that already, check
1109 * if the buffer still exists.
1110 */
1111 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1112 close_buffer(NULL, buf, action);
1113 return retval;
1114 }
1115
1116#ifdef FEAT_WINDOWS
1117 /*
1118 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001119 * (unless it's the only window). Repeat this so long as we end up in
1120 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001122 while (buf == curbuf
1123 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 win_close(curwin, FALSE);
1125#endif
1126
1127 /*
1128 * If the buffer to be deleted is not the current one, delete it here.
1129 */
1130 if (buf != curbuf)
1131 {
1132#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001133 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#endif
1135 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1136 close_buffer(NULL, buf, action);
1137 return OK;
1138 }
1139
1140 /*
1141 * Deleting the current buffer: Need to find another buffer to go to.
1142 * There must be another, otherwise it would have been handled above.
1143 * First use au_new_curbuf, if it is valid.
1144 * Then prefer the buffer we most recently visited.
1145 * Else try to find one that is loaded, after the current buffer,
1146 * then before the current buffer.
1147 * Finally use any buffer.
1148 */
1149 buf = NULL; /* selected buffer */
1150 bp = NULL; /* used when no loaded buffer found */
1151#ifdef FEAT_AUTOCMD
1152 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1153 buf = au_new_curbuf;
1154# ifdef FEAT_JUMPLIST
1155 else
1156# endif
1157#endif
1158#ifdef FEAT_JUMPLIST
1159 if (curwin->w_jumplistlen > 0)
1160 {
1161 int jumpidx;
1162
1163 jumpidx = curwin->w_jumplistidx - 1;
1164 if (jumpidx < 0)
1165 jumpidx = curwin->w_jumplistlen - 1;
1166
1167 forward = jumpidx;
1168 while (jumpidx != curwin->w_jumplistidx)
1169 {
1170 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1171 if (buf != NULL)
1172 {
1173 if (buf == curbuf || !buf->b_p_bl)
1174 buf = NULL; /* skip current and unlisted bufs */
1175 else if (buf->b_ml.ml_mfp == NULL)
1176 {
1177 /* skip unloaded buf, but may keep it for later */
1178 if (bp == NULL)
1179 bp = buf;
1180 buf = NULL;
1181 }
1182 }
1183 if (buf != NULL) /* found a valid buffer: stop searching */
1184 break;
1185 /* advance to older entry in jump list */
1186 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1187 break;
1188 if (--jumpidx < 0)
1189 jumpidx = curwin->w_jumplistlen - 1;
1190 if (jumpidx == forward) /* List exhausted for sure */
1191 break;
1192 }
1193 }
1194#endif
1195
1196 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1197 {
1198 forward = TRUE;
1199 buf = curbuf->b_next;
1200 for (;;)
1201 {
1202 if (buf == NULL)
1203 {
1204 if (!forward) /* tried both directions */
1205 break;
1206 buf = curbuf->b_prev;
1207 forward = FALSE;
1208 continue;
1209 }
1210 /* in non-help buffer, try to skip help buffers, and vv */
1211 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1212 {
1213 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1214 break;
1215 if (bp == NULL) /* remember unloaded buf for later */
1216 bp = buf;
1217 }
1218 if (forward)
1219 buf = buf->b_next;
1220 else
1221 buf = buf->b_prev;
1222 }
1223 }
1224 if (buf == NULL) /* No loaded buffer, use unloaded one */
1225 buf = bp;
1226 if (buf == NULL) /* No loaded buffer, find listed one */
1227 {
1228 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1229 if (buf->b_p_bl && buf != curbuf)
1230 break;
1231 }
1232 if (buf == NULL) /* Still no buffer, just take one */
1233 {
1234 if (curbuf->b_next != NULL)
1235 buf = curbuf->b_next;
1236 else
1237 buf = curbuf->b_prev;
1238 }
1239 }
1240
1241 /*
1242 * make buf current buffer
1243 */
1244 if (action == DOBUF_SPLIT) /* split window first */
1245 {
1246# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001247 /* If 'switchbuf' contains "useopen": jump to first window containing
1248 * "buf" if one exists */
1249 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001250 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001251 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001252 * page containing "buf" if one exists */
1253 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 return OK;
1255 if (win_split(0, 0) == FAIL)
1256# endif
1257 return FAIL;
1258 }
1259#endif
1260
1261 /* go to current buffer - nothing to do */
1262 if (buf == curbuf)
1263 return OK;
1264
1265 /*
1266 * Check if the current buffer may be abandoned.
1267 */
1268 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1269 {
1270#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1271 if ((p_confirm || cmdmod.confirm) && p_write)
1272 {
1273 dialog_changed(curbuf, FALSE);
1274# ifdef FEAT_AUTOCMD
1275 if (!buf_valid(buf))
1276 /* Autocommand deleted buffer, oops! */
1277 return FAIL;
1278# endif
1279 }
1280 if (bufIsChanged(curbuf))
1281#endif
1282 {
1283 EMSG(_(e_nowrtmsg));
1284 return FAIL;
1285 }
1286 }
1287
1288 /* Go to the other buffer. */
1289 set_curbuf(buf, action);
1290
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001291#if defined(FEAT_LISTCMDS) \
1292 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001294 {
1295 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297#endif
1298
1299#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1300 if (aborting()) /* autocmds may abort script processing */
1301 return FAIL;
1302#endif
1303
1304 return OK;
1305}
1306
1307#endif /* FEAT_LISTCMDS */
1308
1309/*
1310 * Set current buffer to "buf". Executes autocommands and closes current
1311 * buffer. "action" tells how to close the current buffer:
1312 * DOBUF_GOTO free or hide it
1313 * DOBUF_SPLIT nothing
1314 * DOBUF_UNLOAD unload it
1315 * DOBUF_DEL delete it
1316 * DOBUF_WIPE wipe it out
1317 */
1318 void
1319set_curbuf(buf, action)
1320 buf_T *buf;
1321 int action;
1322{
1323 buf_T *prevbuf;
1324 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1325 || action == DOBUF_WIPE);
1326
1327 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001328 if (!cmdmod.keepalt)
1329 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001330 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331
1332#ifdef FEAT_VISUAL
1333 /* Don't restart Select mode after switching to another buffer. */
1334 VIsual_reselect = FALSE;
1335#endif
1336
1337 /* close_windows() or apply_autocmds() may change curbuf */
1338 prevbuf = curbuf;
1339
1340#ifdef FEAT_AUTOCMD
1341 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1342# ifdef FEAT_EVAL
1343 if (buf_valid(prevbuf) && !aborting())
1344# else
1345 if (buf_valid(prevbuf))
1346# endif
1347#endif
1348 {
1349#ifdef FEAT_WINDOWS
1350 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001351 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352#endif
1353#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1354 if (buf_valid(prevbuf) && !aborting())
1355#else
1356 if (buf_valid(prevbuf))
1357#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001358 {
1359 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001360 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1362 unload ? action : (action == DOBUF_GOTO
1363 && !P_HID(prevbuf)
1364 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 }
1367#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001368 /* An autocommand may have deleted "buf", already entered it (e.g., when
1369 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001371 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001373 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374# endif
1375#endif
1376 enter_buffer(buf);
1377}
1378
1379/*
1380 * Enter a new current buffer.
1381 * Old curbuf must have been abandoned already!
1382 */
1383 void
1384enter_buffer(buf)
1385 buf_T *buf;
1386{
1387 /* Copy buffer and window local option values. Not for a help buffer. */
1388 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1389 if (!buf->b_help)
1390 get_winopts(buf);
1391#ifdef FEAT_FOLDING
1392 else
1393 /* Remove all folds in the window. */
1394 clearFolding(curwin);
1395 foldUpdateAll(curwin); /* update folds (later). */
1396#endif
1397
Bram Moolenaar860cae12010-06-05 23:22:07 +02001398#ifdef FEAT_SYN_HL
Bram Moolenaarfd29f462010-06-06 16:11:09 +02001399 reset_synblock(curwin);
Bram Moolenaar860cae12010-06-05 23:22:07 +02001400 curwin->w_s = &(buf->b_s);
1401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 /* Get the buffer in the current window. */
1403 curwin->w_buffer = buf;
1404 curbuf = buf;
1405 ++curbuf->b_nwindows;
1406
1407#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001408 if (curwin->w_p_diff)
1409 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410#endif
1411
1412 /* Cursor on first line by default. */
1413 curwin->w_cursor.lnum = 1;
1414 curwin->w_cursor.col = 0;
1415#ifdef FEAT_VIRTUALEDIT
1416 curwin->w_cursor.coladd = 0;
1417#endif
1418 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001419#ifdef FEAT_AUTOCMD
1420 curwin->w_topline_was_set = FALSE;
1421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001423 /* mark cursor position as being invalid */
1424 curwin->w_valid = 0;
1425
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 /* Make sure the buffer is loaded. */
1427 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001428 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001429#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001430 /* If there is no filetype, allow for detecting one. Esp. useful for
1431 * ":ball" used in a autocommand. If there already is a filetype we
1432 * might prefer to keep it. */
1433 if (*curbuf->b_p_ft == NUL)
1434 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001435#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001436
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001437 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439 else
1440 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001441 if (!msg_silent)
1442 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1444#ifdef FEAT_AUTOCMD
1445 curwin->w_topline = 1;
1446# ifdef FEAT_DIFF
1447 curwin->w_topfill = 0;
1448# endif
1449 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1450 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1451#endif
1452 }
1453
1454 /* If autocommands did not change the cursor position, restore cursor lnum
1455 * and possibly cursor col. */
1456 if (curwin->w_cursor.lnum == 1 && inindent(0))
1457 buflist_getfpos();
1458
1459 check_arg_idx(curwin); /* check for valid arg_idx */
1460#ifdef FEAT_TITLE
1461 maketitle();
1462#endif
1463#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001464 /* when autocmds didn't change it */
1465 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#endif
1467 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1468
Bram Moolenaar009b2592004-10-24 19:18:58 +00001469#ifdef FEAT_NETBEANS_INTG
1470 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001471 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001472#endif
1473
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001474 /* Change directories when the 'acd' option is set. */
1475 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476
1477#ifdef FEAT_KEYMAP
1478 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001479 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001481#ifdef FEAT_SPELL
1482 /* May need to set the spell language. Can only do this after the buffer
1483 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001484 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1485 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001486#endif
1487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 redraw_later(NOT_VALID);
1489}
1490
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001491#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1492/*
1493 * Change to the directory of the current buffer.
1494 */
1495 void
1496do_autochdir()
1497{
1498 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1499 shorten_fnames(TRUE);
1500}
1501#endif
1502
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503/*
1504 * functions for dealing with the buffer list
1505 */
1506
1507/*
1508 * Add a file name to the buffer list. Return a pointer to the buffer.
1509 * If the same file name already exists return a pointer to that buffer.
1510 * If it does not exist, or if fname == NULL, a new entry is created.
1511 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1512 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1513 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 * This is the ONLY way to create a new buffer.
1515 */
1516static int top_file_num = 1; /* highest file number */
1517
1518 buf_T *
1519buflist_new(ffname, sfname, lnum, flags)
1520 char_u *ffname; /* full path of fname or relative */
1521 char_u *sfname; /* short fname or NULL */
1522 linenr_T lnum; /* preferred cursor line */
1523 int flags; /* BLN_ defines */
1524{
1525 buf_T *buf;
1526#ifdef UNIX
1527 struct stat st;
1528#endif
1529
1530 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1531
1532 /*
1533 * If file name already exists in the list, update the entry.
1534 */
1535#ifdef UNIX
1536 /* On Unix we can use inode numbers when the file exists. Works better
1537 * for hard links. */
1538 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1539 st.st_dev = (dev_T)-1;
1540#endif
1541 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1542#ifdef UNIX
1543 buflist_findname_stat(ffname, &st)
1544#else
1545 buflist_findname(ffname)
1546#endif
1547 ) != NULL)
1548 {
1549 vim_free(ffname);
1550 if (lnum != 0)
1551 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1552 /* copy the options now, if 'cpo' doesn't have 's' and not done
1553 * already */
1554 buf_copy_options(buf, 0);
1555 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1556 {
1557 buf->b_p_bl = TRUE;
1558#ifdef FEAT_AUTOCMD
1559 if (!(flags & BLN_DUMMY))
1560 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1561#endif
1562 }
1563 return buf;
1564 }
1565
1566 /*
1567 * If the current buffer has no name and no contents, use the current
1568 * buffer. Otherwise: Need to allocate a new buffer structure.
1569 *
1570 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001571 * (A spell file buffer is allocated in spell.c, but that's not a normal
1572 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 */
1574 buf = NULL;
1575 if ((flags & BLN_CURBUF)
1576 && curbuf != NULL
1577 && curbuf->b_ffname == NULL
1578 && curbuf->b_nwindows <= 1
1579 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1580 {
1581 buf = curbuf;
1582#ifdef FEAT_AUTOCMD
1583 /* It's like this buffer is deleted. Watch out for autocommands that
1584 * change curbuf! If that happens, allocate a new buffer anyway. */
1585 if (curbuf->b_p_bl)
1586 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1587 if (buf == curbuf)
1588 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1589# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001590 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001591 return NULL;
1592# endif
1593#endif
1594#ifdef FEAT_QUICKFIX
1595# ifdef FEAT_AUTOCMD
1596 if (buf == curbuf)
1597# endif
1598 {
1599 /* Make sure 'bufhidden' and 'buftype' are empty */
1600 clear_string_option(&buf->b_p_bh);
1601 clear_string_option(&buf->b_p_bt);
1602 }
1603#endif
1604 }
1605 if (buf != curbuf || curbuf == NULL)
1606 {
1607 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1608 if (buf == NULL)
1609 {
1610 vim_free(ffname);
1611 return NULL;
1612 }
1613 }
1614
1615 if (ffname != NULL)
1616 {
1617 buf->b_ffname = ffname;
1618 buf->b_sfname = vim_strsave(sfname);
1619 }
1620
1621 clear_wininfo(buf);
1622 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1623
1624 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1625 || buf->b_wininfo == NULL)
1626 {
1627 vim_free(buf->b_ffname);
1628 buf->b_ffname = NULL;
1629 vim_free(buf->b_sfname);
1630 buf->b_sfname = NULL;
1631 if (buf != curbuf)
1632 free_buffer(buf);
1633 return NULL;
1634 }
1635
1636 if (buf == curbuf)
1637 {
1638 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001639 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (buf != curbuf) /* autocommands deleted the buffer! */
1641 return NULL;
1642#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001643 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644 return NULL;
1645#endif
1646 /* buf->b_nwindows = 0; why was this here? */
1647 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1648#ifdef FEAT_KEYMAP
1649 /* need to reload lmaps and set b:keymap_name */
1650 curbuf->b_kmap_state |= KEYMAP_INIT;
1651#endif
1652 }
1653 else
1654 {
1655 /*
1656 * put new buffer at the end of the buffer list
1657 */
1658 buf->b_next = NULL;
1659 if (firstbuf == NULL) /* buffer list is empty */
1660 {
1661 buf->b_prev = NULL;
1662 firstbuf = buf;
1663 }
1664 else /* append new buffer at end of list */
1665 {
1666 lastbuf->b_next = buf;
1667 buf->b_prev = lastbuf;
1668 }
1669 lastbuf = buf;
1670
1671 buf->b_fnum = top_file_num++;
1672 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1673 {
1674 EMSG(_("W14: Warning: List of file names overflow"));
1675 if (emsg_silent == 0)
1676 {
1677 out_flush();
1678 ui_delay(3000L, TRUE); /* make sure it is noticed */
1679 }
1680 top_file_num = 1;
1681 }
1682
1683 /*
1684 * Always copy the options from the current buffer.
1685 */
1686 buf_copy_options(buf, BCO_ALWAYS);
1687 }
1688
1689 buf->b_wininfo->wi_fpos.lnum = lnum;
1690 buf->b_wininfo->wi_win = curwin;
1691
1692#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001693 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1694#endif
1695#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001696 hash_init(&buf->b_s.b_keywtab);
1697 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698#endif
1699
1700 buf->b_fname = buf->b_sfname;
1701#ifdef UNIX
1702 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001703 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 else
1705 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001706 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707 buf->b_dev = st.st_dev;
1708 buf->b_ino = st.st_ino;
1709 }
1710#endif
1711 buf->b_u_synced = TRUE;
1712 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001713 if (flags & BLN_DUMMY)
1714 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 buf_clear_file(buf);
1716 clrallmarks(buf); /* clear marks */
1717 fmarks_check_names(buf); /* check file marks for this file */
1718 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1719#ifdef FEAT_AUTOCMD
1720 if (!(flags & BLN_DUMMY))
1721 {
1722 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1723 if (flags & BLN_LISTED)
1724 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1725# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001726 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 return NULL;
1728# endif
1729 }
1730#endif
1731
1732 return buf;
1733}
1734
1735/*
1736 * Free the memory for the options of a buffer.
1737 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1738 * 'fileencoding'.
1739 */
1740 void
1741free_buf_options(buf, free_p_ff)
1742 buf_T *buf;
1743 int free_p_ff;
1744{
1745 if (free_p_ff)
1746 {
1747#ifdef FEAT_MBYTE
1748 clear_string_option(&buf->b_p_fenc);
1749#endif
1750 clear_string_option(&buf->b_p_ff);
1751#ifdef FEAT_QUICKFIX
1752 clear_string_option(&buf->b_p_bh);
1753 clear_string_option(&buf->b_p_bt);
1754#endif
1755 }
1756#ifdef FEAT_FIND_ID
1757 clear_string_option(&buf->b_p_def);
1758 clear_string_option(&buf->b_p_inc);
1759# ifdef FEAT_EVAL
1760 clear_string_option(&buf->b_p_inex);
1761# endif
1762#endif
1763#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1764 clear_string_option(&buf->b_p_inde);
1765 clear_string_option(&buf->b_p_indk);
1766#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001767#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1768 clear_string_option(&buf->b_p_bexpr);
1769#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001770#if defined(FEAT_CRYPT)
1771 clear_string_option(&buf->b_p_cm);
1772#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001773#if defined(FEAT_EVAL)
1774 clear_string_option(&buf->b_p_fex);
1775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#ifdef FEAT_CRYPT
1777 clear_string_option(&buf->b_p_key);
1778#endif
1779 clear_string_option(&buf->b_p_kp);
1780 clear_string_option(&buf->b_p_mps);
1781 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001782 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 clear_string_option(&buf->b_p_isk);
1784#ifdef FEAT_KEYMAP
1785 clear_string_option(&buf->b_p_keymap);
1786 ga_clear(&buf->b_kmap_ga);
1787#endif
1788#ifdef FEAT_COMMENTS
1789 clear_string_option(&buf->b_p_com);
1790#endif
1791#ifdef FEAT_FOLDING
1792 clear_string_option(&buf->b_p_cms);
1793#endif
1794 clear_string_option(&buf->b_p_nf);
1795#ifdef FEAT_SYN_HL
1796 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001797#endif
1798#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001799 clear_string_option(&buf->b_s.b_p_spc);
1800 clear_string_option(&buf->b_s.b_p_spf);
1801 vim_free(buf->b_s.b_cap_prog);
1802 buf->b_s.b_cap_prog = NULL;
1803 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804#endif
1805#ifdef FEAT_SEARCHPATH
1806 clear_string_option(&buf->b_p_sua);
1807#endif
1808#ifdef FEAT_AUTOCMD
1809 clear_string_option(&buf->b_p_ft);
1810#endif
1811#ifdef FEAT_OSFILETYPE
1812 clear_string_option(&buf->b_p_oft);
1813#endif
1814#ifdef FEAT_CINDENT
1815 clear_string_option(&buf->b_p_cink);
1816 clear_string_option(&buf->b_p_cino);
1817#endif
1818#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1819 clear_string_option(&buf->b_p_cinw);
1820#endif
1821#ifdef FEAT_INS_EXPAND
1822 clear_string_option(&buf->b_p_cpt);
1823#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001824#ifdef FEAT_COMPL_FUNC
1825 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001826 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#ifdef FEAT_QUICKFIX
1829 clear_string_option(&buf->b_p_gp);
1830 clear_string_option(&buf->b_p_mp);
1831 clear_string_option(&buf->b_p_efm);
1832#endif
1833 clear_string_option(&buf->b_p_ep);
1834 clear_string_option(&buf->b_p_path);
1835 clear_string_option(&buf->b_p_tags);
1836#ifdef FEAT_INS_EXPAND
1837 clear_string_option(&buf->b_p_dict);
1838 clear_string_option(&buf->b_p_tsr);
1839#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001840#ifdef FEAT_TEXTOBJ
1841 clear_string_option(&buf->b_p_qe);
1842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 buf->b_p_ar = -1;
1844}
1845
1846/*
1847 * get alternate file n
1848 * set linenr to lnum or altfpos.lnum if lnum == 0
1849 * also set cursor column to altfpos.col if 'startofline' is not set.
1850 * if (options & GETF_SETMARK) call setpcmark()
1851 * if (options & GETF_ALT) we are jumping to an alternate file.
1852 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1853 *
1854 * return FAIL for failure, OK for success
1855 */
1856 int
1857buflist_getfile(n, lnum, options, forceit)
1858 int n;
1859 linenr_T lnum;
1860 int options;
1861 int forceit;
1862{
1863 buf_T *buf;
1864#ifdef FEAT_WINDOWS
1865 win_T *wp = NULL;
1866#endif
1867 pos_T *fpos;
1868 colnr_T col;
1869
1870 buf = buflist_findnr(n);
1871 if (buf == NULL)
1872 {
1873 if ((options & GETF_ALT) && n == 0)
1874 EMSG(_(e_noalt));
1875 else
1876 EMSGN(_("E92: Buffer %ld not found"), n);
1877 return FAIL;
1878 }
1879
1880 /* if alternate file is the current buffer, nothing to do */
1881 if (buf == curbuf)
1882 return OK;
1883
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001884 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001885 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001886 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001888 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001889#ifdef FEAT_AUTOCMD
1890 if (curbuf_locked())
1891 return FAIL;
1892#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893
1894 /* altfpos may be changed by getfile(), get it now */
1895 if (lnum == 0)
1896 {
1897 fpos = buflist_findfpos(buf);
1898 lnum = fpos->lnum;
1899 col = fpos->col;
1900 }
1901 else
1902 col = 0;
1903
1904#ifdef FEAT_WINDOWS
1905 if (options & GETF_SWITCH)
1906 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001907 /* If 'switchbuf' contains "useopen": jump to first window containing
1908 * "buf" if one exists */
1909 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001911 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1912 * page containing "buf" if one exists */
1913 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001914 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001915 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1916 * isn't empty: open new window */
1917 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001919 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1920 tabpage_new();
1921 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001923 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 }
1925 }
1926#endif
1927
1928 ++RedrawingDisabled;
1929 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1930 lnum, forceit) <= 0)
1931 {
1932 --RedrawingDisabled;
1933
1934 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1935 if (!p_sol && col != 0)
1936 {
1937 curwin->w_cursor.col = col;
1938 check_cursor_col();
1939#ifdef FEAT_VIRTUALEDIT
1940 curwin->w_cursor.coladd = 0;
1941#endif
1942 curwin->w_set_curswant = TRUE;
1943 }
1944 return OK;
1945 }
1946 --RedrawingDisabled;
1947 return FAIL;
1948}
1949
1950/*
1951 * go to the last know line number for the current buffer
1952 */
1953 void
1954buflist_getfpos()
1955{
1956 pos_T *fpos;
1957
1958 fpos = buflist_findfpos(curbuf);
1959
1960 curwin->w_cursor.lnum = fpos->lnum;
1961 check_cursor_lnum();
1962
1963 if (p_sol)
1964 curwin->w_cursor.col = 0;
1965 else
1966 {
1967 curwin->w_cursor.col = fpos->col;
1968 check_cursor_col();
1969#ifdef FEAT_VIRTUALEDIT
1970 curwin->w_cursor.coladd = 0;
1971#endif
1972 curwin->w_set_curswant = TRUE;
1973 }
1974}
1975
Bram Moolenaar81695252004-12-29 20:58:21 +00001976#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1977/*
1978 * Find file in buffer list by name (it has to be for the current window).
1979 * Returns NULL if not found.
1980 */
1981 buf_T *
1982buflist_findname_exp(fname)
1983 char_u *fname;
1984{
1985 char_u *ffname;
1986 buf_T *buf = NULL;
1987
1988 /* First make the name into a full path name */
1989 ffname = FullName_save(fname,
1990#ifdef UNIX
1991 TRUE /* force expansion, get rid of symbolic links */
1992#else
1993 FALSE
1994#endif
1995 );
1996 if (ffname != NULL)
1997 {
1998 buf = buflist_findname(ffname);
1999 vim_free(ffname);
2000 }
2001 return buf;
2002}
2003#endif
2004
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005/*
2006 * Find file in buffer list by name (it has to be for the current window).
2007 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002008 * Skips dummy buffers.
2009 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 */
2011 buf_T *
2012buflist_findname(ffname)
2013 char_u *ffname;
2014{
2015#ifdef UNIX
2016 struct stat st;
2017
2018 if (mch_stat((char *)ffname, &st) < 0)
2019 st.st_dev = (dev_T)-1;
2020 return buflist_findname_stat(ffname, &st);
2021}
2022
2023/*
2024 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2025 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002026 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002027 */
2028 static buf_T *
2029buflist_findname_stat(ffname, stp)
2030 char_u *ffname;
2031 struct stat *stp;
2032{
2033#endif
2034 buf_T *buf;
2035
2036 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002037 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038#ifdef UNIX
2039 , stp
2040#endif
2041 ))
2042 return buf;
2043 return NULL;
2044}
2045
2046#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2047/*
2048 * Find file in buffer list by a regexp pattern.
2049 * Return fnum of the found buffer.
2050 * Return < 0 for error.
2051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 int
2053buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2054 char_u *pattern;
2055 char_u *pattern_end; /* pointer to first char after pattern */
2056 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002057 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058{
2059 buf_T *buf;
2060 regprog_T *prog;
2061 int match = -1;
2062 int find_listed;
2063 char_u *pat;
2064 char_u *patend;
2065 int attempt;
2066 char_u *p;
2067 int toggledollar;
2068
2069 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2070 {
2071 if (*pattern == '%')
2072 match = curbuf->b_fnum;
2073 else
2074 match = curwin->w_alt_fnum;
2075#ifdef FEAT_DIFF
2076 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2077 match = -1;
2078#endif
2079 }
2080
2081 /*
2082 * Try four ways of matching a listed buffer:
2083 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002084 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002085 * attempt == 2: with '$' at end (only match at end)
2086 * attempt == 3: with '^' at start and '$' at end (only full match)
2087 * Repeat this for finding an unlisted buffer if there was no matching
2088 * listed buffer.
2089 */
2090 else
2091 {
2092 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2093 if (pat == NULL)
2094 return -1;
2095 patend = pat + STRLEN(pat) - 1;
2096 toggledollar = (patend > pat && *patend == '$');
2097
2098 /* First try finding a listed buffer. If not found and "unlisted"
2099 * is TRUE, try finding an unlisted buffer. */
2100 find_listed = TRUE;
2101 for (;;)
2102 {
2103 for (attempt = 0; attempt <= 3; ++attempt)
2104 {
2105 /* may add '^' and '$' */
2106 if (toggledollar)
2107 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2108 p = pat;
2109 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2110 ++p;
2111 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2112 if (prog == NULL)
2113 {
2114 vim_free(pat);
2115 return -1;
2116 }
2117
2118 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2119 if (buf->b_p_bl == find_listed
2120#ifdef FEAT_DIFF
2121 && (!diffmode || diff_mode_buf(buf))
2122#endif
2123 && buflist_match(prog, buf) != NULL)
2124 {
2125 if (match >= 0) /* already found a match */
2126 {
2127 match = -2;
2128 break;
2129 }
2130 match = buf->b_fnum; /* remember first match */
2131 }
2132
2133 vim_free(prog);
2134 if (match >= 0) /* found one match */
2135 break;
2136 }
2137
2138 /* Only search for unlisted buffers if there was no match with
2139 * a listed buffer. */
2140 if (!unlisted || !find_listed || match != -1)
2141 break;
2142 find_listed = FALSE;
2143 }
2144
2145 vim_free(pat);
2146 }
2147
2148 if (match == -2)
2149 EMSG2(_("E93: More than one match for %s"), pattern);
2150 else if (match < 0)
2151 EMSG2(_("E94: No matching buffer for %s"), pattern);
2152 return match;
2153}
2154#endif
2155
2156#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2157
2158/*
2159 * Find all buffer names that match.
2160 * For command line expansion of ":buf" and ":sbuf".
2161 * Return OK if matches found, FAIL otherwise.
2162 */
2163 int
2164ExpandBufnames(pat, num_file, file, options)
2165 char_u *pat;
2166 int *num_file;
2167 char_u ***file;
2168 int options;
2169{
2170 int count = 0;
2171 buf_T *buf;
2172 int round;
2173 char_u *p;
2174 int attempt;
2175 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002176 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 *num_file = 0; /* return values in case of FAIL */
2179 *file = NULL;
2180
Bram Moolenaar05159a02005-02-26 23:04:13 +00002181 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2182 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002184 patc = alloc((unsigned)STRLEN(pat) + 11);
2185 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002187 STRCPY(patc, "\\(^\\|[\\/]\\)");
2188 STRCPY(patc + 11, pat + 1);
2189 }
2190 else
2191 patc = pat;
2192
2193 /*
2194 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002195 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002196 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002197 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002198 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002199 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002200 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002201 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002202 if (prog == NULL)
2203 {
2204 if (patc != pat)
2205 vim_free(patc);
2206 return FAIL;
2207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208
2209 /*
2210 * round == 1: Count the matches.
2211 * round == 2: Build the array to keep the matches.
2212 */
2213 for (round = 1; round <= 2; ++round)
2214 {
2215 count = 0;
2216 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2217 {
2218 if (!buf->b_p_bl) /* skip unlisted buffers */
2219 continue;
2220 p = buflist_match(prog, buf);
2221 if (p != NULL)
2222 {
2223 if (round == 1)
2224 ++count;
2225 else
2226 {
2227 if (options & WILD_HOME_REPLACE)
2228 p = home_replace_save(buf, p);
2229 else
2230 p = vim_strsave(p);
2231 (*file)[count++] = p;
2232 }
2233 }
2234 }
2235 if (count == 0) /* no match found, break here */
2236 break;
2237 if (round == 1)
2238 {
2239 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2240 if (*file == NULL)
2241 {
2242 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002243 if (patc != pat)
2244 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 return FAIL;
2246 }
2247 }
2248 }
2249 vim_free(prog);
2250 if (count) /* match(es) found, break here */
2251 break;
2252 }
2253
Bram Moolenaar05159a02005-02-26 23:04:13 +00002254 if (patc != pat)
2255 vim_free(patc);
2256
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 *num_file = count;
2258 return (count == 0 ? FAIL : OK);
2259}
2260
2261#endif /* FEAT_CMDL_COMPL */
2262
2263#ifdef HAVE_BUFLIST_MATCH
2264/*
2265 * Check for a match on the file name for buffer "buf" with regprog "prog".
2266 */
2267 static char_u *
2268buflist_match(prog, buf)
2269 regprog_T *prog;
2270 buf_T *buf;
2271{
2272 char_u *match;
2273
2274 /* First try the short file name, then the long file name. */
2275 match = fname_match(prog, buf->b_sfname);
2276 if (match == NULL)
2277 match = fname_match(prog, buf->b_ffname);
2278
2279 return match;
2280}
2281
2282/*
2283 * Try matching the regexp in "prog" with file name "name".
2284 * Return "name" when there is a match, NULL when not.
2285 */
2286 static char_u *
2287fname_match(prog, name)
2288 regprog_T *prog;
2289 char_u *name;
2290{
2291 char_u *match = NULL;
2292 char_u *p;
2293 regmatch_T regmatch;
2294
2295 if (name != NULL)
2296 {
2297 regmatch.regprog = prog;
2298#ifdef CASE_INSENSITIVE_FILENAME
2299 regmatch.rm_ic = TRUE; /* Always ignore case */
2300#else
2301 regmatch.rm_ic = FALSE; /* Never ignore case */
2302#endif
2303
2304 if (vim_regexec(&regmatch, name, (colnr_T)0))
2305 match = name;
2306 else
2307 {
2308 /* Replace $(HOME) with '~' and try matching again. */
2309 p = home_replace_save(NULL, name);
2310 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2311 match = name;
2312 vim_free(p);
2313 }
2314 }
2315
2316 return match;
2317}
2318#endif
2319
2320/*
2321 * find file in buffer list by number
2322 */
2323 buf_T *
2324buflist_findnr(nr)
2325 int nr;
2326{
2327 buf_T *buf;
2328
2329 if (nr == 0)
2330 nr = curwin->w_alt_fnum;
2331 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2332 if (buf->b_fnum == nr)
2333 return (buf);
2334 return NULL;
2335}
2336
2337/*
2338 * Get name of file 'n' in the buffer list.
2339 * When the file has no name an empty string is returned.
2340 * home_replace() is used to shorten the file name (used for marks).
2341 * Returns a pointer to allocated memory, of NULL when failed.
2342 */
2343 char_u *
2344buflist_nr2name(n, fullname, helptail)
2345 int n;
2346 int fullname;
2347 int helptail; /* for help buffers return tail only */
2348{
2349 buf_T *buf;
2350
2351 buf = buflist_findnr(n);
2352 if (buf == NULL)
2353 return NULL;
2354 return home_replace_save(helptail ? buf : NULL,
2355 fullname ? buf->b_ffname : buf->b_fname);
2356}
2357
2358/*
2359 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2360 * When "copy_options" is TRUE save the local window option values.
2361 * When "lnum" is 0 only do the options.
2362 */
2363 static void
2364buflist_setfpos(buf, win, lnum, col, copy_options)
2365 buf_T *buf;
2366 win_T *win;
2367 linenr_T lnum;
2368 colnr_T col;
2369 int copy_options;
2370{
2371 wininfo_T *wip;
2372
2373 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2374 if (wip->wi_win == win)
2375 break;
2376 if (wip == NULL)
2377 {
2378 /* allocate a new entry */
2379 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2380 if (wip == NULL)
2381 return;
2382 wip->wi_win = win;
2383 if (lnum == 0) /* set lnum even when it's 0 */
2384 lnum = 1;
2385 }
2386 else
2387 {
2388 /* remove the entry from the list */
2389 if (wip->wi_prev)
2390 wip->wi_prev->wi_next = wip->wi_next;
2391 else
2392 buf->b_wininfo = wip->wi_next;
2393 if (wip->wi_next)
2394 wip->wi_next->wi_prev = wip->wi_prev;
2395 if (copy_options && wip->wi_optset)
2396 {
2397 clear_winopt(&wip->wi_opt);
2398#ifdef FEAT_FOLDING
2399 deleteFoldRecurse(&wip->wi_folds);
2400#endif
2401 }
2402 }
2403 if (lnum != 0)
2404 {
2405 wip->wi_fpos.lnum = lnum;
2406 wip->wi_fpos.col = col;
2407 }
2408 if (copy_options)
2409 {
2410 /* Save the window-specific option values. */
2411 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2412#ifdef FEAT_FOLDING
2413 wip->wi_fold_manual = win->w_fold_manual;
2414 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2415#endif
2416 wip->wi_optset = TRUE;
2417 }
2418
2419 /* insert the entry in front of the list */
2420 wip->wi_next = buf->b_wininfo;
2421 buf->b_wininfo = wip;
2422 wip->wi_prev = NULL;
2423 if (wip->wi_next)
2424 wip->wi_next->wi_prev = wip;
2425
2426 return;
2427}
2428
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002429#ifdef FEAT_DIFF
2430static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2431
2432/*
2433 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2434 * page. That's because a diff is local to a tab page.
2435 */
2436 static int
2437wininfo_other_tab_diff(wip)
2438 wininfo_T *wip;
2439{
2440 win_T *wp;
2441
2442 if (wip->wi_opt.wo_diff)
2443 {
2444 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2445 /* return FALSE when it's a window in the current tab page, thus
2446 * the buffer was in diff mode here */
2447 if (wip->wi_win == wp)
2448 return FALSE;
2449 return TRUE;
2450 }
2451 return FALSE;
2452}
2453#endif
2454
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455/*
2456 * Find info for the current window in buffer "buf".
2457 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002458 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2459 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 * Returns NULL when there isn't any info.
2461 */
2462 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002463find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002464 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002465 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
2467 wininfo_T *wip;
2468
2469 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002470 if (wip->wi_win == curwin
2471#ifdef FEAT_DIFF
2472 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2473#endif
2474 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002476
2477 /* If no wininfo for curwin, use the first in the list (that doesn't have
2478 * 'diff' set and is in another tab page). */
2479 if (wip == NULL)
2480 {
2481#ifdef FEAT_DIFF
2482 if (skip_diff_buffer)
2483 {
2484 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2485 if (!wininfo_other_tab_diff(wip))
2486 break;
2487 }
2488 else
2489#endif
2490 wip = buf->b_wininfo;
2491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 return wip;
2493}
2494
2495/*
2496 * Reset the local window options to the values last used in this window.
2497 * If the buffer wasn't used in this window before, use the values from
2498 * the most recently used window. If the values were never set, use the
2499 * global values for the window.
2500 */
2501 void
2502get_winopts(buf)
2503 buf_T *buf;
2504{
2505 wininfo_T *wip;
2506
2507 clear_winopt(&curwin->w_onebuf_opt);
2508#ifdef FEAT_FOLDING
2509 clearFolding(curwin);
2510#endif
2511
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002512 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (wip != NULL && wip->wi_optset)
2514 {
2515 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2516#ifdef FEAT_FOLDING
2517 curwin->w_fold_manual = wip->wi_fold_manual;
2518 curwin->w_foldinvalid = TRUE;
2519 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2520#endif
2521 }
2522 else
2523 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2524
2525#ifdef FEAT_FOLDING
2526 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2527 if (p_fdls >= 0)
2528 curwin->w_p_fdl = p_fdls;
2529#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002530#ifdef FEAT_SYN_HL
2531 check_colorcolumn(curwin);
2532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533}
2534
2535/*
2536 * Find the position (lnum and col) for the buffer 'buf' for the current
2537 * window.
2538 * Returns a pointer to no_position if no position is found.
2539 */
2540 pos_T *
2541buflist_findfpos(buf)
2542 buf_T *buf;
2543{
2544 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002545 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002547 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (wip != NULL)
2549 return &(wip->wi_fpos);
2550 else
2551 return &no_position;
2552}
2553
2554/*
2555 * Find the lnum for the buffer 'buf' for the current window.
2556 */
2557 linenr_T
2558buflist_findlnum(buf)
2559 buf_T *buf;
2560{
2561 return buflist_findfpos(buf)->lnum;
2562}
2563
2564#if defined(FEAT_LISTCMDS) || defined(PROTO)
2565/*
2566 * List all know file names (for :files and :buffers command).
2567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 void
2569buflist_list(eap)
2570 exarg_T *eap;
2571{
2572 buf_T *buf;
2573 int len;
2574 int i;
2575
2576 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2577 {
2578 /* skip unlisted buffers, unless ! was used */
2579 if (!buf->b_p_bl && !eap->forceit)
2580 continue;
2581 msg_putchar('\n');
2582 if (buf_spname(buf) != NULL)
2583 STRCPY(NameBuff, buf_spname(buf));
2584 else
2585 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2586
Bram Moolenaar50cde822005-06-05 21:54:54 +00002587 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 buf->b_fnum,
2589 buf->b_p_bl ? ' ' : 'u',
2590 buf == curbuf ? '%' :
2591 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2592 buf->b_ml.ml_mfp == NULL ? ' ' :
2593 (buf->b_nwindows == 0 ? 'h' : 'a'),
2594 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2595 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002596 : (bufIsChanged(buf) ? '+' : ' '),
2597 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598
2599 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 i = 40 - vim_strsize(IObuff);
2601 do
2602 {
2603 IObuff[len++] = ' ';
2604 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002605 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2606 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002607 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 msg_outtrans(IObuff);
2609 out_flush(); /* output one line at a time */
2610 ui_breakcheck();
2611 }
2612}
2613#endif
2614
2615/*
2616 * Get file name and line number for file 'fnum'.
2617 * Used by DoOneCmd() for translating '%' and '#'.
2618 * Used by insert_reg() and cmdline_paste() for '#' register.
2619 * Return FAIL if not found, OK for success.
2620 */
2621 int
2622buflist_name_nr(fnum, fname, lnum)
2623 int fnum;
2624 char_u **fname;
2625 linenr_T *lnum;
2626{
2627 buf_T *buf;
2628
2629 buf = buflist_findnr(fnum);
2630 if (buf == NULL || buf->b_fname == NULL)
2631 return FAIL;
2632
2633 *fname = buf->b_fname;
2634 *lnum = buflist_findlnum(buf);
2635
2636 return OK;
2637}
2638
2639/*
2640 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2641 * The file name with the full path is also remembered, for when :cd is used.
2642 * Returns FAIL for failure (file name already in use by other buffer)
2643 * OK otherwise.
2644 */
2645 int
2646setfname(buf, ffname, sfname, message)
2647 buf_T *buf;
2648 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002649 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002650{
Bram Moolenaar81695252004-12-29 20:58:21 +00002651 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652#ifdef UNIX
2653 struct stat st;
2654#endif
2655
2656 if (ffname == NULL || *ffname == NUL)
2657 {
2658 /* Removing the name. */
2659 vim_free(buf->b_ffname);
2660 vim_free(buf->b_sfname);
2661 buf->b_ffname = NULL;
2662 buf->b_sfname = NULL;
2663#ifdef UNIX
2664 st.st_dev = (dev_T)-1;
2665#endif
2666 }
2667 else
2668 {
2669 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2670 if (ffname == NULL) /* out of memory */
2671 return FAIL;
2672
2673 /*
2674 * if the file name is already used in another buffer:
2675 * - if the buffer is loaded, fail
2676 * - if the buffer is not loaded, delete it from the list
2677 */
2678#ifdef UNIX
2679 if (mch_stat((char *)ffname, &st) < 0)
2680 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002681#endif
2682 if (!(buf->b_flags & BF_DUMMY))
2683#ifdef UNIX
2684 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002686 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687#endif
2688 if (obuf != NULL && obuf != buf)
2689 {
2690 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2691 {
2692 if (message)
2693 EMSG(_("E95: Buffer with this name already exists"));
2694 vim_free(ffname);
2695 return FAIL;
2696 }
2697 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2698 }
2699 sfname = vim_strsave(sfname);
2700 if (ffname == NULL || sfname == NULL)
2701 {
2702 vim_free(sfname);
2703 vim_free(ffname);
2704 return FAIL;
2705 }
2706#ifdef USE_FNAME_CASE
2707# ifdef USE_LONG_FNAME
2708 if (USE_LONG_FNAME)
2709# endif
2710 fname_case(sfname, 0); /* set correct case for short file name */
2711#endif
2712 vim_free(buf->b_ffname);
2713 vim_free(buf->b_sfname);
2714 buf->b_ffname = ffname;
2715 buf->b_sfname = sfname;
2716 }
2717 buf->b_fname = buf->b_sfname;
2718#ifdef UNIX
2719 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002720 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 else
2722 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002723 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 buf->b_dev = st.st_dev;
2725 buf->b_ino = st.st_ino;
2726 }
2727#endif
2728
2729#ifndef SHORT_FNAME
2730 buf->b_shortname = FALSE;
2731#endif
2732
2733 buf_name_changed(buf);
2734 return OK;
2735}
2736
2737/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002738 * Crude way of changing the name of a buffer. Use with care!
2739 * The name should be relative to the current directory.
2740 */
2741 void
2742buf_set_name(fnum, name)
2743 int fnum;
2744 char_u *name;
2745{
2746 buf_T *buf;
2747
2748 buf = buflist_findnr(fnum);
2749 if (buf != NULL)
2750 {
2751 vim_free(buf->b_sfname);
2752 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002753 buf->b_ffname = vim_strsave(name);
2754 buf->b_sfname = NULL;
2755 /* Allocate ffname and expand into full path. Also resolves .lnk
2756 * files on Win32. */
2757 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002758 buf->b_fname = buf->b_sfname;
2759 }
2760}
2761
2762/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 * Take care of what needs to be done when the name of buffer "buf" has
2764 * changed.
2765 */
2766 void
2767buf_name_changed(buf)
2768 buf_T *buf;
2769{
2770 /*
2771 * If the file name changed, also change the name of the swapfile
2772 */
2773 if (buf->b_ml.ml_mfp != NULL)
2774 ml_setname(buf);
2775
2776 if (curwin->w_buffer == buf)
2777 check_arg_idx(curwin); /* check file name for arg list */
2778#ifdef FEAT_TITLE
2779 maketitle(); /* set window title */
2780#endif
2781#ifdef FEAT_WINDOWS
2782 status_redraw_all(); /* status lines need to be redrawn */
2783#endif
2784 fmarks_check_names(buf); /* check named file marks */
2785 ml_timestamp(buf); /* reset timestamp */
2786}
2787
2788/*
2789 * set alternate file name for current window
2790 *
2791 * Used by do_one_cmd(), do_write() and do_ecmd().
2792 * Return the buffer.
2793 */
2794 buf_T *
2795setaltfname(ffname, sfname, lnum)
2796 char_u *ffname;
2797 char_u *sfname;
2798 linenr_T lnum;
2799{
2800 buf_T *buf;
2801
2802 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2803 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002804 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002805 curwin->w_alt_fnum = buf->b_fnum;
2806 return buf;
2807}
2808
2809/*
2810 * Get alternate file name for current window.
2811 * Return NULL if there isn't any, and give error message if requested.
2812 */
2813 char_u *
2814getaltfname(errmsg)
2815 int errmsg; /* give error message */
2816{
2817 char_u *fname;
2818 linenr_T dummy;
2819
2820 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2821 {
2822 if (errmsg)
2823 EMSG(_(e_noalt));
2824 return NULL;
2825 }
2826 return fname;
2827}
2828
2829/*
2830 * Add a file name to the buflist and return its number.
2831 * Uses same flags as buflist_new(), except BLN_DUMMY.
2832 *
2833 * used by qf_init(), main() and doarglist()
2834 */
2835 int
2836buflist_add(fname, flags)
2837 char_u *fname;
2838 int flags;
2839{
2840 buf_T *buf;
2841
2842 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2843 if (buf != NULL)
2844 return buf->b_fnum;
2845 return 0;
2846}
2847
2848#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2849/*
2850 * Adjust slashes in file names. Called after 'shellslash' was set.
2851 */
2852 void
2853buflist_slash_adjust()
2854{
2855 buf_T *bp;
2856
2857 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2858 {
2859 if (bp->b_ffname != NULL)
2860 slash_adjust(bp->b_ffname);
2861 if (bp->b_sfname != NULL)
2862 slash_adjust(bp->b_sfname);
2863 }
2864}
2865#endif
2866
2867/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002868 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002869 * Also save the local window option values.
2870 */
2871 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002872buflist_altfpos(win)
2873 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002875 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876}
2877
2878/*
2879 * Return TRUE if 'ffname' is not the same file as current file.
2880 * Fname must have a full path (expanded by mch_FullName()).
2881 */
2882 int
2883otherfile(ffname)
2884 char_u *ffname;
2885{
2886 return otherfile_buf(curbuf, ffname
2887#ifdef UNIX
2888 , NULL
2889#endif
2890 );
2891}
2892
2893 static int
2894otherfile_buf(buf, ffname
2895#ifdef UNIX
2896 , stp
2897#endif
2898 )
2899 buf_T *buf;
2900 char_u *ffname;
2901#ifdef UNIX
2902 struct stat *stp;
2903#endif
2904{
2905 /* no name is different */
2906 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2907 return TRUE;
2908 if (fnamecmp(ffname, buf->b_ffname) == 0)
2909 return FALSE;
2910#ifdef UNIX
2911 {
2912 struct stat st;
2913
2914 /* If no struct stat given, get it now */
2915 if (stp == NULL)
2916 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002917 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 st.st_dev = (dev_T)-1;
2919 stp = &st;
2920 }
2921 /* Use dev/ino to check if the files are the same, even when the names
2922 * are different (possible with links). Still need to compare the
2923 * name above, for when the file doesn't exist yet.
2924 * Problem: The dev/ino changes when a file is deleted (and created
2925 * again) and remains the same when renamed/moved. We don't want to
2926 * mch_stat() each buffer each time, that would be too slow. Get the
2927 * dev/ino again when they appear to match, but not when they appear
2928 * to be different: Could skip a buffer when it's actually the same
2929 * file. */
2930 if (buf_same_ino(buf, stp))
2931 {
2932 buf_setino(buf);
2933 if (buf_same_ino(buf, stp))
2934 return FALSE;
2935 }
2936 }
2937#endif
2938 return TRUE;
2939}
2940
2941#if defined(UNIX) || defined(PROTO)
2942/*
2943 * Set inode and device number for a buffer.
2944 * Must always be called when b_fname is changed!.
2945 */
2946 void
2947buf_setino(buf)
2948 buf_T *buf;
2949{
2950 struct stat st;
2951
2952 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2953 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002954 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 buf->b_dev = st.st_dev;
2956 buf->b_ino = st.st_ino;
2957 }
2958 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002959 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960}
2961
2962/*
2963 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2964 */
2965 static int
2966buf_same_ino(buf, stp)
2967 buf_T *buf;
2968 struct stat *stp;
2969{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002970 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002971 && stp->st_dev == buf->b_dev
2972 && stp->st_ino == buf->b_ino);
2973}
2974#endif
2975
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002976/*
2977 * Print info about the current buffer.
2978 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 void
2980fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002981 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 int shorthelp;
2983 int dont_truncate;
2984{
2985 char_u *name;
2986 int n;
2987 char_u *p;
2988 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002989 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990
2991 buffer = alloc(IOSIZE);
2992 if (buffer == NULL)
2993 return;
2994
2995 if (fullname > 1) /* 2 CTRL-G: include buffer number */
2996 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002997 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 p = buffer + STRLEN(buffer);
2999 }
3000 else
3001 p = buffer;
3002
3003 *p++ = '"';
3004 if (buf_spname(curbuf) != NULL)
3005 STRCPY(p, buf_spname(curbuf));
3006 else
3007 {
3008 if (!fullname && curbuf->b_fname != NULL)
3009 name = curbuf->b_fname;
3010 else
3011 name = curbuf->b_ffname;
3012 home_replace(shorthelp ? curbuf : NULL, name, p,
3013 (int)(IOSIZE - (p - buffer)), TRUE);
3014 }
3015
Bram Moolenaara800b422010-06-27 01:15:55 +02003016 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 curbufIsChanged() ? (shortmess(SHM_MOD)
3018 ? " [+]" : _(" [Modified]")) : " ",
3019 (curbuf->b_flags & BF_NOTEDITED)
3020#ifdef FEAT_QUICKFIX
3021 && !bt_dontwrite(curbuf)
3022#endif
3023 ? _("[Not edited]") : "",
3024 (curbuf->b_flags & BF_NEW)
3025#ifdef FEAT_QUICKFIX
3026 && !bt_dontwrite(curbuf)
3027#endif
3028 ? _("[New file]") : "",
3029 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3030 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3031 : _("[readonly]")) : "",
3032 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3033 || curbuf->b_p_ro) ?
3034 " " : "");
3035 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3036 * causes an overflow, thus for large numbers divide instead. */
3037 if (curwin->w_cursor.lnum > 1000000L)
3038 n = (int)(((long)curwin->w_cursor.lnum) /
3039 ((long)curbuf->b_ml.ml_line_count / 100L));
3040 else
3041 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3042 (long)curbuf->b_ml.ml_line_count);
3043 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3044 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003045 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 }
3047#ifdef FEAT_CMDL_INFO
3048 else if (p_ru)
3049 {
3050 /* Current line and column are already on the screen -- webb */
3051 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003052 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003054 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 (long)curbuf->b_ml.ml_line_count, n);
3056 }
3057#endif
3058 else
3059 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003060 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 _("line %ld of %ld --%d%%-- col "),
3062 (long)curwin->w_cursor.lnum,
3063 (long)curbuf->b_ml.ml_line_count,
3064 n);
3065 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003066 len = STRLEN(buffer);
3067 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3069 }
3070
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003071 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072
3073 if (dont_truncate)
3074 {
3075 /* Temporarily set msg_scroll to avoid the message being truncated.
3076 * First call msg_start() to get the message in the right place. */
3077 msg_start();
3078 n = msg_scroll;
3079 msg_scroll = TRUE;
3080 msg(buffer);
3081 msg_scroll = n;
3082 }
3083 else
3084 {
3085 p = msg_trunc_attr(buffer, FALSE, 0);
3086 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 /* Need to repeat the message after redrawing when:
3088 * - When restart_edit is set (otherwise there will be a delay
3089 * before redrawing).
3090 * - When the screen was scrolled but there is no wait-return
3091 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003092 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 }
3094
3095 vim_free(buffer);
3096}
3097
3098 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003099col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003101 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 int col;
3103 int vcol;
3104{
3105 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003106 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003108 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109}
3110
3111#if defined(FEAT_TITLE) || defined(PROTO)
3112/*
3113 * put file name in title bar of window and in icon title
3114 */
3115
3116static char_u *lasttitle = NULL;
3117static char_u *lasticon = NULL;
3118
3119 void
3120maketitle()
3121{
3122 char_u *p;
3123 char_u *t_str = NULL;
3124 char_u *i_name;
3125 char_u *i_str = NULL;
3126 int maxlen = 0;
3127 int len;
3128 int mustset;
3129 char_u buf[IOSIZE];
3130 int off;
3131
3132 if (!redrawing())
3133 {
3134 /* Postpone updating the title when 'lazyredraw' is set. */
3135 need_maketitle = TRUE;
3136 return;
3137 }
3138
3139 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003140 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 return;
3142
3143 if (p_title)
3144 {
3145 if (p_titlelen > 0)
3146 {
3147 maxlen = p_titlelen * Columns / 100;
3148 if (maxlen < 10)
3149 maxlen = 10;
3150 }
3151
3152 t_str = buf;
3153 if (*p_titlestring != NUL)
3154 {
3155#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003156 if (stl_syntax & STL_IN_TITLE)
3157 {
3158 int use_sandbox = FALSE;
3159 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003160
3161# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003162 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003163# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003164 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003166 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003167 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003168 if (called_emsg)
3169 set_string_option_direct((char_u *)"titlestring", -1,
3170 (char_u *)"", OPT_FREE, SID_ERROR);
3171 called_emsg |= save_called_emsg;
3172 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 else
3174#endif
3175 t_str = p_titlestring;
3176 }
3177 else
3178 {
3179 /* format: "fname + (path) (1 of 2) - VIM" */
3180
3181 if (curbuf->b_fname == NULL)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003182 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 else
3184 {
3185 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003186 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 }
3189
3190 switch (bufIsChanged(curbuf)
3191 + (curbuf->b_p_ro * 2)
3192 + (!curbuf->b_p_ma * 4))
3193 {
3194 case 1: STRCAT(buf, " +"); break;
3195 case 2: STRCAT(buf, " ="); break;
3196 case 3: STRCAT(buf, " =+"); break;
3197 case 4:
3198 case 6: STRCAT(buf, " -"); break;
3199 case 5:
3200 case 7: STRCAT(buf, " -+"); break;
3201 }
3202
3203 if (curbuf->b_fname != NULL)
3204 {
3205 /* Get path of file, replace home dir with ~ */
3206 off = (int)STRLEN(buf);
3207 buf[off++] = ' ';
3208 buf[off++] = '(';
3209 home_replace(curbuf, curbuf->b_ffname,
3210 buf + off, IOSIZE - off, TRUE);
3211#ifdef BACKSLASH_IN_FILENAME
3212 /* avoid "c:/name" to be reduced to "c" */
3213 if (isalpha(buf[off]) && buf[off + 1] == ':')
3214 off += 2;
3215#endif
3216 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003217 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003218 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003220 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003221 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003224
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 /* translate unprintable chars */
3226 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003227 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 STRCAT(buf, ")");
3230 }
3231
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003232 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233
3234#if defined(FEAT_CLIENTSERVER)
3235 if (serverName != NULL)
3236 {
3237 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003238 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 }
3240 else
3241#endif
3242 STRCAT(buf, " - VIM");
3243
3244 if (maxlen > 0)
3245 {
3246 /* make it shorter by removing a bit in the middle */
3247 len = vim_strsize(buf);
3248 if (len > maxlen)
3249 trunc_string(buf, buf, maxlen);
3250 }
3251 }
3252 }
3253 mustset = ti_change(t_str, &lasttitle);
3254
3255 if (p_icon)
3256 {
3257 i_str = buf;
3258 if (*p_iconstring != NUL)
3259 {
3260#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003261 if (stl_syntax & STL_IN_ICON)
3262 {
3263 int use_sandbox = FALSE;
3264 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003265
3266# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003267 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003268# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003269 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003271 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003272 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003273 if (called_emsg)
3274 set_string_option_direct((char_u *)"iconstring", -1,
3275 (char_u *)"", OPT_FREE, SID_ERROR);
3276 called_emsg |= save_called_emsg;
3277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 else
3279#endif
3280 i_str = p_iconstring;
3281 }
3282 else
3283 {
3284 if (buf_spname(curbuf) != NULL)
3285 i_name = (char_u *)buf_spname(curbuf);
3286 else /* use file name only in icon */
3287 i_name = gettail(curbuf->b_ffname);
3288 *i_str = NUL;
3289 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003290 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 if (len > 100)
3292 {
3293 len -= 100;
3294#ifdef FEAT_MBYTE
3295 if (has_mbyte)
3296 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3297#endif
3298 i_name += len;
3299 }
3300 STRCPY(i_str, i_name);
3301 trans_characters(i_str, IOSIZE);
3302 }
3303 }
3304
3305 mustset |= ti_change(i_str, &lasticon);
3306
3307 if (mustset)
3308 resettitle();
3309}
3310
3311/*
3312 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3313 * from "str" if it does.
3314 * Return TRUE when "*last" changed.
3315 */
3316 static int
3317ti_change(str, last)
3318 char_u *str;
3319 char_u **last;
3320{
3321 if ((str == NULL) != (*last == NULL)
3322 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3323 {
3324 vim_free(*last);
3325 if (str == NULL)
3326 *last = NULL;
3327 else
3328 *last = vim_strsave(str);
3329 return TRUE;
3330 }
3331 return FALSE;
3332}
3333
3334/*
3335 * Put current window title back (used after calling a shell)
3336 */
3337 void
3338resettitle()
3339{
3340 mch_settitle(lasttitle, lasticon);
3341}
Bram Moolenaarea408852005-06-25 22:49:46 +00003342
3343# if defined(EXITFREE) || defined(PROTO)
3344 void
3345free_titles()
3346{
3347 vim_free(lasttitle);
3348 vim_free(lasticon);
3349}
3350# endif
3351
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352#endif /* FEAT_TITLE */
3353
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003354#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003356 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 * Return length of string in screen cells.
3358 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003359 * Normally works for window "wp", except when working for 'tabline' then it
3360 * is "curwin".
3361 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 * Items are drawn interspersed with the text that surrounds it
3363 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3364 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3365 *
3366 * If maxwidth is not zero, the string will be filled at any middle marker
3367 * or truncated if too long, fillchar is used for all whitespace.
3368 */
3369 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003370build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3371 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003373 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374 size_t outlen; /* length of out[] */
3375 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003376 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 int fillchar;
3378 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003379 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3380 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381{
3382 char_u *p;
3383 char_u *s;
3384 char_u *t;
3385 char_u *linecont;
3386#ifdef FEAT_EVAL
3387 win_T *o_curwin;
3388 buf_T *o_curbuf;
3389#endif
3390 int empty_line;
3391 colnr_T virtcol;
3392 long l;
3393 long n;
3394 int prevchar_isflag;
3395 int prevchar_isitem;
3396 int itemisflag;
3397 int fillable;
3398 char_u *str;
3399 long num;
3400 int width;
3401 int itemcnt;
3402 int curitem;
3403 int groupitem[STL_MAX_ITEM];
3404 int groupdepth;
3405 struct stl_item
3406 {
3407 char_u *start;
3408 int minwid;
3409 int maxwid;
3410 enum
3411 {
3412 Normal,
3413 Empty,
3414 Group,
3415 Middle,
3416 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003417 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003418 Trunc
3419 } type;
3420 } item[STL_MAX_ITEM];
3421 int minwid;
3422 int maxwid;
3423 int zeropad;
3424 char_u base;
3425 char_u opt;
3426#define TMPLEN 70
3427 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003428 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003429 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003430
3431#ifdef FEAT_EVAL
3432 /*
3433 * When the format starts with "%!" then evaluate it as an expression and
3434 * use the result as the actual format string.
3435 */
3436 if (fmt[0] == '%' && fmt[1] == '!')
3437 {
3438 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3439 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003440 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003441 }
3442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003443
3444 if (fillchar == 0)
3445 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003446#ifdef FEAT_MBYTE
3447 /* Can't handle a multi-byte fill character yet. */
3448 else if (mb_char2len(fillchar) > 1)
3449 fillchar = '-';
3450#endif
3451
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 /*
3453 * Get line & check if empty (cursorpos will show "0-1").
3454 * If inversion is possible we use it. Else '=' characters are used.
3455 */
3456 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3457 empty_line = (*linecont == NUL);
3458
3459 groupdepth = 0;
3460 p = out;
3461 curitem = 0;
3462 prevchar_isflag = TRUE;
3463 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003464 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003466 if (curitem == STL_MAX_ITEM)
3467 {
3468 /* There are too many items. Add the error code to the statusline
3469 * to give the user a hint about what went wrong. */
3470 if (p + 6 < out + outlen)
3471 {
3472 mch_memmove(p, " E541", (size_t)5);
3473 p += 5;
3474 }
3475 break;
3476 }
3477
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (*s != NUL && *s != '%')
3479 prevchar_isflag = prevchar_isitem = FALSE;
3480
3481 /*
3482 * Handle up to the next '%' or the end.
3483 */
3484 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3485 *p++ = *s++;
3486 if (*s == NUL || p + 1 >= out + outlen)
3487 break;
3488
3489 /*
3490 * Handle one '%' item.
3491 */
3492 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003493 if (*s == NUL) /* ignore trailing % */
3494 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 if (*s == '%')
3496 {
3497 if (p + 1 >= out + outlen)
3498 break;
3499 *p++ = *s++;
3500 prevchar_isflag = prevchar_isitem = FALSE;
3501 continue;
3502 }
3503 if (*s == STL_MIDDLEMARK)
3504 {
3505 s++;
3506 if (groupdepth > 0)
3507 continue;
3508 item[curitem].type = Middle;
3509 item[curitem++].start = p;
3510 continue;
3511 }
3512 if (*s == STL_TRUNCMARK)
3513 {
3514 s++;
3515 item[curitem].type = Trunc;
3516 item[curitem++].start = p;
3517 continue;
3518 }
3519 if (*s == ')')
3520 {
3521 s++;
3522 if (groupdepth < 1)
3523 continue;
3524 groupdepth--;
3525
3526 t = item[groupitem[groupdepth]].start;
3527 *p = NUL;
3528 l = vim_strsize(t);
3529 if (curitem > groupitem[groupdepth] + 1
3530 && item[groupitem[groupdepth]].minwid == 0)
3531 {
3532 /* remove group if all items are empty */
3533 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3534 if (item[n].type == Normal)
3535 break;
3536 if (n == curitem)
3537 {
3538 p = t;
3539 l = 0;
3540 }
3541 }
3542 if (l > item[groupitem[groupdepth]].maxwid)
3543 {
3544 /* truncate, remove n bytes of text at the start */
3545#ifdef FEAT_MBYTE
3546 if (has_mbyte)
3547 {
3548 /* Find the first character that should be included. */
3549 n = 0;
3550 while (l >= item[groupitem[groupdepth]].maxwid)
3551 {
3552 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003553 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 }
3555 }
3556 else
3557#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003558 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003559
3560 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003561 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 p = p - n + 1;
3563#ifdef FEAT_MBYTE
3564 /* Fill up space left over by half a double-wide char. */
3565 while (++l < item[groupitem[groupdepth]].minwid)
3566 *p++ = fillchar;
3567#endif
3568
3569 /* correct the start of the items for the truncation */
3570 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3571 {
3572 item[l].start -= n;
3573 if (item[l].start < t)
3574 item[l].start = t;
3575 }
3576 }
3577 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3578 {
3579 /* fill */
3580 n = item[groupitem[groupdepth]].minwid;
3581 if (n < 0)
3582 {
3583 /* fill by appending characters */
3584 n = 0 - n;
3585 while (l++ < n && p + 1 < out + outlen)
3586 *p++ = fillchar;
3587 }
3588 else
3589 {
3590 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003591 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 l = n - l;
3593 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003594 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595 p += l;
3596 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3597 item[n].start += l;
3598 for ( ; l > 0; l--)
3599 *t++ = fillchar;
3600 }
3601 }
3602 continue;
3603 }
3604 minwid = 0;
3605 maxwid = 9999;
3606 zeropad = FALSE;
3607 l = 1;
3608 if (*s == '0')
3609 {
3610 s++;
3611 zeropad = TRUE;
3612 }
3613 if (*s == '-')
3614 {
3615 s++;
3616 l = -1;
3617 }
3618 if (VIM_ISDIGIT(*s))
3619 {
3620 minwid = (int)getdigits(&s);
3621 if (minwid < 0) /* overflow */
3622 minwid = 0;
3623 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003624 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
3626 item[curitem].type = Highlight;
3627 item[curitem].start = p;
3628 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3629 s++;
3630 curitem++;
3631 continue;
3632 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003633 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3634 {
3635 if (*s == STL_TABCLOSENR)
3636 {
3637 if (minwid == 0)
3638 {
3639 /* %X ends the close label, go back to the previously
3640 * define tab label nr. */
3641 for (n = curitem - 1; n >= 0; --n)
3642 if (item[n].type == TabPage && item[n].minwid >= 0)
3643 {
3644 minwid = item[n].minwid;
3645 break;
3646 }
3647 }
3648 else
3649 /* close nrs are stored as negative values */
3650 minwid = - minwid;
3651 }
3652 item[curitem].type = TabPage;
3653 item[curitem].start = p;
3654 item[curitem].minwid = minwid;
3655 s++;
3656 curitem++;
3657 continue;
3658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (*s == '.')
3660 {
3661 s++;
3662 if (VIM_ISDIGIT(*s))
3663 {
3664 maxwid = (int)getdigits(&s);
3665 if (maxwid <= 0) /* overflow */
3666 maxwid = 50;
3667 }
3668 }
3669 minwid = (minwid > 50 ? 50 : minwid) * l;
3670 if (*s == '(')
3671 {
3672 groupitem[groupdepth++] = curitem;
3673 item[curitem].type = Group;
3674 item[curitem].start = p;
3675 item[curitem].minwid = minwid;
3676 item[curitem].maxwid = maxwid;
3677 s++;
3678 curitem++;
3679 continue;
3680 }
3681 if (vim_strchr(STL_ALL, *s) == NULL)
3682 {
3683 s++;
3684 continue;
3685 }
3686 opt = *s++;
3687
3688 /* OK - now for the real work */
3689 base = 'D';
3690 itemisflag = FALSE;
3691 fillable = TRUE;
3692 num = -1;
3693 str = NULL;
3694 switch (opt)
3695 {
3696 case STL_FILEPATH:
3697 case STL_FULLPATH:
3698 case STL_FILENAME:
3699 fillable = FALSE; /* don't change ' ' to fillchar */
3700 if (buf_spname(wp->w_buffer) != NULL)
3701 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3702 else
3703 {
3704 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003705 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3707 }
3708 trans_characters(NameBuff, MAXPATHL);
3709 if (opt != STL_FILENAME)
3710 str = NameBuff;
3711 else
3712 str = gettail(NameBuff);
3713 break;
3714
3715 case STL_VIM_EXPR: /* '{' */
3716 itemisflag = TRUE;
3717 t = p;
3718 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3719 *p++ = *s++;
3720 if (*s != '}') /* missing '}' or out of space */
3721 break;
3722 s++;
3723 *p = 0;
3724 p = t;
3725
3726#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003727 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3729
3730 o_curbuf = curbuf;
3731 o_curwin = curwin;
3732 curwin = wp;
3733 curbuf = wp->w_buffer;
3734
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003735 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736
3737 curwin = o_curwin;
3738 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003739 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740
3741 if (str != NULL && *str != 0)
3742 {
3743 if (*skipdigits(str) == NUL)
3744 {
3745 num = atoi((char *)str);
3746 vim_free(str);
3747 str = NULL;
3748 itemisflag = FALSE;
3749 }
3750 }
3751#endif
3752 break;
3753
3754 case STL_LINE:
3755 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3756 ? 0L : (long)(wp->w_cursor.lnum);
3757 break;
3758
3759 case STL_NUMLINES:
3760 num = wp->w_buffer->b_ml.ml_line_count;
3761 break;
3762
3763 case STL_COLUMN:
3764 num = !(State & INSERT) && empty_line
3765 ? 0 : (int)wp->w_cursor.col + 1;
3766 break;
3767
3768 case STL_VIRTCOL:
3769 case STL_VIRTCOL_ALT:
3770 /* In list mode virtcol needs to be recomputed */
3771 virtcol = wp->w_virtcol;
3772 if (wp->w_p_list && lcs_tab1 == NUL)
3773 {
3774 wp->w_p_list = FALSE;
3775 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3776 wp->w_p_list = TRUE;
3777 }
3778 ++virtcol;
3779 /* Don't display %V if it's the same as %c. */
3780 if (opt == STL_VIRTCOL_ALT
3781 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3782 ? 0 : (int)wp->w_cursor.col + 1)))
3783 break;
3784 num = (long)virtcol;
3785 break;
3786
3787 case STL_PERCENTAGE:
3788 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3789 (long)wp->w_buffer->b_ml.ml_line_count);
3790 break;
3791
3792 case STL_ALTPERCENT:
3793 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003794 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 break;
3796
3797 case STL_ARGLISTSTAT:
3798 fillable = FALSE;
3799 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003800 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 str = tmp;
3802 break;
3803
3804 case STL_KEYMAP:
3805 fillable = FALSE;
3806 if (get_keymap_str(wp, tmp, TMPLEN))
3807 str = tmp;
3808 break;
3809 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003810#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003811 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812#else
3813 num = 0;
3814#endif
3815 break;
3816
3817 case STL_BUFNO:
3818 num = wp->w_buffer->b_fnum;
3819 break;
3820
3821 case STL_OFFSET_X:
3822 base = 'X';
3823 case STL_OFFSET:
3824#ifdef FEAT_BYTEOFF
3825 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3826 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3827 0L : l + 1 + (!(State & INSERT) && empty_line ?
3828 0 : (int)wp->w_cursor.col);
3829#endif
3830 break;
3831
3832 case STL_BYTEVAL_X:
3833 base = 'X';
3834 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003835 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 num = 0;
3837 else
3838 {
3839#ifdef FEAT_MBYTE
3840 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3841#else
3842 num = linecont[wp->w_cursor.col];
3843#endif
3844 }
3845 if (num == NL)
3846 num = 0;
3847 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3848 num = NL;
3849 break;
3850
3851 case STL_ROFLAG:
3852 case STL_ROFLAG_ALT:
3853 itemisflag = TRUE;
3854 if (wp->w_buffer->b_p_ro)
3855 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3856 break;
3857
3858 case STL_HELPFLAG:
3859 case STL_HELPFLAG_ALT:
3860 itemisflag = TRUE;
3861 if (wp->w_buffer->b_help)
3862 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003863 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 break;
3865
3866#ifdef FEAT_AUTOCMD
3867 case STL_FILETYPE:
3868 if (*wp->w_buffer->b_p_ft != NUL
3869 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3870 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003871 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3872 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 str = tmp;
3874 }
3875 break;
3876
3877 case STL_FILETYPE_ALT:
3878 itemisflag = TRUE;
3879 if (*wp->w_buffer->b_p_ft != NUL
3880 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
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 for (t = tmp; *t != 0; t++)
3885 *t = TOUPPER_LOC(*t);
3886 str = tmp;
3887 }
3888 break;
3889#endif
3890
3891#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3892 case STL_PREVIEWFLAG:
3893 case STL_PREVIEWFLAG_ALT:
3894 itemisflag = TRUE;
3895 if (wp->w_p_pvw)
3896 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3897 : _("[Preview]"));
3898 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003899
3900 case STL_QUICKFIX:
3901 if (bt_quickfix(wp->w_buffer))
3902 str = (char_u *)(wp->w_llist_ref
3903 ? _(msg_loclist)
3904 : _(msg_qflist));
3905 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906#endif
3907
3908 case STL_MODIFIED:
3909 case STL_MODIFIED_ALT:
3910 itemisflag = TRUE;
3911 switch ((opt == STL_MODIFIED_ALT)
3912 + bufIsChanged(wp->w_buffer) * 2
3913 + (!wp->w_buffer->b_p_ma) * 4)
3914 {
3915 case 2: str = (char_u *)"[+]"; break;
3916 case 3: str = (char_u *)",+"; break;
3917 case 4: str = (char_u *)"[-]"; break;
3918 case 5: str = (char_u *)",-"; break;
3919 case 6: str = (char_u *)"[+-]"; break;
3920 case 7: str = (char_u *)",+-"; break;
3921 }
3922 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003923
3924 case STL_HIGHLIGHT:
3925 t = s;
3926 while (*s != '#' && *s != NUL)
3927 ++s;
3928 if (*s == '#')
3929 {
3930 item[curitem].type = Highlight;
3931 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003932 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003933 curitem++;
3934 }
3935 ++s;
3936 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
3938
3939 item[curitem].start = p;
3940 item[curitem].type = Normal;
3941 if (str != NULL && *str)
3942 {
3943 t = str;
3944 if (itemisflag)
3945 {
3946 if ((t[0] && t[1])
3947 && ((!prevchar_isitem && *t == ',')
3948 || (prevchar_isflag && *t == ' ')))
3949 t++;
3950 prevchar_isflag = TRUE;
3951 }
3952 l = vim_strsize(t);
3953 if (l > 0)
3954 prevchar_isitem = TRUE;
3955 if (l > maxwid)
3956 {
3957 while (l >= maxwid)
3958#ifdef FEAT_MBYTE
3959 if (has_mbyte)
3960 {
3961 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003962 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 }
3964 else
3965#endif
3966 l -= byte2cells(*t++);
3967 if (p + 1 >= out + outlen)
3968 break;
3969 *p++ = '<';
3970 }
3971 if (minwid > 0)
3972 {
3973 for (; l < minwid && p + 1 < out + outlen; l++)
3974 {
3975 /* Don't put a "-" in front of a digit. */
3976 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3977 *p++ = ' ';
3978 else
3979 *p++ = fillchar;
3980 }
3981 minwid = 0;
3982 }
3983 else
3984 minwid *= -1;
3985 while (*t && p + 1 < out + outlen)
3986 {
3987 *p++ = *t++;
3988 /* Change a space by fillchar, unless fillchar is '-' and a
3989 * digit follows. */
3990 if (fillable && p[-1] == ' '
3991 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3992 p[-1] = fillchar;
3993 }
3994 for (; l < minwid && p + 1 < out + outlen; l++)
3995 *p++ = fillchar;
3996 }
3997 else if (num >= 0)
3998 {
3999 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4000 char_u nstr[20];
4001
4002 if (p + 20 >= out + outlen)
4003 break; /* not sufficient space */
4004 prevchar_isitem = TRUE;
4005 t = nstr;
4006 if (opt == STL_VIRTCOL_ALT)
4007 {
4008 *t++ = '-';
4009 minwid--;
4010 }
4011 *t++ = '%';
4012 if (zeropad)
4013 *t++ = '0';
4014 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004015 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 *t = 0;
4017
4018 for (n = num, l = 1; n >= nbase; n /= nbase)
4019 l++;
4020 if (opt == STL_VIRTCOL_ALT)
4021 l++;
4022 if (l > maxwid)
4023 {
4024 l += 2;
4025 n = l - maxwid;
4026 while (l-- > maxwid)
4027 num /= nbase;
4028 *t++ = '>';
4029 *t++ = '%';
4030 *t = t[-3];
4031 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004032 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4033 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 }
4035 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004036 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4037 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 p += STRLEN(p);
4039 }
4040 else
4041 item[curitem].type = Empty;
4042
4043 if (opt == STL_VIM_EXPR)
4044 vim_free(str);
4045
4046 if (num >= 0 || (!itemisflag && str && *str))
4047 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4048 curitem++;
4049 }
4050 *p = NUL;
4051 itemcnt = curitem;
4052
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004053#ifdef FEAT_EVAL
4054 if (usefmt != fmt)
4055 vim_free(usefmt);
4056#endif
4057
Bram Moolenaar071d4272004-06-13 20:20:40 +00004058 width = vim_strsize(out);
4059 if (maxwidth > 0 && width > maxwidth)
4060 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004061 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004062 l = 0;
4063 if (itemcnt == 0)
4064 s = out;
4065 else
4066 {
4067 for ( ; l < itemcnt; l++)
4068 if (item[l].type == Trunc)
4069 {
4070 /* Truncate at %< item. */
4071 s = item[l].start;
4072 break;
4073 }
4074 if (l == itemcnt)
4075 {
4076 /* No %< item, truncate first item. */
4077 s = item[0].start;
4078 l = 0;
4079 }
4080 }
4081
4082 if (width - vim_strsize(s) >= maxwidth)
4083 {
4084 /* Truncation mark is beyond max length */
4085#ifdef FEAT_MBYTE
4086 if (has_mbyte)
4087 {
4088 s = out;
4089 width = 0;
4090 for (;;)
4091 {
4092 width += ptr2cells(s);
4093 if (width >= maxwidth)
4094 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004095 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097 /* Fill up for half a double-wide character. */
4098 while (++width < maxwidth)
4099 *s++ = fillchar;
4100 }
4101 else
4102#endif
4103 s = out + maxwidth - 1;
4104 for (l = 0; l < itemcnt; l++)
4105 if (item[l].start > s)
4106 break;
4107 itemcnt = l;
4108 *s++ = '>';
4109 *s = 0;
4110 }
4111 else
4112 {
4113#ifdef FEAT_MBYTE
4114 if (has_mbyte)
4115 {
4116 n = 0;
4117 while (width >= maxwidth)
4118 {
4119 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004120 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 }
4122 }
4123 else
4124#endif
4125 n = width - maxwidth + 1;
4126 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004127 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 *s = '<';
4129
4130 /* Fill up for half a double-wide character. */
4131 while (++width < maxwidth)
4132 {
4133 s = s + STRLEN(s);
4134 *s++ = fillchar;
4135 *s = NUL;
4136 }
4137
4138 --n; /* count the '<' */
4139 for (; l < itemcnt; l++)
4140 {
4141 if (item[l].start - n >= s)
4142 item[l].start -= n;
4143 else
4144 item[l].start = s;
4145 }
4146 }
4147 width = maxwidth;
4148 }
4149 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4150 {
4151 /* Apply STL_MIDDLE if any */
4152 for (l = 0; l < itemcnt; l++)
4153 if (item[l].type == Middle)
4154 break;
4155 if (l < itemcnt)
4156 {
4157 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004158 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 for (s = item[l].start; s < p; s++)
4160 *s = fillchar;
4161 for (l++; l < itemcnt; l++)
4162 item[l].start += maxwidth - width;
4163 width = maxwidth;
4164 }
4165 }
4166
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004167 /* Store the info about highlighting. */
4168 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004170 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 for (l = 0; l < itemcnt; l++)
4172 {
4173 if (item[l].type == Highlight)
4174 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004175 sp->start = item[l].start;
4176 sp->userhl = item[l].minwid;
4177 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 }
4179 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004180 sp->start = NULL;
4181 sp->userhl = 0;
4182 }
4183
4184 /* Store the info about tab pages labels. */
4185 if (tabtab != NULL)
4186 {
4187 sp = tabtab;
4188 for (l = 0; l < itemcnt; l++)
4189 {
4190 if (item[l].type == TabPage)
4191 {
4192 sp->start = item[l].start;
4193 sp->userhl = item[l].minwid;
4194 sp++;
4195 }
4196 }
4197 sp->start = NULL;
4198 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200
4201 return width;
4202}
4203#endif /* FEAT_STL_OPT */
4204
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004205#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4206 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004208 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4209 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 */
4211 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004212get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004214 char_u *buf;
4215 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216{
4217 long above; /* number of lines above window */
4218 long below; /* number of lines below window */
4219
4220 above = wp->w_topline - 1;
4221#ifdef FEAT_DIFF
4222 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4223#endif
4224 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4225 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004226 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4227 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004229 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004231 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 ? (int)(above / ((above + below) / 100L))
4233 : (int)(above * 100L / (above + below)));
4234}
4235#endif
4236
4237/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004238 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 * Return TRUE if it was appended.
4240 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004241 static int
4242append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 win_T *wp;
4244 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004245 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
4248 char_u *p;
4249
4250 if (ARGCOUNT <= 1) /* nothing to do */
4251 return FALSE;
4252
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004253 p = buf + STRLEN(buf); /* go to the end of the buffer */
4254 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return FALSE;
4256 *p++ = ' ';
4257 *p++ = '(';
4258 if (add_file)
4259 {
4260 STRCPY(p, "file ");
4261 p += 5;
4262 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004263 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4264 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4266 return TRUE;
4267}
4268
4269/*
4270 * If fname is not a full path, make it a full path.
4271 * Returns pointer to allocated memory (NULL for failure).
4272 */
4273 char_u *
4274fix_fname(fname)
4275 char_u *fname;
4276{
4277 /*
4278 * Force expanding the path always for Unix, because symbolic links may
4279 * mess up the full path name, even though it starts with a '/'.
4280 * Also expand when there is ".." in the file name, try to remove it,
4281 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004282 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 * For MS-Windows also expand names like "longna~1" to "longname".
4284 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004285#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 return FullName_save(fname, TRUE);
4287#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004288 if (!vim_isAbsName(fname)
4289 || strstr((char *)fname, "..") != NULL
4290 || strstr((char *)fname, "//") != NULL
4291# ifdef BACKSLASH_IN_FILENAME
4292 || strstr((char *)fname, "\\\\") != NULL
4293# endif
4294# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004296# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 )
4298 return FullName_save(fname, FALSE);
4299
4300 fname = vim_strsave(fname);
4301
Bram Moolenaar9b942202007-10-03 12:31:33 +00004302# ifdef USE_FNAME_CASE
4303# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004305# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 if (fname != NULL)
4308 fname_case(fname, 0); /* set correct case for file name */
4309 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311
4312 return fname;
4313#endif
4314}
4315
4316/*
4317 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4318 * "ffname" becomes a pointer to allocated memory (or NULL).
4319 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 void
4321fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004322 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 char_u **ffname;
4324 char_u **sfname;
4325{
4326 if (*ffname == NULL) /* if no file name given, nothing to do */
4327 return;
4328 if (*sfname == NULL) /* if no short file name given, use ffname */
4329 *sfname = *ffname;
4330 *ffname = fix_fname(*ffname); /* expand to full path */
4331
4332#ifdef FEAT_SHORTCUT
4333 if (!buf->b_p_bin)
4334 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004335 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336
4337 /* If the file name is a shortcut file, use the file it links to. */
4338 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004339 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 {
4341 vim_free(*ffname);
4342 *ffname = rfname;
4343 *sfname = rfname;
4344 }
4345 }
4346#endif
4347}
4348
4349/*
4350 * Get the file name for an argument list entry.
4351 */
4352 char_u *
4353alist_name(aep)
4354 aentry_T *aep;
4355{
4356 buf_T *bp;
4357
4358 /* Use the name from the associated buffer if it exists. */
4359 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004360 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 return aep->ae_fname;
4362 return bp->b_fname;
4363}
4364
4365#if defined(FEAT_WINDOWS) || defined(PROTO)
4366/*
4367 * do_arg_all(): Open up to 'count' windows, one for each argument.
4368 */
4369 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004370do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 int count;
4372 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004373 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004374{
4375 int i;
4376 win_T *wp, *wpnext;
4377 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004378 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 int use_firstwin = FALSE; /* use first window for arglist */
4380 int split_ret = OK;
4381 int p_ea_save;
4382 alist_T *alist; /* argument list to be used */
4383 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004384 tabpage_T *tpnext;
4385 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004386 win_T *new_curwin = NULL;
4387 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388
4389 if (ARGCOUNT <= 0)
4390 {
4391 /* Don't give an error message. We don't want it when the ":all"
4392 * command is in the .vimrc. */
4393 return;
4394 }
4395 setpcmark();
4396
4397 opened_len = ARGCOUNT;
4398 opened = alloc_clear((unsigned)opened_len);
4399 if (opened == NULL)
4400 return;
4401
4402#ifdef FEAT_GUI
4403 need_mouse_correct = TRUE;
4404#endif
4405
4406 /*
4407 * Try closing all windows that are not in the argument list.
4408 * Also close windows that are not full width;
4409 * When 'hidden' or "forceit" set the buffer becomes hidden.
4410 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004411 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004413 if (had_tab > 0)
4414 goto_tabpage_tp(first_tabpage);
4415 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004417 tpnext = curtab->tp_next;
4418 for (wp = firstwin; wp != NULL; wp = wpnext)
4419 {
4420 wpnext = wp->w_next;
4421 buf = wp->w_buffer;
4422 if (buf->b_ffname == NULL
4423 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004425 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004427 )
4428 i = ARGCOUNT;
4429 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004431 /* check if the buffer in this window is in the arglist */
4432 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004434 if (ARGLIST[i].ae_fnum == buf->b_fnum
4435 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004436 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004438 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004439 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004440 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004441 if (i == 0)
4442 {
4443 new_curwin = wp;
4444 new_curtab = curtab;
4445 }
4446 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004447 if (wp->w_alist != curwin->w_alist)
4448 {
4449 /* Use the current argument list for all windows
4450 * containing a file from it. */
4451 alist_unlink(wp->w_alist);
4452 wp->w_alist = curwin->w_alist;
4453 ++wp->w_alist->al_refcount;
4454 }
4455 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004459 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004461 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004463 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004464 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004466 /* If the buffer was changed, and we would like to hide it,
4467 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004468 if (!P_HID(buf) && buf->b_nwindows <= 1
4469 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004471 (void)autowrite(buf, FALSE);
4472#ifdef FEAT_AUTOCMD
4473 /* check if autocommands removed the window */
4474 if (!win_valid(wp) || !buf_valid(buf))
4475 {
4476 wpnext = firstwin; /* start all over... */
4477 continue;
4478 }
4479#endif
4480 }
4481#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004482 /* don't close last window */
4483 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004484#endif
4485 use_firstwin = TRUE;
4486#ifdef FEAT_WINDOWS
4487 else
4488 {
4489 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4490# ifdef FEAT_AUTOCMD
4491 /* check if autocommands removed the next window */
4492 if (!win_valid(wpnext))
4493 wpnext = firstwin; /* start all over... */
4494# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 }
4496#endif
4497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498 }
4499 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004500
4501 /* Without the ":tab" modifier only do the current tab page. */
4502 if (had_tab == 0 || tpnext == NULL)
4503 break;
4504
4505# ifdef FEAT_AUTOCMD
4506 /* check if autocommands removed the next tab page */
4507 if (!valid_tabpage(tpnext))
4508 tpnext = first_tabpage; /* start all over...*/
4509# endif
4510 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 }
4512
4513 /*
4514 * Open a window for files in the argument list that don't have one.
4515 * ARGCOUNT may change while doing this, because of autocommands.
4516 */
4517 if (count > ARGCOUNT || count <= 0)
4518 count = ARGCOUNT;
4519
4520 /* Autocommands may do anything to the argument list. Make sure it's not
4521 * freed while we are working here by "locking" it. We still have to
4522 * watch out for its size to be changed. */
4523 alist = curwin->w_alist;
4524 ++alist->al_refcount;
4525
4526#ifdef FEAT_AUTOCMD
4527 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4528 ++autocmd_no_enter;
4529 ++autocmd_no_leave;
4530#endif
4531 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004532#ifdef FEAT_WINDOWS
4533 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4534 * leaving an empty tab page when executed locally. */
4535 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4536 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4537 use_firstwin = TRUE;
4538#endif
4539
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4541 {
4542 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4543 arg_had_last = TRUE;
4544 if (i < opened_len && opened[i])
4545 {
4546 /* Move the already present window to below the current window */
4547 if (curwin->w_arg_idx != i)
4548 {
4549 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4550 {
4551 if (wpnext->w_arg_idx == i)
4552 {
4553 win_move_after(wpnext, curwin);
4554 break;
4555 }
4556 }
4557 }
4558 }
4559 else if (split_ret == OK)
4560 {
4561 if (!use_firstwin) /* split current window */
4562 {
4563 p_ea_save = p_ea;
4564 p_ea = TRUE; /* use space from all windows */
4565 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4566 p_ea = p_ea_save;
4567 if (split_ret == FAIL)
4568 continue;
4569 }
4570#ifdef FEAT_AUTOCMD
4571 else /* first window: do autocmd for leaving this buffer */
4572 --autocmd_no_leave;
4573#endif
4574
4575 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004576 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 */
4578 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004579 if (i == 0)
4580 {
4581 new_curwin = curwin;
4582 new_curtab = curtab;
4583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4585 ECMD_ONE,
4586 ((P_HID(curwin->w_buffer)
4587 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004588 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589#ifdef FEAT_AUTOCMD
4590 if (use_firstwin)
4591 ++autocmd_no_leave;
4592#endif
4593 use_firstwin = FALSE;
4594 }
4595 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004596
4597 /* When ":tab" was used open a new tab for a new window repeatedly. */
4598 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4599 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
4601
4602 /* Remove the "lock" on the argument list. */
4603 alist_unlink(alist);
4604
4605#ifdef FEAT_AUTOCMD
4606 --autocmd_no_enter;
4607#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004608 /* to window with first arg */
4609 if (valid_tabpage(new_curtab))
4610 goto_tabpage_tp(new_curtab);
4611 if (win_valid(new_curwin))
4612 win_enter(new_curwin, FALSE);
4613
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614#ifdef FEAT_AUTOCMD
4615 --autocmd_no_leave;
4616#endif
4617 vim_free(opened);
4618}
4619
4620# if defined(FEAT_LISTCMDS) || defined(PROTO)
4621/*
4622 * Open a window for a number of buffers.
4623 */
4624 void
4625ex_buffer_all(eap)
4626 exarg_T *eap;
4627{
4628 buf_T *buf;
4629 win_T *wp, *wpnext;
4630 int split_ret = OK;
4631 int p_ea_save;
4632 int open_wins = 0;
4633 int r;
4634 int count; /* Maximum number of windows to open. */
4635 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004636#ifdef FEAT_WINDOWS
4637 int had_tab = cmdmod.tab;
4638 tabpage_T *tpnext;
4639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640
4641 if (eap->addr_count == 0) /* make as many windows as possible */
4642 count = 9999;
4643 else
4644 count = eap->line2; /* make as many windows as specified */
4645 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4646 all = FALSE;
4647 else
4648 all = TRUE;
4649
4650 setpcmark();
4651
4652#ifdef FEAT_GUI
4653 need_mouse_correct = TRUE;
4654#endif
4655
4656 /*
4657 * Close superfluous windows (two windows for the same buffer).
4658 * Also close windows that are not full-width.
4659 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004660#ifdef FEAT_WINDOWS
4661 if (had_tab > 0)
4662 goto_tabpage_tp(first_tabpage);
4663 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004666 tpnext = curtab->tp_next;
4667 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004669 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004670 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004671#ifdef FEAT_VERTSPLIT
4672 || ((cmdmod.split & WSP_VERT)
4673 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004674 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004675 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004677#ifdef FEAT_WINDOWS
4678 || (had_tab > 0 && wp != firstwin)
4679#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004680 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004681 {
4682 win_close(wp, FALSE);
4683#ifdef FEAT_AUTOCMD
4684 wpnext = firstwin; /* just in case an autocommand does
4685 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004686 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004687 open_wins = 0;
4688#endif
4689 }
4690 else
4691 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004693
4694#ifdef FEAT_WINDOWS
4695 /* Without the ":tab" modifier only do the current tab page. */
4696 if (had_tab == 0 || tpnext == NULL)
4697 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004698 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701
4702 /*
4703 * Go through the buffer list. When a buffer doesn't have a window yet,
4704 * open one. Otherwise move the window to the right position.
4705 * Watch out for autocommands that delete buffers or windows!
4706 */
4707#ifdef FEAT_AUTOCMD
4708 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4709 ++autocmd_no_enter;
4710#endif
4711 win_enter(lastwin, FALSE);
4712#ifdef FEAT_AUTOCMD
4713 ++autocmd_no_leave;
4714#endif
4715 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4716 {
4717 /* Check if this buffer needs a window */
4718 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4719 continue;
4720
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004721#ifdef FEAT_WINDOWS
4722 if (had_tab != 0)
4723 {
4724 /* With the ":tab" modifier don't move the window. */
4725 if (buf->b_nwindows > 0)
4726 wp = lastwin; /* buffer has a window, skip it */
4727 else
4728 wp = NULL;
4729 }
4730 else
4731#endif
4732 {
4733 /* Check if this buffer already has a window */
4734 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4735 if (wp->w_buffer == buf)
4736 break;
4737 /* If the buffer already has a window, move it */
4738 if (wp != NULL)
4739 win_move_after(wp, curwin);
4740 }
4741
4742 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
4744 /* Split the window and put the buffer in it */
4745 p_ea_save = p_ea;
4746 p_ea = TRUE; /* use space from all windows */
4747 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4748 ++open_wins;
4749 p_ea = p_ea_save;
4750 if (split_ret == FAIL)
4751 continue;
4752
4753 /* Open the buffer in this window. */
Bram Moolenaare64ac772005-12-07 20:54:59 +00004754#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 swap_exists_action = SEA_DIALOG;
4756#endif
4757 set_curbuf(buf, DOBUF_GOTO);
4758#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004761#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004763# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 break;
4765 }
4766#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004767#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (swap_exists_action == SEA_QUIT)
4769 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004770# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4771 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004772
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004773 /* Reset the error/interrupt/exception state here so that
4774 * aborting() returns FALSE when closing a window. */
4775 enter_cleanup(&cs);
4776# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004777
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004778 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 win_close(curwin, TRUE);
4780 --open_wins;
4781 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004782 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004783
4784# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4785 /* Restore the error/interrupt/exception state if not
4786 * discarded by a new aborting error, interrupt, or uncaught
4787 * exception. */
4788 leave_cleanup(&cs);
4789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791 else
4792 handle_swap_exists(NULL);
4793#endif
4794 }
4795
4796 ui_breakcheck();
4797 if (got_int)
4798 {
4799 (void)vgetc(); /* only break the file loading, not the rest */
4800 break;
4801 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004802#ifdef FEAT_EVAL
4803 /* Autocommands deleted the buffer or aborted script processing!!! */
4804 if (aborting())
4805 break;
4806#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004807#ifdef FEAT_WINDOWS
4808 /* When ":tab" was used open a new tab for a new window repeatedly. */
4809 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4810 cmdmod.tab = 9999;
4811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 }
4813#ifdef FEAT_AUTOCMD
4814 --autocmd_no_enter;
4815#endif
4816 win_enter(firstwin, FALSE); /* back to first window */
4817#ifdef FEAT_AUTOCMD
4818 --autocmd_no_leave;
4819#endif
4820
4821 /*
4822 * Close superfluous windows.
4823 */
4824 for (wp = lastwin; open_wins > count; )
4825 {
4826 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4827 || autowrite(wp->w_buffer, FALSE) == OK);
4828#ifdef FEAT_AUTOCMD
4829 if (!win_valid(wp))
4830 {
4831 /* BufWrite Autocommands made the window invalid, start over */
4832 wp = lastwin;
4833 }
4834 else
4835#endif
4836 if (r)
4837 {
4838 win_close(wp, !P_HID(wp->w_buffer));
4839 --open_wins;
4840 wp = lastwin;
4841 }
4842 else
4843 {
4844 wp = wp->w_prev;
4845 if (wp == NULL)
4846 break;
4847 }
4848 }
4849}
4850# endif /* FEAT_LISTCMDS */
4851
4852#endif /* FEAT_WINDOWS */
4853
Bram Moolenaara3227e22006-03-08 21:32:40 +00004854static int chk_modeline __ARGS((linenr_T, int));
4855
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856/*
4857 * do_modelines() - process mode lines for the current file
4858 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004859 * "flags" can be:
4860 * OPT_WINONLY only set options local to window
4861 * OPT_NOWIN don't set options local to window
4862 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 * Returns immediately if the "ml" option isn't set.
4864 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004866do_modelines(flags)
4867 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004869 linenr_T lnum;
4870 int nmlines;
4871 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872
4873 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4874 return;
4875
4876 /* Disallow recursive entry here. Can happen when executing a modeline
4877 * triggers an autocommand, which reloads modelines with a ":do". */
4878 if (entered)
4879 return;
4880
4881 ++entered;
4882 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4883 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004884 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 nmlines = 0;
4886
4887 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4888 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004889 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 nmlines = 0;
4891 --entered;
4892}
4893
4894#include "version.h" /* for version number */
4895
4896/*
4897 * chk_modeline() - check a single line for a mode string
4898 * Return FAIL if an error encountered.
4899 */
4900 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004901chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004903 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
4905 char_u *s;
4906 char_u *e;
4907 char_u *linecopy; /* local copy of any modeline found */
4908 int prev;
4909 int vers;
4910 int end;
4911 int retval = OK;
4912 char_u *save_sourcing_name;
4913 linenr_T save_sourcing_lnum;
4914#ifdef FEAT_EVAL
4915 scid_T save_SID;
4916#endif
4917
4918 prev = -1;
4919 for (s = ml_get(lnum); *s != NUL; ++s)
4920 {
4921 if (prev == -1 || vim_isspace(prev))
4922 {
4923 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4924 || STRNCMP(s, "vi:", (size_t)3) == 0)
4925 break;
4926 if (STRNCMP(s, "vim", 3) == 0)
4927 {
4928 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4929 e = s + 4;
4930 else
4931 e = s + 3;
4932 vers = getdigits(&e);
4933 if (*e == ':'
4934 && (s[3] == ':'
4935 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4936 || (VIM_VERSION_100 < vers && s[3] == '<')
4937 || (VIM_VERSION_100 > vers && s[3] == '>')
4938 || (VIM_VERSION_100 == vers && s[3] == '=')))
4939 break;
4940 }
4941 }
4942 prev = *s;
4943 }
4944
4945 if (*s)
4946 {
4947 do /* skip over "ex:", "vi:" or "vim:" */
4948 ++s;
4949 while (s[-1] != ':');
4950
4951 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4952 if (linecopy == NULL)
4953 return FAIL;
4954
4955 save_sourcing_lnum = sourcing_lnum;
4956 save_sourcing_name = sourcing_name;
4957 sourcing_lnum = lnum; /* prepare for emsg() */
4958 sourcing_name = (char_u *)"modelines";
4959
4960 end = FALSE;
4961 while (end == FALSE)
4962 {
4963 s = skipwhite(s);
4964 if (*s == NUL)
4965 break;
4966
4967 /*
4968 * Find end of set command: ':' or end of line.
4969 * Skip over "\:", replacing it with ":".
4970 */
4971 for (e = s; *e != ':' && *e != NUL; ++e)
4972 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004973 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 if (*e == NUL)
4975 end = TRUE;
4976
4977 /*
4978 * If there is a "set" command, require a terminating ':' and
4979 * ignore the stuff after the ':'.
4980 * "vi:set opt opt opt: foo" -- foo not interpreted
4981 * "vi:opt opt opt: foo" -- foo interpreted
4982 * Accept "se" for compatibility with Elvis.
4983 */
4984 if (STRNCMP(s, "set ", (size_t)4) == 0
4985 || STRNCMP(s, "se ", (size_t)3) == 0)
4986 {
4987 if (*e != ':') /* no terminating ':'? */
4988 break;
4989 end = TRUE;
4990 s = vim_strchr(s, ' ') + 1;
4991 }
4992 *e = NUL; /* truncate the set command */
4993
4994 if (*s != NUL) /* skip over an empty "::" */
4995 {
4996#ifdef FEAT_EVAL
4997 save_SID = current_SID;
4998 current_SID = SID_MODELINE;
4999#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005000 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001#ifdef FEAT_EVAL
5002 current_SID = save_SID;
5003#endif
5004 if (retval == FAIL) /* stop if error found */
5005 break;
5006 }
5007 s = e + 1; /* advance to next part */
5008 }
5009
5010 sourcing_lnum = save_sourcing_lnum;
5011 sourcing_name = save_sourcing_name;
5012
5013 vim_free(linecopy);
5014 }
5015 return retval;
5016}
5017
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005018#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 int
5020read_viminfo_bufferlist(virp, writing)
5021 vir_T *virp;
5022 int writing;
5023{
5024 char_u *tab;
5025 linenr_T lnum;
5026 colnr_T col;
5027 buf_T *buf;
5028 char_u *sfname;
5029 char_u *xline;
5030
5031 /* Handle long line and escaped characters. */
5032 xline = viminfo_readstring(virp, 1, FALSE);
5033
5034 /* don't read in if there are files on the command-line or if writing: */
5035 if (xline != NULL && !writing && ARGCOUNT == 0
5036 && find_viminfo_parameter('%') != NULL)
5037 {
5038 /* Format is: <fname> Tab <lnum> Tab <col>.
5039 * Watch out for a Tab in the file name, work from the end. */
5040 lnum = 0;
5041 col = 0;
5042 tab = vim_strrchr(xline, '\t');
5043 if (tab != NULL)
5044 {
5045 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005046 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 tab = vim_strrchr(xline, '\t');
5048 if (tab != NULL)
5049 {
5050 *tab++ = '\0';
5051 lnum = atol((char *)tab);
5052 }
5053 }
5054
5055 /* Expand "~/" in the file name at "line + 1" to a full path.
5056 * Then try shortening it by comparing with the current directory */
5057 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005058 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
5060 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5061 if (buf != NULL) /* just in case... */
5062 {
5063 buf->b_last_cursor.lnum = lnum;
5064 buf->b_last_cursor.col = col;
5065 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5066 }
5067 }
5068 vim_free(xline);
5069
5070 return viminfo_readline(virp);
5071}
5072
5073 void
5074write_viminfo_bufferlist(fp)
5075 FILE *fp;
5076{
5077 buf_T *buf;
5078#ifdef FEAT_WINDOWS
5079 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005080 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081#endif
5082 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005083 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084
5085 if (find_viminfo_parameter('%') == NULL)
5086 return;
5087
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005088 /* Without a number -1 is returned: do all buffers. */
5089 max_buffers = get_viminfo_parameter('%');
5090
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005092#define LINE_BUF_LEN (MAXPATHL + 40)
5093 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (line == NULL)
5095 return;
5096
5097#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005098 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 set_last_cursor(win);
5100#else
5101 set_last_cursor(curwin);
5102#endif
5103
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005104 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5106 {
5107 if (buf->b_fname == NULL
5108 || !buf->b_p_bl
5109#ifdef FEAT_QUICKFIX
5110 || bt_quickfix(buf)
5111#endif
5112 || removable(buf->b_ffname))
5113 continue;
5114
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005115 if (max_buffers-- == 0)
5116 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 putc('%', fp);
5118 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005119 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 (long)buf->b_last_cursor.lnum,
5121 buf->b_last_cursor.col);
5122 viminfo_writestring(fp, line);
5123 }
5124 vim_free(line);
5125}
5126#endif
5127
5128
5129/*
5130 * Return special buffer name.
5131 * Returns NULL when the buffer has a normal file name.
5132 */
5133 char *
5134buf_spname(buf)
5135 buf_T *buf;
5136{
5137#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5138 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005139 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005140 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005141 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005142
5143 /*
5144 * For location list window, w_llist_ref points to the location list.
5145 * For quickfix window, w_llist_ref is NULL.
5146 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005147 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005148 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005149 goto win_found;
5150win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005151 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005152 return _(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005153 else
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005154 return _(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156#endif
5157#ifdef FEAT_QUICKFIX
5158 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5159 * contains the name as specified by the user */
5160 if (bt_nofile(buf))
5161 {
5162 if (buf->b_sfname != NULL)
5163 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005164 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166#endif
5167 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005168 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 return NULL;
5170}
5171
5172
5173#if defined(FEAT_SIGNS) || defined(PROTO)
5174/*
5175 * Insert the sign into the signlist.
5176 */
5177 static void
5178insert_sign(buf, prev, next, id, lnum, typenr)
5179 buf_T *buf; /* buffer to store sign in */
5180 signlist_T *prev; /* previous sign entry */
5181 signlist_T *next; /* next sign entry */
5182 int id; /* sign ID */
5183 linenr_T lnum; /* line number which gets the mark */
5184 int typenr; /* typenr of sign we are adding */
5185{
5186 signlist_T *newsign;
5187
5188 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5189 if (newsign != NULL)
5190 {
5191 newsign->id = id;
5192 newsign->lnum = lnum;
5193 newsign->typenr = typenr;
5194 newsign->next = next;
5195#ifdef FEAT_NETBEANS_INTG
5196 newsign->prev = prev;
5197 if (next != NULL)
5198 next->prev = newsign;
5199#endif
5200
5201 if (prev == NULL)
5202 {
5203 /* When adding first sign need to redraw the windows to create the
5204 * column for signs. */
5205 if (buf->b_signlist == NULL)
5206 {
5207 redraw_buf_later(buf, NOT_VALID);
5208 changed_cline_bef_curs();
5209 }
5210
5211 /* first sign in signlist */
5212 buf->b_signlist = newsign;
5213 }
5214 else
5215 prev->next = newsign;
5216 }
5217}
5218
5219/*
5220 * Add the sign into the signlist. Find the right spot to do it though.
5221 */
5222 void
5223buf_addsign(buf, id, lnum, typenr)
5224 buf_T *buf; /* buffer to store sign in */
5225 int id; /* sign ID */
5226 linenr_T lnum; /* line number which gets the mark */
5227 int typenr; /* typenr of sign we are adding */
5228{
5229 signlist_T *sign; /* a sign in the signlist */
5230 signlist_T *prev; /* the previous sign */
5231
5232 prev = NULL;
5233 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5234 {
5235 if (lnum == sign->lnum && id == sign->id)
5236 {
5237 sign->typenr = typenr;
5238 return;
5239 }
5240 else if (
5241#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5242 id < 0 &&
5243#endif
5244 lnum < sign->lnum)
5245 {
5246#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5247 /* XXX - GRP: Is this because of sign slide problem? Or is it
5248 * really needed? Or is it because we allow multiple signs per
5249 * line? If so, should I add that feature to FEAT_SIGNS?
5250 */
5251 while (prev != NULL && prev->lnum == lnum)
5252 prev = prev->prev;
5253 if (prev == NULL)
5254 sign = buf->b_signlist;
5255 else
5256 sign = prev->next;
5257#endif
5258 insert_sign(buf, prev, sign, id, lnum, typenr);
5259 return;
5260 }
5261 prev = sign;
5262 }
5263#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5264 /* XXX - GRP: See previous comment */
5265 while (prev != NULL && prev->lnum == lnum)
5266 prev = prev->prev;
5267 if (prev == NULL)
5268 sign = buf->b_signlist;
5269 else
5270 sign = prev->next;
5271#endif
5272 insert_sign(buf, prev, sign, id, lnum, typenr);
5273
5274 return;
5275}
5276
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005277 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278buf_change_sign_type(buf, markId, typenr)
5279 buf_T *buf; /* buffer to store sign in */
5280 int markId; /* sign ID */
5281 int typenr; /* typenr of sign we are adding */
5282{
5283 signlist_T *sign; /* a sign in the signlist */
5284
5285 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5286 {
5287 if (sign->id == markId)
5288 {
5289 sign->typenr = typenr;
5290 return sign->lnum;
5291 }
5292 }
5293
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005294 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295}
5296
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005297 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298buf_getsigntype(buf, lnum, type)
5299 buf_T *buf;
5300 linenr_T lnum;
5301 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5302{
5303 signlist_T *sign; /* a sign in a b_signlist */
5304
5305 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5306 if (sign->lnum == lnum
5307 && (type == SIGN_ANY
5308# ifdef FEAT_SIGN_ICONS
5309 || (type == SIGN_ICON
5310 && sign_get_image(sign->typenr) != NULL)
5311# endif
5312 || (type == SIGN_TEXT
5313 && sign_get_text(sign->typenr) != NULL)
5314 || (type == SIGN_LINEHL
5315 && sign_get_attr(sign->typenr, TRUE) != 0)))
5316 return sign->typenr;
5317 return 0;
5318}
5319
5320
5321 linenr_T
5322buf_delsign(buf, id)
5323 buf_T *buf; /* buffer sign is stored in */
5324 int id; /* sign id */
5325{
5326 signlist_T **lastp; /* pointer to pointer to current sign */
5327 signlist_T *sign; /* a sign in a b_signlist */
5328 signlist_T *next; /* the next sign in a b_signlist */
5329 linenr_T lnum; /* line number whose sign was deleted */
5330
5331 lastp = &buf->b_signlist;
5332 lnum = 0;
5333 for (sign = buf->b_signlist; sign != NULL; sign = next)
5334 {
5335 next = sign->next;
5336 if (sign->id == id)
5337 {
5338 *lastp = next;
5339#ifdef FEAT_NETBEANS_INTG
5340 if (next != NULL)
5341 next->prev = sign->prev;
5342#endif
5343 lnum = sign->lnum;
5344 vim_free(sign);
5345 break;
5346 }
5347 else
5348 lastp = &sign->next;
5349 }
5350
5351 /* When deleted the last sign need to redraw the windows to remove the
5352 * sign column. */
5353 if (buf->b_signlist == NULL)
5354 {
5355 redraw_buf_later(buf, NOT_VALID);
5356 changed_cline_bef_curs();
5357 }
5358
5359 return lnum;
5360}
5361
5362
5363/*
5364 * Find the line number of the sign with the requested id. If the sign does
5365 * not exist, return 0 as the line number. This will still let the correct file
5366 * get loaded.
5367 */
5368 int
5369buf_findsign(buf, id)
5370 buf_T *buf; /* buffer to store sign in */
5371 int id; /* sign ID */
5372{
5373 signlist_T *sign; /* a sign in the signlist */
5374
5375 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5376 if (sign->id == id)
5377 return sign->lnum;
5378
5379 return 0;
5380}
5381
5382 int
5383buf_findsign_id(buf, lnum)
5384 buf_T *buf; /* buffer whose sign we are searching for */
5385 linenr_T lnum; /* line number of sign */
5386{
5387 signlist_T *sign; /* a sign in the signlist */
5388
5389 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5390 if (sign->lnum == lnum)
5391 return sign->id;
5392
5393 return 0;
5394}
5395
5396
5397# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5398/* see if a given type of sign exists on a specific line */
5399 int
5400buf_findsigntype_id(buf, lnum, typenr)
5401 buf_T *buf; /* buffer whose sign we are searching for */
5402 linenr_T lnum; /* line number of sign */
5403 int typenr; /* sign type number */
5404{
5405 signlist_T *sign; /* a sign in the signlist */
5406
5407 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5408 if (sign->lnum == lnum && sign->typenr == typenr)
5409 return sign->id;
5410
5411 return 0;
5412}
5413
5414
5415# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5416/* return the number of icons on the given line */
5417 int
5418buf_signcount(buf, lnum)
5419 buf_T *buf;
5420 linenr_T lnum;
5421{
5422 signlist_T *sign; /* a sign in the signlist */
5423 int count = 0;
5424
5425 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5426 if (sign->lnum == lnum)
5427 if (sign_get_image(sign->typenr) != NULL)
5428 count++;
5429
5430 return count;
5431}
5432# endif /* FEAT_SIGN_ICONS */
5433# endif /* FEAT_NETBEANS_INTG */
5434
5435
5436/*
5437 * Delete signs in buffer "buf".
5438 */
5439 static void
5440buf_delete_signs(buf)
5441 buf_T *buf;
5442{
5443 signlist_T *next;
5444
5445 while (buf->b_signlist != NULL)
5446 {
5447 next = buf->b_signlist->next;
5448 vim_free(buf->b_signlist);
5449 buf->b_signlist = next;
5450 }
5451}
5452
5453/*
5454 * Delete all signs in all buffers.
5455 */
5456 void
5457buf_delete_all_signs()
5458{
5459 buf_T *buf; /* buffer we are checking for signs */
5460
5461 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5462 if (buf->b_signlist != NULL)
5463 {
5464 /* Need to redraw the windows to remove the sign column. */
5465 redraw_buf_later(buf, NOT_VALID);
5466 buf_delete_signs(buf);
5467 }
5468}
5469
5470/*
5471 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5472 */
5473 void
5474sign_list_placed(rbuf)
5475 buf_T *rbuf;
5476{
5477 buf_T *buf;
5478 signlist_T *p;
5479 char lbuf[BUFSIZ];
5480
5481 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5482 msg_putchar('\n');
5483 if (rbuf == NULL)
5484 buf = firstbuf;
5485 else
5486 buf = rbuf;
5487 while (buf != NULL)
5488 {
5489 if (buf->b_signlist != NULL)
5490 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005491 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5493 msg_putchar('\n');
5494 }
5495 for (p = buf->b_signlist; p != NULL; p = p->next)
5496 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005497 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5499 MSG_PUTS(lbuf);
5500 msg_putchar('\n');
5501 }
5502 if (rbuf != NULL)
5503 break;
5504 buf = buf->b_next;
5505 }
5506}
5507
5508/*
5509 * Adjust a placed sign for inserted/deleted lines.
5510 */
5511 void
5512sign_mark_adjust(line1, line2, amount, amount_after)
5513 linenr_T line1;
5514 linenr_T line2;
5515 long amount;
5516 long amount_after;
5517{
5518 signlist_T *sign; /* a sign in a b_signlist */
5519
5520 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5521 {
5522 if (sign->lnum >= line1 && sign->lnum <= line2)
5523 {
5524 if (amount == MAXLNUM)
5525 sign->lnum = line1;
5526 else
5527 sign->lnum += amount;
5528 }
5529 else if (sign->lnum > line2)
5530 sign->lnum += amount_after;
5531 }
5532}
5533#endif /* FEAT_SIGNS */
5534
5535/*
5536 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5537 */
5538 void
5539set_buflisted(on)
5540 int on;
5541{
5542 if (on != curbuf->b_p_bl)
5543 {
5544 curbuf->b_p_bl = on;
5545#ifdef FEAT_AUTOCMD
5546 if (on)
5547 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5548 else
5549 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5550#endif
5551 }
5552}
5553
5554/*
5555 * Read the file for "buf" again and check if the contents changed.
5556 * Return TRUE if it changed or this could not be checked.
5557 */
5558 int
5559buf_contents_changed(buf)
5560 buf_T *buf;
5561{
5562 buf_T *newbuf;
5563 int differ = TRUE;
5564 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 exarg_T ea;
5567
5568 /* Allocate a buffer without putting it in the buffer list. */
5569 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5570 if (newbuf == NULL)
5571 return TRUE;
5572
5573 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5574 if (prep_exarg(&ea, buf) == FAIL)
5575 {
5576 wipe_buffer(newbuf, FALSE);
5577 return TRUE;
5578 }
5579
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 /* set curwin/curbuf to buf and save a few things */
5581 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582
Bram Moolenaar4770d092006-01-12 23:22:24 +00005583 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 && readfile(buf->b_ffname, buf->b_fname,
5585 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5586 &ea, READ_NEW | READ_DUMMY) == OK)
5587 {
5588 /* compare the two files line by line */
5589 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5590 {
5591 differ = FALSE;
5592 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5593 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5594 {
5595 differ = TRUE;
5596 break;
5597 }
5598 }
5599 }
5600 vim_free(ea.cmd);
5601
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 /* restore curwin/curbuf and a few other things */
5603 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
5605 if (curbuf != newbuf) /* safety check */
5606 wipe_buffer(newbuf, FALSE);
5607
5608 return differ;
5609}
5610
5611/*
5612 * Wipe out a buffer and decrement the last buffer number if it was used for
5613 * this buffer. Call this to wipe out a temp buffer that does not contain any
5614 * marks.
5615 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 void
5617wipe_buffer(buf, aucmd)
5618 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005619 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620{
5621 if (buf->b_fnum == top_file_num - 1)
5622 --top_file_num;
5623
5624#ifdef FEAT_AUTOCMD
5625 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005626 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627#endif
5628 close_buffer(NULL, buf, DOBUF_WIPE);
5629#ifdef FEAT_AUTOCMD
5630 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005631 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632#endif
5633}