blob: 81f6fdee8470051ed8b00f399468c3dcef1266fb [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 Moolenaara971b822011-09-14 14:43:25 +0200419 if (win_valid(win) && win->w_buffer == buf)
420 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421
422#ifdef FEAT_AUTOCMD
423 /* Autocommands may have deleted the buffer. */
424 if (!buf_valid(buf))
425 return;
426# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000427 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428 return;
429# endif
430
431 /* Autocommands may have opened or closed windows for this buffer.
432 * Decrement the count for the close we do here. */
433 if (buf->b_nwindows > 0)
434 --buf->b_nwindows;
435
436 /*
437 * It's possible that autocommands change curbuf to the one being deleted.
438 * This might cause the previous curbuf to be deleted unexpectedly. But
439 * in some cases it's OK to delete the curbuf, because a new one is
440 * obtained anyway. Therefore only return if curbuf changed to the
441 * deleted buffer.
442 */
443 if (buf == curbuf && !is_curbuf)
444 return;
445#endif
446
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000447 /* Change directories when the 'acd' option is set. */
448 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449
450 /*
451 * Remove the buffer from the list.
452 */
453 if (wipe_buf)
454 {
455#ifdef FEAT_SUN_WORKSHOP
456 if (usingSunWorkShop)
457 workshop_file_closed_lineno((char *)buf->b_ffname,
458 (int)buf->b_last_cursor.lnum);
459#endif
460 vim_free(buf->b_ffname);
461 vim_free(buf->b_sfname);
462 if (buf->b_prev == NULL)
463 firstbuf = buf->b_next;
464 else
465 buf->b_prev->b_next = buf->b_next;
466 if (buf->b_next == NULL)
467 lastbuf = buf->b_prev;
468 else
469 buf->b_next->b_prev = buf->b_prev;
470 free_buffer(buf);
471 }
472 else
473 {
474 if (del_buf)
475 {
476 /* Free all internal variables and reset option values, to make
477 * ":bdel" compatible with Vim 5.7. */
478 free_buffer_stuff(buf, TRUE);
479
480 /* Make it look like a new buffer. */
481 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
482
483 /* Init the options when loaded again. */
484 buf->b_p_initialized = FALSE;
485 }
486 buf_clear_file(buf);
487 if (del_buf)
488 buf->b_p_bl = FALSE;
489 }
490}
491
492/*
493 * Make buffer not contain a file.
494 */
495 void
496buf_clear_file(buf)
497 buf_T *buf;
498{
499 buf->b_ml.ml_line_count = 1;
500 unchanged(buf, TRUE);
501#ifndef SHORT_FNAME
502 buf->b_shortname = FALSE;
503#endif
504 buf->b_p_eol = TRUE;
505 buf->b_start_eol = TRUE;
506#ifdef FEAT_MBYTE
507 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000508 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509#endif
510 buf->b_ml.ml_mfp = NULL;
511 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
512#ifdef FEAT_NETBEANS_INTG
513 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000514#endif
515}
516
517/*
518 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200519 * the file. flags:
520 * BFA_DEL buffer is going to be deleted
521 * BFA_WIPE buffer is going to be wiped out
522 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200525buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200527 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528{
529#ifdef FEAT_AUTOCMD
530 int is_curbuf = (buf == curbuf);
531
532 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
533 if (!buf_valid(buf)) /* autocommands may delete the buffer */
534 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200535 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 {
537 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
538 if (!buf_valid(buf)) /* autocommands may delete the buffer */
539 return;
540 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200541 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 {
543 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
544 FALSE, buf);
545 if (!buf_valid(buf)) /* autocommands may delete the buffer */
546 return;
547 }
548# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000549 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 return;
551# endif
552
553 /*
554 * It's possible that autocommands change curbuf to the one being deleted.
555 * This might cause curbuf to be deleted unexpectedly. But in some cases
556 * it's OK to delete the curbuf, because a new one is obtained anyway.
557 * Therefore only return if curbuf changed to the deleted buffer.
558 */
559 if (buf == curbuf && !is_curbuf)
560 return;
561#endif
562#ifdef FEAT_DIFF
563 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
564#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200565#ifdef FEAT_SYN_HL
566 if (curwin->w_buffer == buf)
567 reset_synblock(curwin); /* remove any ownsyntax */
568#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000569
570#ifdef FEAT_FOLDING
571 /* No folds in an empty buffer. */
572# ifdef FEAT_WINDOWS
573 {
574 win_T *win;
575 tabpage_T *tp;
576
577 FOR_ALL_TAB_WINDOWS(tp, win)
578 if (win->w_buffer == buf)
579 clearFolding(win);
580 }
581# else
582 if (curwin->w_buffer == buf)
583 clearFolding(curwin);
584# endif
585#endif
586
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587#ifdef FEAT_TCL
588 tcl_buffer_free(buf);
589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 ml_close(buf, TRUE); /* close and delete the memline/memfile */
591 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200592 if ((flags & BFA_KEEP_UNDO) == 0)
593 {
594 u_blockfree(buf); /* free the memory allocated for undo */
595 u_clearall(buf); /* reset all undo information */
596 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200598 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000600 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601}
602
603/*
604 * Free a buffer structure and the things it contains related to the buffer
605 * itself (not the file, that must have been done already).
606 */
607 static void
608free_buffer(buf)
609 buf_T *buf;
610{
611 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200612#ifdef FEAT_LUA
613 lua_buffer_free(buf);
614#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000615#ifdef FEAT_MZSCHEME
616 mzscheme_buffer_free(buf);
617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000618#ifdef FEAT_PERL
619 perl_buf_free(buf);
620#endif
621#ifdef FEAT_PYTHON
622 python_buffer_free(buf);
623#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200624#ifdef FEAT_PYTHON3
625 python3_buffer_free(buf);
626#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627#ifdef FEAT_RUBY
628 ruby_buffer_free(buf);
629#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000630#ifdef FEAT_AUTOCMD
631 aubuflocal_remove(buf);
632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633 vim_free(buf);
634}
635
636/*
637 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
638 */
639 static void
640free_buffer_stuff(buf, free_options)
641 buf_T *buf;
642 int free_options; /* free options as well */
643{
644 if (free_options)
645 {
646 clear_wininfo(buf); /* including window-local options */
647 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200648#ifdef FEAT_SPELL
649 ga_clear(&buf->b_s.b_langp);
650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 }
652#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000653 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
654 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655#endif
656#ifdef FEAT_USR_CMDS
657 uc_clear(&buf->b_ucmds); /* clear local user commands */
658#endif
659#ifdef FEAT_SIGNS
660 buf_delete_signs(buf); /* delete any signs */
661#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000662#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200663 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000664#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000665#ifdef FEAT_LOCALMAP
666 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
667 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
668#endif
669#ifdef FEAT_MBYTE
670 vim_free(buf->b_start_fenc);
671 buf->b_start_fenc = NULL;
672#endif
673}
674
675/*
676 * Free the b_wininfo list for buffer "buf".
677 */
678 static void
679clear_wininfo(buf)
680 buf_T *buf;
681{
682 wininfo_T *wip;
683
684 while (buf->b_wininfo != NULL)
685 {
686 wip = buf->b_wininfo;
687 buf->b_wininfo = wip->wi_next;
688 if (wip->wi_optset)
689 {
690 clear_winopt(&wip->wi_opt);
691#ifdef FEAT_FOLDING
692 deleteFoldRecurse(&wip->wi_folds);
693#endif
694 }
695 vim_free(wip);
696 }
697}
698
699#if defined(FEAT_LISTCMDS) || defined(PROTO)
700/*
701 * Go to another buffer. Handles the result of the ATTENTION dialog.
702 */
703 void
704goto_buffer(eap, start, dir, count)
705 exarg_T *eap;
706 int start;
707 int dir;
708 int count;
709{
Bram Moolenaare64ac772005-12-07 20:54:59 +0000710# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 buf_T *old_curbuf = curbuf;
712
713 swap_exists_action = SEA_DIALOG;
714# endif
715 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
716 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000717# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000718 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
719 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000720# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
721 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000722
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000723 /* Reset the error/interrupt/exception state here so that
724 * aborting() returns FALSE when closing a window. */
725 enter_cleanup(&cs);
726# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000727
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000728 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 win_close(curwin, TRUE);
730 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000731 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000732
733# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
734 /* Restore the error/interrupt/exception state if not discarded by a
735 * new aborting error, interrupt, or uncaught exception. */
736 leave_cleanup(&cs);
737# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738 }
739 else
740 handle_swap_exists(old_curbuf);
741# endif
742}
743#endif
744
Bram Moolenaare64ac772005-12-07 20:54:59 +0000745#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746/*
747 * Handle the situation of swap_exists_action being set.
748 * It is allowed for "old_curbuf" to be NULL or invalid.
749 */
750 void
751handle_swap_exists(old_curbuf)
752 buf_T *old_curbuf;
753{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000754# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
755 cleanup_T cs;
756# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 if (swap_exists_action == SEA_QUIT)
759 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000760# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
761 /* Reset the error/interrupt/exception state here so that
762 * aborting() returns FALSE when closing a buffer. */
763 enter_cleanup(&cs);
764# endif
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766 /* User selected Quit at ATTENTION prompt. Go back to previous
767 * buffer. If that buffer is gone or the same as the current one,
768 * open a new, empty buffer. */
769 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000770 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
772 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000773 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (old_curbuf != NULL)
775 enter_buffer(old_curbuf);
776 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000777
778# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
779 /* Restore the error/interrupt/exception state if not discarded by a
780 * new aborting error, interrupt, or uncaught exception. */
781 leave_cleanup(&cs);
782# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 }
784 else if (swap_exists_action == SEA_RECOVER)
785 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000786# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
787 /* Reset the error/interrupt/exception state here so that
788 * aborting() returns FALSE when closing a buffer. */
789 enter_cleanup(&cs);
790# endif
791
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 /* User selected Recover at ATTENTION prompt. */
793 msg_scroll = TRUE;
794 ml_recover();
795 MSG_PUTS("\n"); /* don't overwrite the last message */
796 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000797 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000798
799# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
800 /* Restore the error/interrupt/exception state if not discarded by a
801 * new aborting error, interrupt, or uncaught exception. */
802 leave_cleanup(&cs);
803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805 swap_exists_action = SEA_NONE;
806}
807#endif
808
809#if defined(FEAT_LISTCMDS) || defined(PROTO)
810/*
811 * do_bufdel() - delete or unload buffer(s)
812 *
813 * addr_count == 0: ":bdel" - delete current buffer
814 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
815 * buffer "end_bnr", then any other arguments.
816 * addr_count == 2: ":N,N bdel" - delete buffers in range
817 *
818 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
819 * DOBUF_DEL (":bdel")
820 *
821 * Returns error message or NULL
822 */
823 char_u *
824do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
825 int command;
826 char_u *arg; /* pointer to extra arguments */
827 int addr_count;
828 int start_bnr; /* first buffer number in a range */
829 int end_bnr; /* buffer nr or last buffer nr in a range */
830 int forceit;
831{
832 int do_current = 0; /* delete current buffer? */
833 int deleted = 0; /* number of buffers deleted */
834 char_u *errormsg = NULL; /* return value */
835 int bnr; /* buffer number */
836 char_u *p;
837
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 if (addr_count == 0)
839 {
840 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
841 }
842 else
843 {
844 if (addr_count == 2)
845 {
846 if (*arg) /* both range and argument is not allowed */
847 return (char_u *)_(e_trailing);
848 bnr = start_bnr;
849 }
850 else /* addr_count == 1 */
851 bnr = end_bnr;
852
853 for ( ;!got_int; ui_breakcheck())
854 {
855 /*
856 * delete the current buffer last, otherwise when the
857 * current buffer is deleted, the next buffer becomes
858 * the current one and will be loaded, which may then
859 * also be deleted, etc.
860 */
861 if (bnr == curbuf->b_fnum)
862 do_current = bnr;
863 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
864 forceit) == OK)
865 ++deleted;
866
867 /*
868 * find next buffer number to delete/unload
869 */
870 if (addr_count == 2)
871 {
872 if (++bnr > end_bnr)
873 break;
874 }
875 else /* addr_count == 1 */
876 {
877 arg = skipwhite(arg);
878 if (*arg == NUL)
879 break;
880 if (!VIM_ISDIGIT(*arg))
881 {
882 p = skiptowhite_esc(arg);
883 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
884 if (bnr < 0) /* failed */
885 break;
886 arg = p;
887 }
888 else
889 bnr = getdigits(&arg);
890 }
891 }
892 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
893 FORWARD, do_current, forceit) == OK)
894 ++deleted;
895
896 if (deleted == 0)
897 {
898 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000899 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000901 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000903 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 errormsg = IObuff;
905 }
906 else if (deleted >= p_report)
907 {
908 if (command == DOBUF_UNLOAD)
909 {
910 if (deleted == 1)
911 MSG(_("1 buffer unloaded"));
912 else
913 smsg((char_u *)_("%d buffers unloaded"), deleted);
914 }
915 else if (command == DOBUF_DEL)
916 {
917 if (deleted == 1)
918 MSG(_("1 buffer deleted"));
919 else
920 smsg((char_u *)_("%d buffers deleted"), deleted);
921 }
922 else
923 {
924 if (deleted == 1)
925 MSG(_("1 buffer wiped out"));
926 else
927 smsg((char_u *)_("%d buffers wiped out"), deleted);
928 }
929 }
930 }
931
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
933 return errormsg;
934}
935
936/*
937 * Implementation of the commands for the buffer list.
938 *
939 * action == DOBUF_GOTO go to specified buffer
940 * action == DOBUF_SPLIT split window and go to specified buffer
941 * action == DOBUF_UNLOAD unload specified buffer(s)
942 * action == DOBUF_DEL delete specified buffer(s) from buffer list
943 * action == DOBUF_WIPE delete specified buffer(s) really
944 *
945 * start == DOBUF_CURRENT go to "count" buffer from current buffer
946 * start == DOBUF_FIRST go to "count" buffer from first buffer
947 * start == DOBUF_LAST go to "count" buffer from last buffer
948 * start == DOBUF_MOD go to "count" modified buffer from current buffer
949 *
950 * Return FAIL or OK.
951 */
952 int
953do_buffer(action, start, dir, count, forceit)
954 int action;
955 int start;
956 int dir; /* FORWARD or BACKWARD */
957 int count; /* buffer number or number of buffers */
958 int forceit; /* TRUE for :...! */
959{
960 buf_T *buf;
961 buf_T *bp;
962 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
963 || action == DOBUF_WIPE);
964
965 switch (start)
966 {
967 case DOBUF_FIRST: buf = firstbuf; break;
968 case DOBUF_LAST: buf = lastbuf; break;
969 default: buf = curbuf; break;
970 }
971 if (start == DOBUF_MOD) /* find next modified buffer */
972 {
973 while (count-- > 0)
974 {
975 do
976 {
977 buf = buf->b_next;
978 if (buf == NULL)
979 buf = firstbuf;
980 }
981 while (buf != curbuf && !bufIsChanged(buf));
982 }
983 if (!bufIsChanged(buf))
984 {
985 EMSG(_("E84: No modified buffer found"));
986 return FAIL;
987 }
988 }
989 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
990 {
991 while (buf != NULL && buf->b_fnum != count)
992 buf = buf->b_next;
993 }
994 else
995 {
996 bp = NULL;
997 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
998 {
999 /* remember the buffer where we start, we come back there when all
1000 * buffers are unlisted. */
1001 if (bp == NULL)
1002 bp = buf;
1003 if (dir == FORWARD)
1004 {
1005 buf = buf->b_next;
1006 if (buf == NULL)
1007 buf = firstbuf;
1008 }
1009 else
1010 {
1011 buf = buf->b_prev;
1012 if (buf == NULL)
1013 buf = lastbuf;
1014 }
1015 /* don't count unlisted buffers */
1016 if (unload || buf->b_p_bl)
1017 {
1018 --count;
1019 bp = NULL; /* use this buffer as new starting point */
1020 }
1021 if (bp == buf)
1022 {
1023 /* back where we started, didn't find anything. */
1024 EMSG(_("E85: There is no listed buffer"));
1025 return FAIL;
1026 }
1027 }
1028 }
1029
1030 if (buf == NULL) /* could not find it */
1031 {
1032 if (start == DOBUF_FIRST)
1033 {
1034 /* don't warn when deleting */
1035 if (!unload)
1036 EMSGN(_("E86: Buffer %ld does not exist"), count);
1037 }
1038 else if (dir == FORWARD)
1039 EMSG(_("E87: Cannot go beyond last buffer"));
1040 else
1041 EMSG(_("E88: Cannot go before first buffer"));
1042 return FAIL;
1043 }
1044
1045#ifdef FEAT_GUI
1046 need_mouse_correct = TRUE;
1047#endif
1048
1049#ifdef FEAT_LISTCMDS
1050 /*
1051 * delete buffer buf from memory and/or the list
1052 */
1053 if (unload)
1054 {
1055 int forward;
1056 int retval;
1057
1058 /* When unloading or deleting a buffer that's already unloaded and
1059 * unlisted: fail silently. */
1060 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1061 return FAIL;
1062
1063 if (!forceit && bufIsChanged(buf))
1064 {
1065#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1066 if ((p_confirm || cmdmod.confirm) && p_write)
1067 {
1068 dialog_changed(buf, FALSE);
1069# ifdef FEAT_AUTOCMD
1070 if (!buf_valid(buf))
1071 /* Autocommand deleted buffer, oops! It's not changed
1072 * now. */
1073 return FAIL;
1074# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001075 /* If it's still changed fail silently, the dialog already
1076 * mentioned why it fails. */
1077 if (bufIsChanged(buf))
1078 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001080 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081#endif
1082 {
1083 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1084 buf->b_fnum);
1085 return FAIL;
1086 }
1087 }
1088
1089 /*
1090 * If deleting the last (listed) buffer, make it empty.
1091 * The last (listed) buffer cannot be unloaded.
1092 */
1093 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1094 if (bp->b_p_bl && bp != buf)
1095 break;
1096 if (bp == NULL && buf == curbuf)
1097 {
1098 if (action == DOBUF_UNLOAD)
1099 {
1100 EMSG(_("E90: Cannot unload last buffer"));
1101 return FAIL;
1102 }
1103
1104 /* Close any other windows on this buffer, then make it empty. */
1105#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001106 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107#endif
1108 setpcmark();
1109 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001110 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111
1112 /*
1113 * do_ecmd() may create a new buffer, then we have to delete
1114 * the old one. But do_ecmd() may have done that already, check
1115 * if the buffer still exists.
1116 */
1117 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1118 close_buffer(NULL, buf, action);
1119 return retval;
1120 }
1121
1122#ifdef FEAT_WINDOWS
1123 /*
1124 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001125 * (unless it's the only window). Repeat this so long as we end up in
1126 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001128 while (buf == curbuf
1129 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001130 win_close(curwin, FALSE);
1131#endif
1132
1133 /*
1134 * If the buffer to be deleted is not the current one, delete it here.
1135 */
1136 if (buf != curbuf)
1137 {
1138#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001139 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140#endif
1141 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1142 close_buffer(NULL, buf, action);
1143 return OK;
1144 }
1145
1146 /*
1147 * Deleting the current buffer: Need to find another buffer to go to.
1148 * There must be another, otherwise it would have been handled above.
1149 * First use au_new_curbuf, if it is valid.
1150 * Then prefer the buffer we most recently visited.
1151 * Else try to find one that is loaded, after the current buffer,
1152 * then before the current buffer.
1153 * Finally use any buffer.
1154 */
1155 buf = NULL; /* selected buffer */
1156 bp = NULL; /* used when no loaded buffer found */
1157#ifdef FEAT_AUTOCMD
1158 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1159 buf = au_new_curbuf;
1160# ifdef FEAT_JUMPLIST
1161 else
1162# endif
1163#endif
1164#ifdef FEAT_JUMPLIST
1165 if (curwin->w_jumplistlen > 0)
1166 {
1167 int jumpidx;
1168
1169 jumpidx = curwin->w_jumplistidx - 1;
1170 if (jumpidx < 0)
1171 jumpidx = curwin->w_jumplistlen - 1;
1172
1173 forward = jumpidx;
1174 while (jumpidx != curwin->w_jumplistidx)
1175 {
1176 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1177 if (buf != NULL)
1178 {
1179 if (buf == curbuf || !buf->b_p_bl)
1180 buf = NULL; /* skip current and unlisted bufs */
1181 else if (buf->b_ml.ml_mfp == NULL)
1182 {
1183 /* skip unloaded buf, but may keep it for later */
1184 if (bp == NULL)
1185 bp = buf;
1186 buf = NULL;
1187 }
1188 }
1189 if (buf != NULL) /* found a valid buffer: stop searching */
1190 break;
1191 /* advance to older entry in jump list */
1192 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1193 break;
1194 if (--jumpidx < 0)
1195 jumpidx = curwin->w_jumplistlen - 1;
1196 if (jumpidx == forward) /* List exhausted for sure */
1197 break;
1198 }
1199 }
1200#endif
1201
1202 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1203 {
1204 forward = TRUE;
1205 buf = curbuf->b_next;
1206 for (;;)
1207 {
1208 if (buf == NULL)
1209 {
1210 if (!forward) /* tried both directions */
1211 break;
1212 buf = curbuf->b_prev;
1213 forward = FALSE;
1214 continue;
1215 }
1216 /* in non-help buffer, try to skip help buffers, and vv */
1217 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1218 {
1219 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1220 break;
1221 if (bp == NULL) /* remember unloaded buf for later */
1222 bp = buf;
1223 }
1224 if (forward)
1225 buf = buf->b_next;
1226 else
1227 buf = buf->b_prev;
1228 }
1229 }
1230 if (buf == NULL) /* No loaded buffer, use unloaded one */
1231 buf = bp;
1232 if (buf == NULL) /* No loaded buffer, find listed one */
1233 {
1234 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1235 if (buf->b_p_bl && buf != curbuf)
1236 break;
1237 }
1238 if (buf == NULL) /* Still no buffer, just take one */
1239 {
1240 if (curbuf->b_next != NULL)
1241 buf = curbuf->b_next;
1242 else
1243 buf = curbuf->b_prev;
1244 }
1245 }
1246
1247 /*
1248 * make buf current buffer
1249 */
1250 if (action == DOBUF_SPLIT) /* split window first */
1251 {
1252# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001253 /* If 'switchbuf' contains "useopen": jump to first window containing
1254 * "buf" if one exists */
1255 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001256 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001257 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001258 * page containing "buf" if one exists */
1259 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 return OK;
1261 if (win_split(0, 0) == FAIL)
1262# endif
1263 return FAIL;
1264 }
1265#endif
1266
1267 /* go to current buffer - nothing to do */
1268 if (buf == curbuf)
1269 return OK;
1270
1271 /*
1272 * Check if the current buffer may be abandoned.
1273 */
1274 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1275 {
1276#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1277 if ((p_confirm || cmdmod.confirm) && p_write)
1278 {
1279 dialog_changed(curbuf, FALSE);
1280# ifdef FEAT_AUTOCMD
1281 if (!buf_valid(buf))
1282 /* Autocommand deleted buffer, oops! */
1283 return FAIL;
1284# endif
1285 }
1286 if (bufIsChanged(curbuf))
1287#endif
1288 {
1289 EMSG(_(e_nowrtmsg));
1290 return FAIL;
1291 }
1292 }
1293
1294 /* Go to the other buffer. */
1295 set_curbuf(buf, action);
1296
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001297#if defined(FEAT_LISTCMDS) \
1298 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001300 {
1301 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303#endif
1304
1305#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1306 if (aborting()) /* autocmds may abort script processing */
1307 return FAIL;
1308#endif
1309
1310 return OK;
1311}
1312
1313#endif /* FEAT_LISTCMDS */
1314
1315/*
1316 * Set current buffer to "buf". Executes autocommands and closes current
1317 * buffer. "action" tells how to close the current buffer:
1318 * DOBUF_GOTO free or hide it
1319 * DOBUF_SPLIT nothing
1320 * DOBUF_UNLOAD unload it
1321 * DOBUF_DEL delete it
1322 * DOBUF_WIPE wipe it out
1323 */
1324 void
1325set_curbuf(buf, action)
1326 buf_T *buf;
1327 int action;
1328{
1329 buf_T *prevbuf;
1330 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1331 || action == DOBUF_WIPE);
1332
1333 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001334 if (!cmdmod.keepalt)
1335 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001336 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
1338#ifdef FEAT_VISUAL
1339 /* Don't restart Select mode after switching to another buffer. */
1340 VIsual_reselect = FALSE;
1341#endif
1342
1343 /* close_windows() or apply_autocmds() may change curbuf */
1344 prevbuf = curbuf;
1345
1346#ifdef FEAT_AUTOCMD
1347 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1348# ifdef FEAT_EVAL
1349 if (buf_valid(prevbuf) && !aborting())
1350# else
1351 if (buf_valid(prevbuf))
1352# endif
1353#endif
1354 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001355#ifdef FEAT_SYN_HL
1356 if (prevbuf == curwin->w_buffer)
1357 reset_synblock(curwin);
1358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359#ifdef FEAT_WINDOWS
1360 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001361 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#endif
1363#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1364 if (buf_valid(prevbuf) && !aborting())
1365#else
1366 if (buf_valid(prevbuf))
1367#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001368 {
1369 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001370 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1372 unload ? action : (action == DOBUF_GOTO
1373 && !P_HID(prevbuf)
1374 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 }
1377#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001378 /* An autocommand may have deleted "buf", already entered it (e.g., when
1379 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001381 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001383 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384# endif
1385#endif
1386 enter_buffer(buf);
1387}
1388
1389/*
1390 * Enter a new current buffer.
1391 * Old curbuf must have been abandoned already!
1392 */
1393 void
1394enter_buffer(buf)
1395 buf_T *buf;
1396{
1397 /* Copy buffer and window local option values. Not for a help buffer. */
1398 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1399 if (!buf->b_help)
1400 get_winopts(buf);
1401#ifdef FEAT_FOLDING
1402 else
1403 /* Remove all folds in the window. */
1404 clearFolding(curwin);
1405 foldUpdateAll(curwin); /* update folds (later). */
1406#endif
1407
1408 /* Get the buffer in the current window. */
1409 curwin->w_buffer = buf;
1410 curbuf = buf;
1411 ++curbuf->b_nwindows;
1412
1413#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001414 if (curwin->w_p_diff)
1415 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416#endif
1417
Bram Moolenaara971b822011-09-14 14:43:25 +02001418#ifdef FEAT_SYN_HL
1419 curwin->w_s = &(buf->b_s);
1420#endif
1421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 /* Cursor on first line by default. */
1423 curwin->w_cursor.lnum = 1;
1424 curwin->w_cursor.col = 0;
1425#ifdef FEAT_VIRTUALEDIT
1426 curwin->w_cursor.coladd = 0;
1427#endif
1428 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001429#ifdef FEAT_AUTOCMD
1430 curwin->w_topline_was_set = FALSE;
1431#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001433 /* mark cursor position as being invalid */
1434 curwin->w_valid = 0;
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 /* Make sure the buffer is loaded. */
1437 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001438 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001439#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001440 /* If there is no filetype, allow for detecting one. Esp. useful for
1441 * ":ball" used in a autocommand. If there already is a filetype we
1442 * might prefer to keep it. */
1443 if (*curbuf->b_p_ft == NUL)
1444 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001445#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001446
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001447 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 else
1450 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001451 if (!msg_silent)
1452 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1454#ifdef FEAT_AUTOCMD
1455 curwin->w_topline = 1;
1456# ifdef FEAT_DIFF
1457 curwin->w_topfill = 0;
1458# endif
1459 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1460 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1461#endif
1462 }
1463
1464 /* If autocommands did not change the cursor position, restore cursor lnum
1465 * and possibly cursor col. */
1466 if (curwin->w_cursor.lnum == 1 && inindent(0))
1467 buflist_getfpos();
1468
1469 check_arg_idx(curwin); /* check for valid arg_idx */
1470#ifdef FEAT_TITLE
1471 maketitle();
1472#endif
1473#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001474 /* when autocmds didn't change it */
1475 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476#endif
1477 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1478
Bram Moolenaar009b2592004-10-24 19:18:58 +00001479#ifdef FEAT_NETBEANS_INTG
1480 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001481 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001482#endif
1483
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001484 /* Change directories when the 'acd' option is set. */
1485 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486
1487#ifdef FEAT_KEYMAP
1488 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001489 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001491#ifdef FEAT_SPELL
1492 /* May need to set the spell language. Can only do this after the buffer
1493 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001494 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1495 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001496#endif
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 redraw_later(NOT_VALID);
1499}
1500
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001501#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1502/*
1503 * Change to the directory of the current buffer.
1504 */
1505 void
1506do_autochdir()
1507{
1508 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1509 shorten_fnames(TRUE);
1510}
1511#endif
1512
Bram Moolenaar071d4272004-06-13 20:20:40 +00001513/*
1514 * functions for dealing with the buffer list
1515 */
1516
1517/*
1518 * Add a file name to the buffer list. Return a pointer to the buffer.
1519 * If the same file name already exists return a pointer to that buffer.
1520 * If it does not exist, or if fname == NULL, a new entry is created.
1521 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1522 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1523 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 * This is the ONLY way to create a new buffer.
1525 */
1526static int top_file_num = 1; /* highest file number */
1527
1528 buf_T *
1529buflist_new(ffname, sfname, lnum, flags)
1530 char_u *ffname; /* full path of fname or relative */
1531 char_u *sfname; /* short fname or NULL */
1532 linenr_T lnum; /* preferred cursor line */
1533 int flags; /* BLN_ defines */
1534{
1535 buf_T *buf;
1536#ifdef UNIX
1537 struct stat st;
1538#endif
1539
1540 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1541
1542 /*
1543 * If file name already exists in the list, update the entry.
1544 */
1545#ifdef UNIX
1546 /* On Unix we can use inode numbers when the file exists. Works better
1547 * for hard links. */
1548 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1549 st.st_dev = (dev_T)-1;
1550#endif
1551 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1552#ifdef UNIX
1553 buflist_findname_stat(ffname, &st)
1554#else
1555 buflist_findname(ffname)
1556#endif
1557 ) != NULL)
1558 {
1559 vim_free(ffname);
1560 if (lnum != 0)
1561 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1562 /* copy the options now, if 'cpo' doesn't have 's' and not done
1563 * already */
1564 buf_copy_options(buf, 0);
1565 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1566 {
1567 buf->b_p_bl = TRUE;
1568#ifdef FEAT_AUTOCMD
1569 if (!(flags & BLN_DUMMY))
1570 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1571#endif
1572 }
1573 return buf;
1574 }
1575
1576 /*
1577 * If the current buffer has no name and no contents, use the current
1578 * buffer. Otherwise: Need to allocate a new buffer structure.
1579 *
1580 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001581 * (A spell file buffer is allocated in spell.c, but that's not a normal
1582 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 */
1584 buf = NULL;
1585 if ((flags & BLN_CURBUF)
1586 && curbuf != NULL
1587 && curbuf->b_ffname == NULL
1588 && curbuf->b_nwindows <= 1
1589 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1590 {
1591 buf = curbuf;
1592#ifdef FEAT_AUTOCMD
1593 /* It's like this buffer is deleted. Watch out for autocommands that
1594 * change curbuf! If that happens, allocate a new buffer anyway. */
1595 if (curbuf->b_p_bl)
1596 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1597 if (buf == curbuf)
1598 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1599# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001600 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 return NULL;
1602# endif
1603#endif
1604#ifdef FEAT_QUICKFIX
1605# ifdef FEAT_AUTOCMD
1606 if (buf == curbuf)
1607# endif
1608 {
1609 /* Make sure 'bufhidden' and 'buftype' are empty */
1610 clear_string_option(&buf->b_p_bh);
1611 clear_string_option(&buf->b_p_bt);
1612 }
1613#endif
1614 }
1615 if (buf != curbuf || curbuf == NULL)
1616 {
1617 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1618 if (buf == NULL)
1619 {
1620 vim_free(ffname);
1621 return NULL;
1622 }
1623 }
1624
1625 if (ffname != NULL)
1626 {
1627 buf->b_ffname = ffname;
1628 buf->b_sfname = vim_strsave(sfname);
1629 }
1630
1631 clear_wininfo(buf);
1632 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1633
1634 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1635 || buf->b_wininfo == NULL)
1636 {
1637 vim_free(buf->b_ffname);
1638 buf->b_ffname = NULL;
1639 vim_free(buf->b_sfname);
1640 buf->b_sfname = NULL;
1641 if (buf != curbuf)
1642 free_buffer(buf);
1643 return NULL;
1644 }
1645
1646 if (buf == curbuf)
1647 {
1648 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001649 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650 if (buf != curbuf) /* autocommands deleted the buffer! */
1651 return NULL;
1652#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001653 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 return NULL;
1655#endif
1656 /* buf->b_nwindows = 0; why was this here? */
1657 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1658#ifdef FEAT_KEYMAP
1659 /* need to reload lmaps and set b:keymap_name */
1660 curbuf->b_kmap_state |= KEYMAP_INIT;
1661#endif
1662 }
1663 else
1664 {
1665 /*
1666 * put new buffer at the end of the buffer list
1667 */
1668 buf->b_next = NULL;
1669 if (firstbuf == NULL) /* buffer list is empty */
1670 {
1671 buf->b_prev = NULL;
1672 firstbuf = buf;
1673 }
1674 else /* append new buffer at end of list */
1675 {
1676 lastbuf->b_next = buf;
1677 buf->b_prev = lastbuf;
1678 }
1679 lastbuf = buf;
1680
1681 buf->b_fnum = top_file_num++;
1682 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1683 {
1684 EMSG(_("W14: Warning: List of file names overflow"));
1685 if (emsg_silent == 0)
1686 {
1687 out_flush();
1688 ui_delay(3000L, TRUE); /* make sure it is noticed */
1689 }
1690 top_file_num = 1;
1691 }
1692
1693 /*
1694 * Always copy the options from the current buffer.
1695 */
1696 buf_copy_options(buf, BCO_ALWAYS);
1697 }
1698
1699 buf->b_wininfo->wi_fpos.lnum = lnum;
1700 buf->b_wininfo->wi_win = curwin;
1701
1702#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001703 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1704#endif
1705#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001706 hash_init(&buf->b_s.b_keywtab);
1707 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001708#endif
1709
1710 buf->b_fname = buf->b_sfname;
1711#ifdef UNIX
1712 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001713 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 else
1715 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001716 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001717 buf->b_dev = st.st_dev;
1718 buf->b_ino = st.st_ino;
1719 }
1720#endif
1721 buf->b_u_synced = TRUE;
1722 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001723 if (flags & BLN_DUMMY)
1724 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 buf_clear_file(buf);
1726 clrallmarks(buf); /* clear marks */
1727 fmarks_check_names(buf); /* check file marks for this file */
1728 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1729#ifdef FEAT_AUTOCMD
1730 if (!(flags & BLN_DUMMY))
1731 {
1732 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1733 if (flags & BLN_LISTED)
1734 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1735# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001736 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 return NULL;
1738# endif
1739 }
1740#endif
1741
1742 return buf;
1743}
1744
1745/*
1746 * Free the memory for the options of a buffer.
1747 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1748 * 'fileencoding'.
1749 */
1750 void
1751free_buf_options(buf, free_p_ff)
1752 buf_T *buf;
1753 int free_p_ff;
1754{
1755 if (free_p_ff)
1756 {
1757#ifdef FEAT_MBYTE
1758 clear_string_option(&buf->b_p_fenc);
1759#endif
1760 clear_string_option(&buf->b_p_ff);
1761#ifdef FEAT_QUICKFIX
1762 clear_string_option(&buf->b_p_bh);
1763 clear_string_option(&buf->b_p_bt);
1764#endif
1765 }
1766#ifdef FEAT_FIND_ID
1767 clear_string_option(&buf->b_p_def);
1768 clear_string_option(&buf->b_p_inc);
1769# ifdef FEAT_EVAL
1770 clear_string_option(&buf->b_p_inex);
1771# endif
1772#endif
1773#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1774 clear_string_option(&buf->b_p_inde);
1775 clear_string_option(&buf->b_p_indk);
1776#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001777#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1778 clear_string_option(&buf->b_p_bexpr);
1779#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001780#if defined(FEAT_CRYPT)
1781 clear_string_option(&buf->b_p_cm);
1782#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001783#if defined(FEAT_EVAL)
1784 clear_string_option(&buf->b_p_fex);
1785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786#ifdef FEAT_CRYPT
1787 clear_string_option(&buf->b_p_key);
1788#endif
1789 clear_string_option(&buf->b_p_kp);
1790 clear_string_option(&buf->b_p_mps);
1791 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001792 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 clear_string_option(&buf->b_p_isk);
1794#ifdef FEAT_KEYMAP
1795 clear_string_option(&buf->b_p_keymap);
1796 ga_clear(&buf->b_kmap_ga);
1797#endif
1798#ifdef FEAT_COMMENTS
1799 clear_string_option(&buf->b_p_com);
1800#endif
1801#ifdef FEAT_FOLDING
1802 clear_string_option(&buf->b_p_cms);
1803#endif
1804 clear_string_option(&buf->b_p_nf);
1805#ifdef FEAT_SYN_HL
1806 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001807#endif
1808#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001809 clear_string_option(&buf->b_s.b_p_spc);
1810 clear_string_option(&buf->b_s.b_p_spf);
1811 vim_free(buf->b_s.b_cap_prog);
1812 buf->b_s.b_cap_prog = NULL;
1813 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814#endif
1815#ifdef FEAT_SEARCHPATH
1816 clear_string_option(&buf->b_p_sua);
1817#endif
1818#ifdef FEAT_AUTOCMD
1819 clear_string_option(&buf->b_p_ft);
1820#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#ifdef FEAT_CINDENT
1822 clear_string_option(&buf->b_p_cink);
1823 clear_string_option(&buf->b_p_cino);
1824#endif
1825#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1826 clear_string_option(&buf->b_p_cinw);
1827#endif
1828#ifdef FEAT_INS_EXPAND
1829 clear_string_option(&buf->b_p_cpt);
1830#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001831#ifdef FEAT_COMPL_FUNC
1832 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001833 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835#ifdef FEAT_QUICKFIX
1836 clear_string_option(&buf->b_p_gp);
1837 clear_string_option(&buf->b_p_mp);
1838 clear_string_option(&buf->b_p_efm);
1839#endif
1840 clear_string_option(&buf->b_p_ep);
1841 clear_string_option(&buf->b_p_path);
1842 clear_string_option(&buf->b_p_tags);
1843#ifdef FEAT_INS_EXPAND
1844 clear_string_option(&buf->b_p_dict);
1845 clear_string_option(&buf->b_p_tsr);
1846#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001847#ifdef FEAT_TEXTOBJ
1848 clear_string_option(&buf->b_p_qe);
1849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 buf->b_p_ar = -1;
1851}
1852
1853/*
1854 * get alternate file n
1855 * set linenr to lnum or altfpos.lnum if lnum == 0
1856 * also set cursor column to altfpos.col if 'startofline' is not set.
1857 * if (options & GETF_SETMARK) call setpcmark()
1858 * if (options & GETF_ALT) we are jumping to an alternate file.
1859 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1860 *
1861 * return FAIL for failure, OK for success
1862 */
1863 int
1864buflist_getfile(n, lnum, options, forceit)
1865 int n;
1866 linenr_T lnum;
1867 int options;
1868 int forceit;
1869{
1870 buf_T *buf;
1871#ifdef FEAT_WINDOWS
1872 win_T *wp = NULL;
1873#endif
1874 pos_T *fpos;
1875 colnr_T col;
1876
1877 buf = buflist_findnr(n);
1878 if (buf == NULL)
1879 {
1880 if ((options & GETF_ALT) && n == 0)
1881 EMSG(_(e_noalt));
1882 else
1883 EMSGN(_("E92: Buffer %ld not found"), n);
1884 return FAIL;
1885 }
1886
1887 /* if alternate file is the current buffer, nothing to do */
1888 if (buf == curbuf)
1889 return OK;
1890
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001891 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001892 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001893 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001895 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001896#ifdef FEAT_AUTOCMD
1897 if (curbuf_locked())
1898 return FAIL;
1899#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 /* altfpos may be changed by getfile(), get it now */
1902 if (lnum == 0)
1903 {
1904 fpos = buflist_findfpos(buf);
1905 lnum = fpos->lnum;
1906 col = fpos->col;
1907 }
1908 else
1909 col = 0;
1910
1911#ifdef FEAT_WINDOWS
1912 if (options & GETF_SWITCH)
1913 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001914 /* If 'switchbuf' contains "useopen": jump to first window containing
1915 * "buf" if one exists */
1916 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001918 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1919 * page containing "buf" if one exists */
1920 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001921 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001922 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1923 * isn't empty: open new window */
1924 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001926 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1927 tabpage_new();
1928 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001930 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 }
1932 }
1933#endif
1934
1935 ++RedrawingDisabled;
1936 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1937 lnum, forceit) <= 0)
1938 {
1939 --RedrawingDisabled;
1940
1941 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1942 if (!p_sol && col != 0)
1943 {
1944 curwin->w_cursor.col = col;
1945 check_cursor_col();
1946#ifdef FEAT_VIRTUALEDIT
1947 curwin->w_cursor.coladd = 0;
1948#endif
1949 curwin->w_set_curswant = TRUE;
1950 }
1951 return OK;
1952 }
1953 --RedrawingDisabled;
1954 return FAIL;
1955}
1956
1957/*
1958 * go to the last know line number for the current buffer
1959 */
1960 void
1961buflist_getfpos()
1962{
1963 pos_T *fpos;
1964
1965 fpos = buflist_findfpos(curbuf);
1966
1967 curwin->w_cursor.lnum = fpos->lnum;
1968 check_cursor_lnum();
1969
1970 if (p_sol)
1971 curwin->w_cursor.col = 0;
1972 else
1973 {
1974 curwin->w_cursor.col = fpos->col;
1975 check_cursor_col();
1976#ifdef FEAT_VIRTUALEDIT
1977 curwin->w_cursor.coladd = 0;
1978#endif
1979 curwin->w_set_curswant = TRUE;
1980 }
1981}
1982
Bram Moolenaar81695252004-12-29 20:58:21 +00001983#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1984/*
1985 * Find file in buffer list by name (it has to be for the current window).
1986 * Returns NULL if not found.
1987 */
1988 buf_T *
1989buflist_findname_exp(fname)
1990 char_u *fname;
1991{
1992 char_u *ffname;
1993 buf_T *buf = NULL;
1994
1995 /* First make the name into a full path name */
1996 ffname = FullName_save(fname,
1997#ifdef UNIX
1998 TRUE /* force expansion, get rid of symbolic links */
1999#else
2000 FALSE
2001#endif
2002 );
2003 if (ffname != NULL)
2004 {
2005 buf = buflist_findname(ffname);
2006 vim_free(ffname);
2007 }
2008 return buf;
2009}
2010#endif
2011
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012/*
2013 * Find file in buffer list by name (it has to be for the current window).
2014 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002015 * Skips dummy buffers.
2016 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 */
2018 buf_T *
2019buflist_findname(ffname)
2020 char_u *ffname;
2021{
2022#ifdef UNIX
2023 struct stat st;
2024
2025 if (mch_stat((char *)ffname, &st) < 0)
2026 st.st_dev = (dev_T)-1;
2027 return buflist_findname_stat(ffname, &st);
2028}
2029
2030/*
2031 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2032 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002033 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 */
2035 static buf_T *
2036buflist_findname_stat(ffname, stp)
2037 char_u *ffname;
2038 struct stat *stp;
2039{
2040#endif
2041 buf_T *buf;
2042
2043 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002044 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002045#ifdef UNIX
2046 , stp
2047#endif
2048 ))
2049 return buf;
2050 return NULL;
2051}
2052
2053#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2054/*
2055 * Find file in buffer list by a regexp pattern.
2056 * Return fnum of the found buffer.
2057 * Return < 0 for error.
2058 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002059 int
2060buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2061 char_u *pattern;
2062 char_u *pattern_end; /* pointer to first char after pattern */
2063 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002064 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065{
2066 buf_T *buf;
2067 regprog_T *prog;
2068 int match = -1;
2069 int find_listed;
2070 char_u *pat;
2071 char_u *patend;
2072 int attempt;
2073 char_u *p;
2074 int toggledollar;
2075
2076 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2077 {
2078 if (*pattern == '%')
2079 match = curbuf->b_fnum;
2080 else
2081 match = curwin->w_alt_fnum;
2082#ifdef FEAT_DIFF
2083 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2084 match = -1;
2085#endif
2086 }
2087
2088 /*
2089 * Try four ways of matching a listed buffer:
2090 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002091 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 * attempt == 2: with '$' at end (only match at end)
2093 * attempt == 3: with '^' at start and '$' at end (only full match)
2094 * Repeat this for finding an unlisted buffer if there was no matching
2095 * listed buffer.
2096 */
2097 else
2098 {
2099 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2100 if (pat == NULL)
2101 return -1;
2102 patend = pat + STRLEN(pat) - 1;
2103 toggledollar = (patend > pat && *patend == '$');
2104
2105 /* First try finding a listed buffer. If not found and "unlisted"
2106 * is TRUE, try finding an unlisted buffer. */
2107 find_listed = TRUE;
2108 for (;;)
2109 {
2110 for (attempt = 0; attempt <= 3; ++attempt)
2111 {
2112 /* may add '^' and '$' */
2113 if (toggledollar)
2114 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2115 p = pat;
2116 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2117 ++p;
2118 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2119 if (prog == NULL)
2120 {
2121 vim_free(pat);
2122 return -1;
2123 }
2124
2125 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2126 if (buf->b_p_bl == find_listed
2127#ifdef FEAT_DIFF
2128 && (!diffmode || diff_mode_buf(buf))
2129#endif
2130 && buflist_match(prog, buf) != NULL)
2131 {
2132 if (match >= 0) /* already found a match */
2133 {
2134 match = -2;
2135 break;
2136 }
2137 match = buf->b_fnum; /* remember first match */
2138 }
2139
2140 vim_free(prog);
2141 if (match >= 0) /* found one match */
2142 break;
2143 }
2144
2145 /* Only search for unlisted buffers if there was no match with
2146 * a listed buffer. */
2147 if (!unlisted || !find_listed || match != -1)
2148 break;
2149 find_listed = FALSE;
2150 }
2151
2152 vim_free(pat);
2153 }
2154
2155 if (match == -2)
2156 EMSG2(_("E93: More than one match for %s"), pattern);
2157 else if (match < 0)
2158 EMSG2(_("E94: No matching buffer for %s"), pattern);
2159 return match;
2160}
2161#endif
2162
2163#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2164
2165/*
2166 * Find all buffer names that match.
2167 * For command line expansion of ":buf" and ":sbuf".
2168 * Return OK if matches found, FAIL otherwise.
2169 */
2170 int
2171ExpandBufnames(pat, num_file, file, options)
2172 char_u *pat;
2173 int *num_file;
2174 char_u ***file;
2175 int options;
2176{
2177 int count = 0;
2178 buf_T *buf;
2179 int round;
2180 char_u *p;
2181 int attempt;
2182 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002183 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184
2185 *num_file = 0; /* return values in case of FAIL */
2186 *file = NULL;
2187
Bram Moolenaar05159a02005-02-26 23:04:13 +00002188 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2189 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002191 patc = alloc((unsigned)STRLEN(pat) + 11);
2192 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002194 STRCPY(patc, "\\(^\\|[\\/]\\)");
2195 STRCPY(patc + 11, pat + 1);
2196 }
2197 else
2198 patc = pat;
2199
2200 /*
2201 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002202 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002203 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002204 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002205 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002206 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002207 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002208 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002209 if (prog == NULL)
2210 {
2211 if (patc != pat)
2212 vim_free(patc);
2213 return FAIL;
2214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
2216 /*
2217 * round == 1: Count the matches.
2218 * round == 2: Build the array to keep the matches.
2219 */
2220 for (round = 1; round <= 2; ++round)
2221 {
2222 count = 0;
2223 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2224 {
2225 if (!buf->b_p_bl) /* skip unlisted buffers */
2226 continue;
2227 p = buflist_match(prog, buf);
2228 if (p != NULL)
2229 {
2230 if (round == 1)
2231 ++count;
2232 else
2233 {
2234 if (options & WILD_HOME_REPLACE)
2235 p = home_replace_save(buf, p);
2236 else
2237 p = vim_strsave(p);
2238 (*file)[count++] = p;
2239 }
2240 }
2241 }
2242 if (count == 0) /* no match found, break here */
2243 break;
2244 if (round == 1)
2245 {
2246 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2247 if (*file == NULL)
2248 {
2249 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002250 if (patc != pat)
2251 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 return FAIL;
2253 }
2254 }
2255 }
2256 vim_free(prog);
2257 if (count) /* match(es) found, break here */
2258 break;
2259 }
2260
Bram Moolenaar05159a02005-02-26 23:04:13 +00002261 if (patc != pat)
2262 vim_free(patc);
2263
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 *num_file = count;
2265 return (count == 0 ? FAIL : OK);
2266}
2267
2268#endif /* FEAT_CMDL_COMPL */
2269
2270#ifdef HAVE_BUFLIST_MATCH
2271/*
2272 * Check for a match on the file name for buffer "buf" with regprog "prog".
2273 */
2274 static char_u *
2275buflist_match(prog, buf)
2276 regprog_T *prog;
2277 buf_T *buf;
2278{
2279 char_u *match;
2280
2281 /* First try the short file name, then the long file name. */
2282 match = fname_match(prog, buf->b_sfname);
2283 if (match == NULL)
2284 match = fname_match(prog, buf->b_ffname);
2285
2286 return match;
2287}
2288
2289/*
2290 * Try matching the regexp in "prog" with file name "name".
2291 * Return "name" when there is a match, NULL when not.
2292 */
2293 static char_u *
2294fname_match(prog, name)
2295 regprog_T *prog;
2296 char_u *name;
2297{
2298 char_u *match = NULL;
2299 char_u *p;
2300 regmatch_T regmatch;
2301
2302 if (name != NULL)
2303 {
2304 regmatch.regprog = prog;
2305#ifdef CASE_INSENSITIVE_FILENAME
2306 regmatch.rm_ic = TRUE; /* Always ignore case */
2307#else
2308 regmatch.rm_ic = FALSE; /* Never ignore case */
2309#endif
2310
2311 if (vim_regexec(&regmatch, name, (colnr_T)0))
2312 match = name;
2313 else
2314 {
2315 /* Replace $(HOME) with '~' and try matching again. */
2316 p = home_replace_save(NULL, name);
2317 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2318 match = name;
2319 vim_free(p);
2320 }
2321 }
2322
2323 return match;
2324}
2325#endif
2326
2327/*
2328 * find file in buffer list by number
2329 */
2330 buf_T *
2331buflist_findnr(nr)
2332 int nr;
2333{
2334 buf_T *buf;
2335
2336 if (nr == 0)
2337 nr = curwin->w_alt_fnum;
2338 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2339 if (buf->b_fnum == nr)
2340 return (buf);
2341 return NULL;
2342}
2343
2344/*
2345 * Get name of file 'n' in the buffer list.
2346 * When the file has no name an empty string is returned.
2347 * home_replace() is used to shorten the file name (used for marks).
2348 * Returns a pointer to allocated memory, of NULL when failed.
2349 */
2350 char_u *
2351buflist_nr2name(n, fullname, helptail)
2352 int n;
2353 int fullname;
2354 int helptail; /* for help buffers return tail only */
2355{
2356 buf_T *buf;
2357
2358 buf = buflist_findnr(n);
2359 if (buf == NULL)
2360 return NULL;
2361 return home_replace_save(helptail ? buf : NULL,
2362 fullname ? buf->b_ffname : buf->b_fname);
2363}
2364
2365/*
2366 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2367 * When "copy_options" is TRUE save the local window option values.
2368 * When "lnum" is 0 only do the options.
2369 */
2370 static void
2371buflist_setfpos(buf, win, lnum, col, copy_options)
2372 buf_T *buf;
2373 win_T *win;
2374 linenr_T lnum;
2375 colnr_T col;
2376 int copy_options;
2377{
2378 wininfo_T *wip;
2379
2380 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2381 if (wip->wi_win == win)
2382 break;
2383 if (wip == NULL)
2384 {
2385 /* allocate a new entry */
2386 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2387 if (wip == NULL)
2388 return;
2389 wip->wi_win = win;
2390 if (lnum == 0) /* set lnum even when it's 0 */
2391 lnum = 1;
2392 }
2393 else
2394 {
2395 /* remove the entry from the list */
2396 if (wip->wi_prev)
2397 wip->wi_prev->wi_next = wip->wi_next;
2398 else
2399 buf->b_wininfo = wip->wi_next;
2400 if (wip->wi_next)
2401 wip->wi_next->wi_prev = wip->wi_prev;
2402 if (copy_options && wip->wi_optset)
2403 {
2404 clear_winopt(&wip->wi_opt);
2405#ifdef FEAT_FOLDING
2406 deleteFoldRecurse(&wip->wi_folds);
2407#endif
2408 }
2409 }
2410 if (lnum != 0)
2411 {
2412 wip->wi_fpos.lnum = lnum;
2413 wip->wi_fpos.col = col;
2414 }
2415 if (copy_options)
2416 {
2417 /* Save the window-specific option values. */
2418 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2419#ifdef FEAT_FOLDING
2420 wip->wi_fold_manual = win->w_fold_manual;
2421 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2422#endif
2423 wip->wi_optset = TRUE;
2424 }
2425
2426 /* insert the entry in front of the list */
2427 wip->wi_next = buf->b_wininfo;
2428 buf->b_wininfo = wip;
2429 wip->wi_prev = NULL;
2430 if (wip->wi_next)
2431 wip->wi_next->wi_prev = wip;
2432
2433 return;
2434}
2435
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002436#ifdef FEAT_DIFF
2437static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2438
2439/*
2440 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2441 * page. That's because a diff is local to a tab page.
2442 */
2443 static int
2444wininfo_other_tab_diff(wip)
2445 wininfo_T *wip;
2446{
2447 win_T *wp;
2448
2449 if (wip->wi_opt.wo_diff)
2450 {
2451 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2452 /* return FALSE when it's a window in the current tab page, thus
2453 * the buffer was in diff mode here */
2454 if (wip->wi_win == wp)
2455 return FALSE;
2456 return TRUE;
2457 }
2458 return FALSE;
2459}
2460#endif
2461
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462/*
2463 * Find info for the current window in buffer "buf".
2464 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002465 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2466 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 * Returns NULL when there isn't any info.
2468 */
2469 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002470find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002472 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474 wininfo_T *wip;
2475
2476 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002477 if (wip->wi_win == curwin
2478#ifdef FEAT_DIFF
2479 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2480#endif
2481 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002483
2484 /* If no wininfo for curwin, use the first in the list (that doesn't have
2485 * 'diff' set and is in another tab page). */
2486 if (wip == NULL)
2487 {
2488#ifdef FEAT_DIFF
2489 if (skip_diff_buffer)
2490 {
2491 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2492 if (!wininfo_other_tab_diff(wip))
2493 break;
2494 }
2495 else
2496#endif
2497 wip = buf->b_wininfo;
2498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 return wip;
2500}
2501
2502/*
2503 * Reset the local window options to the values last used in this window.
2504 * If the buffer wasn't used in this window before, use the values from
2505 * the most recently used window. If the values were never set, use the
2506 * global values for the window.
2507 */
2508 void
2509get_winopts(buf)
2510 buf_T *buf;
2511{
2512 wininfo_T *wip;
2513
2514 clear_winopt(&curwin->w_onebuf_opt);
2515#ifdef FEAT_FOLDING
2516 clearFolding(curwin);
2517#endif
2518
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002519 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 if (wip != NULL && wip->wi_optset)
2521 {
2522 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2523#ifdef FEAT_FOLDING
2524 curwin->w_fold_manual = wip->wi_fold_manual;
2525 curwin->w_foldinvalid = TRUE;
2526 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2527#endif
2528 }
2529 else
2530 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2531
2532#ifdef FEAT_FOLDING
2533 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2534 if (p_fdls >= 0)
2535 curwin->w_p_fdl = p_fdls;
2536#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002537#ifdef FEAT_SYN_HL
2538 check_colorcolumn(curwin);
2539#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540}
2541
2542/*
2543 * Find the position (lnum and col) for the buffer 'buf' for the current
2544 * window.
2545 * Returns a pointer to no_position if no position is found.
2546 */
2547 pos_T *
2548buflist_findfpos(buf)
2549 buf_T *buf;
2550{
2551 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002552 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002554 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 if (wip != NULL)
2556 return &(wip->wi_fpos);
2557 else
2558 return &no_position;
2559}
2560
2561/*
2562 * Find the lnum for the buffer 'buf' for the current window.
2563 */
2564 linenr_T
2565buflist_findlnum(buf)
2566 buf_T *buf;
2567{
2568 return buflist_findfpos(buf)->lnum;
2569}
2570
2571#if defined(FEAT_LISTCMDS) || defined(PROTO)
2572/*
2573 * List all know file names (for :files and :buffers command).
2574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 void
2576buflist_list(eap)
2577 exarg_T *eap;
2578{
2579 buf_T *buf;
2580 int len;
2581 int i;
2582
2583 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2584 {
2585 /* skip unlisted buffers, unless ! was used */
2586 if (!buf->b_p_bl && !eap->forceit)
2587 continue;
2588 msg_putchar('\n');
2589 if (buf_spname(buf) != NULL)
2590 STRCPY(NameBuff, buf_spname(buf));
2591 else
2592 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2593
Bram Moolenaar50cde822005-06-05 21:54:54 +00002594 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 buf->b_fnum,
2596 buf->b_p_bl ? ' ' : 'u',
2597 buf == curbuf ? '%' :
2598 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2599 buf->b_ml.ml_mfp == NULL ? ' ' :
2600 (buf->b_nwindows == 0 ? 'h' : 'a'),
2601 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2602 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002603 : (bufIsChanged(buf) ? '+' : ' '),
2604 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002605
2606 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 i = 40 - vim_strsize(IObuff);
2608 do
2609 {
2610 IObuff[len++] = ' ';
2611 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002612 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2613 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002614 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 msg_outtrans(IObuff);
2616 out_flush(); /* output one line at a time */
2617 ui_breakcheck();
2618 }
2619}
2620#endif
2621
2622/*
2623 * Get file name and line number for file 'fnum'.
2624 * Used by DoOneCmd() for translating '%' and '#'.
2625 * Used by insert_reg() and cmdline_paste() for '#' register.
2626 * Return FAIL if not found, OK for success.
2627 */
2628 int
2629buflist_name_nr(fnum, fname, lnum)
2630 int fnum;
2631 char_u **fname;
2632 linenr_T *lnum;
2633{
2634 buf_T *buf;
2635
2636 buf = buflist_findnr(fnum);
2637 if (buf == NULL || buf->b_fname == NULL)
2638 return FAIL;
2639
2640 *fname = buf->b_fname;
2641 *lnum = buflist_findlnum(buf);
2642
2643 return OK;
2644}
2645
2646/*
2647 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2648 * The file name with the full path is also remembered, for when :cd is used.
2649 * Returns FAIL for failure (file name already in use by other buffer)
2650 * OK otherwise.
2651 */
2652 int
2653setfname(buf, ffname, sfname, message)
2654 buf_T *buf;
2655 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002656 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002657{
Bram Moolenaar81695252004-12-29 20:58:21 +00002658 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659#ifdef UNIX
2660 struct stat st;
2661#endif
2662
2663 if (ffname == NULL || *ffname == NUL)
2664 {
2665 /* Removing the name. */
2666 vim_free(buf->b_ffname);
2667 vim_free(buf->b_sfname);
2668 buf->b_ffname = NULL;
2669 buf->b_sfname = NULL;
2670#ifdef UNIX
2671 st.st_dev = (dev_T)-1;
2672#endif
2673 }
2674 else
2675 {
2676 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2677 if (ffname == NULL) /* out of memory */
2678 return FAIL;
2679
2680 /*
2681 * if the file name is already used in another buffer:
2682 * - if the buffer is loaded, fail
2683 * - if the buffer is not loaded, delete it from the list
2684 */
2685#ifdef UNIX
2686 if (mch_stat((char *)ffname, &st) < 0)
2687 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002688#endif
2689 if (!(buf->b_flags & BF_DUMMY))
2690#ifdef UNIX
2691 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002693 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694#endif
2695 if (obuf != NULL && obuf != buf)
2696 {
2697 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2698 {
2699 if (message)
2700 EMSG(_("E95: Buffer with this name already exists"));
2701 vim_free(ffname);
2702 return FAIL;
2703 }
2704 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2705 }
2706 sfname = vim_strsave(sfname);
2707 if (ffname == NULL || sfname == NULL)
2708 {
2709 vim_free(sfname);
2710 vim_free(ffname);
2711 return FAIL;
2712 }
2713#ifdef USE_FNAME_CASE
2714# ifdef USE_LONG_FNAME
2715 if (USE_LONG_FNAME)
2716# endif
2717 fname_case(sfname, 0); /* set correct case for short file name */
2718#endif
2719 vim_free(buf->b_ffname);
2720 vim_free(buf->b_sfname);
2721 buf->b_ffname = ffname;
2722 buf->b_sfname = sfname;
2723 }
2724 buf->b_fname = buf->b_sfname;
2725#ifdef UNIX
2726 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002727 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 else
2729 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002730 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 buf->b_dev = st.st_dev;
2732 buf->b_ino = st.st_ino;
2733 }
2734#endif
2735
2736#ifndef SHORT_FNAME
2737 buf->b_shortname = FALSE;
2738#endif
2739
2740 buf_name_changed(buf);
2741 return OK;
2742}
2743
2744/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002745 * Crude way of changing the name of a buffer. Use with care!
2746 * The name should be relative to the current directory.
2747 */
2748 void
2749buf_set_name(fnum, name)
2750 int fnum;
2751 char_u *name;
2752{
2753 buf_T *buf;
2754
2755 buf = buflist_findnr(fnum);
2756 if (buf != NULL)
2757 {
2758 vim_free(buf->b_sfname);
2759 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002760 buf->b_ffname = vim_strsave(name);
2761 buf->b_sfname = NULL;
2762 /* Allocate ffname and expand into full path. Also resolves .lnk
2763 * files on Win32. */
2764 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002765 buf->b_fname = buf->b_sfname;
2766 }
2767}
2768
2769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002770 * Take care of what needs to be done when the name of buffer "buf" has
2771 * changed.
2772 */
2773 void
2774buf_name_changed(buf)
2775 buf_T *buf;
2776{
2777 /*
2778 * If the file name changed, also change the name of the swapfile
2779 */
2780 if (buf->b_ml.ml_mfp != NULL)
2781 ml_setname(buf);
2782
2783 if (curwin->w_buffer == buf)
2784 check_arg_idx(curwin); /* check file name for arg list */
2785#ifdef FEAT_TITLE
2786 maketitle(); /* set window title */
2787#endif
2788#ifdef FEAT_WINDOWS
2789 status_redraw_all(); /* status lines need to be redrawn */
2790#endif
2791 fmarks_check_names(buf); /* check named file marks */
2792 ml_timestamp(buf); /* reset timestamp */
2793}
2794
2795/*
2796 * set alternate file name for current window
2797 *
2798 * Used by do_one_cmd(), do_write() and do_ecmd().
2799 * Return the buffer.
2800 */
2801 buf_T *
2802setaltfname(ffname, sfname, lnum)
2803 char_u *ffname;
2804 char_u *sfname;
2805 linenr_T lnum;
2806{
2807 buf_T *buf;
2808
2809 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2810 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002811 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812 curwin->w_alt_fnum = buf->b_fnum;
2813 return buf;
2814}
2815
2816/*
2817 * Get alternate file name for current window.
2818 * Return NULL if there isn't any, and give error message if requested.
2819 */
2820 char_u *
2821getaltfname(errmsg)
2822 int errmsg; /* give error message */
2823{
2824 char_u *fname;
2825 linenr_T dummy;
2826
2827 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2828 {
2829 if (errmsg)
2830 EMSG(_(e_noalt));
2831 return NULL;
2832 }
2833 return fname;
2834}
2835
2836/*
2837 * Add a file name to the buflist and return its number.
2838 * Uses same flags as buflist_new(), except BLN_DUMMY.
2839 *
2840 * used by qf_init(), main() and doarglist()
2841 */
2842 int
2843buflist_add(fname, flags)
2844 char_u *fname;
2845 int flags;
2846{
2847 buf_T *buf;
2848
2849 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2850 if (buf != NULL)
2851 return buf->b_fnum;
2852 return 0;
2853}
2854
2855#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2856/*
2857 * Adjust slashes in file names. Called after 'shellslash' was set.
2858 */
2859 void
2860buflist_slash_adjust()
2861{
2862 buf_T *bp;
2863
2864 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2865 {
2866 if (bp->b_ffname != NULL)
2867 slash_adjust(bp->b_ffname);
2868 if (bp->b_sfname != NULL)
2869 slash_adjust(bp->b_sfname);
2870 }
2871}
2872#endif
2873
2874/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002875 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002876 * Also save the local window option values.
2877 */
2878 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002879buflist_altfpos(win)
2880 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002881{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002882 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883}
2884
2885/*
2886 * Return TRUE if 'ffname' is not the same file as current file.
2887 * Fname must have a full path (expanded by mch_FullName()).
2888 */
2889 int
2890otherfile(ffname)
2891 char_u *ffname;
2892{
2893 return otherfile_buf(curbuf, ffname
2894#ifdef UNIX
2895 , NULL
2896#endif
2897 );
2898}
2899
2900 static int
2901otherfile_buf(buf, ffname
2902#ifdef UNIX
2903 , stp
2904#endif
2905 )
2906 buf_T *buf;
2907 char_u *ffname;
2908#ifdef UNIX
2909 struct stat *stp;
2910#endif
2911{
2912 /* no name is different */
2913 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2914 return TRUE;
2915 if (fnamecmp(ffname, buf->b_ffname) == 0)
2916 return FALSE;
2917#ifdef UNIX
2918 {
2919 struct stat st;
2920
2921 /* If no struct stat given, get it now */
2922 if (stp == NULL)
2923 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002924 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 st.st_dev = (dev_T)-1;
2926 stp = &st;
2927 }
2928 /* Use dev/ino to check if the files are the same, even when the names
2929 * are different (possible with links). Still need to compare the
2930 * name above, for when the file doesn't exist yet.
2931 * Problem: The dev/ino changes when a file is deleted (and created
2932 * again) and remains the same when renamed/moved. We don't want to
2933 * mch_stat() each buffer each time, that would be too slow. Get the
2934 * dev/ino again when they appear to match, but not when they appear
2935 * to be different: Could skip a buffer when it's actually the same
2936 * file. */
2937 if (buf_same_ino(buf, stp))
2938 {
2939 buf_setino(buf);
2940 if (buf_same_ino(buf, stp))
2941 return FALSE;
2942 }
2943 }
2944#endif
2945 return TRUE;
2946}
2947
2948#if defined(UNIX) || defined(PROTO)
2949/*
2950 * Set inode and device number for a buffer.
2951 * Must always be called when b_fname is changed!.
2952 */
2953 void
2954buf_setino(buf)
2955 buf_T *buf;
2956{
2957 struct stat st;
2958
2959 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2960 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002961 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002962 buf->b_dev = st.st_dev;
2963 buf->b_ino = st.st_ino;
2964 }
2965 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002966 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967}
2968
2969/*
2970 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2971 */
2972 static int
2973buf_same_ino(buf, stp)
2974 buf_T *buf;
2975 struct stat *stp;
2976{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002977 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 && stp->st_dev == buf->b_dev
2979 && stp->st_ino == buf->b_ino);
2980}
2981#endif
2982
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002983/*
2984 * Print info about the current buffer.
2985 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 void
2987fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002988 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 int shorthelp;
2990 int dont_truncate;
2991{
2992 char_u *name;
2993 int n;
2994 char_u *p;
2995 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002996 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997
2998 buffer = alloc(IOSIZE);
2999 if (buffer == NULL)
3000 return;
3001
3002 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3003 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003004 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 p = buffer + STRLEN(buffer);
3006 }
3007 else
3008 p = buffer;
3009
3010 *p++ = '"';
3011 if (buf_spname(curbuf) != NULL)
3012 STRCPY(p, buf_spname(curbuf));
3013 else
3014 {
3015 if (!fullname && curbuf->b_fname != NULL)
3016 name = curbuf->b_fname;
3017 else
3018 name = curbuf->b_ffname;
3019 home_replace(shorthelp ? curbuf : NULL, name, p,
3020 (int)(IOSIZE - (p - buffer)), TRUE);
3021 }
3022
Bram Moolenaara800b422010-06-27 01:15:55 +02003023 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 curbufIsChanged() ? (shortmess(SHM_MOD)
3025 ? " [+]" : _(" [Modified]")) : " ",
3026 (curbuf->b_flags & BF_NOTEDITED)
3027#ifdef FEAT_QUICKFIX
3028 && !bt_dontwrite(curbuf)
3029#endif
3030 ? _("[Not edited]") : "",
3031 (curbuf->b_flags & BF_NEW)
3032#ifdef FEAT_QUICKFIX
3033 && !bt_dontwrite(curbuf)
3034#endif
3035 ? _("[New file]") : "",
3036 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3037 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3038 : _("[readonly]")) : "",
3039 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3040 || curbuf->b_p_ro) ?
3041 " " : "");
3042 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3043 * causes an overflow, thus for large numbers divide instead. */
3044 if (curwin->w_cursor.lnum > 1000000L)
3045 n = (int)(((long)curwin->w_cursor.lnum) /
3046 ((long)curbuf->b_ml.ml_line_count / 100L));
3047 else
3048 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3049 (long)curbuf->b_ml.ml_line_count);
3050 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3051 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003052 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 }
3054#ifdef FEAT_CMDL_INFO
3055 else if (p_ru)
3056 {
3057 /* Current line and column are already on the screen -- webb */
3058 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003059 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003061 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 (long)curbuf->b_ml.ml_line_count, n);
3063 }
3064#endif
3065 else
3066 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003067 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 _("line %ld of %ld --%d%%-- col "),
3069 (long)curwin->w_cursor.lnum,
3070 (long)curbuf->b_ml.ml_line_count,
3071 n);
3072 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003073 len = STRLEN(buffer);
3074 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3076 }
3077
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003078 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079
3080 if (dont_truncate)
3081 {
3082 /* Temporarily set msg_scroll to avoid the message being truncated.
3083 * First call msg_start() to get the message in the right place. */
3084 msg_start();
3085 n = msg_scroll;
3086 msg_scroll = TRUE;
3087 msg(buffer);
3088 msg_scroll = n;
3089 }
3090 else
3091 {
3092 p = msg_trunc_attr(buffer, FALSE, 0);
3093 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 /* Need to repeat the message after redrawing when:
3095 * - When restart_edit is set (otherwise there will be a delay
3096 * before redrawing).
3097 * - When the screen was scrolled but there is no wait-return
3098 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003099 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 }
3101
3102 vim_free(buffer);
3103}
3104
3105 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003106col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003108 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 int col;
3110 int vcol;
3111{
3112 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003113 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003115 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116}
3117
3118#if defined(FEAT_TITLE) || defined(PROTO)
3119/*
3120 * put file name in title bar of window and in icon title
3121 */
3122
3123static char_u *lasttitle = NULL;
3124static char_u *lasticon = NULL;
3125
3126 void
3127maketitle()
3128{
3129 char_u *p;
3130 char_u *t_str = NULL;
3131 char_u *i_name;
3132 char_u *i_str = NULL;
3133 int maxlen = 0;
3134 int len;
3135 int mustset;
3136 char_u buf[IOSIZE];
3137 int off;
3138
3139 if (!redrawing())
3140 {
3141 /* Postpone updating the title when 'lazyredraw' is set. */
3142 need_maketitle = TRUE;
3143 return;
3144 }
3145
3146 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003147 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003148 return;
3149
3150 if (p_title)
3151 {
3152 if (p_titlelen > 0)
3153 {
3154 maxlen = p_titlelen * Columns / 100;
3155 if (maxlen < 10)
3156 maxlen = 10;
3157 }
3158
3159 t_str = buf;
3160 if (*p_titlestring != NUL)
3161 {
3162#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003163 if (stl_syntax & STL_IN_TITLE)
3164 {
3165 int use_sandbox = FALSE;
3166 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003167
3168# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003169 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003170# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003171 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003173 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003174 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003175 if (called_emsg)
3176 set_string_option_direct((char_u *)"titlestring", -1,
3177 (char_u *)"", OPT_FREE, SID_ERROR);
3178 called_emsg |= save_called_emsg;
3179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 else
3181#endif
3182 t_str = p_titlestring;
3183 }
3184 else
3185 {
3186 /* format: "fname + (path) (1 of 2) - VIM" */
3187
3188 if (curbuf->b_fname == NULL)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003189 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
3191 {
3192 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003193 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003195 }
3196
3197 switch (bufIsChanged(curbuf)
3198 + (curbuf->b_p_ro * 2)
3199 + (!curbuf->b_p_ma * 4))
3200 {
3201 case 1: STRCAT(buf, " +"); break;
3202 case 2: STRCAT(buf, " ="); break;
3203 case 3: STRCAT(buf, " =+"); break;
3204 case 4:
3205 case 6: STRCAT(buf, " -"); break;
3206 case 5:
3207 case 7: STRCAT(buf, " -+"); break;
3208 }
3209
3210 if (curbuf->b_fname != NULL)
3211 {
3212 /* Get path of file, replace home dir with ~ */
3213 off = (int)STRLEN(buf);
3214 buf[off++] = ' ';
3215 buf[off++] = '(';
3216 home_replace(curbuf, curbuf->b_ffname,
3217 buf + off, IOSIZE - off, TRUE);
3218#ifdef BACKSLASH_IN_FILENAME
3219 /* avoid "c:/name" to be reduced to "c" */
3220 if (isalpha(buf[off]) && buf[off + 1] == ':')
3221 off += 2;
3222#endif
3223 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003224 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003227 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003228 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003231
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 /* translate unprintable chars */
3233 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003234 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 STRCAT(buf, ")");
3237 }
3238
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003239 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240
3241#if defined(FEAT_CLIENTSERVER)
3242 if (serverName != NULL)
3243 {
3244 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003245 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 }
3247 else
3248#endif
3249 STRCAT(buf, " - VIM");
3250
3251 if (maxlen > 0)
3252 {
3253 /* make it shorter by removing a bit in the middle */
3254 len = vim_strsize(buf);
3255 if (len > maxlen)
3256 trunc_string(buf, buf, maxlen);
3257 }
3258 }
3259 }
3260 mustset = ti_change(t_str, &lasttitle);
3261
3262 if (p_icon)
3263 {
3264 i_str = buf;
3265 if (*p_iconstring != NUL)
3266 {
3267#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003268 if (stl_syntax & STL_IN_ICON)
3269 {
3270 int use_sandbox = FALSE;
3271 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003272
3273# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003274 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003275# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003276 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003278 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003279 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003280 if (called_emsg)
3281 set_string_option_direct((char_u *)"iconstring", -1,
3282 (char_u *)"", OPT_FREE, SID_ERROR);
3283 called_emsg |= save_called_emsg;
3284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 else
3286#endif
3287 i_str = p_iconstring;
3288 }
3289 else
3290 {
3291 if (buf_spname(curbuf) != NULL)
3292 i_name = (char_u *)buf_spname(curbuf);
3293 else /* use file name only in icon */
3294 i_name = gettail(curbuf->b_ffname);
3295 *i_str = NUL;
3296 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003297 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 if (len > 100)
3299 {
3300 len -= 100;
3301#ifdef FEAT_MBYTE
3302 if (has_mbyte)
3303 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3304#endif
3305 i_name += len;
3306 }
3307 STRCPY(i_str, i_name);
3308 trans_characters(i_str, IOSIZE);
3309 }
3310 }
3311
3312 mustset |= ti_change(i_str, &lasticon);
3313
3314 if (mustset)
3315 resettitle();
3316}
3317
3318/*
3319 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3320 * from "str" if it does.
3321 * Return TRUE when "*last" changed.
3322 */
3323 static int
3324ti_change(str, last)
3325 char_u *str;
3326 char_u **last;
3327{
3328 if ((str == NULL) != (*last == NULL)
3329 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3330 {
3331 vim_free(*last);
3332 if (str == NULL)
3333 *last = NULL;
3334 else
3335 *last = vim_strsave(str);
3336 return TRUE;
3337 }
3338 return FALSE;
3339}
3340
3341/*
3342 * Put current window title back (used after calling a shell)
3343 */
3344 void
3345resettitle()
3346{
3347 mch_settitle(lasttitle, lasticon);
3348}
Bram Moolenaarea408852005-06-25 22:49:46 +00003349
3350# if defined(EXITFREE) || defined(PROTO)
3351 void
3352free_titles()
3353{
3354 vim_free(lasttitle);
3355 vim_free(lasticon);
3356}
3357# endif
3358
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359#endif /* FEAT_TITLE */
3360
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003361#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003363 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 * Return length of string in screen cells.
3365 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003366 * Normally works for window "wp", except when working for 'tabline' then it
3367 * is "curwin".
3368 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 * Items are drawn interspersed with the text that surrounds it
3370 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3371 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3372 *
3373 * If maxwidth is not zero, the string will be filled at any middle marker
3374 * or truncated if too long, fillchar is used for all whitespace.
3375 */
3376 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003377build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3378 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003380 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 size_t outlen; /* length of out[] */
3382 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003383 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 int fillchar;
3385 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003386 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3387 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388{
3389 char_u *p;
3390 char_u *s;
3391 char_u *t;
3392 char_u *linecont;
3393#ifdef FEAT_EVAL
3394 win_T *o_curwin;
3395 buf_T *o_curbuf;
3396#endif
3397 int empty_line;
3398 colnr_T virtcol;
3399 long l;
3400 long n;
3401 int prevchar_isflag;
3402 int prevchar_isitem;
3403 int itemisflag;
3404 int fillable;
3405 char_u *str;
3406 long num;
3407 int width;
3408 int itemcnt;
3409 int curitem;
3410 int groupitem[STL_MAX_ITEM];
3411 int groupdepth;
3412 struct stl_item
3413 {
3414 char_u *start;
3415 int minwid;
3416 int maxwid;
3417 enum
3418 {
3419 Normal,
3420 Empty,
3421 Group,
3422 Middle,
3423 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003424 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425 Trunc
3426 } type;
3427 } item[STL_MAX_ITEM];
3428 int minwid;
3429 int maxwid;
3430 int zeropad;
3431 char_u base;
3432 char_u opt;
3433#define TMPLEN 70
3434 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003435 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003436 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003437
3438#ifdef FEAT_EVAL
3439 /*
3440 * When the format starts with "%!" then evaluate it as an expression and
3441 * use the result as the actual format string.
3442 */
3443 if (fmt[0] == '%' && fmt[1] == '!')
3444 {
3445 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3446 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003447 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003448 }
3449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450
3451 if (fillchar == 0)
3452 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003453#ifdef FEAT_MBYTE
3454 /* Can't handle a multi-byte fill character yet. */
3455 else if (mb_char2len(fillchar) > 1)
3456 fillchar = '-';
3457#endif
3458
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 /*
3460 * Get line & check if empty (cursorpos will show "0-1").
3461 * If inversion is possible we use it. Else '=' characters are used.
3462 */
3463 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3464 empty_line = (*linecont == NUL);
3465
3466 groupdepth = 0;
3467 p = out;
3468 curitem = 0;
3469 prevchar_isflag = TRUE;
3470 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003471 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003473 if (curitem == STL_MAX_ITEM)
3474 {
3475 /* There are too many items. Add the error code to the statusline
3476 * to give the user a hint about what went wrong. */
3477 if (p + 6 < out + outlen)
3478 {
3479 mch_memmove(p, " E541", (size_t)5);
3480 p += 5;
3481 }
3482 break;
3483 }
3484
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485 if (*s != NUL && *s != '%')
3486 prevchar_isflag = prevchar_isitem = FALSE;
3487
3488 /*
3489 * Handle up to the next '%' or the end.
3490 */
3491 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3492 *p++ = *s++;
3493 if (*s == NUL || p + 1 >= out + outlen)
3494 break;
3495
3496 /*
3497 * Handle one '%' item.
3498 */
3499 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003500 if (*s == NUL) /* ignore trailing % */
3501 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502 if (*s == '%')
3503 {
3504 if (p + 1 >= out + outlen)
3505 break;
3506 *p++ = *s++;
3507 prevchar_isflag = prevchar_isitem = FALSE;
3508 continue;
3509 }
3510 if (*s == STL_MIDDLEMARK)
3511 {
3512 s++;
3513 if (groupdepth > 0)
3514 continue;
3515 item[curitem].type = Middle;
3516 item[curitem++].start = p;
3517 continue;
3518 }
3519 if (*s == STL_TRUNCMARK)
3520 {
3521 s++;
3522 item[curitem].type = Trunc;
3523 item[curitem++].start = p;
3524 continue;
3525 }
3526 if (*s == ')')
3527 {
3528 s++;
3529 if (groupdepth < 1)
3530 continue;
3531 groupdepth--;
3532
3533 t = item[groupitem[groupdepth]].start;
3534 *p = NUL;
3535 l = vim_strsize(t);
3536 if (curitem > groupitem[groupdepth] + 1
3537 && item[groupitem[groupdepth]].minwid == 0)
3538 {
3539 /* remove group if all items are empty */
3540 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3541 if (item[n].type == Normal)
3542 break;
3543 if (n == curitem)
3544 {
3545 p = t;
3546 l = 0;
3547 }
3548 }
3549 if (l > item[groupitem[groupdepth]].maxwid)
3550 {
3551 /* truncate, remove n bytes of text at the start */
3552#ifdef FEAT_MBYTE
3553 if (has_mbyte)
3554 {
3555 /* Find the first character that should be included. */
3556 n = 0;
3557 while (l >= item[groupitem[groupdepth]].maxwid)
3558 {
3559 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003560 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 }
3562 }
3563 else
3564#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003565 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003566
3567 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003568 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 p = p - n + 1;
3570#ifdef FEAT_MBYTE
3571 /* Fill up space left over by half a double-wide char. */
3572 while (++l < item[groupitem[groupdepth]].minwid)
3573 *p++ = fillchar;
3574#endif
3575
3576 /* correct the start of the items for the truncation */
3577 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3578 {
3579 item[l].start -= n;
3580 if (item[l].start < t)
3581 item[l].start = t;
3582 }
3583 }
3584 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3585 {
3586 /* fill */
3587 n = item[groupitem[groupdepth]].minwid;
3588 if (n < 0)
3589 {
3590 /* fill by appending characters */
3591 n = 0 - n;
3592 while (l++ < n && p + 1 < out + outlen)
3593 *p++ = fillchar;
3594 }
3595 else
3596 {
3597 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003598 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 l = n - l;
3600 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003601 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 p += l;
3603 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3604 item[n].start += l;
3605 for ( ; l > 0; l--)
3606 *t++ = fillchar;
3607 }
3608 }
3609 continue;
3610 }
3611 minwid = 0;
3612 maxwid = 9999;
3613 zeropad = FALSE;
3614 l = 1;
3615 if (*s == '0')
3616 {
3617 s++;
3618 zeropad = TRUE;
3619 }
3620 if (*s == '-')
3621 {
3622 s++;
3623 l = -1;
3624 }
3625 if (VIM_ISDIGIT(*s))
3626 {
3627 minwid = (int)getdigits(&s);
3628 if (minwid < 0) /* overflow */
3629 minwid = 0;
3630 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003631 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 {
3633 item[curitem].type = Highlight;
3634 item[curitem].start = p;
3635 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3636 s++;
3637 curitem++;
3638 continue;
3639 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003640 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3641 {
3642 if (*s == STL_TABCLOSENR)
3643 {
3644 if (minwid == 0)
3645 {
3646 /* %X ends the close label, go back to the previously
3647 * define tab label nr. */
3648 for (n = curitem - 1; n >= 0; --n)
3649 if (item[n].type == TabPage && item[n].minwid >= 0)
3650 {
3651 minwid = item[n].minwid;
3652 break;
3653 }
3654 }
3655 else
3656 /* close nrs are stored as negative values */
3657 minwid = - minwid;
3658 }
3659 item[curitem].type = TabPage;
3660 item[curitem].start = p;
3661 item[curitem].minwid = minwid;
3662 s++;
3663 curitem++;
3664 continue;
3665 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003666 if (*s == '.')
3667 {
3668 s++;
3669 if (VIM_ISDIGIT(*s))
3670 {
3671 maxwid = (int)getdigits(&s);
3672 if (maxwid <= 0) /* overflow */
3673 maxwid = 50;
3674 }
3675 }
3676 minwid = (minwid > 50 ? 50 : minwid) * l;
3677 if (*s == '(')
3678 {
3679 groupitem[groupdepth++] = curitem;
3680 item[curitem].type = Group;
3681 item[curitem].start = p;
3682 item[curitem].minwid = minwid;
3683 item[curitem].maxwid = maxwid;
3684 s++;
3685 curitem++;
3686 continue;
3687 }
3688 if (vim_strchr(STL_ALL, *s) == NULL)
3689 {
3690 s++;
3691 continue;
3692 }
3693 opt = *s++;
3694
3695 /* OK - now for the real work */
3696 base = 'D';
3697 itemisflag = FALSE;
3698 fillable = TRUE;
3699 num = -1;
3700 str = NULL;
3701 switch (opt)
3702 {
3703 case STL_FILEPATH:
3704 case STL_FULLPATH:
3705 case STL_FILENAME:
3706 fillable = FALSE; /* don't change ' ' to fillchar */
3707 if (buf_spname(wp->w_buffer) != NULL)
3708 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3709 else
3710 {
3711 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003712 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003713 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3714 }
3715 trans_characters(NameBuff, MAXPATHL);
3716 if (opt != STL_FILENAME)
3717 str = NameBuff;
3718 else
3719 str = gettail(NameBuff);
3720 break;
3721
3722 case STL_VIM_EXPR: /* '{' */
3723 itemisflag = TRUE;
3724 t = p;
3725 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3726 *p++ = *s++;
3727 if (*s != '}') /* missing '}' or out of space */
3728 break;
3729 s++;
3730 *p = 0;
3731 p = t;
3732
3733#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003734 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3736
3737 o_curbuf = curbuf;
3738 o_curwin = curwin;
3739 curwin = wp;
3740 curbuf = wp->w_buffer;
3741
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003742 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
3744 curwin = o_curwin;
3745 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003746 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747
3748 if (str != NULL && *str != 0)
3749 {
3750 if (*skipdigits(str) == NUL)
3751 {
3752 num = atoi((char *)str);
3753 vim_free(str);
3754 str = NULL;
3755 itemisflag = FALSE;
3756 }
3757 }
3758#endif
3759 break;
3760
3761 case STL_LINE:
3762 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3763 ? 0L : (long)(wp->w_cursor.lnum);
3764 break;
3765
3766 case STL_NUMLINES:
3767 num = wp->w_buffer->b_ml.ml_line_count;
3768 break;
3769
3770 case STL_COLUMN:
3771 num = !(State & INSERT) && empty_line
3772 ? 0 : (int)wp->w_cursor.col + 1;
3773 break;
3774
3775 case STL_VIRTCOL:
3776 case STL_VIRTCOL_ALT:
3777 /* In list mode virtcol needs to be recomputed */
3778 virtcol = wp->w_virtcol;
3779 if (wp->w_p_list && lcs_tab1 == NUL)
3780 {
3781 wp->w_p_list = FALSE;
3782 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3783 wp->w_p_list = TRUE;
3784 }
3785 ++virtcol;
3786 /* Don't display %V if it's the same as %c. */
3787 if (opt == STL_VIRTCOL_ALT
3788 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3789 ? 0 : (int)wp->w_cursor.col + 1)))
3790 break;
3791 num = (long)virtcol;
3792 break;
3793
3794 case STL_PERCENTAGE:
3795 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3796 (long)wp->w_buffer->b_ml.ml_line_count);
3797 break;
3798
3799 case STL_ALTPERCENT:
3800 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003801 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 break;
3803
3804 case STL_ARGLISTSTAT:
3805 fillable = FALSE;
3806 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003807 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 str = tmp;
3809 break;
3810
3811 case STL_KEYMAP:
3812 fillable = FALSE;
3813 if (get_keymap_str(wp, tmp, TMPLEN))
3814 str = tmp;
3815 break;
3816 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003817#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003818 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819#else
3820 num = 0;
3821#endif
3822 break;
3823
3824 case STL_BUFNO:
3825 num = wp->w_buffer->b_fnum;
3826 break;
3827
3828 case STL_OFFSET_X:
3829 base = 'X';
3830 case STL_OFFSET:
3831#ifdef FEAT_BYTEOFF
3832 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3833 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3834 0L : l + 1 + (!(State & INSERT) && empty_line ?
3835 0 : (int)wp->w_cursor.col);
3836#endif
3837 break;
3838
3839 case STL_BYTEVAL_X:
3840 base = 'X';
3841 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003842 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 num = 0;
3844 else
3845 {
3846#ifdef FEAT_MBYTE
3847 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3848#else
3849 num = linecont[wp->w_cursor.col];
3850#endif
3851 }
3852 if (num == NL)
3853 num = 0;
3854 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3855 num = NL;
3856 break;
3857
3858 case STL_ROFLAG:
3859 case STL_ROFLAG_ALT:
3860 itemisflag = TRUE;
3861 if (wp->w_buffer->b_p_ro)
3862 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3863 break;
3864
3865 case STL_HELPFLAG:
3866 case STL_HELPFLAG_ALT:
3867 itemisflag = TRUE;
3868 if (wp->w_buffer->b_help)
3869 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003870 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 break;
3872
3873#ifdef FEAT_AUTOCMD
3874 case STL_FILETYPE:
3875 if (*wp->w_buffer->b_p_ft != NUL
3876 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3877 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003878 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3879 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 str = tmp;
3881 }
3882 break;
3883
3884 case STL_FILETYPE_ALT:
3885 itemisflag = TRUE;
3886 if (*wp->w_buffer->b_p_ft != NUL
3887 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3888 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003889 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3890 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 for (t = tmp; *t != 0; t++)
3892 *t = TOUPPER_LOC(*t);
3893 str = tmp;
3894 }
3895 break;
3896#endif
3897
3898#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3899 case STL_PREVIEWFLAG:
3900 case STL_PREVIEWFLAG_ALT:
3901 itemisflag = TRUE;
3902 if (wp->w_p_pvw)
3903 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3904 : _("[Preview]"));
3905 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003906
3907 case STL_QUICKFIX:
3908 if (bt_quickfix(wp->w_buffer))
3909 str = (char_u *)(wp->w_llist_ref
3910 ? _(msg_loclist)
3911 : _(msg_qflist));
3912 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913#endif
3914
3915 case STL_MODIFIED:
3916 case STL_MODIFIED_ALT:
3917 itemisflag = TRUE;
3918 switch ((opt == STL_MODIFIED_ALT)
3919 + bufIsChanged(wp->w_buffer) * 2
3920 + (!wp->w_buffer->b_p_ma) * 4)
3921 {
3922 case 2: str = (char_u *)"[+]"; break;
3923 case 3: str = (char_u *)",+"; break;
3924 case 4: str = (char_u *)"[-]"; break;
3925 case 5: str = (char_u *)",-"; break;
3926 case 6: str = (char_u *)"[+-]"; break;
3927 case 7: str = (char_u *)",+-"; break;
3928 }
3929 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003930
3931 case STL_HIGHLIGHT:
3932 t = s;
3933 while (*s != '#' && *s != NUL)
3934 ++s;
3935 if (*s == '#')
3936 {
3937 item[curitem].type = Highlight;
3938 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003939 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003940 curitem++;
3941 }
3942 ++s;
3943 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 }
3945
3946 item[curitem].start = p;
3947 item[curitem].type = Normal;
3948 if (str != NULL && *str)
3949 {
3950 t = str;
3951 if (itemisflag)
3952 {
3953 if ((t[0] && t[1])
3954 && ((!prevchar_isitem && *t == ',')
3955 || (prevchar_isflag && *t == ' ')))
3956 t++;
3957 prevchar_isflag = TRUE;
3958 }
3959 l = vim_strsize(t);
3960 if (l > 0)
3961 prevchar_isitem = TRUE;
3962 if (l > maxwid)
3963 {
3964 while (l >= maxwid)
3965#ifdef FEAT_MBYTE
3966 if (has_mbyte)
3967 {
3968 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003969 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 }
3971 else
3972#endif
3973 l -= byte2cells(*t++);
3974 if (p + 1 >= out + outlen)
3975 break;
3976 *p++ = '<';
3977 }
3978 if (minwid > 0)
3979 {
3980 for (; l < minwid && p + 1 < out + outlen; l++)
3981 {
3982 /* Don't put a "-" in front of a digit. */
3983 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3984 *p++ = ' ';
3985 else
3986 *p++ = fillchar;
3987 }
3988 minwid = 0;
3989 }
3990 else
3991 minwid *= -1;
3992 while (*t && p + 1 < out + outlen)
3993 {
3994 *p++ = *t++;
3995 /* Change a space by fillchar, unless fillchar is '-' and a
3996 * digit follows. */
3997 if (fillable && p[-1] == ' '
3998 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
3999 p[-1] = fillchar;
4000 }
4001 for (; l < minwid && p + 1 < out + outlen; l++)
4002 *p++ = fillchar;
4003 }
4004 else if (num >= 0)
4005 {
4006 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4007 char_u nstr[20];
4008
4009 if (p + 20 >= out + outlen)
4010 break; /* not sufficient space */
4011 prevchar_isitem = TRUE;
4012 t = nstr;
4013 if (opt == STL_VIRTCOL_ALT)
4014 {
4015 *t++ = '-';
4016 minwid--;
4017 }
4018 *t++ = '%';
4019 if (zeropad)
4020 *t++ = '0';
4021 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004022 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 *t = 0;
4024
4025 for (n = num, l = 1; n >= nbase; n /= nbase)
4026 l++;
4027 if (opt == STL_VIRTCOL_ALT)
4028 l++;
4029 if (l > maxwid)
4030 {
4031 l += 2;
4032 n = l - maxwid;
4033 while (l-- > maxwid)
4034 num /= nbase;
4035 *t++ = '>';
4036 *t++ = '%';
4037 *t = t[-3];
4038 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004039 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4040 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 }
4042 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004043 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4044 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 p += STRLEN(p);
4046 }
4047 else
4048 item[curitem].type = Empty;
4049
4050 if (opt == STL_VIM_EXPR)
4051 vim_free(str);
4052
4053 if (num >= 0 || (!itemisflag && str && *str))
4054 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4055 curitem++;
4056 }
4057 *p = NUL;
4058 itemcnt = curitem;
4059
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004060#ifdef FEAT_EVAL
4061 if (usefmt != fmt)
4062 vim_free(usefmt);
4063#endif
4064
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 width = vim_strsize(out);
4066 if (maxwidth > 0 && width > maxwidth)
4067 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004068 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 l = 0;
4070 if (itemcnt == 0)
4071 s = out;
4072 else
4073 {
4074 for ( ; l < itemcnt; l++)
4075 if (item[l].type == Trunc)
4076 {
4077 /* Truncate at %< item. */
4078 s = item[l].start;
4079 break;
4080 }
4081 if (l == itemcnt)
4082 {
4083 /* No %< item, truncate first item. */
4084 s = item[0].start;
4085 l = 0;
4086 }
4087 }
4088
4089 if (width - vim_strsize(s) >= maxwidth)
4090 {
4091 /* Truncation mark is beyond max length */
4092#ifdef FEAT_MBYTE
4093 if (has_mbyte)
4094 {
4095 s = out;
4096 width = 0;
4097 for (;;)
4098 {
4099 width += ptr2cells(s);
4100 if (width >= maxwidth)
4101 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004102 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 }
4104 /* Fill up for half a double-wide character. */
4105 while (++width < maxwidth)
4106 *s++ = fillchar;
4107 }
4108 else
4109#endif
4110 s = out + maxwidth - 1;
4111 for (l = 0; l < itemcnt; l++)
4112 if (item[l].start > s)
4113 break;
4114 itemcnt = l;
4115 *s++ = '>';
4116 *s = 0;
4117 }
4118 else
4119 {
4120#ifdef FEAT_MBYTE
4121 if (has_mbyte)
4122 {
4123 n = 0;
4124 while (width >= maxwidth)
4125 {
4126 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004127 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129 }
4130 else
4131#endif
4132 n = width - maxwidth + 1;
4133 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004134 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 *s = '<';
4136
4137 /* Fill up for half a double-wide character. */
4138 while (++width < maxwidth)
4139 {
4140 s = s + STRLEN(s);
4141 *s++ = fillchar;
4142 *s = NUL;
4143 }
4144
4145 --n; /* count the '<' */
4146 for (; l < itemcnt; l++)
4147 {
4148 if (item[l].start - n >= s)
4149 item[l].start -= n;
4150 else
4151 item[l].start = s;
4152 }
4153 }
4154 width = maxwidth;
4155 }
4156 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4157 {
4158 /* Apply STL_MIDDLE if any */
4159 for (l = 0; l < itemcnt; l++)
4160 if (item[l].type == Middle)
4161 break;
4162 if (l < itemcnt)
4163 {
4164 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004165 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004166 for (s = item[l].start; s < p; s++)
4167 *s = fillchar;
4168 for (l++; l < itemcnt; l++)
4169 item[l].start += maxwidth - width;
4170 width = maxwidth;
4171 }
4172 }
4173
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004174 /* Store the info about highlighting. */
4175 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004177 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 for (l = 0; l < itemcnt; l++)
4179 {
4180 if (item[l].type == Highlight)
4181 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004182 sp->start = item[l].start;
4183 sp->userhl = item[l].minwid;
4184 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 }
4186 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004187 sp->start = NULL;
4188 sp->userhl = 0;
4189 }
4190
4191 /* Store the info about tab pages labels. */
4192 if (tabtab != NULL)
4193 {
4194 sp = tabtab;
4195 for (l = 0; l < itemcnt; l++)
4196 {
4197 if (item[l].type == TabPage)
4198 {
4199 sp->start = item[l].start;
4200 sp->userhl = item[l].minwid;
4201 sp++;
4202 }
4203 }
4204 sp->start = NULL;
4205 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207
4208 return width;
4209}
4210#endif /* FEAT_STL_OPT */
4211
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004212#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4213 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004215 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4216 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 */
4218 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004219get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004221 char_u *buf;
4222 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223{
4224 long above; /* number of lines above window */
4225 long below; /* number of lines below window */
4226
4227 above = wp->w_topline - 1;
4228#ifdef FEAT_DIFF
4229 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4230#endif
4231 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4232 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004233 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4234 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004236 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004238 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 ? (int)(above / ((above + below) / 100L))
4240 : (int)(above * 100L / (above + below)));
4241}
4242#endif
4243
4244/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004245 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 * Return TRUE if it was appended.
4247 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004248 static int
4249append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 win_T *wp;
4251 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004252 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
4255 char_u *p;
4256
4257 if (ARGCOUNT <= 1) /* nothing to do */
4258 return FALSE;
4259
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004260 p = buf + STRLEN(buf); /* go to the end of the buffer */
4261 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 return FALSE;
4263 *p++ = ' ';
4264 *p++ = '(';
4265 if (add_file)
4266 {
4267 STRCPY(p, "file ");
4268 p += 5;
4269 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004270 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4271 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4273 return TRUE;
4274}
4275
4276/*
4277 * If fname is not a full path, make it a full path.
4278 * Returns pointer to allocated memory (NULL for failure).
4279 */
4280 char_u *
4281fix_fname(fname)
4282 char_u *fname;
4283{
4284 /*
4285 * Force expanding the path always for Unix, because symbolic links may
4286 * mess up the full path name, even though it starts with a '/'.
4287 * Also expand when there is ".." in the file name, try to remove it,
4288 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004289 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 * For MS-Windows also expand names like "longna~1" to "longname".
4291 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004292#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 return FullName_save(fname, TRUE);
4294#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004295 if (!vim_isAbsName(fname)
4296 || strstr((char *)fname, "..") != NULL
4297 || strstr((char *)fname, "//") != NULL
4298# ifdef BACKSLASH_IN_FILENAME
4299 || strstr((char *)fname, "\\\\") != NULL
4300# endif
4301# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 )
4305 return FullName_save(fname, FALSE);
4306
4307 fname = vim_strsave(fname);
4308
Bram Moolenaar9b942202007-10-03 12:31:33 +00004309# ifdef USE_FNAME_CASE
4310# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004312# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
4314 if (fname != NULL)
4315 fname_case(fname, 0); /* set correct case for file name */
4316 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004317# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 return fname;
4320#endif
4321}
4322
4323/*
4324 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4325 * "ffname" becomes a pointer to allocated memory (or NULL).
4326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 void
4328fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004329 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 char_u **ffname;
4331 char_u **sfname;
4332{
4333 if (*ffname == NULL) /* if no file name given, nothing to do */
4334 return;
4335 if (*sfname == NULL) /* if no short file name given, use ffname */
4336 *sfname = *ffname;
4337 *ffname = fix_fname(*ffname); /* expand to full path */
4338
4339#ifdef FEAT_SHORTCUT
4340 if (!buf->b_p_bin)
4341 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004342 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343
4344 /* If the file name is a shortcut file, use the file it links to. */
4345 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004346 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 {
4348 vim_free(*ffname);
4349 *ffname = rfname;
4350 *sfname = rfname;
4351 }
4352 }
4353#endif
4354}
4355
4356/*
4357 * Get the file name for an argument list entry.
4358 */
4359 char_u *
4360alist_name(aep)
4361 aentry_T *aep;
4362{
4363 buf_T *bp;
4364
4365 /* Use the name from the associated buffer if it exists. */
4366 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004367 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 return aep->ae_fname;
4369 return bp->b_fname;
4370}
4371
4372#if defined(FEAT_WINDOWS) || defined(PROTO)
4373/*
4374 * do_arg_all(): Open up to 'count' windows, one for each argument.
4375 */
4376 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004377do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 int count;
4379 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004380 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381{
4382 int i;
4383 win_T *wp, *wpnext;
4384 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004385 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 int use_firstwin = FALSE; /* use first window for arglist */
4387 int split_ret = OK;
4388 int p_ea_save;
4389 alist_T *alist; /* argument list to be used */
4390 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004391 tabpage_T *tpnext;
4392 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004393 win_T *new_curwin = NULL;
4394 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
4396 if (ARGCOUNT <= 0)
4397 {
4398 /* Don't give an error message. We don't want it when the ":all"
4399 * command is in the .vimrc. */
4400 return;
4401 }
4402 setpcmark();
4403
4404 opened_len = ARGCOUNT;
4405 opened = alloc_clear((unsigned)opened_len);
4406 if (opened == NULL)
4407 return;
4408
4409#ifdef FEAT_GUI
4410 need_mouse_correct = TRUE;
4411#endif
4412
4413 /*
4414 * Try closing all windows that are not in the argument list.
4415 * Also close windows that are not full width;
4416 * When 'hidden' or "forceit" set the buffer becomes hidden.
4417 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004418 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004420 if (had_tab > 0)
4421 goto_tabpage_tp(first_tabpage);
4422 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004424 tpnext = curtab->tp_next;
4425 for (wp = firstwin; wp != NULL; wp = wpnext)
4426 {
4427 wpnext = wp->w_next;
4428 buf = wp->w_buffer;
4429 if (buf->b_ffname == NULL
4430 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004432 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004434 )
4435 i = ARGCOUNT;
4436 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004438 /* check if the buffer in this window is in the arglist */
4439 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004441 if (ARGLIST[i].ae_fnum == buf->b_fnum
4442 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004443 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004445 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004446 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004447 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004448 if (i == 0)
4449 {
4450 new_curwin = wp;
4451 new_curtab = curtab;
4452 }
4453 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004454 if (wp->w_alist != curwin->w_alist)
4455 {
4456 /* Use the current argument list for all windows
4457 * containing a file from it. */
4458 alist_unlink(wp->w_alist);
4459 wp->w_alist = curwin->w_alist;
4460 ++wp->w_alist->al_refcount;
4461 }
4462 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004466 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004468 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004470 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004471 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004473 /* If the buffer was changed, and we would like to hide it,
4474 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004475 if (!P_HID(buf) && buf->b_nwindows <= 1
4476 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004478 (void)autowrite(buf, FALSE);
4479#ifdef FEAT_AUTOCMD
4480 /* check if autocommands removed the window */
4481 if (!win_valid(wp) || !buf_valid(buf))
4482 {
4483 wpnext = firstwin; /* start all over... */
4484 continue;
4485 }
4486#endif
4487 }
4488#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004489 /* don't close last window */
4490 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004491#endif
4492 use_firstwin = TRUE;
4493#ifdef FEAT_WINDOWS
4494 else
4495 {
4496 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4497# ifdef FEAT_AUTOCMD
4498 /* check if autocommands removed the next window */
4499 if (!win_valid(wpnext))
4500 wpnext = firstwin; /* start all over... */
4501# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 }
4503#endif
4504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004507
4508 /* Without the ":tab" modifier only do the current tab page. */
4509 if (had_tab == 0 || tpnext == NULL)
4510 break;
4511
4512# ifdef FEAT_AUTOCMD
4513 /* check if autocommands removed the next tab page */
4514 if (!valid_tabpage(tpnext))
4515 tpnext = first_tabpage; /* start all over...*/
4516# endif
4517 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 }
4519
4520 /*
4521 * Open a window for files in the argument list that don't have one.
4522 * ARGCOUNT may change while doing this, because of autocommands.
4523 */
4524 if (count > ARGCOUNT || count <= 0)
4525 count = ARGCOUNT;
4526
4527 /* Autocommands may do anything to the argument list. Make sure it's not
4528 * freed while we are working here by "locking" it. We still have to
4529 * watch out for its size to be changed. */
4530 alist = curwin->w_alist;
4531 ++alist->al_refcount;
4532
4533#ifdef FEAT_AUTOCMD
4534 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4535 ++autocmd_no_enter;
4536 ++autocmd_no_leave;
4537#endif
4538 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004539#ifdef FEAT_WINDOWS
4540 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4541 * leaving an empty tab page when executed locally. */
4542 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4543 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4544 use_firstwin = TRUE;
4545#endif
4546
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4548 {
4549 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4550 arg_had_last = TRUE;
4551 if (i < opened_len && opened[i])
4552 {
4553 /* Move the already present window to below the current window */
4554 if (curwin->w_arg_idx != i)
4555 {
4556 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4557 {
4558 if (wpnext->w_arg_idx == i)
4559 {
4560 win_move_after(wpnext, curwin);
4561 break;
4562 }
4563 }
4564 }
4565 }
4566 else if (split_ret == OK)
4567 {
4568 if (!use_firstwin) /* split current window */
4569 {
4570 p_ea_save = p_ea;
4571 p_ea = TRUE; /* use space from all windows */
4572 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4573 p_ea = p_ea_save;
4574 if (split_ret == FAIL)
4575 continue;
4576 }
4577#ifdef FEAT_AUTOCMD
4578 else /* first window: do autocmd for leaving this buffer */
4579 --autocmd_no_leave;
4580#endif
4581
4582 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004583 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 */
4585 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004586 if (i == 0)
4587 {
4588 new_curwin = curwin;
4589 new_curtab = curtab;
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4592 ECMD_ONE,
4593 ((P_HID(curwin->w_buffer)
4594 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004595 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596#ifdef FEAT_AUTOCMD
4597 if (use_firstwin)
4598 ++autocmd_no_leave;
4599#endif
4600 use_firstwin = FALSE;
4601 }
4602 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004603
4604 /* When ":tab" was used open a new tab for a new window repeatedly. */
4605 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4606 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 }
4608
4609 /* Remove the "lock" on the argument list. */
4610 alist_unlink(alist);
4611
4612#ifdef FEAT_AUTOCMD
4613 --autocmd_no_enter;
4614#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004615 /* to window with first arg */
4616 if (valid_tabpage(new_curtab))
4617 goto_tabpage_tp(new_curtab);
4618 if (win_valid(new_curwin))
4619 win_enter(new_curwin, FALSE);
4620
Bram Moolenaar071d4272004-06-13 20:20:40 +00004621#ifdef FEAT_AUTOCMD
4622 --autocmd_no_leave;
4623#endif
4624 vim_free(opened);
4625}
4626
4627# if defined(FEAT_LISTCMDS) || defined(PROTO)
4628/*
4629 * Open a window for a number of buffers.
4630 */
4631 void
4632ex_buffer_all(eap)
4633 exarg_T *eap;
4634{
4635 buf_T *buf;
4636 win_T *wp, *wpnext;
4637 int split_ret = OK;
4638 int p_ea_save;
4639 int open_wins = 0;
4640 int r;
4641 int count; /* Maximum number of windows to open. */
4642 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004643#ifdef FEAT_WINDOWS
4644 int had_tab = cmdmod.tab;
4645 tabpage_T *tpnext;
4646#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647
4648 if (eap->addr_count == 0) /* make as many windows as possible */
4649 count = 9999;
4650 else
4651 count = eap->line2; /* make as many windows as specified */
4652 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4653 all = FALSE;
4654 else
4655 all = TRUE;
4656
4657 setpcmark();
4658
4659#ifdef FEAT_GUI
4660 need_mouse_correct = TRUE;
4661#endif
4662
4663 /*
4664 * Close superfluous windows (two windows for the same buffer).
4665 * Also close windows that are not full-width.
4666 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004667#ifdef FEAT_WINDOWS
4668 if (had_tab > 0)
4669 goto_tabpage_tp(first_tabpage);
4670 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004673 tpnext = curtab->tp_next;
4674 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004676 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004677 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004678#ifdef FEAT_VERTSPLIT
4679 || ((cmdmod.split & WSP_VERT)
4680 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004681 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004682 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004684#ifdef FEAT_WINDOWS
4685 || (had_tab > 0 && wp != firstwin)
4686#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004687 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004688 {
4689 win_close(wp, FALSE);
4690#ifdef FEAT_AUTOCMD
4691 wpnext = firstwin; /* just in case an autocommand does
4692 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004693 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004694 open_wins = 0;
4695#endif
4696 }
4697 else
4698 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004700
4701#ifdef FEAT_WINDOWS
4702 /* Without the ":tab" modifier only do the current tab page. */
4703 if (had_tab == 0 || tpnext == NULL)
4704 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004705 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708
4709 /*
4710 * Go through the buffer list. When a buffer doesn't have a window yet,
4711 * open one. Otherwise move the window to the right position.
4712 * Watch out for autocommands that delete buffers or windows!
4713 */
4714#ifdef FEAT_AUTOCMD
4715 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4716 ++autocmd_no_enter;
4717#endif
4718 win_enter(lastwin, FALSE);
4719#ifdef FEAT_AUTOCMD
4720 ++autocmd_no_leave;
4721#endif
4722 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4723 {
4724 /* Check if this buffer needs a window */
4725 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4726 continue;
4727
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004728#ifdef FEAT_WINDOWS
4729 if (had_tab != 0)
4730 {
4731 /* With the ":tab" modifier don't move the window. */
4732 if (buf->b_nwindows > 0)
4733 wp = lastwin; /* buffer has a window, skip it */
4734 else
4735 wp = NULL;
4736 }
4737 else
4738#endif
4739 {
4740 /* Check if this buffer already has a window */
4741 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4742 if (wp->w_buffer == buf)
4743 break;
4744 /* If the buffer already has a window, move it */
4745 if (wp != NULL)
4746 win_move_after(wp, curwin);
4747 }
4748
4749 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
4751 /* Split the window and put the buffer in it */
4752 p_ea_save = p_ea;
4753 p_ea = TRUE; /* use space from all windows */
4754 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4755 ++open_wins;
4756 p_ea = p_ea_save;
4757 if (split_ret == FAIL)
4758 continue;
4759
4760 /* Open the buffer in this window. */
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_DIALOG;
4763#endif
4764 set_curbuf(buf, DOBUF_GOTO);
4765#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004768#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004770# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 break;
4772 }
4773#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004774#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 if (swap_exists_action == SEA_QUIT)
4776 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004777# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4778 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004779
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004780 /* Reset the error/interrupt/exception state here so that
4781 * aborting() returns FALSE when closing a window. */
4782 enter_cleanup(&cs);
4783# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004784
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004785 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 win_close(curwin, TRUE);
4787 --open_wins;
4788 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004789 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004790
4791# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4792 /* Restore the error/interrupt/exception state if not
4793 * discarded by a new aborting error, interrupt, or uncaught
4794 * exception. */
4795 leave_cleanup(&cs);
4796# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 }
4798 else
4799 handle_swap_exists(NULL);
4800#endif
4801 }
4802
4803 ui_breakcheck();
4804 if (got_int)
4805 {
4806 (void)vgetc(); /* only break the file loading, not the rest */
4807 break;
4808 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004809#ifdef FEAT_EVAL
4810 /* Autocommands deleted the buffer or aborted script processing!!! */
4811 if (aborting())
4812 break;
4813#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004814#ifdef FEAT_WINDOWS
4815 /* When ":tab" was used open a new tab for a new window repeatedly. */
4816 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4817 cmdmod.tab = 9999;
4818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 }
4820#ifdef FEAT_AUTOCMD
4821 --autocmd_no_enter;
4822#endif
4823 win_enter(firstwin, FALSE); /* back to first window */
4824#ifdef FEAT_AUTOCMD
4825 --autocmd_no_leave;
4826#endif
4827
4828 /*
4829 * Close superfluous windows.
4830 */
4831 for (wp = lastwin; open_wins > count; )
4832 {
4833 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4834 || autowrite(wp->w_buffer, FALSE) == OK);
4835#ifdef FEAT_AUTOCMD
4836 if (!win_valid(wp))
4837 {
4838 /* BufWrite Autocommands made the window invalid, start over */
4839 wp = lastwin;
4840 }
4841 else
4842#endif
4843 if (r)
4844 {
4845 win_close(wp, !P_HID(wp->w_buffer));
4846 --open_wins;
4847 wp = lastwin;
4848 }
4849 else
4850 {
4851 wp = wp->w_prev;
4852 if (wp == NULL)
4853 break;
4854 }
4855 }
4856}
4857# endif /* FEAT_LISTCMDS */
4858
4859#endif /* FEAT_WINDOWS */
4860
Bram Moolenaara3227e22006-03-08 21:32:40 +00004861static int chk_modeline __ARGS((linenr_T, int));
4862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863/*
4864 * do_modelines() - process mode lines for the current file
4865 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004866 * "flags" can be:
4867 * OPT_WINONLY only set options local to window
4868 * OPT_NOWIN don't set options local to window
4869 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 * Returns immediately if the "ml" option isn't set.
4871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004873do_modelines(flags)
4874 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004876 linenr_T lnum;
4877 int nmlines;
4878 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4881 return;
4882
4883 /* Disallow recursive entry here. Can happen when executing a modeline
4884 * triggers an autocommand, which reloads modelines with a ":do". */
4885 if (entered)
4886 return;
4887
4888 ++entered;
4889 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4890 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004891 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 nmlines = 0;
4893
4894 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4895 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004896 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 nmlines = 0;
4898 --entered;
4899}
4900
4901#include "version.h" /* for version number */
4902
4903/*
4904 * chk_modeline() - check a single line for a mode string
4905 * Return FAIL if an error encountered.
4906 */
4907 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004908chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004910 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
4912 char_u *s;
4913 char_u *e;
4914 char_u *linecopy; /* local copy of any modeline found */
4915 int prev;
4916 int vers;
4917 int end;
4918 int retval = OK;
4919 char_u *save_sourcing_name;
4920 linenr_T save_sourcing_lnum;
4921#ifdef FEAT_EVAL
4922 scid_T save_SID;
4923#endif
4924
4925 prev = -1;
4926 for (s = ml_get(lnum); *s != NUL; ++s)
4927 {
4928 if (prev == -1 || vim_isspace(prev))
4929 {
4930 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4931 || STRNCMP(s, "vi:", (size_t)3) == 0)
4932 break;
4933 if (STRNCMP(s, "vim", 3) == 0)
4934 {
4935 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4936 e = s + 4;
4937 else
4938 e = s + 3;
4939 vers = getdigits(&e);
4940 if (*e == ':'
4941 && (s[3] == ':'
4942 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4943 || (VIM_VERSION_100 < vers && s[3] == '<')
4944 || (VIM_VERSION_100 > vers && s[3] == '>')
4945 || (VIM_VERSION_100 == vers && s[3] == '=')))
4946 break;
4947 }
4948 }
4949 prev = *s;
4950 }
4951
4952 if (*s)
4953 {
4954 do /* skip over "ex:", "vi:" or "vim:" */
4955 ++s;
4956 while (s[-1] != ':');
4957
4958 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4959 if (linecopy == NULL)
4960 return FAIL;
4961
4962 save_sourcing_lnum = sourcing_lnum;
4963 save_sourcing_name = sourcing_name;
4964 sourcing_lnum = lnum; /* prepare for emsg() */
4965 sourcing_name = (char_u *)"modelines";
4966
4967 end = FALSE;
4968 while (end == FALSE)
4969 {
4970 s = skipwhite(s);
4971 if (*s == NUL)
4972 break;
4973
4974 /*
4975 * Find end of set command: ':' or end of line.
4976 * Skip over "\:", replacing it with ":".
4977 */
4978 for (e = s; *e != ':' && *e != NUL; ++e)
4979 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004980 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 if (*e == NUL)
4982 end = TRUE;
4983
4984 /*
4985 * If there is a "set" command, require a terminating ':' and
4986 * ignore the stuff after the ':'.
4987 * "vi:set opt opt opt: foo" -- foo not interpreted
4988 * "vi:opt opt opt: foo" -- foo interpreted
4989 * Accept "se" for compatibility with Elvis.
4990 */
4991 if (STRNCMP(s, "set ", (size_t)4) == 0
4992 || STRNCMP(s, "se ", (size_t)3) == 0)
4993 {
4994 if (*e != ':') /* no terminating ':'? */
4995 break;
4996 end = TRUE;
4997 s = vim_strchr(s, ' ') + 1;
4998 }
4999 *e = NUL; /* truncate the set command */
5000
5001 if (*s != NUL) /* skip over an empty "::" */
5002 {
5003#ifdef FEAT_EVAL
5004 save_SID = current_SID;
5005 current_SID = SID_MODELINE;
5006#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005007 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008#ifdef FEAT_EVAL
5009 current_SID = save_SID;
5010#endif
5011 if (retval == FAIL) /* stop if error found */
5012 break;
5013 }
5014 s = e + 1; /* advance to next part */
5015 }
5016
5017 sourcing_lnum = save_sourcing_lnum;
5018 sourcing_name = save_sourcing_name;
5019
5020 vim_free(linecopy);
5021 }
5022 return retval;
5023}
5024
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005025#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 int
5027read_viminfo_bufferlist(virp, writing)
5028 vir_T *virp;
5029 int writing;
5030{
5031 char_u *tab;
5032 linenr_T lnum;
5033 colnr_T col;
5034 buf_T *buf;
5035 char_u *sfname;
5036 char_u *xline;
5037
5038 /* Handle long line and escaped characters. */
5039 xline = viminfo_readstring(virp, 1, FALSE);
5040
5041 /* don't read in if there are files on the command-line or if writing: */
5042 if (xline != NULL && !writing && ARGCOUNT == 0
5043 && find_viminfo_parameter('%') != NULL)
5044 {
5045 /* Format is: <fname> Tab <lnum> Tab <col>.
5046 * Watch out for a Tab in the file name, work from the end. */
5047 lnum = 0;
5048 col = 0;
5049 tab = vim_strrchr(xline, '\t');
5050 if (tab != NULL)
5051 {
5052 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005053 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 tab = vim_strrchr(xline, '\t');
5055 if (tab != NULL)
5056 {
5057 *tab++ = '\0';
5058 lnum = atol((char *)tab);
5059 }
5060 }
5061
5062 /* Expand "~/" in the file name at "line + 1" to a full path.
5063 * Then try shortening it by comparing with the current directory */
5064 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005065 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
5067 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5068 if (buf != NULL) /* just in case... */
5069 {
5070 buf->b_last_cursor.lnum = lnum;
5071 buf->b_last_cursor.col = col;
5072 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5073 }
5074 }
5075 vim_free(xline);
5076
5077 return viminfo_readline(virp);
5078}
5079
5080 void
5081write_viminfo_bufferlist(fp)
5082 FILE *fp;
5083{
5084 buf_T *buf;
5085#ifdef FEAT_WINDOWS
5086 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005087 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088#endif
5089 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005090 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
5092 if (find_viminfo_parameter('%') == NULL)
5093 return;
5094
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005095 /* Without a number -1 is returned: do all buffers. */
5096 max_buffers = get_viminfo_parameter('%');
5097
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005099#define LINE_BUF_LEN (MAXPATHL + 40)
5100 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 if (line == NULL)
5102 return;
5103
5104#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005105 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 set_last_cursor(win);
5107#else
5108 set_last_cursor(curwin);
5109#endif
5110
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005111 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5113 {
5114 if (buf->b_fname == NULL
5115 || !buf->b_p_bl
5116#ifdef FEAT_QUICKFIX
5117 || bt_quickfix(buf)
5118#endif
5119 || removable(buf->b_ffname))
5120 continue;
5121
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005122 if (max_buffers-- == 0)
5123 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 putc('%', fp);
5125 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005126 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 (long)buf->b_last_cursor.lnum,
5128 buf->b_last_cursor.col);
5129 viminfo_writestring(fp, line);
5130 }
5131 vim_free(line);
5132}
5133#endif
5134
5135
5136/*
5137 * Return special buffer name.
5138 * Returns NULL when the buffer has a normal file name.
5139 */
5140 char *
5141buf_spname(buf)
5142 buf_T *buf;
5143{
5144#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5145 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005146 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005147 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005148 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005149
5150 /*
5151 * For location list window, w_llist_ref points to the location list.
5152 * For quickfix window, w_llist_ref is NULL.
5153 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005154 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005155 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005156 goto win_found;
5157win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005158 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005159 return _(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005160 else
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005161 return _(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163#endif
5164#ifdef FEAT_QUICKFIX
5165 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5166 * contains the name as specified by the user */
5167 if (bt_nofile(buf))
5168 {
5169 if (buf->b_sfname != NULL)
5170 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005171 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 }
5173#endif
5174 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005175 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 return NULL;
5177}
5178
5179
5180#if defined(FEAT_SIGNS) || defined(PROTO)
5181/*
5182 * Insert the sign into the signlist.
5183 */
5184 static void
5185insert_sign(buf, prev, next, id, lnum, typenr)
5186 buf_T *buf; /* buffer to store sign in */
5187 signlist_T *prev; /* previous sign entry */
5188 signlist_T *next; /* next sign entry */
5189 int id; /* sign ID */
5190 linenr_T lnum; /* line number which gets the mark */
5191 int typenr; /* typenr of sign we are adding */
5192{
5193 signlist_T *newsign;
5194
5195 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5196 if (newsign != NULL)
5197 {
5198 newsign->id = id;
5199 newsign->lnum = lnum;
5200 newsign->typenr = typenr;
5201 newsign->next = next;
5202#ifdef FEAT_NETBEANS_INTG
5203 newsign->prev = prev;
5204 if (next != NULL)
5205 next->prev = newsign;
5206#endif
5207
5208 if (prev == NULL)
5209 {
5210 /* When adding first sign need to redraw the windows to create the
5211 * column for signs. */
5212 if (buf->b_signlist == NULL)
5213 {
5214 redraw_buf_later(buf, NOT_VALID);
5215 changed_cline_bef_curs();
5216 }
5217
5218 /* first sign in signlist */
5219 buf->b_signlist = newsign;
5220 }
5221 else
5222 prev->next = newsign;
5223 }
5224}
5225
5226/*
5227 * Add the sign into the signlist. Find the right spot to do it though.
5228 */
5229 void
5230buf_addsign(buf, id, lnum, typenr)
5231 buf_T *buf; /* buffer to store sign in */
5232 int id; /* sign ID */
5233 linenr_T lnum; /* line number which gets the mark */
5234 int typenr; /* typenr of sign we are adding */
5235{
5236 signlist_T *sign; /* a sign in the signlist */
5237 signlist_T *prev; /* the previous sign */
5238
5239 prev = NULL;
5240 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5241 {
5242 if (lnum == sign->lnum && id == sign->id)
5243 {
5244 sign->typenr = typenr;
5245 return;
5246 }
5247 else if (
5248#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5249 id < 0 &&
5250#endif
5251 lnum < sign->lnum)
5252 {
5253#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5254 /* XXX - GRP: Is this because of sign slide problem? Or is it
5255 * really needed? Or is it because we allow multiple signs per
5256 * line? If so, should I add that feature to FEAT_SIGNS?
5257 */
5258 while (prev != NULL && prev->lnum == lnum)
5259 prev = prev->prev;
5260 if (prev == NULL)
5261 sign = buf->b_signlist;
5262 else
5263 sign = prev->next;
5264#endif
5265 insert_sign(buf, prev, sign, id, lnum, typenr);
5266 return;
5267 }
5268 prev = sign;
5269 }
5270#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5271 /* XXX - GRP: See previous comment */
5272 while (prev != NULL && prev->lnum == lnum)
5273 prev = prev->prev;
5274 if (prev == NULL)
5275 sign = buf->b_signlist;
5276 else
5277 sign = prev->next;
5278#endif
5279 insert_sign(buf, prev, sign, id, lnum, typenr);
5280
5281 return;
5282}
5283
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005284 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285buf_change_sign_type(buf, markId, typenr)
5286 buf_T *buf; /* buffer to store sign in */
5287 int markId; /* sign ID */
5288 int typenr; /* typenr of sign we are adding */
5289{
5290 signlist_T *sign; /* a sign in the signlist */
5291
5292 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5293 {
5294 if (sign->id == markId)
5295 {
5296 sign->typenr = typenr;
5297 return sign->lnum;
5298 }
5299 }
5300
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005301 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302}
5303
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005304 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305buf_getsigntype(buf, lnum, type)
5306 buf_T *buf;
5307 linenr_T lnum;
5308 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5309{
5310 signlist_T *sign; /* a sign in a b_signlist */
5311
5312 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5313 if (sign->lnum == lnum
5314 && (type == SIGN_ANY
5315# ifdef FEAT_SIGN_ICONS
5316 || (type == SIGN_ICON
5317 && sign_get_image(sign->typenr) != NULL)
5318# endif
5319 || (type == SIGN_TEXT
5320 && sign_get_text(sign->typenr) != NULL)
5321 || (type == SIGN_LINEHL
5322 && sign_get_attr(sign->typenr, TRUE) != 0)))
5323 return sign->typenr;
5324 return 0;
5325}
5326
5327
5328 linenr_T
5329buf_delsign(buf, id)
5330 buf_T *buf; /* buffer sign is stored in */
5331 int id; /* sign id */
5332{
5333 signlist_T **lastp; /* pointer to pointer to current sign */
5334 signlist_T *sign; /* a sign in a b_signlist */
5335 signlist_T *next; /* the next sign in a b_signlist */
5336 linenr_T lnum; /* line number whose sign was deleted */
5337
5338 lastp = &buf->b_signlist;
5339 lnum = 0;
5340 for (sign = buf->b_signlist; sign != NULL; sign = next)
5341 {
5342 next = sign->next;
5343 if (sign->id == id)
5344 {
5345 *lastp = next;
5346#ifdef FEAT_NETBEANS_INTG
5347 if (next != NULL)
5348 next->prev = sign->prev;
5349#endif
5350 lnum = sign->lnum;
5351 vim_free(sign);
5352 break;
5353 }
5354 else
5355 lastp = &sign->next;
5356 }
5357
5358 /* When deleted the last sign need to redraw the windows to remove the
5359 * sign column. */
5360 if (buf->b_signlist == NULL)
5361 {
5362 redraw_buf_later(buf, NOT_VALID);
5363 changed_cline_bef_curs();
5364 }
5365
5366 return lnum;
5367}
5368
5369
5370/*
5371 * Find the line number of the sign with the requested id. If the sign does
5372 * not exist, return 0 as the line number. This will still let the correct file
5373 * get loaded.
5374 */
5375 int
5376buf_findsign(buf, id)
5377 buf_T *buf; /* buffer to store sign in */
5378 int id; /* sign ID */
5379{
5380 signlist_T *sign; /* a sign in the signlist */
5381
5382 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5383 if (sign->id == id)
5384 return sign->lnum;
5385
5386 return 0;
5387}
5388
5389 int
5390buf_findsign_id(buf, lnum)
5391 buf_T *buf; /* buffer whose sign we are searching for */
5392 linenr_T lnum; /* line number of sign */
5393{
5394 signlist_T *sign; /* a sign in the signlist */
5395
5396 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5397 if (sign->lnum == lnum)
5398 return sign->id;
5399
5400 return 0;
5401}
5402
5403
5404# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5405/* see if a given type of sign exists on a specific line */
5406 int
5407buf_findsigntype_id(buf, lnum, typenr)
5408 buf_T *buf; /* buffer whose sign we are searching for */
5409 linenr_T lnum; /* line number of sign */
5410 int typenr; /* sign type number */
5411{
5412 signlist_T *sign; /* a sign in the signlist */
5413
5414 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5415 if (sign->lnum == lnum && sign->typenr == typenr)
5416 return sign->id;
5417
5418 return 0;
5419}
5420
5421
5422# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5423/* return the number of icons on the given line */
5424 int
5425buf_signcount(buf, lnum)
5426 buf_T *buf;
5427 linenr_T lnum;
5428{
5429 signlist_T *sign; /* a sign in the signlist */
5430 int count = 0;
5431
5432 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5433 if (sign->lnum == lnum)
5434 if (sign_get_image(sign->typenr) != NULL)
5435 count++;
5436
5437 return count;
5438}
5439# endif /* FEAT_SIGN_ICONS */
5440# endif /* FEAT_NETBEANS_INTG */
5441
5442
5443/*
5444 * Delete signs in buffer "buf".
5445 */
5446 static void
5447buf_delete_signs(buf)
5448 buf_T *buf;
5449{
5450 signlist_T *next;
5451
5452 while (buf->b_signlist != NULL)
5453 {
5454 next = buf->b_signlist->next;
5455 vim_free(buf->b_signlist);
5456 buf->b_signlist = next;
5457 }
5458}
5459
5460/*
5461 * Delete all signs in all buffers.
5462 */
5463 void
5464buf_delete_all_signs()
5465{
5466 buf_T *buf; /* buffer we are checking for signs */
5467
5468 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5469 if (buf->b_signlist != NULL)
5470 {
5471 /* Need to redraw the windows to remove the sign column. */
5472 redraw_buf_later(buf, NOT_VALID);
5473 buf_delete_signs(buf);
5474 }
5475}
5476
5477/*
5478 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5479 */
5480 void
5481sign_list_placed(rbuf)
5482 buf_T *rbuf;
5483{
5484 buf_T *buf;
5485 signlist_T *p;
5486 char lbuf[BUFSIZ];
5487
5488 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5489 msg_putchar('\n');
5490 if (rbuf == NULL)
5491 buf = firstbuf;
5492 else
5493 buf = rbuf;
5494 while (buf != NULL)
5495 {
5496 if (buf->b_signlist != NULL)
5497 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005498 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5500 msg_putchar('\n');
5501 }
5502 for (p = buf->b_signlist; p != NULL; p = p->next)
5503 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005504 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5506 MSG_PUTS(lbuf);
5507 msg_putchar('\n');
5508 }
5509 if (rbuf != NULL)
5510 break;
5511 buf = buf->b_next;
5512 }
5513}
5514
5515/*
5516 * Adjust a placed sign for inserted/deleted lines.
5517 */
5518 void
5519sign_mark_adjust(line1, line2, amount, amount_after)
5520 linenr_T line1;
5521 linenr_T line2;
5522 long amount;
5523 long amount_after;
5524{
5525 signlist_T *sign; /* a sign in a b_signlist */
5526
5527 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5528 {
5529 if (sign->lnum >= line1 && sign->lnum <= line2)
5530 {
5531 if (amount == MAXLNUM)
5532 sign->lnum = line1;
5533 else
5534 sign->lnum += amount;
5535 }
5536 else if (sign->lnum > line2)
5537 sign->lnum += amount_after;
5538 }
5539}
5540#endif /* FEAT_SIGNS */
5541
5542/*
5543 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5544 */
5545 void
5546set_buflisted(on)
5547 int on;
5548{
5549 if (on != curbuf->b_p_bl)
5550 {
5551 curbuf->b_p_bl = on;
5552#ifdef FEAT_AUTOCMD
5553 if (on)
5554 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5555 else
5556 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5557#endif
5558 }
5559}
5560
5561/*
5562 * Read the file for "buf" again and check if the contents changed.
5563 * Return TRUE if it changed or this could not be checked.
5564 */
5565 int
5566buf_contents_changed(buf)
5567 buf_T *buf;
5568{
5569 buf_T *newbuf;
5570 int differ = TRUE;
5571 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 exarg_T ea;
5574
5575 /* Allocate a buffer without putting it in the buffer list. */
5576 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5577 if (newbuf == NULL)
5578 return TRUE;
5579
5580 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5581 if (prep_exarg(&ea, buf) == FAIL)
5582 {
5583 wipe_buffer(newbuf, FALSE);
5584 return TRUE;
5585 }
5586
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 /* set curwin/curbuf to buf and save a few things */
5588 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
Bram Moolenaar4770d092006-01-12 23:22:24 +00005590 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 && readfile(buf->b_ffname, buf->b_fname,
5592 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5593 &ea, READ_NEW | READ_DUMMY) == OK)
5594 {
5595 /* compare the two files line by line */
5596 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5597 {
5598 differ = FALSE;
5599 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5600 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5601 {
5602 differ = TRUE;
5603 break;
5604 }
5605 }
5606 }
5607 vim_free(ea.cmd);
5608
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 /* restore curwin/curbuf and a few other things */
5610 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 if (curbuf != newbuf) /* safety check */
5613 wipe_buffer(newbuf, FALSE);
5614
5615 return differ;
5616}
5617
5618/*
5619 * Wipe out a buffer and decrement the last buffer number if it was used for
5620 * this buffer. Call this to wipe out a temp buffer that does not contain any
5621 * marks.
5622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 void
5624wipe_buffer(buf, aucmd)
5625 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005626 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627{
5628 if (buf->b_fnum == top_file_num - 1)
5629 --top_file_num;
5630
5631#ifdef FEAT_AUTOCMD
5632 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005633 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634#endif
5635 close_buffer(NULL, buf, DOBUF_WIPE);
5636#ifdef FEAT_AUTOCMD
5637 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005638 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639#endif
5640}