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