/* 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.
*/
/*
* evalbuffer.c: Buffer related builtin functions
*/
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
/*
* Mark references in functions of buffers.
*/
int
set_ref_in_buffers(int copyID)
{
int abort = FALSE;
buf_T *bp;
FOR_ALL_BUFFERS(bp)
{
listener_T *lnr;
for (lnr = bp->b_listener; !abort && lnr != NULL; lnr = lnr->lr_next)
abort = abort || set_ref_in_callback(&lnr->lr_callback, copyID);
# ifdef FEAT_JOB_CHANNEL
if (!abort)
abort = abort || set_ref_in_callback(&bp->b_prompt_callback, copyID);
if (!abort)
abort = abort || set_ref_in_callback(&bp->b_prompt_interrupt, copyID);
# endif
#ifdef FEAT_COMPL_FUNC
if (!abort)
abort = abort || set_ref_in_callback(&bp->b_cfu_cb, copyID);
if (!abort)
abort = abort || set_ref_in_callback(&bp->b_ofu_cb, copyID);
if (!abort)
abort = abort || set_ref_in_callback(&bp->b_tsrfu_cb, copyID);
#endif
if (!abort)
abort = abort || set_ref_in_callback(&bp->b_tfu_cb, copyID);
if (abort)
break;
}
return abort;
}
buf_T *
buflist_find_by_name(char_u *name, int curtab_only)
{
int save_magic;
char_u *save_cpo;
buf_T *buf;
// Ignore 'magic' and 'cpoptions' here to make scripts portable
save_magic = p_magic;
p_magic = TRUE;
save_cpo = p_cpo;
p_cpo = empty_option;
buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
TRUE, FALSE, curtab_only));
p_magic = save_magic;
p_cpo = save_cpo;
return buf;
}
/*
* Find a buffer by number or exact name.
*/
buf_T *
find_buffer(typval_T *avar)
{
buf_T *buf = NULL;
if (avar->v_type == VAR_NUMBER)
buf = buflist_findnr((int)avar->vval.v_number);
else if (in_vim9script() && check_for_string_arg(avar, 0) == FAIL)
return NULL;
else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
{
buf = buflist_findname_exp(avar->vval.v_string);
if (buf == NULL)
{
// No full path name match, try a match with a URL or a "nofile"
// buffer, these don't use the full path.
FOR_ALL_BUFFERS(buf)
if (buf->b_fname != NULL
&& (path_with_url(buf->b_fname) || bt_nofilename(buf))
&& STRCMP(buf->b_fname, avar->vval.v_string) == 0)
break;
}
}
return buf;
}
/*
* If there is a window for "curbuf", make it the current window.
*/
static void
find_win_for_curbuf(void)
{
wininfo_T *wip;
// The b_wininfo list should have the windows that recently contained the
// buffer, going over this is faster than going over all the windows.
// Do check the buffer is still there.
FOR_ALL_BUF_WININFO(curbuf, wip)
{
if (wip->wi_win != NULL && wip->wi_win->w_buffer == curbuf)
{
curwin = wip->wi_win;
break;
}
}
}
typedef struct {
win_T *cob_curwin_save;
aco_save_T cob_aco;
int cob_using_aco;
int cob_save_VIsual_active;
} cob_T;
/*
* Used before making a change in "buf", which is not the current one: Make
* "buf" the current buffer and find a window for this buffer, so that side
* effects are done correctly (e.g., adjusting marks).
*
* Information is saved in "cob" and MUST be restored by calling
* change_other_buffer_restore().
*
* If this fails then "curbuf" will not be equal to "buf".
*/
static void
change_other_buffer_prepare(cob_T *cob, buf_T *buf)
{
CLEAR_POINTER(cob);
// Set "curbuf" to the buffer being changed. Then make sure there is a
// window for it to handle any side effects.
cob->cob_save_VIsual_active = VIsual_active;
VIsual_active = FALSE;
cob->cob_curwin_save = curwin;
curbuf = buf;
find_win_for_curbuf(); // simplest: find existing window for "buf"
if (curwin->w_buffer !=