#if defined(MSDOS) || defined(WIN16) || defined(WIN32) || defined(_WIN64)
# include "vimio.h"
#endif
#if defined __EMX__
# include "vimio.h"
#endif
#include "vim.h"
#ifdef __TANDEM
# include <limits.h>
#endif
#if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
# include <utime.h>
#endif
#if defined(HAVE_COPYFILE_H)
#include <copyfile.h>
#endif
#define BUFSIZE 8192
#define SMBUFSIZE 256
#ifdef FEAT_CRYPT
# define CRYPT_MAGIC "VimCrypt~01!"
# define CRYPT_MAGIC_LEN 12
#endif
#define USE_MCH_ACCESS
#if defined(sun) && defined(S_ISCHR)
# define OPEN_CHR_FILES
static int is_dev_fd_file(char_u *fname);
#endif
#ifdef FEAT_MBYTE
static char_u *next_fenc __ARGS((char_u **pp));
# ifdef FEAT_EVAL
static char_u *readfile_charconvert __ARGS((char_u *fname, char_u *fenc, int *fdp));
# endif
#endif
#ifdef FEAT_VIMINFO
static void check_marks_read __ARGS((void));
#endif
#ifdef FEAT_CRYPT
static char_u *check_for_cryptkey __ARGS((char_u *cryptkey, char_u *ptr, long *sizep, long *filesizep, int newfile));
#endif
#ifdef UNIX
static void set_file_time __ARGS((char_u *fname, time_t atime, time_t mtime));
#endif
static int set_rw_fname __ARGS((char_u *fname, char_u *sfname));
static int msg_add_fileformat __ARGS((int eol_type));
static void msg_add_eol __ARGS((void));
static int check_mtime __ARGS((buf_T *buf, struct stat *s));
static int time_differs __ARGS((long t1, long t2));
#ifdef FEAT_AUTOCMD
static int apply_autocmds_exarg __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, buf_T *buf, exarg_T *eap));
static int au_find_group __ARGS((char_u *name));
# define AUGROUP_DEFAULT -1
# define AUGROUP_ERROR -2
# define AUGROUP_ALL -3
#endif
#if defined(FEAT_CRYPT) || defined(FEAT_MBYTE)
# define HAS_BW_FLAGS
# define FIO_LATIN1 0x01
# define FIO_UTF8 0x02
# define FIO_UCS2 0x04
# define FIO_UCS4 0x08
# define FIO_UTF16 0x10
# ifdef WIN3264
# define FIO_CODEPAGE 0x20
# define FIO_PUT_CP(x) (((x) & 0xffff) << 16)
# define FIO_GET_CP(x) (((x)>>16) & 0xffff)
# endif
# ifdef MACOS_X
# define FIO_MACROMAN 0x20
# endif
# define FIO_ENDIAN_L 0x80
# define FIO_ENCRYPTED 0x1000
# define FIO_NOCONVERT 0x2000
# define FIO_UCSBOM 0x4000
# define FIO_ALL -1
#endif
#define CONV_RESTLEN 30
#define ICONV_MULT 8
struct bw_info
{
int bw_fd;
char_u *bw_buf;
int bw_len;
#ifdef HAS_BW_FLAGS
int bw_flags;
#endif
#ifdef FEAT_MBYTE
char_u bw_rest[CONV_RESTLEN];
int bw_restlen;
int bw_first;
char_u *bw_conv_buf;
int bw_conv_buflen;
int bw_conv_error;
# ifdef USE_ICONV
iconv_t bw_iconv_fd;
# endif
#endif
};
static int buf_write_bytes __ARGS((struct bw_info *ip));
#ifdef FEAT_MBYTE
static linenr_T readfile_linenr __ARGS((linenr_T linecnt, char_u *p, char_u *endp));
static int ucs2bytes __ARGS((unsigned c, char_u **pp, int flags));
static int same_encoding __ARGS((char_u *a, char_u *b));
static int get_fio_flags __ARGS((char_u *ptr));
static char_u *check_for_bom __ARGS((char_u *p, long size, int *lenp, int flags));
static int make_bom __ARGS((char_u *buf, char_u *name));
# ifdef WIN3264
static int get_win_fio_flags __ARGS((char_u *ptr));
# endif
# ifdef MACOS_X
static int get_mac_fio_flags __ARGS((char_u *ptr));
# endif
#endif
static int move_lines __ARGS((buf_T *frombuf, buf_T *tobuf));
void
filemess(buf, name, s, attr)
buf_T *buf;
char_u *name;
char_u *s;
int attr;
{
int msg_scroll_save;
if (msg_silent != 0)
return;
msg_add_fname(buf, name);
if (STRLEN(IObuff) > IOSIZE - 80)
IObuff[IOSIZE - 80] = NUL;
STRCAT(IObuff, s);
msg_scroll_save = msg_scroll;
if (shortmess(SHM_OVERALL) && !exiting && p_verbose == 0)
msg_scroll = FALSE;
if (!msg_scroll)
check_for_delay(FALSE);
msg_start();
msg_scroll = msg_scroll_save;
msg_scrolled_ign = TRUE;
msg_outtrans_attr(msg_may_trunc(FALSE, IObuff), attr);
msg_clr_eos();
out_flush();
msg_scrolled_ign = FALSE;
}
int
readfile(fname, sfname, from, lines_to_skip, lines_to_read, eap, flags)
char_u *fname;
char_u *sfname;
linenr_T from;
linenr_T lines_to_skip;
linenr_T lines_to_read;
exarg_T *eap;
int flags;
{
int fd = 0;
int newfile = (flags & READ_NEW);
int check_readonly;
int filtering = (flags & READ_FILTER);
int read_stdin = (flags & READ_STDIN);
int read_buffer = (flags & READ_BUFFER);
int set_options = newfile || read_buffer
|| (eap != NULL && eap->read_edit);
linenr_T read_buf_lnum = 1;
colnr_T read_buf_col = 0;
char_u c;
linenr_T lnum = from;
char_u *ptr = NULL;
char_u *buffer = NULL;
char_u *new_buffer = NULL;
char_u *line_start = NULL;
int wasempty;
colnr_T len;
long size = 0;
char_u *p;
long filesize = 0;
int skip_read = FALSE;
#ifdef FEAT_CRYPT
char_u *cryptkey = NULL;
#endif
int split = 0;
#define UNKNOWN 0x0fffffff
linenr_T linecnt;
int error = FALSE;
int ff_error = EOL_UNKNOWN;
long linerest = 0;
#ifdef UNIX
int perm = 0;
int swap_mode = -1;
#else
int perm;
#endif
int fileformat = 0;
int keep_fileformat = FALSE;
struct stat st;
int file_readonly;
linenr_T skip_count = 0;
linenr_T read_count = 0;
int msg_save = msg_scroll;
linenr_T read_no_eol_lnum = 0;
int try_mac = (vim_strchr(p_ffs, 'm') != NULL);
int try_dos = (vim_strchr(p_ffs, 'd') != NULL);
int try_unix = (vim_strchr(p_ffs, 'x') != NULL);
int file_rewind = FALSE;
#ifdef FEAT_MBYTE
int can_retry;
linenr_T conv_error = 0;
linenr_T illegal_byte = 0;
int keep_dest_enc = FALSE;
int bad_char_behavior = BAD_REPLACE;
char_u *tmpname = NULL;
int fio_flags = 0;
char_u *fenc;
int fenc_alloced;
char_u *fenc_next = NULL;
int advance_fenc = FALSE;
long real_size = 0;
# ifdef USE_ICONV
iconv_t iconv_fd = (iconv_t)-1;
# ifdef FEAT_EVAL
int did_iconv = FALSE;
# endif
# endif
int converted = FALSE;
int notconverted = FALSE;
char_u conv_rest[CONV_RESTLEN];
int conv_restlen = 0;
#endif
write_no_eol_lnum = 0;
if (curbuf->b_ffname == NULL
&& !filtering
&& fname != NULL
&& vim_strchr(p_cpo, CPO_FNAMER) != NULL
&& !(flags & READ_DUMMY))
{
if (set_rw_fname(fname, sfname) == FAIL)
return FAIL;
}
ex_no_reprint = TRUE;
need_fileinfo = FALSE;
if (sfname == NULL)
sfname = fname;
#if defined(UNIX) || defined(__EMX__)
fname = sfname;
#endif
#ifdef FEAT_AUTOCMD
if (!filtering && !read_stdin && !read_buffer)
{
pos_T pos;
pos = curbuf->b_op_start;
curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
curbuf->b_op_start.col = 0;
if (newfile)
{
if (apply_autocmds_exarg(EVENT_BUFREADCMD, NULL, sfname,
FALSE, curbuf, eap))
#ifdef FEAT_EVAL
return aborting() ? FAIL : OK;
#else
return OK;
#endif
}
else if (apply_autocmds_exarg(EVENT_FILEREADCMD, sfname, sfname,
FALSE, NULL, eap))
#ifdef FEAT_EVAL
return aborting() ? FAIL : OK;
#else
return OK;
#endif
curbuf->b_op_start = pos;
}
#endif
if ((shortmess(SHM_OVER) || curbuf->b_help) && p_verbose == 0)
msg_scroll = FALSE;
else
msg_scroll = TRUE;
if (fname != NULL && *fname != NUL)
{
p = fname + STRLEN(fname);
if (after_pathsep(fname, p) || STRLEN(fname) >= MAXPATHL)
{
filemess(curbuf, fname, (char_u *)_("Illegal file name"), 0);
msg_end();
msg_scroll = msg_save;
return FAIL;
}
}
#ifdef UNIX
if (!read_stdin && !read_buffer)
{
perm = mch_getperm(fname);
if (perm >= 0 && !S_ISREG(perm)
# 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(fname))
# endif
)
{
if (S_ISDIR(perm))
filemess(curbuf, fname, (char_u *)_("is a directory"), 0);
else
filemess(curbuf, fname, (char_u *)_("is not a file"), 0);
msg_end();
msg_scroll = msg_save;
return FAIL;
}
# if defined(MSDOS) || defined(MSWIN) || defined(OS2)
if (!p_odev && mch_nodetype(fname) == NODE_WRITABLE)
{
filemess(curbuf, fname, (char_u *)_("is a device (disabled with 'opendevice' option)"), 0);
msg_end();
msg_scroll = msg_save;
return FAIL;
}
# endif
}
#endif
if (set_options)
{
if (eap != NULL && eap->force_ff != 0)
set_fileformat(get_fileformat_force(curbuf, eap), OPT_LOCAL);
else if (*p_ffs != NUL)
set_fileformat(default_fileformat(), OPT_LOCAL);
}
if (eap != NULL && eap->force_bin != 0)
{
int oldval = curbuf->b_p_bin;
curbuf->b_p_bin = (eap->force_bin == FORCE_BIN);
set_options_bin(oldval, curbuf->b_p_bin, OPT_LOCAL);
}
check_readonly = (newfile && (curbuf->b_flags & BF_CHECK_RO));
if (check_readonly && !readonlymode)
curbuf->b_p_ro = FALSE;
if (newfile && !read_stdin && !read_buffer)
{
if (mch_stat((char *)fname, &st) >= 0)
{
buf_store_time(curbuf, &st, fname);
curbuf->b_mtime_read = curbuf->b_mtime;
#if defined(RISCOS) && defined(FEAT_OSFILETYPE)
mch_read_filetype(fname);
#endif
#ifdef UNIX
swap_mode = (st.st_mode & 0644) | 0600;
#endif
#ifdef FEAT_CW_EDITOR
(void)GetFSSpecFromPath(curbuf->b_ffname, &curbuf->b_FSSpec);
#endif
#ifdef VMS
curbuf->b_fab_rfm = st.st_fab_rfm;
curbuf->b_fab_rat = st.st_fab_rat;
curbuf->b_fab_mrs = st.st_fab_mrs;
#endif
}
else
{
curbuf->b_mtime = 0;
curbuf->b_mtime_read = 0;
curbuf->b_orig_size = 0;
curbuf->b_orig_mode = 0;
}
curbuf->b_flags &= ~(BF_NEW | BF_NEW_W);
}
file_readonly = FALSE;
if (read_stdin)
{
#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
setmode(0, O_BINARY);
#endif
}
else if (!read_buffer)
{
#ifdef USE_MCH_ACCESS
if (
# ifdef UNIX
!(perm & 0222) ||
# endif
mch_access((char *)fname, W_OK))
file_readonly = TRUE;
fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
#else
if (!newfile
|| readonlymode
|| (fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0)
{
file_readonly = TRUE;
fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
}
#endif
}
if (fd < 0)
{
#ifndef UNIX
int isdir_f;
#endif
msg_scroll = msg_save;
#ifndef UNIX
isdir_f = (mch_isdir(fname));
perm = mch_getperm(fname);
if (isdir_f)
{
filemess(curbuf, sfname, (char_u *)_("is a directory"), 0);
curbuf->b_p_ro = TRUE;
}
else
#endif
if (newfile)
{
if (perm < 0)
{
curbuf->b_flags |= BF_NEW;
#ifdef FEAT_QUICKFIX
if (!bt_dontwrite(curbuf))
#endif
check_need_swap(newfile);
if (dir_of_file_exists(fname))
filemess(curbuf, sfname, (char_u *)_("[New File]"), 0);
else
filemess(curbuf, sfname,
(char_u *)_("[New DIRECTORY]"), 0);
#ifdef FEAT_VIMINFO
check_marks_read();
#endif
#ifdef FEAT_MBYTE
if (eap != NULL && eap->force_enc != 0)
{
fenc = enc_canonize(eap->cmd + eap->force_enc);
if (fenc != NULL)
set_string_option_direct((char_u *)"fenc", -1,
fenc, OPT_FREE|OPT_LOCAL, 0);
vim_free(fenc);
}
#endif
#ifdef FEAT_AUTOCMD
apply_autocmds_exarg(EVENT_BUFNEWFILE, sfname, sfname,
FALSE, curbuf, eap);
#endif
save_file_ff(curbuf);
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
if (aborting())
return FAIL;
#endif
return OK;
}
else
{
filemess(curbuf, sfname, (char_u *)(
# ifdef EFBIG
(errno == EFBIG) ? _("[File too big]") :
# endif
_("[Permission Denied]")), 0);
curbuf->b_p_ro = TRUE;
}
}
return FAIL;
}
if ((check_readonly && file_readonly) || curbuf->b_help)
curbuf->b_p_ro = TRUE;
if (set_options)
{
if (!read_buffer)
{
curbuf->b_p_eol = TRUE;
curbuf->b_start_eol = TRUE;
}
#ifdef FEAT_MBYTE
curbuf->b_p_bomb = FALSE;
curbuf->b_start_bomb = FALSE;
#endif
}
#ifdef FEAT_QUICKFIX
if (!bt_dontwrite(curbuf))
#endif
{
check_need_swap(newfile);
#ifdef UNIX
if (swap_mode > 0 && curbuf->b_ml.ml_mfp->mf_fname != NULL)
(void)mch_setperm(curbuf->b_ml.ml_mfp->mf_fname, (long)swap_mode);
#endif
}
#if defined(HAS_SWAP_EXISTS_ACTION)
if (swap_exists_action == SEA_QUIT)
{
if (!read_buffer && !read_stdin)
close(fd);
return FAIL;
}
#endif
++no_wait_return;
curbuf->b_op_start.lnum = ((from == 0) ? 1 : from);
curbuf->b_op_start.col = 0;
#ifdef FEAT_AUTOCMD
if (!read_buffer)
{
int m = msg_scroll;
int n = msg_scrolled;
buf_T *old_curbuf = curbuf;
if (!read_stdin)
close(fd);
msg_scroll = TRUE;
if (filtering)
apply_autocmds_exarg(EVENT_FILTERREADPRE, NULL, sfname,
FALSE, curbuf, eap);
else if (read_stdin)
apply_autocmds_exarg(EVENT_STDINREADPRE, NULL, sfname,
FALSE, curbuf, eap);
else if (newfile)
apply_autocmds_exarg(EVENT_BUFREADPRE, NULL, sfname,
FALSE, curbuf, eap);
else
apply_autocmds_exarg(EVENT_FILEREADPRE, sfname, sfname,
FALSE, NULL, eap);
if (msg_scrolled == n)
msg_scroll = m;
#ifdef FEAT_EVAL
if (aborting())
{
--no_wait_return;
msg_scroll = msg_save;
curbuf->b_p_ro = TRUE;
return FAIL;
}
#endif
if (!read_stdin && (curbuf != old_curbuf
|| (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) < 0))
{
--no_wait_return;
msg_scroll = msg_save;
if (fd < 0)
EMSG(_("E200: *ReadPre autocommands made the file unreadable"));
else
EMSG(_("E201: *ReadPre autocommands must not change current buffer"));
curbuf->b_p_ro = TRUE;
return FAIL;
}
}
#endif
wasempty = (curbuf->b_ml.ml_flags & ML_EMPTY);
if (!recoverymode && !filtering && !(flags & READ_DUMMY))
{
if (read_stdin)
{
#ifndef ALWAYS_USE_GUI
mch_msg(_("Vim: Reading from stdin...\n"));
#endif
#ifdef FEAT_GUI
if (gui.in_use && !gui.dying && !gui.starting)
{
p = (char_u *)_("Reading from stdin...");
gui_write(p, (int)STRLEN(p));
}
#endif
}
else if (!read_buffer)
filemess(curbuf, sfname, (char_u *)"", 0);
}
msg_scroll = FALSE;
linecnt = curbuf->b_ml.ml_line_count;
#ifdef FEAT_MBYTE
if (eap != NULL && eap->bad_char != 0)
{
bad_char_behavior = eap->bad_char;
if (set_options)
curbuf->b_bad_char = eap->bad_char;
}
else
curbuf->b_bad_char = 0;
if (eap != NULL && eap->force_enc != 0)
{
fenc = enc_canonize(eap->cmd + eap->force_enc);
fenc_alloced = TRUE;
keep_dest_enc = TRUE;
}
else if (curbuf->b_p_bin)
{
fenc = (char_u *)"";
fenc_alloced = FALSE;
}
else if (curbuf->b_help)
{
char_u firstline[80];
int fc;
fenc = (char_u *)"latin1";
c = enc_utf8;
if (!c && !read_stdin)
{
fc = fname[STRLEN(fname) - 1];
if (TOLOWER_ASC(fc) == 'x')
{
len = vim_read(fd, firstline, 80);
lseek(fd, (off_t)0L, SEEK_SET);
for (p = firstline; p < firstline + len; ++p)
if (*p >= 0x80)
{
c = TRUE;
break;
}
}
}
if (c)
{
fenc_next = fenc;
fenc = (char_u *)"utf-8";
if (!enc_utf8)
keep_dest_enc = TRUE;
}
fenc_alloced = FALSE;
}
else if (*p_fencs == NUL)
{
fenc = curbuf->b_p_fenc;
fenc_alloced = FALSE;
}
else
{
fenc_next = p_fencs;
fenc = next_fenc(&fenc_next);
fenc_alloced = TRUE;
}
#endif
retry:
if (file_rewind)
{
if (read_buffer)
{
read_buf_lnum = 1;
read_buf_col = 0;
}
else if (read_stdin || lseek(fd, (off_t)0L, SEEK_SET) != 0)
{
error = TRUE;
goto failed;
}
while (lnum > from)
ml_delete(lnum--, FALSE);
file_rewind = FALSE;
#ifdef FEAT_MBYTE
if (set_options)
{
curbuf->b_p_bomb = FALSE;
curbuf->b_start_bomb = FALSE;
}
conv_error = 0;
#endif
}
if (keep_fileformat)
keep_fileformat = FALSE;
else
{
if (eap != NULL && eap->force_ff != 0)
{
fileformat = get_fileformat_force(curbuf, eap);
try_unix = try_dos = try_mac = FALSE;
}
else if (curbuf->b_p_bin)
fileformat = EOL_UNIX;
else if (*p_ffs == NUL)
fileformat = get_fileformat(curbuf);
else
fileformat = EOL_UNKNOWN;
}
#ifdef FEAT_MBYTE
# ifdef USE_ICONV
if (iconv_fd != (iconv_t)-1)
{
iconv_close(iconv_fd);
iconv_fd = (iconv_t)-1;
}
# endif
if (advance_fenc)
{
advance_fenc = FALSE;
if (eap != NULL && eap->force_enc != 0)
{
notconverted = TRUE;
conv_error = 0;
if (fenc_alloced)
vim_free(fenc);
fenc = (char_u *)"";
fenc_alloced = FALSE;
}
else
{
if (fenc_alloced)
vim_free(fenc);
if (fenc_next != NULL)
{
fenc = next_fenc(&fenc_next);
fenc_alloced = (fenc_next != NULL);
}
else
{
fenc = (char_u *)"";
fenc_alloced = FALSE;
}
}
if (tmpname != NULL)
{
mch_remove(tmpname);
vim_free(tmpname);
tmpname = NULL;
}
}
fio_flags = 0;
converted = (*fenc != NUL && !same_encoding(p_enc, fenc));
if (converted || enc_unicode != 0)
{
if (STRCMP(fenc, ENC_UCSBOM) == 0)
fio_flags = FIO_UCSBOM;
else if (enc_utf8 || STRCMP(p_enc, "latin1") == 0)
fio_flags = get_fio_flags(fenc);
# ifdef WIN3264
if (fio_flags == 0)
fio_flags = get_win_fio_flags(fenc);
# endif
# ifdef MACOS_X
if (fio_flags == 0)
fio_flags = get_mac_fio_flags(fenc);
# endif
# ifdef USE_ICONV
if (fio_flags == 0
# ifdef FEAT_EVAL
&& !did_iconv
# endif
)
iconv_fd = (iconv_t)my_iconv_open(
enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc);
# endif
# ifdef FEAT_EVAL
if (fio_flags == 0 && !read_stdin && !read_buffer && *p_ccv != NUL
# ifdef USE_ICONV
&& iconv_fd == (iconv_t)-1
# endif
)
{
# ifdef USE_ICONV
did_iconv = FALSE;
# endif
if (tmpname == NULL)
{
tmpname = readfile_charconvert(fname, fenc, &fd);
if (tmpname == NULL)
{
advance_fenc = TRUE;
if (fd < 0)
{
EMSG(_("E202: Conversion made file unreadable!"));
error = TRUE;
goto failed;
}
goto retry;
}
}
}
else
# endif
{
if (fio_flags == 0
# ifdef USE_ICONV
&& iconv_fd == (iconv_t)-1
# endif
)
{
advance_fenc = TRUE;
goto retry;
}
}
}
can_retry = (*fenc != NUL && !read_stdin && !keep_dest_enc);
#endif
if (!skip_read)
{
linerest = 0;
filesize = 0;
skip_count = lines_to_skip;
read_count = lines_to_read;
#ifdef FEAT_MBYTE
conv_restlen = 0;
#endif
}
while (!error && !got_int)
{
#if SIZEOF_INT <= 2
if (linerest >= 0x7ff0)
{
++split;
*ptr = NL;
size = 1;
}
else
#endif
{
if (!skip_read)
{
#if SIZEOF_INT > 2
# if defined(SSIZE_MAX) && (SSIZE_MAX < 0x10000L)
size = SSIZE_MAX;
# else
size = 0x10000L;
# endif
#else
size = 0x7ff0L - linerest;
#endif
for ( ; size >= 10; size = (long)((long_u)size >> 1))
{
if ((new_buffer = lalloc((long_u)(size + linerest + 1),
FALSE)) != NULL)
break;
}
if (new_buffer == NULL)
{
do_outofmem_msg((long_u)(size * 2 + linerest + 1));
error = TRUE;
break;
}
if (linerest)
mch_memmove(new_buffer, ptr - linerest, (size_t)linerest);
vim_free(buffer);
buffer = new_buffer;
ptr = buffer + linerest;
line_start = buffer;
#ifdef FEAT_MBYTE
real_size = (int)size;
# ifdef USE_ICONV
if (iconv_fd != (iconv_t)-1)
size = size / ICONV_MULT;
else
# endif
if (fio_flags & FIO_LATIN1)
size = size / 2;
else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
size = (size * 2 / 3) & ~1;
else if (fio_flags & FIO_UCS4)
size = (size * 2 / 3) & ~3;
else if (fio_flags == FIO_UCSBOM)
size = size / ICONV_MULT;
# ifdef WIN3264
else if (fio_flags & FIO_CODEPAGE)
size = size / ICONV_MULT;
# endif
# ifdef MACOS_X
else if (fio_flags & FIO_MACROMAN)
size = size / ICONV_MULT;
# endif
#endif
#ifdef FEAT_MBYTE
if (conv_restlen > 0)
{
mch_memmove(ptr, conv_rest, conv_restlen);
ptr += conv_restlen;
size -= conv_restlen;
}
#endif
if (read_buffer)
{
if (read_buf_lnum > from)
size = 0;
else
{
int n, ni;
long tlen;
tlen = 0;
for (;;)
{
p = ml_get(read_buf_lnum) + read_buf_col;
n = (int)STRLEN(p);
if ((int)tlen + n + 1 > size)
{
n = (int)(size - tlen);
for (ni = 0; ni < n; ++ni)
{
if (p[ni] == NL)
ptr[tlen++] = NUL;
else
ptr[tlen++] = p[ni];
}
read_buf_col += n;
break;
}
else
{
for (ni = 0; ni < n; ++ni)
{
if (p[ni] == NL)
ptr[tlen++] = NUL;
else
ptr[tlen++] = p[ni];
}
ptr[tlen++] = NL;
read_buf_col = 0;
if (++read_buf_lnum > from)
{
if (!curbuf->b_p_eol)
--tlen;
size = tlen;
break;
}
}
}
}
}
else
{
size = vim_read(fd, ptr, size);
}
if (size <= 0)
{
if (size < 0)
error = TRUE;
#ifdef FEAT_MBYTE
else if (conv_restlen > 0)
{
if (fio_flags != 0
# ifdef USE_ICONV
|| iconv_fd != (iconv_t)-1
# endif
)
{
if (conv_error == 0)
conv_error = curbuf->b_ml.ml_line_count
- linecnt + 1;
}
else if (illegal_byte == 0)
illegal_byte = curbuf->b_ml.ml_line_count
- linecnt + 1;
if (bad_char_behavior == BAD_DROP)
{
*(ptr - conv_restlen) = NUL;
conv_restlen = 0;
}
else
{
if (bad_char_behavior != BAD_KEEP && (fio_flags != 0
# ifdef USE_ICONV
|| iconv_fd != (iconv_t)-1
# endif
))
{
while (conv_restlen > 0)
{
*(--ptr) = bad_char_behavior;
--conv_restlen;
}
}
fio_flags = 0;
# ifdef USE_ICONV
if (iconv_fd != (iconv_t)-1)
{
iconv_close(iconv_fd);
iconv_fd = (iconv_t)-1;
}
# endif
}
}
#endif
}
#ifdef FEAT_CRYPT
if (filesize == 0)
cryptkey = check_for_cryptkey(cryptkey, ptr, &size,
&filesize, newfile);
if (cryptkey != NULL && size > 0)
for (p = ptr; p < ptr + size; ++p)
ZDECODE(*p);
#endif
}
skip_read = FALSE;
#ifdef FEAT_MBYTE
if ((filesize == 0
# ifdef FEAT_CRYPT
|| (filesize == CRYPT_MAGIC_LEN && cryptkey != NULL)
# endif
)
&& (fio_flags == FIO_UCSBOM
|| (!curbuf->b_p_bomb
&& tmpname == NULL
&& (*fenc == 'u' || (*fenc == NUL && enc_utf8)))))
{
char_u *ccname;
int blen;
if (size < 2 || curbuf->b_p_bin)
ccname = NULL;
else
ccname = check_for_bom(ptr, size, &blen,
fio_flags == FIO_UCSBOM ? FIO_ALL : get_fio_flags(fenc));
if (ccname != NULL)
{
filesize += blen;
size -= blen;
mch_memmove(ptr, ptr + blen, (size_t)size);
if (set_options)
{
curbuf->b_p_bomb = TRUE;
curbuf->b_start_bomb = TRUE;
}
}
if (fio_flags == FIO_UCSBOM)
{
if (ccname == NULL)
{
advance_fenc = TRUE;
}
else
{
if (fenc_alloced)
vim_free(fenc);
fenc = ccname;
fenc_alloced = FALSE;
}
skip_read = TRUE;
goto retry;
}
}
ptr -= conv_restlen;
size += conv_restlen;
conv_restlen = 0;
#endif
if (size <= 0)
break;
#ifdef FEAT_MBYTE
# ifdef USE_ICONV
if (iconv_fd != (iconv_t)-1)
{
const char *fromp;
char *top;
size_t from_size;
size_t to_size;
fromp = (char *)ptr;
from_size = size;
ptr += size;
top = (char *)ptr;
to_size = real_size - size;
while ((iconv(iconv_fd, (void *)&fromp, &from_size,
&top, &to_size)
== (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
|| from_size > CONV_RESTLEN)
{
if (can_retry)
goto rewind_retry;
if (conv_error == 0)
conv_error = readfile_linenr(linecnt,
ptr, (char_u *)top);
++fromp;
--from_size;
if (bad_char_behavior == BAD_KEEP)
{
*top++ = *(fromp - 1);
--to_size;
}
else if (bad_char_behavior != BAD_DROP)
{
*top++ = bad_char_behavior;
--to_size;
}
}
if (from_size > 0)
{
mch_memmove(conv_rest, (char_u *)fromp, from_size);
conv_restlen = (int)from_size;
}
line_start = ptr - linerest;
mch_memmove(line_start, buffer, (size_t)linerest);
size = (long)((char_u *)top - ptr);
}
# endif
# ifdef WIN3264
if (fio_flags & FIO_CODEPAGE)
{
char_u *src, *dst;
WCHAR ucs2buf[3];
int ucs2len;
int codepage = FIO_GET_CP(fio_flags);
int bytelen;
int found_bad;
char replstr[2];
if (bad_char_behavior > 0)
replstr[0] = bad_char_behavior;
else
replstr[0] = '?';
replstr[1] = NUL;
src = ptr + real_size - size;
mch_memmove(src, ptr, size);
dst = ptr;
size = size;
while (size > 0)
{
found_bad = FALSE;
# ifdef CP_UTF8
if (codepage == CP_UTF8)
{
bytelen = (int)utf_ptr2len_len(src, size);
if (bytelen > size)
{
if (bytelen <= CONV_RESTLEN)
break;
bytelen = size;
found_bad = TRUE;
}
else
{
int u8c = utf_ptr2char(src);
if (u8c > 0xffff || (*src >= 0x80 && bytelen == 1))
found_bad = TRUE;
ucs2buf[0] = u8c;
ucs2len = 1;
}
}
else
# endif
{
for (bytelen = 1; bytelen <= size && bytelen <= 3;
++bytelen)
{
ucs2len = MultiByteToWideChar(codepage,
MB_ERR_INVALID_CHARS,
(LPCSTR)src, bytelen,
ucs2buf, 3);
if (ucs2len > 0)
break;
}
if (ucs2len == 0)
{
if (size == 1)
break;
found_bad = TRUE;
bytelen = 1;
}
}
if (!found_bad)
{
int i;
if (enc_utf8)
{
for (i = 0; i < ucs2len; ++i)
dst += utf_char2bytes(ucs2buf[i], dst);
}
else
{
BOOL bad = FALSE;
int dstlen;
dstlen = WideCharToMultiByte(enc_codepage, 0,
(LPCWSTR)ucs2buf, ucs2len,
(LPSTR)dst, (int)(src - dst),
replstr, &bad);
if (bad)
found_bad = TRUE;
else
dst += dstlen;
}
}
if (found_bad)
{
if (can_retry)
goto rewind_retry;
if (conv_error == 0)
conv_error = readfile_linenr(linecnt, ptr, dst);
if (bad_char_behavior != BAD_DROP)
{
if (bad_char_behavior == BAD_KEEP)
{
mch_memmove(dst, src, bytelen);
dst += bytelen;
}
else
*dst++ = bad_char_behavior;
}
}
src += bytelen;
size -= bytelen;
}
if (size > 0)
{
mch_memmove(conv_rest, src, size);
conv_restlen = size;
}
size = (long)(dst - ptr);
}
else
# endif
# ifdef MACOS_CONVERT
if (fio_flags & FIO_MACROMAN)
{
if (macroman2enc(ptr, &size, real_size) == FAIL)
goto rewind_retry;
}
else
# endif
if (fio_flags != 0)
{
int u8c;
char_u *dest;
char_u *tail = NULL;
dest = ptr + real_size;
if (fio_flags == FIO_LATIN1 || fio_flags == FIO_UTF8)
{
p = ptr + size;
if (fio_flags == FIO_UTF8)
{
tail = ptr + size - 1;
while (tail > ptr && (*tail & 0xc0) == 0x80)
--tail;
if (tail + utf_byte2len(*tail) <= ptr + size)
tail = NULL;
else
p = tail;
}
}
else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
{
p = ptr + (size & ~1);
if (size & 1)
tail = p;
if ((fio_flags & FIO_UTF16) && p > ptr)
{
if (fio_flags & FIO_ENDIAN_L)
{
u8c = (*--p << 8);
u8c += *--p;
}
else
{
u8c = *--p;
u8c += (*--p << 8);
}
if (u8c >= 0xd800 && u8c <= 0xdbff)
tail = p;
else
p += 2;
}
}
else
{
p = ptr + (size & ~3);
if (size & 3)
tail = p;
}
if (tail != NULL)
{
conv_restlen = (int)((ptr + size) - tail);
mch_memmove(conv_rest, (char_u *)tail, conv_restlen);
size -= conv_restlen;
}
while (p > ptr)
{
if (fio_flags & FIO_LATIN1)
u8c = *--p;
else if (fio_flags & (FIO_UCS2 | FIO_UTF16))
{
if (fio_flags & FIO_ENDIAN_L)
{
u8c = (*--p << 8);
u8c += *--p;
}
else
{
u8c = *--p;
u8c += (*--p << 8);
}
if ((fio_flags & FIO_UTF16)
&& u8c >= 0xdc00 && u8c <= 0xdfff)
{
int u16c;
if (p == ptr)
{
if (can_retry)
goto rewind_retry;
if (conv_error == 0)
conv_error = readfile_linenr(linecnt,
ptr, p);
if (bad_char_behavior == BAD_DROP)
continue;
if (bad_char_behavior != BAD_KEEP)
u8c = bad_char_behavior;
}
if (fio_flags & FIO_ENDIAN_L)
{
u16c = (*--p << 8);
u16c += *--p;
}
else
{
u16c = *--p;
u16c += (*--p << 8);
}
u8c = 0x10000 + ((u16c & 0x3ff) << 10)
+ (u8c & 0x3ff);
if (u16c < 0xd800 || u16c > 0xdbff)
{
if (can_retry)
goto rewind_retry;
if (conv_error == 0)
conv_error = readfile_linenr(linecnt,
ptr, p);
if (bad_char_behavior == BAD_DROP)
continue;
if (bad_char_behavior != BAD_KEEP)
u8c = bad_char_behavior;
}
}
}
else if (fio_flags & FIO_UCS4)
{
if (fio_flags & FIO_ENDIAN_L)
{
u8c = (*--p << 24);
u8c += (*--p << 16);
u8c += (*--p << 8);
u8c += *--p;
}
else
{
u8c = *--p;
u8c += (*--p << 8);
u8c += (*--p << 16);
u8c += (*--p << 24);
}
}
else
{
if (*--p < 0x80)
u8c = *p;
else
{
len = utf_head_off(ptr, p);
p -= len;
u8c = utf_ptr2char(p);
if (len == 0)
{
if (can_retry)
goto rewind_retry;
if (conv_error == 0)
conv_error = readfile_linenr(linecnt,
ptr, p);
if (bad_char_behavior == BAD_DROP)
continue;
if (bad_char_behavior != BAD_KEEP)
u8c = bad_char_behavior;
}
}
}
if (enc_utf8)
{
dest -= utf_char2len(u8c);
(void)utf_char2bytes(u8c, dest);
}
else
{
--dest;
if (u8c >= 0x100)
{
if (can_retry)
goto rewind_retry;
if (conv_error == 0)
conv_error = readfile_linenr(linecnt, ptr, p);
if (bad_char_behavior == BAD_DROP)
++dest;
else if (bad_char_behavior == BAD_KEEP)
*dest = u8c;
else if (eap != NULL && eap->bad_char != 0)
*dest = bad_char_behavior;
else
*dest = 0xBF;
}
else
*dest = u8c;
}
}
line_start = dest - linerest;
mch_memmove(line_start, buffer, (size_t)linerest);
size = (long)((ptr + real_size) - dest);
ptr = dest;
}
else if (enc_utf8 && !curbuf->b_p_bin)
{
int incomplete_tail = FALSE;
for (p = ptr; ; ++p)
{
int todo = (int)((ptr + size) - p);
int l;
if (todo <= 0)
break;
if (*p >= 0x80)
{
l = utf_ptr2len_len(p, todo);
if (l > todo && !incomplete_tail)
{
if (p > ptr || filesize > 0)
incomplete_tail = TRUE;
if (p > ptr)
{
conv_restlen = todo;
mch_memmove(conv_rest, p, conv_restlen);
size -= conv_restlen;
break;
}
}
if (l == 1 || l > todo)
{
if (can_retry && !incomplete_tail)
break;
# ifdef USE_ICONV
if (iconv_fd != (iconv_t)-1 && conv_error == 0)
conv_error = readfile_linenr(linecnt, ptr, p);
# endif
if (conv_error == 0 && illegal_byte == 0)
illegal_byte = readfile_linenr(linecnt, ptr, p);
if (bad_char_behavior == BAD_DROP)
{
mch_memmove(p, p + 1, todo - 1);
--p;
--size;
}
else if (bad_char_behavior != BAD_KEEP)
*p = bad_char_behavior;
}
else
p += l - 1;
}
}
if (p < ptr + size && !incomplete_tail)
{
rewind_retry:
# if defined(FEAT_EVAL) && defined(USE_ICONV)
if (*p_ccv != NUL && iconv_fd != (iconv_t)-1)
did_iconv = TRUE;
else
# endif
advance_fenc = TRUE;
file_rewind = TRUE;
goto retry;
}
}
#endif
filesize += size;
if (fileformat == EOL_UNKNOWN)
{
if (try_dos || try_unix)
{
for (p = ptr; p < ptr + size; ++p)
{
if (*p == NL)
{
if (!try_unix
|| (try_dos && p > ptr && p[-1] == CAR))
fileformat = EOL_DOS;
else
fileformat = EOL_UNIX;
break;
}
}
if (fileformat == EOL_UNIX && try_mac)
{
try_mac = 1;
try_unix = 1;
for (; p >= ptr && *p != CAR; p--)
;
if (p >= ptr)
{
for (p = ptr; p < ptr + size; ++p)
{
if (*p == NL)
try_unix++;
else if (*p == CAR)
try_mac++;
}
if (try_mac > try_unix)
fileformat = EOL_MAC;
}
}
}
if (fileformat == EOL_UNKNOWN && try_mac)
fileformat = EOL_MAC;
if (fileformat == EOL_UNKNOWN)
fileformat = default_fileformat();
if (set_options)
set_fileformat(fileformat, OPT_LOCAL);
}
}
if (p_bf) {
int i=0;
for (p = ptr; p < ptr + size; ++p) {
if (isprint(*p) || *p == '\t' || *p == '\n' || *p == '\f')
ptr[i++] = *p;
}
size=i;
}
if (fileformat == EOL_MAC)
{
--ptr;
while (++ptr, --size >= 0)
{
if ((c = *ptr) != NUL && c != CAR && c != NL)
continue;
if (c == NUL)
*ptr = NL;
else if (c == NL)
*ptr = CAR;
else
{
if (skip_count == 0)
{
*ptr = NUL;
len = (colnr_T) (ptr - line_start + 1);
if (ml_append(lnum, line_start, len, newfile) == FAIL)
{
error = TRUE;
break;
}
++lnum;
if (--read_count == 0)
{
error = TRUE;
line_start = ptr;
break;
}
}
else
--skip_count;
line_start = ptr + 1;
}
}
}
else
{
--ptr;
while (++ptr, --size >= 0)
{
if ((c = *ptr) != NUL && c != NL)
continue;
if (c == NUL)
*ptr = NL;
else
{
if (skip_count == 0)
{
*ptr = NUL;
len = (colnr_T)(ptr - line_start + 1);
if (fileformat == EOL_DOS)
{
if (ptr[-1] == CAR)
{
ptr[-1] = NUL;
--len;
}
else if (ff_error != EOL_DOS)
{
if ( try_unix
&& !read_stdin
&& (read_buffer
|| lseek(fd, (off_t)0L, SEEK_SET) == 0))
{
fileformat = EOL_UNIX;
if (set_options)
set_fileformat(EOL_UNIX, OPT_LOCAL);
file_rewind = TRUE;
keep_fileformat = TRUE;
goto retry;
}
ff_error = EOL_DOS;
}
}
if (ml_append(lnum, line_start, len, newfile) == FAIL)
{
error = TRUE;
break;
}
++lnum;
if (--read_count == 0)
{
error = TRUE;
line_start = ptr;
break;
}
}
else
--skip_count;
line_start = ptr + 1;
}
}
}
linerest = (long)(ptr - line_start);
ui_breakcheck();
}
failed:
if (error && read_count == 0)
error = FALSE;
if (!error
&& !got_int
&& linerest != 0
&& !(!curbuf->b_p_bin
&& fileformat == EOL_DOS
&& *line_start == Ctrl_Z
&& ptr == line_start + 1))
{
if (set_options)
curbuf->b_p_eol = FALSE;
*ptr = NUL;
if (ml_append(lnum, line_start,
(colnr_T)(ptr - line_start + 1), newfile) == FAIL)
error = TRUE;
else
read_no_eol_lnum = ++lnum;
}
if (set_options)
save_file_ff(curbuf);
#ifdef FEAT_CRYPT
if (cryptkey != curbuf->b_p_key)
vim_free(cryptkey);
#endif
#ifdef FEAT_MBYTE
if (set_options)
set_string_option_direct((char_u *)"fenc", -1, fenc,
OPT_FREE|OPT_LOCAL, 0);
if (fenc_alloced)
vim_free(fenc);
# ifdef USE_ICONV
if (iconv_fd != (iconv_t)-1)
{
iconv_close(iconv_fd);
iconv_fd = (iconv_t)-1;
}
# endif
#endif
if (!read_buffer && !read_stdin)
close(fd);
vim_free(buffer);
#ifdef HAVE_DUP
if (read_stdin)
{
close(0);
ignored = dup(2);
}
#endif
#ifdef FEAT_MBYTE
if (tmpname != NULL)
{
mch_remove(tmpname);
vim_free(tmpname);
}
#endif
--no_wait_return;
if (!recoverymode)
{
if (newfile && wasempty && !(curbuf->b_ml.ml_flags & ML_EMPTY))
{
#ifdef FEAT_NETBEANS_INTG
netbeansFireChanges = 0;
#endif
ml_delete(curbuf->b_ml.ml_line_count, FALSE);
#ifdef FEAT_NETBEANS_INTG
netbeansFireChanges = 1;
#endif
--linecnt;
}
linecnt = curbuf->b_ml.ml_line_count - linecnt;
if (filesize == 0)
linecnt = 0;
if (newfile || read_buffer)
{
redraw_curbuf_later(NOT_VALID);
#ifdef FEAT_DIFF
diff_invalidate(curbuf);
#endif
#ifdef FEAT_FOLDING
foldUpdateAll(curwin);
#endif
}
else if (linecnt)
appended_lines_mark(from, linecnt);
#ifndef ALWAYS_USE_GUI
if (read_stdin)
{
settmode(TMODE_RAW);
starttermcap();
screenclear();
}
#endif
if (got_int)
{
if (!(flags & READ_DUMMY))
{
filemess(curbuf, sfname, (char_u *)_(e_interr), 0);
if (newfile)
curbuf->b_p_ro = TRUE;
}
msg_scroll = msg_save;
#ifdef FEAT_VIMINFO
check_marks_read();
#endif
return OK;
}
if (!filtering && !(flags & READ_DUMMY))
{
msg_add_fname(curbuf, sfname);
c = FALSE;
#ifdef UNIX
# ifdef S_ISFIFO
if (S_ISFIFO(perm))
{
STRCAT(IObuff, _("[fifo/socket]"));
c = TRUE;
}
# else
# ifdef S_IFIFO
if ((perm & S_IFMT) == S_IFIFO)
{
STRCAT(IObuff, _("[fifo]"));
c = TRUE;
}
# endif
# ifdef S_IFSOCK
if ((perm & S_IFMT) == S_IFSOCK)
{
STRCAT(IObuff, _("[socket]"));
c = TRUE;
}
# endif
# endif
# ifdef OPEN_CHR_FILES
if (S_ISCHR(perm))
{
STRCAT(IObuff, _("[character special]"));
c = TRUE;
}
# endif
#endif
if (curbuf->b_p_ro)
{
STRCAT(IObuff, shortmess(SHM_RO) ? _("[RO]") : _("[readonly]"));
c = TRUE;
}
if (read_no_eol_lnum)
{
msg_add_eol();
c = TRUE;
}
if (ff_error == EOL_DOS)
{
STRCAT(IObuff, _("[CR missing]"));
c = TRUE;
}
if (split)
{
STRCAT(IObuff, _("[long lines split]"));
c = TRUE;
}
#ifdef FEAT_MBYTE
if (notconverted)
{
STRCAT(IObuff, _("[NOT converted]"));
c = TRUE;
}
else if (converted)
{
STRCAT(IObuff, _("[converted]"));
c = TRUE;
}
#endif
#ifdef FEAT_CRYPT
if (cryptkey != NULL)
{
STRCAT(IObuff, _("[crypted]"));
c = TRUE;
}
#endif
#ifdef FEAT_MBYTE
if (conv_error != 0)
{
sprintf((char *)IObuff + STRLEN(IObuff),
_("[CONVERSION ERROR in line %ld]"), (long)conv_error);
c = TRUE;
}
else if (illegal_byte > 0)
{
sprintf((char *)IObuff + STRLEN(IObuff),
_("[ILLEGAL BYTE in line %ld]"), (long)illegal_byte);
c = TRUE;
}
else
#endif
if (error)
{
STRCAT(IObuff, _("[READ ERRORS]"));
c = TRUE;
}
if (msg_add_fileformat(fileformat))
c = TRUE;
#ifdef FEAT_CRYPT
if (cryptkey != NULL)
msg_add_lines(c, (long)linecnt, filesize - CRYPT_MAGIC_LEN);
else
#endif
msg_add_lines(c, (long)linecnt, filesize);
vim_free(keep_msg);
keep_msg = NULL;
msg_scrolled_ign = TRUE;
#ifdef ALWAYS_USE_GUI
if (read_stdin || read_buffer)
p = msg_may_trunc(FALSE, IObuff);
else
#endif
p = msg_trunc_attr(IObuff, FALSE, 0);
if (read_stdin || read_buffer || restart_edit != 0
|| (msg_scrolled != 0 && !need_wait_return))
set_keep_msg(p, 0);
msg_scrolled_ign = FALSE;
}
if (newfile && (error
#ifdef FEAT_MBYTE
|| conv_error != 0
|| (illegal_byte > 0 && bad_char_behavior != BAD_KEEP)
#endif
))
curbuf->b_p_ro = TRUE;
u_clearline();
if (exmode_active)
curwin->w_cursor.lnum = from + linecnt;
else
curwin->w_cursor.lnum = from + 1;
check_cursor_lnum();
beginline(BL_WHITE | BL_FIX);
curbuf->b_op_start.lnum = from + 1;
curbuf->b_op_start.col = 0;
curbuf->b_op_end.lnum = from + linecnt;
curbuf->b_op_end.col = 0;
#ifdef WIN32
if (newfile && !read_stdin && !read_buffer
&& mch_stat((char *)fname, &st) >= 0)
{
buf_store_time(curbuf, &st, fname);
curbuf->b_mtime_read = curbuf->b_mtime;
}
#endif
}
msg_scroll = msg_save;
#ifdef FEAT_VIMINFO
check_marks_read();
#endif
write_no_eol_lnum = read_no_eol_lnum;
#ifdef FEAT_AUTOCMD
if (!read_stdin && !read_buffer)
{
int m = msg_scroll;
int n = msg_scrolled;
if (set_options)
save_file_ff(curbuf);
msg_scroll = TRUE;
if (filtering)
apply_autocmds_exarg(EVENT_FILTERREADPOST, NULL, sfname,
FALSE, curbuf, eap);
else if (newfile)
apply_autocmds_exarg(EVENT_BUFREADPOST, NULL, sfname,
FALSE, curbuf, eap);
else
apply_autocmds_exarg(EVENT_FILEREADPOST, sfname, sfname,
FALSE, NULL, eap);
if (msg_scrolled == n)
msg_scroll = m;
#ifdef FEAT_EVAL
if (aborting())
return FAIL;
#endif
}
#endif
if (recoverymode && error)
return FAIL;
return OK;
}
#ifdef OPEN_CHR_FILES
static int
is_dev_fd_file(fname)
char_u *fname;
{
return (STRNCMP(fname, "/dev/fd/", 8) == 0
&& VIM_ISDIGIT(fname[8])
&& *skipdigits(fname + 9) == NUL
&& (fname[9] != NUL
|| (fname[8] != '0' && fname[8] != '1' && fname[8] != '2')));
}
#endif
#ifdef FEAT_MBYTE
static linenr_T
readfile_linenr(linecnt, p, endp)
linenr_T linecnt;
char_u *p;
char_u *endp;
{
char_u *s;
linenr_T lnum;
lnum = curbuf->b_ml.ml_line_count - linecnt + 1;
for (s = p; s < endp; ++s)
if (*s == '\n')
++lnum;
return lnum;
}
#endif
int
prep_exarg(eap, buf)
exarg_T *eap;
buf_T *buf;
{
eap->cmd = alloc((unsigned)(STRLEN(buf->b_p_ff)
#ifdef FEAT_MBYTE
+ STRLEN(buf->b_p_fenc)
#endif
+ 15));
if (eap->cmd == NULL)
return FAIL;
#ifdef FEAT_MBYTE
sprintf((char *)eap->cmd, "e ++ff=%s ++enc=%s", buf->b_p_ff, buf->b_p_fenc);
eap->force_enc = 14 + (int)STRLEN(buf->b_p_ff);
eap->bad_char = buf->b_bad_char;
#else
sprintf((char *)eap->cmd, "e ++ff=%s", buf->b_p_ff);
#endif
eap->force_ff = 7;
eap->force_bin = buf->b_p_bin ? FORCE_BIN : FORCE_NOBIN;
eap->read_edit = FALSE;
eap->forceit = FALSE;
return OK;
}
#ifdef FEAT_MBYTE
static char_u *
next_fenc(pp)
char_u **pp;
{
char_u *p;
char_u *r;
if (**pp == NUL)
{
*pp = NULL;
return (char_u *)"";
}
p = vim_strchr(*pp, ',');
if (p == NULL)
{
r = enc_canonize(*pp);
*pp += STRLEN(*pp);
}
else
{
r = vim_strnsave(*pp, (int)(p - *pp));
*pp = p + 1;
if (r != NULL)
{
p = enc_canonize(r);
vim_free(r);
r = p;
}
}
if (r == NULL)
{
r = (char_u *)"";
*pp = NULL;
}
return r;
}
# ifdef FEAT_EVAL
static char_u *
readfile_charconvert(fname, fenc, fdp)
char_u *fname;
char_u *fenc;
int *fdp;
{
char_u *tmpname;
char_u *errmsg = NULL;
tmpname = vim_tempname('r');
if (tmpname == NULL)
errmsg = (char_u *)_("Can't find temp file for conversion");
else
{
close(*fdp);
*fdp = -1;
if (eval_charconvert(fenc, enc_utf8 ? (char_u *)"utf-8" : p_enc,
fname, tmpname) == FAIL)
errmsg = (char_u *)_("Conversion with 'charconvert' failed");
if (errmsg == NULL && (*fdp = mch_open((char *)tmpname,
O_RDONLY | O_EXTRA, 0)) < 0)
errmsg = (char_u *)_("can't read output of 'charconvert'");
}
if (errmsg != NULL)
{
MSG(errmsg);
if (tmpname != NULL)
{
mch_remove(tmpname);
vim_free(tmpname);
tmpname = NULL;
}
}
if (*fdp < 0)
*fdp = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0);
return tmpname;
}
# endif
#endif
#ifdef FEAT_VIMINFO
static void
check_marks_read()
{
if (!curbuf->b_marks_read && get_viminfo_parameter('\'') > 0
&& curbuf->b_ffname != NULL)
read_viminfo(NULL, VIF_WANT_MARKS);
curbuf->b_marks_read = TRUE;
}
#endif
#ifdef FEAT_CRYPT
static char_u *
check_for_cryptkey(cryptkey, ptr, sizep, filesizep, newfile)
char_u *cryptkey;
char_u *ptr;
long *sizep;
long *filesizep;
int newfile;
{
if (*sizep >= CRYPT_MAGIC_LEN
&& STRNCMP(ptr, CRYPT_MAGIC, CRYPT_MAGIC_LEN) == 0)
{
if (cryptkey == NULL)
{
if (*curbuf->b_p_key)
cryptkey = curbuf->b_p_key;
else
{
cryptkey = get_crypt_key(newfile, FALSE);
if (cryptkey != NULL && *cryptkey == NUL)
{
if (cryptkey != curbuf->b_p_key)
vim_free(cryptkey);
cryptkey = NULL;
}
}
}
if (cryptkey != NULL)
{
crypt_init_keys(cryptkey);
*filesizep += CRYPT_MAGIC_LEN;
*sizep -= CRYPT_MAGIC_LEN;
mch_memmove(ptr, ptr + CRYPT_MAGIC_LEN, (size_t)*sizep);
}
}
else if (newfile && *curbuf->b_p_key && !starting)
set_option_value((char_u *)"key", 0L, (char_u *)"", OPT_LOCAL);
return cryptkey;
}
#endif
#ifdef UNIX
static void
set_file_time(fname, atime, mtime)
char_u *fname;
time_t atime;
time_t mtime;
{
# if defined(HAVE_UTIME) && defined(HAVE_UTIME_H)
struct utimbuf buf;
buf.actime = atime;
buf.modtime = mtime;
(void)utime((char *)fname, &buf);
# else
# if defined(HAVE_UTIMES)
struct timeval tvp[2];
tvp[0].tv_sec = atime;
tvp[0].tv_usec = 0;
tvp[1].tv_sec = mtime;
tvp[1].tv_usec = 0;
# ifdef NeXT
(void)utimes((char *)fname, tvp);
# else
(void)utimes((char *)fname, (const struct timeval *)&tvp);
# endif
# endif
# endif
}
#endif
#if defined(VMS) && !defined(MIN)
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
int
check_file_readonly(fname, perm)
char_u *fname;
int perm;
{
#ifndef USE_MCH_ACCESS
int fd = 0;
#endif
return (
#ifdef USE_MCH_ACCESS
# ifdef UNIX
(perm & 0222) == 0 ||
# endif
mch_access((char *)fname, W_OK)
#else
(fd = mch_open((char *)fname, O_RDWR | O_EXTRA, 0)) < 0
? TRUE : (close(fd), FALSE)
#endif
);
}
int
buf_write(buf, fname, sfname, start, end, eap, append, forceit,
reset_changed, filtering)
buf_T *buf;
char_u *fname;
char_u *sfname;
linenr_T start, end;
exarg_T *eap;
int append;
int forceit;
int reset_changed;
int filtering;
{
int fd;
char_u *backup = NULL;
int backup_copy = FALSE;
int dobackup;
char_u *ffname;
char_u *wfname = NULL;
char_u *s;
char_u *ptr;
char_u c;
int len;
linenr_T lnum;
long nchars;
char_u *errmsg = NULL;
char_u *errnum = NULL;
char_u *buffer;
char_u smallbuf[SMBUFSIZE];
char_u *backup_ext;
int bufsize;
long perm;
int retval = OK;
int newfile = FALSE;
int msg_save = msg_scroll;
int overwriting;
int no_eol = FALSE;
int device = FALSE;
struct stat st_old;
int prev_got_int = got_int;
int file_readonly = FALSE;
static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')";
#if defined(UNIX) || defined(__EMX__XX)
int made_writable = FALSE;
#endif
int whole = (start == 1 && end == buf->b_ml.ml_line_count);
#ifdef FEAT_AUTOCMD
linenr_T old_line_count = buf->b_ml.ml_line_count;
#endif
int attr;
int fileformat;
int write_bin;
struct bw_info write_info;
#ifdef FEAT_MBYTE
int converted = FALSE;
int notconverted = FALSE;
char_u *fenc;
char_u *fenc_tofree = NULL;
#endif
#ifdef HAS_BW_FLAGS
int wb_flags = 0;
#endif
#ifdef HAVE_ACL
vim_acl_T acl = NULL;
#endif
#ifdef HAVE_COPYFILE
copyfile_state_t copyfile_state = NULL;
#endif
if (fname == NULL || *fname == NUL)
return FAIL;
if (check_secure())
return FAIL;
if (STRLEN(fname) >= MAXPATHL)
{
EMSG(_(e_longname));
return FAIL;
}
#ifdef FEAT_MBYTE
write_info.bw_conv_buf = NULL;
write_info.bw_conv_error = FALSE;
write_info.bw_restlen = 0;
# ifdef USE_ICONV
write_info.bw_iconv_fd = (iconv_t)-1;
# endif
#endif
ex_no_reprint = TRUE;
if (buf->b_ffname == NULL
&& reset_changed
&& whole
&& buf == curbuf
#ifdef FEAT_QUICKFIX
&& !bt_nofile(buf)
#endif
&& !filtering
&& (!append || vim_strchr(p_cpo, CPO_FNAMEAPP) != NULL)
&& vim_strchr(p_cpo, CPO_FNAMEW) != NULL)
{
if (set_rw_fname(fname, sfname) == FAIL)
return FAIL;
buf = curbuf;
}
if (sfname == NULL)
sfname = fname;
ffname = fname;
#ifdef UNIX
fname = sfname;
#endif
if (buf->b_ffname != NULL && fnamecmp(ffname, buf->b_ffname) == 0)
overwriting = TRUE;
else
overwriting = FALSE;
if (exiting)
settmode(TMODE_COOK);
++no_wait_return;
buf->b_op_start.lnum = start;
buf->b_op_start.col = 0;
buf->b_op_end.lnum = end;
buf->b_op_end.col = 0;
#ifdef FEAT_AUTOCMD
{
aco_save_T aco;
int buf_ffname = FALSE;
int buf_sfname = FALSE;
int buf_fname_f = FALSE;
int buf_fname_s = FALSE;
int did_cmd = FALSE;
int nofile_err = FALSE;
int empty_memline = (buf->b_ml.ml_mfp == NULL);
if (ffname == buf->b_ffname)
buf_ffname = TRUE;
if (sfname == buf->b_sfname)
buf_sfname = TRUE;
if (fname == buf->b_ffname)
buf_fname_f = TRUE;
if (fname == buf->b_sfname)
buf_fname_s = TRUE;
aucmd_prepbuf(&aco, buf);
if (append)
{
if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD,
sfname, sfname, FALSE, curbuf, eap)))
{
#ifdef FEAT_QUICKFIX
if (overwriting && bt_nofile(curbuf))
nofile_err = TRUE;
else
#endif
apply_autocmds_exarg(EVENT_FILEAPPENDPRE,
sfname, sfname, FALSE, curbuf, eap);
}
}
else if (filtering)
{
apply_autocmds_exarg(EVENT_FILTERWRITEPRE,
NULL, sfname, FALSE, curbuf, eap);
}
else if (reset_changed && whole)
{
if (!(did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD,
sfname, sfname, FALSE, curbuf, eap)))
{
#ifdef FEAT_QUICKFIX
if (overwriting && bt_nofile(curbuf))
nofile_err = TRUE;
else
#endif
apply_autocmds_exarg(EVENT_BUFWRITEPRE,
sfname, sfname, FALSE, curbuf, eap);
}
}
else
{
if (!(did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD,
sfname, sfname, FALSE, curbuf, eap)))
{
#ifdef FEAT_QUICKFIX
if (overwriting && bt_nofile(curbuf))
nofile_err = TRUE;
else
#endif
apply_autocmds_exarg(EVENT_FILEWRITEPRE,
sfname, sfname, FALSE, curbuf, eap);
}
}
aucmd_restbuf(&aco);
if (!buf_valid(buf))
buf = NULL;
if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline)
|| did_cmd || nofile_err
#ifdef FEAT_EVAL
|| aborting()
#endif
)
{
--no_wait_return;
msg_scroll = msg_save;
if (nofile_err)
EMSG(_("E676: No matching autocommands for acwrite buffer"));
if (nofile_err
#ifdef FEAT_EVAL
|| aborting()
#endif
)
return FAIL;
if (did_cmd)
{
if (buf == NULL)
return OK;
if (overwriting)
{
ml_timestamp(buf);
if (append)
buf->b_flags &= ~BF_NEW;
else
buf->b_flags &= ~BF_WRITE_MASK;
}
if (reset_changed && buf->b_changed && !append
&& (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
return FAIL;
return OK;
}
#ifdef FEAT_EVAL
if (!aborting())
#endif
EMSG(_("E203: Autocommands deleted or unloaded buffer to be written"));
return FAIL;
}
if (buf->b_ml.ml_line_count != old_line_count)
{
if (whole)
end = buf->b_ml.ml_line_count;
else if (buf->b_ml.ml_line_count > old_line_count)
end += buf->b_ml.ml_line_count - old_line_count;
else
{
end -= old_line_count - buf->b_ml.ml_line_count;
if (end < start)
{
--no_wait_return;
msg_scroll = msg_save;
EMSG(_("E204: Autocommand changed number of lines in unexpected way"));
return FAIL;
}
}
}
if (buf_ffname)
ffname = buf->b_ffname;
if (buf_sfname)
sfname = buf->b_sfname;
if (buf_fname_f)
fname = buf->b_ffname;
if (buf_fname_s)
fname = buf->b_sfname;
}
#endif
#ifdef FEAT_NETBEANS_INTG
if (usingNetbeans && isNetbeansBuffer(buf))
{
if (whole)
{
if (buf->b_changed || isNetbeansModified(buf))
{
--no_wait_return;
msg_scroll = msg_save;
netbeans_save_buffer(buf);
return retval;
}
else
{
errnum = (char_u *)"E656: ";
errmsg = (char_u *)_("NetBeans disallows writes of unmodified buffers");
buffer = NULL;
goto fail;
}
}
else
{
errnum = (char_u *)"E657: ";
errmsg = (char_u *)_("Partial writes disallowed for NetBeans buffers");
buffer = NULL;
goto fail;
}
}
#endif
if (shortmess(SHM_OVER) && !exiting)
msg_scroll = FALSE;
else
msg_scroll = TRUE;
if (!filtering)
filemess(buf,
#ifndef UNIX
sfname,
#else
fname,
#endif
(char_u *)"", 0);
msg_scroll = FALSE;
buffer = alloc(BUFSIZE);
if (buffer == NULL)
{
buffer = smallbuf;
bufsize = SMBUFSIZE;
}
else
bufsize = BUFSIZE;
#if defined(UNIX) && !defined(ARCHIE)
st_old.st_dev = 0;
st_old.st_ino = 0;
perm = -1;
if (mch_stat((char *)fname, &st_old) < 0)
newfile = TRUE;
else
{
perm = st_old.st_mode;
if (!S_ISREG(st_old.st_mode))
{
if (S_ISDIR(st_old.st_mode))
{
errnum = (char_u *)"E502: ";
errmsg = (char_u *)_("is a directory");
goto fail;
}
if (mch_nodetype(fname) != NODE_WRITABLE)
{
errnum = (char_u *)"E503: ";
errmsg = (char_u *)_("is not a file or writable device");
goto fail;
}
device = TRUE;
newfile = TRUE;
perm = -1;
}
}
#else
c = mch_nodetype(fname);
if (c == NODE_OTHER)
{
errnum = (char_u *)"E503: ";
errmsg = (char_u *)_("is not a file or writable device");
goto fail;
}
if (c == NODE_WRITABLE)
{
# if defined(MSDOS) || defined(MSWIN) || defined(OS2)
if (!p_odev)
{
errnum = (char_u *)"E796: ";
errmsg = (char_u *)_("writing to device disabled with 'opendevice' option");
goto fail;
}
# endif
device = TRUE;
newfile = TRUE;
perm = -1;
}
else
{
perm = mch_getperm(fname);
if (perm < 0)
newfile = TRUE;
else if (mch_isdir(fname))
{
errnum = (char_u *)"E502: ";
errmsg = (char_u *)_("is a directory");
goto fail;
}
if (overwriting)
(void)mch_stat((char *)fname, &st_old);
}
#endif
if (!device && !newfile)
{
file_readonly = check_file_readonly(fname, (int)perm);
if (!forceit && file_readonly)
{
if (vim_strchr(p_cpo, CPO_FWRITE) != NULL)
{
errnum = (char_u *)"E504: ";
errmsg = (char_u *)_(err_readonly);
}
else
{
errnum = (char_u *)"E505: ";
errmsg = (char_u *)_("is read-only (add ! to override)");
}
goto fail;
}
if (overwriting)
{
retval = check_mtime(buf, &st_old);
if (retval == FAIL)
goto fail;
}
}
#ifdef HAVE_ACL
if (!newfile)
acl = mch_get_acl(fname);
#endif
#ifdef HAVE_COPYFILE
if (!newfile && copyfile((char*)fname, NULL, 0, COPYFILE_XATTR | COPYFILE_CHECK))
{
copyfile_state = copyfile_state_alloc();
copyfile((char*)fname, NULL, copyfile_state, 0);
}
#endif
dobackup = (p_wb || p_bk || *p_pm != NUL);
#ifdef FEAT_WILDIGN
if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname))
dobackup = FALSE;
#endif
prev_got_int = got_int;
got_int = FALSE;
buf->b_saving = TRUE;
if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup)
{
#if defined(UNIX) || defined(WIN32)
struct stat st;
#endif
if ((bkc_flags & BKC_YES) || append)
backup_copy = TRUE;
#if defined(UNIX) || defined(WIN32)
else if ((bkc_flags & BKC_AUTO))
{
int i;
# ifdef UNIX
if (st_old.st_nlink > 1
|| mch_lstat((char *)fname, &st) < 0
|| st.st_dev != st_old.st_dev
|| st.st_ino != st_old.st_ino
# ifndef HAVE_FCHOWN
|| st.st_uid != st_old.st_uid
|| st.st_gid != st_old.st_gid
# endif
)
backup_copy = TRUE;
else
# else
# ifdef WIN32
if (mch_is_linked(fname))
backup_copy = TRUE;
else
# endif
# endif
{
STRCPY(IObuff, fname);
for (i = 4913; ; i += 123)
{
sprintf((char *)gettail(IObuff), "%d", i);
if (mch_lstat((char *)IObuff, &st) < 0)
break;
}
fd = mch_open((char *)IObuff,
O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm);
if (fd < 0)
backup_copy = TRUE;
else
{
# ifdef UNIX
# ifdef HAVE_FCHOWN
ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
# endif
if (mch_stat((char *)IObuff, &st) < 0
|| st.st_uid != st_old.st_uid
|| st.st_gid != st_old.st_gid
|| st.st_mode != perm)
backup_copy = TRUE;
# endif
close(fd);
mch_remove(IObuff);
}
}
}
# ifdef UNIX
if ((bkc_flags & BKC_BREAKSYMLINK) || (bkc_flags & BKC_BREAKHARDLINK))
{
int lstat_res;
lstat_res = mch_lstat((char *)fname, &st);
if ((bkc_flags & BKC_BREAKSYMLINK)
&& lstat_res == 0
&& st.st_ino != st_old.st_ino)
backup_copy = FALSE;
if ((bkc_flags & BKC_BREAKHARDLINK)
&& st_old.st_nlink > 1
&& (lstat_res != 0 || st.st_ino == st_old.st_ino))
backup_copy = FALSE;
}
#endif
#endif
if (*p_bex == NUL)
{
#ifdef RISCOS
backup_ext = (char_u *)"/bak";
#else
backup_ext = (char_u *)".bak";
#endif
}
else
backup_ext = p_bex;
if (backup_copy
&& (fd = mch_open((char *)fname, O_RDONLY | O_EXTRA, 0)) >= 0)
{
int bfd;
char_u *copybuf, *wp;
int some_error = FALSE;
struct stat st_new;
char_u *dirp;
char_u *rootname;
#if defined(UNIX) && !defined(SHORT_FNAME)
int did_set_shortname;
#endif
copybuf = alloc(BUFSIZE + 1);
if (copybuf == NULL)
{
some_error = TRUE;
goto nobackup;
}
dirp = p_bdir;
while (*dirp)
{
#ifdef UNIX
st_new.st_ino = 0;
st_new.st_dev = 0;
st_new.st_gid = 0;
#endif
(void)copy_option_part(&dirp, copybuf, BUFSIZE, ",");
rootname = get_file_in_dir(fname, copybuf);
if (rootname == NULL)
{
some_error = TRUE;
goto nobackup;
}
#if defined(UNIX) && !defined(SHORT_FNAME)
did_set_shortname = FALSE;
#endif
for (;;)
{
backup = buf_modname(
#ifdef SHORT_FNAME
TRUE,
#else
(buf->b_p_sn || buf->b_shortname),
#endif
rootname, backup_ext, FALSE);
if (backup == NULL)
{
vim_free(rootname);
some_error = TRUE;
goto nobackup;
}
if (mch_stat((char *)backup, &st_new) >= 0)
{
#ifdef UNIX
if (st_new.st_dev == st_old.st_dev
&& st_new.st_ino == st_old.st_ino)
{
vim_free(backup);
backup = NULL;
# ifndef SHORT_FNAME
if (!(buf->b_shortname || buf->b_p_sn))
{
buf->b_shortname = TRUE;
did_set_shortname = TRUE;
continue;
}
if (did_set_shortname)
buf->b_shortname = FALSE;
# endif
break;
}
#endif
if (!p_bk)
{
wp = backup + STRLEN(backup) - 1
- STRLEN(backup_ext);
if (wp < backup)
wp = backup;
*wp = 'z';
while (*wp > 'a'
&& mch_stat((char *)backup, &st_new) >= 0)
--*wp;
if (*wp == 'a')
{
vim_free(backup);
backup = NULL;
}
}
}
break;
}
vim_free(rootname);
if (backup != NULL)
{
mch_remove(backup);
bfd = mch_open((char *)backup,
O_WRONLY|O_CREAT|O_EXTRA|O_EXCL|O_NOFOLLOW,
perm & 0777);
if (bfd < 0)
{
vim_free(backup);
backup = NULL;
}
else
{
(void)mch_setperm(backup, perm & 0777);
#ifdef UNIX
if (st_new.st_gid != st_old.st_gid
# ifdef HAVE_FCHOWN
&& fchown(bfd, (uid_t)-1, st_old.st_gid) != 0
# endif
)
mch_setperm(backup,
(perm & 0707) | ((perm & 07) << 3));
# ifdef HAVE_SELINUX
mch_copy_sec(fname, backup);
# endif
#endif
write_info.bw_fd = bfd;
write_info.bw_buf = copybuf;
#ifdef HAS_BW_FLAGS
write_info.bw_flags = FIO_NOCONVERT;
#endif
while ((write_info.bw_len = vim_read(fd, copybuf,
BUFSIZE)) > 0)
{
if (buf_write_bytes(&write_info) == FAIL)
{
errmsg = (char_u *)_("E506: Can't write to backup file (add ! to override)");
break;
}
ui_breakcheck();
if (got_int)
{
errmsg = (char_u *)_(e_interr);
break;
}
}
if (close(bfd) < 0 && errmsg == NULL)
errmsg = (char_u *)_("E507: Close error for backup file (add ! to override)");
if (write_info.bw_len < 0)
errmsg = (char_u *)_("E508: Can't read file for backup (add ! to override)");
#ifdef UNIX
set_file_time(backup, st_old.st_atime, st_old.st_mtime);
#endif
#ifdef HAVE_ACL
mch_set_acl(backup, acl);
#endif
#ifdef HAVE_COPYFILE
if (copyfile_state)
copyfile(NULL, (char*)backup, copyfile_state, COPYFILE_XATTR);
#endif
#ifdef HAVE_SELINUX
mch_copy_sec(fname, backup);
#endif
break;
}
}
}
nobackup:
close(fd);
vim_free(copybuf);
if (backup == NULL && errmsg == NULL)
errmsg = (char_u *)_("E509: Cannot create backup file (add ! to override)");
if ((some_error || errmsg != NULL) && !forceit)
{
retval = FAIL;
goto fail;
}
errmsg = NULL;
}
else
{
char_u *dirp;
char_u *p;
char_u *rootname;
if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL)
{
errnum = (char_u *)"E504: ";
errmsg = (char_u *)_(err_readonly);
goto fail;
}
dirp = p_bdir;
while (*dirp)
{
(void)copy_option_part(&dirp, IObuff, IOSIZE, ",");
rootname = get_file_in_dir(fname, IObuff);
if (rootname == NULL)
backup = NULL;
else
{
backup = buf_modname(
#ifdef SHORT_FNAME
TRUE,
#else
(buf->b_p_sn || buf->b_shortname),
#endif
rootname, backup_ext, FALSE);
vim_free(rootname);
}
if (backup != NULL)
{
if (!p_bk && mch_getperm(backup) >= 0)
{
p = backup + STRLEN(backup) - 1 - STRLEN(backup_ext);
if (p < backup)
p = backup;
*p = 'z';
while (*p > 'a' && mch_getperm(backup) >= 0)
--*p;
if (*p == 'a')
{
vim_free(backup);
backup = NULL;
}
}
}
if (backup != NULL)
{
if (vim_rename(fname, backup) == 0)
break;
vim_free(backup);
backup = NULL;
}
}
if (backup == NULL && !forceit)
{
errmsg = (char_u *)_("E510: Can't make backup file (add ! to override)");
goto fail;
}
}
}
#if defined(UNIX) && !defined(ARCHIE)
if (forceit && perm >= 0 && !(perm & 0200) && st_old.st_uid == getuid()
&& vim_strchr(p_cpo, CPO_FWRITE) == NULL)
{
perm |= 0200;
(void)mch_setperm(fname, perm);
made_writable = TRUE;
}
#endif
if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL)
{
buf->b_p_ro = FALSE;
#ifdef FEAT_TITLE
need_maketitle = TRUE;
#endif
#ifdef FEAT_WINDOWS
status_redraw_all();
#endif
}
if (end > buf->b_ml.ml_line_count)
end = buf->b_ml.ml_line_count;
if (buf->b_ml.ml_flags & ML_EMPTY)
start = end + 1;
if (reset_changed && !newfile && overwriting
&& !(exiting && backup != NULL))
{
ml_preserve(buf, FALSE);
if (got_int)
{
errmsg = (char_u *)_(e_interr);
goto restore_backup;
}
}
#ifdef MACOS_CLASSIC
if (backup == NULL && overwriting && !append)
if (mch_has_resource_fork(fname))
{
errmsg = (char_u *)_("E460: The resource fork would be lost (add ! to override)");
goto restore_backup;
}
#endif
#ifdef VMS
vms_remove_version(fname);
#endif
wfname = fname;
#ifdef FEAT_MBYTE
if (eap != NULL && eap->force_enc != 0)
{
fenc = eap->cmd + eap->force_enc;
fenc = enc_canonize(fenc);
fenc_tofree = fenc;
}
else
fenc = buf->b_p_fenc;
converted = (*fenc != NUL && !same_encoding(p_enc, fenc));
if (converted && (enc_utf8 || STRCMP(p_enc, "latin1") == 0))
{
wb_flags = get_fio_flags(fenc);
if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8))
{
if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8))
write_info.bw_conv_buflen = bufsize * 2;
else
write_info.bw_conv_buflen = bufsize * 4;
write_info.bw_conv_buf
= lalloc((long_u)write_info.bw_conv_buflen, TRUE);
if (write_info.bw_conv_buf == NULL)
end = 0;
}
}
# ifdef WIN3264
if (converted && wb_flags == 0 && (wb_flags = get_win_fio_flags(fenc)) != 0)
{
write_info.bw_conv_buflen = bufsize * 4;
write_info.bw_conv_buf
= lalloc((long_u)write_info.bw_conv_buflen, TRUE);
if (write_info.bw_conv_buf == NULL)
end = 0;
}
# endif
# ifdef MACOS_X
if (converted && wb_flags == 0 && (wb_flags = get_mac_fio_flags(fenc)) != 0)
{
write_info.bw_conv_buflen = bufsize * 3;
write_info.bw_conv_buf
= lalloc((long_u)write_info.bw_conv_buflen, TRUE);
if (write_info.bw_conv_buf == NULL)
end = 0;
}
# endif
# if defined(FEAT_EVAL) || defined(USE_ICONV)
if (converted && wb_flags == 0)
{
# ifdef USE_ICONV
write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc,
enc_utf8 ? (char_u *)"utf-8" : p_enc);
if (write_info.bw_iconv_fd != (iconv_t)-1)
{
write_info.bw_conv_buflen = bufsize * ICONV_MULT;
write_info.bw_conv_buf
= lalloc((long_u)write_info.bw_conv_buflen, TRUE);
if (write_info.bw_conv_buf == NULL)
end = 0;
write_info.bw_first = TRUE;
}
# ifdef FEAT_EVAL
else
# endif
# endif
# ifdef FEAT_EVAL
if (*p_ccv != NUL)
{
wfname = vim_tempname('w');
if (wfname == NULL)
{
errmsg = (char_u *)_("E214: Can't find temp file for writing");
goto restore_backup;
}
}
# endif
}
# endif
if (converted && wb_flags == 0
# ifdef USE_ICONV
&& write_info.bw_iconv_fd == (iconv_t)-1
# endif
# ifdef FEAT_EVAL
&& wfname == fname
# endif
)
{
if (!forceit)
{
errmsg = (char_u *)_("E213: Cannot convert (add ! to write without conversion)");
goto restore_backup;
}
notconverted = TRUE;
}
#endif
while ((fd = mch_open((char *)wfname, O_WRONLY | O_EXTRA | (append
? (forceit ? (O_APPEND | O_CREAT) : O_APPEND)
: (O_CREAT | O_TRUNC))
, perm < 0 ? 0666 : (perm & 0777))) < 0)
{
if (errmsg == NULL)
{
#ifdef UNIX
struct stat st;
if ((!newfile && st_old.st_nlink > 1)
|| (mch_lstat((char *)fname, &st) == 0
&& (st.st_dev != st_old.st_dev
|| st.st_ino != st_old.st_ino)))
errmsg = (char_u *)_("E166: Can't open linked file for writing");
else
#endif
{
errmsg = (char_u *)_("E212: Can't open file for writing");
if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL
&& perm >= 0)
{
#ifdef UNIX
if (!(perm & 0200))
made_writable = TRUE;
perm |= 0200;
if (st_old.st_uid != getuid() || st_old.st_gid != getgid())
perm &= 0777;
#endif
if (!append)
mch_remove(wfname);
continue;
}
}
}
restore_backup:
{
struct stat st;
if (backup != NULL && wfname == fname)
{
if (backup_copy)
{
if (mch_stat((char *)fname, &st) < 0)
vim_rename(backup, fname);
if (mch_stat((char *)fname, &st) >= 0)
mch_remove(backup);
}
else
{
vim_rename(backup, fname);
}
}
if (!newfile && mch_stat((char *)fname, &st) < 0)
end = 0;
}
#ifdef FEAT_MBYTE
if (wfname != fname)
vim_free(wfname);
#endif
goto fail;
}
errmsg = NULL;
#if defined(MACOS_CLASSIC) || defined(WIN3264)
if (backup != NULL && overwriting && !append)
{
if (backup_copy)
(void)mch_copy_file_attribute(wfname, backup);
else
(void)mch_copy_file_attribute(backup, wfname);
}
if (!overwriting && !append)
{
if (buf->b_ffname != NULL)
(void)mch_copy_file_attribute(buf->b_ffname, wfname);
}
#endif
write_info.bw_fd = fd;
#ifdef FEAT_CRYPT
if (*buf->b_p_key && !filtering)
{
crypt_init_keys(buf->b_p_key);
write_info.bw_buf = (char_u *)CRYPT_MAGIC;
write_info.bw_len = CRYPT_MAGIC_LEN;
write_info.bw_flags = FIO_NOCONVERT;
if (buf_write_bytes(&write_info) == FAIL)
end = 0;
wb_flags |= FIO_ENCRYPTED;
}
#endif
write_info.bw_buf = buffer;
nchars = 0;
if (eap != NULL && eap->force_bin != 0)
write_bin = (eap->force_bin == FORCE_BIN);
else
write_bin = buf->b_p_bin;
#ifdef FEAT_MBYTE
if (buf->b_p_bomb && !write_bin && (!append || perm < 0))
{
write_info.bw_len = make_bom(buffer, fenc);
if (write_info.bw_len > 0)
{
write_info.bw_flags = FIO_NOCONVERT | wb_flags;
if (buf_write_bytes(&write_info) == FAIL)
end = 0;
else
nchars += write_info.bw_len;
}
}
#endif
write_info.bw_len = bufsize;
#ifdef HAS_BW_FLAGS
write_info.bw_flags = wb_flags;
#endif
fileformat = get_fileformat_force(buf, eap);
s = buffer;
len = 0;
for (lnum = start; lnum <= end; ++lnum)
{
ptr = ml_get_buf(buf, lnum, FALSE) - 1;
while ((c = *++ptr) != NUL)
{
if (c == NL)
*s = NUL;
else if (c == CAR && fileformat == EOL_MAC)
*s = NL;
else
*s = c;
++s;
if (++len != bufsize)
continue;
if (buf_write_bytes(&write_info) == FAIL)
{
end = 0;
break;
}
nchars += bufsize;
s = buffer;
len = 0;
}
if (end == 0
|| (lnum == end
&& write_bin
&& (lnum == write_no_eol_lnum
|| (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol))))
{
++lnum;
no_eol = TRUE;
break;
}
if (fileformat == EOL_UNIX)
*s++ = NL;
else
{
*s++ = CAR;
if (fileformat == EOL_DOS)
{
if (++len == bufsize)
{
if (buf_write_bytes(&write_info) == FAIL)
{
end = 0;
break;
}
nchars += bufsize;
s = buffer;
len = 0;
}
*s++ = NL;
}
}
if (++len == bufsize && end)
{
if (buf_write_bytes(&write_info) == FAIL)
{
end = 0;
break;
}
nchars += bufsize;
s = buffer;
len = 0;
ui_breakcheck();
if (got_int)
{
end = 0;
break;
}
}
#ifdef VMS
if (buf->b_fab_rfm == FAB$C_VFC
|| ((buf->b_fab_rat & (FAB$M_FTN | FAB$M_CR)) != 0))
{
int b2write;
buf->b_fab_mrs = (buf->b_fab_mrs == 0
? MIN(4096, bufsize)
: MIN(buf->b_fab_mrs, bufsize));
b2write = len;
while (b2write > 0)
{
write_info.bw_len = MIN(b2write, buf->b_fab_mrs);
if (buf_write_bytes(&write_info) == FAIL)
{
end = 0;
break;
}
b2write -= MIN(b2write, buf->b_fab_mrs);
}
write_info.bw_len = bufsize;
nchars += len;
s = buffer;
len = 0;
}
#endif
}
if (len > 0 && end > 0)
{
write_info.bw_len = len;
if (buf_write_bytes(&write_info) == FAIL)
end = 0;
nchars += len;
}
#if defined(UNIX) && defined(HAVE_FSYNC)
if (p_fs && fsync(fd) != 0 && !device)
{
errmsg = (char_u *)_("E667: Fsync failed");
end = 0;
}
#endif
#ifdef HAVE_SELINUX
if (!backup_copy)
mch_copy_sec(backup, wfname);
#endif
#ifdef UNIX
if (backup != NULL && !backup_copy)
{
# ifdef HAVE_FCHOWN
struct stat st;
if (mch_stat((char *)wfname, &st) < 0
|| st.st_uid != st_old.st_uid
|| st.st_gid != st_old.st_gid)
{
ignored = fchown(fd, st_old.st_uid, st_old.st_gid);
if (perm >= 0)
(void)mch_setperm(wfname, perm);
}
# endif
buf_setino(buf);
}
else if (buf->b_dev < 0)
buf_setino(buf);
#endif
if (close(fd) != 0)
{
errmsg = (char_u *)_("E512: Close failed");
end = 0;
}
#ifdef UNIX
if (made_writable)
perm &= ~0200;
#endif
if (perm >= 0)
(void)mch_setperm(wfname, perm);
#ifdef RISCOS
if (!append && !filtering)
mch_set_filetype(wfname, buf->b_p_oft);
#endif
#ifdef HAVE_ACL
if (!backup_copy)
mch_set_acl(wfname, acl);
#endif
#ifdef HAVE_COPYFILE
if (!backup_copy && copyfile_state)
copyfile(NULL, (char*)wfname, copyfile_state, COPYFILE_XATTR);
#endif
#if defined(FEAT_MBYTE) && defined(FEAT_EVAL)
if (wfname != fname)
{
if (end != 0)
{
if (eval_charconvert(enc_utf8 ? (char_u *)"utf-8" : p_enc, fenc,
wfname, fname) == FAIL)
{
write_info.bw_conv_error = TRUE;
end = 0;
}
}
mch_remove(wfname);
vim_free(wfname);
}
#endif
if (end == 0)
{
if (errmsg == NULL)
{
#ifdef FEAT_MBYTE
if (write_info.bw_conv_error)
errmsg = (char_u *)_("E513: write error, conversion failed (make 'fenc' empty to override)");
else
#endif
if (got_int)
errmsg = (char_u *)_(e_interr);
else
errmsg = (char_u *)_("E514: write error (file system full?)");
}
if (backup != NULL)
{
if (backup_copy)
{
if (got_int)
{
MSG(_(e_interr));
out_flush();
}
if ((fd = mch_open((char *)backup, O_RDONLY | O_EXTRA, 0)) >= 0)
{
if ((write_info.bw_fd = mch_open((char *)fname,
O_WRONLY | O_CREAT | O_TRUNC | O_EXTRA,
perm & 0777)) >= 0)
{
write_info.bw_buf = smallbuf;
#ifdef HAS_BW_FLAGS
write_info.bw_flags = FIO_NOCONVERT;
#endif
while ((write_info.bw_len = vim_read(fd, smallbuf,
SMBUFSIZE)) > 0)
if (buf_write_bytes(&write_info) == FAIL)
break;
if (close(write_info.bw_fd) >= 0
&& write_info.bw_len == 0)
end = 1;
}
close(fd);
}
}
else
{
if (vim_rename(backup, fname) == 0)
end = 1;
}
}
goto fail;
}
lnum -= start;
--no_wait_return;
#if !(defined(UNIX) || defined(VMS))
fname = sfname;
#endif
if (!filtering)
{
msg_add_fname(buf, fname);
c = FALSE;
#ifdef FEAT_MBYTE
if (write_info.bw_conv_error)
{
STRCAT(IObuff, _(" CONVERSION ERROR"));
c = TRUE;
}
else if (notconverted)
{
STRCAT(IObuff, _("[NOT converted]"));
c = TRUE;
}
else if (converted)
{
STRCAT(IObuff, _("[converted]"));
c = TRUE;
}
#endif
if (device)
{
STRCAT(IObuff, _("[Device]"));
c = TRUE;
}
else if (newfile)
{
STRCAT(IObuff, shortmess(SHM_NEW) ? _("[New]") : _("[New File]"));
c = TRUE;
}
if (no_eol)
{
msg_add_eol();
c = TRUE;
}
if (msg_add_fileformat(fileformat))
c = TRUE;
#ifdef FEAT_CRYPT
if (wb_flags & FIO_ENCRYPTED)
{
STRCAT(IObuff, _("[crypted]"));
c = TRUE;
}
#endif
msg_add_lines(c, (long)lnum, nchars);
if (!shortmess(SHM_WRITE))
{
if (append)
STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [a]") : _(" appended"));
else
STRCAT(IObuff, shortmess(SHM_WRI) ? _(" [w]") : _(" written"));
}
set_keep_msg(msg_trunc_attr(IObuff, FALSE, 0), 0);
}
if (reset_changed && whole && !append
#ifdef FEAT_MBYTE
&& !write_info.bw_conv_error
#endif
&& (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)
)
{
unchanged(buf, TRUE);
u_unchanged(buf);
}
if (overwriting)
{
ml_timestamp(buf);
if (append)
buf->b_flags &= ~BF_NEW;
else
buf->b_flags &= ~BF_WRITE_MASK;
}
if (*p_pm && dobackup)
{
char *org = (char *)buf_modname(
#ifdef SHORT_FNAME
TRUE,
#else
(buf->b_p_sn || buf->b_shortname),
#endif
fname, p_pm, FALSE);
if (backup != NULL)
{
struct stat st;
if (org == NULL)
EMSG(_("E205: Patchmode: can't save original file"));
else if (mch_stat(org, &st) < 0)
{
vim_rename(backup, (char_u *)org);
vim_free(backup);
backup = NULL;
#ifdef UNIX
set_file_time((char_u *)org, st_old.st_atime, st_old.st_mtime);
#endif
}
}
else
{
int empty_fd;
if (org == NULL
|| (empty_fd = mch_open(org,
O_CREAT | O_EXTRA | O_EXCL | O_NOFOLLOW,
perm < 0 ? 0666 : (perm & 0777))) < 0)
EMSG(_("E206: patchmode: can't touch empty original file"));
else
close(empty_fd);
}
if (org != NULL)
{
mch_setperm((char_u *)org, mch_getperm(fname) & 0777);
vim_free(org);
}
}
if (!p_bk && backup != NULL && mch_remove(backup) != 0)
EMSG(_("E207: Can't delete backup file"));
#ifdef FEAT_SUN_WORKSHOP
if (usingSunWorkShop)
workshop_file_saved((char *) ffname);
#endif
goto nofail;
fail:
--no_wait_return;
nofail:
buf->b_saving = FALSE;
vim_free(backup);
if (buffer != smallbuf)
vim_free(buffer);
#ifdef FEAT_MBYTE
vim_free(fenc_tofree);
vim_free(write_info.bw_conv_buf);
# ifdef USE_ICONV
if (write_info.bw_iconv_fd != (iconv_t)-1)
{
iconv_close(write_info.bw_iconv_fd);
write_info.bw_iconv_fd = (iconv_t)-1;
}
# endif
#endif
#ifdef HAVE_ACL
mch_free_acl(acl);
#endif
#ifdef HAVE_COPYFILE
if (copyfile_state)
copyfile_state_free(copyfile_state);
#endif
if (errmsg != NULL)
{
int numlen = errnum != NULL ? (int)STRLEN(errnum) : 0;
attr = hl_attr(HLF_E);
msg_add_fname(buf,
#ifndef UNIX
sfname
#else
fname
#endif
);
if (STRLEN(IObuff) + STRLEN(errmsg) + numlen >= IOSIZE)
IObuff[IOSIZE - STRLEN(errmsg) - numlen - 1] = NUL;
if (errnum != NULL)
{
STRMOVE(IObuff + numlen, IObuff);
mch_memmove(IObuff, errnum, (size_t)numlen);
}
STRCAT(IObuff, errmsg);
emsg(IObuff);
retval = FAIL;
if (end == 0)
{
MSG_PUTS_ATTR(_("\nWARNING: Original file may be lost or damaged\n"),
attr | MSG_HIST);
MSG_PUTS_ATTR(_("don't quit the editor until the file is successfully written!"),
attr | MSG_HIST);
if (mch_stat((char *)fname, &st_old) >= 0)
{
buf_store_time(buf, &st_old, fname);
buf->b_mtime_read = buf->b_mtime;
}
}
}
msg_scroll = msg_save;
#ifdef FEAT_AUTOCMD
#ifdef FEAT_EVAL
if (!should_abort(retval))
#else
if (!got_int)
#endif
{
aco_save_T aco;
write_no_eol_lnum = 0;
aucmd_prepbuf(&aco, buf);
if (append)
apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname,
FALSE, curbuf, eap);
else if (filtering)
apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname,
FALSE, curbuf, eap);
else if (reset_changed && whole)
apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname,
FALSE, curbuf, eap);
else
apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname,
FALSE, curbuf, eap);
aucmd_restbuf(&aco);
#ifdef FEAT_EVAL
if (aborting())
retval = FALSE;
#endif
}
#endif
got_int |= prev_got_int;
#ifdef MACOS_CLASSIC
mch_post_buffer_write(buf);
#endif
return retval;
}
static int
set_rw_fname(fname, sfname)
char_u *fname;
char_u *sfname;
{
#ifdef FEAT_AUTOCMD
if (curbuf->b_p_bl)
apply_autocmds(EVENT_BUFDELETE, NULL, NULL, FALSE, curbuf);
apply_autocmds(EVENT_BUFWIPEOUT, NULL, NULL, FALSE, curbuf);
# ifdef FEAT_EVAL
if (aborting())
return FAIL;
# endif
#endif
if (setfname(curbuf, fname, sfname, FALSE) == OK)
curbuf->b_flags |= BF_NOTEDITED;
#ifdef FEAT_AUTOCMD
apply_autocmds(EVENT_BUFNEW, NULL, NULL, FALSE, curbuf);
if (curbuf->b_p_bl)
apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, curbuf);
# ifdef FEAT_EVAL
if (aborting())
return FAIL;
# endif
if (*curbuf->b_p_ft == NUL)
{
if (au_has_group((char_u *)"filetypedetect"))
(void)do_doautocmd((char_u *)"filetypedetect BufRead", FALSE);
do_modelines(0);
}
#endif
return OK;
}
void
msg_add_fname(buf, fname)
buf_T *buf;
char_u *fname;
{
if (fname == NULL)
fname = (char_u *)"-stdin-";
home_replace(buf, fname, IObuff + 1, IOSIZE - 4, TRUE);
IObuff[0] = '"';
STRCAT(IObuff, "\" ");
}
static int
msg_add_fileformat(eol_type)
int eol_type;
{
#ifndef USE_CRNL
if (eol_type == EOL_DOS)
{
STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[dos]") : _("[dos format]"));
return TRUE;
}
#endif
#ifndef USE_CR
if (eol_type == EOL_MAC)
{
STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[mac]") : _("[mac format]"));
return TRUE;
}
#endif
#if defined(USE_CRNL) || defined(USE_CR)
if (eol_type == EOL_UNIX)
{
STRCAT(IObuff, shortmess(SHM_TEXT) ? _("[unix]") : _("[unix format]"));
return TRUE;
}
#endif
return FALSE;
}
void
msg_add_lines(insert_space, lnum, nchars)
int insert_space;
long lnum;
long nchars;
{
char_u *p;
p = IObuff + STRLEN(IObuff);
if (insert_space)
*p++ = ' ';
if (shortmess(SHM_LINES))
sprintf((char *)p, "%ldL, %ldC", lnum, nchars);
else
{
if (lnum == 1)
STRCPY(p, _("1 line, "));
else
sprintf((char *)p, _("%ld lines, "), lnum);
p += STRLEN(p);
if (nchars == 1)
STRCPY(p, _("1 character"));
else
sprintf((char *)p, _("%ld characters"), nchars);
}
}
static void
msg_add_eol()
{
STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]"));
}
static int
check_mtime(buf, st)
buf_T *buf;
struct stat *st;
{
if (buf->b_mtime_read != 0
&& time_differs((long)st->st_mtime, buf->b_mtime_read))
{
msg_scroll = TRUE;
msg_silent = 0;
MSG_ATTR(_("WARNING: The file has been changed since reading it!!!"),
hl_attr(HLF_E));
if (ask_yesno((char_u *)_("Do you really want to write to it"),
TRUE) == 'n')
return FAIL;
msg_scroll = FALSE;
}
return OK;
}
static int
time_differs(t1, t2)
long t1, t2;
{
#if defined(__linux__) || defined(MSDOS) || defined(MSWIN)
return (t1 - t2 > 1 || t2 - t1 > 1);
#else
return (t1 != t2);
#endif
}
static int
buf_write_bytes(ip)
struct bw_info *ip;
{
int wlen;
char_u *buf = ip->bw_buf;
int len = ip->bw_len;
#ifdef HAS_BW_FLAGS
int flags = ip->bw_flags;
#endif
#ifdef FEAT_MBYTE
if (!(flags & FIO_NOCONVERT))
{
char_u *p;
unsigned c;
int n;
if (flags & FIO_UTF8)
{
p = ip->bw_conv_buf;
for (wlen = 0; wlen < len; ++wlen)
p += utf_char2bytes(buf[wlen], p);
buf = ip->bw_conv_buf;
len = (int)(p - ip->bw_conv_buf);
}
else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1))
{
if (flags & FIO_LATIN1)
p = buf;
else
p = ip->bw_conv_buf;
for (wlen = 0; wlen < len; wlen += n)
{
if (wlen == 0 && ip->bw_restlen != 0)
{
int l;
l = CONV_RESTLEN - ip->bw_restlen;
if (l > len)
l = len;
mch_memmove(ip->bw_rest + ip->bw_restlen, buf, (size_t)l);
n = utf_ptr2len_len(ip->bw_rest, ip->bw_restlen + l);
if (n > ip->bw_restlen + len)
{
if (ip->bw_restlen + len > CONV_RESTLEN)
return FAIL;
ip->bw_restlen += len;
break;
}
if (n > 1)
c = utf_ptr2char(ip->bw_rest);
else
c = ip->bw_rest[0];
if (n >= ip->bw_restlen)
{
n -= ip->bw_restlen;
ip->bw_restlen = 0;
}
else
{
ip->bw_restlen -= n;
mch_memmove(ip->bw_rest, ip->bw_rest + n,
(size_t)ip->bw_restlen);
n = 0;
}
}
else
{
n = utf_ptr2len_len(buf + wlen, len - wlen);
if (n > len - wlen)
{
if (len - wlen > CONV_RESTLEN)
return FAIL;
ip->bw_restlen = len - wlen;
mch_memmove(ip->bw_rest, buf + wlen,
(size_t)ip->bw_restlen);
break;
}
if (n > 1)
c = utf_ptr2char(buf + wlen);
else
c = buf[wlen];
}
ip->bw_conv_error |= ucs2bytes(c, &p, flags);
}
if (flags & FIO_LATIN1)
len = (int)(p - buf);
else
{
buf = ip->bw_conv_buf;
len = (int)(p - ip->bw_conv_buf);
}
}
# ifdef WIN3264
else if (flags & FIO_CODEPAGE)
{
char_u *from;
size_t fromlen;
char_u *to;
int u8c;
BOOL bad = FALSE;
int needed;
if (ip->bw_restlen > 0)
{
fromlen = len + ip->bw_restlen;
from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
}
else
{
from = buf;
fromlen = len;
}
to = ip->bw_conv_buf;
if (enc_utf8)
{
while (fromlen > 0)
{
n = (int)utf_ptr2len_len(from, (int)fromlen);
if (n > (int)fromlen)
break;
u8c = utf_ptr2char(from);
*to++ = (u8c & 0xff);
*to++ = (u8c >> 8);
fromlen -= n;
from += n;
}
if (fromlen > CONV_RESTLEN)
{
ip->bw_conv_error = TRUE;
return FAIL;
}
mch_memmove(ip->bw_rest, from, fromlen);
ip->bw_restlen = (int)fromlen;
}
else
{
ip->bw_restlen = 0;
needed = MultiByteToWideChar(enc_codepage,
MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen,
NULL, 0);
if (needed == 0)
{
needed = MultiByteToWideChar(enc_codepage,
MB_ERR_INVALID_CHARS, (LPCSTR)from, (int)fromlen - 1,
NULL, 0);
if (needed == 0)
{
ip->bw_conv_error = TRUE;
return FAIL;
}
ip->bw_rest[0] = from[fromlen - 1];
ip->bw_restlen = 1;
}
needed = MultiByteToWideChar(enc_codepage, MB_ERR_INVALID_CHARS,
(LPCSTR)from, (int)(fromlen - ip->bw_restlen),
(LPWSTR)to, needed);
if (needed == 0)
{
ip->bw_conv_error = TRUE;
return FAIL;
}
to += needed * 2;
}
fromlen = to - ip->bw_conv_buf;
buf = to;
# ifdef CP_UTF8
if (FIO_GET_CP(flags) == CP_UTF8)
{
for (from = ip->bw_conv_buf; fromlen > 1; fromlen -= 2)
{
u8c = *from++;
u8c += (*from++ << 8);
to += utf_char2bytes(u8c, to);
if (to + 6 >= ip->bw_conv_buf + ip->bw_conv_buflen)
{
ip->bw_conv_error = TRUE;
return FAIL;
}
}
len = (int)(to - buf);
}
else
#endif
{
len = WideCharToMultiByte(FIO_GET_CP(flags), 0,
(LPCWSTR)ip->bw_conv_buf, (int)fromlen / sizeof(WCHAR),
(LPSTR)to, (int)(ip->bw_conv_buflen - fromlen), 0,
&bad);
if (bad)
{
ip->bw_conv_error = TRUE;
return FAIL;
}
}
}
# endif
# ifdef MACOS_CONVERT
else if (flags & FIO_MACROMAN)
{
char_u *from;
size_t fromlen;
if (ip->bw_restlen > 0)
{
fromlen = len + ip->bw_restlen;
from = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
mch_memmove(from, ip->bw_rest, (size_t)ip->bw_restlen);
mch_memmove(from + ip->bw_restlen, buf, (size_t)len);
}
else
{
from = buf;
fromlen = len;
}
if (enc2macroman(from, fromlen,
ip->bw_conv_buf, &len, ip->bw_conv_buflen,
ip->bw_rest, &ip->bw_restlen) == FAIL)
{
ip->bw_conv_error = TRUE;
return FAIL;
}
buf = ip->bw_conv_buf;
}
# endif
# ifdef USE_ICONV
if (ip->bw_iconv_fd != (iconv_t)-1)
{
const char *from;
size_t fromlen;
char *to;
size_t tolen;
if (ip->bw_restlen > 0)
{
fromlen = len + ip->bw_restlen;
from = (char *)ip->bw_conv_buf + ip->bw_conv_buflen - fromlen;
mch_memmove((void *)from, ip->bw_rest, (size_t)ip->bw_restlen);
mch_memmove((void *)(from + ip->bw_restlen), buf, (size_t)len);
tolen = ip->bw_conv_buflen - fromlen;
}
else
{
from = (const char *)buf;
fromlen = len;
tolen = ip->bw_conv_buflen;
}
to = (char *)ip->bw_conv_buf;
if (ip->bw_first)
{
size_t save_len = tolen;
(void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen);
if (to == NULL)
{
to = (char *)ip->bw_conv_buf;
tolen = save_len;
}
ip->bw_first = FALSE;
}
if ((iconv(ip->bw_iconv_fd, (void *)&from, &fromlen, &to, &tolen)
== (size_t)-1 && ICONV_ERRNO != ICONV_EINVAL)
|| fromlen > CONV_RESTLEN)
{
ip->bw_conv_error = TRUE;
return FAIL;
}
if (fromlen > 0)
mch_memmove(ip->bw_rest, (void *)from, fromlen);
ip->bw_restlen = (int)fromlen;
buf = ip->bw_conv_buf;
len = (int)((char_u *)to - ip->bw_conv_buf);
}
# endif
}
#endif
#ifdef FEAT_CRYPT
if (flags & FIO_ENCRYPTED)
{
int ztemp, t, i;
for (i = 0; i < len; i++)
{
ztemp = buf[i];
buf[i] = ZENCODE(ztemp, t);
}
}
#endif
while (len > 0)
{
wlen = vim_write(ip->bw_fd, buf, len);
if (wlen <= 0)
return FAIL;
len -= wlen;
buf += wlen;
}
return OK;
}
#ifdef FEAT_MBYTE
static int
ucs2bytes(c, pp, flags)
unsigned c;
char_u **pp;
int flags;
{
char_u *p = *pp;
int error = FALSE;
int cc;
if (flags & FIO_UCS4)
{
if (flags & FIO_ENDIAN_L)
{
*p++ = c;
*p++ = (c >> 8);
*p++ = (c >> 16);
*p++ = (c >> 24);
}
else
{
*p++ = (c >> 24);
*p++ = (c >> 16);
*p++ = (c >> 8);
*p++ = c;
}
}
else if (flags & (FIO_UCS2 | FIO_UTF16))
{
if (c >= 0x10000)
{
if (flags & FIO_UTF16)
{
c -= 0x10000;
if (c >= 0x100000)
error = TRUE;
cc = ((c >> 10) & 0x3ff) + 0xd800;
if (flags & FIO_ENDIAN_L)
{
*p++ = cc;
*p++ = ((unsigned)cc >> 8);
}
else
{
*p++ = ((unsigned)cc >> 8);
*p++ = cc;
}
c = (c & 0x3ff) + 0xdc00;
}
else
error = TRUE;
}
if (flags & FIO_ENDIAN_L)
{
*p++ = c;
*p++ = (c >> 8);
}
else
{
*p++ = (c >> 8);
*p++ = c;
}
}
else
{
if (c >= 0x100)
{
error = TRUE;
*p++ = 0xBF;
}
else
*p++ = c;
}
*pp = p;
return error;
}
static int
same_encoding(a, b)
char_u *a;
char_u *b;
{
int f;
if (STRCMP(a, b) == 0)
return TRUE;
f = get_fio_flags(a);
return (f != 0 && get_fio_flags(b) == f);
}
static int
get_fio_flags(ptr)
char_u *ptr;
{
int prop;
if (*ptr == NUL)
ptr = p_enc;
prop = enc_canon_props(ptr);
if (prop & ENC_UNICODE)
{
if (prop & ENC_2BYTE)
{
if (prop & ENC_ENDIAN_L)
return FIO_UCS2 | FIO_ENDIAN_L;
return FIO_UCS2;
}
if (prop & ENC_4BYTE)
{
if (prop & ENC_ENDIAN_L)
return FIO_UCS4 | FIO_ENDIAN_L;
return FIO_UCS4;
}
if (prop & ENC_2WORD)
{
if (prop & ENC_ENDIAN_L)
return FIO_UTF16 | FIO_ENDIAN_L;
return FIO_UTF16;
}
return FIO_UTF8;
}
if (prop & ENC_LATIN1)
return FIO_LATIN1;
return 0;
}
#ifdef WIN3264
static int
get_win_fio_flags(ptr)
char_u *ptr;
{
int cp;
if (!enc_utf8 && enc_codepage <= 0)
return 0;
cp = encname2codepage(ptr);
if (cp == 0)
{
# ifdef CP_UTF8
if (STRCMP(ptr, "utf-8") == 0)
cp = CP_UTF8;
else
# endif
return 0;
}
return FIO_PUT_CP(cp) | FIO_CODEPAGE;
}
#endif
#ifdef MACOS_X
static int
get_mac_fio_flags(ptr)
char_u *ptr;
{
if ((enc_utf8 || STRCMP(p_enc, "latin1") == 0)
&& (enc_canon_props(ptr) & ENC_MACROMAN))
return FIO_MACROMAN;
return 0;
}
#endif
static char_u *
check_for_bom(p, size, lenp, flags)
char_u *p;
long size;
int *lenp;
int flags;
{
char *name = NULL;
int len = 2;
if (p[0] == 0xef && p[1] == 0xbb && size >= 3 && p[2] == 0xbf
&& (flags == FIO_ALL || flags == FIO_UTF8 || flags == 0))
{
name = "utf-8";
len = 3;
}
else if (p[0] == 0xff && p[1] == 0xfe)
{
if (size >= 4 && p[2] == 0 && p[3] == 0
&& (flags == FIO_ALL || flags == (FIO_UCS4 | FIO_ENDIAN_L)))
{
name = "ucs-4le";
len = 4;
}
else if (flags == (FIO_UCS2 | FIO_ENDIAN_L))
name = "ucs-2le";
else if (flags == FIO_ALL || flags == (FIO_UTF16 | FIO_ENDIAN_L))
name = "utf-16le";
}
else if (p[0] == 0xfe && p[1] == 0xff
&& (flags == FIO_ALL || flags == FIO_UCS2 || flags == FIO_UTF16))
{
if (flags == FIO_UCS2)
name = "ucs-2";
else
name = "utf-16";
}
else if (size >= 4 && p[0] == 0 && p[1] == 0 && p[2] == 0xfe
&& p[3] == 0xff && (flags == FIO_ALL || flags == FIO_UCS4))
{
name = "ucs-4";
len = 4;
}
*lenp = len;
return (char_u *)name;
}
static int
make_bom(buf, name)
char_u *buf;
char_u *name;
{
int flags;
char_u *p;
flags = get_fio_flags(name);
if (flags == FIO_LATIN1 || flags == 0)
return 0;
if (flags == FIO_UTF8)
{
buf[0] = 0xef;
buf[1] = 0xbb;
buf[2] = 0xbf;
return 3;
}
p = buf;
(void)ucs2bytes(0xfeff, &p, flags);
return (int)(p - buf);
}
#endif
#if defined(FEAT_VIMINFO) || defined(FEAT_BROWSE) || \
defined(FEAT_QUICKFIX) || defined(FEAT_AUTOCMD) || defined(PROTO)
char_u *
shorten_fname1(full_path)
char_u *full_path;
{
char_u dirname[MAXPATHL];
char_u *p = full_path;
if (mch_dirname(dirname, MAXPATHL) == OK)
{
p = shorten_fname(full_path, dirname);
if (p == NULL || *p == NUL)
p = full_path;
}
return p;
}
#endif
char_u *
shorten_fname(full_path, dir_name)
char_u *full_path;
char_u *dir_name;
{
int len;
char_u *p;
if (full_path == NULL)
return NULL;
len = (int)STRLEN(dir_name);
if (fnamencmp(dir_name, full_path, len) == 0)
{
p = full_path + len;
#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
if (!((len > 2) && (*(p - 2) == ':')))
#endif
{
if (vim_ispathsep(*p))
++p;
#ifndef VMS
else
p = NULL;
#endif
}
}
#if defined(MSDOS) || defined(MSWIN) || defined(OS2)
else if (len > 3
&& TOUPPER_LOC(full_path[0]) == TOUPPER_LOC(dir_name[0])
&& full_path[1] == ':'
&& vim_ispathsep(full_path[2]))
p = full_path + 2;
#endif
else
p = NULL;
return p;
}
void
shorten_fnames(force)
int force;
{
char_u dirname[MAXPATHL];
buf_T *buf;
char_u *p;
mch_dirname(dirname, MAXPATHL);
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
{
if (buf->b_fname != NULL
#ifdef FEAT_QUICKFIX
&& !bt_nofile(buf)
#endif
&& !path_with_url(buf->b_fname)
&& (force
|| buf->b_sfname == NULL
|| mch_isFullName(buf->b_sfname)))
{
vim_free(buf->b_sfname);
buf->b_sfname = NULL;
p = shorten_fname(buf->b_ffname, dirname);
if (p != NULL)
{
buf->b_sfname = vim_strsave(p);
buf->b_fname = buf->b_sfname;
}
if (p == NULL || buf->b_fname == NULL)
buf->b_fname = buf->b_ffname;
}
mf_fullname(buf->b_ml.ml_mfp);
}
#ifdef FEAT_WINDOWS
status_redraw_all();
redraw_tabline = TRUE;
#endif
}
#if (defined(FEAT_DND) && defined(FEAT_GUI_GTK)) \
|| defined(FEAT_GUI_MSWIN) \
|| defined(FEAT_GUI_MAC) \
|| defined(PROTO)
void
shorten_filenames(fnames, count)
char_u **fnames;
int count;
{
int i;
char_u dirname[MAXPATHL];
char_u *p;
if (fnames == NULL || count < 1)
return;
mch_dirname(dirname, sizeof(dirname));
for (i = 0; i < count; ++i)
{
if ((p = shorten_fname(fnames[i], dirname)) != NULL)
{
p = vim_strsave(p);
vim_free(fnames[i]);
fnames[i] = p;
}
}
}
#endif
char_u *
modname(fname, ext, prepend_dot)
char_u *fname, *ext;
int prepend_dot;
{
return buf_modname(
#ifdef SHORT_FNAME
TRUE,
#else
(curbuf->b_p_sn || curbuf->b_shortname),
#endif
fname, ext, prepend_dot);
}
char_u *
buf_modname(shortname, fname, ext, prepend_dot)
int shortname;
char_u *fname, *ext;
int prepend_dot;
{
char_u *retval;
char_u *s;
char_u *e;
char_u *ptr;
int fnamelen, extlen;
extlen = (int)STRLEN(ext);
if (fname == NULL || *fname == NUL)
{
retval = alloc((unsigned)(MAXPATHL + extlen + 3));
if (retval == NULL)
return NULL;
if (mch_dirname(retval, MAXPATHL) == FAIL ||
(fnamelen = (int)STRLEN(retval)) == 0)
{
vim_free(retval);
return NULL;
}
if (!after_pathsep(retval, retval + fnamelen))
{
retval[fnamelen++] = PATHSEP;
retval[fnamelen] = NUL;
}
#ifndef SHORT_FNAME
prepend_dot = FALSE;
#endif
}
else
{
fnamelen = (int)STRLEN(fname);
retval = alloc((unsigned)(fnamelen + extlen + 3));
if (retval == NULL)
return NULL;
STRCPY(retval, fname);
#ifdef VMS
vms_remove_version(retval);
#endif
}
for (ptr = retval + fnamelen; ptr > retval; mb_ptr_back(retval, ptr))
{
#ifndef RISCOS
if (*ext == '.'
# ifdef USE_LONG_FNAME
&& (!USE_LONG_FNAME || shortname)
# else
# ifndef SHORT_FNAME
&& shortname
# endif
# endif
)
if (*ptr == '.')
*ptr = '_';
#endif
if (vim_ispathsep(*ptr))
{
++ptr;
break;
}
}
#ifndef SHORT_FNAME
if (STRLEN(ptr) > (unsigned)BASENAMELEN)
ptr[BASENAMELEN] = '\0';
#endif
s = ptr + STRLEN(ptr);
#ifdef USE_LONG_FNAME
if (!USE_LONG_FNAME || shortname)
#else
# ifndef SHORT_FNAME
if (shortname)
# endif
#endif
{
if (fname == NULL || *fname == NUL
|| vim_ispathsep(fname[STRLEN(fname) - 1]))
{
#ifdef RISCOS
if (*ext == '/')
#else
if (*ext == '.')
#endif
*s++ = '_';
}
#ifdef RISCOS
else if (*ext == '/' || *ext == '_')
#else
else if (*ext == '.')
#endif
{
if (s - ptr > (size_t)8)
{
s = ptr + 8;
*s = '\0';
}
}
#ifdef RISCOS
else if ((e = vim_strchr(ptr, '/')) == NULL)
*s++ = '/';
#else
else if ((e = vim_strchr(ptr, '.')) == NULL)
*s++ = '.';
#endif
else if ((int)STRLEN(e) + extlen > 4)
s = e + 4 - extlen;
}
#if defined(OS2) || defined(USE_LONG_FNAME) || defined(WIN3264)
else if ((fname == NULL || *fname == NUL) && *ext == '.')
*s++ = '_';
#endif
STRCPY(s, ext);
#ifndef SHORT_FNAME
if (prepend_dot && !shortname && *(e = gettail(retval)) !=
#ifdef RISCOS
'/'
#else
'.'
#endif
#ifdef USE_LONG_FNAME
&& USE_LONG_FNAME
#endif
)
{
STRMOVE(e + 1, e);
#ifdef RISCOS
*e = '/';
#else
*e = '.';
#endif
}
#endif
if (fname != NULL && STRCMP(fname, retval) == 0)
{
while (--s >= ptr)
{
if (*s != '_')
{
*s = '_';
break;
}
}
if (s < ptr)
*ptr = 'v';
}
return retval;
}
int
vim_fgets(buf, size, fp)
char_u *buf;
int size;
FILE *fp;
{
char *eof;
#define FGETS_SIZE 200
char tbuf[FGETS_SIZE];
buf[size - 2] = NUL;
#ifdef USE_CR
eof = fgets_cr((char *)buf, size, fp);
#else
eof = fgets((char *)buf, size, fp);
#endif
if (buf[size - 2] != NUL && buf[size - 2] != '\n')
{
buf[size - 1] = NUL;
do
{
tbuf[FGETS_SIZE - 2] = NUL;
#ifdef USE_CR
ignoredp = fgets_cr((char *)tbuf, FGETS_SIZE, fp);
#else
ignoredp = fgets((char *)tbuf, FGETS_SIZE, fp);
#endif
} while (tbuf[FGETS_SIZE - 2] != NUL && tbuf[FGETS_SIZE - 2] != '\n');
}
return (eof == NULL);
}
#if defined(USE_CR) || defined(PROTO)
int
tag_fgets(buf, size, fp)
char_u *buf;
int size;
FILE *fp;
{
int i = 0;
int c;
int eof = FALSE;
for (;;)
{
c = fgetc(fp);
if (c == EOF)
{
eof = TRUE;
break;
}
if (c == '\r')
{
if (i < size - 1)
buf[i++] = '\n';
c = fgetc(fp);
if (c != '\n')
ungetc(c, fp);
break;
}
if (i < size - 1)
buf[i++] = c;
if (c == '\n')
break;
}
buf[i] = NUL;
return eof;
}
#endif
int
vim_rename(from, to)
char_u *from;
char_u *to;
{
int fd_in;
int fd_out;
int n;
char *errmsg = NULL;
char *buffer;
#ifdef AMIGA
BPTR flock;
#endif
struct stat st;
long perm;
#ifdef HAVE_ACL
vim_acl_T acl;
#endif
#if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME)
int use_tmp_file = FALSE;
#endif
if (fnamecmp(from, to) == 0)
{
#ifdef CASE_INSENSITIVE_FILENAME
if (STRCMP(gettail(from), gettail(to)) != 0)
use_tmp_file = TRUE;
else
#endif
return 0;
}
if (mch_stat((char *)from, &st) < 0)
return -1;
#ifdef UNIX
{
struct stat st_to;
if (mch_stat((char *)to, &st_to) >= 0
&& st.st_dev == st_to.st_dev
&& st.st_ino == st_to.st_ino)
use_tmp_file = TRUE;
}
#endif
#if defined(UNIX) || defined(CASE_INSENSITIVE_FILENAME)
if (use_tmp_file)
{
char tempname[MAXPATHL + 1];
if (STRLEN(from) >= MAXPATHL - 5)
return -1;
STRCPY(tempname, from);
for (n = 123; n < 99999; ++n)
{
sprintf((char *)gettail((char_u *)tempname), "%d", n);
if (mch_stat(tempname, &st) < 0)
{
if (mch_rename((char *)from, tempname) == 0)
{
if (mch_rename(tempname, (char *)to) == 0)
return 0;
mch_rename(tempname, (char *)from);
return -1;
}
return -1;
}
}
return -1;
}
#endif
#ifdef AMIGA
flock = Lock((UBYTE *)from, (long)ACCESS_READ);
#endif
mch_remove(to);
#ifdef AMIGA
if (flock)
UnLock(flock);
#endif
if (mch_rename((char *)from, (char *)to) == 0)
return 0;
perm = mch_getperm(from);
#ifdef HAVE_ACL
acl = mch_get_acl(from);
#endif
fd_in = mch_open((char *)from, O_RDONLY|O_EXTRA, 0);
if (fd_in == -1)
{
#ifdef HAVE_ACL
mch_free_acl(acl);
#endif
return -1;
}
fd_out = mch_open((char *)to,
O_CREAT|O_EXCL|O_WRONLY|O_EXTRA|O_NOFOLLOW, (int)perm);
if (fd_out == -1)
{
close(fd_in);
#ifdef HAVE_ACL
mch_free_acl(acl);
#endif
return -1;
}
buffer = (char *)alloc(BUFSIZE);
if (buffer == NULL)
{
close(fd_out);
close(fd_in);
#ifdef HAVE_ACL
mch_free_acl(acl);
#endif
return -1;
}
while ((n = vim_read(fd_in, buffer, BUFSIZE)) > 0)
if (vim_write(fd_out, buffer, n) != n)
{
errmsg = _("E208: Error writing to \"%s\"");
break;
}
vim_free(buffer);
close(fd_in);
if (close(fd_out) < 0)
errmsg = _("E209: Error closing \"%s\"");
if (n < 0)
{
errmsg = _("E210: Error reading \"%s\"");
to = from;
}
#ifndef UNIX
mch_setperm(to, perm);
#endif
#ifdef HAVE_ACL
mch_set_acl(to, acl);
mch_free_acl(acl);
#endif
if (errmsg != NULL)
{
EMSG2(errmsg, to);
return -1;
}
mch_remove(from);
return 0;
}
static int already_warned = FALSE;
int
check_timestamps(focus)
int focus;
{
buf_T *buf;
int didit = 0;
int n;
if (no_check_timestamps > 0)
return FALSE;
if (focus && did_check_timestamps)
{
need_check_timestamps = TRUE;
return FALSE;
}
if (!stuff_empty() || global_busy || !typebuf_typed()
#ifdef FEAT_AUTOCMD
|| autocmd_busy || curbuf_lock > 0
#endif
)
need_check_timestamps = TRUE;
else
{
++no_wait_return;
did_check_timestamps = TRUE;
already_warned = FALSE;
for (buf = firstbuf; buf != NULL; )
{
if (buf->b_nwindows > 0)
{
n = buf_check_timestamp(buf, focus);
if (didit < n)
didit = n;
if (n > 0 && !buf_valid(buf))
{
buf = firstbuf;
continue;
}
}
buf = buf->b_next;
}
--no_wait_return;
need_check_timestamps = FALSE;
if (need_wait_return && didit == 2)
{
msg_puts((char_u *)"\n");
out_flush();
}
}
return didit;
}
static int
move_lines(frombuf, tobuf)
buf_T *frombuf;
buf_T *tobuf;
{
buf_T *tbuf = curbuf;
int retval = OK;
linenr_T lnum;
char_u *p;
curbuf = tobuf;
for (lnum = 1; lnum <= frombuf->b_ml.ml_line_count; ++lnum)
{
p = vim_strsave(ml_get_buf(frombuf, lnum, FALSE));
if (p == NULL || ml_append(lnum - 1, p, 0, FALSE) == FAIL)
{
vim_free(p);
retval = FAIL;
break;
}
vim_free(p);
}
if (retval != FAIL)
{
curbuf = frombuf;
for (lnum = curbuf->b_ml.ml_line_count; lnum > 0; --lnum)
if (ml_delete(lnum, FALSE) == FAIL)
{
retval = FAIL;
break;
}
}
curbuf = tbuf;
return retval;
}
int
buf_check_timestamp(buf, focus)
buf_T *buf;
int focus;
{
struct stat st;
int stat_res;
int retval = 0;
char_u *path;
char_u *tbuf;
char *mesg = NULL;
char *mesg2 = "";
int helpmesg = FALSE;
int reload = FALSE;
#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
int can_reload = FALSE;
#endif
size_t orig_size = buf->b_orig_size;
int orig_mode = buf->b_orig_mode;
#ifdef FEAT_GUI
int save_mouse_correct = need_mouse_correct;
#endif
#ifdef FEAT_AUTOCMD
static int busy = FALSE;
int n;
char_u *s;
#endif
char *reason;
if (buf->b_ffname == NULL
|| buf->b_ml.ml_mfp == NULL
#if defined(FEAT_QUICKFIX)
|| *buf->b_p_bt != NUL
#endif
|| buf->b_saving
#ifdef FEAT_AUTOCMD
|| busy
#endif
#ifdef FEAT_NETBEANS_INTG
|| isNetbeansBuffer(buf)
#endif
)
return 0;
if ( !(buf->b_flags & BF_NOTEDITED)
&& buf->b_mtime != 0
&& ((stat_res = mch_stat((char *)buf->b_ffname, &st)) < 0
|| time_differs((long)st.st_mtime, buf->b_mtime)
#ifdef HAVE_ST_MODE
|| (int)st.st_mode != buf->b_orig_mode
#else
|| mch_getperm(buf->b_ffname) != buf->b_orig_mode
#endif
))
{
retval = 1;
if (stat_res < 0)
{
buf->b_mtime = 0;
buf->b_orig_size = 0;
buf->b_orig_mode = 0;
}
else
buf_store_time(buf, &st, buf->b_ffname);
if (mch_isdir(buf->b_fname))
;
else if ((buf->b_p_ar >= 0 ? buf->b_p_ar : p_ar)
&& !bufIsChanged(buf) && stat_res >= 0)
reload = TRUE;
else
{
if (stat_res < 0)
reason = "deleted";
else if (bufIsChanged(buf))
reason = "conflict";
else if (orig_size != buf->b_orig_size || buf_contents_changed(buf))
reason = "changed";
else if (orig_mode != buf->b_orig_mode)
reason = "mode";
else
reason = "time";
#ifdef FEAT_AUTOCMD
busy = TRUE;
# ifdef FEAT_EVAL
set_vim_var_string(VV_FCS_REASON, (char_u *)reason, -1);
set_vim_var_string(VV_FCS_CHOICE, (char_u *)"", -1);
# endif
n = apply_autocmds(EVENT_FILECHANGEDSHELL,
buf->b_fname, buf->b_fname, FALSE, buf);
busy = FALSE;
if (n)
{
if (!buf_valid(buf))
EMSG(_("E246: FileChangedShell autocommand deleted buffer"));
# ifdef FEAT_EVAL
s = get_vim_var_str(VV_FCS_CHOICE);
if (STRCMP(s, "reload") == 0 && *reason != 'd')
reload = TRUE;
else if (STRCMP(s, "ask") == 0)
n = FALSE;
else
# endif
return 2;
}
if (!n)
#endif
{
if (*reason == 'd')
mesg = _("E211: File \"%s\" no longer available");
else
{
helpmesg = TRUE;
#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
can_reload = TRUE;
#endif
if (reason[2] == 'n')
{
mesg = _("W12: Warning: File \"%s\" has changed and the buffer was changed in Vim as well");
mesg2 = _("See \":help W12\" for more info.");
}
else if (reason[1] == 'h')
{
mesg = _("W11: Warning: File \"%s\" has changed since editing started");
mesg2 = _("See \":help W11\" for more info.");
}
else if (*reason == 'm')
{
mesg = _("W16: Warning: Mode of file \"%s\" has changed since editing started");
mesg2 = _("See \":help W16\" for more info.");
}
}
}
}
}
else if ((buf->b_flags & BF_NEW) && !(buf->b_flags & BF_NEW_W)
&& vim_fexists(buf->b_ffname))
{
retval = 1;
mesg = _("W13: Warning: File \"%s\" has been created after editing started");
buf->b_flags |= BF_NEW_W;
#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
can_reload = TRUE;
#endif
}
if (mesg != NULL)
{
path = home_replace_save(buf, buf->b_fname);
if (path != NULL)
{
if (!helpmesg)
mesg2 = "";
tbuf = alloc((unsigned)(STRLEN(path) + STRLEN(mesg)
+ STRLEN(mesg2) + 2));
sprintf((char *)tbuf, mesg, path);
#if defined(FEAT_CON_DIALOG) || defined(FEAT_GUI_DIALOG)
if (can_reload)
{
if (*mesg2 != NUL)
{
STRCAT(tbuf, "\n");
STRCAT(tbuf, mesg2);
}
if (do_dialog(VIM_WARNING, (char_u *)_("Warning"), tbuf,
(char_u *)_("&OK\n&Load File"), 1, NULL) == 2)
reload = TRUE;
}
else
#endif
if (State > NORMAL_BUSY || (State & CMDLINE) || already_warned)
{
if (*mesg2 != NUL)
{
STRCAT(tbuf, "; ");
STRCAT(tbuf, mesg2);
}
EMSG(tbuf);
retval = 2;
}
else
{
# ifdef FEAT_AUTOCMD
if (!autocmd_busy)
# endif
{
msg_start();
msg_puts_attr(tbuf, hl_attr(HLF_E) + MSG_HIST);
if (*mesg2 != NUL)
msg_puts_attr((char_u *)mesg2,
hl_attr(HLF_W) + MSG_HIST);
msg_clr_eos();
(void)msg_end();
if (emsg_silent == 0)
{
out_flush();
# ifdef FEAT_GUI
if (!focus)
# endif
ui_delay(1000L, TRUE);
redraw_cmdline = FALSE;
}
}
already_warned = TRUE;
}
vim_free(path);
vim_free(tbuf);
}
}
if (reload)
buf_reload(buf, orig_mode);
#ifdef FEAT_AUTOCMD
if (buf_valid(buf) && retval != 0)
(void)apply_autocmds(EVENT_FILECHANGEDSHELLPOST,
buf->b_fname, buf->b_fname, FALSE, buf);
#endif
#ifdef FEAT_GUI
need_mouse_correct = save_mouse_correct;
#endif
return retval;
}
void
buf_reload(buf, orig_mode)
buf_T *buf;
int orig_mode;
{
exarg_T ea;
pos_T old_cursor;
linenr_T old_topline;
int old_ro = buf->b_p_ro;
buf_T *savebuf;
int saved = OK;
aco_save_T aco;
aucmd_prepbuf(&aco, buf);
if (prep_exarg(&ea, buf) == OK)
{
old_cursor = curwin->w_cursor;
old_topline = curwin->w_topline;
if (bufempty())
savebuf = NULL;
else
{
savebuf = buflist_new(NULL, NULL, (linenr_T)1, BLN_DUMMY);
if (savebuf != NULL && buf == curbuf)
{
curbuf = savebuf;
curwin->w_buffer = savebuf;
saved = ml_open(curbuf);
curbuf = buf;
curwin->w_buffer = buf;
}
if (savebuf == NULL || saved == FAIL || buf != curbuf
|| move_lines(buf, savebuf) == FAIL)
{
EMSG2(_("E462: Could not prepare for reloading \"%s\""),
buf->b_fname);
saved = FAIL;
}
}
if (saved == OK)
{
curbuf->b_flags |= BF_CHECK_RO;
#ifdef FEAT_AUTOCMD
keep_filetype = TRUE;
#endif
if (readfile(buf->b_ffname, buf->b_fname, (linenr_T)0,
(linenr_T)0,
(linenr_T)MAXLNUM, &ea, READ_NEW) == FAIL)
{
#if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL)
if (!aborting())
#endif
EMSG2(_("E321: Could not reload \"%s\""), buf->b_fname);
if (savebuf != NULL && buf_valid(savebuf) && buf == curbuf)
{
while (!bufempty())
if (ml_delete(buf->b_ml.ml_line_count, FALSE) == FAIL)
break;
(void)move_lines(savebuf, buf);
}
}
else if (buf == curbuf)
{
unchanged(buf, TRUE);
u_blockfree(buf);
u_clearall(buf);
}
}
vim_free(ea.cmd);
if (savebuf != NULL && buf_valid(savebuf))
wipe_buffer(savebuf, FALSE);
#ifdef FEAT_DIFF
diff_invalidate(curbuf);
#endif
if (old_topline > curbuf->b_ml.ml_line_count)
curwin->w_topline = curbuf->b_ml.ml_line_count;
else
curwin->w_topline = old_topline;
curwin->w_cursor = old_cursor;
check_cursor();
update_topline();
#ifdef FEAT_AUTOCMD
keep_filetype = FALSE;
#endif
#ifdef FEAT_FOLDING
{
win_T *wp;
FOR_ALL_WINDOWS(wp)
if (wp->w_buffer == curwin->w_buffer
&& !foldmethodIsManual(wp))
foldUpdateAll(wp);
}
#endif
if (orig_mode == curbuf->b_orig_mode)
curbuf->b_p_ro |= old_ro;
}
aucmd_restbuf(&aco);
}
void
buf_store_time(buf, st, fname)
buf_T *buf;
struct stat *st;
char_u *fname;
{
buf->b_mtime = (long)st->st_mtime;
buf->b_orig_size = (size_t)st->st_size;
#ifdef HAVE_ST_MODE
buf->b_orig_mode = (int)st->st_mode;
#else
buf->b_orig_mode = mch_getperm(fname);
#endif
}
void
write_lnum_adjust(offset)
linenr_T offset;
{
if (write_no_eol_lnum != 0)
write_no_eol_lnum += offset;
}
#if defined(TEMPDIRNAMES) || defined(PROTO)
static long temp_count = 0;
void
vim_deltempdir()
{
char_u **files;
int file_count;
int i;
if (vim_tempdir != NULL)
{
sprintf((char *)NameBuff, "%s*", vim_tempdir);
if (gen_expand_wildcards(1, &NameBuff, &file_count, &files,
EW_DIR|EW_FILE|EW_SILENT) == OK)
{
for (i = 0; i < file_count; ++i)
mch_remove(files[i]);
FreeWild(file_count, files);
}
gettail(NameBuff)[-1] = NUL;
(void)mch_rmdir(NameBuff);
vim_free(vim_tempdir);
vim_tempdir = NULL;
}
}
#endif
char_u *
vim_tempname(extra_char)
int extra_char;
{
#ifdef USE_TMPNAM
char_u itmp[L_tmpnam];
#else
char_u itmp[TEMPNAMELEN];
#endif
#ifdef TEMPDIRNAMES
static char *(tempdirs[]) = {TEMPDIRNAMES};
int i;
long nr;
long off;
# ifndef EEXIST
struct stat st;
# endif
if (vim_tempdir == NULL)
{
for (i = 0; i < sizeof(tempdirs) / sizeof(char *); ++i)
{
expand_env((char_u *)tempdirs[i], itmp, TEMPNAMELEN - 20);
if (mch_isdir(itmp))
{
# ifdef __EMX__
if (vim_strchr(itmp, '/') != NULL)
STRCAT(itmp, "/");
else
# endif
add_pathsep(itmp);
nr = (mch_get_pid() + (long)time(NULL)) % 1000000L;
for (off = 0; off < 10000L; ++off)
{
int r;
#if defined(UNIX) || defined(VMS)
mode_t umask_save;
#endif
sprintf((char *)itmp + STRLEN(itmp), "v%ld", nr + off);
# ifndef EEXIST
if (mch_stat((char *)itmp, &st) >= 0)
continue;
# endif
#if defined(UNIX) || defined(VMS)
umask_save = umask(077);
#endif
r = vim_mkdir(itmp, 0700);
#if defined(UNIX) || defined(VMS)
(void)umask(umask_save);
#endif
if (r == 0)
{
char_u *buf;
buf = alloc((unsigned)MAXPATHL + 1);
if (buf != NULL)
{
if (vim_FullName(itmp, buf, MAXPATHL, FALSE)
== FAIL)
STRCPY(buf, itmp);
# ifdef __EMX__
if (vim_strchr(buf, '/') != NULL)
STRCAT(buf, "/");
else
# endif
add_pathsep(buf);
vim_tempdir = vim_strsave(buf);
vim_free(buf);
}
break;
}
# ifdef EEXIST
if (errno != EEXIST)
# endif
break;
}
if (vim_tempdir != NULL)
break;
}
}
}
if (vim_tempdir != NULL)
{
sprintf((char *)itmp, "%s%ld", vim_tempdir, temp_count++);
return vim_strsave(itmp);
}
return NULL;
#else
# ifdef WIN3264
char szTempFile[_MAX_PATH + 1];
char buf4[4];
char_u *retval;
char_u *p;
STRCPY(itmp, "");
if (GetTempPath(_MAX_PATH, szTempFile) == 0)
szTempFile[0] = NUL;
strcpy(buf4, "VIM");
buf4[2] = extra_char;
if (GetTempFileName(szTempFile, buf4, 0, itmp) == 0)
return NULL;
(void)DeleteFile(itmp);
retval = vim_strsave(itmp);
if (*p_shcf == '-' || p_ssl)
for (p = retval; *p; ++p)
if (*p == '\\')
*p = '/';
return retval;
# else
# ifdef USE_TMPNAM
if (*tmpnam((char *)itmp) == NUL)
return NULL;
# else
char_u *p;
# ifdef VMS_TEMPNAM
sprintf((char *)itmp, "VIM%c", extra_char);
p = (char_u *)tempnam("tmp:", (char *)itmp);
if (p != NULL)
{
STRCPY(itmp, p);
STRCAT(itmp, ".txt");
free(p);
}
else
return NULL;
# else
STRCPY(itmp, TEMPNAME);
if ((p = vim_strchr(itmp, '?')) != NULL)
*p = extra_char;
if (mktemp((char *)itmp) == NULL)
return NULL;
# endif
# endif
return vim_strsave(itmp);
# endif
#endif
}
#if defined(BACKSLASH_IN_FILENAME) || defined(PROTO)
void
forward_slash(fname)
char_u *fname;
{
char_u *p;
for (p = fname; *p != NUL; ++p)
# ifdef FEAT_MBYTE
if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
++p;
else
# endif
if (*p == '\\')
*p = '/';
}
#endif
#if defined(FEAT_AUTOCMD) || defined(PROTO)
typedef struct AutoCmd
{
char_u *cmd;
char nested;
char last;
#ifdef FEAT_EVAL
scid_T scriptID;
#endif
struct AutoCmd *next;
} AutoCmd;
typedef struct AutoPat
{
int group;
char_u *pat;
int patlen;
regprog_T *reg_prog;
char allow_dirs;
char last;
AutoCmd *cmds;
struct AutoPat *next;
int buflocal_nr;
} AutoPat;
static struct event_name
{
char *name;
event_T event;
} event_names[] =
{
{"BufAdd", EVENT_BUFADD},
{"BufCreate", EVENT_BUFADD},
{"BufDelete", EVENT_BUFDELETE},
{"BufEnter", EVENT_BUFENTER},
{"BufFilePost", EVENT_BUFFILEPOST},
{"BufFilePre", EVENT_BUFFILEPRE},
{"BufHidden", EVENT_BUFHIDDEN},
{"BufLeave", EVENT_BUFLEAVE},
{"BufNew", EVENT_BUFNEW},
{"BufNewFile", EVENT_BUFNEWFILE},
{"BufRead", EVENT_BUFREADPOST},
{"BufReadCmd", EVENT_BUFREADCMD},
{"BufReadPost", EVENT_BUFREADPOST},
{"BufReadPre", EVENT_BUFREADPRE},
{"BufUnload", EVENT_BUFUNLOAD},
{"BufWinEnter", EVENT_BUFWINENTER},
{"BufWinLeave", EVENT_BUFWINLEAVE},
{"BufWipeout", EVENT_BUFWIPEOUT},
{"BufWrite", EVENT_BUFWRITEPRE},
{"BufWritePost", EVENT_BUFWRITEPOST},
{"BufWritePre", EVENT_BUFWRITEPRE},
{"BufWriteCmd", EVENT_BUFWRITECMD},
{"CmdwinEnter", EVENT_CMDWINENTER},
{"CmdwinLeave", EVENT_CMDWINLEAVE},
{"ColorScheme", EVENT_COLORSCHEME},
{"CursorHold", EVENT_CURSORHOLD},
{"CursorHoldI", EVENT_CURSORHOLDI},
{"CursorMoved", EVENT_CURSORMOVED},
{"CursorMovedI", EVENT_CURSORMOVEDI},
{"EncodingChanged", EVENT_ENCODINGCHANGED},
{"FileEncoding", EVENT_ENCODINGCHANGED},
{"FileAppendPost", EVENT_FILEAPPENDPOST},
{"FileAppendPre", EVENT_FILEAPPENDPRE},
{"FileAppendCmd", EVENT_FILEAPPENDCMD},
{"FileChangedShell",EVENT_FILECHANGEDSHELL},
{"FileChangedShellPost",EVENT_FILECHANGEDSHELLPOST},
{"FileChangedRO", EVENT_FILECHANGEDRO},
{"FileReadPost", EVENT_FILEREADPOST},
{"FileReadPre", EVENT_FILEREADPRE},
{"FileReadCmd", EVENT_FILEREADCMD},
{"FileType", EVENT_FILETYPE},
{"FileWritePost", EVENT_FILEWRITEPOST},
{"FileWritePre", EVENT_FILEWRITEPRE},
{"FileWriteCmd", EVENT_FILEWRITECMD},
{"FilterReadPost", EVENT_FILTERREADPOST},
{"FilterReadPre", EVENT_FILTERREADPRE},
{"FilterWritePost", EVENT_FILTERWRITEPOST},
{"FilterWritePre", EVENT_FILTERWRITEPRE},
{"FocusGained", EVENT_FOCUSGAINED},
{"FocusLost", EVENT_FOCUSLOST},
{"FuncUndefined", EVENT_FUNCUNDEFINED},
{"GUIEnter", EVENT_GUIENTER},
{"GUIFailed", EVENT_GUIFAILED},
{"InsertChange", EVENT_INSERTCHANGE},
{"InsertEnter", EVENT_INSERTENTER},
{"InsertLeave", EVENT_INSERTLEAVE},
{"MenuPopup", EVENT_MENUPOPUP},
{"QuickFixCmdPost", EVENT_QUICKFIXCMDPOST},
{"QuickFixCmdPre", EVENT_QUICKFIXCMDPRE},
{"RemoteReply", EVENT_REMOTEREPLY},
{"SessionLoadPost", EVENT_SESSIONLOADPOST},
{"ShellCmdPost", EVENT_SHELLCMDPOST},
{"ShellFilterPost", EVENT_SHELLFILTERPOST},
{"SourcePre", EVENT_SOURCEPRE},
{"SourceCmd", EVENT_SOURCECMD},
{"SpellFileMissing",EVENT_SPELLFILEMISSING},
{"StdinReadPost", EVENT_STDINREADPOST},
{"StdinReadPre", EVENT_STDINREADPRE},
{"SwapExists", EVENT_SWAPEXISTS},
{"Syntax", EVENT_SYNTAX},
{"TabEnter", EVENT_TABENTER},
{"TabLeave", EVENT_TABLEAVE},
{"TermChanged", EVENT_TERMCHANGED},
{"TermResponse", EVENT_TERMRESPONSE},
{"User", EVENT_USER},
{"VimEnter", EVENT_VIMENTER},
{"VimLeave", EVENT_VIMLEAVE},
{"VimLeavePre", EVENT_VIMLEAVEPRE},
{"WinEnter", EVENT_WINENTER},
{"WinLeave", EVENT_WINLEAVE},
{"VimResized", EVENT_VIMRESIZED},
{NULL, (event_T)0}
};
static AutoPat *first_autopat[NUM_EVENTS] =
{
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
typedef struct AutoPatCmd
{
AutoPat *curpat;
AutoCmd *nextcmd;
int group;
char_u *fname;
char_u *sfname;
char_u *tail;
event_T event;
int arg_bufnr;
struct AutoPatCmd *next;
} AutoPatCmd;
static AutoPatCmd *active_apc_list = NULL;
static garray_T augroups = {0, 0, sizeof(char_u *), 10, NULL};
#define AUGROUP_NAME(i) (((char_u **)augroups.ga_data)[i])
static int current_augroup = AUGROUP_DEFAULT;
static int au_need_clean = FALSE;
static void show_autocmd __ARGS((AutoPat *ap, event_T event));
static void au_remove_pat __ARGS((AutoPat *ap));
static void au_remove_cmds __ARGS((AutoPat *ap));
static void au_cleanup __ARGS((void));
static int au_new_group __ARGS((char_u *name));
static void au_del_group __ARGS((char_u *name));
static event_T event_name2nr __ARGS((char_u *start, char_u **end));
static char_u *event_nr2name __ARGS((event_T event));
static char_u *find_end_event __ARGS((char_u *arg, int have_group));
static int event_ignored __ARGS((event_T event));
static int au_get_grouparg __ARGS((char_u **argp));
static int do_autocmd_event __ARGS((event_T event, char_u *pat, int nested, char_u *cmd, int forceit, int group));
static char_u *getnextac __ARGS((int c, void *cookie, int indent));
static int apply_autocmds_group __ARGS((event_T event, char_u *fname, char_u *fname_io, int force, int group, buf_T *buf, exarg_T *eap));
static void auto_next_pat __ARGS((AutoPatCmd *apc, int stop_at_last));
static event_T last_event;
static int last_group;
static int autocmd_blocked = 0;
static void
show_autocmd(ap, event)
AutoPat *ap;
event_T event;
{
AutoCmd *ac;
if (got_int)
return;
if (ap->pat == NULL)
return;
msg_putchar('\n');
if (got_int)
return;
if (event != last_event || ap->group != last_group)
{
if (ap->group != AUGROUP_DEFAULT)
{
if (AUGROUP_NAME(ap->group) == NULL)
msg_puts_attr((char_u *)_("--Deleted--"), hl_attr(HLF_E));
else
msg_puts_attr(AUGROUP_NAME(ap->group), hl_attr(HLF_T));
msg_puts((char_u *)" ");
}
msg_puts_attr(event_nr2name(event), hl_attr(HLF_T));
last_event = event;
last_group = ap->group;
msg_putchar('\n');
if (got_int)
return;
}
msg_col = 4;
msg_outtrans(ap->pat);
for (ac = ap->cmds; ac != NULL; ac = ac->next)
{
if (ac->cmd != NULL)
{
if (msg_col >= 14)
msg_putchar('\n');
msg_col = 14;
if (got_int)
return;
msg_outtrans(ac->cmd);
#ifdef FEAT_EVAL
if (p_verbose > 0)
last_set_msg(ac->scriptID);
#endif
if (got_int)
return;
if (ac->next != NULL)
{
msg_putchar('\n');
if (got_int)
return;
}
}
}
}
static void
au_remove_pat(ap)
AutoPat *ap;
{
vim_free(ap->pat);
ap->pat = NULL;
ap->buflocal_nr = -1;
au_need_clean = TRUE;
}
static void
au_remove_cmds(ap)
AutoPat *ap;
{
AutoCmd *ac;
for (ac = ap->cmds; ac != NULL; ac = ac->next)
{
vim_free(ac->cmd);
ac->cmd = NULL;
}
au_need_clean = TRUE;
}
static void
au_cleanup()
{
AutoPat *ap, **prev_ap;
AutoCmd *ac, **prev_ac;
event_T event;
if (autocmd_busy || !au_need_clean)
return;
for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
event = (event_T)((int)event + 1))
{
prev_ap = &(first_autopat[(int)event]);
for (ap = *prev_ap; ap != NULL; ap = *prev_ap)
{
prev_ac = &(ap->cmds);
for (ac = *prev_ac; ac != NULL; ac = *prev_ac)
{
if (ap->pat == NULL || ac->cmd == NULL)
{
*prev_ac = ac->next;
vim_free(ac->cmd);
vim_free(ac);
}
else
prev_ac = &(ac->next);
}
if (ap->pat == NULL)
{
*prev_ap = ap->next;
vim_free(ap->reg_prog);
vim_free(ap);
}
else
prev_ap = &(ap->next);
}
}
au_need_clean = FALSE;
}
void
aubuflocal_remove(buf)
buf_T *buf;
{
AutoPat *ap;
event_T event;
AutoPatCmd *apc;
for (apc = active_apc_list; apc; apc = apc->next)
if (buf->b_fnum == apc->arg_bufnr)
apc->arg_bufnr = 0;
for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
event = (event_T)((int)event + 1))
for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
if (ap->buflocal_nr == buf->b_fnum)
{
au_remove_pat(ap);
if (p_verbose >= 6)
{
verbose_enter();
smsg((char_u *)
_("auto-removing autocommand: %s <buffer=%d>"),
event_nr2name(event), buf->b_fnum);
verbose_leave();
}
}
au_cleanup();
}
static int
au_new_group(name)
char_u *name;
{
int i;
i = au_find_group(name);
if (i == AUGROUP_ERROR)
{
for (i = 0; i < augroups.ga_len; ++i)
if (AUGROUP_NAME(i) == NULL)
break;
if (i == augroups.ga_len && ga_grow(&augroups, 1) == FAIL)
return AUGROUP_ERROR;
AUGROUP_NAME(i) = vim_strsave(name);
if (AUGROUP_NAME(i) == NULL)
return AUGROUP_ERROR;
if (i == augroups.ga_len)
++augroups.ga_len;
}
return i;
}
static void
au_del_group(name)
char_u *name;
{
int i;
i = au_find_group(name);
if (i == AUGROUP_ERROR)
EMSG2(_("E367: No such group: \"%s\""), name);
else
{
vim_free(AUGROUP_NAME(i));
AUGROUP_NAME(i) = NULL;
}
}
static int
au_find_group(name)
char_u *name;
{
int i;
for (i = 0; i < augroups.ga_len; ++i)
if (AUGROUP_NAME(i) != NULL && STRCMP(AUGROUP_NAME(i), name) == 0)
return i;
return AUGROUP_ERROR;
}
int
au_has_group(name)
char_u *name;
{
return au_find_group(name) != AUGROUP_ERROR;
}
void
do_augroup(arg, del_group)
char_u *arg;
int del_group;
{
int i;
if (del_group)
{
if (*arg == NUL)
EMSG(_(e_argreq));
else
au_del_group(arg);
}
else if (STRICMP(arg, "end") == 0)
current_augroup = AUGROUP_DEFAULT;
else if (*arg)
{
i = au_new_group(arg);
if (i != AUGROUP_ERROR)
current_augroup = i;
}
else
{
msg_start();
for (i = 0; i < augroups.ga_len; ++i)
{
if (AUGROUP_NAME(i) != NULL)
{
msg_puts(AUGROUP_NAME(i));
msg_puts((char_u *)" ");
}
}
msg_clr_eos();
msg_end();
}
}
#if defined(EXITFREE) || defined(PROTO)
void
free_all_autocmds()
{
for (current_augroup = -1; current_augroup < augroups.ga_len;
++current_augroup)
do_autocmd((char_u *)"", TRUE);
ga_clear_strings(&augroups);
}
#endif
static event_T
event_name2nr(start, end)
char_u *start;
char_u **end;
{
char_u *p;
int i;
int len;
for (p = start; *p && !vim_iswhite(*p) && *p != ','; ++p)
;
for (i = 0; event_names[i].name != NULL; ++i)
{
len = (int)STRLEN(event_names[i].name);
if (len == p - start && STRNICMP(event_names[i].name, start, len) == 0)
break;
}
if (*p == ',')
++p;
*end = p;
if (event_names[i].name == NULL)
return NUM_EVENTS;
return event_names[i].event;
}
static char_u *
event_nr2name(event)
event_T event;
{
int i;
for (i = 0; event_names[i].name != NULL; ++i)
if (event_names[i].event == event)
return (char_u *)event_names[i].name;
return (char_u *)"Unknown";
}
static char_u *
find_end_event(arg, have_group)
char_u *arg;
int have_group;
{
char_u *pat;
char_u *p;
if (*arg == '*')
{
if (arg[1] && !vim_iswhite(arg[1]))
{
EMSG2(_("E215: Illegal character after *: %s"), arg);
return NULL;
}
pat = arg + 1;
}
else
{
for (pat = arg; *pat && !vim_iswhite(*pat); pat = p)
{
if ((int)event_name2nr(pat, &p) >= (int)NUM_EVENTS)
{
if (have_group)
EMSG2(_("E216: No such event: %s"), pat);
else
EMSG2(_("E216: No such group or event: %s"), pat);
return NULL;
}
}
}
return pat;
}
static int
event_ignored(event)
event_T event;
{
char_u *p = p_ei;
while (*p != NUL)
{
if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ','))
return TRUE;
if (event_name2nr(p, &p) == event)
return TRUE;
}
return FALSE;
}
int
check_ei()
{
char_u *p = p_ei;
while (*p)
{
if (STRNICMP(p, "all", 3) == 0 && (p[3] == NUL || p[3] == ','))
{
p += 3;
if (*p == ',')
++p;
}
else if (event_name2nr(p, &p) == NUM_EVENTS)
return FAIL;
}
return OK;
}
# if defined(FEAT_SYN_HL) || defined(PROTO)
char_u *
au_event_disable(what)
char *what;
{
char_u *new_ei;
char_u *save_ei;
save_ei = vim_strsave(p_ei);
if (save_ei != NULL)
{
new_ei = vim_strnsave(p_ei, (int)(STRLEN(p_ei) + STRLEN(what)));
if (new_ei != NULL)
{
STRCAT(new_ei, what);
set_string_option_direct((char_u *)"ei", -1, new_ei,
OPT_FREE, SID_NONE);
vim_free(new_ei);
}
}
return save_ei;
}
void
au_event_restore(old_ei)
char_u *old_ei;
{
if (old_ei != NULL)
{
set_string_option_direct((char_u *)"ei", -1, old_ei,
OPT_FREE, SID_NONE);
vim_free(old_ei);
}
}
# endif
void
do_autocmd(arg, forceit)
char_u *arg;
int forceit;
{
char_u *pat;
char_u *envpat = NULL;
char_u *cmd;
event_T event;
int need_free = FALSE;
int nested = FALSE;
int group;
group = au_get_grouparg(&arg);
if (arg == NULL)
return;
pat = find_end_event(arg, group != AUGROUP_ALL);
if (pat == NULL)
return;
pat = skipwhite(pat);
cmd = pat;
while (*cmd && (!vim_iswhite(*cmd) || cmd[-1] == '\\'))
cmd++;
if (*cmd)
*cmd++ = NUL;
if (vim_strchr(pat, '$') != NULL || vim_strchr(pat, '~') != NULL)
{
#ifdef BACKSLASH_IN_FILENAME
int p_ssl_save = p_ssl;
p_ssl = TRUE;
#endif
envpat = expand_env_save(pat);
#ifdef BACKSLASH_IN_FILENAME
p_ssl = p_ssl_save;
#endif
if (envpat != NULL)
pat = envpat;
}
cmd = skipwhite(cmd);
if (*cmd != NUL && STRNCMP(cmd, "nested", 6) == 0 && vim_iswhite(cmd[6]))
{
nested = TRUE;
cmd = skipwhite(cmd + 6);
}
if (*cmd != NUL)
{
cmd = expand_sfile(cmd);
if (cmd == NULL)
return;
need_free = TRUE;
}
if (!forceit && *cmd == NUL)
{
MSG_PUTS_TITLE(_("\n--- Auto-Commands ---"));
}
last_event = (event_T)-1;
last_group = AUGROUP_ERROR;
if (*arg == '*' || *arg == NUL)
{
for (event = (event_T)0; (int)event < (int)NUM_EVENTS;
event = (event_T)((int)event + 1))
if (do_autocmd_event(event, pat,
nested, cmd, forceit, group) == FAIL)
break;
}
else
{
while (*arg && !vim_iswhite(*arg))
if (do_autocmd_event(event_name2nr(arg, &arg), pat,
nested, cmd, forceit, group) == FAIL)
break;
}
if (need_free)
vim_free(cmd);
vim_free(envpat);
}
static int
au_get_grouparg(argp)
char_u **argp;
{
char_u *group_name;
char_u *p;
char_u *arg = *argp;
int group = AUGROUP_ALL;
p = skiptowhite(arg);
if (p > arg)
{
group_name = vim_strnsave(arg, (int)(p - arg));
if (group_name == NULL)
return AUGROUP_ERROR;
group = au_find_group(group_name);
if (group == AUGROUP_ERROR)
group = AUGROUP_ALL;
else
*argp = skipwhite(p);
vim_free(group_name);
}
return group;
}
static int
do_autocmd_event(event, pat, nested, cmd, forceit, group)
event_T event;
char_u *pat;
int nested;
char_u *cmd;
int forceit;
int group;
{
AutoPat *ap;
AutoPat **prev_ap;
AutoCmd *ac;
AutoCmd **prev_ac;
int brace_level;
char_u *endpat;
int findgroup;
int allgroups;
int patlen;
int is_buflocal;
int buflocal_nr;
char_u buflocal_pat[25];
if (group == AUGROUP_ALL)
findgroup = current_augroup;
else
findgroup = group;
allgroups = (group == AUGROUP_ALL && !forceit && *cmd == NUL);
if (*pat == NUL)
{
for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
{
if (forceit)
{
if (ap->group == findgroup)
au_remove_pat(ap);
}
else if (group == AUGROUP_ALL || ap->group == group)
show_autocmd(ap, event);
}
}
for ( ; *pat; pat = (*endpat == ',' ? endpat + 1 : endpat))
{
brace_level = 0;
for (endpat = pat; *endpat && (*endpat != ',' || brace_level
|| endpat[-1] == '\\'); ++endpat)
{
if (*endpat == '{')
brace_level++;
else if (*endpat == '}')
brace_level--;
}
if (pat == endpat)
continue;
patlen = (int)(endpat - pat);
is_buflocal = FALSE;
buflocal_nr = 0;
if (patlen >= 7 && STRNCMP(pat, "<buffer", 7) == 0
&& pat[patlen - 1] == '>')
{
is_buflocal = TRUE;
if (patlen == 8)
buflocal_nr = curbuf->b_fnum;
else if (patlen > 9 && pat[7] == '=')
{
if (patlen == 13 && STRNICMP(pat, "<buffer=abuf>", 13))
buflocal_nr = autocmd_bufnr;
else if (skipdigits(pat + 8) == pat + patlen - 1)
buflocal_nr = atoi((char *)pat + 8);
}
}
if (is_buflocal)
{
sprintf((char *)buflocal_pat, "<buffer=%d>", buflocal_nr);
pat = buflocal_pat;
patlen = (int)STRLEN(buflocal_pat);
}
prev_ap = &first_autopat[(int)event];
while ((ap = *prev_ap) != NULL)
{
if (ap->pat != NULL)
{
if ((allgroups || ap->group == findgroup)
&& ap->patlen == patlen
&& STRNCMP(pat, ap->pat, patlen) == 0)
{
if (forceit)
{
if (*cmd != NUL && ap->next == NULL)
{
au_remove_cmds(ap);
break;
}
au_remove_pat(ap);
}
else if (*cmd == NUL)
show_autocmd(ap, event);
else if (ap->next == NULL)
break;
}
}
prev_ap = &ap->next;
}
if (*cmd != NUL)
{
if (ap == NULL)
{
if (is_buflocal && (buflocal_nr == 0
|| buflist_findnr(buflocal_nr) == NULL))
{
EMSGN(_("E680: <buffer=%d>: invalid buffer number "),
buflocal_nr);
return FAIL;
}
ap = (AutoPat *)alloc((unsigned)sizeof(AutoPat));
if (ap == NULL)
return FAIL;
ap->pat = vim_strnsave(pat, patlen);
ap->patlen = patlen;
if (ap->pat == NULL)
{
vim_free(ap);
return FAIL;
}
if (is_buflocal)
{
ap->buflocal_nr = buflocal_nr;
ap->reg_prog = NULL;
}
else
{
char_u *reg_pat;
ap->buflocal_nr = 0;
reg_pat = file_pat_to_reg_pat(pat, endpat,
&ap->allow_dirs, TRUE);
if (reg_pat != NULL)
ap->reg_prog = vim_regcomp(reg_pat, RE_MAGIC);
vim_free(reg_pat);
if (reg_pat == NULL || ap->reg_prog == NULL)
{
vim_free(ap->pat);
vim_free(ap);
return FAIL;
}
}
ap->cmds = NULL;
*prev_ap = ap;
ap->next = NULL;
if (group == AUGROUP_ALL)
ap->group = current_augroup;
else
ap->group = group;
}
prev_ac = &(ap->cmds);
while ((ac = *prev_ac) != NULL)
prev_ac = &ac->next;
ac = (AutoCmd *)alloc((unsigned)sizeof(AutoCmd));
if (ac == NULL)
return FAIL;
ac->cmd = vim_strsave(cmd);
#ifdef FEAT_EVAL
ac->scriptID = current_SID;
#endif
if (ac->cmd == NULL)
{
vim_free(ac);
return FAIL;
}
ac->next = NULL;
*prev_ac = ac;
ac->nested = nested;
}
}
au_cleanup();
return OK;
}
int
do_doautocmd(arg, do_msg)
char_u *arg;
int do_msg;
{
char_u *fname;
int nothing_done = TRUE;
int group;
group = au_get_grouparg(&arg);
if (arg == NULL)
return FAIL;
if (*arg == '*')
{
EMSG(_("E217: Can't execute autocommands for ALL events"));
return FAIL;
}
fname = find_end_event(arg, group != AUGROUP_ALL);
if (fname == NULL)
return FAIL;
fname = skipwhite(fname);
while (*arg && !vim_iswhite(*arg))
if (apply_autocmds_group(event_name2nr(arg, &arg),
fname, NULL, TRUE, group, curbuf, NULL))
nothing_done = FALSE;
if (nothing_done && do_msg)
MSG(_("No matching autocommands"));
#ifdef FEAT_EVAL
return aborting() ? FAIL : OK;
#else
return OK;
#endif
}
void
ex_doautoall(eap)
exarg_T *eap;
{
int retval;
aco_save_T aco;
buf_T *buf;
for (buf = firstbuf; buf != NULL; buf = buf->b_next)
{
if (buf->b_ml.ml_mfp != NULL)
{
aucmd_prepbuf(&aco, buf);
retval = do_doautocmd(eap->arg, FALSE);
do_modelines(aco.save_curwin == NULL ? OPT_NOWIN : 0);
aucmd_restbuf(&aco);
if (retval == FAIL || !buf_valid(buf))
break;
}
}
check_cursor();
}
void
aucmd_prepbuf(aco, buf)
aco_save_T *aco;
buf_T *buf;
{
win_T *win;
aco->new_curbuf = buf;
if (buf == curbuf)
win = curwin;
else
#ifdef FEAT_WINDOWS
for (win = firstwin; win != NULL; win = win->w_next)
if (win->w_buffer == buf)
break;
#else
win = NULL;
#endif
if (win != NULL)
{
aco->save_curwin = curwin;
curwin = win;
aco->save_buf = win->w_buffer;
aco->new_curwin = win;
}
else
{
aco->save_curwin = NULL;
aco->save_buf = curbuf;
--curbuf->b_nwindows;
curwin->w_buffer = buf;
++buf->b_nwindows;
aco->save_cursor = curwin->w_cursor;
curwin->w_cursor.lnum = 1;
curwin->w_cursor.col = 0;
aco->save_topline = curwin->w_topline;
curwin->w_topline = 1;
#ifdef FEAT_DIFF
aco->save_topfill = curwin->w_topfill;
curwin->w_topfill = 0;
#endif
}
curbuf = buf;
}
void
aucmd_restbuf(aco)
aco_save_T *aco;
{
if (aco->save_curwin != NULL)
{
#ifdef FEAT_WINDOWS
if (win_valid(aco->save_curwin))
#endif
{
if (curwin == aco->new_curwin
&& buf_valid(aco->save_buf)
&& aco->save_buf->b_ml.ml_mfp != NULL)
{
--curbuf->b_nwindows;
curbuf = aco->save_buf;
curwin->w_buffer = curbuf;
++curbuf->b_nwindows;
}
curwin = aco->save_curwin;
curbuf = curwin->w_buffer;
}
}
else
{
if (buf_valid(aco->save_buf) && aco->save_buf->b_ml.ml_mfp != NULL)
{
--curbuf->b_nwindows;
curbuf = aco->save_buf;
curwin->w_buffer = curbuf;
++curbuf->b_nwindows;
curwin->w_cursor = aco->save_cursor;
check_cursor();
if (aco->save_topline <= curbuf->b_ml.ml_line_count)
{
curwin->w_topline = aco->save_topline;
#ifdef FEAT_DIFF
curwin->w_topfill = aco->save_topfill;
#endif
}
else
{
curwin->w_topline = curbuf->b_ml.ml_line_count;
#ifdef FEAT_DIFF
curwin->w_topfill = 0;
#endif
}
}
}
}
static int autocmd_nested = FALSE;
int
apply_autocmds(event, fname, fname_io, force, buf)
event_T event;
char_u *fname;
char_u *fname_io;
int force;
buf_T *buf;
{
return apply_autocmds_group(event, fname, fname_io, force,
AUGROUP_ALL, buf, NULL);
}
static int
apply_autocmds_exarg(event, fname, fname_io, force, buf, eap)
event_T event;
char_u *fname;
char_u *fname_io;
int force;
buf_T *buf;
exarg_T *eap;
{
return apply_autocmds_group(event, fname, fname_io, force,
AUGROUP_ALL, buf, eap);
}
int
apply_autocmds_retval(event, fname, fname_io, force, buf, retval)
event_T event;
char_u *fname;
char_u *fname_io;
int force;
buf_T *buf;
int *retval;
{
int did_cmd;
#ifdef FEAT_EVAL
if (should_abort(*retval))
return FALSE;
#endif
did_cmd = apply_autocmds_group(event, fname, fname_io, force,
AUGROUP_ALL, buf, NULL);
if (did_cmd
#ifdef FEAT_EVAL
&& aborting()
#endif
)
*retval = FAIL;
return did_cmd;
}
int
has_cursorhold()
{
return (first_autopat[(int)(get_real_state() == NORMAL_BUSY
? EVENT_CURSORHOLD : EVENT_CURSORHOLDI)] != NULL);
}
int
trigger_cursorhold()
{
int state;
if (!did_cursorhold && has_cursorhold() && !Recording
#ifdef FEAT_INS_EXPAND
&& !ins_compl_active()
#endif
)
{
state = get_real_state();
if (state == NORMAL_BUSY || (state & INSERT) != 0)
return TRUE;
}
return FALSE;
}
int
has_cursormoved()
{
return (first_autopat[(int)EVENT_CURSORMOVED] != NULL);
}
int
has_cursormovedI()
{
return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
}
static int
apply_autocmds_group(event, fname, fname_io, force, group, buf, eap)
event_T event;
char_u *fname;
char_u *fname_io;
int force;
int group;
buf_T *buf;
exarg_T *eap;
{
char_u *sfname = NULL;
char_u *tail;
int save_changed;
buf_T *old_curbuf;
int retval = FALSE;
char_u *save_sourcing_name;
linenr_T save_sourcing_lnum;
char_u *save_autocmd_fname;
int save_autocmd_fname_full;
int save_autocmd_bufnr;
char_u *save_autocmd_match;
int save_autocmd_busy;
int save_autocmd_nested;
static int nesting = 0;
AutoPatCmd patcmd;
AutoPat *ap;
#ifdef FEAT_EVAL
scid_T save_current_SID;
void *save_funccalp;
char_u *save_cmdarg;
long save_cmdbang;
#endif
static int filechangeshell_busy = FALSE;
#ifdef FEAT_PROFILE
proftime_T wait_time;
#endif
if (first_autopat[(int)event] == NULL || autocmd_blocked > 0)
goto BYPASS_AU;
if (autocmd_busy && !(force || autocmd_nested))
goto BYPASS_AU;
#ifdef FEAT_EVAL
if (aborting())
goto BYPASS_AU;
#endif
if (filechangeshell_busy && (event == EVENT_FILECHANGEDSHELL
|| event == EVENT_FILECHANGEDSHELLPOST))
goto BYPASS_AU;
if (event_ignored(event))
goto BYPASS_AU;
if (nesting == 10)
{
EMSG(_("E218: autocommand nesting too deep"));
goto BYPASS_AU;
}
if ( (autocmd_no_enter
&& (event == EVENT_WINENTER || event == EVENT_BUFENTER))
|| (autocmd_no_leave
&& (event == EVENT_WINLEAVE || event == EVENT_BUFLEAVE)))
goto BYPASS_AU;
save_autocmd_fname = autocmd_fname;
save_autocmd_fname_full = autocmd_fname_full;
save_autocmd_bufnr = autocmd_bufnr;
save_autocmd_match = autocmd_match;
save_autocmd_busy = autocmd_busy;
save_autocmd_nested = autocmd_nested;
save_changed = curbuf->b_changed;
old_curbuf = curbuf;
if (fname_io == NULL)
{
if (fname != NULL && *fname != NUL)
autocmd_fname = fname;
else if (buf != NULL)
autocmd_fname = buf->b_ffname;
else
autocmd_fname = NULL;
}
else
autocmd_fname = fname_io;
if (autocmd_fname != NULL)
autocmd_fname = vim_strsave(autocmd_fname);
autocmd_fname_full = FALSE;
if (buf == NULL)
autocmd_bufnr = 0;
else
autocmd_bufnr = buf->b_fnum;
if (fname == NULL || *fname == NUL)
{
if (buf == NULL)
fname = NULL;
else
{
#ifdef FEAT_SYN_HL
if (event == EVENT_SYNTAX)
fname = buf->b_p_syn;
else
#endif
if (event == EVENT_FILETYPE)
fname = buf->b_p_ft;
else
{
if (buf->b_sfname != NULL)
sfname = vim_strsave(buf->b_sfname);
fname = buf->b_ffname;
}
}
if (fname == NULL)
fname = (char_u *)"";
fname = vim_strsave(fname);
}
else
{
sfname = vim_strsave(fname);
if (event == EVENT_FILETYPE
|| event == EVENT_SYNTAX
|| event == EVENT_REMOTEREPLY
|| event == EVENT_SPELLFILEMISSING
|| event == EVENT_QUICKFIXCMDPRE
|| event == EVENT_QUICKFIXCMDPOST)
fname = vim_strsave(fname);
else
fname = FullName_save(fname, FALSE);
}
if (fname == NULL)
{
vim_free(sfname);
retval = FALSE;
goto BYPASS_AU;
}
#ifdef BACKSLASH_IN_FILENAME
if (sfname != NULL)
forward_slash(sfname);
forward_slash(fname);
#endif
#ifdef VMS
if (sfname != NULL)
vms_remove_version(sfname);
vms_remove_version(fname);
#endif
autocmd_match = fname;
++RedrawingDisabled;
save_sourcing_name = sourcing_name;
sourcing_name = NULL;
save_sourcing_lnum = sourcing_lnum;
sourcing_lnum = 0;
#ifdef FEAT_EVAL
save_current_SID = current_SID;
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
prof_child_enter(&wait_time);
# endif
save_funccalp = save_funccal();
#endif
if (!autocmd_busy)
{
save_search_patterns();
saveRedobuff();
did_filetype = keep_filetype;
}
autocmd_busy = TRUE;
filechangeshell_busy = (event == EVENT_FILECHANGEDSHELL);
++nesting;
if (event == EVENT_FILETYPE)
did_filetype = TRUE;
tail = gettail(fname);
patcmd.curpat = first_autopat[(int)event];
patcmd.nextcmd = NULL;
patcmd.group = group;
patcmd.fname = fname;
patcmd.sfname = sfname;
patcmd.tail = tail;
patcmd.event = event;
patcmd.arg_bufnr = autocmd_bufnr;
patcmd.next = NULL;
auto_next_pat(&patcmd, FALSE);
if (patcmd.curpat != NULL)
{
patcmd.next = active_apc_list;
active_apc_list = &patcmd;
#ifdef FEAT_EVAL
save_cmdbang = get_vim_var_nr(VV_CMDBANG);
if (eap != NULL)
{
save_cmdarg = set_cmdarg(eap, NULL);
set_vim_var_nr(VV_CMDBANG, (long)eap->forceit);
}
else
save_cmdarg = NULL;
#endif
retval = TRUE;
for (ap = patcmd.curpat; ap->next != NULL; ap = ap->next)
ap->last = FALSE;
ap->last = TRUE;
check_lnums(TRUE);
do_cmdline(NULL, getnextac, (void *)&patcmd,
DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
#ifdef FEAT_EVAL
if (eap != NULL)
{
(void)set_cmdarg(NULL, save_cmdarg);
set_vim_var_nr(VV_CMDBANG, save_cmdbang);
}
#endif
if (active_apc_list == &patcmd)
active_apc_list = patcmd.next;
}
--RedrawingDisabled;
autocmd_busy = save_autocmd_busy;
filechangeshell_busy = FALSE;
autocmd_nested = save_autocmd_nested;
vim_free(sourcing_name);
sourcing_name = save_sourcing_name;
sourcing_lnum = save_sourcing_lnum;
vim_free(autocmd_fname);
autocmd_fname = save_autocmd_fname;
autocmd_fname_full = save_autocmd_fname_full;
autocmd_bufnr = save_autocmd_bufnr;
autocmd_match = save_autocmd_match;
#ifdef FEAT_EVAL
current_SID = save_current_SID;
restore_funccal(save_funccalp);
# ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
prof_child_exit(&wait_time);
# endif
#endif
vim_free(fname);
vim_free(sfname);
--nesting;
if (!autocmd_busy)
{
restore_search_patterns();
restoreRedobuff();
did_filetype = FALSE;
}
if (curbuf == old_curbuf
&& (event == EVENT_BUFREADPOST
|| event == EVENT_BUFWRITEPOST
|| event == EVENT_FILEAPPENDPOST
|| event == EVENT_VIMLEAVE
|| event == EVENT_VIMLEAVEPRE))
{
#ifdef FEAT_TITLE
if (curbuf->b_changed != save_changed)
need_maketitle = TRUE;
#endif
curbuf->b_changed = save_changed;
}
au_cleanup();
BYPASS_AU:
if (event == EVENT_BUFWIPEOUT && buf != NULL)
aubuflocal_remove(buf);
return retval;
}
# ifdef FEAT_EVAL
static char_u *old_termresponse = NULL;
# endif
void
block_autocmds()
{
# ifdef FEAT_EVAL
if (autocmd_blocked == 0)
old_termresponse = get_vim_var_str(VV_TERMRESPONSE);
# endif
++autocmd_blocked;
}
void
unblock_autocmds()
{
--autocmd_blocked;
# ifdef FEAT_EVAL
if (autocmd_blocked == 0
&& get_vim_var_str(VV_TERMRESPONSE) != old_termresponse)
apply_autocmds(EVENT_TERMRESPONSE, NULL, NULL, FALSE, curbuf);
# endif
}
static void
auto_next_pat(apc, stop_at_last)
AutoPatCmd *apc;
int stop_at_last;
{
AutoPat *ap;
AutoCmd *cp;
char_u *name;
char *s;
vim_free(sourcing_name);
sourcing_name = NULL;
for (ap = apc->curpat; ap != NULL && !got_int; ap = ap->next)
{
apc->curpat = NULL;
if (ap->pat != NULL && ap->cmds != NULL
&& (apc->group == AUGROUP_ALL || apc->group == ap->group))
{
if (ap->buflocal_nr == 0
? (match_file_pat(NULL, ap->reg_prog, apc->fname,
apc->sfname, apc->tail, ap->allow_dirs))
: ap->buflocal_nr == apc->arg_bufnr)
{
name = event_nr2name(apc->event);
s = _("%s Auto commands for \"%s\"");
sourcing_name = alloc((unsigned)(STRLEN(s)
+ STRLEN(name) + ap->patlen + 1));
if (sourcing_name != NULL)
{
sprintf((char *)sourcing_name, s,
(char *)name, (char *)ap->pat);
if (p_verbose >= 8)
{
verbose_enter();
smsg((char_u *)_("Executing %s"), sourcing_name);
verbose_leave();
}
}
apc->curpat = ap;
apc->nextcmd = ap->cmds;
for (cp = ap->cmds; cp->next != NULL; cp = cp->next)
cp->last = FALSE;
cp->last = TRUE;
}
line_breakcheck();
if (apc->curpat != NULL)
break;
}
if (stop_at_last && ap->last)
break;
}
}
static char_u *
getnextac(c, cookie, indent)
int c;
void *cookie;
int indent;
{
AutoPatCmd *acp = (AutoPatCmd *)cookie;
char_u *retval;
AutoCmd *ac;
if (acp->curpat == NULL)
return NULL;
for (;;)
{
while (acp->nextcmd != NULL && acp->nextcmd->cmd == NULL)
if (acp->nextcmd->last)
acp->nextcmd = NULL;
else
acp->nextcmd = acp->nextcmd->next;
if (acp->nextcmd != NULL)
break;
if (acp->curpat->last)
acp->curpat = NULL;
else
acp->curpat = acp->curpat->next;
if (acp->curpat != NULL)
auto_next_pat(acp, TRUE);
if (acp->curpat == NULL)
return NULL;
}
ac = acp->nextcmd;
if (p_verbose >= 9)
{
verbose_enter_scroll();
smsg((char_u *)_("autocommand %s"), ac->cmd);
msg_puts((char_u *)"\n");
verbose_leave_scroll();
}
retval = vim_strsave(ac->cmd);
autocmd_nested = ac->nested;
#ifdef FEAT_EVAL
current_SID = ac->scriptID;
#endif
if (ac->last)
acp->nextcmd = NULL;
else
acp->nextcmd = ac->next;
return retval;
}
int
has_autocmd(event, sfname, buf)
event_T event;
char_u *sfname;
buf_T *buf;
{
AutoPat *ap;
char_u *fname;
char_u *tail = gettail(sfname);
int retval = FALSE;
fname = FullName_save(sfname, FALSE);
if (fname == NULL)
return FALSE;
#ifdef BACKSLASH_IN_FILENAME
sfname = vim_strsave(sfname);
if (sfname != NULL)
forward_slash(sfname);
forward_slash(fname);
#endif
for (ap = first_autopat[(int)event]; ap != NULL; ap = ap->next)
if (ap->pat != NULL && ap->cmds != NULL
&& (ap->buflocal_nr == 0
? match_file_pat(NULL, ap->reg_prog,
fname, sfname, tail, ap->allow_dirs)
: buf != NULL && ap->buflocal_nr == buf->b_fnum
))
{
retval = TRUE;
break;
}
vim_free(fname);
#ifdef BACKSLASH_IN_FILENAME
vim_free(sfname);
#endif
return retval;
}
#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
char_u *
get_augroup_name(xp, idx)
expand_T *xp;
int idx;
{
if (idx == augroups.ga_len)
return (char_u *)"END";
if (idx >= augroups.ga_len)
return NULL;
if (AUGROUP_NAME(idx) == NULL)
return (char_u *)"";
return AUGROUP_NAME(idx);
}
static int include_groups = FALSE;
char_u *
set_context_in_autocmd(xp, arg, doautocmd)
expand_T *xp;
char_u *arg;
int doautocmd;
{
char_u *p;
int group;
include_groups = FALSE;
p = arg;
group = au_get_grouparg(&arg);
if (group == AUGROUP_ERROR)
return NULL;
if (*arg == NUL && group != AUGROUP_ALL && !vim_iswhite(arg[-1]))
{
arg = p;
group = AUGROUP_ALL;
}
for (p = arg; *p != NUL && !vim_iswhite(*p); ++p)
if (*p == ',')
arg = p + 1;
if (*p == NUL)
{
if (group == AUGROUP_ALL)
include_groups = TRUE;
xp->xp_context = EXPAND_EVENTS;
xp->xp_pattern = arg;
return NULL;
}
arg = skipwhite(p);
while (*arg && (!vim_iswhite(*arg) || arg[-1] == '\\'))
arg++;
if (*arg)
return arg;
if (doautocmd)
xp->xp_context = EXPAND_FILES;
else
xp->xp_context = EXPAND_NOTHING;
return NULL;
}
char_u *
get_event_name(xp, idx)
expand_T *xp;
int idx;
{
if (idx < augroups.ga_len)
{
if (!include_groups || AUGROUP_NAME(idx) == NULL)
return (char_u *)"";
return AUGROUP_NAME(idx);
}
return (char_u *)event_names[idx - augroups.ga_len].name;
}
#endif
int
autocmd_supported(name)
char_u *name;
{
char_u *p;
return (event_name2nr(name, &p) != NUM_EVENTS);
}
int
au_exists(arg)
char_u *arg;
{
char_u *arg_save;
char_u *pattern = NULL;
char_u *event_name;
char_u *p;
event_T event;
AutoPat *ap;
buf_T *buflocal_buf = NULL;
int group;
int retval = FALSE;
arg_save = vim_strsave(arg);
if (arg_save == NULL)
return FALSE;
p = vim_strchr(arg_save, '#');
if (p != NULL)
*p++ = NUL;
group = au_find_group(arg_save);
if (group == AUGROUP_ERROR)
{
group = AUGROUP_ALL;
event_name = arg_save;
}
else
{
if (p == NULL)
{
retval = TRUE;
goto theend;
}
event_name = p;
p = vim_strchr(event_name, '#');
if (p != NULL)
*p++ = NUL;
}
pattern = p;
event = event_name2nr(event_name, &p);
if (event == NUM_EVENTS)
goto theend;
ap = first_autopat[(int)event];
if (ap == NULL)
goto theend;
if (pattern == NULL)
{
retval = TRUE;
goto theend;
}
if (STRICMP(pattern, "<buffer>") == 0)
buflocal_buf = curbuf;
for ( ; ap != NULL; ap = ap->next)
if (ap->pat != NULL && ap->cmds != NULL
&& (group == AUGROUP_ALL || ap->group == group)
&& (buflocal_buf == NULL
? fnamecmp(ap->pat, pattern) == 0
: ap->buflocal_nr == buflocal_buf->b_fnum))
{
retval = TRUE;
break;
}
theend:
vim_free(arg_save);
return retval;
}
#else
void
aucmd_prepbuf(aco, buf)
aco_save_T *aco;
buf_T *buf;
{
aco->save_buf = curbuf;
curbuf = buf;
curwin->w_buffer = buf;
}
void
aucmd_restbuf(aco)
aco_save_T *aco;
{
curbuf = aco->save_buf;
curwin->w_buffer = curbuf;
}
#endif
#if defined(FEAT_AUTOCMD) || defined(FEAT_WILDIGN) || defined(PROTO)
int
match_file_pat(pattern, prog, fname, sfname, tail, allow_dirs)
char_u *pattern;
regprog_T *prog;
char_u *fname;
char_u *sfname;
char_u *tail;
int allow_dirs;
{
regmatch_T regmatch;
int result = FALSE;
#ifdef FEAT_OSFILETYPE
int no_pattern = FALSE;
char_u *type_start;
char_u c;
int match = FALSE;
#endif
#ifdef CASE_INSENSITIVE_FILENAME
regmatch.rm_ic = TRUE;
#else
regmatch.rm_ic = FALSE;
#endif
#ifdef FEAT_OSFILETYPE
if (*pattern == '<')
{
for (type_start = pattern + 1; (c = *pattern); pattern++)
{
if ((c == ';' || c == '>') && match == FALSE)
{
*pattern = NUL;
match = mch_check_filetype(fname, type_start);
*pattern = c;
type_start = pattern + 1;
}
if (c == '>')
break;
}
if (match == FALSE || c == NUL)
regmatch.regprog = NULL;
else if (*pattern == NUL)
{
regmatch.regprog = NULL;
no_pattern = TRUE;
}
else
regmatch.regprog = vim_regcomp(pattern + 1, RE_MAGIC);
}
else
#endif
{
if (prog != NULL)
regmatch.regprog = prog;
else
regmatch.regprog = vim_regcomp(pattern, RE_MAGIC);
}
if (
#ifdef FEAT_OSFILETYPE
no_pattern ||
#endif
(regmatch.regprog != NULL
&& ((allow_dirs
&& (vim_regexec(®match, fname, (colnr_T)0)
|| (sfname != NULL
&& vim_regexec(®match, sfname, (colnr_T)0))))
|| (!allow_dirs && vim_regexec(®match, tail, (colnr_T)0)))))
result = TRUE;
if (prog == NULL)
vim_free(regmatch.regprog);
return result;
}
#endif
#if defined(FEAT_WILDIGN) || defined(PROTO)
int
match_file_list(list, sfname, ffname)
char_u *list;
char_u *sfname;
char_u *ffname;
{
char_u buf[100];
char_u *tail;
char_u *regpat;
char allow_dirs;
int match;
char_u *p;
tail = gettail(sfname);
p = list;
while (*p)
{
copy_option_part(&p, buf, 100, ",");
regpat = file_pat_to_reg_pat(buf, NULL, &allow_dirs, FALSE);
if (regpat == NULL)
break;
match = match_file_pat(regpat, NULL, ffname, sfname,
tail, (int)allow_dirs);
vim_free(regpat);
if (match)
return TRUE;
}
return FALSE;
}
#endif
char_u *
file_pat_to_reg_pat(pat, pat_end, allow_dirs, no_bslash)
char_u *pat;
char_u *pat_end;
char *allow_dirs;
int no_bslash;
{
int size;
char_u *endp;
char_u *reg_pat;
char_u *p;
int i;
int nested = 0;
int add_dollar = TRUE;
#ifdef FEAT_OSFILETYPE
int check_length = 0;
#endif
if (allow_dirs != NULL)
*allow_dirs = FALSE;
if (pat_end == NULL)
pat_end = pat + STRLEN(pat);
#ifdef FEAT_OSFILETYPE
if (*pat == '<')
{
for (p = pat + 1; p < pat_end && *p != '>'; p++)
;
if (p < pat_end)
{
check_length = p - pat + 1;
if (p + 1 >= pat_end)
{
reg_pat = (char_u *)alloc(check_length + 1);
if (reg_pat != NULL)
{
mch_memmove(reg_pat, pat, (size_t)check_length);
reg_pat[check_length] = NUL;
}
return reg_pat;
}
}
}
pat += check_length;
size = 2 + check_length;
#else
size = 2;
#endif
for (p = pat; p < pat_end; p++)
{
switch (*p)
{
case '*':
case '.':
case ',':
case '{':
case '}':
case '~':
size += 2;
break;
#ifdef BACKSLASH_IN_FILENAME
case '\\':
case '/':
size += 4;
break;
#endif
default:
size++;
# ifdef FEAT_MBYTE
if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
{
++p;
++size;
}
# endif
break;
}
}
reg_pat = alloc(size + 1);
if (reg_pat == NULL)
return NULL;
#ifdef FEAT_OSFILETYPE
if (check_length)
mch_memmove(reg_pat, pat - check_length, (size_t)check_length);
i = check_length;
#else
i = 0;
#endif
if (pat[0] == '*')
while (pat[0] == '*' && pat < pat_end - 1)
pat++;
else
reg_pat[i++] = '^';
endp = pat_end - 1;
if (*endp == '*')
{
while (endp - pat > 0 && *endp == '*')
endp--;
add_dollar = FALSE;
}
for (p = pat; *p && nested >= 0 && p <= endp; p++)
{
switch (*p)
{
case '*':
reg_pat[i++] = '.';
reg_pat[i++] = '*';
while (p[1] == '*')
++p;
break;
case '.':
#ifdef RISCOS
if (allow_dirs != NULL)
*allow_dirs = TRUE;
#endif
case '~':
reg_pat[i++] = '\\';
reg_pat[i++] = *p;
break;
case '?':
#ifdef RISCOS
case '#':
#endif
reg_pat[i++] = '.';
break;
case '\\':
if (p[1] == NUL)
break;
#ifdef BACKSLASH_IN_FILENAME
if (!no_bslash)
{
if ((vim_isfilec(p[1]) || p[1] == '*' || p[1] == '?')
&& p[1] != '+')
{
reg_pat[i++] = '[';
reg_pat[i++] = '\\';
reg_pat[i++] = '/';
reg_pat[i++] = ']';
if (allow_dirs != NULL)
*allow_dirs = TRUE;
break;
}
}
#endif
if (*++p == '?'
#ifdef BACKSLASH_IN_FILENAME
&& no_bslash
#endif
)
reg_pat[i++] = '?';
else
if (*p == ',')
reg_pat[i++] = ',';
else
{
if (allow_dirs != NULL && vim_ispathsep(*p)
#ifdef BACKSLASH_IN_FILENAME
&& (!no_bslash || *p != '\\')
#endif
)
*allow_dirs = TRUE;
reg_pat[i++] = '\\';
reg_pat[i++] = *p;
}
break;
#ifdef BACKSLASH_IN_FILENAME
case '/':
reg_pat[i++] = '[';
reg_pat[i++] = '\\';
reg_pat[i++] = '/';
reg_pat[i++] = ']';
if (allow_dirs != NULL)
*allow_dirs = TRUE;
break;
#endif
case '{':
reg_pat[i++] = '\\';
reg_pat[i++] = '(';
nested++;
break;
case '}':
reg_pat[i++] = '\\';
reg_pat[i++] = ')';
--nested;
break;
case ',':
if (nested)
{
reg_pat[i++] = '\\';
reg_pat[i++] = '|';
}
else
reg_pat[i++] = ',';
break;
default:
# ifdef FEAT_MBYTE
if (enc_dbcs != 0 && (*mb_ptr2len)(p) > 1)
reg_pat[i++] = *p++;
else
# endif
if (allow_dirs != NULL && vim_ispathsep(*p))
*allow_dirs = TRUE;
reg_pat[i++] = *p;
break;
}
}
if (add_dollar)
reg_pat[i++] = '$';
reg_pat[i] = NUL;
if (nested != 0)
{
if (nested < 0)
EMSG(_("E219: Missing {."));
else
EMSG(_("E220: Missing }."));
vim_free(reg_pat);
reg_pat = NULL;
}
return reg_pat;
}