blob: e5d2dde2559ecbf1a3fd1ec1f54a9efcbd26450f [file] [log] [blame]
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* buffer.c: functions for dealing with the buffer structure
*/
/*
* The buffer list is a double linked list of all buffers.
* Each buffer can be in one of these states:
* never loaded: BF_NEVERLOADED is set, only the file name is valid
* not loaded: b_ml.ml_mfp == NULL, no memfile allocated
* hidden: b_nwindows == 0, loaded but not displayed in a window
* normal: loaded and displayed in a window
*
* Instead of storing file names all over the place, each file name is
* stored in the buffer list. It can be referenced by a number.
*
* The current implementation remembers all file names ever used.
*/
#include "vim.h"
#if defined(FEAT_CMDL_COMPL) || defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL)
static char_u *buflist_match(regmatch_T *rmp, buf_T *buf, int ignore_case);
# define HAVE_BUFLIST_MATCH
static char_u *fname_match(regmatch_T *rmp, char_u *name, int ignore_case);
#endif
static void buflist_setfpos(buf_T *buf, win_T *win, linenr_T lnum, colnr_T col, int copy_options);
static wininfo_T *find_wininfo(buf_T *buf, int skip_diff_buffer);
#ifdef UNIX
static buf_T *buflist_findname_stat(char_u *ffname, stat_T *st);
static int otherfile_buf(buf_T *buf, char_u *ffname, stat_T *stp);
static int buf_same_ino(buf_T *buf, stat_T *stp);
#else
static int otherfile_buf(buf_T *buf, char_u *ffname);
#endif
#ifdef FEAT_TITLE
static int ti_change(char_u *str, char_u **last);
#endif
static int append_arg_number(win_T *wp, char_u *buf, int buflen, int add_file);
static void free_buffer(buf_T *);
static void free_buffer_stuff(buf_T *buf, int free_options);
static void clear_wininfo(buf_T *buf);
#ifdef UNIX
# define dev_T dev_t
#else
# define dev_T unsigned
#endif
#if defined(FEAT_SIGNS)
static void insert_sign(buf_T *buf, signlist_T *prev, signlist_T *next, int id, linenr_T lnum, int typenr);
#endif
#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
static char *msg_loclist = N_("[Location List]");
static char *msg_qflist = N_("[Quickfix List]");
#endif
#ifdef FEAT_AUTOCMD
static char *e_auabort = N_("E855: Autocommands caused command to abort");
#endif
/* Number of times free_buffer() was called. */
static int buf_free_count = 0;
/* Read data from buffer for retrying. */
static int
read_buffer(
int read_stdin, /* read file from stdin, otherwise fifo */
exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
int flags) /* extra flags for readfile() */
{
int retval = OK;
linenr_T line_count;
/*
* Read from the buffer which the text is already filled in and append at
* the end. This makes it possible to retry when 'fileformat' or
* 'fileencoding' was guessed wrong.
*/
line_count = curbuf->b_ml.ml_line_count;
retval = readfile(
read_stdin ? NULL : curbuf->b_ffname,
read_stdin ? NULL : curbuf->b_fname,
(linenr_T)line_count, (linenr_T)0, (linenr_T)MAXLNUM, eap,
flags | READ_BUFFER);
if (retval == OK)
{
/* Delete the binary lines. */
while (--line_count >= 0)
ml_delete((linenr_T)1, FALSE);
}
else
{
/* Delete the converted lines. */
while (curbuf->b_ml.ml_line_count > line_count)
ml_delete(line_count, FALSE);
}
/* Put the cursor on the first line. */
curwin->w_cursor.lnum = 1;
curwin->w_cursor.col = 0;
if (read_stdin)
{
/* Set or reset 'modified' before executing autocommands, so that
* it can be changed there. */
if (!readonlymode && !bufempty())
changed();
else if (retval != FAIL)
unchanged(curbuf, FALSE);
#ifdef FEAT_AUTOCMD
# ifdef FEAT_EVAL
apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, FALSE,
curbuf, &retval);
# else
apply_autocmds(EVENT_STDINREADPOST, NULL, NULL, FALSE, curbuf);
# endif
#endif
}
return retval;
}
/*
* Open current buffer, that is: open the memfile and read the file into
* memory.
* Return FAIL for failure, OK otherwise.
*/
int
open_buffer(
int read_stdin, /* read file from stdin */
exarg_T *eap, /* for forced 'ff' and 'fenc' or NULL */
int flags) /* extra flags for readfile() */
{
int retval = OK;
#ifdef FEAT_AUTOCMD
bufref_T old_curbuf;
#endif
#ifdef FEAT_SYN_HL
long old_tw = curbuf->b_p_tw;
#endif
int read_fifo = FALSE;
/*
* The 'readonly' flag is only set when BF_NEVERLOADED is being reset.
* When re-entering the same buffer, it should not change, because the
* user may have reset the flag by hand.
*/
if (readonlymode && curbuf->b_ffname != NULL
&& (curbuf->b_flags & BF_NEVERLOADED))
curbuf->b_p_ro = TRUE;
if (ml_open(curbuf) == FAIL)
{
/*
* There MUST be a memfile, otherwise we can't do anything
* If we can't create one for the current buffer, take another buffer
*/
close_buffer(NULL, curbuf, 0, FALSE);
FOR_ALL_BUFFERS(curbuf)
if (curbuf->b_ml.ml_mfp != NULL)
break;
/*
* if there is no memfile at all, exit
* This is OK, since there are no changes to lose.
*/
if (curbuf == NULL)
{
EMSG(_("E82: Cannot allocate any buffer, exiting..."));
getout(2);
}
EMSG(_("E83: Cannot allocate buffer, using other one..."));
enter_buffer(curbuf);
#ifdef FEAT_SYN_HL
if (old_tw != curbuf->b_p_tw)
check_colorcolumn(curwin);
#endif
return FAIL;
}
#ifdef FEAT_AUTOCMD
/* The autocommands in readfile() may change the buffer, but only AFTER
* reading the file. */
set_bufref(&old_curbuf, curbuf);
modified_was_set = FALSE;
#endif
/* mark cursor position as being invalid */
curwin->w_valid = 0;
if (curbuf->b_ffname != NULL
#ifdef FEAT_NETBEANS_INTG
&& netbeansReadFile
#endif
)
{
int old_msg_silent = msg_silent;
#ifdef UNIX
int save_bin = curbuf->b_p_bin;
int perm;
#endif
#ifdef FEAT_NETBEANS_INTG
int oldFire = netbeansFireChanges;
netbeansFireChanges = 0;
#endif
#ifdef UNIX
perm = mch_getperm(curbuf->b_ffname);
if (perm >= 0 && (0
# ifdef S_ISFIFO
|| S_ISFIFO(perm)
# endif
# ifdef S_ISSOCK
|| S_ISSOCK(perm)
# endif
# ifdef OPEN_CHR_FILES
|| (S_ISCHR(perm) && is_dev_fd_file(curbuf->b_ffname))
# endif
))
read_fifo = TRUE;
if (read_fifo)
curbuf->b_p_bin = TRUE;
#endif
if (shortmess(SHM_FILEINFO))
msg_silent = 1;
retval = readfile(curbuf->b_ffname, curbuf->b_fname,
(linenr_T)0, (linenr_T)0, (linenr_T)MAXLNUM, eap,
flags | READ_NEW | (read_fifo ? READ_FIFO : 0));
#ifdef UNIX
if (read_fifo)
{
curbuf->b_p_bin = save_bin;
if (retval == OK)
retval = read_buffer(FALSE, eap, flags);
}
#endif
msg_silent = old_msg_silent;
#ifdef FEAT_NETBEANS_INTG
netbeansFireChanges = oldFire;
#endif
/* Help buffer is filtered. */
if (curbuf->b_help)
fix_help_buffer();
}
else if (read_stdin)
{
int save_bin = curbuf->b_p_bin;
/*
* First read the text in binary mode into the buffer.
* Then read from that same buffer and append at the end. This makes
* it possible to retry when 'fileformat' or 'fileencoding' was
* guessed wrong.
*/
curbuf->b_p_bin = TRUE;
retval = readfile(NULL, NULL, (linenr_T)0,
(linenr_T)0, (linenr_T)MAXLNUM, NULL,
flags | (READ_NEW + READ_STDIN));
curbuf->b_p_bin = save_bin;
if (retval == OK)
retval = read_buffer(TRUE, eap, flags);
}
/* if first time loading this buffer, init b_chartab[] */
if (curbuf->b_flags & BF_NEVERLOADED)
{
(void)buf_init_chartab(curbuf, FALSE);
#ifdef FEAT_CINDENT
parse_cino(curbuf);
#endif
}
/*
* Set/reset the Changed flag first, autocmds may change the buffer.
* Apply the automatic commands, before processing the modelines.
* So the modelines have priority over auto commands.
*/
/* When reading stdin, the buffer contents always needs writing, so set
* the changed flag. Unless in readonly mode: "ls | gview -".
* When interrupted and 'cpoptions' contains 'i' set changed flag. */
if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
#ifdef FEAT_AUTOCMD
|| modified_was_set /* ":set modified" used in autocmd */
# ifdef FEAT_EVAL
|| (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
# endif
#endif
)
changed();
else if (retval != FAIL && !read_stdin && !read_fifo)
unchanged(curbuf, FALSE);
save_file_ff(curbuf); /* keep this fileformat */
/* require "!" to overwrite the file, because it wasn't read completely */
#ifdef FEAT_EVAL
if (aborting())
#else
if (got_int)
#endif
curbuf->b_flags |= BF_READERR;
#ifdef FEAT_FOLDING
/* Need to update automatic folding. Do this before the autocommands,
* they may use the fold info. */
foldUpdateAll(curwin);
#endif
#ifdef FEAT_AUTOCMD
/* need to set w_topline, unless some autocommand already did that. */
if (!(curwin->w_valid & VALID_TOPLINE))
{
curwin->w_topline = 1;
# ifdef FEAT_DIFF
curwin->w_topfill = 0;
# endif
}
# ifdef FEAT_EVAL
apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval);
# else
apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
# endif
#endif
if (retval != FAIL)
{
#ifdef FEAT_AUTOCMD
/*
* The autocommands may have changed the current buffer. Apply the
* modelines to the correct buffer, if it still exists and is loaded.
*/
if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL)
{
aco_save_T aco;
/* Go to the buffer that was opened. */
aucmd_prepbuf(&aco, old_curbuf.br_buf);
#endif
do_modelines(0);
curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED);
#ifdef FEAT_AUTOCMD
# ifdef FEAT_EVAL
apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
&retval);
# else
apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
# endif
/* restore curwin/curbuf and a few other things */
aucmd_restbuf(&aco);
}
#endif
}
return retval;
}
/*
* Store "buf" in "bufref" and set the free count.
*/
void
set_bufref(bufref_T *bufref, buf_T *buf)
{
bufref->br_buf = buf;
bufref->br_buf_free_count = buf_free_count;
}
/*
* Return TRUE if "bufref->br_buf" points to a valid buffer.
* Only goes through the buffer list if buf_free_count changed.
*/
int
bufref_valid(bufref_T *bufref)
{
return bufref->br_buf_free_count == buf_free_count
? TRUE : buf_valid(bufref->br_buf);
}
/*
* Return TRUE if "buf" points to a valid buffer (in the buffer list).
* This can be slow if there are many buffers, prefer using bufref_valid().
*/
int
buf_valid(buf_T *buf)
{
buf_T *bp;
/* Assume that we more often have a recent buffer, start with the last
* one. */
for (bp = lastbuf; bp != NULL; bp = bp->b_prev)
if (bp == buf)
return TRUE;
return FALSE;
}
/*
* A hash table used to quickly lookup a buffer by its number.
*/
static hashtab_T buf_hashtab;
static void
buf_hashtab_add(buf_T *buf)
{
sprintf((char *)buf->b_key, "%x", buf->b_fnum);
if (hash_add(&buf_hashtab, buf->b_key) == FAIL)
EMSG(_("E931: Buffer cannot be registered"));
}
static void
buf_hashtab_remove(buf_T *buf)
{
hashitem_T *hi = hash_find(&buf_hashtab, buf->b_key);
if (!HASHITEM_EMPTY(hi))
hash_remove(&buf_hashtab, hi);
}
/*
* Close the link to a buffer.
* "action" is used when there is no longer a window for the buffer.
* It can be:
* 0 buffer becomes hidden
* DOBUF_UNLOAD buffer is unloaded
* DOBUF_DELETE buffer is unloaded and removed from buffer list
* DOBUF_WIPE buffer is unloaded and really deleted
* When doing all but the first one on the current buffer, the caller should
* get a new buffer very soon!
*
* The 'bufhidden' option can force freeing and deleting.
*
* When "abort_if_last" is TRUE then do not close the buffer if autocommands
* cause there to be only one window with this buffer. e.g. when ":quit" is
* supposed to close the window but autocommands close all other windows.
*/
void
close_buffer(
win_T *win, /* if not NULL, set b_last_cursor */
buf_T *buf,
int action,
int abort_if_last UNUSED)
{
#ifdef FEAT_AUTOCMD
int is_curbuf;
int nwindows;
bufref_T bufref;
# ifdef FEAT_WINDOWS
int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
win_T *the_curwin = curwin;
tabpage_T *the_curtab = curtab;
# endif
#endif
int unload_buf = (action != 0);
int del_buf = (action == DOBUF_DEL || action == DOBUF_WIPE);
int wipe_buf = (action == DOBUF_WIPE);
#ifdef FEAT_QUICKFIX
/*
* Force unloading or deleting when 'bufhidden' says so.
* The caller must take care of NOT deleting/freeing when 'bufhidden' is
* "hide" (otherwise we could never free or delete a buffer).
*/
if (buf->b_p_bh[0] == 'd') /* 'bufhidden' == "delete" */
{
del_buf = TRUE;
unload_buf = TRUE;
}
else if (buf->b_p_bh[0] == 'w') /* 'bufhidden' == "wipe" */
{
del_buf = TRUE;
unload_buf = TRUE;
wipe_buf = TRUE;
}
else if (buf->b_p_bh[0] == 'u') /* 'bufhidden' == "unload" */
unload_buf = TRUE;
#endif
#ifdef FEAT_AUTOCMD
/* Disallow deleting the buffer when it is locked (already being closed or
* halfway a command that relies on it). Unloading is allowed. */
if (buf->b_locked > 0 && (del_buf || wipe_buf))
{
EMSG(_("E937: Attempt to delete a buffer that is in use"));
return;
}
#endif
if (win != NULL
#ifdef FEAT_WINDOWS
&& win_valid_any_tab(win) /* in case autocommands closed the window */
#endif
)
{
/* Set b_last_cursor when closing the last window for the buffer.
* Remember the last cursor position and window options of the buffer.
* This used to be only for the current window, but then options like
* 'foldmethod' may be lost with a ":only" command. */
if (buf->b_nwindows == 1)
set_last_cursor(win);
buflist_setfpos(buf, win,
win->w_cursor.lnum == 1 ? 0 : win->w_cursor.lnum,
win->w_cursor.col, TRUE);
}
#ifdef FEAT_AUTOCMD
set_bufref(&bufref, buf);
/* When the buffer is no longer in a window, trigger BufWinLeave */
if (buf->b_nwindows == 1)
{
++buf->b_locked;
if (apply_autocmds(EVENT_BUFWINLEAVE, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
{
/* Autocommands deleted the buffer. */
aucmd_abort:
EMSG(_(e_auabort));
return;
}
--buf->b_locked;
if (abort_if_last && one_window())
/* Autocommands made this the only window. */
goto aucmd_abort;
/* When the buffer becomes hidden, but is not unloaded, trigger
* BufHidden */
if (!unload_buf)
{
++buf->b_locked;
if (apply_autocmds(EVENT_BUFHIDDEN, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
/* Autocommands deleted the buffer. */
goto aucmd_abort;
--buf->b_locked;
if (abort_if_last && one_window())
/* Autocommands made this the only window. */
goto aucmd_abort;
}
# ifdef FEAT_EVAL
if (aborting()) /* autocmds may abort script processing */
return;
# endif
}
# ifdef FEAT_WINDOWS
/* If the buffer was in curwin and the window has changed, go back to that
* window, if it still exists. This avoids that ":edit x" triggering a
* "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
{
block_autocmds();
goto_tabpage_win(the_curtab, the_curwin);
unblock_autocmds();
}
# endif
nwindows = buf->b_nwindows;
#endif
/* decrease the link count from windows (unless not in any window) */
if (buf->b_nwindows > 0)
--buf->b_nwindows;
/* Return when a window is displaying the buffer or when it's not
* unloaded. */
if (buf->b_nwindows > 0 || !unload_buf)
return;
/* Always remove the buffer when there is no file name. */
if (buf->b_ffname == NULL)
del_buf = TRUE;
/* When closing the current buffer stop Visual mode before freeing
* anything. */
if (buf == curbuf && VIsual_active
#if defined(EXITFREE)
&& !entered_free_all_mem
#endif
)
end_visual_mode();
/*
* Free all things allocated for this buffer.
* Also calls the "BufDelete" autocommands when del_buf is TRUE.
*/
#ifdef FEAT_AUTOCMD
/* Remember if we are closing the current buffer. Restore the number of
* windows, so that autocommands in buf_freeall() don't get confused. */
is_curbuf = (buf == curbuf);
buf->b_nwindows = nwindows;
#endif
buf_freeall(buf, (del_buf ? BFA_DEL : 0) + (wipe_buf ? BFA_WIPE : 0));
#ifdef FEAT_AUTOCMD
/* Autocommands may have deleted the buffer. */
if (!bufref_valid(&bufref))
return;
# ifdef FEAT_EVAL
if (aborting()) /* autocmds may abort script processing */
return;
# endif
/*
* It's possible that autocommands change curbuf to the one being deleted.
* This might cause the previous curbuf to be deleted unexpectedly. But
* in some cases it's OK to delete the curbuf, because a new one is
* obtained anyway. Therefore only return if curbuf changed to the
* deleted buffer.
*/
if (buf == curbuf && !is_curbuf)
return;
if (
#ifdef FEAT_WINDOWS
win_valid_any_tab(win) &&
#else
win != NULL &&
#endif
win->w_buffer == buf)
win->w_buffer = NULL; /* make sure we don't use the buffer now */
/* Autocommands may have opened or closed windows for this buffer.
* Decrement the count for the close we do here. */
if (buf->b_nwindows > 0)
--buf->b_nwindows;
#endif
/* Change directories when the 'acd' option is set. */
DO_AUTOCHDIR
/*
* Remove the buffer from the list.
*/
if (wipe_buf)
{
#ifdef FEAT_SUN_WORKSHOP
if (usingSunWorkShop)
workshop_file_closed_lineno((char *)buf->b_ffname,
(int)buf->b_last_cursor.lnum);
#endif
vim_free(buf->b_ffname);
vim_free(buf->b_sfname);
if (buf->b_prev == NULL)
firstbuf = buf->b_next;
else
buf->b_prev->b_next = buf->b_next;
if (buf->b_next == NULL)
lastbuf = buf->b_prev;
else
buf->b_next->b_prev = buf->b_prev;
free_buffer(buf);
}
else
{
if (del_buf)
{
/* Free all internal variables and reset option values, to make
* ":bdel" compatible with Vim 5.7. */
free_buffer_stuff(buf, TRUE);
/* Make it look like a new buffer. */
buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
/* Init the options when loaded again. */
buf->b_p_initialized = FALSE;
}
buf_clear_file(buf);
if (del_buf)
buf->b_p_bl = FALSE;
}
}
/*
* Make buffer not contain a file.
*/
void
buf_clear_file(buf_T *buf)
{
buf->b_ml.ml_line_count = 1;
unchanged(buf, TRUE);
buf->b_shortname = FALSE;
buf->b_p_eol = TRUE;
buf->b_start_eol = TRUE;
#ifdef FEAT_MBYTE
buf->b_p_bomb = FALSE;
buf->b_start_bomb = FALSE;
#endif
buf->b_ml.ml_mfp = NULL;
buf->b_ml.ml_flags = ML_EMPTY; /* empty buffer */
#ifdef FEAT_NETBEANS_INTG
netbeans_deleted_all_lines(buf);
#endif
}
/*
* buf_freeall() - free all things allocated for a buffer that are related to
* the file. Careful: get here with "curwin" NULL when exiting.
* flags:
* BFA_DEL buffer is going to be deleted
* BFA_WIPE buffer is going to be wiped out
* BFA_KEEP_UNDO do not free undo information
*/
void
buf_freeall(buf_T *buf, int flags)
{
#ifdef FEAT_AUTOCMD
int is_curbuf = (buf == curbuf);
bufref_T bufref;
# ifdef FEAT_WINDOWS
int is_curwin = (curwin != NULL && curwin->w_buffer == buf);
win_T *the_curwin = curwin;
tabpage_T *the_curtab = curtab;
# endif
/* Make sure the buffer isn't closed by autocommands. */
++buf->b_locked;
set_bufref(&bufref, buf);
if (buf->b_ml.ml_mfp != NULL)
{
if (apply_autocmds(EVENT_BUFUNLOAD, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
/* autocommands deleted the buffer */
return;
}
if ((flags & BFA_DEL) && buf->b_p_bl)
{
if (apply_autocmds(EVENT_BUFDELETE, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
/* autocommands deleted the buffer */
return;
}
if (flags & BFA_WIPE)
{
if (apply_autocmds(EVENT_BUFWIPEOUT, buf->b_fname, buf->b_fname,
FALSE, buf)
&& !bufref_valid(&bufref))
/* autocommands deleted the buffer */
return;
}
--buf->b_locked;
# ifdef FEAT_WINDOWS
/* If the buffer was in curwin and the window has changed, go back to that
* window, if it still exists. This avoids that ":edit x" triggering a
* "tabnext" BufUnload autocmd leaves a window behind without a buffer. */
if (is_curwin && curwin != the_curwin && win_valid_any_tab(the_curwin))
{
block_autocmds();
goto_tabpage_win(the_curtab, the_curwin);
unblock_autocmds();
}
# endif
# ifdef FEAT_EVAL
if (aborting()) /* autocmds may abort script processing */
return;
# endif
/*
* It's possible that autocommands change curbuf to the one being deleted.
* This might cause curbuf to be deleted unexpectedly. But in some cases
* it's OK to delete the curbuf, because a new one is obtained anyway.
* Therefore only return if curbuf changed to the deleted buffer.
*/
if (buf == curbuf && !is_curbuf)
return;
#endif
#ifdef FEAT_DIFF
diff_buf_delete(buf); /* Can't use 'diff' for unloaded buffer. */
#endif
#ifdef FEAT_SYN_HL
/* Remove any ownsyntax, unless exiting. */
if (curwin != NULL && curwin->w_buffer == buf)
reset_synblock(curwin);
#endif
#ifdef FEAT_FOLDING
/* No folds in an empty buffer. */
# ifdef FEAT_WINDOWS
{
win_T *win;
tabpage_T *tp;
FOR_ALL_TAB_WINDOWS(tp, win)
if (win->w_buffer == buf)
clearFolding(win);
}
# else
if (curwin != NULL && curwin->w_buffer == buf)
clearFolding(curwin);
# endif
#endif
#ifdef FEAT_TCL
tcl_buffer_free(buf);
#endif
ml_close(buf, TRUE); /* close and delete the memline/memfile */
buf->b_ml.ml_line_count = 0; /* no lines in buffer */
if ((flags & BFA_KEEP_UNDO) == 0)
{
u_blockfree(buf); /* free the memory allocated for undo */
u_clearall(buf); /* reset all undo information */
}
#ifdef FEAT_SYN_HL
syntax_clear(&buf->b_s); /* reset syntax info */
#endif
buf->b_flags &= ~BF_READERR; /* a read error is no longer relevant */
}
/*
* Free a buffer structure and the things it contains related to the buffer
* itself (not the file, that must have been done already).
*/
static void
free_buffer(buf_T *buf)
{
++buf_free_count;
free_buffer_stuff(buf, TRUE);
#ifdef FEAT_EVAL
unref_var_dict(buf->b_vars);
#endif
#ifdef FEAT_LUA
lua_buffer_free(buf);
#endif
#ifdef FEAT_MZSCHEME
mzscheme_buffer_free(buf);
#endif
#ifdef FEAT_PERL
perl_buf_free(buf);
#endif
#ifdef FEAT_PYTHON
python_buffer_free(buf);
#endif
#ifdef FEAT_PYTHON3
python3_buffer_free(buf);
#endif
#ifdef FEAT_RUBY
ruby_buffer_free(buf);
#endif
#ifdef FEAT_JOB_CHANNEL
channel_buffer_free(buf);
#endif
buf_hashtab_remove(buf);
#ifdef FEAT_AUTOCMD
aubuflocal_remove(buf);
if (autocmd_busy)
{
/* Do not free the buffer structure while autocommands are executing,
* it's still needed. Free it when autocmd_busy is reset. */
buf->b_next = au_pending_free_buf;
au_pending_free_buf = buf;
}
else
#endif
vim_free(buf);
}
/*
* Free stuff in the buffer for ":bdel" and when wiping out the buffer.
*/
static void
free_buffer_stuff(
buf_T *buf,
int free_options) /* free options as well */
{
if (free_options)
{
clear_wininfo(buf); /* including window-local options */
free_buf_options(buf, TRUE);
#ifdef FEAT_SPELL
ga_clear(&buf->b_s.b_langp);
#endif
}
#ifdef FEAT_EVAL
vars_clear(&buf->b_vars->dv_hashtab); /* free all internal variables */
hash_init(&buf->b_vars->dv_hashtab);
#endif
#ifdef FEAT_USR_CMDS
uc_clear(&buf->b_ucmds); /* clear local user commands */
#endif
#ifdef FEAT_SIGNS
buf_delete_signs(buf); /* delete any signs */
#endif
#ifdef FEAT_NETBEANS_INTG
netbeans_file_killed(buf);
#endif
#ifdef FEAT_LOCALMAP
map_clear_int(buf, MAP_ALL_MODES, TRUE, FALSE); /* clear local mappings */
map_clear_int(buf, MAP_ALL_MODES, TRUE, TRUE); /* clear local abbrevs */
#endif
#ifdef FEAT_MBYTE
vim_free(buf->b_start_fenc);
buf->b_start_fenc = NULL;
#endif
}
/*
* Free the b_wininfo list for buffer "buf".
*/
static void
clear_wininfo(buf_T *buf)
{
wininfo_T *wip;
while (buf->b_wininfo != NULL)
{
wip = buf->b_wininfo;
buf->b_wininfo = wip->wi_next;
if (wip->wi_optset)
{
clear_winopt(&wip->wi_opt);
#ifdef FEAT_FOLDING
deleteFoldRecurse(&wip->wi_folds);
#endif
}
vim_free(wip);
}
}
#if defined(FEAT_LISTCMDS) || defined(PROTO)
/*
* Go to another buffer. Handles the result of the ATTENTION dialog.
*/
void
goto_buffer(
exarg_T *eap,
int start,
int dir,
int count)
{
# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
bufref_T old_curbuf;
set_bufref(&old_curbuf, curbuf);
swap_exists_action = SEA_DIALOG;
# endif
(void)do_buffer(*eap->cmd == 's' ? DOBUF_SPLIT : DOBUF_GOTO,
start, dir, count, eap->forceit);
# if defined(FEAT_WINDOWS) && defined(HAS_SWAP_EXISTS_ACTION)
if (swap_exists_action == SEA_QUIT && *eap->cmd == 's')
{
# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
cleanup_T cs;
/* Reset the error/interrupt/exception state here so that
* aborting() returns FALSE when closing a window. */
enter_cleanup(&cs);
# endif
/* Quitting means closing the split window, nothing else. */
win_close(curwin, TRUE);
swap_exists_action = SEA_NONE;
swap_exists_did_quit = TRUE;
# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
/* Restore the error/interrupt/exception state if not discarded by a
* new aborting error, interrupt, or uncaught exception. */
leave_cleanup(&cs);
# endif
}
else
handle_swap_exists(&old_curbuf);
# endif
}
#endif
#if defined(HAS_SWAP_EXISTS_ACTION) || defined(PROTO)
/*
* Handle the situation of swap_exists_action being set.
* It is allowed for "old_curbuf" to be NULL or invalid.
*/
void
handle_swap_exists(bufref_T *old_curbuf)
{
# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
cleanup_T cs;
# endif
#ifdef FEAT_SYN_HL
long old_tw = curbuf->b_p_tw;
#endif
buf_T *buf;
if (swap_exists_action == SEA_QUIT)
{
# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
/* Reset the error/interrupt/exception state here so that
* aborting() returns FALSE when closing a buffer. */
enter_cleanup(&cs);
# endif
/* User selected Quit at ATTENTION prompt. Go back to previous
* buffer. If that buffer is gone or the same as the current one,
* open a new, empty buffer. */
swap_exists_action = SEA_NONE; /* don't want it again */
swap_exists_did_quit = TRUE;
close_buffer(curwin, curbuf, DOBUF_UNLOAD, FALSE);
if (old_curbuf == NULL || !bufref_valid(old_curbuf)
|| old_curbuf->br_buf == curbuf)
buf = buflist_new(NULL, NULL, 1L, BLN_CURBUF | BLN_LISTED);
else
buf = old_curbuf->br_buf;
if (buf != NULL)
{
enter_buffer(buf);
#ifdef FEAT_SYN_HL
if (old_tw != curbuf->b_p_tw)
check_colorcolumn(curwin);
#endif
}
/* If "old_curbuf" is NULL we are in big trouble here... */
# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
/* Restore the error/interrupt/exception state if not discarded by a
* new aborting error, interrupt, or uncaught exception. */
leave_cleanup(&cs);
# endif
}
else if (swap_exists_action == SEA_RECOVER)
{
# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
/* Reset the error/interrupt/exception state here so that
* aborting() returns FALSE when closing a buffer. */
enter_cleanup(&cs);
# endif
/* User selected Recover at ATTENTION prompt. */
msg_scroll = TRUE;
ml_recover();
MSG_PUTS("\n"); /* don't overwrite the last message */
cmdline_row = msg_row;
do_modelines(0);
# if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
/* Restore the error/interrupt/exception state if not discarded by a
* new aborting error, interrupt, or uncaught exception. */
leave_cleanup(&cs);
# endif
}
swap_exists_action = SEA_NONE;
}
#endif
#if defined(FEAT_LISTCMDS) || defined(PROTO)
/*
* do_bufdel() - delete or unload buffer(s)
*
* addr_count == 0: ":bdel" - delete current buffer
* addr_count == 1: ":N bdel" or ":bdel N [N ..]" - first delete
* buffer "end_bnr", then any other arguments.
* addr_count == 2: ":N,N bdel" - delete buffers in range
*
* command can be DOBUF_UNLOAD (":bunload"), DOBUF_WIPE (":bwipeout") or
* DOBUF_DEL (":bdel")
*
* Returns error message or NULL
*/
char_u *
do_bufdel(
int command,
char_u *arg, /* pointer to extra arguments */
int addr_count,
int start_bnr, /* first buffer number in a range */
int end_bnr, /* buffer nr or last buffer nr in a range */
int forceit)
{
int do_current = 0; /* delete current buffer? */
int deleted = 0; /* number of buffers deleted */
char_u *errormsg = NULL; /* return value */
int bnr; /* buffer number */
char_u *p;
if (addr_count == 0)
{
(void)do_buffer(command, DOBUF_CURRENT, FORWARD, 0, forceit);
}
else
{
if (addr_count == 2)
{
if (*arg) /* both range and argument is not allowed */
return (char_u *)_(e_trailing);
bnr = start_bnr;
}
else /* addr_count == 1 */
bnr = end_bnr;
for ( ;!got_int; ui_breakcheck())
{
/*
* delete the current buffer last, otherwise when the
* current buffer is deleted, the next buffer becomes
* the current one and will be loaded, which may then
* also be deleted, etc.
*/
if (bnr == curbuf->b_fnum)
do_current = bnr;
else if (do_buffer(command, DOBUF_FIRST, FORWARD, (int)bnr,
forceit) == OK)
++deleted;
/*
* find next buffer number to delete/unload
*/
if (addr_count == 2)
{
if (++bnr > end_bnr)
break;
}
else /* addr_count == 1 */
{
arg = skipwhite(arg);
if (*arg == NUL)
break;
if (!VIM_ISDIGIT(*arg))
{
p = skiptowhite_esc(arg);
bnr = buflist_findpat(arg, p, command == DOBUF_WIPE,
FALSE, FALSE);
if (bnr < 0) /* failed */
break;
arg = p;
}
else
bnr = getdigits(&arg);
}
}
if (!got_int && do_current && do_buffer(command, DOBUF_FIRST,
FORWARD, do_current, forceit) == OK)
++deleted;
if (deleted == 0)
{
if (command == DOBUF_UNLOAD)
STRCPY(IObuff, _("E515: No buffers were unloaded"));
else if (command == DOBUF_DEL)
STRCPY(IObuff, _("E516: No buffers were deleted"));
else
STRCPY(IObuff, _("E517: No buffers were wiped out"));
errormsg = IObuff;
}
else if (deleted >= p_report)
{
if (command == DOBUF_UNLOAD)
{
if (deleted == 1)
MSG(_("1 buffer unloaded"));
else
smsg((char_u *)_("%d buffers unloaded"), deleted);
}
else if (command == DOBUF_DEL)
{
if (deleted == 1)
MSG(_("1 buffer deleted"));
else
smsg((char_u *)_("%d buffers deleted"), deleted);
}
else
{
if (deleted == 1)
MSG(_("1 buffer wiped out"));
else
smsg((char_u *)_("%d buffers wiped out"), deleted);
}
}
}
return errormsg;
}
#endif /* FEAT_LISTCMDS */
#if defined(FEAT_LISTCMDS) || defined(FEAT_PYTHON) \
|| defined(FEAT_PYTHON3) || defined(PROTO)
static int empty_curbuf(int close_others, int forceit, int action);
/*
* Make the current buffer empty.
* Used when it is wiped out and it's the last buffer.
*/
static int
empty_curbuf(
int close_others,
int forceit,
int action)
{
int retval;
buf_T *buf = curbuf;
bufref_T bufref;
if (action == DOBUF_UNLOAD)
{
EMSG(_("E90: Cannot unload last buffer"));
return FAIL;
}
set_bufref(&bufref, buf);
#ifdef FEAT_WINDOWS
if (close_others)
/* Close any other windows on this buffer, then make it empty. */
close_windows(buf, TRUE);
#endif
setpcmark();
retval = do_ecmd(0, NULL, NULL, NULL, ECMD_ONE,
forceit ? ECMD_FORCEIT : 0, curwin);
/*
* do_ecmd() may create a new buffer, then we have to delete
* the old one. But do_ecmd() may have done that already, check
* if the buffer still exists.
*/
if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows == 0)
close_buffer(NULL, buf, action, FALSE);
if (!close_others)
need_fileinfo = FALSE;
return retval;
}
/*
* Implementation of the commands for the buffer list.
*
* action == DOBUF_GOTO go to specified buffer
* action == DOBUF_SPLIT split window and go to specified buffer
* action == DOBUF_UNLOAD unload specified buffer(s)
* action == DOBUF_DEL delete specified buffer(s) from buffer list
* action == DOBUF_WIPE delete specified buffer(s) really
*
* start == DOBUF_CURRENT go to "count" buffer from current buffer
* start == DOBUF_FIRST go to "count" buffer from first buffer
* start == DOBUF_LAST go to "count" buffer from last buffer
* start == DOBUF_MOD go to "count" modified buffer from current buffer
*
* Return FAIL or OK.
*/
int
do_buffer(
int action,
int start,
int dir, /* FORWARD or BACKWARD */
int count, /* buffer number or number of buffers */
int forceit) /* TRUE for :...! */
{
buf_T *buf;
buf_T *bp;
int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
|| action == DOBUF_WIPE);
switch (start)
{
case DOBUF_FIRST: buf = firstbuf; break;
case DOBUF_LAST: buf = lastbuf; break;
default: buf = curbuf; break;
}
if (start == DOBUF_MOD) /* find next modified buffer */
{
while (count-- > 0)
{
do
{
buf = buf->b_next;
if (buf == NULL)
buf = firstbuf;
}
while (buf != curbuf && !bufIsChanged(buf));
}
if (!bufIsChanged(buf))
{
EMSG(_("E84: No modified buffer found"));
return FAIL;
}
}
else if (start == DOBUF_FIRST && count) /* find specified buffer number */
{
while (buf != NULL && buf->b_fnum != count)
buf = buf->b_next;
}
else
{
bp = NULL;
while (count > 0 || (!unload && !buf->b_p_bl && bp != buf))
{
/* remember the buffer where we start, we come back there when all
* buffers are unlisted. */
if (bp == NULL)
bp = buf;
if (dir == FORWARD)
{
buf = buf->b_next;
if (buf == NULL)
buf = firstbuf;
}
else
{
buf = buf->b_prev;
if (buf == NULL)
buf = lastbuf;
}
/* don't count unlisted buffers */
if (unload || buf->b_p_bl)
{
--count;
bp = NULL; /* use this buffer as new starting point */
}
if (bp == buf)
{
/* back where we started, didn't find anything. */
EMSG(_("E85: There is no listed buffer"));
return FAIL;
}
}
}
if (buf == NULL) /* could not find it */
{
if (start == DOBUF_FIRST)
{
/* don't warn when deleting */
if (!unload)
EMSGN(_(e_nobufnr), count);
}
else if (dir == FORWARD)
EMSG(_("E87: Cannot go beyond last buffer"));
else
EMSG(_("E88: Cannot go before first buffer"));
return FAIL;
}
#ifdef FEAT_GUI
need_mouse_correct = TRUE;
#endif
#ifdef FEAT_LISTCMDS
/*
* delete buffer buf from memory and/or the list
*/
if (unload)
{
int forward;
# if defined(FEAT_AUTOCMD) || defined(FEAT_WINDOWS)
bufref_T bufref;
set_bufref(&bufref, buf);
# endif
/* When unloading or deleting a buffer that's already unloaded and
* unlisted: fail silently. */
if (action != DOBUF_WIPE && buf->b_ml.ml_mfp == NULL && !buf->b_p_bl)
return FAIL;
if (!forceit && bufIsChanged(buf))
{
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if ((p_confirm || cmdmod.confirm) && p_write)
{
dialog_changed(buf, FALSE);
# ifdef FEAT_AUTOCMD
if (!bufref_valid(&bufref))
/* Autocommand deleted buffer, oops! It's not changed
* now. */
return FAIL;
# endif
/* If it's still changed fail silently, the dialog already
* mentioned why it fails. */
if (bufIsChanged(buf))
return FAIL;
}
else
#endif
{
EMSGN(_("E89: No write since last change for buffer %ld (add ! to override)"),
buf->b_fnum);
return FAIL;
}
}
/* When closing the current buffer stop Visual mode. */
if (buf == curbuf && VIsual_active)
end_visual_mode();
/*
* If deleting the last (listed) buffer, make it empty.
* The last (listed) buffer cannot be unloaded.
*/
FOR_ALL_BUFFERS(bp)
if (bp->b_p_bl && bp != buf)
break;
if (bp == NULL && buf == curbuf)
return empty_curbuf(TRUE, forceit, action);
#ifdef FEAT_WINDOWS
/*
* If the deleted buffer is the current one, close the current window
* (unless it's the only window). Repeat this so long as we end up in
* a window with this buffer.
*/
while (buf == curbuf
# ifdef FEAT_AUTOCMD
&& !(curwin->w_closing || curwin->w_buffer->b_locked > 0)
# endif
&& (!ONE_WINDOW || first_tabpage->tp_next != NULL))
{
if (win_close(curwin, FALSE) == FAIL)
break;
}
#endif
/*
* If the buffer to be deleted is not the current one, delete it here.
*/
if (buf != curbuf)
{
#ifdef FEAT_WINDOWS
close_windows(buf, FALSE);
if (buf != curbuf && bufref_valid(&bufref))
#endif
if (buf->b_nwindows <= 0)
close_buffer(NULL, buf, action, FALSE);
return OK;
}
/*
* Deleting the current buffer: Need to find another buffer to go to.
* There should be another, otherwise it would have been handled
* above. However, autocommands may have deleted all buffers.
* First use au_new_curbuf.br_buf, if it is valid.
* Then prefer the buffer we most recently visited.
* Else try to find one that is loaded, after the current buffer,
* then before the current buffer.
* Finally use any buffer.
*/
buf = NULL; /* selected buffer */
bp = NULL; /* used when no loaded buffer found */
#ifdef FEAT_AUTOCMD
if (au_new_curbuf.br_buf != NULL && bufref_valid(&au_new_curbuf))
buf = au_new_curbuf.br_buf;
# ifdef FEAT_JUMPLIST
else
# endif
#endif
#ifdef FEAT_JUMPLIST
if (curwin->w_jumplistlen > 0)
{
int jumpidx;
jumpidx = curwin->w_jumplistidx - 1;
if (jumpidx < 0)
jumpidx = curwin->w_jumplistlen - 1;
forward = jumpidx;
while (jumpidx != curwin->w_jumplistidx)
{
buf = buflist_findnr(curwin->w_jumplist[jumpidx].fmark.fnum);
if (buf != NULL)
{
if (buf == curbuf || !buf->b_p_bl)
buf = NULL; /* skip current and unlisted bufs */
else if (buf->b_ml.ml_mfp == NULL)
{
/* skip unloaded buf, but may keep it for later */
if (bp == NULL)
bp = buf;
buf = NULL;
}
}
if (buf != NULL) /* found a valid buffer: stop searching */
break;
/* advance to older entry in jump list */
if (!jumpidx && curwin->w_jumplistidx == curwin->w_jumplistlen)
break;
if (--jumpidx < 0)
jumpidx = curwin->w_jumplistlen - 1;
if (jumpidx == forward) /* List exhausted for sure */
break;
}
}
#endif
if (buf == NULL) /* No previous buffer, Try 2'nd approach */
{
forward = TRUE;
buf = curbuf->b_next;
for (;;)
{
if (buf == NULL)
{
if (!forward) /* tried both directions */
break;
buf = curbuf->b_prev;
forward = FALSE;
continue;
}
/* in non-help buffer, try to skip help buffers, and vv */
if (buf->b_help == curbuf->b_help && buf->b_p_bl)
{
if (buf->b_ml.ml_mfp != NULL) /* found loaded buffer */
break;
if (bp == NULL) /* remember unloaded buf for later */
bp = buf;
}
if (forward)
buf = buf->b_next;
else
buf = buf->b_prev;
}
}
if (buf == NULL) /* No loaded buffer, use unloaded one */
buf = bp;
if (buf == NULL) /* No loaded buffer, find listed one */
{
FOR_ALL_BUFFERS(buf)
if (buf->b_p_bl && buf != curbuf)
break;
}
if (buf == NULL) /* Still no buffer, just take one */
{
if (curbuf->b_next != NULL)
buf = curbuf->b_next;
else
buf = curbuf->b_prev;
}
}
if (buf == NULL)
{
/* Autocommands must have wiped out all other buffers. Only option
* now is to make the current buffer empty. */
return empty_curbuf(FALSE, forceit, action);
}
/*
* make buf current buffer
*/
if (action == DOBUF_SPLIT) /* split window first */
{
# ifdef FEAT_WINDOWS
/* If 'switchbuf' contains "useopen": jump to first window containing
* "buf" if one exists */
if ((swb_flags & SWB_USEOPEN) && buf_jump_open_win(buf))
return OK;
/* If 'switchbuf' contains "usetab": jump to first window in any tab
* page containing "buf" if one exists */
if ((swb_flags & SWB_USETAB) && buf_jump_open_tab(buf))
return OK;
if (win_split(0, 0) == FAIL)
# endif
return FAIL;
}
#endif
/* go to current buffer - nothing to do */
if (buf == curbuf)
return OK;
/*
* Check if the current buffer may be abandoned.
*/
if (action == DOBUF_GOTO && !can_abandon(curbuf, forceit))
{
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if ((p_confirm || cmdmod.confirm) && p_write)
{
# ifdef FEAT_AUTOCMD
bufref_T bufref;
set_bufref(&bufref, buf);
# endif
dialog_changed(curbuf, FALSE);
# ifdef FEAT_AUTOCMD
if (!bufref_valid(&bufref))
/* Autocommand deleted buffer, oops! */
return FAIL;
# endif
}
if (bufIsChanged(curbuf))
#endif
{
EMSG(_(e_nowrtmsg));
return FAIL;
}
}
/* Go to the other buffer. */
set_curbuf(buf, action);
#if defined(FEAT_LISTCMDS) \
&& (defined(FEAT_SCROLLBIND) || defined(FEAT_CURSORBIND))
if (action == DOBUF_SPLIT)
{
RESET_BINDING(curwin); /* reset 'scrollbind' and 'cursorbind' */
}
#endif
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
if (aborting()) /* autocmds may abort script processing */
return FAIL;
#endif
return OK;
}
#endif
/*
* Set current buffer to "buf". Executes autocommands and closes current
* buffer. "action" tells how to close the current buffer:
* DOBUF_GOTO free or hide it
* DOBUF_SPLIT nothing
* DOBUF_UNLOAD unload it
* DOBUF_DEL delete it
* DOBUF_WIPE wipe it out
*/
void
set_curbuf(buf_T *buf, int action)
{
buf_T *prevbuf;
int unload = (action == DOBUF_UNLOAD || action == DOBUF_DEL
|| action == DOBUF_WIPE);
#ifdef FEAT_SYN_HL
long old_tw = curbuf->b_p_tw;
#endif
bufref_T bufref;
setpcmark();
if (!cmdmod.keepalt)
curwin->w_alt_fnum = curbuf->b_fnum; /* remember alternate file */
buflist_altfpos(curwin); /* remember curpos */
/* Don't restart Select mode after switching to another buffer. */
VIsual_reselect = FALSE;
/* close_windows() or apply_autocmds() may change curbuf */
prevbuf = curbuf;
set_bufref(&bufref, prevbuf);
#ifdef FEAT_AUTOCMD
if (!apply_autocmds(EVENT_BUFLEAVE, NULL, NULL, FALSE, curbuf)
# ifdef FEAT_EVAL
|| (bufref_valid(&bufref) && !aborting())
# else
|| bufref_valid(&bufref)
# endif
)
#endif
{
#ifdef FEAT_SYN_HL
if (prevbuf == curwin->w_buffer)
reset_synblock(curwin);
#endif
#ifdef FEAT_WINDOWS
if (unload)
close_windows(prevbuf, FALSE);
#endif
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
if (bufref_valid(&bufref) && !aborting())
#else
if (bufref_valid(&bufref))
#endif
{
#ifdef FEAT_WINDOWS
win_T *previouswin = curwin;
#endif
if (prevbuf == curbuf)
u_sync(FALSE);
close_buffer(prevbuf == curwin->w_buffer ? curwin : NULL, prevbuf,
unload ? action : (action == DOBUF_GOTO
&& !P_HID(prevbuf)
&& !bufIsChanged(prevbuf)) ? DOBUF_UNLOAD : 0, FALSE);
#ifdef FEAT_WINDOWS
if (curwin != previouswin && win_valid(previouswin))
/* autocommands changed curwin, Grr! */
curwin = previouswin;
#endif
}
}
#ifdef FEAT_AUTOCMD
/* An autocommand may have deleted "buf", already entered it (e.g., when
* it did ":bunload") or aborted the script processing.
* If curwin->w_buffer is null, enter_buffer() will make it valid again */
if ((buf_valid(buf) && buf != curbuf
# ifdef FEAT_EVAL
&& !aborting()
# endif
# ifdef FEAT_WINDOWS
) || curwin->w_buffer == NULL
# endif
)
#endif
{
enter_buffer(buf);
#ifdef FEAT_SYN_HL
if (old_tw != curbuf->b_p_tw)
check_colorcolumn(curwin);
#endif
}
}
/*
* Enter a new current buffer.
* Old curbuf must have been abandoned already! This also means "curbuf" may
* be pointing to freed memory.
*/
void
enter_buffer(buf_T *buf)
{
/* Copy buffer and window local option values. Not for a help buffer. */
buf_copy_options(buf, BCO_ENTER | BCO_NOHELP);
if (!buf->b_help)
get_winopts(buf);
#ifdef FEAT_FOLDING
else
/* Remove all folds in the window. */
clearFolding(curwin);
foldUpdateAll(curwin); /* update folds (later). */
#endif
/* Get the buffer in the current window. */
curwin->w_buffer = buf;
curbuf = buf;
++curbuf->b_nwindows;
#ifdef FEAT_DIFF
if (curwin->w_p_diff)
diff_buf_add(curbuf);
#endif
#ifdef FEAT_SYN_HL
curwin->w_s = &(buf->b_s);
#endif
/* Cursor on first line by default. */
curwin->w_cursor.lnum = 1;
curwin->w_cursor.col = 0;
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
curwin->w_set_curswant = TRUE;
#ifdef FEAT_AUTOCMD
curwin->w_topline_was_set = FALSE;
#endif
/* mark cursor position as being invalid */
curwin->w_valid = 0;
/* Make sure the buffer is loaded. */
if (curbuf->b_ml.ml_mfp == NULL) /* need to load the file */
{
#ifdef FEAT_AUTOCMD
/* If there is no filetype, allow for detecting one. Esp. useful for
* ":ball" used in a autocommand. If there already is a filetype we
* might prefer to keep it. */
if (*curbuf->b_p_ft == NUL)
did_filetype = FALSE;
#endif
open_buffer(FALSE, NULL, 0);
}
else
{
if (!msg_silent)
need_fileinfo = TRUE; /* display file info after redraw */
(void)buf_check_timestamp(curbuf, FALSE); /* check if file changed */
#ifdef FEAT_AUTOCMD
curwin->w_topline = 1;
# ifdef FEAT_DIFF
curwin->w_topfill = 0;
# endif
apply_autocmds(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf);
apply_autocmds(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf);
#endif
}
/* If autocommands did not change the cursor position, restore cursor lnum
* and possibly cursor col. */
if (curwin->w_cursor.lnum == 1 && inindent(0))
buflist_getfpos();
check_arg_idx(curwin); /* check for valid arg_idx */
#ifdef FEAT_TITLE
maketitle();
#endif
#ifdef FEAT_AUTOCMD
/* when autocmds didn't change it */
if (curwin->w_topline == 1 && !curwin->w_topline_was_set)
#endif
scroll_cursor_halfway(FALSE); /* redisplay at correct position */
#ifdef FEAT_NETBEANS_INTG
/* Send fileOpened event because we've changed buffers. */
netbeans_file_activated(curbuf);
#endif
/* Change directories when the 'acd' option is set. */
DO_AUTOCHDIR
#ifdef FEAT_KEYMAP
if (curbuf->b_kmap_state & KEYMAP_INIT)
(void)keymap_init();
#endif
#ifdef FEAT_SPELL
/* May need to set the spell language. Can only do this after the buffer
* has been properly setup. */
if (!curbuf->b_help && curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
(void)did_set_spelllang(curwin);
#endif
#ifdef FEAT_VIMINFO
curbuf->b_last_used = vim_time();
#endif
redraw_later(NOT_VALID);
}
#if defined(FEAT_AUTOCHDIR) || defined(PROTO)
/*
* Change to the directory of the current buffer.
* Don't do this while still starting up.
*/
void
do_autochdir(void)
{
if ((starting == 0 || test_autochdir)
&& curbuf->b_ffname != NULL
&& vim_chdirfile(curbuf->b_ffname) == OK)
shorten_fnames(TRUE);
}
#endif
/*
* functions for dealing with the buffer list
*/
static int top_file_num = 1; /* highest file number */
/*
* Add a file name to the buffer list. Return a pointer to the buffer.
* If the same file name already exists return a pointer to that buffer.
* If it does not exist, or if fname == NULL, a new entry is created.
* If (flags & BLN_CURBUF) is TRUE, may use current buffer.
* If (flags & BLN_LISTED) is TRUE, add new buffer to buffer list.
* If (flags & BLN_DUMMY) is TRUE, don't count it as a real buffer.
* If (flags & BLN_NEW) is TRUE, don't use an existing buffer.
* If (flags & BLN_NOOPT) is TRUE, don't copy options from the current buffer
* if the buffer already exists.
* This is the ONLY way to create a new buffer.
*/
buf_T *
buflist_new(
char_u *ffname, /* full path of fname or relative */
char_u *sfname, /* short fname or NULL */
linenr_T lnum, /* preferred cursor line */
int flags) /* BLN_ defines */
{
buf_T *buf;
#ifdef UNIX
stat_T st;
#endif
if (top_file_num == 1)
hash_init(&buf_hashtab);
fname_expand(curbuf, &ffname, &sfname); /* will allocate ffname */
/*
* If file name already exists in the list, update the entry.
*/
#ifdef UNIX
/* On Unix we can use inode numbers when the file exists. Works better
* for hard links. */
if (sfname == NULL || mch_stat((char *)sfname, &st) < 0)
st.st_dev = (dev_T)-1;
#endif
if (ffname != NULL && !(flags & (BLN_DUMMY | BLN_NEW)) && (buf =
#ifdef UNIX
buflist_findname_stat(ffname, &st)
#else
buflist_findname(ffname)
#endif
) != NULL)
{
vim_free(ffname);
if (lnum != 0)
buflist_setfpos(buf, curwin, lnum, (colnr_T)0, FALSE);
if ((flags & BLN_NOOPT) == 0)
/* copy the options now, if 'cpo' doesn't have 's' and not done
* already */
buf_copy_options(buf, 0);
if ((flags & BLN_LISTED) && !buf->b_p_bl)
{
#ifdef FEAT_AUTOCMD
bufref_T bufref;
#endif
buf->b_p_bl = TRUE;
#ifdef FEAT_AUTOCMD
set_bufref(&bufref, buf);
if (!(flags & BLN_DUMMY))
{
if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
&& !bufref_valid(&bufref))
return NULL;
}
#endif
}
return buf;
}
/*
* If the current buffer has no name and no contents, use the current
* buffer. Otherwise: Need to allocate a new buffer structure.
*
* This is the ONLY place where a new buffer structure is allocated!
* (A spell file buffer is allocated in spell.c, but that's not a normal
* buffer.)
*/
buf = NULL;
if ((flags & BLN_CURBUF)
&& curbuf != NULL
&& curbuf->b_ffname == NULL
&& curbuf->b_nwindows <= 1
&& (curbuf->b_ml.ml_mfp == NULL || bufempty()))
{
buf = curbuf;
#ifdef FEAT_AUTOCMD
/* It's like this buffer is deleted. Watch out for autocommands that
* change curbuf! If that happens, allocate a new buffer anyway. */
if (curbuf->b_p_bl)
apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
if (buf == curbuf)
apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
# ifdef FEAT_EVAL
if (aborting()) /* autocmds may abort script processing */
return NULL;
# endif
#endif
#ifdef FEAT_QUICKFIX
# ifdef FEAT_AUTOCMD
if (buf == curbuf)
# endif
{
/* Make sure 'bufhidden' and 'buftype' are empty */
clear_string_option(&buf->b_p_bh);
clear_string_option(&buf->b_p_bt);
}
#endif
}
if (buf != curbuf || curbuf == NULL)
{
buf = (buf_T *)alloc_clear((unsigned)sizeof(buf_T));
if (buf == NULL)
{
vim_free(ffname);
return NULL;
}
#ifdef FEAT_EVAL
/* init b: variables */
buf->b_vars = dict_alloc();
if (buf->b_vars == NULL)
{
vim_free(ffname);
vim_free(buf);
return NULL;
}
init_var_dict(buf->b_vars, &buf->b_bufvar, VAR_SCOPE);
#endif
}
if (ffname != NULL)
{
buf->b_ffname = ffname;
buf->b_sfname = vim_strsave(sfname);
}
clear_wininfo(buf);
buf->b_wininfo = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
if ((ffname != NULL && (buf->b_ffname == NULL || buf->b_sfname == NULL))
|| buf->b_wininfo == NULL)
{
vim_free(buf->b_ffname);
buf->b_ffname = NULL;
vim_free(buf->b_sfname);
buf->b_sfname = NULL;
if (buf != curbuf)
free_buffer(buf);
return NULL;
}
if (buf == curbuf)
{
/* free all things allocated for this buffer */
buf_freeall(buf, 0);
if (buf != curbuf) /* autocommands deleted the buffer! */
return NULL;
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
if (aborting()) /* autocmds may abort script processing */
return NULL;
#endif
free_buffer_stuff(buf, FALSE); /* delete local variables et al. */
/* Init the options. */
buf->b_p_initialized = FALSE;
buf_copy_options(buf, BCO_ENTER);
#ifdef FEAT_KEYMAP
/* need to reload lmaps and set b:keymap_name */
curbuf->b_kmap_state |= KEYMAP_INIT;
#endif
}
else
{
/*
* put new buffer at the end of the buffer list
*/
buf->b_next = NULL;
if (firstbuf == NULL) /* buffer list is empty */
{
buf->b_prev = NULL;
firstbuf = buf;
}
else /* append new buffer at end of list */
{
lastbuf->b_next = buf;
buf->b_prev = lastbuf;
}
lastbuf = buf;
buf->b_fnum = top_file_num++;
if (top_file_num < 0) /* wrap around (may cause duplicates) */
{
EMSG(_("W14: Warning: List of file names overflow"));
if (emsg_silent == 0)
{
out_flush();
ui_delay(3000L, TRUE); /* make sure it is noticed */
}
top_file_num = 1;
}
buf_hashtab_add(buf);
/*
* Always copy the options from the current buffer.
*/
buf_copy_options(buf, BCO_ALWAYS);
}
buf->b_wininfo->wi_fpos.lnum = lnum;
buf->b_wininfo->wi_win = curwin;
#ifdef FEAT_SYN_HL
hash_init(&buf->b_s.b_keywtab);
hash_init(&buf->b_s.b_keywtab_ic);
#endif
buf->b_fname = buf->b_sfname;
#ifdef UNIX
if (st.st_dev == (dev_T)-1)
buf->b_dev_valid = FALSE;
else
{
buf->b_dev_valid = TRUE;
buf->b_dev = st.st_dev;
buf->b_ino = st.st_ino;
}
#endif
buf->b_u_synced = TRUE;
buf->b_flags = BF_CHECK_RO | BF_NEVERLOADED;
if (flags & BLN_DUMMY)
buf->b_flags |= BF_DUMMY;
buf_clear_file(buf);
clrallmarks(buf); /* clear marks */
fmarks_check_names(buf); /* check file marks for this file */
buf->b_p_bl = (flags & BLN_LISTED) ? TRUE : FALSE; /* init 'buflisted' */
#ifdef FEAT_AUTOCMD
if (!(flags & BLN_DUMMY))
{
bufref_T bufref;
/* Tricky: these autocommands may change the buffer list. They could
* also split the window with re-using the one empty buffer. This may
* result in unexpectedly losing the empty buffer. */
set_bufref(&bufref, buf);
if (apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, buf)
&& !bufref_valid(&bufref))
return NULL;
if (flags & BLN_LISTED)
{
if (apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, buf)
&& !bufref_valid(&bufref))
return NULL;
}
# ifdef FEAT_EVAL
if (aborting()) /* autocmds may abort script processing */
return NULL;
# endif
}
#endif
return buf;
}
/*
* Free the memory for the options of a buffer.
* If "free_p_ff" is TRUE also free 'fileformat', 'buftype' and
* 'fileencoding'.
*/
void
free_buf_options(
buf_T *buf,
int free_p_ff)
{
if (free_p_ff)
{
#ifdef FEAT_MBYTE
clear_string_option(&buf->b_p_fenc);
#endif
clear_string_option(&buf->b_p_ff);
#ifdef FEAT_QUICKFIX
clear_string_option(&buf->b_p_bh);
clear_string_option(&buf->b_p_bt);
#endif
}
#ifdef FEAT_FIND_ID
clear_string_option(&buf->b_p_def);
clear_string_option(&buf->b_p_inc);
# ifdef FEAT_EVAL
clear_string_option(&buf->b_p_inex);
# endif
#endif
#if defined(FEAT_CINDENT) && defined(FEAT_EVAL)
clear_string_option(&buf->b_p_inde);
clear_string_option(&buf->b_p_indk);
#endif
#if defined(FEAT_BEVAL) && defined(FEAT_EVAL)
clear_string_option(&buf->b_p_bexpr);
#endif
#if defined(FEAT_CRYPT)
clear_string_option(&buf->b_p_cm);
#endif
#if defined(FEAT_EVAL)
clear_string_option(&buf->b_p_fex);
#endif
#ifdef FEAT_CRYPT
clear_string_option(&buf->b_p_key);
#endif
clear_string_option(&buf->b_p_kp);
clear_string_option(&buf->b_p_mps);
clear_string_option(&buf->b_p_fo);
clear_string_option(&buf->b_p_flp);
clear_string_option(&buf->b_p_isk);
#ifdef FEAT_KEYMAP
clear_string_option(&buf->b_p_keymap);
ga_clear(&buf->b_kmap_ga);
#endif
#ifdef FEAT_COMMENTS
clear_string_option(&buf->b_p_com);
#endif
#ifdef FEAT_FOLDING
clear_string_option(&buf->b_p_cms);
#endif
clear_string_option(&buf->b_p_nf);
#ifdef FEAT_SYN_HL
clear_string_option(&buf->b_p_syn);
clear_string_option(&buf->b_s.b_syn_isk);
#endif
#ifdef FEAT_SPELL
clear_string_option(&buf->b_s.b_p_spc);
clear_string_option(&buf->b_s.b_p_spf);
vim_regfree(buf->b_s.b_cap_prog);
buf->b_s.b_cap_prog = NULL;
clear_string_option(&buf->b_s.b_p_spl);
#endif
#ifdef FEAT_SEARCHPATH
clear_string_option(&buf->b_p_sua);
#endif
#ifdef FEAT_AUTOCMD
clear_string_option(&buf->b_p_ft);
#endif
#ifdef FEAT_CINDENT
clear_string_option(&buf->b_p_cink);
clear_string_option(&buf->b_p_cino);
#endif
#if defined(FEAT_CINDENT) || defined(FEAT_SMARTINDENT)
clear_string_option(&buf->b_p_cinw);
#endif
#ifdef FEAT_INS_EXPAND
clear_string_option(&buf->b_p_cpt);
#endif
#ifdef FEAT_COMPL_FUNC
clear_string_option(&buf->b_p_cfu);
clear_string_option(&buf->b_p_ofu);
#endif
#ifdef FEAT_QUICKFIX
clear_string_option(&buf->b_p_gp);
clear_string_option(&buf->b_p_mp);
clear_string_option(&buf->b_p_efm);
#endif
clear_string_option(&buf->b_p_ep);
clear_string_option(&buf->b_p_path);
clear_string_option(&buf->b_p_tags);
clear_string_option(&buf->b_p_tc);
#ifdef FEAT_INS_EXPAND
clear_string_option(&buf->b_p_dict);
clear_string_option(&buf->b_p_tsr);
#endif
#ifdef FEAT_TEXTOBJ
clear_string_option(&buf->b_p_qe);
#endif
buf->b_p_ar = -1;
buf->b_p_ul = NO_LOCAL_UNDOLEVEL;
#ifdef FEAT_LISP
clear_string_option(&buf->b_p_lw);
#endif
clear_string_option(&buf->b_p_bkc);
}
/*
* get alternate file n
* set linenr to lnum or altfpos.lnum if lnum == 0
* also set cursor column to altfpos.col if 'startofline' is not set.
* if (options & GETF_SETMARK) call setpcmark()
* if (options & GETF_ALT) we are jumping to an alternate file.
* if (options & GETF_SWITCH) respect 'switchbuf' settings when jumping
*
* return FAIL for failure, OK for success
*/
int
buflist_getfile(
int n,
linenr_T lnum,
int options,
int forceit)
{
buf_T *buf;
#ifdef FEAT_WINDOWS
win_T *wp = NULL;
#endif
pos_T *fpos;
colnr_T col;
buf = buflist_findnr(n);
if (buf == NULL)
{
if ((options & GETF_ALT) && n == 0)
EMSG(_(e_noalt));
else
EMSGN(_("E92: Buffer %ld not found"), n);
return FAIL;
}
/* if alternate file is the current buffer, nothing to do */
if (buf == curbuf)
return OK;
if (text_locked())
{
text_locked_msg();
return FAIL;
}
#ifdef FEAT_AUTOCMD
if (curbuf_locked())
return FAIL;
#endif
/* altfpos may be changed by getfile(), get it now */
if (lnum == 0)
{
fpos = buflist_findfpos(buf);
lnum = fpos->lnum;
col = fpos->col;
}
else
col = 0;
#ifdef FEAT_WINDOWS
if (options & GETF_SWITCH)
{
/* If 'switchbuf' contains "useopen": jump to first window containing
* "buf" if one exists */
if (swb_flags & SWB_USEOPEN)
wp = buf_jump_open_win(buf);
/* If 'switchbuf' contains "usetab": jump to first window in any tab
* page containing "buf" if one exists */
if (wp == NULL && (swb_flags & SWB_USETAB))
wp = buf_jump_open_tab(buf);
/* If 'switchbuf' contains "split", "vsplit" or "newtab" and the
* current buffer isn't empty: open new tab or window */
if (wp == NULL && (swb_flags & (SWB_VSPLIT | SWB_SPLIT | SWB_NEWTAB))
&& !bufempty())
{
if (swb_flags & SWB_NEWTAB)
tabpage_new();
else if (win_split(0, (swb_flags & SWB_VSPLIT) ? WSP_VERT : 0)
== FAIL)
return FAIL;
RESET_BINDING(curwin);
}
}
#endif
++RedrawingDisabled;
if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
lnum, forceit) <= 0)
{
--RedrawingDisabled;
/* cursor is at to BOL and w_cursor.lnum is checked due to getfile() */
if (!p_sol && col != 0)
{
curwin->w_cursor.col = col;
check_cursor_col();
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
curwin->w_set_curswant = TRUE;
}
return OK;
}
--RedrawingDisabled;
return FAIL;
}
/*
* go to the last know line number for the current buffer
*/
void
buflist_getfpos(void)
{
pos_T *fpos;
fpos = buflist_findfpos(curbuf);
curwin->w_cursor.lnum = fpos->lnum;
check_cursor_lnum();
if (p_sol)
curwin->w_cursor.col = 0;
else
{
curwin->w_cursor.col = fpos->col;
check_cursor_col();
#ifdef FEAT_VIRTUALEDIT
curwin->w_cursor.coladd = 0;
#endif
curwin->w_set_curswant = TRUE;
}
}
#if defined(FEAT_QUICKFIX) || defined(FEAT_EVAL) || defined(PROTO)
/*
* Find file in buffer list by name (it has to be for the current window).
* Returns NULL if not found.
*/
buf_T *
buflist_findname_exp(char_u *fname)
{
char_u *ffname;
buf_T *buf = NULL;
/* First make the name into a full path name */
ffname = FullName_save(fname,
#ifdef UNIX
TRUE /* force expansion, get rid of symbolic links */
#else
FALSE
#endif
);
if (ffname != NULL)
{
buf = buflist_findname(ffname);
vim_free(ffname);
}
return buf;
}
#endif
/*
* Find file in buffer list by name (it has to be for the current window).
* "ffname" must have a full path.
* Skips dummy buffers.
* Returns NULL if not found.
*/
buf_T *
buflist_findname(char_u *ffname)
{
#ifdef UNIX
stat_T st;
if (mch_stat((char *)ffname, &st) < 0)
st.st_dev = (dev_T)-1;
return buflist_findname_stat(ffname, &st);
}
/*
* Same as buflist_findname(), but pass the stat structure to avoid getting it
* twice for the same file.
* Returns NULL if not found.
*/
static buf_T *
buflist_findname_stat(
char_u *ffname,
stat_T *stp)
{
#endif
buf_T *buf;
/* Start at the last buffer, expect to find a match sooner. */
for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
if ((buf->b_flags & BF_DUMMY) == 0 && !otherfile_buf(buf, ffname
#ifdef UNIX
, stp
#endif
))
return buf;
return NULL;
}
#if defined(FEAT_LISTCMDS) || defined(FEAT_EVAL) || defined(FEAT_PERL) \
|| defined(PROTO)
/*
* Find file in buffer list by a regexp pattern.
* Return fnum of the found buffer.
* Return < 0 for error.
*/
int
buflist_findpat(
char_u *pattern,
char_u *pattern_end, /* pointer to first char after pattern */
int unlisted, /* find unlisted buffers */
int diffmode UNUSED, /* find diff-mode buffers only */
int curtab_only) /* find buffers in current tab only */
{
buf_T *buf;
int match = -1;
int find_listed;
char_u *pat;
char_u *patend;
int attempt;
char_u *p;
int toggledollar;
if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#'))
{
if (*pattern == '%')
match = curbuf->b_fnum;
else
match = curwin->w_alt_fnum;
#ifdef FEAT_DIFF
if (diffmode && !diff_mode_buf(buflist_findnr(match)))
match = -1;
#endif
}
/*
* Try four ways of matching a listed buffer:
* attempt == 0: without '^' or '$' (at any position)
* attempt == 1: with '^' at start (only at position 0)
* attempt == 2: with '$' at end (only match at end)
* attempt == 3: with '^' at start and '$' at end (only full match)
* Repeat this for finding an unlisted buffer if there was no matching
* listed buffer.
*/
else
{
pat = file_pat_to_reg_pat(pattern, pattern_end, NULL, FALSE);
if (pat == NULL)
return -1;
patend = pat + STRLEN(pat) - 1;
toggledollar = (patend > pat && *patend == '$');
/* First try finding a listed buffer. If not found and "unlisted"
* is TRUE, try finding an unlisted buffer. */
find_listed = TRUE;
for (;;)
{
for (attempt = 0; attempt <= 3; ++attempt)
{
regmatch_T regmatch;
/* may add '^' and '$' */
if (toggledollar)
*patend = (attempt < 2) ? NUL : '$'; /* add/remove '$' */
p = pat;
if (*p == '^' && !(attempt & 1)) /* add/remove '^' */
++p;
regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
if (regmatch.regprog == NULL)
{
vim_free(pat);
return -1;
}
for (buf = lastbuf; buf != NULL; buf = buf->b_prev)
if (buf->b_p_bl == find_listed
#ifdef FEAT_DIFF
&& (!diffmode || diff_mode_buf(buf))
#endif
&& buflist_match(&regmatch, buf, FALSE) != NULL)
{
if (curtab_only)
{
/* Ignore the match if the buffer is not open in
* the current tab. */
#ifdef FEAT_WINDOWS
win_T *wp;
FOR_ALL_WINDOWS(wp)
if (wp->w_buffer == buf)
break;
if (wp == NULL)
continue;
#else
if (curwin->w_buffer != buf)
continue;
#endif
}
if (match >= 0) /* already found a match */
{
match = -2;
break;
}
match = buf->b_fnum; /* remember first match */
}
vim_regfree(regmatch.regprog);
if (match >= 0) /* found one match */
break;
}
/* Only search for unlisted buffers if there was no match with
* a listed buffer. */
if (!unlisted || !find_listed || match != -1)
break;
find_listed = FALSE;
}
vim_free(pat);
}
if (match == -2)
EMSG2(_("E93: More than one match for %s"), pattern);
else if (match < 0)
EMSG2(_("E94: No matching buffer for %s"), pattern);
return match;
}
#endif
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
/*
* Find all buffer names that match.
* For command line expansion of ":buf" and ":sbuf".
* Return OK if matches found, FAIL otherwise.
*/
int
ExpandBufnames(
char_u *pat,
int *num_file,
char_u ***file,
int options)
{
int count = 0;
buf_T *buf;
int round;
char_u *p;
int attempt;
char_u *patc;
*num_file = 0; /* return values in case of FAIL */
*file = NULL;
/* Make a copy of "pat" and change "^" to "\(^\|[\/]\)". */
if (*pat == '^')
{
patc = alloc((unsigned)STRLEN(pat) + 11);
if (patc == NULL)
return FAIL;
STRCPY(patc, "\\(^\\|[\\/]\\)");
STRCPY(patc + 11, pat + 1);
}
else
patc = pat;
/*
* attempt == 0: try match with '\<', match at start of word
* attempt == 1: try match without '\<', match anywhere
*/
for (attempt = 0; attempt <= 1; ++attempt)
{
regmatch_T regmatch;
if (attempt > 0 && patc == pat)
break; /* there was no anchor, no need to try again */
regmatch.regprog = vim_regcomp(patc + attempt * 11, RE_MAGIC);
if (regmatch.regprog == NULL)
{
if (patc != pat)
vim_free(patc);
return FAIL;
}
/*
* round == 1: Count the matches.
* round == 2: Build the array to keep the matches.
*/
for (round = 1; round <= 2; ++round)
{
count = 0;
FOR_ALL_BUFFERS(buf)
{
if (!buf->b_p_bl) /* skip unlisted buffers */
continue;
p = buflist_match(&regmatch, buf, p_wic);
if (p != NULL)
{
if (round == 1)
++count;
else
{
if (options & WILD_HOME_REPLACE)
p = home_replace_save(buf, p);
else
p = vim_strsave(p);
(*file)[count++] = p;
}
}
}
if (count == 0) /* no match found, break here */
break;
if (round == 1)
{
*file = (char_u **)alloc((unsigned)(count * sizeof(char_u *)));
if (*file == NULL)
{
vim_regfree(regmatch.regprog);
if (patc != pat)
vim_free(patc);
return FAIL;
}
}
}
vim_regfree(regmatch.regprog);
if (count) /* match(es) found, break here */
break;
}
if (patc != pat)
vim_free(patc);
*num_file = count;
return (count == 0 ? FAIL : OK);
}
#endif /* FEAT_CMDL_COMPL */
#ifdef HAVE_BUFLIST_MATCH
/*
* Check for a match on the file name for buffer "buf" with regprog "prog".
*/
static char_u *
buflist_match(
regmatch_T *rmp,
buf_T *buf,
int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
{
char_u *match;
/* First try the short file name, then the long file name. */
match = fname_match(rmp, buf->b_sfname, ignore_case);
if (match == NULL)
match = fname_match(rmp, buf->b_ffname, ignore_case);
return match;
}
/*
* Try matching the regexp in "prog" with file name "name".
* Return "name" when there is a match, NULL when not.
*/
static char_u *
fname_match(
regmatch_T *rmp,
char_u *name,
int ignore_case) /* when TRUE ignore case, when FALSE use 'fic' */
{
char_u *match = NULL;
char_u *p;
if (name != NULL)
{
/* Ignore case when 'fileignorecase' or the argument is set. */
rmp->rm_ic = p_fic || ignore_case;
if (vim_regexec(rmp, name, (colnr_T)0))
match = name;
else
{
/* Replace $(HOME) with '~' and try matching again. */
p = home_replace_save(NULL, name);
if (p != NULL && vim_regexec(rmp, p, (colnr_T)0))
match = name;
vim_free(p);
}
}
return match;
}
#endif
/*
* Find a file in the buffer list by buffer number.
*/
buf_T *
buflist_findnr(int nr)
{
char_u key[VIM_SIZEOF_INT * 2 + 1];
hashitem_T *hi;
if (nr == 0)
nr = curwin->w_alt_fnum;
sprintf((char *)key, "%x", nr);
hi = hash_find(&buf_hashtab, key);
if (!HASHITEM_EMPTY(hi))
return (buf_T *)(hi->hi_key
- ((unsigned)(curbuf->b_key - (char_u *)curbuf)));
return NULL;
}
/*
* Get name of file 'n' in the buffer list.
* When the file has no name an empty string is returned.
* home_replace() is used to shorten the file name (used for marks).
* Returns a pointer to allocated memory, of NULL when failed.
*/
char_u *
buflist_nr2name(
int n,
int fullname,
int helptail) /* for help buffers return tail only */
{
buf_T *buf;
buf = buflist_findnr(n);
if (buf == NULL)
return NULL;
return home_replace_save(helptail ? buf : NULL,
fullname ? buf->b_ffname : buf->b_fname);
}
/*
* Set the "lnum" and "col" for the buffer "buf" and the current window.
* When "copy_options" is TRUE save the local window option values.
* When "lnum" is 0 only do the options.
*/
static void
buflist_setfpos(
buf_T *buf,
win_T *win,
linenr_T lnum,
colnr_T col,
int copy_options)
{
wininfo_T *wip;
for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
if (wip->wi_win == win)
break;
if (wip == NULL)
{
/* allocate a new entry */
wip = (wininfo_T *)alloc_clear((unsigned)sizeof(wininfo_T));
if (wip == NULL)
return;
wip->wi_win = win;
if (lnum == 0) /* set lnum even when it's 0 */
lnum = 1;
}
else
{
/* remove the entry from the list */
if (wip->wi_prev)
wip->wi_prev->wi_next = wip->wi_next;
else
buf->b_wininfo = wip->wi_next;
if (wip->wi_next)
wip->wi_next->wi_prev = wip->wi_prev;
if (copy_options && wip->wi_optset)
{
clear_winopt(&wip->wi_opt);
#ifdef FEAT_FOLDING
deleteFoldRecurse(&wip->wi_folds);
#endif
}
}
if (lnum != 0)
{
wip->wi_fpos.lnum = lnum;
wip->wi_fpos.col = col;
}
if (copy_options)
{
/* Save the window-specific option values. */
copy_winopt(&win->w_onebuf_opt, &wip->wi_opt);
#ifdef FEAT_FOLDING
wip->wi_fold_manual = win->w_fold_manual;
cloneFoldGrowArray(&win->w_folds, &wip->wi_folds);
#endif
wip->wi_optset = TRUE;
}
/* insert the entry in front of the list */
wip->wi_next = buf->b_wininfo;
buf->b_wininfo = wip;
wip->wi_prev = NULL;
if (wip->wi_next)
wip->wi_next->wi_prev = wip;
return;
}
#ifdef FEAT_DIFF
static int wininfo_other_tab_diff(wininfo_T *wip);
/*
* Return TRUE when "wip" has 'diff' set and the diff is only for another tab
* page. That's because a diff is local to a tab page.
*/
static int
wininfo_other_tab_diff(wininfo_T *wip)
{
win_T *wp;
if (wip->wi_opt.wo_diff)
{
FOR_ALL_WINDOWS(wp)
/* return FALSE when it's a window in the current tab page, thus
* the buffer was in diff mode here */
if (wip->wi_win == wp)
return FALSE;
return TRUE;
}
return FALSE;
}
#endif
/*
* Find info for the current window in buffer "buf".
* If not found, return the info for the most recently used window.
* When "skip_diff_buffer" is TRUE avoid windows with 'diff' set that is in
* another tab page.
* Returns NULL when there isn't any info.
*/
static wininfo_T *
find_wininfo(
buf_T *buf,
int skip_diff_buffer UNUSED)
{
wininfo_T *wip;
for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
if (wip->wi_win == curwin
#ifdef FEAT_DIFF
&& (!skip_diff_buffer || !wininfo_other_tab_diff(wip))
#endif
)
break;
/* If no wininfo for curwin, use the first in the list (that doesn't have
* 'diff' set and is in another tab page). */
if (wip == NULL)
{
#ifdef FEAT_DIFF
if (skip_diff_buffer)
{
for (wip = buf->b_wininfo; wip != NULL; wip = wip->wi_next)
if (!wininfo_other_tab_diff(wip))
break;
}
else
#endif
wip = buf->b_wininfo;
}
return wip;
}
/*
* Reset the local window options to the values last used in this window.
* If the buffer wasn't used in this window before, use the values from
* the most recently used window. If the values were never set, use the
* global values for the window.
*/
void
get_winopts(buf_T *buf)
{
wininfo_T *wip;
clear_winopt(&curwin->w_onebuf_opt);
#ifdef FEAT_FOLDING
clearFolding(curwin);
#endif
wip = find_wininfo(buf, TRUE);
if (wip != NULL && wip->wi_optset)
{
copy_winopt(&wip->wi_opt, &curwin->w_onebuf_opt);
#ifdef FEAT_FOLDING
curwin->w_fold_manual = wip->wi_fold_manual;
curwin->w_foldinvalid = TRUE;
cloneFoldGrowArray(&wip->wi_folds, &curwin->w_folds);
#endif
}
else
copy_winopt(&curwin->w_allbuf_opt, &curwin->w_onebuf_opt);
#ifdef FEAT_FOLDING
/* Set 'foldlevel' to 'foldlevelstart' if it's not negative. */
if (p_fdls >= 0)
curwin->w_p_fdl = p_fdls;
#endif
#ifdef FEAT_SYN_HL
check_colorcolumn(curwin);
#endif
}
/*
* Find the position (lnum and col) for the buffer 'buf' for the current
* window.
* Returns a pointer to no_position if no position is found.
*/
pos_T *
buflist_findfpos(buf_T *buf)
{
wininfo_T *wip;
static pos_T no_position = INIT_POS_T(1, 0, 0);
wip = find_wininfo(buf, FALSE);
if (wip != NULL)
return &(wip->wi_fpos);
else
return &no_position;
}
/*
* Find the lnum for the buffer 'buf' for the current window.
*/
linenr_T
buflist_findlnum(buf_T *buf)
{
return buflist_findfpos(buf)->lnum;
}
#if defined(FEAT_LISTCMDS) || defined(PROTO)
/*
* List all know file names (for :files and :buffers command).
*/
void
buflist_list(exarg_T *eap)
{
buf_T *buf;
int len;
int i;
for (buf = firstbuf; buf != NULL && !got_int; buf = buf->b_next)
{
/* skip unlisted buffers, unless ! was used */
if ((!buf->b_p_bl && !eap->forceit && !vim_strchr(eap->arg, 'u'))
|| (vim_strchr(eap->arg, 'u') && buf->b_p_bl)