blob: 2884b30381e8ea99a5b50f21bc3fd3c6907ba16e [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * buffer.c: functions for dealing with the buffer structure
12 */
13
14/*
15 * The buffer list is a double linked list of all buffers.
16 * Each buffer can be in one of these states:
17 * never loaded: BF_NEVERLOADED is set, only the file name is valid
18 * not loaded: b_ml.ml_mfp == NULL, no memfile allocated
19 * hidden: b_nwindows == 0, loaded but not displayed in a window
20 * normal: loaded and displayed in a window
21 *
22 * Instead of storing file names all over the place, each file name is
23 * stored in the buffer list. It can be referenced by a number.
24 *
25 * The current implementation remembers all file names ever used.
26 */
27
Bram Moolenaar071d4272004-06-13 20:20:40 +000028#include "vim.h"
29
30#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
31static char_u *buflist_match __ARGS((regprog_T *prog, buf_T *buf));
32# define HAVE_BUFLIST_MATCH
33static char_u *fname_match __ARGS((regprog_T *prog, char_u *name));
34#endif
35static void buflist_setfpos __ARGS((buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options));
Bram Moolenaar701f7af2008-11-15 13:12:07 +000036static wininfo_T *find_wininfo __ARGS((buf_T *buf, int skip_diff_buffer));
Bram Moolenaar071d4272004-06-13 20:20:40 +000037#ifdef UNIX
38static buf_T *buflist_findname_stat __ARGS((char_u *ffname, struct stat *st));
39static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname, struct stat *stp));
40static int buf_same_ino __ARGS((buf_T *buf, struct stat *stp));
41#else
42static int otherfile_buf __ARGS((buf_T *buf, char_u *ffname));
43#endif
44#ifdef FEAT_TITLE
45static int ti_change __ARGS((char_u *str, char_u **last));
46#endif
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000047static int append_arg_number __ARGS((win_T *wp, char_u *buf, int buflen, int add_file));
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static void free_buffer __ARGS((buf_T *));
49static void free_buffer_stuff __ARGS((buf_T *buf, int free_options));
50static void clear_wininfo __ARGS((buf_T *buf));
51
52#ifdef UNIX
53# define dev_T dev_t
54#else
55# define dev_T unsigned
56#endif
57
58#if defined(FEAT_SIGNS)
59static void insert_sign __ARGS((buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr));
60static void buf_delete_signs __ARGS((buf_T *buf));
61#endif
62
Bram Moolenaar7fd73202010-07-25 16:58:46 +020063#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
64static char *msg_loclist = N_("[Location List]");
65static char *msg_qflist = N_("[Quickfix List]");
66#endif
67
Bram Moolenaar071d4272004-06-13 20:20:40 +000068/*
Bram Moolenaar55debbe2010-05-23 23:34:36 +020069 * Open current buffer, that is: open the memfile and read the file into
70 * memory.
71 * Return FAIL for failure, OK otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000072 */
73 int
Bram Moolenaar59f931e2010-07-24 20:27:03 +020074open_buffer(read_stdin, eap, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 int read_stdin; /* read file from stdin */
76 exarg_T *eap; /* for forced 'ff' and 'fenc' or NULL */
Bram Moolenaar59f931e2010-07-24 20:27:03 +020077 int flags; /* extra flags for readfile() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000078{
79 int retval = OK;
80#ifdef FEAT_AUTOCMD
81 buf_T *old_curbuf;
82#endif
83
84 /*
85 * The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
86 * When re-entering the same buffer, it should not change, because the
87 * user may have reset the flag by hand.
88 */
89 if (readonlymode && curbuf->b_ffname != NULL
90 && (curbuf->b_flags & BF_NEVERLOADED))
91 curbuf->b_p_ro = TRUE;
92
Bram Moolenaar4770d092006-01-12 23:22:24 +000093 if (ml_open(curbuf) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 {
95 /*
96 * There MUST be a memfile, otherwise we can't do anything
97 * If we can't create one for the current buffer, take another buffer
98 */
99 close_buffer(NULL, curbuf, 0);
100 for (curbuf = firstbuf; curbuf != NULL; curbuf = curbuf->b_next)
101 if (curbuf->b_ml.ml_mfp != NULL)
102 break;
103 /*
104 * if there is no memfile at all, exit
Bram Moolenaare37d50a2008-08-06 17:06:04 +0000105 * This is OK, since there are no changes to lose.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 */
107 if (curbuf == NULL)
108 {
109 EMSG(_("E82: Cannot allocate any buffer, exiting..."));
110 getout(2);
111 }
112 EMSG(_("E83: Cannot allocate buffer, using other one..."));
113 enter_buffer(curbuf);
114 return FAIL;
115 }
116
117#ifdef FEAT_AUTOCMD
118 /* The autocommands in readfile() may change the buffer, but only AFTER
119 * reading the file. */
120 old_curbuf = curbuf;
121 modified_was_set = FALSE;
122#endif
123
124 /* mark cursor position as being invalid */
Bram Moolenaar89c0ea42010-02-24 16:58:36 +0100125 curwin->w_valid = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000126
127 if (curbuf->b_ffname != NULL
128#ifdef FEAT_NETBEANS_INTG
129 && netbeansReadFile
130#endif
131 )
132 {
133#ifdef FEAT_NETBEANS_INTG
134 int oldFire = netbeansFireChanges;
135
136 netbeansFireChanges = 0;
137#endif
138 retval = readfile(curbuf->b_ffname, curbuf->b_fname,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200139 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
140 flags | READ_NEW);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141#ifdef FEAT_NETBEANS_INTG
142 netbeansFireChanges = oldFire;
143#endif
144 /* Help buffer is filtered. */
145 if (curbuf->b_help)
146 fix_help_buffer();
147 }
148 else if (read_stdin)
149 {
150 int save_bin = curbuf->b_p_bin;
151 linenr_T line_count;
152
153 /*
154 * First read the text in binary mode into the buffer.
155 * Then read from that same buffer and append at the end. This makes
156 * it possible to retry when 'fileformat' or 'fileencoding' was
157 * guessed wrong.
158 */
159 curbuf->b_p_bin = TRUE;
160 retval = readfile(NULL, NULL, (linenr_T)0,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200161 (linenr_T)0, (linenr_T)MAXLNUM, NULL,
162 flags | (READ_NEW + READ_STDIN));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163 curbuf->b_p_bin = save_bin;
164 if (retval == OK)
165 {
166 line_count = curbuf->b_ml.ml_line_count;
167 retval = readfile(NULL, NULL, (linenr_T)line_count,
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200168 (linenr_T)0, (linenr_T)MAXLNUM, eap,
169 flags | READ_BUFFER);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 if (retval == OK)
171 {
172 /* Delete the binary lines. */
173 while (--line_count >= 0)
174 ml_delete((linenr_T)1, FALSE);
175 }
176 else
177 {
178 /* Delete the converted lines. */
179 while (curbuf->b_ml.ml_line_count > line_count)
180 ml_delete(line_count, FALSE);
181 }
182 /* Put the cursor on the first line. */
183 curwin->w_cursor.lnum = 1;
184 curwin->w_cursor.col = 0;
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000185
186 /* Set or reset 'modified' before executing autocommands, so that
187 * it can be changed there. */
188 if (!readonlymode && !bufempty())
189 changed();
190 else if (retval != FAIL)
191 unchanged(curbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192#ifdef FEAT_AUTOCMD
193# ifdef FEAT_EVAL
194 apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
195 curbuf, &retval);
196# else
197 apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
198# endif
199#endif
200 }
201 }
202
203 /* if first time loading this buffer, init b_chartab[] */
204 if (curbuf->b_flags & BF_NEVERLOADED)
205 (void)buf_init_chartab(curbuf, FALSE);
206
207 /*
208 * Set/reset the Changed flag first, autocmds may change the buffer.
209 * Apply the automatic commands, before processing the modelines.
210 * So the modelines have priority over auto commands.
211 */
212 /* When reading stdin, the buffer contents always needs writing, so set
213 * the changed flag. Unless in readonly mode: "ls | gview -".
214 * When interrupted and 'cpoptions' contains 'i' set changed flag. */
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000215 if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000216#ifdef FEAT_AUTOCMD
217 || modified_was_set /* ":set modified" used in autocmd */
218# ifdef FEAT_EVAL
219 || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
220# endif
221#endif
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000222 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223 changed();
Bram Moolenaar512e6b82007-06-19 13:36:52 +0000224 else if (retval != FAIL && !read_stdin)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 unchanged(curbuf, FALSE);
226 save_file_ff(curbuf); /* keep this fileformat */
227
228 /* require "!" to overwrite the file, because it wasn't read completely */
229#ifdef FEAT_EVAL
230 if (aborting())
231#else
232 if (got_int)
233#endif
234 curbuf->b_flags |= BF_READERR;
235
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000236#ifdef FEAT_FOLDING
237 /* Need to update automatic folding. Do this before the autocommands,
238 * they may use the fold info. */
239 foldUpdateAll(curwin);
240#endif
241
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#ifdef FEAT_AUTOCMD
243 /* need to set w_topline, unless some autocommand already did that. */
244 if (!(curwin->w_valid & VALID_TOPLINE))
245 {
246 curwin->w_topline = 1;
247# ifdef FEAT_DIFF
248 curwin->w_topfill = 0;
249# endif
250 }
251# ifdef FEAT_EVAL
252 apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
253# else
254 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
255# endif
256#endif
257
258 if (retval != FAIL)
259 {
260#ifdef FEAT_AUTOCMD
261 /*
262 * The autocommands may have changed the current buffer. Apply the
263 * modelines to the correct buffer, if it still exists and is loaded.
264 */
265 if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL)
266 {
267 aco_save_T aco;
268
269 /* Go to the buffer that was opened. */
270 aucmd_prepbuf(&aco, old_curbuf);
271#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +0000272 do_modelines(0);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273 curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
274
275#ifdef FEAT_AUTOCMD
276# ifdef FEAT_EVAL
277 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
278 &retval);
279# else
280 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
281# endif
282
283 /* restore curwin/curbuf and a few other things */
284 aucmd_restbuf(&aco);
285 }
286#endif
287 }
288
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 return retval;
290}
291
292/*
293 * Return TRUE if "buf" points to a valid buffer (in the buffer list).
294 */
295 int
296buf_valid(buf)
297 buf_T *buf;
298{
299 buf_T *bp;
300
301 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
302 if (bp == buf)
303 return TRUE;
304 return FALSE;
305}
306
307/*
308 * Close the link to a buffer.
309 * "action" is used when there is no longer a window for the buffer.
310 * It can be:
311 * 0 buffer becomes hidden
312 * DOBUF_UNLOAD buffer is unloaded
313 * DOBUF_DELETE buffer is unloaded and removed from buffer list
314 * DOBUF_WIPE buffer is unloaded and really deleted
315 * When doing all but the first one on the current buffer, the caller should
316 * get a new buffer very soon!
317 *
318 * The 'bufhidden' option can force freeing and deleting.
319 */
320 void
321close_buffer(win, buf, action)
322 win_T *win; /* if not NULL, set b_last_cursor */
323 buf_T *buf;
324 int action;
325{
326#ifdef FEAT_AUTOCMD
327 int is_curbuf;
Bram Moolenaar2660c0e2010-01-19 14:59:56 +0100328 int nwindows;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329#endif
330 int unload_buf = (action != 0);
331 int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
332 int wipe_buf = (action == DOBUF_WIPE);
333
334#ifdef FEAT_QUICKFIX
335 /*
336 * Force unloading or deleting when 'bufhidden' says so.
337 * The caller must take care of NOT deleting/freeing when 'bufhidden' is
338 * "hide" (otherwise we could never free or delete a buffer).
339 */
340 if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
341 {
342 del_buf = TRUE;
343 unload_buf = TRUE;
344 }
345 else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
346 {
347 del_buf = TRUE;
348 unload_buf = TRUE;
349 wipe_buf = TRUE;
350 }
351 else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
352 unload_buf = TRUE;
353#endif
354
355 if (win != NULL)
356 {
357 /* Set b_last_cursor when closing the last window for the buffer.
358 * Remember the last cursor position and window options of the buffer.
359 * This used to be only for the current window, but then options like
360 * 'foldmethod' may be lost with a ":only" command. */
361 if (buf->b_nwindows == 1)
362 set_last_cursor(win);
363 buflist_setfpos(buf, win,
364 win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
365 win->w_cursor.col, TRUE);
366 }
367
368#ifdef FEAT_AUTOCMD
369 /* When the buffer is no longer in a window, trigger BufWinLeave */
370 if (buf->b_nwindows == 1)
371 {
372 apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
373 FALSE, buf);
374 if (!buf_valid(buf)) /* autocommands may delete the buffer */
375 return;
376
377 /* When the buffer becomes hidden, but is not unloaded, trigger
378 * BufHidden */
379 if (!unload_buf)
380 {
381 apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
382 FALSE, buf);
383 if (!buf_valid(buf)) /* autocmds may delete the buffer */
384 return;
385 }
386# ifdef FEAT_EVAL
387 if (aborting()) /* autocmds may abort script processing */
388 return;
389# endif
390 }
391 nwindows = buf->b_nwindows;
392#endif
393
394 /* decrease the link count from windows (unless not in any window) */
395 if (buf->b_nwindows > 0)
396 --buf->b_nwindows;
397
398 /* Return when a window is displaying the buffer or when it's not
399 * unloaded. */
400 if (buf->b_nwindows > 0 || !unload_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402
403 /* Always remove the buffer when there is no file name. */
404 if (buf->b_ffname == NULL)
405 del_buf = TRUE;
406
407 /*
408 * Free all things allocated for this buffer.
409 * Also calls the "BufDelete" autocommands when del_buf is TRUE.
410 */
411#ifdef FEAT_AUTOCMD
412 /* Remember if we are closing the current buffer. Restore the number of
413 * windows, so that autocommands in buf_freeall() don't get confused. */
414 is_curbuf = (buf == curbuf);
415 buf->b_nwindows = nwindows;
416#endif
417
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200418 buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
Bram Moolenaarddab3322011-09-14 17:50:14 +0200419 if (
420#ifdef FEAT_WINDOWS
421 win_valid(win) &&
Bram Moolenaar83bac4c2011-12-30 13:39:10 +0100422#else
423 win != NULL &&
Bram Moolenaarddab3322011-09-14 17:50:14 +0200424#endif
425 win->w_buffer == buf)
Bram Moolenaara971b822011-09-14 14:43:25 +0200426 win->w_buffer = NULL; /* make sure we don't use the buffer now */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427
428#ifdef FEAT_AUTOCMD
429 /* Autocommands may have deleted the buffer. */
430 if (!buf_valid(buf))
431 return;
432# ifdef FEAT_EVAL
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000433 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 return;
435# endif
436
437 /* Autocommands may have opened or closed windows for this buffer.
438 * Decrement the count for the close we do here. */
439 if (buf->b_nwindows > 0)
440 --buf->b_nwindows;
441
442 /*
443 * It's possible that autocommands change curbuf to the one being deleted.
444 * This might cause the previous curbuf to be deleted unexpectedly. But
445 * in some cases it's OK to delete the curbuf, because a new one is
446 * obtained anyway. Therefore only return if curbuf changed to the
447 * deleted buffer.
448 */
449 if (buf == curbuf && !is_curbuf)
450 return;
451#endif
452
Bram Moolenaar498efdb2006-09-05 14:31:54 +0000453 /* Change directories when the 'acd' option is set. */
454 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455
456 /*
457 * Remove the buffer from the list.
458 */
459 if (wipe_buf)
460 {
461#ifdef FEAT_SUN_WORKSHOP
462 if (usingSunWorkShop)
463 workshop_file_closed_lineno((char *)buf->b_ffname,
464 (int)buf->b_last_cursor.lnum);
465#endif
466 vim_free(buf->b_ffname);
467 vim_free(buf->b_sfname);
468 if (buf->b_prev == NULL)
469 firstbuf = buf->b_next;
470 else
471 buf->b_prev->b_next = buf->b_next;
472 if (buf->b_next == NULL)
473 lastbuf = buf->b_prev;
474 else
475 buf->b_next->b_prev = buf->b_prev;
476 free_buffer(buf);
477 }
478 else
479 {
480 if (del_buf)
481 {
482 /* Free all internal variables and reset option values, to make
483 * ":bdel" compatible with Vim 5.7. */
484 free_buffer_stuff(buf, TRUE);
485
486 /* Make it look like a new buffer. */
487 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
488
489 /* Init the options when loaded again. */
490 buf->b_p_initialized = FALSE;
491 }
492 buf_clear_file(buf);
493 if (del_buf)
494 buf->b_p_bl = FALSE;
495 }
496}
497
498/*
499 * Make buffer not contain a file.
500 */
501 void
502buf_clear_file(buf)
503 buf_T *buf;
504{
505 buf->b_ml.ml_line_count = 1;
506 unchanged(buf, TRUE);
507#ifndef SHORT_FNAME
508 buf->b_shortname = FALSE;
509#endif
510 buf->b_p_eol = TRUE;
511 buf->b_start_eol = TRUE;
512#ifdef FEAT_MBYTE
513 buf->b_p_bomb = FALSE;
Bram Moolenaar83eb8852007-08-12 13:51:26 +0000514 buf->b_start_bomb = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515#endif
516 buf->b_ml.ml_mfp = NULL;
517 buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
518#ifdef FEAT_NETBEANS_INTG
519 netbeans_deleted_all_lines(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520#endif
521}
522
523/*
524 * buf_freeall() - free all things allocated for a buffer that are related to
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200525 * the file. flags:
526 * BFA_DEL buffer is going to be deleted
527 * BFA_WIPE buffer is going to be wiped out
528 * BFA_KEEP_UNDO do not free undo information
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 void
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200531buf_freeall(buf, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 buf_T *buf;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200533 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534{
535#ifdef FEAT_AUTOCMD
536 int is_curbuf = (buf == curbuf);
537
538 apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname, FALSE, buf);
539 if (!buf_valid(buf)) /* autocommands may delete the buffer */
540 return;
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200541 if ((flags & BFA_DEL) && buf->b_p_bl)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 {
543 apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname, FALSE, buf);
544 if (!buf_valid(buf)) /* autocommands may delete the buffer */
545 return;
546 }
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200547 if (flags & BFA_WIPE)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 {
549 apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
550 FALSE, buf);
551 if (!buf_valid(buf)) /* autocommands may delete the buffer */
552 return;
553 }
554# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +0000555 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 return;
557# endif
558
559 /*
560 * It's possible that autocommands change curbuf to the one being deleted.
561 * This might cause curbuf to be deleted unexpectedly. But in some cases
562 * it's OK to delete the curbuf, because a new one is obtained anyway.
563 * Therefore only return if curbuf changed to the deleted buffer.
564 */
565 if (buf == curbuf && !is_curbuf)
566 return;
567#endif
568#ifdef FEAT_DIFF
569 diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
570#endif
Bram Moolenaara971b822011-09-14 14:43:25 +0200571#ifdef FEAT_SYN_HL
Bram Moolenaar89c71222011-11-30 15:40:56 +0100572 /* Remove any ownsyntax, unless exiting. */
573 if (firstwin != NULL && curwin->w_buffer == buf)
574 reset_synblock(curwin);
Bram Moolenaara971b822011-09-14 14:43:25 +0200575#endif
Bram Moolenaarb6799ac2007-05-10 16:44:05 +0000576
577#ifdef FEAT_FOLDING
578 /* No folds in an empty buffer. */
579# ifdef FEAT_WINDOWS
580 {
581 win_T *win;
582 tabpage_T *tp;
583
584 FOR_ALL_TAB_WINDOWS(tp, win)
585 if (win->w_buffer == buf)
586 clearFolding(win);
587 }
588# else
589 if (curwin->w_buffer == buf)
590 clearFolding(curwin);
591# endif
592#endif
593
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594#ifdef FEAT_TCL
595 tcl_buffer_free(buf);
596#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 ml_close(buf, TRUE); /* close and delete the memline/memfile */
598 buf->b_ml.ml_line_count = 0; /* no lines in buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +0200599 if ((flags & BFA_KEEP_UNDO) == 0)
600 {
601 u_blockfree(buf); /* free the memory allocated for undo */
602 u_clearall(buf); /* reset all undo information */
603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +0200605 syntax_clear(&buf->b_s); /* reset syntax info */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000607 buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608}
609
610/*
611 * Free a buffer structure and the things it contains related to the buffer
612 * itself (not the file, that must have been done already).
613 */
614 static void
615free_buffer(buf)
616 buf_T *buf;
617{
618 free_buffer_stuff(buf, TRUE);
Bram Moolenaar0ba04292010-07-14 23:23:17 +0200619#ifdef FEAT_LUA
620 lua_buffer_free(buf);
621#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +0000622#ifdef FEAT_MZSCHEME
623 mzscheme_buffer_free(buf);
624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625#ifdef FEAT_PERL
626 perl_buf_free(buf);
627#endif
628#ifdef FEAT_PYTHON
629 python_buffer_free(buf);
630#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +0200631#ifdef FEAT_PYTHON3
632 python3_buffer_free(buf);
633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634#ifdef FEAT_RUBY
635 ruby_buffer_free(buf);
636#endif
Bram Moolenaarb5bf5b82004-12-24 14:35:23 +0000637#ifdef FEAT_AUTOCMD
638 aubuflocal_remove(buf);
639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640 vim_free(buf);
641}
642
643/*
644 * Free stuff in the buffer for ":bdel" and when wiping out the buffer.
645 */
646 static void
647free_buffer_stuff(buf, free_options)
648 buf_T *buf;
649 int free_options; /* free options as well */
650{
651 if (free_options)
652 {
653 clear_wininfo(buf); /* including window-local options */
654 free_buf_options(buf, TRUE);
Bram Moolenaarbeca0552010-10-27 16:18:00 +0200655#ifdef FEAT_SPELL
656 ga_clear(&buf->b_s.b_langp);
657#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 }
659#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +0000660 vars_clear(&buf->b_vars.dv_hashtab); /* free all internal variables */
661 hash_init(&buf->b_vars.dv_hashtab);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662#endif
663#ifdef FEAT_USR_CMDS
664 uc_clear(&buf->b_ucmds); /* clear local user commands */
665#endif
666#ifdef FEAT_SIGNS
667 buf_delete_signs(buf); /* delete any signs */
668#endif
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000669#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +0200670 netbeans_file_killed(buf);
Bram Moolenaard7f8f5c2009-01-06 15:14:30 +0000671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672#ifdef FEAT_LOCALMAP
673 map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
674 map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
675#endif
676#ifdef FEAT_MBYTE
677 vim_free(buf->b_start_fenc);
678 buf->b_start_fenc = NULL;
679#endif
680}
681
682/*
683 * Free the b_wininfo list for buffer "buf".
684 */
685 static void
686clear_wininfo(buf)
687 buf_T *buf;
688{
689 wininfo_T *wip;
690
691 while (buf->b_wininfo != NULL)
692 {
693 wip = buf->b_wininfo;
694 buf->b_wininfo = wip->wi_next;
695 if (wip->wi_optset)
696 {
697 clear_winopt(&wip->wi_opt);
698#ifdef FEAT_FOLDING
699 deleteFoldRecurse(&wip->wi_folds);
700#endif
701 }
702 vim_free(wip);
703 }
704}
705
706#if defined(FEAT_LISTCMDS) || defined(PROTO)
707/*
708 * Go to another buffer. Handles the result of the ATTENTION dialog.
709 */
710 void
711goto_buffer(eap, start, dir, count)
712 exarg_T *eap;
713 int start;
714 int dir;
715 int count;
716{
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 buf_T *old_curbuf = curbuf;
719
720 swap_exists_action = SEA_DIALOG;
721# endif
722 (void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
723 start, dir, count, eap->forceit);
Bram Moolenaare64ac772005-12-07 20:54:59 +0000724# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725 if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
726 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000727# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
728 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000729
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000730 /* Reset the error/interrupt/exception state here so that
731 * aborting() returns FALSE when closing a window. */
732 enter_cleanup(&cs);
733# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000734
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000735 /* Quitting means closing the split window, nothing else. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736 win_close(curwin, TRUE);
737 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000738 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000739
740# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
741 /* Restore the error/interrupt/exception state if not discarded by a
742 * new aborting error, interrupt, or uncaught exception. */
743 leave_cleanup(&cs);
744# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745 }
746 else
747 handle_swap_exists(old_curbuf);
748# endif
749}
750#endif
751
Bram Moolenaare64ac772005-12-07 20:54:59 +0000752#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000753/*
754 * Handle the situation of swap_exists_action being set.
755 * It is allowed for "old_curbuf" to be NULL or invalid.
756 */
757 void
758handle_swap_exists(old_curbuf)
759 buf_T *old_curbuf;
760{
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000761# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
762 cleanup_T cs;
763# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +0000764
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (swap_exists_action == SEA_QUIT)
766 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000767# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
768 /* Reset the error/interrupt/exception state here so that
769 * aborting() returns FALSE when closing a buffer. */
770 enter_cleanup(&cs);
771# endif
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773 /* User selected Quit at ATTENTION prompt. Go back to previous
774 * buffer. If that buffer is gone or the same as the current one,
775 * open a new, empty buffer. */
776 swap_exists_action = SEA_NONE; /* don't want it again */
Bram Moolenaar12033fb2005-12-16 21:49:31 +0000777 swap_exists_did_quit = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 close_buffer(curwin, curbuf, DOBUF_UNLOAD);
779 if (!buf_valid(old_curbuf) || old_curbuf == curbuf)
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000780 old_curbuf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (old_curbuf != NULL)
782 enter_buffer(old_curbuf);
783 /* If "old_curbuf" is NULL we are in big trouble here... */
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000784
785# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
786 /* Restore the error/interrupt/exception state if not discarded by a
787 * new aborting error, interrupt, or uncaught exception. */
788 leave_cleanup(&cs);
789# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 }
791 else if (swap_exists_action == SEA_RECOVER)
792 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000793# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
794 /* Reset the error/interrupt/exception state here so that
795 * aborting() returns FALSE when closing a buffer. */
796 enter_cleanup(&cs);
797# endif
798
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 /* User selected Recover at ATTENTION prompt. */
800 msg_scroll = TRUE;
801 ml_recover();
802 MSG_PUTS("\n"); /* don't overwrite the last message */
803 cmdline_row = msg_row;
Bram Moolenaara3227e22006-03-08 21:32:40 +0000804 do_modelines(0);
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000805
806# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
807 /* Restore the error/interrupt/exception state if not discarded by a
808 * new aborting error, interrupt, or uncaught exception. */
809 leave_cleanup(&cs);
810# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812 swap_exists_action = SEA_NONE;
813}
814#endif
815
816#if defined(FEAT_LISTCMDS) || defined(PROTO)
817/*
818 * do_bufdel() - delete or unload buffer(s)
819 *
820 * addr_count == 0: ":bdel" - delete current buffer
821 * addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
822 * buffer "end_bnr", then any other arguments.
823 * addr_count == 2: ":N,N bdel" - delete buffers in range
824 *
825 * command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
826 * DOBUF_DEL (":bdel")
827 *
828 * Returns error message or NULL
829 */
830 char_u *
831do_bufdel(command, arg, addr_count, start_bnr, end_bnr, forceit)
832 int command;
833 char_u *arg; /* pointer to extra arguments */
834 int addr_count;
835 int start_bnr; /* first buffer number in a range */
836 int end_bnr; /* buffer nr or last buffer nr in a range */
837 int forceit;
838{
839 int do_current = 0; /* delete current buffer? */
840 int deleted = 0; /* number of buffers deleted */
841 char_u *errormsg = NULL; /* return value */
842 int bnr; /* buffer number */
843 char_u *p;
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 if (addr_count == 0)
846 {
847 (void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
848 }
849 else
850 {
851 if (addr_count == 2)
852 {
853 if (*arg) /* both range and argument is not allowed */
854 return (char_u *)_(e_trailing);
855 bnr = start_bnr;
856 }
857 else /* addr_count == 1 */
858 bnr = end_bnr;
859
860 for ( ;!got_int; ui_breakcheck())
861 {
862 /*
863 * delete the current buffer last, otherwise when the
864 * current buffer is deleted, the next buffer becomes
865 * the current one and will be loaded, which may then
866 * also be deleted, etc.
867 */
868 if (bnr == curbuf->b_fnum)
869 do_current = bnr;
870 else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
871 forceit) == OK)
872 ++deleted;
873
874 /*
875 * find next buffer number to delete/unload
876 */
877 if (addr_count == 2)
878 {
879 if (++bnr > end_bnr)
880 break;
881 }
882 else /* addr_count == 1 */
883 {
884 arg = skipwhite(arg);
885 if (*arg == NUL)
886 break;
887 if (!VIM_ISDIGIT(*arg))
888 {
889 p = skiptowhite_esc(arg);
890 bnr = buflist_findpat(arg, p, command == DOBUF_WIPE, FALSE);
891 if (bnr < 0) /* failed */
892 break;
893 arg = p;
894 }
895 else
896 bnr = getdigits(&arg);
897 }
898 }
899 if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
900 FORWARD, do_current, forceit) == OK)
901 ++deleted;
902
903 if (deleted == 0)
904 {
905 if (command == DOBUF_UNLOAD)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000906 STRCPY(IObuff, _("E515: No buffers were unloaded"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 else if (command == DOBUF_DEL)
Bram Moolenaar51485f02005-06-04 21:55:20 +0000908 STRCPY(IObuff, _("E516: No buffers were deleted"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 else
Bram Moolenaar51485f02005-06-04 21:55:20 +0000910 STRCPY(IObuff, _("E517: No buffers were wiped out"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 errormsg = IObuff;
912 }
913 else if (deleted >= p_report)
914 {
915 if (command == DOBUF_UNLOAD)
916 {
917 if (deleted == 1)
918 MSG(_("1 buffer unloaded"));
919 else
920 smsg((char_u *)_("%d buffers unloaded"), deleted);
921 }
922 else if (command == DOBUF_DEL)
923 {
924 if (deleted == 1)
925 MSG(_("1 buffer deleted"));
926 else
927 smsg((char_u *)_("%d buffers deleted"), deleted);
928 }
929 else
930 {
931 if (deleted == 1)
932 MSG(_("1 buffer wiped out"));
933 else
934 smsg((char_u *)_("%d buffers wiped out"), deleted);
935 }
936 }
937 }
938
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
940 return errormsg;
941}
942
943/*
944 * Implementation of the commands for the buffer list.
945 *
946 * action == DOBUF_GOTO go to specified buffer
947 * action == DOBUF_SPLIT split window and go to specified buffer
948 * action == DOBUF_UNLOAD unload specified buffer(s)
949 * action == DOBUF_DEL delete specified buffer(s) from buffer list
950 * action == DOBUF_WIPE delete specified buffer(s) really
951 *
952 * start == DOBUF_CURRENT go to "count" buffer from current buffer
953 * start == DOBUF_FIRST go to "count" buffer from first buffer
954 * start == DOBUF_LAST go to "count" buffer from last buffer
955 * start == DOBUF_MOD go to "count" modified buffer from current buffer
956 *
957 * Return FAIL or OK.
958 */
959 int
960do_buffer(action, start, dir, count, forceit)
961 int action;
962 int start;
963 int dir; /* FORWARD or BACKWARD */
964 int count; /* buffer number or number of buffers */
965 int forceit; /* TRUE for :...! */
966{
967 buf_T *buf;
968 buf_T *bp;
969 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
970 || action == DOBUF_WIPE);
971
972 switch (start)
973 {
974 case DOBUF_FIRST: buf = firstbuf; break;
975 case DOBUF_LAST: buf = lastbuf; break;
976 default: buf = curbuf; break;
977 }
978 if (start == DOBUF_MOD) /* find next modified buffer */
979 {
980 while (count-- > 0)
981 {
982 do
983 {
984 buf = buf->b_next;
985 if (buf == NULL)
986 buf = firstbuf;
987 }
988 while (buf != curbuf && !bufIsChanged(buf));
989 }
990 if (!bufIsChanged(buf))
991 {
992 EMSG(_("E84: No modified buffer found"));
993 return FAIL;
994 }
995 }
996 else if (start == DOBUF_FIRST && count) /* find specified buffer number */
997 {
998 while (buf != NULL && buf->b_fnum != count)
999 buf = buf->b_next;
1000 }
1001 else
1002 {
1003 bp = NULL;
1004 while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
1005 {
1006 /* remember the buffer where we start, we come back there when all
1007 * buffers are unlisted. */
1008 if (bp == NULL)
1009 bp = buf;
1010 if (dir == FORWARD)
1011 {
1012 buf = buf->b_next;
1013 if (buf == NULL)
1014 buf = firstbuf;
1015 }
1016 else
1017 {
1018 buf = buf->b_prev;
1019 if (buf == NULL)
1020 buf = lastbuf;
1021 }
1022 /* don't count unlisted buffers */
1023 if (unload || buf->b_p_bl)
1024 {
1025 --count;
1026 bp = NULL; /* use this buffer as new starting point */
1027 }
1028 if (bp == buf)
1029 {
1030 /* back where we started, didn't find anything. */
1031 EMSG(_("E85: There is no listed buffer"));
1032 return FAIL;
1033 }
1034 }
1035 }
1036
1037 if (buf == NULL) /* could not find it */
1038 {
1039 if (start == DOBUF_FIRST)
1040 {
1041 /* don't warn when deleting */
1042 if (!unload)
1043 EMSGN(_("E86: Buffer %ld does not exist"), count);
1044 }
1045 else if (dir == FORWARD)
1046 EMSG(_("E87: Cannot go beyond last buffer"));
1047 else
1048 EMSG(_("E88: Cannot go before first buffer"));
1049 return FAIL;
1050 }
1051
1052#ifdef FEAT_GUI
1053 need_mouse_correct = TRUE;
1054#endif
1055
1056#ifdef FEAT_LISTCMDS
1057 /*
1058 * delete buffer buf from memory and/or the list
1059 */
1060 if (unload)
1061 {
1062 int forward;
1063 int retval;
1064
1065 /* When unloading or deleting a buffer that's already unloaded and
1066 * unlisted: fail silently. */
1067 if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
1068 return FAIL;
1069
1070 if (!forceit && bufIsChanged(buf))
1071 {
1072#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1073 if ((p_confirm || cmdmod.confirm) && p_write)
1074 {
1075 dialog_changed(buf, FALSE);
1076# ifdef FEAT_AUTOCMD
1077 if (!buf_valid(buf))
1078 /* Autocommand deleted buffer, oops! It's not changed
1079 * now. */
1080 return FAIL;
1081# endif
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001082 /* If it's still changed fail silently, the dialog already
1083 * mentioned why it fails. */
1084 if (bufIsChanged(buf))
1085 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 }
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001087 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088#endif
1089 {
1090 EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
1091 buf->b_fnum);
1092 return FAIL;
1093 }
1094 }
1095
1096 /*
1097 * If deleting the last (listed) buffer, make it empty.
1098 * The last (listed) buffer cannot be unloaded.
1099 */
1100 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
1101 if (bp->b_p_bl && bp != buf)
1102 break;
1103 if (bp == NULL && buf == curbuf)
1104 {
1105 if (action == DOBUF_UNLOAD)
1106 {
1107 EMSG(_("E90: Cannot unload last buffer"));
1108 return FAIL;
1109 }
1110
1111 /* Close any other windows on this buffer, then make it empty. */
1112#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001113 close_windows(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114#endif
1115 setpcmark();
1116 retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001117 forceit ? ECMD_FORCEIT : 0, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
1119 /*
1120 * do_ecmd() may create a new buffer, then we have to delete
1121 * the old one. But do_ecmd() may have done that already, check
1122 * if the buffer still exists.
1123 */
1124 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows == 0)
1125 close_buffer(NULL, buf, action);
1126 return retval;
1127 }
1128
1129#ifdef FEAT_WINDOWS
1130 /*
1131 * If the deleted buffer is the current one, close the current window
Bram Moolenaarf740b292006-02-16 22:11:02 +00001132 * (unless it's the only window). Repeat this so long as we end up in
1133 * a window with this buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 */
Bram Moolenaarf740b292006-02-16 22:11:02 +00001135 while (buf == curbuf
1136 && (firstwin != lastwin || first_tabpage->tp_next != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 win_close(curwin, FALSE);
1138#endif
1139
1140 /*
1141 * If the buffer to be deleted is not the current one, delete it here.
1142 */
1143 if (buf != curbuf)
1144 {
1145#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00001146 close_windows(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#endif
1148 if (buf != curbuf && buf_valid(buf) && buf->b_nwindows <= 0)
1149 close_buffer(NULL, buf, action);
1150 return OK;
1151 }
1152
1153 /*
1154 * Deleting the current buffer: Need to find another buffer to go to.
1155 * There must be another, otherwise it would have been handled above.
1156 * First use au_new_curbuf, if it is valid.
1157 * Then prefer the buffer we most recently visited.
1158 * Else try to find one that is loaded, after the current buffer,
1159 * then before the current buffer.
1160 * Finally use any buffer.
1161 */
1162 buf = NULL; /* selected buffer */
1163 bp = NULL; /* used when no loaded buffer found */
1164#ifdef FEAT_AUTOCMD
1165 if (au_new_curbuf != NULL && buf_valid(au_new_curbuf))
1166 buf = au_new_curbuf;
1167# ifdef FEAT_JUMPLIST
1168 else
1169# endif
1170#endif
1171#ifdef FEAT_JUMPLIST
1172 if (curwin->w_jumplistlen > 0)
1173 {
1174 int jumpidx;
1175
1176 jumpidx = curwin->w_jumplistidx - 1;
1177 if (jumpidx < 0)
1178 jumpidx = curwin->w_jumplistlen - 1;
1179
1180 forward = jumpidx;
1181 while (jumpidx != curwin->w_jumplistidx)
1182 {
1183 buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
1184 if (buf != NULL)
1185 {
1186 if (buf == curbuf || !buf->b_p_bl)
1187 buf = NULL; /* skip current and unlisted bufs */
1188 else if (buf->b_ml.ml_mfp == NULL)
1189 {
1190 /* skip unloaded buf, but may keep it for later */
1191 if (bp == NULL)
1192 bp = buf;
1193 buf = NULL;
1194 }
1195 }
1196 if (buf != NULL) /* found a valid buffer: stop searching */
1197 break;
1198 /* advance to older entry in jump list */
1199 if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
1200 break;
1201 if (--jumpidx < 0)
1202 jumpidx = curwin->w_jumplistlen - 1;
1203 if (jumpidx == forward) /* List exhausted for sure */
1204 break;
1205 }
1206 }
1207#endif
1208
1209 if (buf == NULL) /* No previous buffer, Try 2'nd approach */
1210 {
1211 forward = TRUE;
1212 buf = curbuf->b_next;
1213 for (;;)
1214 {
1215 if (buf == NULL)
1216 {
1217 if (!forward) /* tried both directions */
1218 break;
1219 buf = curbuf->b_prev;
1220 forward = FALSE;
1221 continue;
1222 }
1223 /* in non-help buffer, try to skip help buffers, and vv */
1224 if (buf->b_help == curbuf->b_help && buf->b_p_bl)
1225 {
1226 if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
1227 break;
1228 if (bp == NULL) /* remember unloaded buf for later */
1229 bp = buf;
1230 }
1231 if (forward)
1232 buf = buf->b_next;
1233 else
1234 buf = buf->b_prev;
1235 }
1236 }
1237 if (buf == NULL) /* No loaded buffer, use unloaded one */
1238 buf = bp;
1239 if (buf == NULL) /* No loaded buffer, find listed one */
1240 {
1241 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
1242 if (buf->b_p_bl && buf != curbuf)
1243 break;
1244 }
1245 if (buf == NULL) /* Still no buffer, just take one */
1246 {
1247 if (curbuf->b_next != NULL)
1248 buf = curbuf->b_next;
1249 else
1250 buf = curbuf->b_prev;
1251 }
1252 }
1253
1254 /*
1255 * make buf current buffer
1256 */
1257 if (action == DOBUF_SPLIT) /* split window first */
1258 {
1259# ifdef FEAT_WINDOWS
Bram Moolenaarf2330482008-06-24 20:19:36 +00001260 /* If 'switchbuf' contains "useopen": jump to first window containing
1261 * "buf" if one exists */
1262 if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001263 return OK;
Bram Moolenaar9381ab72008-11-12 11:52:19 +00001264 /* If 'switchbuf' contains "usetab": jump to first window in any tab
Bram Moolenaarf2330482008-06-24 20:19:36 +00001265 * page containing "buf" if one exists */
1266 if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 return OK;
1268 if (win_split(0, 0) == FAIL)
1269# endif
1270 return FAIL;
1271 }
1272#endif
1273
1274 /* go to current buffer - nothing to do */
1275 if (buf == curbuf)
1276 return OK;
1277
1278 /*
1279 * Check if the current buffer may be abandoned.
1280 */
1281 if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
1282 {
1283#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1284 if ((p_confirm || cmdmod.confirm) && p_write)
1285 {
1286 dialog_changed(curbuf, FALSE);
1287# ifdef FEAT_AUTOCMD
1288 if (!buf_valid(buf))
1289 /* Autocommand deleted buffer, oops! */
1290 return FAIL;
1291# endif
1292 }
1293 if (bufIsChanged(curbuf))
1294#endif
1295 {
1296 EMSG(_(e_nowrtmsg));
1297 return FAIL;
1298 }
1299 }
1300
1301 /* Go to the other buffer. */
1302 set_curbuf(buf, action);
1303
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001304#if defined(FEAT_LISTCMDS) \
1305 && (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 if (action == DOBUF_SPLIT)
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001307 {
1308 RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
1309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310#endif
1311
1312#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1313 if (aborting()) /* autocmds may abort script processing */
1314 return FAIL;
1315#endif
1316
1317 return OK;
1318}
1319
1320#endif /* FEAT_LISTCMDS */
1321
1322/*
1323 * Set current buffer to "buf". Executes autocommands and closes current
1324 * buffer. "action" tells how to close the current buffer:
1325 * DOBUF_GOTO free or hide it
1326 * DOBUF_SPLIT nothing
1327 * DOBUF_UNLOAD unload it
1328 * DOBUF_DEL delete it
1329 * DOBUF_WIPE wipe it out
1330 */
1331 void
1332set_curbuf(buf, action)
1333 buf_T *buf;
1334 int action;
1335{
1336 buf_T *prevbuf;
1337 int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
1338 || action == DOBUF_WIPE);
1339
1340 setpcmark();
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001341 if (!cmdmod.keepalt)
1342 curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
Bram Moolenaar701f7af2008-11-15 13:12:07 +00001343 buflist_altfpos(curwin); /* remember curpos */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344
1345#ifdef FEAT_VISUAL
1346 /* Don't restart Select mode after switching to another buffer. */
1347 VIsual_reselect = FALSE;
1348#endif
1349
1350 /* close_windows() or apply_autocmds() may change curbuf */
1351 prevbuf = curbuf;
1352
1353#ifdef FEAT_AUTOCMD
1354 apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf);
1355# ifdef FEAT_EVAL
1356 if (buf_valid(prevbuf) && !aborting())
1357# else
1358 if (buf_valid(prevbuf))
1359# endif
1360#endif
1361 {
Bram Moolenaara971b822011-09-14 14:43:25 +02001362#ifdef FEAT_SYN_HL
1363 if (prevbuf == curwin->w_buffer)
1364 reset_synblock(curwin);
1365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366#ifdef FEAT_WINDOWS
1367 if (unload)
Bram Moolenaarf740b292006-02-16 22:11:02 +00001368 close_windows(prevbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369#endif
1370#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
1371 if (buf_valid(prevbuf) && !aborting())
1372#else
1373 if (buf_valid(prevbuf))
1374#endif
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001375 {
1376 if (prevbuf == curbuf)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001377 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
1379 unload ? action : (action == DOBUF_GOTO
1380 && !P_HID(prevbuf)
1381 && !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0);
Bram Moolenaar607a95ed2006-03-28 20:57:42 +00001382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 }
1384#ifdef FEAT_AUTOCMD
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001385 /* An autocommand may have deleted "buf", already entered it (e.g., when
1386 * it did ":bunload") or aborted the script processing! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387# ifdef FEAT_EVAL
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001388 if (buf_valid(buf) && buf != curbuf && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389# else
Bram Moolenaar5bd266c2008-09-01 15:33:17 +00001390 if (buf_valid(buf) && buf != curbuf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391# endif
1392#endif
1393 enter_buffer(buf);
1394}
1395
1396/*
1397 * Enter a new current buffer.
1398 * Old curbuf must have been abandoned already!
1399 */
1400 void
1401enter_buffer(buf)
1402 buf_T *buf;
1403{
1404 /* Copy buffer and window local option values. Not for a help buffer. */
1405 buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
1406 if (!buf->b_help)
1407 get_winopts(buf);
1408#ifdef FEAT_FOLDING
1409 else
1410 /* Remove all folds in the window. */
1411 clearFolding(curwin);
1412 foldUpdateAll(curwin); /* update folds (later). */
1413#endif
1414
1415 /* Get the buffer in the current window. */
1416 curwin->w_buffer = buf;
1417 curbuf = buf;
1418 ++curbuf->b_nwindows;
1419
1420#ifdef FEAT_DIFF
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001421 if (curwin->w_p_diff)
1422 diff_buf_add(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423#endif
1424
Bram Moolenaara971b822011-09-14 14:43:25 +02001425#ifdef FEAT_SYN_HL
1426 curwin->w_s = &(buf->b_s);
1427#endif
1428
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 /* Cursor on first line by default. */
1430 curwin->w_cursor.lnum = 1;
1431 curwin->w_cursor.col = 0;
1432#ifdef FEAT_VIRTUALEDIT
1433 curwin->w_cursor.coladd = 0;
1434#endif
1435 curwin->w_set_curswant = TRUE;
Bram Moolenaard4153d42008-11-15 15:06:17 +00001436#ifdef FEAT_AUTOCMD
1437 curwin->w_topline_was_set = FALSE;
1438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439
Bram Moolenaar89c0ea42010-02-24 16:58:36 +01001440 /* mark cursor position as being invalid */
1441 curwin->w_valid = 0;
1442
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443 /* Make sure the buffer is loaded. */
1444 if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001445 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001446#ifdef FEAT_AUTOCMD
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001447 /* If there is no filetype, allow for detecting one. Esp. useful for
1448 * ":ball" used in a autocommand. If there already is a filetype we
1449 * might prefer to keep it. */
1450 if (*curbuf->b_p_ft == NUL)
1451 did_filetype = FALSE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001452#endif
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001453
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001454 open_buffer(FALSE, NULL, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00001455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 else
1457 {
Bram Moolenaar55b7cf82006-09-09 12:52:42 +00001458 if (!msg_silent)
1459 need_fileinfo = TRUE; /* display file info after redraw */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 (void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
1461#ifdef FEAT_AUTOCMD
1462 curwin->w_topline = 1;
1463# ifdef FEAT_DIFF
1464 curwin->w_topfill = 0;
1465# endif
1466 apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
1467 apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
1468#endif
1469 }
1470
1471 /* If autocommands did not change the cursor position, restore cursor lnum
1472 * and possibly cursor col. */
1473 if (curwin->w_cursor.lnum == 1 && inindent(0))
1474 buflist_getfpos();
1475
1476 check_arg_idx(curwin); /* check for valid arg_idx */
1477#ifdef FEAT_TITLE
1478 maketitle();
1479#endif
1480#ifdef FEAT_AUTOCMD
Bram Moolenaard4153d42008-11-15 15:06:17 +00001481 /* when autocmds didn't change it */
1482 if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483#endif
1484 scroll_cursor_halfway(FALSE); /* redisplay at correct position */
1485
Bram Moolenaar009b2592004-10-24 19:18:58 +00001486#ifdef FEAT_NETBEANS_INTG
1487 /* Send fileOpened event because we've changed buffers. */
Bram Moolenaarb26e6322010-05-22 21:34:09 +02001488 netbeans_file_activated(curbuf);
Bram Moolenaar009b2592004-10-24 19:18:58 +00001489#endif
1490
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001491 /* Change directories when the 'acd' option is set. */
1492 DO_AUTOCHDIR
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493
1494#ifdef FEAT_KEYMAP
1495 if (curbuf->b_kmap_state & KEYMAP_INIT)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001496 (void)keymap_init();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497#endif
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001498#ifdef FEAT_SPELL
1499 /* May need to set the spell language. Can only do this after the buffer
1500 * has been properly setup. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001501 if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
1502 (void)did_set_spelllang(curwin);
Bram Moolenaar706cdeb2007-05-06 21:55:31 +00001503#endif
1504
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 redraw_later(NOT_VALID);
1506}
1507
Bram Moolenaar498efdb2006-09-05 14:31:54 +00001508#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
1509/*
1510 * Change to the directory of the current buffer.
1511 */
1512 void
1513do_autochdir()
1514{
1515 if (curbuf->b_ffname != NULL && vim_chdirfile(curbuf->b_ffname) == OK)
1516 shorten_fnames(TRUE);
1517}
1518#endif
1519
Bram Moolenaar071d4272004-06-13 20:20:40 +00001520/*
1521 * functions for dealing with the buffer list
1522 */
1523
1524/*
1525 * Add a file name to the buffer list. Return a pointer to the buffer.
1526 * If the same file name already exists return a pointer to that buffer.
1527 * If it does not exist, or if fname == NULL, a new entry is created.
1528 * If (flags & BLN_CURBUF) is TRUE, may use current buffer.
1529 * If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
1530 * If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001531 * This is the ONLY way to create a new buffer.
1532 */
1533static int top_file_num = 1; /* highest file number */
1534
1535 buf_T *
1536buflist_new(ffname, sfname, lnum, flags)
1537 char_u *ffname; /* full path of fname or relative */
1538 char_u *sfname; /* short fname or NULL */
1539 linenr_T lnum; /* preferred cursor line */
1540 int flags; /* BLN_ defines */
1541{
1542 buf_T *buf;
1543#ifdef UNIX
1544 struct stat st;
1545#endif
1546
1547 fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
1548
1549 /*
1550 * If file name already exists in the list, update the entry.
1551 */
1552#ifdef UNIX
1553 /* On Unix we can use inode numbers when the file exists. Works better
1554 * for hard links. */
1555 if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
1556 st.st_dev = (dev_T)-1;
1557#endif
1558 if (ffname != NULL && !(flags & BLN_DUMMY) && (buf =
1559#ifdef UNIX
1560 buflist_findname_stat(ffname, &st)
1561#else
1562 buflist_findname(ffname)
1563#endif
1564 ) != NULL)
1565 {
1566 vim_free(ffname);
1567 if (lnum != 0)
1568 buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
1569 /* copy the options now, if 'cpo' doesn't have 's' and not done
1570 * already */
1571 buf_copy_options(buf, 0);
1572 if ((flags & BLN_LISTED) && !buf->b_p_bl)
1573 {
1574 buf->b_p_bl = TRUE;
1575#ifdef FEAT_AUTOCMD
1576 if (!(flags & BLN_DUMMY))
1577 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1578#endif
1579 }
1580 return buf;
1581 }
1582
1583 /*
1584 * If the current buffer has no name and no contents, use the current
1585 * buffer. Otherwise: Need to allocate a new buffer structure.
1586 *
1587 * This is the ONLY place where a new buffer structure is allocated!
Bram Moolenaar4770d092006-01-12 23:22:24 +00001588 * (A spell file buffer is allocated in spell.c, but that's not a normal
1589 * buffer.)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 */
1591 buf = NULL;
1592 if ((flags & BLN_CURBUF)
1593 && curbuf != NULL
1594 && curbuf->b_ffname == NULL
1595 && curbuf->b_nwindows <= 1
1596 && (curbuf->b_ml.ml_mfp == NULL || bufempty()))
1597 {
1598 buf = curbuf;
1599#ifdef FEAT_AUTOCMD
1600 /* It's like this buffer is deleted. Watch out for autocommands that
1601 * change curbuf! If that happens, allocate a new buffer anyway. */
1602 if (curbuf->b_p_bl)
1603 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
1604 if (buf == curbuf)
1605 apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
1606# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001607 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 return NULL;
1609# endif
1610#endif
1611#ifdef FEAT_QUICKFIX
1612# ifdef FEAT_AUTOCMD
1613 if (buf == curbuf)
1614# endif
1615 {
1616 /* Make sure 'bufhidden' and 'buftype' are empty */
1617 clear_string_option(&buf->b_p_bh);
1618 clear_string_option(&buf->b_p_bt);
1619 }
1620#endif
1621 }
1622 if (buf != curbuf || curbuf == NULL)
1623 {
1624 buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
1625 if (buf == NULL)
1626 {
1627 vim_free(ffname);
1628 return NULL;
1629 }
1630 }
1631
1632 if (ffname != NULL)
1633 {
1634 buf->b_ffname = ffname;
1635 buf->b_sfname = vim_strsave(sfname);
1636 }
1637
1638 clear_wininfo(buf);
1639 buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
1640
1641 if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
1642 || buf->b_wininfo == NULL)
1643 {
1644 vim_free(buf->b_ffname);
1645 buf->b_ffname = NULL;
1646 vim_free(buf->b_sfname);
1647 buf->b_sfname = NULL;
1648 if (buf != curbuf)
1649 free_buffer(buf);
1650 return NULL;
1651 }
1652
1653 if (buf == curbuf)
1654 {
1655 /* free all things allocated for this buffer */
Bram Moolenaar59f931e2010-07-24 20:27:03 +02001656 buf_freeall(buf, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001657 if (buf != curbuf) /* autocommands deleted the buffer! */
1658 return NULL;
1659#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001660 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 return NULL;
1662#endif
1663 /* buf->b_nwindows = 0; why was this here? */
1664 free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
1665#ifdef FEAT_KEYMAP
1666 /* need to reload lmaps and set b:keymap_name */
1667 curbuf->b_kmap_state |= KEYMAP_INIT;
1668#endif
1669 }
1670 else
1671 {
1672 /*
1673 * put new buffer at the end of the buffer list
1674 */
1675 buf->b_next = NULL;
1676 if (firstbuf == NULL) /* buffer list is empty */
1677 {
1678 buf->b_prev = NULL;
1679 firstbuf = buf;
1680 }
1681 else /* append new buffer at end of list */
1682 {
1683 lastbuf->b_next = buf;
1684 buf->b_prev = lastbuf;
1685 }
1686 lastbuf = buf;
1687
1688 buf->b_fnum = top_file_num++;
1689 if (top_file_num < 0) /* wrap around (may cause duplicates) */
1690 {
1691 EMSG(_("W14: Warning: List of file names overflow"));
1692 if (emsg_silent == 0)
1693 {
1694 out_flush();
1695 ui_delay(3000L, TRUE); /* make sure it is noticed */
1696 }
1697 top_file_num = 1;
1698 }
1699
1700 /*
1701 * Always copy the options from the current buffer.
1702 */
1703 buf_copy_options(buf, BCO_ALWAYS);
1704 }
1705
1706 buf->b_wininfo->wi_fpos.lnum = lnum;
1707 buf->b_wininfo->wi_win = curwin;
1708
1709#ifdef FEAT_EVAL
Bram Moolenaar1fad5d42005-01-25 21:44:33 +00001710 init_var_dict(&buf->b_vars, &buf->b_bufvar); /* init b: variables */
1711#endif
1712#ifdef FEAT_SYN_HL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001713 hash_init(&buf->b_s.b_keywtab);
1714 hash_init(&buf->b_s.b_keywtab_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715#endif
1716
1717 buf->b_fname = buf->b_sfname;
1718#ifdef UNIX
1719 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001720 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 else
1722 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00001723 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 buf->b_dev = st.st_dev;
1725 buf->b_ino = st.st_ino;
1726 }
1727#endif
1728 buf->b_u_synced = TRUE;
1729 buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
Bram Moolenaar81695252004-12-29 20:58:21 +00001730 if (flags & BLN_DUMMY)
1731 buf->b_flags |= BF_DUMMY;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 buf_clear_file(buf);
1733 clrallmarks(buf); /* clear marks */
1734 fmarks_check_names(buf); /* check file marks for this file */
1735 buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
1736#ifdef FEAT_AUTOCMD
1737 if (!(flags & BLN_DUMMY))
1738 {
1739 apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf);
1740 if (flags & BLN_LISTED)
1741 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf);
1742# ifdef FEAT_EVAL
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00001743 if (aborting()) /* autocmds may abort script processing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 return NULL;
1745# endif
1746 }
1747#endif
1748
1749 return buf;
1750}
1751
1752/*
1753 * Free the memory for the options of a buffer.
1754 * If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
1755 * 'fileencoding'.
1756 */
1757 void
1758free_buf_options(buf, free_p_ff)
1759 buf_T *buf;
1760 int free_p_ff;
1761{
1762 if (free_p_ff)
1763 {
1764#ifdef FEAT_MBYTE
1765 clear_string_option(&buf->b_p_fenc);
1766#endif
1767 clear_string_option(&buf->b_p_ff);
1768#ifdef FEAT_QUICKFIX
1769 clear_string_option(&buf->b_p_bh);
1770 clear_string_option(&buf->b_p_bt);
1771#endif
1772 }
1773#ifdef FEAT_FIND_ID
1774 clear_string_option(&buf->b_p_def);
1775 clear_string_option(&buf->b_p_inc);
1776# ifdef FEAT_EVAL
1777 clear_string_option(&buf->b_p_inex);
1778# endif
1779#endif
1780#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
1781 clear_string_option(&buf->b_p_inde);
1782 clear_string_option(&buf->b_p_indk);
1783#endif
Bram Moolenaar371d5402006-03-20 21:47:49 +00001784#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
1785 clear_string_option(&buf->b_p_bexpr);
1786#endif
Bram Moolenaar49771f42010-07-20 17:32:38 +02001787#if defined(FEAT_CRYPT)
1788 clear_string_option(&buf->b_p_cm);
1789#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00001790#if defined(FEAT_EVAL)
1791 clear_string_option(&buf->b_p_fex);
1792#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793#ifdef FEAT_CRYPT
1794 clear_string_option(&buf->b_p_key);
1795#endif
1796 clear_string_option(&buf->b_p_kp);
1797 clear_string_option(&buf->b_p_mps);
1798 clear_string_option(&buf->b_p_fo);
Bram Moolenaar86b68352004-12-27 21:59:20 +00001799 clear_string_option(&buf->b_p_flp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 clear_string_option(&buf->b_p_isk);
1801#ifdef FEAT_KEYMAP
1802 clear_string_option(&buf->b_p_keymap);
1803 ga_clear(&buf->b_kmap_ga);
1804#endif
1805#ifdef FEAT_COMMENTS
1806 clear_string_option(&buf->b_p_com);
1807#endif
1808#ifdef FEAT_FOLDING
1809 clear_string_option(&buf->b_p_cms);
1810#endif
1811 clear_string_option(&buf->b_p_nf);
1812#ifdef FEAT_SYN_HL
1813 clear_string_option(&buf->b_p_syn);
Bram Moolenaarf71a3db2006-03-12 21:50:18 +00001814#endif
1815#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +02001816 clear_string_option(&buf->b_s.b_p_spc);
1817 clear_string_option(&buf->b_s.b_p_spf);
1818 vim_free(buf->b_s.b_cap_prog);
1819 buf->b_s.b_cap_prog = NULL;
1820 clear_string_option(&buf->b_s.b_p_spl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821#endif
1822#ifdef FEAT_SEARCHPATH
1823 clear_string_option(&buf->b_p_sua);
1824#endif
1825#ifdef FEAT_AUTOCMD
1826 clear_string_option(&buf->b_p_ft);
1827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828#ifdef FEAT_CINDENT
1829 clear_string_option(&buf->b_p_cink);
1830 clear_string_option(&buf->b_p_cino);
1831#endif
1832#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
1833 clear_string_option(&buf->b_p_cinw);
1834#endif
1835#ifdef FEAT_INS_EXPAND
1836 clear_string_option(&buf->b_p_cpt);
1837#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001838#ifdef FEAT_COMPL_FUNC
1839 clear_string_option(&buf->b_p_cfu);
Bram Moolenaare344bea2005-09-01 20:46:49 +00001840 clear_string_option(&buf->b_p_ofu);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842#ifdef FEAT_QUICKFIX
1843 clear_string_option(&buf->b_p_gp);
1844 clear_string_option(&buf->b_p_mp);
1845 clear_string_option(&buf->b_p_efm);
1846#endif
1847 clear_string_option(&buf->b_p_ep);
1848 clear_string_option(&buf->b_p_path);
1849 clear_string_option(&buf->b_p_tags);
1850#ifdef FEAT_INS_EXPAND
1851 clear_string_option(&buf->b_p_dict);
1852 clear_string_option(&buf->b_p_tsr);
1853#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001854#ifdef FEAT_TEXTOBJ
1855 clear_string_option(&buf->b_p_qe);
1856#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 buf->b_p_ar = -1;
1858}
1859
1860/*
1861 * get alternate file n
1862 * set linenr to lnum or altfpos.lnum if lnum == 0
1863 * also set cursor column to altfpos.col if 'startofline' is not set.
1864 * if (options & GETF_SETMARK) call setpcmark()
1865 * if (options & GETF_ALT) we are jumping to an alternate file.
1866 * if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
1867 *
1868 * return FAIL for failure, OK for success
1869 */
1870 int
1871buflist_getfile(n, lnum, options, forceit)
1872 int n;
1873 linenr_T lnum;
1874 int options;
1875 int forceit;
1876{
1877 buf_T *buf;
1878#ifdef FEAT_WINDOWS
1879 win_T *wp = NULL;
1880#endif
1881 pos_T *fpos;
1882 colnr_T col;
1883
1884 buf = buflist_findnr(n);
1885 if (buf == NULL)
1886 {
1887 if ((options & GETF_ALT) && n == 0)
1888 EMSG(_(e_noalt));
1889 else
1890 EMSGN(_("E92: Buffer %ld not found"), n);
1891 return FAIL;
1892 }
1893
1894 /* if alternate file is the current buffer, nothing to do */
1895 if (buf == curbuf)
1896 return OK;
1897
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001898 if (text_locked())
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001899 {
Bram Moolenaar2d3f4892006-01-20 23:02:51 +00001900 text_locked_msg();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901 return FAIL;
Bram Moolenaar05a7bb32006-01-19 22:09:32 +00001902 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001903#ifdef FEAT_AUTOCMD
1904 if (curbuf_locked())
1905 return FAIL;
1906#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907
1908 /* altfpos may be changed by getfile(), get it now */
1909 if (lnum == 0)
1910 {
1911 fpos = buflist_findfpos(buf);
1912 lnum = fpos->lnum;
1913 col = fpos->col;
1914 }
1915 else
1916 col = 0;
1917
1918#ifdef FEAT_WINDOWS
1919 if (options & GETF_SWITCH)
1920 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001921 /* If 'switchbuf' contains "useopen": jump to first window containing
1922 * "buf" if one exists */
1923 if (swb_flags & SWB_USEOPEN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 wp = buf_jump_open_win(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001925 /* If 'switchbuf' contians "usetab": jump to first window in any tab
1926 * page containing "buf" if one exists */
1927 if (wp == NULL && (swb_flags & SWB_USETAB))
Bram Moolenaar779b74b2006-04-10 14:55:34 +00001928 wp = buf_jump_open_tab(buf);
Bram Moolenaarf2330482008-06-24 20:19:36 +00001929 /* If 'switchbuf' contains "split" or "newtab" and the current buffer
1930 * isn't empty: open new window */
1931 if (wp == NULL && (swb_flags & (SWB_SPLIT | SWB_NEWTAB)) && !bufempty())
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 {
Bram Moolenaarf2330482008-06-24 20:19:36 +00001933 if (swb_flags & SWB_NEWTAB) /* Open in a new tab */
1934 tabpage_new();
1935 else if (win_split(0, 0) == FAIL) /* Open in a new window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 return FAIL;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001937 RESET_BINDING(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 }
1939 }
1940#endif
1941
1942 ++RedrawingDisabled;
1943 if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
1944 lnum, forceit) <= 0)
1945 {
1946 --RedrawingDisabled;
1947
1948 /* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
1949 if (!p_sol && col != 0)
1950 {
1951 curwin->w_cursor.col = col;
1952 check_cursor_col();
1953#ifdef FEAT_VIRTUALEDIT
1954 curwin->w_cursor.coladd = 0;
1955#endif
1956 curwin->w_set_curswant = TRUE;
1957 }
1958 return OK;
1959 }
1960 --RedrawingDisabled;
1961 return FAIL;
1962}
1963
1964/*
1965 * go to the last know line number for the current buffer
1966 */
1967 void
1968buflist_getfpos()
1969{
1970 pos_T *fpos;
1971
1972 fpos = buflist_findfpos(curbuf);
1973
1974 curwin->w_cursor.lnum = fpos->lnum;
1975 check_cursor_lnum();
1976
1977 if (p_sol)
1978 curwin->w_cursor.col = 0;
1979 else
1980 {
1981 curwin->w_cursor.col = fpos->col;
1982 check_cursor_col();
1983#ifdef FEAT_VIRTUALEDIT
1984 curwin->w_cursor.coladd = 0;
1985#endif
1986 curwin->w_set_curswant = TRUE;
1987 }
1988}
1989
Bram Moolenaar81695252004-12-29 20:58:21 +00001990#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
1991/*
1992 * Find file in buffer list by name (it has to be for the current window).
1993 * Returns NULL if not found.
1994 */
1995 buf_T *
1996buflist_findname_exp(fname)
1997 char_u *fname;
1998{
1999 char_u *ffname;
2000 buf_T *buf = NULL;
2001
2002 /* First make the name into a full path name */
2003 ffname = FullName_save(fname,
2004#ifdef UNIX
2005 TRUE /* force expansion, get rid of symbolic links */
2006#else
2007 FALSE
2008#endif
2009 );
2010 if (ffname != NULL)
2011 {
2012 buf = buflist_findname(ffname);
2013 vim_free(ffname);
2014 }
2015 return buf;
2016}
2017#endif
2018
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019/*
2020 * Find file in buffer list by name (it has to be for the current window).
2021 * "ffname" must have a full path.
Bram Moolenaar81695252004-12-29 20:58:21 +00002022 * Skips dummy buffers.
2023 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 */
2025 buf_T *
2026buflist_findname(ffname)
2027 char_u *ffname;
2028{
2029#ifdef UNIX
2030 struct stat st;
2031
2032 if (mch_stat((char *)ffname, &st) < 0)
2033 st.st_dev = (dev_T)-1;
2034 return buflist_findname_stat(ffname, &st);
2035}
2036
2037/*
2038 * Same as buflist_findname(), but pass the stat structure to avoid getting it
2039 * twice for the same file.
Bram Moolenaar81695252004-12-29 20:58:21 +00002040 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041 */
2042 static buf_T *
2043buflist_findname_stat(ffname, stp)
2044 char_u *ffname;
2045 struct stat *stp;
2046{
2047#endif
2048 buf_T *buf;
2049
2050 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar81695252004-12-29 20:58:21 +00002051 if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052#ifdef UNIX
2053 , stp
2054#endif
2055 ))
2056 return buf;
2057 return NULL;
2058}
2059
2060#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) || defined(PROTO)
2061/*
2062 * Find file in buffer list by a regexp pattern.
2063 * Return fnum of the found buffer.
2064 * Return < 0 for error.
2065 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002066 int
2067buflist_findpat(pattern, pattern_end, unlisted, diffmode)
2068 char_u *pattern;
2069 char_u *pattern_end; /* pointer to first char after pattern */
2070 int unlisted; /* find unlisted buffers */
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00002071 int diffmode UNUSED; /* find diff-mode buffers only */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072{
2073 buf_T *buf;
2074 regprog_T *prog;
2075 int match = -1;
2076 int find_listed;
2077 char_u *pat;
2078 char_u *patend;
2079 int attempt;
2080 char_u *p;
2081 int toggledollar;
2082
2083 if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
2084 {
2085 if (*pattern == '%')
2086 match = curbuf->b_fnum;
2087 else
2088 match = curwin->w_alt_fnum;
2089#ifdef FEAT_DIFF
2090 if (diffmode && !diff_mode_buf(buflist_findnr(match)))
2091 match = -1;
2092#endif
2093 }
2094
2095 /*
2096 * Try four ways of matching a listed buffer:
2097 * attempt == 0: without '^' or '$' (at any position)
Bram Moolenaarb6799ac2007-05-10 16:44:05 +00002098 * attempt == 1: with '^' at start (only at position 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 * attempt == 2: with '$' at end (only match at end)
2100 * attempt == 3: with '^' at start and '$' at end (only full match)
2101 * Repeat this for finding an unlisted buffer if there was no matching
2102 * listed buffer.
2103 */
2104 else
2105 {
2106 pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
2107 if (pat == NULL)
2108 return -1;
2109 patend = pat + STRLEN(pat) - 1;
2110 toggledollar = (patend > pat && *patend == '$');
2111
2112 /* First try finding a listed buffer. If not found and "unlisted"
2113 * is TRUE, try finding an unlisted buffer. */
2114 find_listed = TRUE;
2115 for (;;)
2116 {
2117 for (attempt = 0; attempt <= 3; ++attempt)
2118 {
2119 /* may add '^' and '$' */
2120 if (toggledollar)
2121 *patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
2122 p = pat;
2123 if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
2124 ++p;
2125 prog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
2126 if (prog == NULL)
2127 {
2128 vim_free(pat);
2129 return -1;
2130 }
2131
2132 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2133 if (buf->b_p_bl == find_listed
2134#ifdef FEAT_DIFF
2135 && (!diffmode || diff_mode_buf(buf))
2136#endif
2137 && buflist_match(prog, buf) != NULL)
2138 {
2139 if (match >= 0) /* already found a match */
2140 {
2141 match = -2;
2142 break;
2143 }
2144 match = buf->b_fnum; /* remember first match */
2145 }
2146
2147 vim_free(prog);
2148 if (match >= 0) /* found one match */
2149 break;
2150 }
2151
2152 /* Only search for unlisted buffers if there was no match with
2153 * a listed buffer. */
2154 if (!unlisted || !find_listed || match != -1)
2155 break;
2156 find_listed = FALSE;
2157 }
2158
2159 vim_free(pat);
2160 }
2161
2162 if (match == -2)
2163 EMSG2(_("E93: More than one match for %s"), pattern);
2164 else if (match < 0)
2165 EMSG2(_("E94: No matching buffer for %s"), pattern);
2166 return match;
2167}
2168#endif
2169
2170#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2171
2172/*
2173 * Find all buffer names that match.
2174 * For command line expansion of ":buf" and ":sbuf".
2175 * Return OK if matches found, FAIL otherwise.
2176 */
2177 int
2178ExpandBufnames(pat, num_file, file, options)
2179 char_u *pat;
2180 int *num_file;
2181 char_u ***file;
2182 int options;
2183{
2184 int count = 0;
2185 buf_T *buf;
2186 int round;
2187 char_u *p;
2188 int attempt;
2189 regprog_T *prog;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002190 char_u *patc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
2192 *num_file = 0; /* return values in case of FAIL */
2193 *file = NULL;
2194
Bram Moolenaar05159a02005-02-26 23:04:13 +00002195 /* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
2196 if (*pat == '^')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00002198 patc = alloc((unsigned)STRLEN(pat) + 11);
2199 if (patc == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00002201 STRCPY(patc, "\\(^\\|[\\/]\\)");
2202 STRCPY(patc + 11, pat + 1);
2203 }
2204 else
2205 patc = pat;
2206
2207 /*
2208 * attempt == 0: try match with '\<', match at start of word
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002209 * attempt == 1: try match without '\<', match anywhere
Bram Moolenaar05159a02005-02-26 23:04:13 +00002210 */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002211 for (attempt = 0; attempt <= 1; ++attempt)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002212 {
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002213 if (attempt > 0 && patc == pat)
Bram Moolenaar05159a02005-02-26 23:04:13 +00002214 break; /* there was no anchor, no need to try again */
Bram Moolenaard8a4e562005-05-18 22:06:55 +00002215 prog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002216 if (prog == NULL)
2217 {
2218 if (patc != pat)
2219 vim_free(patc);
2220 return FAIL;
2221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222
2223 /*
2224 * round == 1: Count the matches.
2225 * round == 2: Build the array to keep the matches.
2226 */
2227 for (round = 1; round <= 2; ++round)
2228 {
2229 count = 0;
2230 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2231 {
2232 if (!buf->b_p_bl) /* skip unlisted buffers */
2233 continue;
2234 p = buflist_match(prog, buf);
2235 if (p != NULL)
2236 {
2237 if (round == 1)
2238 ++count;
2239 else
2240 {
2241 if (options & WILD_HOME_REPLACE)
2242 p = home_replace_save(buf, p);
2243 else
2244 p = vim_strsave(p);
2245 (*file)[count++] = p;
2246 }
2247 }
2248 }
2249 if (count == 0) /* no match found, break here */
2250 break;
2251 if (round == 1)
2252 {
2253 *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
2254 if (*file == NULL)
2255 {
2256 vim_free(prog);
Bram Moolenaar05159a02005-02-26 23:04:13 +00002257 if (patc != pat)
2258 vim_free(patc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 return FAIL;
2260 }
2261 }
2262 }
2263 vim_free(prog);
2264 if (count) /* match(es) found, break here */
2265 break;
2266 }
2267
Bram Moolenaar05159a02005-02-26 23:04:13 +00002268 if (patc != pat)
2269 vim_free(patc);
2270
Bram Moolenaar071d4272004-06-13 20:20:40 +00002271 *num_file = count;
2272 return (count == 0 ? FAIL : OK);
2273}
2274
2275#endif /* FEAT_CMDL_COMPL */
2276
2277#ifdef HAVE_BUFLIST_MATCH
2278/*
2279 * Check for a match on the file name for buffer "buf" with regprog "prog".
2280 */
2281 static char_u *
2282buflist_match(prog, buf)
2283 regprog_T *prog;
2284 buf_T *buf;
2285{
2286 char_u *match;
2287
2288 /* First try the short file name, then the long file name. */
2289 match = fname_match(prog, buf->b_sfname);
2290 if (match == NULL)
2291 match = fname_match(prog, buf->b_ffname);
2292
2293 return match;
2294}
2295
2296/*
2297 * Try matching the regexp in "prog" with file name "name".
2298 * Return "name" when there is a match, NULL when not.
2299 */
2300 static char_u *
2301fname_match(prog, name)
2302 regprog_T *prog;
2303 char_u *name;
2304{
2305 char_u *match = NULL;
2306 char_u *p;
2307 regmatch_T regmatch;
2308
2309 if (name != NULL)
2310 {
2311 regmatch.regprog = prog;
2312#ifdef CASE_INSENSITIVE_FILENAME
2313 regmatch.rm_ic = TRUE; /* Always ignore case */
2314#else
2315 regmatch.rm_ic = FALSE; /* Never ignore case */
2316#endif
2317
2318 if (vim_regexec(&regmatch, name, (colnr_T)0))
2319 match = name;
2320 else
2321 {
2322 /* Replace $(HOME) with '~' and try matching again. */
2323 p = home_replace_save(NULL, name);
2324 if (p != NULL && vim_regexec(&regmatch, p, (colnr_T)0))
2325 match = name;
2326 vim_free(p);
2327 }
2328 }
2329
2330 return match;
2331}
2332#endif
2333
2334/*
2335 * find file in buffer list by number
2336 */
2337 buf_T *
2338buflist_findnr(nr)
2339 int nr;
2340{
2341 buf_T *buf;
2342
2343 if (nr == 0)
2344 nr = curwin->w_alt_fnum;
2345 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
2346 if (buf->b_fnum == nr)
2347 return (buf);
2348 return NULL;
2349}
2350
2351/*
2352 * Get name of file 'n' in the buffer list.
2353 * When the file has no name an empty string is returned.
2354 * home_replace() is used to shorten the file name (used for marks).
2355 * Returns a pointer to allocated memory, of NULL when failed.
2356 */
2357 char_u *
2358buflist_nr2name(n, fullname, helptail)
2359 int n;
2360 int fullname;
2361 int helptail; /* for help buffers return tail only */
2362{
2363 buf_T *buf;
2364
2365 buf = buflist_findnr(n);
2366 if (buf == NULL)
2367 return NULL;
2368 return home_replace_save(helptail ? buf : NULL,
2369 fullname ? buf->b_ffname : buf->b_fname);
2370}
2371
2372/*
2373 * Set the "lnum" and "col" for the buffer "buf" and the current window.
2374 * When "copy_options" is TRUE save the local window option values.
2375 * When "lnum" is 0 only do the options.
2376 */
2377 static void
2378buflist_setfpos(buf, win, lnum, col, copy_options)
2379 buf_T *buf;
2380 win_T *win;
2381 linenr_T lnum;
2382 colnr_T col;
2383 int copy_options;
2384{
2385 wininfo_T *wip;
2386
2387 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2388 if (wip->wi_win == win)
2389 break;
2390 if (wip == NULL)
2391 {
2392 /* allocate a new entry */
2393 wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
2394 if (wip == NULL)
2395 return;
2396 wip->wi_win = win;
2397 if (lnum == 0) /* set lnum even when it's 0 */
2398 lnum = 1;
2399 }
2400 else
2401 {
2402 /* remove the entry from the list */
2403 if (wip->wi_prev)
2404 wip->wi_prev->wi_next = wip->wi_next;
2405 else
2406 buf->b_wininfo = wip->wi_next;
2407 if (wip->wi_next)
2408 wip->wi_next->wi_prev = wip->wi_prev;
2409 if (copy_options && wip->wi_optset)
2410 {
2411 clear_winopt(&wip->wi_opt);
2412#ifdef FEAT_FOLDING
2413 deleteFoldRecurse(&wip->wi_folds);
2414#endif
2415 }
2416 }
2417 if (lnum != 0)
2418 {
2419 wip->wi_fpos.lnum = lnum;
2420 wip->wi_fpos.col = col;
2421 }
2422 if (copy_options)
2423 {
2424 /* Save the window-specific option values. */
2425 copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
2426#ifdef FEAT_FOLDING
2427 wip->wi_fold_manual = win->w_fold_manual;
2428 cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
2429#endif
2430 wip->wi_optset = TRUE;
2431 }
2432
2433 /* insert the entry in front of the list */
2434 wip->wi_next = buf->b_wininfo;
2435 buf->b_wininfo = wip;
2436 wip->wi_prev = NULL;
2437 if (wip->wi_next)
2438 wip->wi_next->wi_prev = wip;
2439
2440 return;
2441}
2442
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002443#ifdef FEAT_DIFF
2444static int wininfo_other_tab_diff __ARGS((wininfo_T *wip));
2445
2446/*
2447 * Return TRUE when "wip" has 'diff' set and the diff is only for another tab
2448 * page. That's because a diff is local to a tab page.
2449 */
2450 static int
2451wininfo_other_tab_diff(wip)
2452 wininfo_T *wip;
2453{
2454 win_T *wp;
2455
2456 if (wip->wi_opt.wo_diff)
2457 {
2458 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2459 /* return FALSE when it's a window in the current tab page, thus
2460 * the buffer was in diff mode here */
2461 if (wip->wi_win == wp)
2462 return FALSE;
2463 return TRUE;
2464 }
2465 return FALSE;
2466}
2467#endif
2468
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469/*
2470 * Find info for the current window in buffer "buf".
2471 * If not found, return the info for the most recently used window.
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002472 * When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
2473 * another tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 * Returns NULL when there isn't any info.
2475 */
2476 static wininfo_T *
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002477find_wininfo(buf, skip_diff_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002479 int skip_diff_buffer UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480{
2481 wininfo_T *wip;
2482
2483 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002484 if (wip->wi_win == curwin
2485#ifdef FEAT_DIFF
2486 && (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
2487#endif
2488 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 break;
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002490
2491 /* If no wininfo for curwin, use the first in the list (that doesn't have
2492 * 'diff' set and is in another tab page). */
2493 if (wip == NULL)
2494 {
2495#ifdef FEAT_DIFF
2496 if (skip_diff_buffer)
2497 {
2498 for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
2499 if (!wininfo_other_tab_diff(wip))
2500 break;
2501 }
2502 else
2503#endif
2504 wip = buf->b_wininfo;
2505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 return wip;
2507}
2508
2509/*
2510 * Reset the local window options to the values last used in this window.
2511 * If the buffer wasn't used in this window before, use the values from
2512 * the most recently used window. If the values were never set, use the
2513 * global values for the window.
2514 */
2515 void
2516get_winopts(buf)
2517 buf_T *buf;
2518{
2519 wininfo_T *wip;
2520
2521 clear_winopt(&curwin->w_onebuf_opt);
2522#ifdef FEAT_FOLDING
2523 clearFolding(curwin);
2524#endif
2525
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002526 wip = find_wininfo(buf, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 if (wip != NULL && wip->wi_optset)
2528 {
2529 copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
2530#ifdef FEAT_FOLDING
2531 curwin->w_fold_manual = wip->wi_fold_manual;
2532 curwin->w_foldinvalid = TRUE;
2533 cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
2534#endif
2535 }
2536 else
2537 copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
2538
2539#ifdef FEAT_FOLDING
2540 /* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
2541 if (p_fdls >= 0)
2542 curwin->w_p_fdl = p_fdls;
2543#endif
Bram Moolenaar1701e402011-05-05 17:32:44 +02002544#ifdef FEAT_SYN_HL
2545 check_colorcolumn(curwin);
2546#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547}
2548
2549/*
2550 * Find the position (lnum and col) for the buffer 'buf' for the current
2551 * window.
2552 * Returns a pointer to no_position if no position is found.
2553 */
2554 pos_T *
2555buflist_findfpos(buf)
2556 buf_T *buf;
2557{
2558 wininfo_T *wip;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002559 static pos_T no_position = INIT_POS_T(1, 0, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002561 wip = find_wininfo(buf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 if (wip != NULL)
2563 return &(wip->wi_fpos);
2564 else
2565 return &no_position;
2566}
2567
2568/*
2569 * Find the lnum for the buffer 'buf' for the current window.
2570 */
2571 linenr_T
2572buflist_findlnum(buf)
2573 buf_T *buf;
2574{
2575 return buflist_findfpos(buf)->lnum;
2576}
2577
2578#if defined(FEAT_LISTCMDS) || defined(PROTO)
2579/*
2580 * List all know file names (for :files and :buffers command).
2581 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 void
2583buflist_list(eap)
2584 exarg_T *eap;
2585{
2586 buf_T *buf;
2587 int len;
2588 int i;
2589
2590 for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
2591 {
2592 /* skip unlisted buffers, unless ! was used */
2593 if (!buf->b_p_bl && !eap->forceit)
2594 continue;
2595 msg_putchar('\n');
2596 if (buf_spname(buf) != NULL)
2597 STRCPY(NameBuff, buf_spname(buf));
2598 else
2599 home_replace(buf, buf->b_fname, NameBuff, MAXPATHL, TRUE);
2600
Bram Moolenaar50cde822005-06-05 21:54:54 +00002601 len = vim_snprintf((char *)IObuff, IOSIZE - 20, "%3d%c%c%c%c%c \"%s\"",
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 buf->b_fnum,
2603 buf->b_p_bl ? ' ' : 'u',
2604 buf == curbuf ? '%' :
2605 (curwin->w_alt_fnum == buf->b_fnum ? '#' : ' '),
2606 buf->b_ml.ml_mfp == NULL ? ' ' :
2607 (buf->b_nwindows == 0 ? 'h' : 'a'),
2608 !buf->b_p_ma ? '-' : (buf->b_p_ro ? '=' : ' '),
2609 (buf->b_flags & BF_READERR) ? 'x'
Bram Moolenaar51485f02005-06-04 21:55:20 +00002610 : (bufIsChanged(buf) ? '+' : ' '),
2611 NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002612
2613 /* put "line 999" in column 40 or after the file name */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 i = 40 - vim_strsize(IObuff);
2615 do
2616 {
2617 IObuff[len++] = ' ';
2618 } while (--i > 0 && len < IOSIZE - 18);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002619 vim_snprintf((char *)IObuff + len, (size_t)(IOSIZE - len),
2620 _("line %ld"), buf == curbuf ? curwin->w_cursor.lnum
Bram Moolenaar9c13b352005-05-19 20:53:52 +00002621 : (long)buflist_findlnum(buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 msg_outtrans(IObuff);
2623 out_flush(); /* output one line at a time */
2624 ui_breakcheck();
2625 }
2626}
2627#endif
2628
2629/*
2630 * Get file name and line number for file 'fnum'.
2631 * Used by DoOneCmd() for translating '%' and '#'.
2632 * Used by insert_reg() and cmdline_paste() for '#' register.
2633 * Return FAIL if not found, OK for success.
2634 */
2635 int
2636buflist_name_nr(fnum, fname, lnum)
2637 int fnum;
2638 char_u **fname;
2639 linenr_T *lnum;
2640{
2641 buf_T *buf;
2642
2643 buf = buflist_findnr(fnum);
2644 if (buf == NULL || buf->b_fname == NULL)
2645 return FAIL;
2646
2647 *fname = buf->b_fname;
2648 *lnum = buflist_findlnum(buf);
2649
2650 return OK;
2651}
2652
2653/*
2654 * Set the file name for "buf"' to 'ffname', short file name to 'sfname'.
2655 * The file name with the full path is also remembered, for when :cd is used.
2656 * Returns FAIL for failure (file name already in use by other buffer)
2657 * OK otherwise.
2658 */
2659 int
2660setfname(buf, ffname, sfname, message)
2661 buf_T *buf;
2662 char_u *ffname, *sfname;
Bram Moolenaar81695252004-12-29 20:58:21 +00002663 int message; /* give message when buffer already exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
Bram Moolenaar81695252004-12-29 20:58:21 +00002665 buf_T *obuf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666#ifdef UNIX
2667 struct stat st;
2668#endif
2669
2670 if (ffname == NULL || *ffname == NUL)
2671 {
2672 /* Removing the name. */
2673 vim_free(buf->b_ffname);
2674 vim_free(buf->b_sfname);
2675 buf->b_ffname = NULL;
2676 buf->b_sfname = NULL;
2677#ifdef UNIX
2678 st.st_dev = (dev_T)-1;
2679#endif
2680 }
2681 else
2682 {
2683 fname_expand(buf, &ffname, &sfname); /* will allocate ffname */
2684 if (ffname == NULL) /* out of memory */
2685 return FAIL;
2686
2687 /*
2688 * if the file name is already used in another buffer:
2689 * - if the buffer is loaded, fail
2690 * - if the buffer is not loaded, delete it from the list
2691 */
2692#ifdef UNIX
2693 if (mch_stat((char *)ffname, &st) < 0)
2694 st.st_dev = (dev_T)-1;
Bram Moolenaar81695252004-12-29 20:58:21 +00002695#endif
2696 if (!(buf->b_flags & BF_DUMMY))
2697#ifdef UNIX
2698 obuf = buflist_findname_stat(ffname, &st);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699#else
Bram Moolenaar81695252004-12-29 20:58:21 +00002700 obuf = buflist_findname(ffname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701#endif
2702 if (obuf != NULL && obuf != buf)
2703 {
2704 if (obuf->b_ml.ml_mfp != NULL) /* it's loaded, fail */
2705 {
2706 if (message)
2707 EMSG(_("E95: Buffer with this name already exists"));
2708 vim_free(ffname);
2709 return FAIL;
2710 }
2711 close_buffer(NULL, obuf, DOBUF_WIPE); /* delete from the list */
2712 }
2713 sfname = vim_strsave(sfname);
2714 if (ffname == NULL || sfname == NULL)
2715 {
2716 vim_free(sfname);
2717 vim_free(ffname);
2718 return FAIL;
2719 }
2720#ifdef USE_FNAME_CASE
2721# ifdef USE_LONG_FNAME
2722 if (USE_LONG_FNAME)
2723# endif
2724 fname_case(sfname, 0); /* set correct case for short file name */
2725#endif
2726 vim_free(buf->b_ffname);
2727 vim_free(buf->b_sfname);
2728 buf->b_ffname = ffname;
2729 buf->b_sfname = sfname;
2730 }
2731 buf->b_fname = buf->b_sfname;
2732#ifdef UNIX
2733 if (st.st_dev == (dev_T)-1)
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002734 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002735 else
2736 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002737 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 buf->b_dev = st.st_dev;
2739 buf->b_ino = st.st_ino;
2740 }
2741#endif
2742
2743#ifndef SHORT_FNAME
2744 buf->b_shortname = FALSE;
2745#endif
2746
2747 buf_name_changed(buf);
2748 return OK;
2749}
2750
2751/*
Bram Moolenaar86b68352004-12-27 21:59:20 +00002752 * Crude way of changing the name of a buffer. Use with care!
2753 * The name should be relative to the current directory.
2754 */
2755 void
2756buf_set_name(fnum, name)
2757 int fnum;
2758 char_u *name;
2759{
2760 buf_T *buf;
2761
2762 buf = buflist_findnr(fnum);
2763 if (buf != NULL)
2764 {
2765 vim_free(buf->b_sfname);
2766 vim_free(buf->b_ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00002767 buf->b_ffname = vim_strsave(name);
2768 buf->b_sfname = NULL;
2769 /* Allocate ffname and expand into full path. Also resolves .lnk
2770 * files on Win32. */
2771 fname_expand(buf, &buf->b_ffname, &buf->b_sfname);
Bram Moolenaar86b68352004-12-27 21:59:20 +00002772 buf->b_fname = buf->b_sfname;
2773 }
2774}
2775
2776/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 * Take care of what needs to be done when the name of buffer "buf" has
2778 * changed.
2779 */
2780 void
2781buf_name_changed(buf)
2782 buf_T *buf;
2783{
2784 /*
2785 * If the file name changed, also change the name of the swapfile
2786 */
2787 if (buf->b_ml.ml_mfp != NULL)
2788 ml_setname(buf);
2789
2790 if (curwin->w_buffer == buf)
2791 check_arg_idx(curwin); /* check file name for arg list */
2792#ifdef FEAT_TITLE
2793 maketitle(); /* set window title */
2794#endif
2795#ifdef FEAT_WINDOWS
2796 status_redraw_all(); /* status lines need to be redrawn */
2797#endif
2798 fmarks_check_names(buf); /* check named file marks */
2799 ml_timestamp(buf); /* reset timestamp */
2800}
2801
2802/*
2803 * set alternate file name for current window
2804 *
2805 * Used by do_one_cmd(), do_write() and do_ecmd().
2806 * Return the buffer.
2807 */
2808 buf_T *
2809setaltfname(ffname, sfname, lnum)
2810 char_u *ffname;
2811 char_u *sfname;
2812 linenr_T lnum;
2813{
2814 buf_T *buf;
2815
2816 /* Create a buffer. 'buflisted' is not set if it's a new buffer */
2817 buf = buflist_new(ffname, sfname, lnum, 0);
Bram Moolenaard4755bb2004-09-02 19:12:26 +00002818 if (buf != NULL && !cmdmod.keepalt)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819 curwin->w_alt_fnum = buf->b_fnum;
2820 return buf;
2821}
2822
2823/*
2824 * Get alternate file name for current window.
2825 * Return NULL if there isn't any, and give error message if requested.
2826 */
2827 char_u *
2828getaltfname(errmsg)
2829 int errmsg; /* give error message */
2830{
2831 char_u *fname;
2832 linenr_T dummy;
2833
2834 if (buflist_name_nr(0, &fname, &dummy) == FAIL)
2835 {
2836 if (errmsg)
2837 EMSG(_(e_noalt));
2838 return NULL;
2839 }
2840 return fname;
2841}
2842
2843/*
2844 * Add a file name to the buflist and return its number.
2845 * Uses same flags as buflist_new(), except BLN_DUMMY.
2846 *
2847 * used by qf_init(), main() and doarglist()
2848 */
2849 int
2850buflist_add(fname, flags)
2851 char_u *fname;
2852 int flags;
2853{
2854 buf_T *buf;
2855
2856 buf = buflist_new(fname, NULL, (linenr_T)0, flags);
2857 if (buf != NULL)
2858 return buf->b_fnum;
2859 return 0;
2860}
2861
2862#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
2863/*
2864 * Adjust slashes in file names. Called after 'shellslash' was set.
2865 */
2866 void
2867buflist_slash_adjust()
2868{
2869 buf_T *bp;
2870
2871 for (bp = firstbuf; bp != NULL; bp = bp->b_next)
2872 {
2873 if (bp->b_ffname != NULL)
2874 slash_adjust(bp->b_ffname);
2875 if (bp->b_sfname != NULL)
2876 slash_adjust(bp->b_sfname);
2877 }
2878}
2879#endif
2880
2881/*
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002882 * Set alternate cursor position for the current buffer and window "win".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 * Also save the local window option values.
2884 */
2885 void
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002886buflist_altfpos(win)
2887 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888{
Bram Moolenaar701f7af2008-11-15 13:12:07 +00002889 buflist_setfpos(curbuf, win, win->w_cursor.lnum, win->w_cursor.col, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890}
2891
2892/*
2893 * Return TRUE if 'ffname' is not the same file as current file.
2894 * Fname must have a full path (expanded by mch_FullName()).
2895 */
2896 int
2897otherfile(ffname)
2898 char_u *ffname;
2899{
2900 return otherfile_buf(curbuf, ffname
2901#ifdef UNIX
2902 , NULL
2903#endif
2904 );
2905}
2906
2907 static int
2908otherfile_buf(buf, ffname
2909#ifdef UNIX
2910 , stp
2911#endif
2912 )
2913 buf_T *buf;
2914 char_u *ffname;
2915#ifdef UNIX
2916 struct stat *stp;
2917#endif
2918{
2919 /* no name is different */
2920 if (ffname == NULL || *ffname == NUL || buf->b_ffname == NULL)
2921 return TRUE;
2922 if (fnamecmp(ffname, buf->b_ffname) == 0)
2923 return FALSE;
2924#ifdef UNIX
2925 {
2926 struct stat st;
2927
2928 /* If no struct stat given, get it now */
2929 if (stp == NULL)
2930 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002931 if (!buf->b_dev_valid || mch_stat((char *)ffname, &st) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 st.st_dev = (dev_T)-1;
2933 stp = &st;
2934 }
2935 /* Use dev/ino to check if the files are the same, even when the names
2936 * are different (possible with links). Still need to compare the
2937 * name above, for when the file doesn't exist yet.
2938 * Problem: The dev/ino changes when a file is deleted (and created
2939 * again) and remains the same when renamed/moved. We don't want to
2940 * mch_stat() each buffer each time, that would be too slow. Get the
2941 * dev/ino again when they appear to match, but not when they appear
2942 * to be different: Could skip a buffer when it's actually the same
2943 * file. */
2944 if (buf_same_ino(buf, stp))
2945 {
2946 buf_setino(buf);
2947 if (buf_same_ino(buf, stp))
2948 return FALSE;
2949 }
2950 }
2951#endif
2952 return TRUE;
2953}
2954
2955#if defined(UNIX) || defined(PROTO)
2956/*
2957 * Set inode and device number for a buffer.
2958 * Must always be called when b_fname is changed!.
2959 */
2960 void
2961buf_setino(buf)
2962 buf_T *buf;
2963{
2964 struct stat st;
2965
2966 if (buf->b_fname != NULL && mch_stat((char *)buf->b_fname, &st) >= 0)
2967 {
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002968 buf->b_dev_valid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 buf->b_dev = st.st_dev;
2970 buf->b_ino = st.st_ino;
2971 }
2972 else
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002973 buf->b_dev_valid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002974}
2975
2976/*
2977 * Return TRUE if dev/ino in buffer "buf" matches with "stp".
2978 */
2979 static int
2980buf_same_ino(buf, stp)
2981 buf_T *buf;
2982 struct stat *stp;
2983{
Bram Moolenaarf1726cc2009-05-13 18:48:16 +00002984 return (buf->b_dev_valid
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985 && stp->st_dev == buf->b_dev
2986 && stp->st_ino == buf->b_ino);
2987}
2988#endif
2989
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002990/*
2991 * Print info about the current buffer.
2992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 void
2994fileinfo(fullname, shorthelp, dont_truncate)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002995 int fullname; /* when non-zero print full path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 int shorthelp;
2997 int dont_truncate;
2998{
2999 char_u *name;
3000 int n;
3001 char_u *p;
3002 char_u *buffer;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003003 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004
3005 buffer = alloc(IOSIZE);
3006 if (buffer == NULL)
3007 return;
3008
3009 if (fullname > 1) /* 2 CTRL-G: include buffer number */
3010 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003011 vim_snprintf((char *)buffer, IOSIZE, "buf %d: ", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 p = buffer + STRLEN(buffer);
3013 }
3014 else
3015 p = buffer;
3016
3017 *p++ = '"';
3018 if (buf_spname(curbuf) != NULL)
3019 STRCPY(p, buf_spname(curbuf));
3020 else
3021 {
3022 if (!fullname && curbuf->b_fname != NULL)
3023 name = curbuf->b_fname;
3024 else
3025 name = curbuf->b_ffname;
3026 home_replace(shorthelp ? curbuf : NULL, name, p,
3027 (int)(IOSIZE - (p - buffer)), TRUE);
3028 }
3029
Bram Moolenaara800b422010-06-27 01:15:55 +02003030 vim_snprintf_add((char *)buffer, IOSIZE, "\"%s%s%s%s%s%s",
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 curbufIsChanged() ? (shortmess(SHM_MOD)
3032 ? " [+]" : _(" [Modified]")) : " ",
3033 (curbuf->b_flags & BF_NOTEDITED)
3034#ifdef FEAT_QUICKFIX
3035 && !bt_dontwrite(curbuf)
3036#endif
3037 ? _("[Not edited]") : "",
3038 (curbuf->b_flags & BF_NEW)
3039#ifdef FEAT_QUICKFIX
3040 && !bt_dontwrite(curbuf)
3041#endif
3042 ? _("[New file]") : "",
3043 (curbuf->b_flags & BF_READERR) ? _("[Read errors]") : "",
3044 curbuf->b_p_ro ? (shortmess(SHM_RO) ? "[RO]"
3045 : _("[readonly]")) : "",
3046 (curbufIsChanged() || (curbuf->b_flags & BF_WRITE_MASK)
3047 || curbuf->b_p_ro) ?
3048 " " : "");
3049 /* With 32 bit longs and more than 21,474,836 lines multiplying by 100
3050 * causes an overflow, thus for large numbers divide instead. */
3051 if (curwin->w_cursor.lnum > 1000000L)
3052 n = (int)(((long)curwin->w_cursor.lnum) /
3053 ((long)curbuf->b_ml.ml_line_count / 100L));
3054 else
3055 n = (int)(((long)curwin->w_cursor.lnum * 100L) /
3056 (long)curbuf->b_ml.ml_line_count);
3057 if (curbuf->b_ml.ml_flags & ML_EMPTY)
3058 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003059 vim_snprintf_add((char *)buffer, IOSIZE, "%s", _(no_lines_msg));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 }
3061#ifdef FEAT_CMDL_INFO
3062 else if (p_ru)
3063 {
3064 /* Current line and column are already on the screen -- webb */
3065 if (curbuf->b_ml.ml_line_count == 1)
Bram Moolenaara800b422010-06-27 01:15:55 +02003066 vim_snprintf_add((char *)buffer, IOSIZE, _("1 line --%d%%--"), n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 else
Bram Moolenaara800b422010-06-27 01:15:55 +02003068 vim_snprintf_add((char *)buffer, IOSIZE, _("%ld lines --%d%%--"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 (long)curbuf->b_ml.ml_line_count, n);
3070 }
3071#endif
3072 else
3073 {
Bram Moolenaara800b422010-06-27 01:15:55 +02003074 vim_snprintf_add((char *)buffer, IOSIZE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 _("line %ld of %ld --%d%%-- col "),
3076 (long)curwin->w_cursor.lnum,
3077 (long)curbuf->b_ml.ml_line_count,
3078 n);
3079 validate_virtcol();
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003080 len = STRLEN(buffer);
3081 col_print(buffer + len, IOSIZE - len,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082 (int)curwin->w_cursor.col + 1, (int)curwin->w_virtcol + 1);
3083 }
3084
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003085 (void)append_arg_number(curwin, buffer, IOSIZE, !shortmess(SHM_FILE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086
3087 if (dont_truncate)
3088 {
3089 /* Temporarily set msg_scroll to avoid the message being truncated.
3090 * First call msg_start() to get the message in the right place. */
3091 msg_start();
3092 n = msg_scroll;
3093 msg_scroll = TRUE;
3094 msg(buffer);
3095 msg_scroll = n;
3096 }
3097 else
3098 {
3099 p = msg_trunc_attr(buffer, FALSE, 0);
3100 if (restart_edit != 0 || (msg_scrolled && !need_wait_return))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 /* Need to repeat the message after redrawing when:
3102 * - When restart_edit is set (otherwise there will be a delay
3103 * before redrawing).
3104 * - When the screen was scrolled but there is no wait-return
3105 * prompt. */
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003106 set_keep_msg(p, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 }
3108
3109 vim_free(buffer);
3110}
3111
3112 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003113col_print(buf, buflen, col, vcol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003114 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003115 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 int col;
3117 int vcol;
3118{
3119 if (col == vcol)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003120 vim_snprintf((char *)buf, buflen, "%d", col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003122 vim_snprintf((char *)buf, buflen, "%d-%d", col, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123}
3124
3125#if defined(FEAT_TITLE) || defined(PROTO)
3126/*
3127 * put file name in title bar of window and in icon title
3128 */
3129
3130static char_u *lasttitle = NULL;
3131static char_u *lasticon = NULL;
3132
3133 void
3134maketitle()
3135{
3136 char_u *p;
3137 char_u *t_str = NULL;
3138 char_u *i_name;
3139 char_u *i_str = NULL;
3140 int maxlen = 0;
3141 int len;
3142 int mustset;
3143 char_u buf[IOSIZE];
3144 int off;
3145
3146 if (!redrawing())
3147 {
3148 /* Postpone updating the title when 'lazyredraw' is set. */
3149 need_maketitle = TRUE;
3150 return;
3151 }
3152
3153 need_maketitle = FALSE;
Bram Moolenaarbed7bec2010-07-25 13:42:29 +02003154 if (!p_title && !p_icon && lasttitle == NULL && lasticon == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 return;
3156
3157 if (p_title)
3158 {
3159 if (p_titlelen > 0)
3160 {
3161 maxlen = p_titlelen * Columns / 100;
3162 if (maxlen < 10)
3163 maxlen = 10;
3164 }
3165
3166 t_str = buf;
3167 if (*p_titlestring != NUL)
3168 {
3169#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003170 if (stl_syntax & STL_IN_TITLE)
3171 {
3172 int use_sandbox = FALSE;
3173 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003174
3175# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003176 use_sandbox = was_set_insecurely((char_u *)"titlestring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003177# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003178 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 build_stl_str_hl(curwin, t_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003180 p_titlestring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003181 0, maxlen, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003182 if (called_emsg)
3183 set_string_option_direct((char_u *)"titlestring", -1,
3184 (char_u *)"", OPT_FREE, SID_ERROR);
3185 called_emsg |= save_called_emsg;
3186 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 else
3188#endif
3189 t_str = p_titlestring;
3190 }
3191 else
3192 {
3193 /* format: "fname + (path) (1 of 2) - VIM" */
3194
3195 if (curbuf->b_fname == NULL)
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003196 vim_strncpy(buf, (char_u *)_("[No Name]"), IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 else
3198 {
3199 p = transstr(gettail(curbuf->b_fname));
Bram Moolenaarb6356332005-07-18 21:40:44 +00003200 vim_strncpy(buf, p, IOSIZE - 100);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202 }
3203
3204 switch (bufIsChanged(curbuf)
3205 + (curbuf->b_p_ro * 2)
3206 + (!curbuf->b_p_ma * 4))
3207 {
3208 case 1: STRCAT(buf, " +"); break;
3209 case 2: STRCAT(buf, " ="); break;
3210 case 3: STRCAT(buf, " =+"); break;
3211 case 4:
3212 case 6: STRCAT(buf, " -"); break;
3213 case 5:
3214 case 7: STRCAT(buf, " -+"); break;
3215 }
3216
3217 if (curbuf->b_fname != NULL)
3218 {
3219 /* Get path of file, replace home dir with ~ */
3220 off = (int)STRLEN(buf);
3221 buf[off++] = ' ';
3222 buf[off++] = '(';
3223 home_replace(curbuf, curbuf->b_ffname,
3224 buf + off, IOSIZE - off, TRUE);
3225#ifdef BACKSLASH_IN_FILENAME
3226 /* avoid "c:/name" to be reduced to "c" */
3227 if (isalpha(buf[off]) && buf[off + 1] == ':')
3228 off += 2;
3229#endif
3230 /* remove the file name */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003231 p = gettail_sep(buf + off);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232 if (p == buf + off)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233 /* must be a help buffer */
Bram Moolenaarb6356332005-07-18 21:40:44 +00003234 vim_strncpy(buf + off, (char_u *)_("help"),
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003235 (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003237 *p = NUL;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003238
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 /* translate unprintable chars */
3240 p = transstr(buf + off);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003241 vim_strncpy(buf + off, p, (size_t)(IOSIZE - off - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 vim_free(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003243 STRCAT(buf, ")");
3244 }
3245
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003246 append_arg_number(curwin, buf, IOSIZE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003247
3248#if defined(FEAT_CLIENTSERVER)
3249 if (serverName != NULL)
3250 {
3251 STRCAT(buf, " - ");
Bram Moolenaaref9d6aa2011-04-11 16:56:35 +02003252 vim_strcat(buf, serverName, IOSIZE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 }
3254 else
3255#endif
3256 STRCAT(buf, " - VIM");
3257
3258 if (maxlen > 0)
3259 {
3260 /* make it shorter by removing a bit in the middle */
3261 len = vim_strsize(buf);
3262 if (len > maxlen)
3263 trunc_string(buf, buf, maxlen);
3264 }
3265 }
3266 }
3267 mustset = ti_change(t_str, &lasttitle);
3268
3269 if (p_icon)
3270 {
3271 i_str = buf;
3272 if (*p_iconstring != NUL)
3273 {
3274#ifdef FEAT_STL_OPT
Bram Moolenaard3667a22006-03-16 21:35:52 +00003275 if (stl_syntax & STL_IN_ICON)
3276 {
3277 int use_sandbox = FALSE;
3278 int save_called_emsg = called_emsg;
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003279
3280# ifdef FEAT_EVAL
Bram Moolenaard3667a22006-03-16 21:35:52 +00003281 use_sandbox = was_set_insecurely((char_u *)"iconstring", 0);
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003282# endif
Bram Moolenaard3667a22006-03-16 21:35:52 +00003283 called_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284 build_stl_str_hl(curwin, i_str, sizeof(buf),
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003285 p_iconstring, use_sandbox,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003286 0, 0, NULL, NULL);
Bram Moolenaard3667a22006-03-16 21:35:52 +00003287 if (called_emsg)
3288 set_string_option_direct((char_u *)"iconstring", -1,
3289 (char_u *)"", OPT_FREE, SID_ERROR);
3290 called_emsg |= save_called_emsg;
3291 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 else
3293#endif
3294 i_str = p_iconstring;
3295 }
3296 else
3297 {
3298 if (buf_spname(curbuf) != NULL)
3299 i_name = (char_u *)buf_spname(curbuf);
3300 else /* use file name only in icon */
3301 i_name = gettail(curbuf->b_ffname);
3302 *i_str = NUL;
3303 /* Truncate name at 100 bytes. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003304 len = (int)STRLEN(i_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 if (len > 100)
3306 {
3307 len -= 100;
3308#ifdef FEAT_MBYTE
3309 if (has_mbyte)
3310 len += (*mb_tail_off)(i_name, i_name + len) + 1;
3311#endif
3312 i_name += len;
3313 }
3314 STRCPY(i_str, i_name);
3315 trans_characters(i_str, IOSIZE);
3316 }
3317 }
3318
3319 mustset |= ti_change(i_str, &lasticon);
3320
3321 if (mustset)
3322 resettitle();
3323}
3324
3325/*
3326 * Used for title and icon: Check if "str" differs from "*last". Set "*last"
3327 * from "str" if it does.
3328 * Return TRUE when "*last" changed.
3329 */
3330 static int
3331ti_change(str, last)
3332 char_u *str;
3333 char_u **last;
3334{
3335 if ((str == NULL) != (*last == NULL)
3336 || (str != NULL && *last != NULL && STRCMP(str, *last) != 0))
3337 {
3338 vim_free(*last);
3339 if (str == NULL)
3340 *last = NULL;
3341 else
3342 *last = vim_strsave(str);
3343 return TRUE;
3344 }
3345 return FALSE;
3346}
3347
3348/*
3349 * Put current window title back (used after calling a shell)
3350 */
3351 void
3352resettitle()
3353{
3354 mch_settitle(lasttitle, lasticon);
3355}
Bram Moolenaarea408852005-06-25 22:49:46 +00003356
3357# if defined(EXITFREE) || defined(PROTO)
3358 void
3359free_titles()
3360{
3361 vim_free(lasttitle);
3362 vim_free(lasticon);
3363}
3364# endif
3365
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366#endif /* FEAT_TITLE */
3367
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003368#if defined(FEAT_STL_OPT) || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369/*
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003370 * Build a string from the status line items in "fmt".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 * Return length of string in screen cells.
3372 *
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003373 * Normally works for window "wp", except when working for 'tabline' then it
3374 * is "curwin".
3375 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 * Items are drawn interspersed with the text that surrounds it
3377 * Specials: %-<wid>(xxx%) => group, %= => middle marker, %< => truncation
3378 * Item: %-<minwid>.<maxwid><itemch> All but <itemch> are optional
3379 *
3380 * If maxwidth is not zero, the string will be filled at any middle marker
3381 * or truncated if too long, fillchar is used for all whitespace.
3382 */
3383 int
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003384build_stl_str_hl(wp, out, outlen, fmt, use_sandbox, fillchar,
3385 maxwidth, hltab, tabtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 win_T *wp;
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003387 char_u *out; /* buffer to write into != NameBuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 size_t outlen; /* length of out[] */
3389 char_u *fmt;
Bram Moolenaar2c4278f2009-05-17 11:33:22 +00003390 int use_sandbox UNUSED; /* "fmt" was set insecurely, use sandbox */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 int fillchar;
3392 int maxwidth;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003393 struct stl_hlrec *hltab; /* return: HL attributes (can be NULL) */
3394 struct stl_hlrec *tabtab; /* return: tab page nrs (can be NULL) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395{
3396 char_u *p;
3397 char_u *s;
3398 char_u *t;
3399 char_u *linecont;
3400#ifdef FEAT_EVAL
3401 win_T *o_curwin;
3402 buf_T *o_curbuf;
3403#endif
3404 int empty_line;
3405 colnr_T virtcol;
3406 long l;
3407 long n;
3408 int prevchar_isflag;
3409 int prevchar_isitem;
3410 int itemisflag;
3411 int fillable;
3412 char_u *str;
3413 long num;
3414 int width;
3415 int itemcnt;
3416 int curitem;
3417 int groupitem[STL_MAX_ITEM];
3418 int groupdepth;
3419 struct stl_item
3420 {
3421 char_u *start;
3422 int minwid;
3423 int maxwid;
3424 enum
3425 {
3426 Normal,
3427 Empty,
3428 Group,
3429 Middle,
3430 Highlight,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003431 TabPage,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432 Trunc
3433 } type;
3434 } item[STL_MAX_ITEM];
3435 int minwid;
3436 int maxwid;
3437 int zeropad;
3438 char_u base;
3439 char_u opt;
3440#define TMPLEN 70
3441 char_u tmp[TMPLEN];
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003442 char_u *usefmt = fmt;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003443 struct stl_hlrec *sp;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003444
3445#ifdef FEAT_EVAL
3446 /*
3447 * When the format starts with "%!" then evaluate it as an expression and
3448 * use the result as the actual format string.
3449 */
3450 if (fmt[0] == '%' && fmt[1] == '!')
3451 {
3452 usefmt = eval_to_string_safe(fmt + 2, NULL, use_sandbox);
3453 if (usefmt == NULL)
Bram Moolenaar4100af72006-08-29 14:48:14 +00003454 usefmt = fmt;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003455 }
3456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
3458 if (fillchar == 0)
3459 fillchar = ' ';
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003460#ifdef FEAT_MBYTE
3461 /* Can't handle a multi-byte fill character yet. */
3462 else if (mb_char2len(fillchar) > 1)
3463 fillchar = '-';
3464#endif
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 /*
3467 * Get line & check if empty (cursorpos will show "0-1").
3468 * If inversion is possible we use it. Else '=' characters are used.
3469 */
3470 linecont = ml_get_buf(wp->w_buffer, wp->w_cursor.lnum, FALSE);
3471 empty_line = (*linecont == NUL);
3472
3473 groupdepth = 0;
3474 p = out;
3475 curitem = 0;
3476 prevchar_isflag = TRUE;
3477 prevchar_isitem = FALSE;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003478 for (s = usefmt; *s; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 {
Bram Moolenaarb75d09d2011-02-15 14:24:46 +01003480 if (curitem == STL_MAX_ITEM)
3481 {
3482 /* There are too many items. Add the error code to the statusline
3483 * to give the user a hint about what went wrong. */
3484 if (p + 6 < out + outlen)
3485 {
3486 mch_memmove(p, " E541", (size_t)5);
3487 p += 5;
3488 }
3489 break;
3490 }
3491
Bram Moolenaar071d4272004-06-13 20:20:40 +00003492 if (*s != NUL && *s != '%')
3493 prevchar_isflag = prevchar_isitem = FALSE;
3494
3495 /*
3496 * Handle up to the next '%' or the end.
3497 */
3498 while (*s != NUL && *s != '%' && p + 1 < out + outlen)
3499 *p++ = *s++;
3500 if (*s == NUL || p + 1 >= out + outlen)
3501 break;
3502
3503 /*
3504 * Handle one '%' item.
3505 */
3506 s++;
Bram Moolenaar1d87f512011-02-01 21:55:01 +01003507 if (*s == NUL) /* ignore trailing % */
3508 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509 if (*s == '%')
3510 {
3511 if (p + 1 >= out + outlen)
3512 break;
3513 *p++ = *s++;
3514 prevchar_isflag = prevchar_isitem = FALSE;
3515 continue;
3516 }
3517 if (*s == STL_MIDDLEMARK)
3518 {
3519 s++;
3520 if (groupdepth > 0)
3521 continue;
3522 item[curitem].type = Middle;
3523 item[curitem++].start = p;
3524 continue;
3525 }
3526 if (*s == STL_TRUNCMARK)
3527 {
3528 s++;
3529 item[curitem].type = Trunc;
3530 item[curitem++].start = p;
3531 continue;
3532 }
3533 if (*s == ')')
3534 {
3535 s++;
3536 if (groupdepth < 1)
3537 continue;
3538 groupdepth--;
3539
3540 t = item[groupitem[groupdepth]].start;
3541 *p = NUL;
3542 l = vim_strsize(t);
3543 if (curitem > groupitem[groupdepth] + 1
3544 && item[groupitem[groupdepth]].minwid == 0)
3545 {
3546 /* remove group if all items are empty */
3547 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3548 if (item[n].type == Normal)
3549 break;
3550 if (n == curitem)
3551 {
3552 p = t;
3553 l = 0;
3554 }
3555 }
3556 if (l > item[groupitem[groupdepth]].maxwid)
3557 {
3558 /* truncate, remove n bytes of text at the start */
3559#ifdef FEAT_MBYTE
3560 if (has_mbyte)
3561 {
3562 /* Find the first character that should be included. */
3563 n = 0;
3564 while (l >= item[groupitem[groupdepth]].maxwid)
3565 {
3566 l -= ptr2cells(t + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003567 n += (*mb_ptr2len)(t + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 }
3569 }
3570 else
3571#endif
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003572 n = (long)(p - t) - item[groupitem[groupdepth]].maxwid + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573
3574 *t = '<';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003575 mch_memmove(t + 1, t + n, (size_t)(p - (t + n)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003576 p = p - n + 1;
3577#ifdef FEAT_MBYTE
3578 /* Fill up space left over by half a double-wide char. */
3579 while (++l < item[groupitem[groupdepth]].minwid)
3580 *p++ = fillchar;
3581#endif
3582
3583 /* correct the start of the items for the truncation */
3584 for (l = groupitem[groupdepth] + 1; l < curitem; l++)
3585 {
3586 item[l].start -= n;
3587 if (item[l].start < t)
3588 item[l].start = t;
3589 }
3590 }
3591 else if (abs(item[groupitem[groupdepth]].minwid) > l)
3592 {
3593 /* fill */
3594 n = item[groupitem[groupdepth]].minwid;
3595 if (n < 0)
3596 {
3597 /* fill by appending characters */
3598 n = 0 - n;
3599 while (l++ < n && p + 1 < out + outlen)
3600 *p++ = fillchar;
3601 }
3602 else
3603 {
3604 /* fill by inserting characters */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003605 mch_memmove(t + n - l, t, (size_t)(p - t));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 l = n - l;
3607 if (p + l >= out + outlen)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003608 l = (long)((out + outlen) - p - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 p += l;
3610 for (n = groupitem[groupdepth] + 1; n < curitem; n++)
3611 item[n].start += l;
3612 for ( ; l > 0; l--)
3613 *t++ = fillchar;
3614 }
3615 }
3616 continue;
3617 }
3618 minwid = 0;
3619 maxwid = 9999;
3620 zeropad = FALSE;
3621 l = 1;
3622 if (*s == '0')
3623 {
3624 s++;
3625 zeropad = TRUE;
3626 }
3627 if (*s == '-')
3628 {
3629 s++;
3630 l = -1;
3631 }
3632 if (VIM_ISDIGIT(*s))
3633 {
3634 minwid = (int)getdigits(&s);
3635 if (minwid < 0) /* overflow */
3636 minwid = 0;
3637 }
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003638 if (*s == STL_USER_HL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 item[curitem].type = Highlight;
3641 item[curitem].start = p;
3642 item[curitem].minwid = minwid > 9 ? 1 : minwid;
3643 s++;
3644 curitem++;
3645 continue;
3646 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003647 if (*s == STL_TABPAGENR || *s == STL_TABCLOSENR)
3648 {
3649 if (*s == STL_TABCLOSENR)
3650 {
3651 if (minwid == 0)
3652 {
3653 /* %X ends the close label, go back to the previously
3654 * define tab label nr. */
3655 for (n = curitem - 1; n >= 0; --n)
3656 if (item[n].type == TabPage && item[n].minwid >= 0)
3657 {
3658 minwid = item[n].minwid;
3659 break;
3660 }
3661 }
3662 else
3663 /* close nrs are stored as negative values */
3664 minwid = - minwid;
3665 }
3666 item[curitem].type = TabPage;
3667 item[curitem].start = p;
3668 item[curitem].minwid = minwid;
3669 s++;
3670 curitem++;
3671 continue;
3672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 if (*s == '.')
3674 {
3675 s++;
3676 if (VIM_ISDIGIT(*s))
3677 {
3678 maxwid = (int)getdigits(&s);
3679 if (maxwid <= 0) /* overflow */
3680 maxwid = 50;
3681 }
3682 }
3683 minwid = (minwid > 50 ? 50 : minwid) * l;
3684 if (*s == '(')
3685 {
3686 groupitem[groupdepth++] = curitem;
3687 item[curitem].type = Group;
3688 item[curitem].start = p;
3689 item[curitem].minwid = minwid;
3690 item[curitem].maxwid = maxwid;
3691 s++;
3692 curitem++;
3693 continue;
3694 }
3695 if (vim_strchr(STL_ALL, *s) == NULL)
3696 {
3697 s++;
3698 continue;
3699 }
3700 opt = *s++;
3701
3702 /* OK - now for the real work */
3703 base = 'D';
3704 itemisflag = FALSE;
3705 fillable = TRUE;
3706 num = -1;
3707 str = NULL;
3708 switch (opt)
3709 {
3710 case STL_FILEPATH:
3711 case STL_FULLPATH:
3712 case STL_FILENAME:
3713 fillable = FALSE; /* don't change ' ' to fillchar */
3714 if (buf_spname(wp->w_buffer) != NULL)
3715 STRCPY(NameBuff, buf_spname(wp->w_buffer));
3716 else
3717 {
3718 t = (opt == STL_FULLPATH) ? wp->w_buffer->b_ffname
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003719 : wp->w_buffer->b_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 home_replace(wp->w_buffer, t, NameBuff, MAXPATHL, TRUE);
3721 }
3722 trans_characters(NameBuff, MAXPATHL);
3723 if (opt != STL_FILENAME)
3724 str = NameBuff;
3725 else
3726 str = gettail(NameBuff);
3727 break;
3728
3729 case STL_VIM_EXPR: /* '{' */
3730 itemisflag = TRUE;
3731 t = p;
3732 while (*s != '}' && *s != NUL && p + 1 < out + outlen)
3733 *p++ = *s++;
3734 if (*s != '}') /* missing '}' or out of space */
3735 break;
3736 s++;
3737 *p = 0;
3738 p = t;
3739
3740#ifdef FEAT_EVAL
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003741 vim_snprintf((char *)tmp, sizeof(tmp), "%d", curbuf->b_fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 set_internal_string_var((char_u *)"actual_curbuf", tmp);
3743
3744 o_curbuf = curbuf;
3745 o_curwin = curwin;
3746 curwin = wp;
3747 curbuf = wp->w_buffer;
3748
Bram Moolenaar2a0449d2006-02-20 21:27:21 +00003749 str = eval_to_string_safe(p, &t, use_sandbox);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750
3751 curwin = o_curwin;
3752 curbuf = o_curbuf;
Bram Moolenaar01824652005-01-31 18:58:23 +00003753 do_unlet((char_u *)"g:actual_curbuf", TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754
3755 if (str != NULL && *str != 0)
3756 {
3757 if (*skipdigits(str) == NUL)
3758 {
3759 num = atoi((char *)str);
3760 vim_free(str);
3761 str = NULL;
3762 itemisflag = FALSE;
3763 }
3764 }
3765#endif
3766 break;
3767
3768 case STL_LINE:
3769 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY)
3770 ? 0L : (long)(wp->w_cursor.lnum);
3771 break;
3772
3773 case STL_NUMLINES:
3774 num = wp->w_buffer->b_ml.ml_line_count;
3775 break;
3776
3777 case STL_COLUMN:
3778 num = !(State & INSERT) && empty_line
3779 ? 0 : (int)wp->w_cursor.col + 1;
3780 break;
3781
3782 case STL_VIRTCOL:
3783 case STL_VIRTCOL_ALT:
3784 /* In list mode virtcol needs to be recomputed */
3785 virtcol = wp->w_virtcol;
3786 if (wp->w_p_list && lcs_tab1 == NUL)
3787 {
3788 wp->w_p_list = FALSE;
3789 getvcol(wp, &wp->w_cursor, NULL, &virtcol, NULL);
3790 wp->w_p_list = TRUE;
3791 }
3792 ++virtcol;
3793 /* Don't display %V if it's the same as %c. */
3794 if (opt == STL_VIRTCOL_ALT
3795 && (virtcol == (colnr_T)(!(State & INSERT) && empty_line
3796 ? 0 : (int)wp->w_cursor.col + 1)))
3797 break;
3798 num = (long)virtcol;
3799 break;
3800
3801 case STL_PERCENTAGE:
3802 num = (int)(((long)wp->w_cursor.lnum * 100L) /
3803 (long)wp->w_buffer->b_ml.ml_line_count);
3804 break;
3805
3806 case STL_ALTPERCENT:
3807 str = tmp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003808 get_rel_pos(wp, str, TMPLEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 break;
3810
3811 case STL_ARGLISTSTAT:
3812 fillable = FALSE;
3813 tmp[0] = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003814 if (append_arg_number(wp, tmp, (int)sizeof(tmp), FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 str = tmp;
3816 break;
3817
3818 case STL_KEYMAP:
3819 fillable = FALSE;
3820 if (get_keymap_str(wp, tmp, TMPLEN))
3821 str = tmp;
3822 break;
3823 case STL_PAGENUM:
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00003824#if defined(FEAT_PRINTER) || defined(FEAT_GUI_TABLINE)
Bram Moolenaarba6c0522006-02-25 21:45:02 +00003825 num = printer_page_num;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826#else
3827 num = 0;
3828#endif
3829 break;
3830
3831 case STL_BUFNO:
3832 num = wp->w_buffer->b_fnum;
3833 break;
3834
3835 case STL_OFFSET_X:
3836 base = 'X';
3837 case STL_OFFSET:
3838#ifdef FEAT_BYTEOFF
3839 l = ml_find_line_or_offset(wp->w_buffer, wp->w_cursor.lnum, NULL);
3840 num = (wp->w_buffer->b_ml.ml_flags & ML_EMPTY) || l < 0 ?
3841 0L : l + 1 + (!(State & INSERT) && empty_line ?
3842 0 : (int)wp->w_cursor.col);
3843#endif
3844 break;
3845
3846 case STL_BYTEVAL_X:
3847 base = 'X';
3848 case STL_BYTEVAL:
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003849 if (wp->w_cursor.col > (colnr_T)STRLEN(linecont))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 num = 0;
3851 else
3852 {
3853#ifdef FEAT_MBYTE
3854 num = (*mb_ptr2char)(linecont + wp->w_cursor.col);
3855#else
3856 num = linecont[wp->w_cursor.col];
3857#endif
3858 }
3859 if (num == NL)
3860 num = 0;
3861 else if (num == CAR && get_fileformat(wp->w_buffer) == EOL_MAC)
3862 num = NL;
3863 break;
3864
3865 case STL_ROFLAG:
3866 case STL_ROFLAG_ALT:
3867 itemisflag = TRUE;
3868 if (wp->w_buffer->b_p_ro)
3869 str = (char_u *)((opt == STL_ROFLAG_ALT) ? ",RO" : "[RO]");
3870 break;
3871
3872 case STL_HELPFLAG:
3873 case STL_HELPFLAG_ALT:
3874 itemisflag = TRUE;
3875 if (wp->w_buffer->b_help)
3876 str = (char_u *)((opt == STL_HELPFLAG_ALT) ? ",HLP"
Bram Moolenaar899dddf2006-03-26 21:06:50 +00003877 : _("[Help]"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 break;
3879
3880#ifdef FEAT_AUTOCMD
3881 case STL_FILETYPE:
3882 if (*wp->w_buffer->b_p_ft != NUL
3883 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 3)
3884 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003885 vim_snprintf((char *)tmp, sizeof(tmp), "[%s]",
3886 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 str = tmp;
3888 }
3889 break;
3890
3891 case STL_FILETYPE_ALT:
3892 itemisflag = TRUE;
3893 if (*wp->w_buffer->b_p_ft != NUL
3894 && STRLEN(wp->w_buffer->b_p_ft) < TMPLEN - 2)
3895 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00003896 vim_snprintf((char *)tmp, sizeof(tmp), ",%s",
3897 wp->w_buffer->b_p_ft);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 for (t = tmp; *t != 0; t++)
3899 *t = TOUPPER_LOC(*t);
3900 str = tmp;
3901 }
3902 break;
3903#endif
3904
3905#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
3906 case STL_PREVIEWFLAG:
3907 case STL_PREVIEWFLAG_ALT:
3908 itemisflag = TRUE;
3909 if (wp->w_p_pvw)
3910 str = (char_u *)((opt == STL_PREVIEWFLAG_ALT) ? ",PRV"
3911 : _("[Preview]"));
3912 break;
Bram Moolenaar7fd73202010-07-25 16:58:46 +02003913
3914 case STL_QUICKFIX:
3915 if (bt_quickfix(wp->w_buffer))
3916 str = (char_u *)(wp->w_llist_ref
3917 ? _(msg_loclist)
3918 : _(msg_qflist));
3919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920#endif
3921
3922 case STL_MODIFIED:
3923 case STL_MODIFIED_ALT:
3924 itemisflag = TRUE;
3925 switch ((opt == STL_MODIFIED_ALT)
3926 + bufIsChanged(wp->w_buffer) * 2
3927 + (!wp->w_buffer->b_p_ma) * 4)
3928 {
3929 case 2: str = (char_u *)"[+]"; break;
3930 case 3: str = (char_u *)",+"; break;
3931 case 4: str = (char_u *)"[-]"; break;
3932 case 5: str = (char_u *)",-"; break;
3933 case 6: str = (char_u *)"[+-]"; break;
3934 case 7: str = (char_u *)",+-"; break;
3935 }
3936 break;
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003937
3938 case STL_HIGHLIGHT:
3939 t = s;
3940 while (*s != '#' && *s != NUL)
3941 ++s;
3942 if (*s == '#')
3943 {
3944 item[curitem].type = Highlight;
3945 item[curitem].start = p;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003946 item[curitem].minwid = -syn_namen2id(t, (int)(s - t));
Bram Moolenaar030f0df2006-02-21 22:02:53 +00003947 curitem++;
3948 }
3949 ++s;
3950 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
3952
3953 item[curitem].start = p;
3954 item[curitem].type = Normal;
3955 if (str != NULL && *str)
3956 {
3957 t = str;
3958 if (itemisflag)
3959 {
3960 if ((t[0] && t[1])
3961 && ((!prevchar_isitem && *t == ',')
3962 || (prevchar_isflag && *t == ' ')))
3963 t++;
3964 prevchar_isflag = TRUE;
3965 }
3966 l = vim_strsize(t);
3967 if (l > 0)
3968 prevchar_isitem = TRUE;
3969 if (l > maxwid)
3970 {
3971 while (l >= maxwid)
3972#ifdef FEAT_MBYTE
3973 if (has_mbyte)
3974 {
3975 l -= ptr2cells(t);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003976 t += (*mb_ptr2len)(t);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 }
3978 else
3979#endif
3980 l -= byte2cells(*t++);
3981 if (p + 1 >= out + outlen)
3982 break;
3983 *p++ = '<';
3984 }
3985 if (minwid > 0)
3986 {
3987 for (; l < minwid && p + 1 < out + outlen; l++)
3988 {
3989 /* Don't put a "-" in front of a digit. */
3990 if (l + 1 == minwid && fillchar == '-' && VIM_ISDIGIT(*t))
3991 *p++ = ' ';
3992 else
3993 *p++ = fillchar;
3994 }
3995 minwid = 0;
3996 }
3997 else
3998 minwid *= -1;
3999 while (*t && p + 1 < out + outlen)
4000 {
4001 *p++ = *t++;
4002 /* Change a space by fillchar, unless fillchar is '-' and a
4003 * digit follows. */
4004 if (fillable && p[-1] == ' '
4005 && (!VIM_ISDIGIT(*t) || fillchar != '-'))
4006 p[-1] = fillchar;
4007 }
4008 for (; l < minwid && p + 1 < out + outlen; l++)
4009 *p++ = fillchar;
4010 }
4011 else if (num >= 0)
4012 {
4013 int nbase = (base == 'D' ? 10 : (base == 'O' ? 8 : 16));
4014 char_u nstr[20];
4015
4016 if (p + 20 >= out + outlen)
4017 break; /* not sufficient space */
4018 prevchar_isitem = TRUE;
4019 t = nstr;
4020 if (opt == STL_VIRTCOL_ALT)
4021 {
4022 *t++ = '-';
4023 minwid--;
4024 }
4025 *t++ = '%';
4026 if (zeropad)
4027 *t++ = '0';
4028 *t++ = '*';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004029 *t++ = nbase == 16 ? base : (char_u)(nbase == 8 ? 'o' : 'd');
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 *t = 0;
4031
4032 for (n = num, l = 1; n >= nbase; n /= nbase)
4033 l++;
4034 if (opt == STL_VIRTCOL_ALT)
4035 l++;
4036 if (l > maxwid)
4037 {
4038 l += 2;
4039 n = l - maxwid;
4040 while (l-- > maxwid)
4041 num /= nbase;
4042 *t++ = '>';
4043 *t++ = '%';
4044 *t = t[-3];
4045 *++t = 0;
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004046 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4047 0, num, n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 else
Bram Moolenaar9c13b352005-05-19 20:53:52 +00004050 vim_snprintf((char *)p, outlen - (p - out), (char *)nstr,
4051 minwid, num);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004052 p += STRLEN(p);
4053 }
4054 else
4055 item[curitem].type = Empty;
4056
4057 if (opt == STL_VIM_EXPR)
4058 vim_free(str);
4059
4060 if (num >= 0 || (!itemisflag && str && *str))
4061 prevchar_isflag = FALSE; /* Item not NULL, but not a flag */
4062 curitem++;
4063 }
4064 *p = NUL;
4065 itemcnt = curitem;
4066
Bram Moolenaar030f0df2006-02-21 22:02:53 +00004067#ifdef FEAT_EVAL
4068 if (usefmt != fmt)
4069 vim_free(usefmt);
4070#endif
4071
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 width = vim_strsize(out);
4073 if (maxwidth > 0 && width > maxwidth)
4074 {
Bram Moolenaar9381ab72008-11-12 11:52:19 +00004075 /* Result is too long, must truncate somewhere. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 l = 0;
4077 if (itemcnt == 0)
4078 s = out;
4079 else
4080 {
4081 for ( ; l < itemcnt; l++)
4082 if (item[l].type == Trunc)
4083 {
4084 /* Truncate at %< item. */
4085 s = item[l].start;
4086 break;
4087 }
4088 if (l == itemcnt)
4089 {
4090 /* No %< item, truncate first item. */
4091 s = item[0].start;
4092 l = 0;
4093 }
4094 }
4095
4096 if (width - vim_strsize(s) >= maxwidth)
4097 {
4098 /* Truncation mark is beyond max length */
4099#ifdef FEAT_MBYTE
4100 if (has_mbyte)
4101 {
4102 s = out;
4103 width = 0;
4104 for (;;)
4105 {
4106 width += ptr2cells(s);
4107 if (width >= maxwidth)
4108 break;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004109 s += (*mb_ptr2len)(s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 }
4111 /* Fill up for half a double-wide character. */
4112 while (++width < maxwidth)
4113 *s++ = fillchar;
4114 }
4115 else
4116#endif
4117 s = out + maxwidth - 1;
4118 for (l = 0; l < itemcnt; l++)
4119 if (item[l].start > s)
4120 break;
4121 itemcnt = l;
4122 *s++ = '>';
4123 *s = 0;
4124 }
4125 else
4126 {
4127#ifdef FEAT_MBYTE
4128 if (has_mbyte)
4129 {
4130 n = 0;
4131 while (width >= maxwidth)
4132 {
4133 width -= ptr2cells(s + n);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004134 n += (*mb_ptr2len)(s + n);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 }
4136 }
4137 else
4138#endif
4139 n = width - maxwidth + 1;
4140 p = s + n;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004141 STRMOVE(s + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 *s = '<';
4143
4144 /* Fill up for half a double-wide character. */
4145 while (++width < maxwidth)
4146 {
4147 s = s + STRLEN(s);
4148 *s++ = fillchar;
4149 *s = NUL;
4150 }
4151
4152 --n; /* count the '<' */
4153 for (; l < itemcnt; l++)
4154 {
4155 if (item[l].start - n >= s)
4156 item[l].start -= n;
4157 else
4158 item[l].start = s;
4159 }
4160 }
4161 width = maxwidth;
4162 }
4163 else if (width < maxwidth && STRLEN(out) + maxwidth - width + 1 < outlen)
4164 {
4165 /* Apply STL_MIDDLE if any */
4166 for (l = 0; l < itemcnt; l++)
4167 if (item[l].type == Middle)
4168 break;
4169 if (l < itemcnt)
4170 {
4171 p = item[l].start + maxwidth - width;
Bram Moolenaarf2330482008-06-24 20:19:36 +00004172 STRMOVE(p, item[l].start);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 for (s = item[l].start; s < p; s++)
4174 *s = fillchar;
4175 for (l++; l < itemcnt; l++)
4176 item[l].start += maxwidth - width;
4177 width = maxwidth;
4178 }
4179 }
4180
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004181 /* Store the info about highlighting. */
4182 if (hltab != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004184 sp = hltab;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 for (l = 0; l < itemcnt; l++)
4186 {
4187 if (item[l].type == Highlight)
4188 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004189 sp->start = item[l].start;
4190 sp->userhl = item[l].minwid;
4191 sp++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004194 sp->start = NULL;
4195 sp->userhl = 0;
4196 }
4197
4198 /* Store the info about tab pages labels. */
4199 if (tabtab != NULL)
4200 {
4201 sp = tabtab;
4202 for (l = 0; l < itemcnt; l++)
4203 {
4204 if (item[l].type == TabPage)
4205 {
4206 sp->start = item[l].start;
4207 sp->userhl = item[l].minwid;
4208 sp++;
4209 }
4210 }
4211 sp->start = NULL;
4212 sp->userhl = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 }
4214
4215 return width;
4216}
4217#endif /* FEAT_STL_OPT */
4218
Bram Moolenaarba6c0522006-02-25 21:45:02 +00004219#if defined(FEAT_STL_OPT) || defined(FEAT_CMDL_INFO) \
4220 || defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004222 * Get relative cursor position in window into "buf[buflen]", in the form 99%,
4223 * using "Top", "Bot" or "All" when appropriate.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 */
4225 void
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004226get_rel_pos(wp, buf, buflen)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 win_T *wp;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004228 char_u *buf;
4229 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230{
4231 long above; /* number of lines above window */
4232 long below; /* number of lines below window */
4233
4234 above = wp->w_topline - 1;
4235#ifdef FEAT_DIFF
4236 above += diff_check_fill(wp, wp->w_topline) - wp->w_topfill;
4237#endif
4238 below = wp->w_buffer->b_ml.ml_line_count - wp->w_botline + 1;
4239 if (below <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004240 vim_strncpy(buf, (char_u *)(above == 0 ? _("All") : _("Bot")),
4241 (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 else if (above <= 0)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004243 vim_strncpy(buf, (char_u *)_("Top"), (size_t)(buflen - 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004245 vim_snprintf((char *)buf, (size_t)buflen, "%2d%%", above > 1000000L
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 ? (int)(above / ((above + below) / 100L))
4247 : (int)(above * 100L / (above + below)));
4248}
4249#endif
4250
4251/*
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004252 * Append (file 2 of 8) to "buf[buflen]", if editing more than one file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 * Return TRUE if it was appended.
4254 */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004255 static int
4256append_arg_number(wp, buf, buflen, add_file)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 win_T *wp;
4258 char_u *buf;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004259 int buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 int add_file; /* Add "file" before the arg number */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261{
4262 char_u *p;
4263
4264 if (ARGCOUNT <= 1) /* nothing to do */
4265 return FALSE;
4266
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004267 p = buf + STRLEN(buf); /* go to the end of the buffer */
4268 if (p - buf + 35 >= buflen) /* getting too long */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 return FALSE;
4270 *p++ = ' ';
4271 *p++ = '(';
4272 if (add_file)
4273 {
4274 STRCPY(p, "file ");
4275 p += 5;
4276 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004277 vim_snprintf((char *)p, (size_t)(buflen - (p - buf)),
4278 wp->w_arg_idx_invalid ? "(%d) of %d)"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 : "%d of %d)", wp->w_arg_idx + 1, ARGCOUNT);
4280 return TRUE;
4281}
4282
4283/*
4284 * If fname is not a full path, make it a full path.
4285 * Returns pointer to allocated memory (NULL for failure).
4286 */
4287 char_u *
4288fix_fname(fname)
4289 char_u *fname;
4290{
4291 /*
4292 * Force expanding the path always for Unix, because symbolic links may
4293 * mess up the full path name, even though it starts with a '/'.
4294 * Also expand when there is ".." in the file name, try to remove it,
4295 * because "c:/src/../README" is equal to "c:/README".
Bram Moolenaar9b942202007-10-03 12:31:33 +00004296 * Similarly "c:/src//file" is equal to "c:/src/file".
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 * For MS-Windows also expand names like "longna~1" to "longname".
4298 */
Bram Moolenaar38323e42007-03-06 19:22:53 +00004299#ifdef UNIX
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 return FullName_save(fname, TRUE);
4301#else
Bram Moolenaar9b942202007-10-03 12:31:33 +00004302 if (!vim_isAbsName(fname)
4303 || strstr((char *)fname, "..") != NULL
4304 || strstr((char *)fname, "//") != NULL
4305# ifdef BACKSLASH_IN_FILENAME
4306 || strstr((char *)fname, "\\\\") != NULL
4307# endif
4308# if defined(MSWIN) || defined(DJGPP)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 || vim_strchr(fname, '~') != NULL
Bram Moolenaar9b942202007-10-03 12:31:33 +00004310# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 )
4312 return FullName_save(fname, FALSE);
4313
4314 fname = vim_strsave(fname);
4315
Bram Moolenaar9b942202007-10-03 12:31:33 +00004316# ifdef USE_FNAME_CASE
4317# ifdef USE_LONG_FNAME
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 if (USE_LONG_FNAME)
Bram Moolenaar9b942202007-10-03 12:31:33 +00004319# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 {
4321 if (fname != NULL)
4322 fname_case(fname, 0); /* set correct case for file name */
4323 }
Bram Moolenaar9b942202007-10-03 12:31:33 +00004324# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
4326 return fname;
4327#endif
4328}
4329
4330/*
4331 * Make "ffname" a full file name, set "sfname" to "ffname" if not NULL.
4332 * "ffname" becomes a pointer to allocated memory (or NULL).
4333 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 void
4335fname_expand(buf, ffname, sfname)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00004336 buf_T *buf UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 char_u **ffname;
4338 char_u **sfname;
4339{
4340 if (*ffname == NULL) /* if no file name given, nothing to do */
4341 return;
4342 if (*sfname == NULL) /* if no short file name given, use ffname */
4343 *sfname = *ffname;
4344 *ffname = fix_fname(*ffname); /* expand to full path */
4345
4346#ifdef FEAT_SHORTCUT
4347 if (!buf->b_p_bin)
4348 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004349 char_u *rfname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350
4351 /* If the file name is a shortcut file, use the file it links to. */
4352 rfname = mch_resolve_shortcut(*ffname);
Bram Moolenaarf193fff2006-04-27 00:02:13 +00004353 if (rfname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 {
4355 vim_free(*ffname);
4356 *ffname = rfname;
4357 *sfname = rfname;
4358 }
4359 }
4360#endif
4361}
4362
4363/*
4364 * Get the file name for an argument list entry.
4365 */
4366 char_u *
4367alist_name(aep)
4368 aentry_T *aep;
4369{
4370 buf_T *bp;
4371
4372 /* Use the name from the associated buffer if it exists. */
4373 bp = buflist_findnr(aep->ae_fnum);
Bram Moolenaar84212822006-11-07 21:59:47 +00004374 if (bp == NULL || bp->b_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 return aep->ae_fname;
4376 return bp->b_fname;
4377}
4378
4379#if defined(FEAT_WINDOWS) || defined(PROTO)
4380/*
4381 * do_arg_all(): Open up to 'count' windows, one for each argument.
4382 */
4383 void
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004384do_arg_all(count, forceit, keep_tabs)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 int count;
4386 int forceit; /* hide buffers in current windows */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004387 int keep_tabs; /* keep current tabs, for ":tab drop file" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388{
4389 int i;
4390 win_T *wp, *wpnext;
4391 char_u *opened; /* array of flags for which args are open */
Bram Moolenaard089d9b2007-09-30 12:02:55 +00004392 int opened_len; /* length of opened[] */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 int use_firstwin = FALSE; /* use first window for arglist */
4394 int split_ret = OK;
4395 int p_ea_save;
4396 alist_T *alist; /* argument list to be used */
4397 buf_T *buf;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004398 tabpage_T *tpnext;
4399 int had_tab = cmdmod.tab;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004400 win_T *new_curwin = NULL;
4401 tabpage_T *new_curtab = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
4403 if (ARGCOUNT <= 0)
4404 {
4405 /* Don't give an error message. We don't want it when the ":all"
4406 * command is in the .vimrc. */
4407 return;
4408 }
4409 setpcmark();
4410
4411 opened_len = ARGCOUNT;
4412 opened = alloc_clear((unsigned)opened_len);
4413 if (opened == NULL)
4414 return;
4415
4416#ifdef FEAT_GUI
4417 need_mouse_correct = TRUE;
4418#endif
4419
4420 /*
4421 * Try closing all windows that are not in the argument list.
4422 * Also close windows that are not full width;
4423 * When 'hidden' or "forceit" set the buffer becomes hidden.
4424 * Windows that have a changed buffer and can't be hidden won't be closed.
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004425 * When the ":tab" modifier was used do this for all tab pages.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004427 if (had_tab > 0)
4428 goto_tabpage_tp(first_tabpage);
4429 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004431 tpnext = curtab->tp_next;
4432 for (wp = firstwin; wp != NULL; wp = wpnext)
4433 {
4434 wpnext = wp->w_next;
4435 buf = wp->w_buffer;
4436 if (buf->b_ffname == NULL
4437 || buf->b_nwindows > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438#ifdef FEAT_VERTSPLIT
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004439 || wp->w_width != Columns
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004441 )
4442 i = ARGCOUNT;
4443 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004445 /* check if the buffer in this window is in the arglist */
4446 for (i = 0; i < ARGCOUNT; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004448 if (ARGLIST[i].ae_fnum == buf->b_fnum
4449 || fullpathcmp(alist_name(&ARGLIST[i]),
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004450 buf->b_ffname, TRUE) & FPC_SAME)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004452 if (i < opened_len)
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004453 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004454 opened[i] = TRUE;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004455 if (i == 0)
4456 {
4457 new_curwin = wp;
4458 new_curtab = curtab;
4459 }
4460 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004461 if (wp->w_alist != curwin->w_alist)
4462 {
4463 /* Use the current argument list for all windows
4464 * containing a file from it. */
4465 alist_unlink(wp->w_alist);
4466 wp->w_alist = curwin->w_alist;
4467 ++wp->w_alist->al_refcount;
4468 }
4469 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 }
4472 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004473 wp->w_arg_idx = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004475 if (i == ARGCOUNT && !keep_tabs) /* close this window */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004477 if (P_HID(buf) || forceit || buf->b_nwindows > 1
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004478 || !bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004480 /* If the buffer was changed, and we would like to hide it,
4481 * try autowriting. */
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004482 if (!P_HID(buf) && buf->b_nwindows <= 1
4483 && bufIsChanged(buf))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004485 (void)autowrite(buf, FALSE);
4486#ifdef FEAT_AUTOCMD
4487 /* check if autocommands removed the window */
4488 if (!win_valid(wp) || !buf_valid(buf))
4489 {
4490 wpnext = firstwin; /* start all over... */
4491 continue;
4492 }
4493#endif
4494 }
4495#ifdef FEAT_WINDOWS
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004496 /* don't close last window */
4497 if (firstwin == lastwin && first_tabpage->tp_next == NULL)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004498#endif
4499 use_firstwin = TRUE;
4500#ifdef FEAT_WINDOWS
4501 else
4502 {
4503 win_close(wp, !P_HID(buf) && !bufIsChanged(buf));
4504# ifdef FEAT_AUTOCMD
4505 /* check if autocommands removed the next window */
4506 if (!win_valid(wpnext))
4507 wpnext = firstwin; /* start all over... */
4508# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 }
4510#endif
4511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
4513 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004514
4515 /* Without the ":tab" modifier only do the current tab page. */
4516 if (had_tab == 0 || tpnext == NULL)
4517 break;
4518
4519# ifdef FEAT_AUTOCMD
4520 /* check if autocommands removed the next tab page */
4521 if (!valid_tabpage(tpnext))
4522 tpnext = first_tabpage; /* start all over...*/
4523# endif
4524 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526
4527 /*
4528 * Open a window for files in the argument list that don't have one.
4529 * ARGCOUNT may change while doing this, because of autocommands.
4530 */
4531 if (count > ARGCOUNT || count <= 0)
4532 count = ARGCOUNT;
4533
4534 /* Autocommands may do anything to the argument list. Make sure it's not
4535 * freed while we are working here by "locking" it. We still have to
4536 * watch out for its size to be changed. */
4537 alist = curwin->w_alist;
4538 ++alist->al_refcount;
4539
4540#ifdef FEAT_AUTOCMD
4541 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4542 ++autocmd_no_enter;
4543 ++autocmd_no_leave;
4544#endif
4545 win_enter(lastwin, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004546#ifdef FEAT_WINDOWS
4547 /* ":drop all" should re-use an empty window to avoid "--remote-tab"
4548 * leaving an empty tab page when executed locally. */
4549 if (keep_tabs && bufempty() && curbuf->b_nwindows == 1
4550 && curbuf->b_ffname == NULL && !curbuf->b_changed)
4551 use_firstwin = TRUE;
4552#endif
4553
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 for (i = 0; i < count && i < alist->al_ga.ga_len && !got_int; ++i)
4555 {
4556 if (alist == &global_alist && i == global_alist.al_ga.ga_len - 1)
4557 arg_had_last = TRUE;
4558 if (i < opened_len && opened[i])
4559 {
4560 /* Move the already present window to below the current window */
4561 if (curwin->w_arg_idx != i)
4562 {
4563 for (wpnext = firstwin; wpnext != NULL; wpnext = wpnext->w_next)
4564 {
4565 if (wpnext->w_arg_idx == i)
4566 {
4567 win_move_after(wpnext, curwin);
4568 break;
4569 }
4570 }
4571 }
4572 }
4573 else if (split_ret == OK)
4574 {
4575 if (!use_firstwin) /* split current window */
4576 {
4577 p_ea_save = p_ea;
4578 p_ea = TRUE; /* use space from all windows */
4579 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4580 p_ea = p_ea_save;
4581 if (split_ret == FAIL)
4582 continue;
4583 }
4584#ifdef FEAT_AUTOCMD
4585 else /* first window: do autocmd for leaving this buffer */
4586 --autocmd_no_leave;
4587#endif
4588
4589 /*
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004590 * edit file "i"
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 */
4592 curwin->w_arg_idx = i;
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004593 if (i == 0)
4594 {
4595 new_curwin = curwin;
4596 new_curtab = curtab;
4597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 (void)do_ecmd(0, alist_name(&AARGLIST(alist)[i]), NULL, NULL,
4599 ECMD_ONE,
4600 ((P_HID(curwin->w_buffer)
4601 || bufIsChanged(curwin->w_buffer)) ? ECMD_HIDE : 0)
Bram Moolenaar701f7af2008-11-15 13:12:07 +00004602 + ECMD_OLDBUF, curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603#ifdef FEAT_AUTOCMD
4604 if (use_firstwin)
4605 ++autocmd_no_leave;
4606#endif
4607 use_firstwin = FALSE;
4608 }
4609 ui_breakcheck();
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004610
4611 /* When ":tab" was used open a new tab for a new window repeatedly. */
4612 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4613 cmdmod.tab = 9999;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 }
4615
4616 /* Remove the "lock" on the argument list. */
4617 alist_unlink(alist);
4618
4619#ifdef FEAT_AUTOCMD
4620 --autocmd_no_enter;
4621#endif
Bram Moolenaar8ee89262006-03-11 21:16:47 +00004622 /* to window with first arg */
4623 if (valid_tabpage(new_curtab))
4624 goto_tabpage_tp(new_curtab);
4625 if (win_valid(new_curwin))
4626 win_enter(new_curwin, FALSE);
4627
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628#ifdef FEAT_AUTOCMD
4629 --autocmd_no_leave;
4630#endif
4631 vim_free(opened);
4632}
4633
4634# if defined(FEAT_LISTCMDS) || defined(PROTO)
4635/*
4636 * Open a window for a number of buffers.
4637 */
4638 void
4639ex_buffer_all(eap)
4640 exarg_T *eap;
4641{
4642 buf_T *buf;
4643 win_T *wp, *wpnext;
4644 int split_ret = OK;
4645 int p_ea_save;
4646 int open_wins = 0;
4647 int r;
4648 int count; /* Maximum number of windows to open. */
4649 int all; /* When TRUE also load inactive buffers. */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004650#ifdef FEAT_WINDOWS
4651 int had_tab = cmdmod.tab;
4652 tabpage_T *tpnext;
4653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654
4655 if (eap->addr_count == 0) /* make as many windows as possible */
4656 count = 9999;
4657 else
4658 count = eap->line2; /* make as many windows as specified */
4659 if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide)
4660 all = FALSE;
4661 else
4662 all = TRUE;
4663
4664 setpcmark();
4665
4666#ifdef FEAT_GUI
4667 need_mouse_correct = TRUE;
4668#endif
4669
4670 /*
4671 * Close superfluous windows (two windows for the same buffer).
4672 * Also close windows that are not full-width.
4673 */
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004674#ifdef FEAT_WINDOWS
4675 if (had_tab > 0)
4676 goto_tabpage_tp(first_tabpage);
4677 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004680 tpnext = curtab->tp_next;
4681 for (wp = firstwin; wp != NULL; wp = wpnext)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 {
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004683 wpnext = wp->w_next;
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004684 if ((wp->w_buffer->b_nwindows > 1
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004685#ifdef FEAT_VERTSPLIT
4686 || ((cmdmod.split & WSP_VERT)
4687 ? wp->w_height + wp->w_status_height < Rows - p_ch
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004688 - tabline_height()
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004689 : wp->w_width != Columns)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690#endif
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004691#ifdef FEAT_WINDOWS
4692 || (had_tab > 0 && wp != firstwin)
4693#endif
Bram Moolenaarbfb2d402006-03-03 22:50:42 +00004694 ) && firstwin != lastwin)
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004695 {
4696 win_close(wp, FALSE);
4697#ifdef FEAT_AUTOCMD
4698 wpnext = firstwin; /* just in case an autocommand does
4699 something strange with windows */
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004700 tpnext = first_tabpage; /* start all over...*/
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004701 open_wins = 0;
4702#endif
4703 }
4704 else
4705 ++open_wins;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004707
4708#ifdef FEAT_WINDOWS
4709 /* Without the ":tab" modifier only do the current tab page. */
4710 if (had_tab == 0 || tpnext == NULL)
4711 break;
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004712 goto_tabpage_tp(tpnext);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 }
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
4716 /*
4717 * Go through the buffer list. When a buffer doesn't have a window yet,
4718 * open one. Otherwise move the window to the right position.
4719 * Watch out for autocommands that delete buffers or windows!
4720 */
4721#ifdef FEAT_AUTOCMD
4722 /* Don't execute Win/Buf Enter/Leave autocommands here. */
4723 ++autocmd_no_enter;
4724#endif
4725 win_enter(lastwin, FALSE);
4726#ifdef FEAT_AUTOCMD
4727 ++autocmd_no_leave;
4728#endif
4729 for (buf = firstbuf; buf != NULL && open_wins < count; buf = buf->b_next)
4730 {
4731 /* Check if this buffer needs a window */
4732 if ((!all && buf->b_ml.ml_mfp == NULL) || !buf->b_p_bl)
4733 continue;
4734
Bram Moolenaarb475fb92006-03-02 22:40:52 +00004735#ifdef FEAT_WINDOWS
4736 if (had_tab != 0)
4737 {
4738 /* With the ":tab" modifier don't move the window. */
4739 if (buf->b_nwindows > 0)
4740 wp = lastwin; /* buffer has a window, skip it */
4741 else
4742 wp = NULL;
4743 }
4744 else
4745#endif
4746 {
4747 /* Check if this buffer already has a window */
4748 for (wp = firstwin; wp != NULL; wp = wp->w_next)
4749 if (wp->w_buffer == buf)
4750 break;
4751 /* If the buffer already has a window, move it */
4752 if (wp != NULL)
4753 win_move_after(wp, curwin);
4754 }
4755
4756 if (wp == NULL && split_ret == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
4758 /* Split the window and put the buffer in it */
4759 p_ea_save = p_ea;
4760 p_ea = TRUE; /* use space from all windows */
4761 split_ret = win_split(0, WSP_ROOM | WSP_BELOW);
4762 ++open_wins;
4763 p_ea = p_ea_save;
4764 if (split_ret == FAIL)
4765 continue;
4766
4767 /* Open the buffer in this window. */
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_DIALOG;
4770#endif
4771 set_curbuf(buf, DOBUF_GOTO);
4772#ifdef FEAT_AUTOCMD
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 if (!buf_valid(buf)) /* autocommands deleted the buffer!!! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 {
Bram Moolenaare64ac772005-12-07 20:54:59 +00004775#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 swap_exists_action = SEA_NONE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004777# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 break;
4779 }
4780#endif
Bram Moolenaare64ac772005-12-07 20:54:59 +00004781#if defined(HAS_SWAP_EXISTS_ACTION)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 if (swap_exists_action == SEA_QUIT)
4783 {
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004784# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4785 cleanup_T cs;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004786
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004787 /* Reset the error/interrupt/exception state here so that
4788 * aborting() returns FALSE when closing a window. */
4789 enter_cleanup(&cs);
4790# endif
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004791
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004792 /* User selected Quit at ATTENTION prompt; close this window. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 win_close(curwin, TRUE);
4794 --open_wins;
4795 swap_exists_action = SEA_NONE;
Bram Moolenaar12033fb2005-12-16 21:49:31 +00004796 swap_exists_did_quit = TRUE;
Bram Moolenaarc0197e22004-09-13 20:26:32 +00004797
4798# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
4799 /* Restore the error/interrupt/exception state if not
4800 * discarded by a new aborting error, interrupt, or uncaught
4801 * exception. */
4802 leave_cleanup(&cs);
4803# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 else
4806 handle_swap_exists(NULL);
4807#endif
4808 }
4809
4810 ui_breakcheck();
4811 if (got_int)
4812 {
4813 (void)vgetc(); /* only break the file loading, not the rest */
4814 break;
4815 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00004816#ifdef FEAT_EVAL
4817 /* Autocommands deleted the buffer or aborted script processing!!! */
4818 if (aborting())
4819 break;
4820#endif
Bram Moolenaare1438bb2006-03-01 22:01:55 +00004821#ifdef FEAT_WINDOWS
4822 /* When ":tab" was used open a new tab for a new window repeatedly. */
4823 if (had_tab > 0 && tabpage_index(NULL) <= p_tpm)
4824 cmdmod.tab = 9999;
4825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827#ifdef FEAT_AUTOCMD
4828 --autocmd_no_enter;
4829#endif
4830 win_enter(firstwin, FALSE); /* back to first window */
4831#ifdef FEAT_AUTOCMD
4832 --autocmd_no_leave;
4833#endif
4834
4835 /*
4836 * Close superfluous windows.
4837 */
4838 for (wp = lastwin; open_wins > count; )
4839 {
4840 r = (P_HID(wp->w_buffer) || !bufIsChanged(wp->w_buffer)
4841 || autowrite(wp->w_buffer, FALSE) == OK);
4842#ifdef FEAT_AUTOCMD
4843 if (!win_valid(wp))
4844 {
4845 /* BufWrite Autocommands made the window invalid, start over */
4846 wp = lastwin;
4847 }
4848 else
4849#endif
4850 if (r)
4851 {
4852 win_close(wp, !P_HID(wp->w_buffer));
4853 --open_wins;
4854 wp = lastwin;
4855 }
4856 else
4857 {
4858 wp = wp->w_prev;
4859 if (wp == NULL)
4860 break;
4861 }
4862 }
4863}
4864# endif /* FEAT_LISTCMDS */
4865
4866#endif /* FEAT_WINDOWS */
4867
Bram Moolenaara3227e22006-03-08 21:32:40 +00004868static int chk_modeline __ARGS((linenr_T, int));
4869
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870/*
4871 * do_modelines() - process mode lines for the current file
4872 *
Bram Moolenaara3227e22006-03-08 21:32:40 +00004873 * "flags" can be:
4874 * OPT_WINONLY only set options local to window
4875 * OPT_NOWIN don't set options local to window
4876 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 * Returns immediately if the "ml" option isn't set.
4878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 void
Bram Moolenaara3227e22006-03-08 21:32:40 +00004880do_modelines(flags)
4881 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882{
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00004883 linenr_T lnum;
4884 int nmlines;
4885 static int entered = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 if (!curbuf->b_p_ml || (nmlines = (int)p_mls) == 0)
4888 return;
4889
4890 /* Disallow recursive entry here. Can happen when executing a modeline
4891 * triggers an autocommand, which reloads modelines with a ":do". */
4892 if (entered)
4893 return;
4894
4895 ++entered;
4896 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count && lnum <= nmlines;
4897 ++lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004898 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 nmlines = 0;
4900
4901 for (lnum = curbuf->b_ml.ml_line_count; lnum > 0 && lnum > nmlines
4902 && lnum > curbuf->b_ml.ml_line_count - nmlines; --lnum)
Bram Moolenaara3227e22006-03-08 21:32:40 +00004903 if (chk_modeline(lnum, flags) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 nmlines = 0;
4905 --entered;
4906}
4907
4908#include "version.h" /* for version number */
4909
4910/*
4911 * chk_modeline() - check a single line for a mode string
4912 * Return FAIL if an error encountered.
4913 */
4914 static int
Bram Moolenaara3227e22006-03-08 21:32:40 +00004915chk_modeline(lnum, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 linenr_T lnum;
Bram Moolenaara3227e22006-03-08 21:32:40 +00004917 int flags; /* Same as for do_modelines(). */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918{
4919 char_u *s;
4920 char_u *e;
4921 char_u *linecopy; /* local copy of any modeline found */
4922 int prev;
4923 int vers;
4924 int end;
4925 int retval = OK;
4926 char_u *save_sourcing_name;
4927 linenr_T save_sourcing_lnum;
4928#ifdef FEAT_EVAL
4929 scid_T save_SID;
4930#endif
4931
4932 prev = -1;
4933 for (s = ml_get(lnum); *s != NUL; ++s)
4934 {
4935 if (prev == -1 || vim_isspace(prev))
4936 {
4937 if ((prev != -1 && STRNCMP(s, "ex:", (size_t)3) == 0)
4938 || STRNCMP(s, "vi:", (size_t)3) == 0)
4939 break;
4940 if (STRNCMP(s, "vim", 3) == 0)
4941 {
4942 if (s[3] == '<' || s[3] == '=' || s[3] == '>')
4943 e = s + 4;
4944 else
4945 e = s + 3;
4946 vers = getdigits(&e);
4947 if (*e == ':'
4948 && (s[3] == ':'
4949 || (VIM_VERSION_100 >= vers && isdigit(s[3]))
4950 || (VIM_VERSION_100 < vers && s[3] == '<')
4951 || (VIM_VERSION_100 > vers && s[3] == '>')
4952 || (VIM_VERSION_100 == vers && s[3] == '=')))
4953 break;
4954 }
4955 }
4956 prev = *s;
4957 }
4958
4959 if (*s)
4960 {
4961 do /* skip over "ex:", "vi:" or "vim:" */
4962 ++s;
4963 while (s[-1] != ':');
4964
4965 s = linecopy = vim_strsave(s); /* copy the line, it will change */
4966 if (linecopy == NULL)
4967 return FAIL;
4968
4969 save_sourcing_lnum = sourcing_lnum;
4970 save_sourcing_name = sourcing_name;
4971 sourcing_lnum = lnum; /* prepare for emsg() */
4972 sourcing_name = (char_u *)"modelines";
4973
4974 end = FALSE;
4975 while (end == FALSE)
4976 {
4977 s = skipwhite(s);
4978 if (*s == NUL)
4979 break;
4980
4981 /*
4982 * Find end of set command: ':' or end of line.
4983 * Skip over "\:", replacing it with ":".
4984 */
4985 for (e = s; *e != ':' && *e != NUL; ++e)
4986 if (e[0] == '\\' && e[1] == ':')
Bram Moolenaarf2330482008-06-24 20:19:36 +00004987 STRMOVE(e, e + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 if (*e == NUL)
4989 end = TRUE;
4990
4991 /*
4992 * If there is a "set" command, require a terminating ':' and
4993 * ignore the stuff after the ':'.
4994 * "vi:set opt opt opt: foo" -- foo not interpreted
4995 * "vi:opt opt opt: foo" -- foo interpreted
4996 * Accept "se" for compatibility with Elvis.
4997 */
4998 if (STRNCMP(s, "set ", (size_t)4) == 0
4999 || STRNCMP(s, "se ", (size_t)3) == 0)
5000 {
5001 if (*e != ':') /* no terminating ':'? */
5002 break;
5003 end = TRUE;
5004 s = vim_strchr(s, ' ') + 1;
5005 }
5006 *e = NUL; /* truncate the set command */
5007
5008 if (*s != NUL) /* skip over an empty "::" */
5009 {
5010#ifdef FEAT_EVAL
5011 save_SID = current_SID;
5012 current_SID = SID_MODELINE;
5013#endif
Bram Moolenaara3227e22006-03-08 21:32:40 +00005014 retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015#ifdef FEAT_EVAL
5016 current_SID = save_SID;
5017#endif
5018 if (retval == FAIL) /* stop if error found */
5019 break;
5020 }
5021 s = e + 1; /* advance to next part */
5022 }
5023
5024 sourcing_lnum = save_sourcing_lnum;
5025 sourcing_name = save_sourcing_name;
5026
5027 vim_free(linecopy);
5028 }
5029 return retval;
5030}
5031
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005032#if defined(FEAT_VIMINFO) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 int
5034read_viminfo_bufferlist(virp, writing)
5035 vir_T *virp;
5036 int writing;
5037{
5038 char_u *tab;
5039 linenr_T lnum;
5040 colnr_T col;
5041 buf_T *buf;
5042 char_u *sfname;
5043 char_u *xline;
5044
5045 /* Handle long line and escaped characters. */
5046 xline = viminfo_readstring(virp, 1, FALSE);
5047
5048 /* don't read in if there are files on the command-line or if writing: */
5049 if (xline != NULL && !writing && ARGCOUNT == 0
5050 && find_viminfo_parameter('%') != NULL)
5051 {
5052 /* Format is: <fname> Tab <lnum> Tab <col>.
5053 * Watch out for a Tab in the file name, work from the end. */
5054 lnum = 0;
5055 col = 0;
5056 tab = vim_strrchr(xline, '\t');
5057 if (tab != NULL)
5058 {
5059 *tab++ = '\0';
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005060 col = (colnr_T)atoi((char *)tab);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 tab = vim_strrchr(xline, '\t');
5062 if (tab != NULL)
5063 {
5064 *tab++ = '\0';
5065 lnum = atol((char *)tab);
5066 }
5067 }
5068
5069 /* Expand "~/" in the file name at "line + 1" to a full path.
5070 * Then try shortening it by comparing with the current directory */
5071 expand_env(xline, NameBuff, MAXPATHL);
Bram Moolenaard089d9b2007-09-30 12:02:55 +00005072 sfname = shorten_fname1(NameBuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 buf = buflist_new(NameBuff, sfname, (linenr_T)0, BLN_LISTED);
5075 if (buf != NULL) /* just in case... */
5076 {
5077 buf->b_last_cursor.lnum = lnum;
5078 buf->b_last_cursor.col = col;
5079 buflist_setfpos(buf, curwin, lnum, col, FALSE);
5080 }
5081 }
5082 vim_free(xline);
5083
5084 return viminfo_readline(virp);
5085}
5086
5087 void
5088write_viminfo_bufferlist(fp)
5089 FILE *fp;
5090{
5091 buf_T *buf;
5092#ifdef FEAT_WINDOWS
5093 win_T *win;
Bram Moolenaarf740b292006-02-16 22:11:02 +00005094 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095#endif
5096 char_u *line;
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005097 int max_buffers;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
5099 if (find_viminfo_parameter('%') == NULL)
5100 return;
5101
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005102 /* Without a number -1 is returned: do all buffers. */
5103 max_buffers = get_viminfo_parameter('%');
5104
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 /* Allocate room for the file name, lnum and col. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005106#define LINE_BUF_LEN (MAXPATHL + 40)
5107 line = alloc(LINE_BUF_LEN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 if (line == NULL)
5109 return;
5110
5111#ifdef FEAT_WINDOWS
Bram Moolenaarf740b292006-02-16 22:11:02 +00005112 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 set_last_cursor(win);
5114#else
5115 set_last_cursor(curwin);
5116#endif
5117
Bram Moolenaar2f1e0502010-08-13 11:18:02 +02005118 fputs(_("\n# Buffer list:\n"), fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 for (buf = firstbuf; buf != NULL ; buf = buf->b_next)
5120 {
5121 if (buf->b_fname == NULL
5122 || !buf->b_p_bl
5123#ifdef FEAT_QUICKFIX
5124 || bt_quickfix(buf)
5125#endif
5126 || removable(buf->b_ffname))
5127 continue;
5128
Bram Moolenaar15d0a8c2004-09-06 17:44:46 +00005129 if (max_buffers-- == 0)
5130 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 putc('%', fp);
5132 home_replace(NULL, buf->b_ffname, line, MAXPATHL, TRUE);
Bram Moolenaara800b422010-06-27 01:15:55 +02005133 vim_snprintf_add((char *)line, LINE_BUF_LEN, "\t%ld\t%d",
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 (long)buf->b_last_cursor.lnum,
5135 buf->b_last_cursor.col);
5136 viminfo_writestring(fp, line);
5137 }
5138 vim_free(line);
5139}
5140#endif
5141
5142
5143/*
5144 * Return special buffer name.
5145 * Returns NULL when the buffer has a normal file name.
5146 */
5147 char *
5148buf_spname(buf)
5149 buf_T *buf;
5150{
5151#if defined(FEAT_QUICKFIX) && defined(FEAT_WINDOWS)
5152 if (bt_quickfix(buf))
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005153 {
Bram Moolenaarb561a612008-03-12 12:46:13 +00005154 win_T *win = NULL;
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005155 tabpage_T *tp;
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005156
5157 /*
5158 * For location list window, w_llist_ref points to the location list.
5159 * For quickfix window, w_llist_ref is NULL.
5160 */
Bram Moolenaar91d8e0c2008-03-12 11:23:53 +00005161 FOR_ALL_TAB_WINDOWS(tp, win)
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005162 if (win->w_buffer == buf)
Bram Moolenaarbb9c7d12009-02-21 23:03:09 +00005163 goto win_found;
5164win_found:
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005165 if (win != NULL && win->w_llist_ref != NULL)
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005166 return _(msg_loclist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005167 else
Bram Moolenaar7fd73202010-07-25 16:58:46 +02005168 return _(msg_qflist);
Bram Moolenaar28c258f2006-01-25 22:02:51 +00005169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170#endif
5171#ifdef FEAT_QUICKFIX
5172 /* There is no _file_ when 'buftype' is "nofile", b_sfname
5173 * contains the name as specified by the user */
5174 if (bt_nofile(buf))
5175 {
5176 if (buf->b_sfname != NULL)
5177 return (char *)buf->b_sfname;
Bram Moolenaarf4f664c2008-12-03 10:21:57 +00005178 return _("[Scratch]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180#endif
5181 if (buf->b_fname == NULL)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00005182 return _("[No Name]");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 return NULL;
5184}
5185
5186
5187#if defined(FEAT_SIGNS) || defined(PROTO)
5188/*
5189 * Insert the sign into the signlist.
5190 */
5191 static void
5192insert_sign(buf, prev, next, id, lnum, typenr)
5193 buf_T *buf; /* buffer to store sign in */
5194 signlist_T *prev; /* previous sign entry */
5195 signlist_T *next; /* next sign entry */
5196 int id; /* sign ID */
5197 linenr_T lnum; /* line number which gets the mark */
5198 int typenr; /* typenr of sign we are adding */
5199{
5200 signlist_T *newsign;
5201
5202 newsign = (signlist_T *)lalloc((long_u)sizeof(signlist_T), FALSE);
5203 if (newsign != NULL)
5204 {
5205 newsign->id = id;
5206 newsign->lnum = lnum;
5207 newsign->typenr = typenr;
5208 newsign->next = next;
5209#ifdef FEAT_NETBEANS_INTG
5210 newsign->prev = prev;
5211 if (next != NULL)
5212 next->prev = newsign;
5213#endif
5214
5215 if (prev == NULL)
5216 {
5217 /* When adding first sign need to redraw the windows to create the
5218 * column for signs. */
5219 if (buf->b_signlist == NULL)
5220 {
5221 redraw_buf_later(buf, NOT_VALID);
5222 changed_cline_bef_curs();
5223 }
5224
5225 /* first sign in signlist */
5226 buf->b_signlist = newsign;
5227 }
5228 else
5229 prev->next = newsign;
5230 }
5231}
5232
5233/*
5234 * Add the sign into the signlist. Find the right spot to do it though.
5235 */
5236 void
5237buf_addsign(buf, id, lnum, typenr)
5238 buf_T *buf; /* buffer to store sign in */
5239 int id; /* sign ID */
5240 linenr_T lnum; /* line number which gets the mark */
5241 int typenr; /* typenr of sign we are adding */
5242{
5243 signlist_T *sign; /* a sign in the signlist */
5244 signlist_T *prev; /* the previous sign */
5245
5246 prev = NULL;
5247 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5248 {
5249 if (lnum == sign->lnum && id == sign->id)
5250 {
5251 sign->typenr = typenr;
5252 return;
5253 }
5254 else if (
5255#ifndef FEAT_NETBEANS_INTG /* keep signs sorted by lnum */
5256 id < 0 &&
5257#endif
5258 lnum < sign->lnum)
5259 {
5260#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5261 /* XXX - GRP: Is this because of sign slide problem? Or is it
5262 * really needed? Or is it because we allow multiple signs per
5263 * line? If so, should I add that feature to FEAT_SIGNS?
5264 */
5265 while (prev != NULL && prev->lnum == lnum)
5266 prev = prev->prev;
5267 if (prev == NULL)
5268 sign = buf->b_signlist;
5269 else
5270 sign = prev->next;
5271#endif
5272 insert_sign(buf, prev, sign, id, lnum, typenr);
5273 return;
5274 }
5275 prev = sign;
5276 }
5277#ifdef FEAT_NETBEANS_INTG /* insert new sign at head of list for this lnum */
5278 /* XXX - GRP: See previous comment */
5279 while (prev != NULL && prev->lnum == lnum)
5280 prev = prev->prev;
5281 if (prev == NULL)
5282 sign = buf->b_signlist;
5283 else
5284 sign = prev->next;
5285#endif
5286 insert_sign(buf, prev, sign, id, lnum, typenr);
5287
5288 return;
5289}
5290
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005291 linenr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292buf_change_sign_type(buf, markId, typenr)
5293 buf_T *buf; /* buffer to store sign in */
5294 int markId; /* sign ID */
5295 int typenr; /* typenr of sign we are adding */
5296{
5297 signlist_T *sign; /* a sign in the signlist */
5298
5299 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5300 {
5301 if (sign->id == markId)
5302 {
5303 sign->typenr = typenr;
5304 return sign->lnum;
5305 }
5306 }
5307
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005308 return (linenr_T)0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309}
5310
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00005311 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312buf_getsigntype(buf, lnum, type)
5313 buf_T *buf;
5314 linenr_T lnum;
5315 int type; /* SIGN_ICON, SIGN_TEXT, SIGN_ANY, SIGN_LINEHL */
5316{
5317 signlist_T *sign; /* a sign in a b_signlist */
5318
5319 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5320 if (sign->lnum == lnum
5321 && (type == SIGN_ANY
5322# ifdef FEAT_SIGN_ICONS
5323 || (type == SIGN_ICON
5324 && sign_get_image(sign->typenr) != NULL)
5325# endif
5326 || (type == SIGN_TEXT
5327 && sign_get_text(sign->typenr) != NULL)
5328 || (type == SIGN_LINEHL
5329 && sign_get_attr(sign->typenr, TRUE) != 0)))
5330 return sign->typenr;
5331 return 0;
5332}
5333
5334
5335 linenr_T
5336buf_delsign(buf, id)
5337 buf_T *buf; /* buffer sign is stored in */
5338 int id; /* sign id */
5339{
5340 signlist_T **lastp; /* pointer to pointer to current sign */
5341 signlist_T *sign; /* a sign in a b_signlist */
5342 signlist_T *next; /* the next sign in a b_signlist */
5343 linenr_T lnum; /* line number whose sign was deleted */
5344
5345 lastp = &buf->b_signlist;
5346 lnum = 0;
5347 for (sign = buf->b_signlist; sign != NULL; sign = next)
5348 {
5349 next = sign->next;
5350 if (sign->id == id)
5351 {
5352 *lastp = next;
5353#ifdef FEAT_NETBEANS_INTG
5354 if (next != NULL)
5355 next->prev = sign->prev;
5356#endif
5357 lnum = sign->lnum;
5358 vim_free(sign);
5359 break;
5360 }
5361 else
5362 lastp = &sign->next;
5363 }
5364
5365 /* When deleted the last sign need to redraw the windows to remove the
5366 * sign column. */
5367 if (buf->b_signlist == NULL)
5368 {
5369 redraw_buf_later(buf, NOT_VALID);
5370 changed_cline_bef_curs();
5371 }
5372
5373 return lnum;
5374}
5375
5376
5377/*
5378 * Find the line number of the sign with the requested id. If the sign does
5379 * not exist, return 0 as the line number. This will still let the correct file
5380 * get loaded.
5381 */
5382 int
5383buf_findsign(buf, id)
5384 buf_T *buf; /* buffer to store sign in */
5385 int id; /* sign ID */
5386{
5387 signlist_T *sign; /* a sign in the signlist */
5388
5389 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5390 if (sign->id == id)
5391 return sign->lnum;
5392
5393 return 0;
5394}
5395
5396 int
5397buf_findsign_id(buf, lnum)
5398 buf_T *buf; /* buffer whose sign we are searching for */
5399 linenr_T lnum; /* line number of sign */
5400{
5401 signlist_T *sign; /* a sign in the signlist */
5402
5403 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5404 if (sign->lnum == lnum)
5405 return sign->id;
5406
5407 return 0;
5408}
5409
5410
5411# if defined(FEAT_NETBEANS_INTG) || defined(PROTO)
5412/* see if a given type of sign exists on a specific line */
5413 int
5414buf_findsigntype_id(buf, lnum, typenr)
5415 buf_T *buf; /* buffer whose sign we are searching for */
5416 linenr_T lnum; /* line number of sign */
5417 int typenr; /* sign type number */
5418{
5419 signlist_T *sign; /* a sign in the signlist */
5420
5421 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5422 if (sign->lnum == lnum && sign->typenr == typenr)
5423 return sign->id;
5424
5425 return 0;
5426}
5427
5428
5429# if defined(FEAT_SIGN_ICONS) || defined(PROTO)
5430/* return the number of icons on the given line */
5431 int
5432buf_signcount(buf, lnum)
5433 buf_T *buf;
5434 linenr_T lnum;
5435{
5436 signlist_T *sign; /* a sign in the signlist */
5437 int count = 0;
5438
5439 for (sign = buf->b_signlist; sign != NULL; sign = sign->next)
5440 if (sign->lnum == lnum)
5441 if (sign_get_image(sign->typenr) != NULL)
5442 count++;
5443
5444 return count;
5445}
5446# endif /* FEAT_SIGN_ICONS */
5447# endif /* FEAT_NETBEANS_INTG */
5448
5449
5450/*
5451 * Delete signs in buffer "buf".
5452 */
5453 static void
5454buf_delete_signs(buf)
5455 buf_T *buf;
5456{
5457 signlist_T *next;
5458
5459 while (buf->b_signlist != NULL)
5460 {
5461 next = buf->b_signlist->next;
5462 vim_free(buf->b_signlist);
5463 buf->b_signlist = next;
5464 }
5465}
5466
5467/*
5468 * Delete all signs in all buffers.
5469 */
5470 void
5471buf_delete_all_signs()
5472{
5473 buf_T *buf; /* buffer we are checking for signs */
5474
5475 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
5476 if (buf->b_signlist != NULL)
5477 {
5478 /* Need to redraw the windows to remove the sign column. */
5479 redraw_buf_later(buf, NOT_VALID);
5480 buf_delete_signs(buf);
5481 }
5482}
5483
5484/*
5485 * List placed signs for "rbuf". If "rbuf" is NULL do it for all buffers.
5486 */
5487 void
5488sign_list_placed(rbuf)
5489 buf_T *rbuf;
5490{
5491 buf_T *buf;
5492 signlist_T *p;
5493 char lbuf[BUFSIZ];
5494
5495 MSG_PUTS_TITLE(_("\n--- Signs ---"));
5496 msg_putchar('\n');
5497 if (rbuf == NULL)
5498 buf = firstbuf;
5499 else
5500 buf = rbuf;
5501 while (buf != NULL)
5502 {
5503 if (buf->b_signlist != NULL)
5504 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005505 vim_snprintf(lbuf, BUFSIZ, _("Signs for %s:"), buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 MSG_PUTS_ATTR(lbuf, hl_attr(HLF_D));
5507 msg_putchar('\n');
5508 }
5509 for (p = buf->b_signlist; p != NULL; p = p->next)
5510 {
Bram Moolenaar9c13b352005-05-19 20:53:52 +00005511 vim_snprintf(lbuf, BUFSIZ, _(" line=%ld id=%d name=%s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 (long)p->lnum, p->id, sign_typenr2name(p->typenr));
5513 MSG_PUTS(lbuf);
5514 msg_putchar('\n');
5515 }
5516 if (rbuf != NULL)
5517 break;
5518 buf = buf->b_next;
5519 }
5520}
5521
5522/*
5523 * Adjust a placed sign for inserted/deleted lines.
5524 */
5525 void
5526sign_mark_adjust(line1, line2, amount, amount_after)
5527 linenr_T line1;
5528 linenr_T line2;
5529 long amount;
5530 long amount_after;
5531{
5532 signlist_T *sign; /* a sign in a b_signlist */
5533
5534 for (sign = curbuf->b_signlist; sign != NULL; sign = sign->next)
5535 {
5536 if (sign->lnum >= line1 && sign->lnum <= line2)
5537 {
5538 if (amount == MAXLNUM)
5539 sign->lnum = line1;
5540 else
5541 sign->lnum += amount;
5542 }
5543 else if (sign->lnum > line2)
5544 sign->lnum += amount_after;
5545 }
5546}
5547#endif /* FEAT_SIGNS */
5548
5549/*
5550 * Set 'buflisted' for curbuf to "on" and trigger autocommands if it changed.
5551 */
5552 void
5553set_buflisted(on)
5554 int on;
5555{
5556 if (on != curbuf->b_p_bl)
5557 {
5558 curbuf->b_p_bl = on;
5559#ifdef FEAT_AUTOCMD
5560 if (on)
5561 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
5562 else
5563 apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
5564#endif
5565 }
5566}
5567
5568/*
5569 * Read the file for "buf" again and check if the contents changed.
5570 * Return TRUE if it changed or this could not be checked.
5571 */
5572 int
5573buf_contents_changed(buf)
5574 buf_T *buf;
5575{
5576 buf_T *newbuf;
5577 int differ = TRUE;
5578 linenr_T lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 exarg_T ea;
5581
5582 /* Allocate a buffer without putting it in the buffer list. */
5583 newbuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
5584 if (newbuf == NULL)
5585 return TRUE;
5586
5587 /* Force the 'fileencoding' and 'fileformat' to be equal. */
5588 if (prep_exarg(&ea, buf) == FAIL)
5589 {
5590 wipe_buffer(newbuf, FALSE);
5591 return TRUE;
5592 }
5593
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 /* set curwin/curbuf to buf and save a few things */
5595 aucmd_prepbuf(&aco, newbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar4770d092006-01-12 23:22:24 +00005597 if (ml_open(curbuf) == OK
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 && readfile(buf->b_ffname, buf->b_fname,
5599 (linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM,
5600 &ea, READ_NEW | READ_DUMMY) == OK)
5601 {
5602 /* compare the two files line by line */
5603 if (buf->b_ml.ml_line_count == curbuf->b_ml.ml_line_count)
5604 {
5605 differ = FALSE;
5606 for (lnum = 1; lnum <= curbuf->b_ml.ml_line_count; ++lnum)
5607 if (STRCMP(ml_get_buf(buf, lnum, FALSE), ml_get(lnum)) != 0)
5608 {
5609 differ = TRUE;
5610 break;
5611 }
5612 }
5613 }
5614 vim_free(ea.cmd);
5615
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 /* restore curwin/curbuf and a few other things */
5617 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618
5619 if (curbuf != newbuf) /* safety check */
5620 wipe_buffer(newbuf, FALSE);
5621
5622 return differ;
5623}
5624
5625/*
5626 * Wipe out a buffer and decrement the last buffer number if it was used for
5627 * this buffer. Call this to wipe out a temp buffer that does not contain any
5628 * marks.
5629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 void
5631wipe_buffer(buf, aucmd)
5632 buf_T *buf;
Bram Moolenaar0c094b92009-05-14 20:20:33 +00005633 int aucmd UNUSED; /* When TRUE trigger autocommands. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634{
5635 if (buf->b_fnum == top_file_num - 1)
5636 --top_file_num;
5637
5638#ifdef FEAT_AUTOCMD
5639 if (!aucmd) /* Don't trigger BufDelete autocommands here. */
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005640 block_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641#endif
5642 close_buffer(NULL, buf, DOBUF_WIPE);
5643#ifdef FEAT_AUTOCMD
5644 if (!aucmd)
Bram Moolenaar78ab3312007-09-29 12:16:41 +00005645 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646#endif
5647}