/* 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.
*/
/*
* ex_cmds2.c: some more functions for command line commands
*/
#include "vim.h"
#include "version.h"
/*
* If 'autowrite' option set, try to write the file.
* Careful: autocommands may make "buf" invalid!
*
* return FAIL for failure, OK otherwise
*/
int
autowrite(buf_T *buf, int forceit)
{
int r;
bufref_T bufref;
if (!(p_aw || p_awa) || !p_write
#ifdef FEAT_QUICKFIX
// never autowrite a "nofile" or "nowrite" buffer
|| bt_dontwrite(buf)
#endif
|| (!forceit && buf->b_p_ro) || buf->b_ffname == NULL)
return FAIL;
set_bufref(&bufref, buf);
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.
if (bufref_valid(&bufref) && bufIsChanged(buf))
r = FAIL;
return r;
}
/*
* Flush all buffers, except the ones that are readonly or are never written.
*/
void
autowrite_all(void)
{
buf_T *buf;
if (!(p_aw || p_awa) || !p_write)
return;
FOR_ALL_BUFFERS(buf)
if (bufIsChanged(buf) && !buf->b_p_ro && !bt_dontwrite(buf))
{
bufref_T bufref;
set_bufref(&bufref, buf);
(void)buf_write_all(buf, FALSE);
// an autocommand may have deleted the buffer
if