Newer
Older
#endif
/*
* If 'autowrite' option set, try to write the file.
* Careful: autocommands may make "buf" invalid!
*
* return FAIL for failure, OK otherwise
*/
int
if (!(p_aw || p_awa) || !p_write
#ifdef FEAT_QUICKFIX
/* never autowrite a "nofile" or "nowrite" buffer */
|| bt_dontwrite(buf)
|| (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
r = buf_write_all(buf, forceit);
/* Writing may succeed but the buffer still changed, e.g., when there is a
* conversion error. We do want to return FAIL then. */
}
/*
* flush all buffers, except the ones that are readonly
*/
void
{
buf_T *buf;
if (!(p_aw || p_awa) || !p_write)
return;
if (bufIsChanged(buf) && !buf->b_p_ro)
{
/* an autocommand may have deleted the buffer */
* Return TRUE if buffer was changed and cannot be abandoned.
* For flags use the CCGD_ values.
int forceit = (flags & CCGD_FORCEIT);
bufref_T bufref;
set_bufref(&bufref, buf);
&& ((flags & CCGD_MULTWIN) || buf->b_nwindows <= 1)
&& (!(flags & CCGD_AW) || autowrite(buf, forceit) == FAIL))
{
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
if ((p_confirm || cmdmod.confirm) && p_write)
{
buf_T *buf2;
int count = 0;
if (bufIsChanged(buf2)
&& (buf2->b_ffname != NULL
# ifdef FEAT_BROWSE
|| cmdmod.browse
# endif
))
++count;
/* Autocommand deleted buffer, oops! It's not changed now. */
return FALSE;
/* Autocommand deleted buffer, oops! It's not changed now. */
return FALSE;
return bufIsChanged(buf);
}
#endif
no_write_message();
no_write_message_nobang(curbuf);
return TRUE;
}
return FALSE;
}
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG) || defined(PROTO)
#if defined(FEAT_BROWSE) || defined(PROTO)
/*
* When wanting to write a file without a file name, ask the user for a name.
*/
void
{
if (buf->b_fname == NULL)
{
char_u *fname;
fname = do_browse(BROWSE_SAVE, (char_u *)_("Save As"),
NULL, NULL, NULL, NULL, buf);
if (fname != NULL)
{
if (setfname(buf, fname, NULL, TRUE) == OK)
buf->b_flags |= BF_NOTEDITED;
vim_free(fname);
}
}
}
#endif
/*
* Ask the user what to do when abandoning a changed buffer.
* Must check 'write' option first!
*/
void
dialog_changed(
buf_T *buf,
int checkall) /* may abandon all changed buffers */
dialog_msg(buff, _("Save changes to \"%s\"?"), buf->b_fname);
if (checkall)
ret = vim_dialog_yesnoallcancel(VIM_QUESTION, NULL, buff, 1);
else
ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
/* Init ea pseudo-structure, this is needed for the check_overwrite()
* function. */
ea.append = ea.forceit = FALSE;
if (ret == VIM_YES)
{
#ifdef FEAT_BROWSE
/* May get file name, when there is none */
browse_save_fname(buf);
#endif
if (buf->b_fname != NULL && check_overwrite(&ea, buf,
buf->b_fname, buf->b_ffname, FALSE) == OK)
/* didn't hit Cancel */
(void)buf_write_all(buf, FALSE);
}
else if (ret == VIM_NO)
{
unchanged(buf, TRUE);
}
else if (ret == VIM_ALL)
{
/*
* Write all modified files that can be written.
* Skip readonly buffers, these need to be confirmed
* individually.
*/
{
if (bufIsChanged(buf2)
&& (buf2->b_ffname != NULL
#ifdef FEAT_BROWSE
|| cmdmod.browse
#endif
)
&& !buf2->b_p_ro)
{
#ifdef FEAT_BROWSE
/* May get file name, when there is none */
browse_save_fname(buf2);
#endif
if (buf2->b_fname != NULL && check_overwrite(&ea, buf2,
buf2->b_fname, buf2->b_ffname, FALSE) == OK)
/* didn't hit Cancel */
/* an autocommand may have deleted the buffer */
buf2 = firstbuf;
}
}
}
else if (ret == VIM_DISCARDALL)
{
/*
* mark all buffers as unchanged
*/
unchanged(buf2, TRUE);
}
}
#endif
/*
* Return TRUE if the buffer "buf" can be abandoned, either by making it
* hidden, autowriting it or unloading it.
*/
int
return ( buf_hide(buf)
|| !bufIsChanged(buf)
|| buf->b_nwindows > 1
|| autowrite(buf, forceit) == OK
|| forceit);
}
static void add_bufnum(int *bufnrs, int *bufnump, int nr);
/*
* Add a buffer number to "bufnrs", unless it's already there.
*/
static void
{
int i;
for (i = 0; i < *bufnump; ++i)
if (bufnrs[i] == nr)
return;
bufnrs[*bufnump] = nr;
*bufnump = *bufnump + 1;
}
/*
* Return TRUE if any buffer was changed and cannot be abandoned.
* That changed buffer becomes the current buffer.
* When "unload" is TRUE the current buffer is unloaded instead of making it
check_changed_any(
int hidden, /* Only check hidden buffers */
int unload)
int i;
int bufnum = 0;
int bufcount = 0;
int *bufnrs;
tabpage_T *tp;
/* Make a list of all buffers, with the most important ones first. */
++bufcount;
if (bufcount == 0)
return FALSE;
bufnrs = (int *)alloc(sizeof(int) * bufcount);
if (bufnrs == NULL)
return FALSE;
/* curbuf */
bufnrs[bufnum++] = curbuf->b_fnum;
/* buffers in current tab */
FOR_ALL_WINDOWS(wp)
if (wp->w_buffer != curbuf)
add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
/* buffers in other tabs */
if (tp != curtab)
for (wp = tp->tp_firstwin; wp != NULL; wp = wp->w_next)
add_bufnum(bufnrs, &bufnum, wp->w_buffer->b_fnum);
/* any other buffer */
add_bufnum(bufnrs, &bufnum, buf->b_fnum);
for (i = 0; i < bufnum; ++i)
{
buf = buflist_findnr(bufnrs[i]);
if (buf == NULL)
continue;
if ((!hidden || buf->b_nwindows == 0) && bufIsChanged(buf))
#ifdef FEAT_TERMINAL
if (term_job_running(buf->b_term))
{
if (term_try_stop_job(buf) == FAIL)
break;
}
else
#endif
/* Try auto-writing the buffer. If this fails but the buffer no
* longer exists it's not changed, that's OK. */
if (check_changed(buf, (p_awa ? CCGD_AW : 0)
| CCGD_MULTWIN
/* Get here if "buf" cannot be abandoned. */
exiting = FALSE;
#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
/*
* When ":confirm" used, don't give an error message.
*/
if (!(p_confirm || cmdmod.confirm))
#endif
{
/* There must be a wait_return for this message, do_buffer()
* may cause a redraw. But wait_return() is a no-op when vgetc()
* is busy (Quit used from window menu), then make sure we don't
* cause a scroll up. */
{
msg_row = cmdline_row;
msg_col = 0;
msg_didout = FALSE;
}
if (
#ifdef FEAT_TERMINAL
term_job_running(buf->b_term)
? EMSG2(_("E947: Job still running in buffer \"%s\""),
buf->b_fname)
:
#endif
EMSG2(_("E162: No write since last change for buffer \"%s\""),
buf_spname(buf) != NULL ? buf_spname(buf) : buf->b_fname))
{
save = no_wait_return;
no_wait_return = FALSE;
wait_return(FALSE);
no_wait_return = save;
}
}
/* Try to find a window that contains the buffer. */
if (buf != curbuf)
/* Paranoia: did autocms wipe out the buffer with changes? */
/* Open the changed buffer in the current window. */
if (buf != curbuf)
theend:
vim_free(bufnrs);
return ret;
}
/*
* return FAIL if there is no file name, OK if there is one
* give error message for FAIL
*/
int
{
if (curbuf->b_ffname == NULL)
{
EMSG(_(e_noname));
return FAIL;
}
return OK;
}
/*
* flush the contents of a buffer, unless it has no file name
*
* return FAIL for failure, OK otherwise
*/
int
{
int retval;
buf_T *old_curbuf = curbuf;
retval = (buf_write(buf, buf->b_ffname, buf->b_fname,
(linenr_T)1, buf->b_ml.ml_line_count, NULL,
FALSE, forceit, TRUE, FALSE));
if (curbuf != old_curbuf)
msg_source(HL_ATTR(HLF_W));
MSG(_("Warning: Entered other buffer unexpectedly (check autocommands)"));
return retval;
}
/*
* Code to handle the argument list.
*/
static char_u *do_one_arg(char_u *str);
static int do_arglist(char_u *str, int what, int after);
static void alist_check_arg_idx(void);
static int editing_arg_idx(win_T *win);
static int alist_add_list(int count, char_u **files, int after);
#define AL_SET 1
#define AL_ADD 2
#define AL_DEL 3
* Isolate one argument, taking backticks.
* Changes the argument in-place, puts a NUL after it. Backticks remain.
* Return a pointer to the start of the next argument.
*/
{
char_u *p;
int inbacktick;
inbacktick = FALSE;
for (p = str; *str; ++str)
{
/* When the backslash is used for escaping the special meaning of a
* character we need to keep it until wildcard expansion. */
if (rem_backslash(str))
{
*p++ = *str++;
*p++ = *str;
}
else
{
/* An item ends at a space not in backticks */
if (!inbacktick && vim_isspace(*str))
}
}
str = skipwhite(str);
*p = NUL;
return str;
}
/*
* Separate the arguments in "str" and return a list of pointers in the
* growarray "gap".
*/
static int
get_arglist(garray_T *gap, char_u *str, int escaped)
{
ga_init2(gap, (int)sizeof(char_u *), 20);
while (*str != NUL)
{
if (ga_grow(gap, 1) == FAIL)
{
ga_clear(gap);
return FAIL;
}
((char_u **)gap->ga_data)[gap->ga_len++] = str;
/* If str is escaped, don't handle backslashes or spaces */
if (!escaped)
return OK;
/* Isolate one argument, change it in-place, put a NUL after it. */
str = do_one_arg(str);
}
return OK;
}
#if defined(FEAT_QUICKFIX) || defined(FEAT_SYN_HL) || defined(PROTO)
/*
* Parse a list of arguments (file names), expand them and return in
* "fnames[fcountp]". When "wig" is TRUE, removes files matching 'wildignore'.
get_arglist_exp(
char_u *str,
int *fcountp,
char_u ***fnamesp,
int wig)
if (get_arglist(&ga, str, TRUE) == FAIL)
if (wig == TRUE)
i = expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
else
i = gen_expand_wildcards(ga.ga_len, (char_u **)ga.ga_data,
fcountp, fnamesp, EW_FILE|EW_NOTFOUND);
ga_clear(&ga);
return i;
}
#endif
/*
* Redefine the argument list.
*/
void
{
do_arglist(str, AL_SET, 0);
}
/*
* "what" == AL_SET: Redefine the argument list to 'str'.
* "what" == AL_ADD: add files in 'str' to the argument list after "after".
* "what" == AL_DEL: remove files in 'str' from the argument list.
*
* Return FAIL for failure, OK otherwise.
*/
static int
{
garray_T new_ga;
int exp_count;
char_u **exp_files;
int i;
char_u *p;
int match;
int arg_escaped = TRUE;
/*
* Set default argument for ":argadd" command.
*/
if (what == AL_ADD && *str == NUL)
{
if (curbuf->b_ffname == NULL)
return FAIL;
str = curbuf->b_fname;
arg_escaped = FALSE;
/*
* Collect all file name arguments in "new_ga".
*/
if (get_arglist(&new_ga, str, arg_escaped) == FAIL)
if (what == AL_DEL)
{
regmatch_T regmatch;
int didone;
/*
* Delete the items: use each item as a regexp and find a match in the
* argument list.
*/
regmatch.rm_ic = p_fic; /* ignore case when 'fileignorecase' is set */
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
for (i = 0; i < new_ga.ga_len && !got_int; ++i)
{
p = ((char_u **)new_ga.ga_data)[i];
p = file_pat_to_reg_pat(p, NULL, NULL, FALSE);
if (p == NULL)
break;
regmatch.regprog = vim_regcomp(p, p_magic ? RE_MAGIC : 0);
if (regmatch.regprog == NULL)
{
vim_free(p);
break;
}
didone = FALSE;
for (match = 0; match < ARGCOUNT; ++match)
if (vim_regexec(®match, alist_name(&ARGLIST[match]),
(colnr_T)0))
{
didone = TRUE;
vim_free(ARGLIST[match].ae_fname);
mch_memmove(ARGLIST + match, ARGLIST + match + 1,
(ARGCOUNT - match - 1) * sizeof(aentry_T));
--ALIST(curwin)->al_ga.ga_len;
if (curwin->w_arg_idx > match)
--curwin->w_arg_idx;
--match;
}
vim_free(p);
if (!didone)
EMSG2(_(e_nomatch2), ((char_u **)new_ga.ga_data)[i]);
}
ga_clear(&new_ga);
}
else
{
i = expand_wildcards(new_ga.ga_len, (char_u **)new_ga.ga_data,
&exp_count, &exp_files, EW_DIR|EW_FILE|EW_ADDSLASH|EW_NOTFOUND);
ga_clear(&new_ga);
{
EMSG(_(e_nomatch));
return FAIL;
}
if (what == AL_ADD)
{
(void)alist_add_list(exp_count, exp_files, after);
vim_free(exp_files);
}
else /* what == AL_SET */
alist_set(ALIST(curwin), exp_count, exp_files, FALSE, NULL, 0);
}
alist_check_arg_idx();
return OK;
}
/*
* Check the validity of the arg_idx for each other window.
*/
static void
if (win->w_alist == curwin->w_alist)
check_arg_idx(win);
}
/*
* Return TRUE if window "win" is editing the file at the current argument
|| (win->w_buffer->b_fnum
!= WARGLIST(win)[win->w_arg_idx].ae_fnum
&& (win->w_buffer->b_ffname == NULL
|| !(fullpathcmp(
alist_name(&WARGLIST(win)[win->w_arg_idx]),
win->w_buffer->b_ffname, TRUE) & FPC_SAME))));
}
/*
* Check if window "win" is editing the w_arg_idx file in its argument list.
*/
void
{
if (WARGCOUNT(win) > 1 && !editing_arg_idx(win))
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
{
/* We are not editing the current entry in the argument list.
* Set "arg_had_last" if we are editing the last one. */
win->w_arg_idx_invalid = TRUE;
if (win->w_arg_idx != WARGCOUNT(win) - 1
&& arg_had_last == FALSE
&& ALIST(win) == &global_alist
&& GARGCOUNT > 0
&& win->w_arg_idx < GARGCOUNT
&& (win->w_buffer->b_fnum == GARGLIST[GARGCOUNT - 1].ae_fnum
|| (win->w_buffer->b_ffname != NULL
&& (fullpathcmp(alist_name(&GARGLIST[GARGCOUNT - 1]),
win->w_buffer->b_ffname, TRUE) & FPC_SAME))))
arg_had_last = TRUE;
}
else
{
/* We are editing the current entry in the argument list.
* Set "arg_had_last" if it's also the last one */
win->w_arg_idx_invalid = FALSE;
if (win->w_arg_idx == WARGCOUNT(win) - 1
&& win->w_alist == &global_alist)
arg_had_last = TRUE;
}
}
/*
* ":args", ":argslocal" and ":argsglobal".
*/
void
{
int i;
if (eap->cmdidx != CMD_args)
{
alist_unlink(ALIST(curwin));
if (eap->cmdidx == CMD_argglobal)
ALIST(curwin) = &global_alist;
else /* eap->cmdidx == CMD_arglocal */
alist_new();
}
if (!ends_excmd(*eap->arg))
{
/*
* ":args file ..": define new argument list, handle like ":next"
* Also for ":argslocal file .." and ":argsglobal file ..".
*/
ex_next(eap);
}
{
/*
* ":args": list arguments.
*/
if (ARGCOUNT > 0)
{
char_u **items = (char_u **)alloc(sizeof(char_u *) * ARGCOUNT);
if (items != NULL)
/* Overwrite the command, for a short list there is no
* scrolling required and no wait_return(). */
gotocmdline(TRUE);
for (i = 0; i < ARGCOUNT; ++i)
items[i] = alist_name(&ARGLIST[i]);
list_in_columns(items, ARGCOUNT, curwin->w_arg_idx);
vim_free(items);
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
}
}
}
else if (eap->cmdidx == CMD_arglocal)
{
garray_T *gap = &curwin->w_alist->al_ga;
/*
* ":argslocal": make a local copy of the global argument list.
*/
if (ga_grow(gap, GARGCOUNT) == OK)
for (i = 0; i < GARGCOUNT; ++i)
if (GARGLIST[i].ae_fname != NULL)
{
AARGLIST(curwin->w_alist)[gap->ga_len].ae_fname =
vim_strsave(GARGLIST[i].ae_fname);
AARGLIST(curwin->w_alist)[gap->ga_len].ae_fnum =
GARGLIST[i].ae_fnum;
++gap->ga_len;
}
}
}
/*
* ":previous", ":sprevious", ":Next" and ":sNext".
*/
void
{
/* If past the last one already, go to the last one. */
if (curwin->w_arg_idx - (int)eap->line2 >= ARGCOUNT)
do_argfile(eap, ARGCOUNT - 1);
else
do_argfile(eap, curwin->w_arg_idx - (int)eap->line2);
}
/*
* ":rewind", ":first", ":sfirst" and ":srewind".
*/
void
{
do_argfile(eap, 0);
}
/*
* ":last" and ":slast".
*/
void
{
do_argfile(eap, ARGCOUNT - 1);
}
/*
* ":argument" and ":sargument".
*/
void
{
int i;
if (eap->addr_count > 0)
i = eap->line2 - 1;
else
i = curwin->w_arg_idx;
do_argfile(eap, i);
}
/*
* Edit file "argn" of the argument lists.
*/
void
if (argn < 0 || argn >= ARGCOUNT)
{
if (ARGCOUNT <= 1)
EMSG(_("E163: There is only one file to edit"));
else if (argn < 0)
EMSG(_("E164: Cannot go before first file"));
else
EMSG(_("E165: Cannot go beyond last file"));
}
else
{
setpcmark();
#ifdef FEAT_GUI
need_mouse_correct = TRUE;
#endif
/* split window or create new tab page first */
if (*eap->cmd == 's' || cmdmod.tab != 0)
{
if (win_split(0, 0) == FAIL)
return;
}
else
{
/*
* if 'hidden' set, only check for changed file when re-editing
* the same buffer
*/
other = TRUE;
if (buf_hide(curbuf))
{
p = fix_fname(alist_name(&ARGLIST[argn]));
other = otherfile(p);
vim_free(p);
}
if ((!buf_hide(curbuf) || !other)
&& check_changed(curbuf, CCGD_AW
| (other ? 0 : CCGD_MULTWIN)
| (eap->forceit ? CCGD_FORCEIT : 0)
| CCGD_EXCMD))
return;
}
curwin->w_arg_idx = argn;
if (argn == ARGCOUNT - 1 && curwin->w_alist == &global_alist)
/* Edit the file; always use the last known line number.
* When it fails (e.g. Abort for already edited file) restore the
* argument index. */
if (do_ecmd(0, alist_name(&ARGLIST[curwin->w_arg_idx]), NULL,
(buf_hide(curwin->w_buffer) ? ECMD_HIDE : 0)
+ (eap->forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
/* like Vi: set the mark where the cursor is in the file. */
setmark('\'');
}
}
/*
* ":next", and commands that behave like it.
*/
void
{
int i;
/*
* check for changed buffer now, if this fails the argument list is not
* redefined.
*/
if ( buf_hide(curbuf)
|| !check_changed(curbuf, CCGD_AW
| (eap->forceit ? CCGD_FORCEIT : 0)
| CCGD_EXCMD))
{
if (*eap->arg != NUL) /* redefine file list */
{
if (do_arglist(eap->arg, AL_SET, 0) == FAIL)
return;
i = 0;
}
else
i = curwin->w_arg_idx + (int)eap->line2;
do_argfile(eap, i);
}
}
/*
* ":argedit"
*/
void
int i = eap->addr_count ? (int)eap->line2 : curwin->w_arg_idx + 1;
if (do_arglist(eap->arg, AL_ADD, i) == FAIL)
return;
#ifdef FEAT_TITLE
maketitle();
#endif
if (curwin->w_arg_idx == 0 && (curbuf->b_ml.ml_flags & ML_EMPTY)
&& curbuf->b_ffname == NULL)
i = 0;
if (i < ARGCOUNT)
do_argfile(eap, i);
}
/*
* ":argadd"
*/
void
{
do_arglist(eap->arg, AL_ADD,
eap->addr_count > 0 ? (int)eap->line2 : curwin->w_arg_idx + 1);
#ifdef FEAT_TITLE
maketitle();
#endif
}
/*
* ":argdelete"
*/
void
{
int i;
int n;
if (eap->addr_count > 0)
{
/* ":1,4argdel": Delete all arguments in the range. */
if (eap->line2 > ARGCOUNT)
eap->line2 = ARGCOUNT;
n = eap->line2 - eap->line1 + 1;
if (*eap->arg != NUL)
/* Can't have both a range and an argument. */
else if (n <= 0)
{
/* Don't give an error for ":%argdel" if the list is empty. */
if (eap->line1 != 1 || eap->line2 != 0)
EMSG(_(e_invrange));
}
else
{
for (i = eap->line1; i <= eap->line2; ++i)
vim_free(ARGLIST[i - 1].ae_fname);
mch_memmove(ARGLIST + eap->line1 - 1, ARGLIST + eap->line2,