diff options
34 files changed, 2900 insertions, 2073 deletions
diff --git a/.gitignore b/.gitignore index 5ada7d171a..d04b91dbb7 100644 --- a/.gitignore +++ b/.gitignore @@ -43,6 +43,7 @@ compile_commands.json /test/old/testdir/test*.res /test/old/testdir/test*.log /test/old/testdir/messages +/test/old/testdir/starttime /test/old/testdir/viminfo /test/old/testdir/test.ok /test/old/testdir/*.failed diff --git a/cmake.config/iwyu/mapping.imp b/cmake.config/iwyu/mapping.imp index 4e55aa9875..22710d8571 100644 --- a/cmake.config/iwyu/mapping.imp +++ b/cmake.config/iwyu/mapping.imp @@ -20,6 +20,7 @@ { include: [ '"autocmd.h.generated.h"', private, '"nvim/autocmd.h"', public ] }, { include: [ '"buffer.h.generated.h"', private, '"nvim/buffer.h"', public ] }, { include: [ '"buffer_updates.h.generated.h"', private, '"nvim/buffer_updates.h"', public ] }, + { include: [ '"bufwrite.h.generated.h"', private, '"nvim/bufwrite.h"', public ] }, { include: [ '"change.h.generated.h"', private, '"nvim/change.h"', public ] }, { include: [ '"channel.h.generated.h"', private, '"nvim/channel.h"', public ] }, { include: [ '"charset.h.generated.h"', private, '"nvim/charset.h"', public ] }, diff --git a/runtime/doc/builtin.txt b/runtime/doc/builtin.txt index 15ccfd9b92..9a8ce4d2ad 100644 --- a/runtime/doc/builtin.txt +++ b/runtime/doc/builtin.txt @@ -506,6 +506,7 @@ submatch({nr} [, {list}]) String or List specific match in ":s" or substitute() substitute({expr}, {pat}, {sub}, {flags}) String all {pat} in {expr} replaced with {sub} +swapfilelist() List swap files found in 'directory' swapinfo({fname}) Dict information about swap file {fname} swapname({buf}) String swap file of buffer {buf} synID({lnum}, {col}, {trans}) Number syntax ID at {lnum} and {col} @@ -8417,6 +8418,17 @@ substitute({string}, {pat}, {sub}, {flags}) *substitute()* Can also be used as a |method|: > GetString()->substitute(pat, sub, flags) +swapfilelist() *swapfilelist()* + Returns a list of swap file names, like what "vim -r" shows. + See the |-r| command argument. The 'directory' option is used + for the directories to inspect. If you only want to get a + list of swap files in the current directory then temporarily + set 'directory' to a dot: > + let save_dir = &directory + let &directory = '.' + let swapfiles = swapfilelist() + let &directory = save_dir + swapinfo({fname}) *swapinfo()* The result is a dictionary, which holds information about the swapfile {fname}. The available fields are: diff --git a/runtime/doc/usr_41.txt b/runtime/doc/usr_41.txt index 17a34b1236..89111535ca 100644 --- a/runtime/doc/usr_41.txt +++ b/runtime/doc/usr_41.txt @@ -883,6 +883,7 @@ Buffers, windows and the argument list: getwininfo() get a list with window information getchangelist() get a list of change list entries getjumplist() get a list of jump list entries + swapfilelist() list of existing swap files in 'directory' swapinfo() information about a swap file swapname() get the swap file path of a buffer diff --git a/runtime/lua/vim/filetype.lua b/runtime/lua/vim/filetype.lua index 74ab7d8260..6cfe6e6c35 100644 --- a/runtime/lua/vim/filetype.lua +++ b/runtime/lua/vim/filetype.lua @@ -1469,6 +1469,7 @@ local filename = { ['.gprc'] = 'gp', ['/.gnupg/gpg.conf'] = 'gpg', ['/.gnupg/options'] = 'gpg', + ['Jenkinsfile'] = 'groovy', ['/var/backups/gshadow.bak'] = 'group', ['/etc/gshadow'] = 'group', ['/etc/group-'] = 'group', diff --git a/runtime/lua/vim/filetype/detect.lua b/runtime/lua/vim/filetype/detect.lua index d1eabadc4a..94114ae7c3 100644 --- a/runtime/lua/vim/filetype/detect.lua +++ b/runtime/lua/vim/filetype/detect.lua @@ -1550,8 +1550,15 @@ local patterns_text = { ['^SNNS pattern definition file'] = 'snnspat', ['^SNNS result file'] = 'snnsres', ['^%%.-[Vv]irata'] = { 'virata', { start_lnum = 1, end_lnum = 5 } }, - ['[0-9:%.]* *execve%('] = 'strace', - ['^__libc_start_main'] = 'strace', + function(lines) + if + -- inaccurate fast match first, then use accurate slow match + (lines[1]:find('execve%(') and lines[1]:find('^[0-9:%.]* *execve%(')) + or lines[1]:find('^__libc_start_main') + then + return 'strace' + end + end, -- VSE JCL ['^\\* $$ JOB\\>'] = { 'vsejcl', { vim_regex = true } }, ['^// *JOB\\>'] = { 'vsejcl', { vim_regex = true } }, diff --git a/src/nvim/bufwrite.c b/src/nvim/bufwrite.c new file mode 100644 index 0000000000..89d63a39d5 --- /dev/null +++ b/src/nvim/bufwrite.c @@ -0,0 +1,1927 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + +// bufwrite.c: functions for writing a buffer + +#include <fcntl.h> +#include <iconv.h> +#include <inttypes.h> +#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <sys/stat.h> +#include <uv.h> + +#include "auto/config.h" +#include "nvim/ascii.h" +#include "nvim/autocmd.h" +#include "nvim/buffer.h" +#include "nvim/buffer_defs.h" +#include "nvim/bufwrite.h" +#include "nvim/change.h" +#include "nvim/drawscreen.h" +#include "nvim/eval.h" +#include "nvim/eval/typval_defs.h" +#include "nvim/ex_cmds.h" +#include "nvim/ex_cmds_defs.h" +#include "nvim/ex_eval.h" +#include "nvim/fileio.h" +#include "nvim/gettext.h" +#include "nvim/globals.h" +#include "nvim/highlight_defs.h" +#include "nvim/iconv.h" +#include "nvim/input.h" +#include "nvim/macros.h" +#include "nvim/mbyte.h" +#include "nvim/memline.h" +#include "nvim/memory.h" +#include "nvim/message.h" +#include "nvim/option.h" +#include "nvim/os/fs_defs.h" +#include "nvim/os/input.h" +#include "nvim/os/os.h" +#include "nvim/path.h" +#include "nvim/pos.h" +#include "nvim/sha256.h" +#include "nvim/strings.h" +#include "nvim/types.h" +#include "nvim/ui.h" +#include "nvim/undo.h" +#include "nvim/vim.h" + +static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; +static const char e_no_matching_autocommands_for_buftype_str_buffer[] + = N_("E676: No matching autocommands for buftype=%s buffer"); + +typedef struct { + const char *num; + char *msg; + int arg; + bool alloc; +} Error_T; + +#define SMALLBUFSIZE 256 // size of emergency write buffer + +// Structure to pass arguments from buf_write() to buf_write_bytes(). +struct bw_info { + int bw_fd; // file descriptor + char *bw_buf; // buffer with data to be written + int bw_len; // length of data + int bw_flags; // FIO_ flags + uint8_t bw_rest[CONV_RESTLEN]; // not converted bytes + int bw_restlen; // nr of bytes in bw_rest[] + int bw_first; // first write call + char *bw_conv_buf; // buffer for writing converted chars + size_t bw_conv_buflen; // size of bw_conv_buf + int bw_conv_error; // set for conversion error + linenr_T bw_conv_error_lnum; // first line with error or zero + linenr_T bw_start_lnum; // line number at start of buffer + iconv_t bw_iconv_fd; // descriptor for iconv() or -1 +}; + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "bufwrite.c.generated.h" +#endif + +/// Convert a Unicode character to bytes. +/// +/// @param c character to convert +/// @param[in,out] pp pointer to store the result at +/// @param flags FIO_ flags that specify which encoding to use +/// +/// @return true for an error, false when it's OK. +static bool ucs2bytes(unsigned c, char **pp, int flags) + FUNC_ATTR_NONNULL_ALL +{ + uint8_t *p = (uint8_t *)(*pp); + bool error = false; + + if (flags & FIO_UCS4) { + if (flags & FIO_ENDIAN_L) { + *p++ = (uint8_t)c; + *p++ = (uint8_t)(c >> 8); + *p++ = (uint8_t)(c >> 16); + *p++ = (uint8_t)(c >> 24); + } else { + *p++ = (uint8_t)(c >> 24); + *p++ = (uint8_t)(c >> 16); + *p++ = (uint8_t)(c >> 8); + *p++ = (uint8_t)c; + } + } else if (flags & (FIO_UCS2 | FIO_UTF16)) { + if (c >= 0x10000) { + if (flags & FIO_UTF16) { + // Make two words, ten bits of the character in each. First + // word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff + c -= 0x10000; + if (c >= 0x100000) { + error = true; + } + int cc = (int)(((c >> 10) & 0x3ff) + 0xd800); + if (flags & FIO_ENDIAN_L) { + *p++ = (uint8_t)cc; + *p++ = (uint8_t)(cc >> 8); + } else { + *p++ = (uint8_t)(cc >> 8); + *p++ = (uint8_t)cc; + } + c = (c & 0x3ff) + 0xdc00; + } else { + error = true; + } + } + if (flags & FIO_ENDIAN_L) { + *p++ = (uint8_t)c; + *p++ = (uint8_t)(c >> 8); + } else { + *p++ = (uint8_t)(c >> 8); + *p++ = (uint8_t)c; + } + } else { // Latin1 + if (c >= 0x100) { + error = true; + *p++ = 0xBF; + } else { + *p++ = (uint8_t)c; + } + } + + *pp = (char *)p; + return error; +} + +static int buf_write_convert_with_iconv(struct bw_info *ip, char **bufp, int *lenp) +{ + const char *from; + size_t fromlen; + size_t tolen; + + int len = *lenp; + + // Convert with iconv(). + if (ip->bw_restlen > 0) { + // Need to concatenate the remainder of the previous call and + // the bytes of the current call. Use the end of the + // conversion buffer for this. + fromlen = (size_t)len + (size_t)ip->bw_restlen; + char *fp = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; + memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); + memmove(fp + ip->bw_restlen, *bufp, (size_t)len); + from = fp; + tolen = ip->bw_conv_buflen - fromlen; + } else { + from = *bufp; + fromlen = (size_t)len; + tolen = ip->bw_conv_buflen; + } + char *to = ip->bw_conv_buf; + + if (ip->bw_first) { + size_t save_len = tolen; + + // output the initial shift state sequence + (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); + + // There is a bug in iconv() on Linux (which appears to be + // wide-spread) which sets "to" to NULL and messes up "tolen". + if (to == NULL) { + to = ip->bw_conv_buf; + tolen = save_len; + } + ip->bw_first = false; + } + + // If iconv() has an error or there is not enough room, fail. + 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; + } + + // copy remainder to ip->bw_rest[] to be used for the next call. + if (fromlen > 0) { + memmove(ip->bw_rest, (void *)from, fromlen); + } + ip->bw_restlen = (int)fromlen; + + *bufp = ip->bw_conv_buf; + *lenp = (int)(to - ip->bw_conv_buf); + + return OK; +} + +static int buf_write_convert(struct bw_info *ip, char **bufp, int *lenp) +{ + int flags = ip->bw_flags; // extra flags + + if (flags & FIO_UTF8) { + // Convert latin1 in the buffer to UTF-8 in the file. + char *p = ip->bw_conv_buf; // translate to buffer + for (int wlen = 0; wlen < *lenp; wlen++) { + p += utf_char2bytes((uint8_t)(*bufp)[wlen], p); + } + *bufp = ip->bw_conv_buf; + *lenp = (int)(p - ip->bw_conv_buf); + } else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) { + unsigned c; + int n = 0; + // Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or + // Latin1 chars in the file. + // translate in-place (can only get shorter) or to buffer + char *p = flags & FIO_LATIN1 ? *bufp : ip->bw_conv_buf; + for (int wlen = 0; wlen < *lenp; wlen += n) { + if (wlen == 0 && ip->bw_restlen != 0) { + // Use remainder of previous call. Append the start of + // buf[] to get a full sequence. Might still be too + // short! + int l = MIN(*lenp, CONV_RESTLEN - ip->bw_restlen); + memmove(ip->bw_rest + ip->bw_restlen, *bufp, (size_t)l); + n = utf_ptr2len_len((char *)ip->bw_rest, ip->bw_restlen + l); + if (n > ip->bw_restlen + *lenp) { + // We have an incomplete byte sequence at the end to + // be written. We can't convert it without the + // remaining bytes. Keep them for the next call. + if (ip->bw_restlen + *lenp > CONV_RESTLEN) { + return FAIL; + } + ip->bw_restlen += *lenp; + break; + } + if (n > 1) { + c = (unsigned)utf_ptr2char((char *)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; + memmove(ip->bw_rest, ip->bw_rest + n, + (size_t)ip->bw_restlen); + n = 0; + } + } else { + n = utf_ptr2len_len(*bufp + wlen, *lenp - wlen); + if (n > *lenp - wlen) { + // We have an incomplete byte sequence at the end to + // be written. We can't convert it without the + // remaining bytes. Keep them for the next call. + if (*lenp - wlen > CONV_RESTLEN) { + return FAIL; + } + ip->bw_restlen = *lenp - wlen; + memmove(ip->bw_rest, *bufp + wlen, + (size_t)ip->bw_restlen); + break; + } + if (n > 1) { + c = (unsigned)utf_ptr2char(*bufp + wlen); + } else { + c = (uint8_t)(*bufp)[wlen]; + } + } + + if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) { + ip->bw_conv_error = true; + ip->bw_conv_error_lnum = ip->bw_start_lnum; + } + if (c == NL) { + ip->bw_start_lnum++; + } + } + if (flags & FIO_LATIN1) { + *lenp = (int)(p - *bufp); + } else { + *bufp = ip->bw_conv_buf; + *lenp = (int)(p - ip->bw_conv_buf); + } + } + + if (ip->bw_iconv_fd != (iconv_t)-1) { + if (buf_write_convert_with_iconv(ip, bufp, lenp) == FAIL) { + return FAIL; + } + } + + return OK; +} + +/// Call write() to write a number of bytes to the file. +/// Handles 'encoding' conversion. +/// +/// @return FAIL for failure, OK otherwise. +static int buf_write_bytes(struct bw_info *ip) +{ + char *buf = ip->bw_buf; // data to write + int len = ip->bw_len; // length of data + int flags = ip->bw_flags; // extra flags + + // Skip conversion when writing the BOM. + if (!(flags & FIO_NOCONVERT)) { + if (buf_write_convert(ip, &buf, &len) == FAIL) { + return FAIL; + } + } + + if (ip->bw_fd < 0) { + // Only checking conversion, which is OK if we get here. + return OK; + } + int wlen = (int)write_eintr(ip->bw_fd, buf, (size_t)len); + return (wlen < len) ? FAIL : OK; +} + +/// Check modification time of file, before writing to it. +/// The size isn't checked, because using a tool like "gzip" takes care of +/// using the same timestamp but can't set the size. +static int check_mtime(buf_T *buf, FileInfo *file_info) +{ + if (buf->b_mtime_read != 0 + && time_differs(file_info, buf->b_mtime_read, buf->b_mtime_read_ns)) { + msg_scroll = true; // Don't overwrite messages here. + msg_silent = 0; // Must give this prompt. + // Don't use emsg() here, don't want to flush the buffers. + msg_attr(_("WARNING: The file has been changed since reading it!!!"), + HL_ATTR(HLF_E)); + if (ask_yesno(_("Do you really want to write to it"), true) == 'n') { + return FAIL; + } + msg_scroll = false; // Always overwrite the file message now. + } + return OK; +} + +/// Generate a BOM in "buf[4]" for encoding "name". +/// +/// @return the length of the BOM (zero when no BOM). +static int make_bom(char *buf_in, char *name) +{ + uint8_t *buf = (uint8_t *)buf_in; + int flags = get_fio_flags(name); + + // Can't put a BOM in a non-Unicode file. + if (flags == FIO_LATIN1 || flags == 0) { + return 0; + } + + if (flags == FIO_UTF8) { // UTF-8 + buf[0] = 0xef; + buf[1] = 0xbb; + buf[2] = 0xbf; + return 3; + } + char *p = (char *)buf; + (void)ucs2bytes(0xfeff, &p, flags); + return (int)((uint8_t *)p - buf); +} + +static int buf_write_do_autocmds(buf_T *buf, char **fnamep, char **sfnamep, char **ffnamep, + linenr_T start, linenr_T *endp, exarg_T *eap, bool append, + bool filtering, bool reset_changed, bool overwriting, bool whole, + const pos_T orig_start, const pos_T orig_end) +{ + linenr_T old_line_count = buf->b_ml.ml_line_count; + int msg_save = msg_scroll; + + aco_save_T aco; + bool did_cmd = false; + bool nofile_err = false; + bool empty_memline = buf->b_ml.ml_mfp == NULL; + bufref_T bufref; + + char *sfname = *sfnamep; + + // Apply PRE autocommands. + // Set curbuf to the buffer to be written. + // Careful: The autocommands may call buf_write() recursively! + bool buf_ffname = *ffnamep == buf->b_ffname; + bool buf_sfname = sfname == buf->b_sfname; + bool buf_fname_f = *fnamep == buf->b_ffname; + bool buf_fname_s = *fnamep == buf->b_sfname; + + // Set curwin/curbuf to buf and save a few things. + aucmd_prepbuf(&aco, buf); + set_bufref(&bufref, buf); + + if (append) { + did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, sfname, sfname, false, curbuf, eap); + if (!did_cmd) { + if (overwriting && bt_nofilename(curbuf)) { + nofile_err = true; + } else { + 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) { + bool was_changed = curbufIsChanged(); + + did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, sfname, sfname, false, curbuf, eap); + if (did_cmd) { + if (was_changed && !curbufIsChanged()) { + // Written everything correctly and BufWriteCmd has reset + // 'modified': Correct the undo information so that an + // undo now sets 'modified'. + u_unchanged(curbuf); + u_update_save_nr(curbuf); + } + } else { + if (overwriting && bt_nofilename(curbuf)) { + nofile_err = true; + } else { + apply_autocmds_exarg(EVENT_BUFWRITEPRE, + sfname, sfname, false, curbuf, eap); + } + } + } else { + did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, sfname, sfname, false, curbuf, eap); + if (!did_cmd) { + if (overwriting && bt_nofilename(curbuf)) { + nofile_err = true; + } else { + apply_autocmds_exarg(EVENT_FILEWRITEPRE, + sfname, sfname, false, curbuf, eap); + } + } + } + + // restore curwin/curbuf and a few other things + aucmd_restbuf(&aco); + + // In three situations we return here and don't write the file: + // 1. the autocommands deleted or unloaded the buffer. + // 2. The autocommands abort script processing. + // 3. If one of the "Cmd" autocommands was executed. + if (!bufref_valid(&bufref)) { + buf = NULL; + } + if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) + || did_cmd || nofile_err + || aborting()) { + if (buf != NULL && (cmdmod.cmod_flags & CMOD_LOCKMARKS)) { + // restore the original '[ and '] positions + buf->b_op_start = orig_start; + buf->b_op_end = orig_end; + } + + no_wait_return--; + msg_scroll = msg_save; + if (nofile_err) { + semsg(_(e_no_matching_autocommands_for_buftype_str_buffer), curbuf->b_p_bt); + } + + if (nofile_err + || aborting()) { + // An aborting error, interrupt or exception in the + // autocommands. + return FAIL; + } + if (did_cmd) { + if (buf == NULL) { + // The buffer was deleted. We assume it was written + // (can't retry anyway). + return OK; + } + if (overwriting) { + // Assume the buffer was written, update the timestamp. + 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)) { + // Buffer still changed, the autocommands didn't work properly. + return FAIL; + } + return OK; + } + if (!aborting()) { + emsg(_("E203: Autocommands deleted or unloaded buffer to be written")); + } + return FAIL; + } + + // The autocommands may have changed the number of lines in the file. + // When writing the whole file, adjust the end. + // When writing part of the file, assume that the autocommands only + // changed the number of lines that are to be written (tricky!). + if (buf->b_ml.ml_line_count != old_line_count) { + if (whole) { // write all + *endp = buf->b_ml.ml_line_count; + } else if (buf->b_ml.ml_line_count > old_line_count) { // more lines + *endp += buf->b_ml.ml_line_count - old_line_count; + } else { // less lines + *endp -= old_line_count - buf->b_ml.ml_line_count; + if (*endp < start) { + no_wait_return--; + msg_scroll = msg_save; + emsg(_("E204: Autocommand changed number of lines in unexpected way")); + return FAIL; + } + } + } + + // The autocommands may have changed the name of the buffer, which may + // be kept in fname, ffname and sfname. + if (buf_ffname) { + *ffnamep = buf->b_ffname; + } + if (buf_sfname) { + *sfnamep = buf->b_sfname; + } + if (buf_fname_f) { + *fnamep = buf->b_ffname; + } + if (buf_fname_s) { + *fnamep = buf->b_sfname; + } + return NOTDONE; +} + +static void buf_write_do_post_autocmds(buf_T *buf, char *fname, exarg_T *eap, bool append, + bool filtering, bool reset_changed, bool whole) +{ + aco_save_T aco; + + curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read + + // Apply POST autocommands. + // Careful: The autocommands may call buf_write() recursively! + 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); + } + + // restore curwin/curbuf and a few other things + aucmd_restbuf(&aco); +} + +static inline Error_T set_err_num(const char *num, const char *msg) +{ + return (Error_T){ .num = num, .msg = (char *)msg, .arg = 0 }; +} + +static inline Error_T set_err(const char *msg) +{ + return (Error_T){ .num = NULL, .msg = (char *)msg, .arg = 0 }; +} + +static inline Error_T set_err_arg(const char *msg, int arg) +{ + return (Error_T){ .num = NULL, .msg = (char *)msg, .arg = arg }; +} + +static void emit_err(Error_T *e) +{ + if (e->num != NULL) { + if (e->arg != 0) { + semsg("%s: %s%s: %s", e->num, IObuff, e->msg, os_strerror(e->arg)); + } else { + semsg("%s: %s%s", e->num, IObuff, e->msg); + } + } else if (e->arg != 0) { + semsg(e->msg, os_strerror(e->arg)); + } else { + emsg(e->msg); + } + if (e->alloc) { + xfree(e->msg); + } +} + +#if defined(UNIX) + +static int get_fileinfo_os(char *fname, FileInfo *file_info_old, bool overwriting, long *perm, + bool *device, bool *newfile, Error_T *err) +{ + *perm = -1; + if (!os_fileinfo(fname, file_info_old)) { + *newfile = true; + } else { + *perm = (long)file_info_old->stat.st_mode; + if (!S_ISREG(file_info_old->stat.st_mode)) { // not a file + if (S_ISDIR(file_info_old->stat.st_mode)) { + *err = set_err_num("E502", _("is a directory")); + return FAIL; + } + if (os_nodetype(fname) != NODE_WRITABLE) { + *err = set_err_num("E503", _("is not a file or writable device")); + return FAIL; + } + // It's a device of some kind (or a fifo) which we can write to + // but for which we can't make a backup. + *device = true; + *newfile = true; + *perm = -1; + } + } + return OK; +} + +#else + +static int get_fileinfo_os(char *fname, FileInfo *file_info_old, bool overwriting, long *perm, + bool *device, bool *newfile, Error_T *err) +{ + // Check for a writable device name. + char nodetype = fname == NULL ? NODE_OTHER : (char)os_nodetype(fname); + if (nodetype == NODE_OTHER) { + *err = set_err_num("E503", _("is not a file or writable device")); + return FAIL; + } + if (nodetype == NODE_WRITABLE) { + *device = true; + *newfile = true; + *perm = -1; + } else { + *perm = os_getperm(fname); + if (*perm < 0) { + *newfile = true; + } else if (os_isdir(fname)) { + *err = set_err_num("E502", _("is a directory")); + return FAIL; + } + if (overwriting) { + os_fileinfo(fname, file_info_old); + } + } + return OK; +} + +#endif + +/// @param buf +/// @param fname File name +/// @param overwriting +/// @param forceit +/// @param[out] file_info_old +/// @param[out] perm +/// @param[out] device +/// @param[out] newfile +/// @param[out] readonly +static int get_fileinfo(buf_T *buf, char *fname, bool overwriting, bool forceit, + FileInfo *file_info_old, long *perm, bool *device, bool *newfile, + bool *readonly, Error_T *err) +{ + if (get_fileinfo_os(fname, file_info_old, overwriting, perm, device, newfile, err) == FAIL) { + return FAIL; + } + + *readonly = false; // overwritten file is read-only + + if (!*device && !*newfile) { + // Check if the file is really writable (when renaming the file to + // make a backup we won't discover it later). + *readonly = !os_file_is_writable(fname); + + if (!forceit && *readonly) { + if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) { + *err = set_err_num("E504", _(err_readonly)); + } else { + *err = set_err_num("E505", _("is read-only (add ! to override)")); + } + return FAIL; + } + + // If 'forceit' is false, check if the timestamp hasn't changed since reading the file. + if (overwriting && !forceit) { + int retval = check_mtime(buf, file_info_old); + if (retval == FAIL) { + return FAIL; + } + } + } + return OK; +} + +static int buf_write_make_backup(char *fname, bool append, FileInfo *file_info_old, vim_acl_T acl, + long perm, unsigned int bkc, bool file_readonly, bool forceit, + int *backup_copyp, char **backupp, Error_T *err) +{ + FileInfo file_info; + const bool no_prepend_dot = false; + + if ((bkc & BKC_YES) || append) { // "yes" + *backup_copyp = true; + } else if ((bkc & BKC_AUTO)) { // "auto" + // Don't rename the file when: + // - it's a hard link + // - it's a symbolic link + // - we don't have write permission in the directory + if (os_fileinfo_hardlinks(file_info_old) > 1 + || !os_fileinfo_link(fname, &file_info) + || !os_fileinfo_id_equal(&file_info, file_info_old)) { + *backup_copyp = true; + } else { + // Check if we can create a file and set the owner/group to + // the ones from the original file. + // First find a file name that doesn't exist yet (use some + // arbitrary numbers). + STRCPY(IObuff, fname); + for (int i = 4913;; i += 123) { + char *tail = path_tail(IObuff); + size_t size = (size_t)(tail - IObuff); + snprintf(tail, IOSIZE - size, "%d", i); + if (!os_fileinfo_link(IObuff, &file_info)) { + break; + } + } + int fd = os_open(IObuff, + O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, (int)perm); + if (fd < 0) { // can't write in directory + *backup_copyp = true; + } else { +#ifdef UNIX + os_fchown(fd, (uv_uid_t)file_info_old->stat.st_uid, (uv_gid_t)file_info_old->stat.st_gid); + if (!os_fileinfo(IObuff, &file_info) + || file_info.stat.st_uid != file_info_old->stat.st_uid + || file_info.stat.st_gid != file_info_old->stat.st_gid + || (long)file_info.stat.st_mode != perm) { + *backup_copyp = true; + } +#endif + // Close the file before removing it, on MS-Windows we + // can't delete an open file. + close(fd); + os_remove(IObuff); + } + } + } + + // Break symlinks and/or hardlinks if we've been asked to. + if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) { +#ifdef UNIX + bool file_info_link_ok = os_fileinfo_link(fname, &file_info); + + // Symlinks. + if ((bkc & BKC_BREAKSYMLINK) + && file_info_link_ok + && !os_fileinfo_id_equal(&file_info, file_info_old)) { + *backup_copyp = false; + } + + // Hardlinks. + if ((bkc & BKC_BREAKHARDLINK) + && os_fileinfo_hardlinks(file_info_old) > 1 + && (!file_info_link_ok + || os_fileinfo_id_equal(&file_info, file_info_old))) { + *backup_copyp = false; + } +#endif + } + + // make sure we have a valid backup extension to use + char *backup_ext = *p_bex == NUL ? ".bak" : p_bex; + + if (*backup_copyp) { + int some_error = false; + + // Try to make the backup in each directory in the 'bdir' option. + // + // Unix semantics has it, that we may have a writable file, + // that cannot be recreated with a simple open(..., O_CREAT, ) e.g: + // - the directory is not writable, + // - the file may be a symbolic link, + // - the file may belong to another user/group, etc. + // + // For these reasons, the existing writable file must be truncated + // and reused. Creation of a backup COPY will be attempted. + char *dirp = p_bdir; + while (*dirp) { + // Isolate one directory name, using an entry in 'bdir'. + size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); + char *p = IObuff + dir_len; + bool trailing_pathseps = after_pathsep(IObuff, p) && p[-1] == p[-2]; + if (trailing_pathseps) { + IObuff[dir_len - 2] = NUL; + } + if (*dirp == NUL && !os_isdir(IObuff)) { + int ret; + char *failed_dir; + if ((ret = os_mkdir_recurse(IObuff, 0755, &failed_dir, NULL)) != 0) { + semsg(_("E303: Unable to create directory \"%s\" for backup file: %s"), + failed_dir, os_strerror(ret)); + xfree(failed_dir); + } + } + if (trailing_pathseps) { + // Ends with '//', Use Full path + if ((p = make_percent_swname(IObuff, fname)) + != NULL) { + *backupp = modname(p, backup_ext, no_prepend_dot); + xfree(p); + } + } + + char *rootname = get_file_in_dir(fname, IObuff); + if (rootname == NULL) { + some_error = true; // out of memory + goto nobackup; + } + + FileInfo file_info_new; + { + // + // Make the backup file name. + // + if (*backupp == NULL) { + *backupp = modname(rootname, backup_ext, no_prepend_dot); + } + + if (*backupp == NULL) { + xfree(rootname); + some_error = true; // out of memory + goto nobackup; + } + + // Check if backup file already exists. + if (os_fileinfo(*backupp, &file_info_new)) { + if (os_fileinfo_id_equal(&file_info_new, file_info_old)) { + // + // Backup file is same as original file. + // May happen when modname() gave the same file back (e.g. silly + // link). If we don't check here, we either ruin the file when + // copying or erase it after writing. + // + XFREE_CLEAR(*backupp); // no backup file to delete + } else if (!p_bk) { + // We are not going to keep the backup file, so don't + // delete an existing one, and try to use another name instead. + // Change one character, just before the extension. + // + char *wp = *backupp + strlen(*backupp) - 1 - strlen(backup_ext); + if (wp < *backupp) { // empty file name ??? + wp = *backupp; + } + *wp = 'z'; + while (*wp > 'a' && os_fileinfo(*backupp, &file_info_new)) { + (*wp)--; + } + // They all exist??? Must be something wrong. + if (*wp == 'a') { + XFREE_CLEAR(*backupp); + } + } + } + } + xfree(rootname); + + // Try to create the backup file + if (*backupp != NULL) { + // remove old backup, if present + os_remove(*backupp); + + // set file protection same as original file, but + // strip s-bit. + (void)os_setperm(*backupp, perm & 0777); + +#ifdef UNIX + // + // Try to set the group of the backup same as the original file. If + // this fails, set the protection bits for the group same as the + // protection bits for others. + // + if (file_info_new.stat.st_gid != file_info_old->stat.st_gid + && os_chown(*backupp, (uv_uid_t)-1, (uv_gid_t)file_info_old->stat.st_gid) != 0) { + os_setperm(*backupp, ((int)perm & 0707) | (((int)perm & 07) << 3)); + } +#endif + + // copy the file + if (os_copy(fname, *backupp, UV_FS_COPYFILE_FICLONE) != 0) { + *err = set_err(_("E509: Cannot create backup file (add ! to override)")); + XFREE_CLEAR(*backupp); + *backupp = NULL; + continue; + } + +#ifdef UNIX + os_file_settime(*backupp, + (double)file_info_old->stat.st_atim.tv_sec, + (double)file_info_old->stat.st_mtim.tv_sec); +#endif + os_set_acl(*backupp, acl); + *err = set_err(NULL); + break; + } + } + +nobackup: + if (*backupp == NULL && err->msg == NULL) { + *err = set_err(_("E509: Cannot create backup file (add ! to override)")); + } + // Ignore errors when forceit is true. + if ((some_error || err->msg != NULL) && !forceit) { + return FAIL; + } + *err = set_err(NULL); + } else { + // Make a backup by renaming the original file. + + // If 'cpoptions' includes the "W" flag, we don't want to + // overwrite a read-only file. But rename may be possible + // anyway, thus we need an extra check here. + if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) { + *err = set_err_num("E504", _(err_readonly)); + return FAIL; + } + + // Form the backup file name - change path/fo.o.h to + // path/fo.o.h.bak Try all directories in 'backupdir', first one + // that works is used. + char *dirp = p_bdir; + while (*dirp) { + // Isolate one directory name and make the backup file name. + size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); + char *p = IObuff + dir_len; + bool trailing_pathseps = after_pathsep(IObuff, p) && p[-1] == p[-2]; + if (trailing_pathseps) { + IObuff[dir_len - 2] = NUL; + } + if (*dirp == NUL && !os_isdir(IObuff)) { + int ret; + char *failed_dir; + if ((ret = os_mkdir_recurse(IObuff, 0755, &failed_dir, NULL)) != 0) { + semsg(_("E303: Unable to create directory \"%s\" for backup file: %s"), + failed_dir, os_strerror(ret)); + xfree(failed_dir); + } + } + if (trailing_pathseps) { + // path ends with '//', use full path + if ((p = make_percent_swname(IObuff, fname)) + != NULL) { + *backupp = modname(p, backup_ext, no_prepend_dot); + xfree(p); + } + } + + if (*backupp == NULL) { + char *rootname = get_file_in_dir(fname, IObuff); + if (rootname == NULL) { + *backupp = NULL; + } else { + *backupp = modname(rootname, backup_ext, no_prepend_dot); + xfree(rootname); + } + } + + if (*backupp != NULL) { + // If we are not going to keep the backup file, don't + // delete an existing one, try to use another name. + // Change one character, just before the extension. + if (!p_bk && os_path_exists(*backupp)) { + p = *backupp + strlen(*backupp) - 1 - strlen(backup_ext); + if (p < *backupp) { // empty file name ??? + p = *backupp; + } + *p = 'z'; + while (*p > 'a' && os_path_exists(*backupp)) { + (*p)--; + } + // They all exist??? Must be something wrong! + if (*p == 'a') { + XFREE_CLEAR(*backupp); + } + } + } + if (*backupp != NULL) { + // Delete any existing backup and move the current version + // to the backup. For safety, we don't remove the backup + // until the write has finished successfully. And if the + // 'backup' option is set, leave it around. + + // If the renaming of the original file to the backup file + // works, quit here. + /// + if (vim_rename(fname, *backupp) == 0) { + break; + } + + XFREE_CLEAR(*backupp); // don't do the rename below + } + } + if (*backupp == NULL && !forceit) { + *err = set_err(_("E510: Can't make backup file (add ! to override)")); + return FAIL; + } + } + return OK; +} + +/// buf_write() - write to file "fname" lines "start" through "end" +/// +/// We do our own buffering here because fwrite() is so slow. +/// +/// If "forceit" is true, we don't care for errors when attempting backups. +/// In case of an error everything possible is done to restore the original +/// file. But when "forceit" is true, we risk losing it. +/// +/// When "reset_changed" is true and "append" == false and "start" == 1 and +/// "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. +/// +/// This function must NOT use NameBuff (because it's called by autowrite()). +/// +/// +/// @param eap for forced 'ff' and 'fenc', can be NULL! +/// @param append append to the file +/// +/// @return FAIL for failure, OK otherwise +int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T end, exarg_T *eap, + int append, int forceit, int reset_changed, int filtering) +{ + int retval = OK; + int msg_save = msg_scroll; + int prev_got_int = got_int; + // writing everything + int whole = (start == 1 && end == buf->b_ml.ml_line_count); + int write_undo_file = false; + context_sha256_T sha_ctx; + unsigned int bkc = get_bkc_value(buf); + + if (fname == NULL || *fname == NUL) { // safety check + return FAIL; + } + if (buf->b_ml.ml_mfp == NULL) { + // This can happen during startup when there is a stray "w" in the + // vimrc file. + emsg(_(e_emptybuf)); + return FAIL; + } + + // Disallow writing in secure mode. + if (check_secure()) { + return FAIL; + } + + // Avoid a crash for a long name. + if (strlen(fname) >= MAXPATHL) { + emsg(_(e_longname)); + return FAIL; + } + + // must init bw_conv_buf and bw_iconv_fd before jumping to "fail" + struct bw_info write_info; // info for buf_write_bytes() + write_info.bw_conv_buf = NULL; + write_info.bw_conv_error = false; + write_info.bw_conv_error_lnum = 0; + write_info.bw_restlen = 0; + write_info.bw_iconv_fd = (iconv_t)-1; + + // After writing a file changedtick changes but we don't want to display + // the line. + ex_no_reprint = true; + + // If there is no file name yet, use the one for the written file. + // BF_NOTEDITED is set to reflect this (in case the write fails). + // Don't do this when the write is for a filter command. + // Don't do this when appending. + // Only do this when 'cpoptions' contains the 'F' flag. + if (buf->b_ffname == NULL + && reset_changed + && whole + && buf == curbuf + && !bt_nofilename(buf) + && !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; // just in case autocmds made "buf" invalid + } + + if (sfname == NULL) { + sfname = fname; + } + + // For Unix: Use the short file name whenever possible. + // Avoids problems with networks and when directory names are changed. + // Don't do this for Windows, a "cd" in a sub-shell may have moved us to + // another directory, which we don't detect. + char *ffname = fname; // remember full fname +#ifdef UNIX + fname = sfname; +#endif + +// true if writing over original + int overwriting = buf->b_ffname != NULL && path_fnamecmp(ffname, buf->b_ffname) == 0; + + no_wait_return++; // don't wait for return yet + + const pos_T orig_start = buf->b_op_start; + const pos_T orig_end = buf->b_op_end; + + // Set '[ and '] marks to the lines to be written. + buf->b_op_start.lnum = start; + buf->b_op_start.col = 0; + buf->b_op_end.lnum = end; + buf->b_op_end.col = 0; + + int res = buf_write_do_autocmds(buf, &fname, &sfname, &ffname, start, &end, eap, append, + filtering, reset_changed, overwriting, whole, orig_start, + orig_end); + if (res != NOTDONE) { + return res; + } + + if (cmdmod.cmod_flags & CMOD_LOCKMARKS) { + // restore the original '[ and '] positions + buf->b_op_start = orig_start; + buf->b_op_end = orig_end; + } + + if (shortmess(SHM_OVER) && !exiting) { + msg_scroll = false; // overwrite previous file message + } else { + msg_scroll = true; // don't overwrite previous file message + } + if (!filtering) { + // show that we are busy +#ifndef UNIX + filemess(buf, sfname, "", 0); +#else + filemess(buf, fname, "", 0); +#endif + } + msg_scroll = false; // always overwrite the file message now + + char *buffer = verbose_try_malloc(WRITEBUFSIZE); + int bufsize; + char smallbuf[SMALLBUFSIZE]; + // can't allocate big buffer, use small one (to be able to write when out of + // memory) + if (buffer == NULL) { + buffer = smallbuf; + bufsize = SMALLBUFSIZE; + } else { + bufsize = WRITEBUFSIZE; + } + + Error_T err = { 0 }; + long perm; // file permissions + bool newfile = false; // true if file doesn't exist yet + bool device = false; // writing to a device + bool file_readonly = false; // overwritten file is read-only + char *backup = NULL; + char *fenc_tofree = NULL; // allocated "fenc" + + // Get information about original file (if there is one). + FileInfo file_info_old; + + vim_acl_T acl = NULL; // ACL copied from original file to + // backup or new file + + if (get_fileinfo(buf, fname, overwriting, forceit, &file_info_old, &perm, &device, &newfile, + &file_readonly, &err) == FAIL) { + goto fail; + } + + // For systems that support ACL: get the ACL from the original file. + if (!newfile) { + acl = os_get_acl(fname); + } + + // If 'backupskip' is not empty, don't make a backup for some files. + bool dobackup = (p_wb || p_bk || *p_pm != NUL); + if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) { + dobackup = false; + } + + int backup_copy = false; // copy the original file? + + // Save the value of got_int and reset it. We don't want a previous + // interruption cancel writing, only hitting CTRL-C while writing should + // abort it. + prev_got_int = got_int; + got_int = false; + + // Mark the buffer as 'being saved' to prevent changed buffer warnings + buf->b_saving = true; + + // If we are not appending or filtering, the file exists, and the + // 'writebackup', 'backup' or 'patchmode' option is set, need a backup. + // When 'patchmode' is set also make a backup when appending. + // + // Do not make any backup, if 'writebackup' and 'backup' are both switched + // off. This helps when editing large files on almost-full disks. + if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) { + if (buf_write_make_backup(fname, append, &file_info_old, acl, perm, bkc, file_readonly, forceit, + &backup_copy, &backup, &err) == FAIL) { + retval = FAIL; + goto fail; + } + } + +#if defined(UNIX) + int made_writable = false; // 'w' bit has been set + + // When using ":w!" and the file was read-only: make it writable + if (forceit && perm >= 0 && !(perm & 0200) + && file_info_old.stat.st_uid == getuid() + && vim_strchr(p_cpo, CPO_FWRITE) == NULL) { + perm |= 0200; + (void)os_setperm(fname, (int)perm); + made_writable = true; + } +#endif + + // When using ":w!" and writing to the current file, 'readonly' makes no + // sense, reset it, unless 'Z' appears in 'cpoptions'. + if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) { + buf->b_p_ro = false; + need_maketitle = true; // set window title later + status_redraw_all(); // redraw status lines later + } + + 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; + } + + char *wfname = NULL; // name of file to write to + + // If the original file is being overwritten, there is a small chance that + // we crash in the middle of writing. Therefore the file is preserved now. + // This makes all block numbers positive so that recovery does not need + // the original file. + // Don't do this if there is a backup file and we are exiting. + if (reset_changed && !newfile && overwriting + && !(exiting && backup != NULL)) { + ml_preserve(buf, false, !!p_fs); + if (got_int) { + err = set_err(_(e_interr)); + goto restore_backup; + } + } + + // Default: write the file directly. May write to a temp file for + // multi-byte conversion. + wfname = fname; + + char *fenc; // effective 'fileencoding' + + // Check for forced 'fileencoding' from "++opt=val" argument. + 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; + } + + // Check if the file needs to be converted. + int converted = need_conversion(fenc); + int wb_flags = 0; + + // Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or + // Latin1 to Unicode conversion. This is handled in buf_write_bytes(). + // Prepare the flags for it and allocate bw_conv_buf when needed. + if (converted) { + wb_flags = get_fio_flags(fenc); + if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) { + // Need to allocate a buffer to translate into. + if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) { + write_info.bw_conv_buflen = (size_t)bufsize * 2; + } else { // FIO_UCS4 + write_info.bw_conv_buflen = (size_t)bufsize * 4; + } + write_info.bw_conv_buf = verbose_try_malloc(write_info.bw_conv_buflen); + if (!write_info.bw_conv_buf) { + end = 0; + } + } + } + + if (converted && wb_flags == 0) { + // Use iconv() conversion when conversion is needed and it's not done + // internally. + write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, "utf-8"); + if (write_info.bw_iconv_fd != (iconv_t)-1) { + // We're going to use iconv(), allocate a buffer to convert in. + write_info.bw_conv_buflen = (size_t)bufsize * ICONV_MULT; + write_info.bw_conv_buf = verbose_try_malloc(write_info.bw_conv_buflen); + if (!write_info.bw_conv_buf) { + end = 0; + } + write_info.bw_first = true; + } else { + // When the file needs to be converted with 'charconvert' after + // writing, write to a temp file instead and let the conversion + // overwrite the original file. + if (*p_ccv != NUL) { + wfname = vim_tempname(); + if (wfname == NULL) { // Can't write without a tempfile! + err = set_err(_("E214: Can't find temp file for writing")); + goto restore_backup; + } + } + } + } + + int notconverted = false; + + if (converted && wb_flags == 0 + && write_info.bw_iconv_fd == (iconv_t)-1 + && wfname == fname) { + if (!forceit) { + err = set_err(_("E213: Cannot convert (add ! to write without conversion)")); + goto restore_backup; + } + notconverted = true; + } + + int no_eol = false; // no end-of-line written + long nchars; + linenr_T lnum; + int fileformat; + int checking_conversion; + + int fd; + + // If conversion is taking place, we may first pretend to write and check + // for conversion errors. Then loop again to write for real. + // When not doing conversion this writes for real right away. + for (checking_conversion = true;; checking_conversion = false) { + // There is no need to check conversion when: + // - there is no conversion + // - we make a backup file, that can be restored in case of conversion + // failure. + if (!converted || dobackup) { + checking_conversion = false; + } + + if (checking_conversion) { + // Make sure we don't write anything. + fd = -1; + write_info.bw_fd = fd; + } else { + // Open the file "wfname" for writing. + // We may try to open the file twice: If we can't write to the file + // and forceit is true we delete the existing file and try to + // create a new one. If this still fails we may have lost the + // original file! (this may happen when the user reached his + // quotum for number of files). + // Appending will fail if the file does not exist and forceit is + // false. + const int fflags = O_WRONLY | (append + ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) + : (O_CREAT | O_TRUNC)); + const int mode = perm < 0 ? 0666 : (perm & 0777); + + while ((fd = os_open(wfname, fflags, mode)) < 0) { + // A forced write will try to create a new file if the old one + // is still readonly. This may also happen when the directory + // is read-only. In that case the mch_remove() will fail. + if (err.msg == NULL) { +#ifdef UNIX + FileInfo file_info; + + // Don't delete the file when it's a hard or symbolic link. + if ((!newfile && os_fileinfo_hardlinks(&file_info_old) > 1) + || (os_fileinfo_link(fname, &file_info) + && !os_fileinfo_id_equal(&file_info, &file_info_old))) { + err = set_err(_("E166: Can't open linked file for writing")); + } else { + err = set_err_arg(_("E212: Can't open file for writing: %s"), fd); + if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL && perm >= 0) { + // we write to the file, thus it should be marked + // writable after all + if (!(perm & 0200)) { + made_writable = true; + } + perm |= 0200; + if (file_info_old.stat.st_uid != getuid() + || file_info_old.stat.st_gid != getgid()) { + perm &= 0777; + } + if (!append) { // don't remove when appending + os_remove(wfname); + } + continue; + } + } +#else + err = set_err_arg(_("E212: Can't open file for writing: %s"), fd); + if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL && perm >= 0) { + if (!append) { // don't remove when appending + os_remove(wfname); + } + continue; + } +#endif + } + +restore_backup: + { + // If we failed to open the file, we don't need a backup. Throw it + // away. If we moved or removed the original file try to put the + // backup in its place. + if (backup != NULL && wfname == fname) { + if (backup_copy) { + // There is a small chance that we removed the original, + // try to move the copy in its place. + // This may not work if the vim_rename() fails. + // In that case we leave the copy around. + // If file does not exist, put the copy in its place + if (!os_path_exists(fname)) { + vim_rename(backup, fname); + } + // if original file does exist throw away the copy + if (os_path_exists(fname)) { + os_remove(backup); + } + } else { + // try to put the original file back + vim_rename(backup, fname); + } + } + + // if original file no longer exists give an extra warning + if (!newfile && !os_path_exists(fname)) { + end = 0; + } + } + + if (wfname != fname) { + xfree(wfname); + } + goto fail; + } + write_info.bw_fd = fd; + } + err = set_err(NULL); + + write_info.bw_buf = buffer; + nchars = 0; + + // use "++bin", "++nobin" or 'binary' + int write_bin; + if (eap != NULL && eap->force_bin != 0) { + write_bin = (eap->force_bin == FORCE_BIN); + } else { + write_bin = buf->b_p_bin; + } + + // Skip the BOM when appending and the file already existed, the BOM + // only makes sense at the start of the file. + if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) { + write_info.bw_len = make_bom(buffer, fenc); + if (write_info.bw_len > 0) { + // don't convert + write_info.bw_flags = FIO_NOCONVERT | wb_flags; + if (buf_write_bytes(&write_info) == FAIL) { + end = 0; + } else { + nchars += write_info.bw_len; + } + } + } + write_info.bw_start_lnum = start; + + write_undo_file = (buf->b_p_udf && overwriting && !append + && !filtering && reset_changed && !checking_conversion); + if (write_undo_file) { + // Prepare for computing the hash value of the text. + sha256_start(&sha_ctx); + } + + write_info.bw_len = bufsize; + write_info.bw_flags = wb_flags; + fileformat = get_fileformat_force(buf, eap); + char *s = buffer; + int len = 0; + for (lnum = start; lnum <= end; lnum++) { + // The next while loop is done once for each character written. + // Keep it fast! + char *ptr = ml_get_buf(buf, lnum, false) - 1; + if (write_undo_file) { + sha256_update(&sha_ctx, (uint8_t *)ptr + 1, (uint32_t)(strlen(ptr + 1) + 1)); + } + char c; + while ((c = *++ptr) != NUL) { + if (c == NL) { + *s = NUL; // replace newlines with NULs + } else if (c == CAR && fileformat == EOL_MAC) { + *s = NL; // Mac: replace CRs with NLs + } else { + *s = c; + } + s++; + if (++len != bufsize) { + continue; + } + if (buf_write_bytes(&write_info) == FAIL) { + end = 0; // write error: break loop + break; + } + nchars += bufsize; + s = buffer; + len = 0; + write_info.bw_start_lnum = lnum; + } + // write failed or last line has no EOL: stop here + if (end == 0 + || (lnum == end + && (write_bin || !buf->b_p_fixeol) + && ((write_bin && lnum == buf->b_no_eol_lnum) + || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) { + lnum++; // written the line, count it + no_eol = true; + break; + } + if (fileformat == EOL_UNIX) { + *s++ = NL; + } else { + *s++ = CAR; // EOL_MAC or EOL_DOS: write CR + if (fileformat == EOL_DOS) { // write CR-NL + if (++len == bufsize) { + if (buf_write_bytes(&write_info) == FAIL) { + end = 0; // write error: break loop + break; + } + nchars += bufsize; + s = buffer; + len = 0; + } + *s++ = NL; + } + } + if (++len == bufsize) { + if (buf_write_bytes(&write_info) == FAIL) { + end = 0; // Write error: break loop. + break; + } + nchars += bufsize; + s = buffer; + len = 0; + + os_breakcheck(); + if (got_int) { + end = 0; // Interrupted, break loop. + break; + } + } + } + if (len > 0 && end > 0) { + write_info.bw_len = len; + if (buf_write_bytes(&write_info) == FAIL) { + end = 0; // write error + } + nchars += len; + } + + if (!buf->b_p_fixeol && buf->b_p_eof) { + // write trailing CTRL-Z + (void)write_eintr(write_info.bw_fd, "\x1a", 1); + } + + // Stop when writing done or an error was encountered. + if (!checking_conversion || end == 0) { + break; + } + + // If no error happened until now, writing should be ok, so loop to + // really write the buffer. + } + + // If we started writing, finish writing. Also when an error was + // encountered. + if (!checking_conversion) { + // On many journalling file systems there is a bug that causes both the + // original and the backup file to be lost when halting the system right + // after writing the file. That's because only the meta-data is + // journalled. Syncing the file slows down the system, but assures it has + // been written to disk and we don't lose it. + // For a device do try the fsync() but don't complain if it does not work + // (could be a pipe). + // If the 'fsync' option is false, don't fsync(). Useful for laptops. + int error; + if (p_fs && (error = os_fsync(fd)) != 0 && !device + // fsync not supported on this storage. + && error != UV_ENOTSUP) { + err = set_err_arg(e_fsync, error); + end = 0; + } + +#ifdef UNIX + // When creating a new file, set its owner/group to that of the original + // file. Get the new device and inode number. + if (backup != NULL && !backup_copy) { + // don't change the owner when it's already OK, some systems remove + // permission or ACL stuff + FileInfo file_info; + if (!os_fileinfo(wfname, &file_info) + || file_info.stat.st_uid != file_info_old.stat.st_uid + || file_info.stat.st_gid != file_info_old.stat.st_gid) { + os_fchown(fd, (uv_uid_t)file_info_old.stat.st_uid, (uv_gid_t)file_info_old.stat.st_gid); + if (perm >= 0) { // Set permission again, may have changed. + (void)os_setperm(wfname, (int)perm); + } + } + buf_set_file_id(buf); + } else if (!buf->file_id_valid) { + // Set the file_id when creating a new file. + buf_set_file_id(buf); + } +#endif + + if ((error = os_close(fd)) != 0) { + err = set_err_arg(_("E512: Close failed: %s"), error); + end = 0; + } + +#ifdef UNIX + if (made_writable) { + perm &= ~0200; // reset 'w' bit for security reasons + } +#endif + if (perm >= 0) { // Set perm. of new file same as old file. + (void)os_setperm(wfname, (int)perm); + } + // Probably need to set the ACL before changing the user (can't set the + // ACL on a file the user doesn't own). + if (!backup_copy) { + os_set_acl(wfname, acl); + } + + if (wfname != fname) { + // The file was written to a temp file, now it needs to be converted + // with 'charconvert' to (overwrite) the output file. + if (end != 0) { + if (eval_charconvert("utf-8", fenc, wfname, fname) == FAIL) { + write_info.bw_conv_error = true; + end = 0; + } + } + os_remove(wfname); + xfree(wfname); + } + } + + if (end == 0) { + // Error encountered. + if (err.msg == NULL) { + if (write_info.bw_conv_error) { + if (write_info.bw_conv_error_lnum == 0) { + err = set_err(_("E513: write error, conversion failed " + "(make 'fenc' empty to override)")); + } else { + err = set_err(xmalloc(300)); + err.alloc = true; + vim_snprintf(err.msg, 300, // NOLINT(runtime/printf) + _("E513: write error, conversion failed in line %" PRIdLINENR + " (make 'fenc' empty to override)"), + write_info.bw_conv_error_lnum); + } + } else if (got_int) { + err = set_err(_(e_interr)); + } else { + err = set_err(_("E514: write error (file system full?)")); + } + } + + // If we have a backup file, try to put it in place of the new file, + // because the new file is probably corrupt. This avoids losing the + // original file when trying to make a backup when writing the file a + // second time. + // When "backup_copy" is set we need to copy the backup over the new + // file. Otherwise rename the backup file. + // If this is OK, don't give the extra warning message. + if (backup != NULL) { + if (backup_copy) { + // This may take a while, if we were interrupted let the user + // know we got the message. + if (got_int) { + msg(_(e_interr)); + ui_flush(); + } + + // copy the file. + if (os_copy(backup, fname, UV_FS_COPYFILE_FICLONE) + == 0) { + end = 1; // success + } + } else { + if (vim_rename(backup, fname) == 0) { + end = 1; + } + } + } + goto fail; + } + + lnum -= start; // compute number of written lines + no_wait_return--; // may wait for return now + +#if !defined(UNIX) + fname = sfname; // use shortname now, for the messages +#endif + if (!filtering) { + add_quoted_fname(IObuff, IOSIZE, buf, fname); + bool insert_space = false; + if (write_info.bw_conv_error) { + STRCAT(IObuff, _(" CONVERSION ERROR")); + insert_space = true; + if (write_info.bw_conv_error_lnum != 0) { + vim_snprintf_add(IObuff, IOSIZE, _(" in line %" PRId64 ";"), + (int64_t)write_info.bw_conv_error_lnum); + } + } else if (notconverted) { + STRCAT(IObuff, _("[NOT converted]")); + insert_space = true; + } else if (converted) { + STRCAT(IObuff, _("[converted]")); + insert_space = true; + } + if (device) { + STRCAT(IObuff, _("[Device]")); + insert_space = true; + } else if (newfile) { + STRCAT(IObuff, new_file_message()); + insert_space = true; + } + if (no_eol) { + msg_add_eol(); + insert_space = true; + } + // may add [unix/dos/mac] + if (msg_add_fileformat(fileformat)) { + insert_space = true; + } + msg_add_lines(insert_space, (long)lnum, nchars); // add line/char count + 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); + } + + // When written everything correctly: reset 'modified'. Unless not + // writing to the original file and '+' is not in 'cpoptions'. + if (reset_changed && whole && !append + && !write_info.bw_conv_error + && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) { + unchanged(buf, true, false); + const varnumber_T changedtick = buf_get_changedtick(buf); + if (buf->b_last_changedtick + 1 == changedtick) { + // b:changedtick may be incremented in unchanged() but that + // should not trigger a TextChanged event. + buf->b_last_changedtick = changedtick; + } + u_unchanged(buf); + u_update_save_nr(buf); + } + + // If written to the current file, update the timestamp of the swap file + // and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. + if (overwriting) { + ml_timestamp(buf); + if (append) { + buf->b_flags &= ~BF_NEW; + } else { + buf->b_flags &= ~BF_WRITE_MASK; + } + } + + // If we kept a backup until now, and we are in patch mode, then we make + // the backup file our 'original' file. + if (*p_pm && dobackup) { + char *const org = modname(fname, p_pm, false); + + if (backup != NULL) { + // If the original file does not exist yet + // the current backup file becomes the original file + if (org == NULL) { + emsg(_("E205: Patchmode: can't save original file")); + } else if (!os_path_exists(org)) { + vim_rename(backup, org); + XFREE_CLEAR(backup); // don't delete the file +#ifdef UNIX + os_file_settime(org, + (double)file_info_old.stat.st_atim.tv_sec, + (double)file_info_old.stat.st_mtim.tv_sec); +#endif + } + } else { + // If there is no backup file, remember that a (new) file was + // created. + int empty_fd; + + if (org == NULL + || (empty_fd = os_open(org, + O_CREAT | 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) { + os_setperm(org, os_getperm(fname) & 0777); + xfree(org); + } + } + + // Remove the backup unless 'backup' option is set + if (!p_bk && backup != NULL + && !write_info.bw_conv_error + && os_remove(backup) != 0) { + emsg(_("E207: Can't delete backup file")); + } + + goto nofail; + + // Finish up. We get here either after failure or success. +fail: + no_wait_return--; // may wait for return now +nofail: + + // Done saving, we accept changed buffer warnings again + buf->b_saving = false; + + xfree(backup); + if (buffer != smallbuf) { + xfree(buffer); + } + xfree(fenc_tofree); + xfree(write_info.bw_conv_buf); + if (write_info.bw_iconv_fd != (iconv_t)-1) { + iconv_close(write_info.bw_iconv_fd); + write_info.bw_iconv_fd = (iconv_t)-1; + } + os_free_acl(acl); + + if (err.msg != NULL) { + // - 100 to save some space for further error message +#ifndef UNIX + add_quoted_fname(IObuff, IOSIZE - 100, buf, sfname); +#else + add_quoted_fname(IObuff, IOSIZE - 100, buf, fname); +#endif + emit_err(&err); + + retval = FAIL; + if (end == 0) { + const int attr = HL_ATTR(HLF_E); // Set highlight for error messages. + 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); + + // Update the timestamp to avoid an "overwrite changed file" + // prompt when writing again. + if (os_fileinfo(fname, &file_info_old)) { + buf_store_file_info(buf, &file_info_old); + buf->b_mtime_read = buf->b_mtime; + buf->b_mtime_read_ns = buf->b_mtime_ns; + } + } + } + msg_scroll = msg_save; + + // When writing the whole file and 'undofile' is set, also write the undo + // file. + if (retval == OK && write_undo_file) { + uint8_t hash[UNDO_HASH_SIZE]; + + sha256_finish(&sha_ctx, hash); + u_write_undo(NULL, false, buf, hash); + } + + if (!should_abort(retval)) { + buf_write_do_post_autocmds(buf, fname, eap, append, filtering, reset_changed, whole); + if (aborting()) { // autocmds may abort script processing + retval = false; + } + } + + got_int |= prev_got_int; + + return retval; +} diff --git a/src/nvim/bufwrite.h b/src/nvim/bufwrite.h new file mode 100644 index 0000000000..0845abf4ee --- /dev/null +++ b/src/nvim/bufwrite.h @@ -0,0 +1,11 @@ +#ifndef NVIM_BUFWRITE_H +#define NVIM_BUFWRITE_H + +#include "nvim/buffer_defs.h" +#include "nvim/ex_cmds_defs.h" + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "bufwrite.h.generated.h" +#endif + +#endif // NVIM_BUFWRITE_H diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 52c5732f23..b2efcbac58 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -22,6 +22,7 @@ #include "nvim/ascii.h" #include "nvim/autocmd.h" #include "nvim/buffer.h" +#include "nvim/bufwrite.h" #include "nvim/change.h" #include "nvim/charset.h" #include "nvim/cursor.h" diff --git a/src/nvim/drawline.c b/src/nvim/drawline.c index 2268a7cd3e..604af21899 100644 --- a/src/nvim/drawline.c +++ b/src/nvim/drawline.c @@ -2947,6 +2947,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, bool nochange, } wlv.boguscols = 0; + wlv.vcol_off = 0; wlv.row++; // When not wrapping and finished diff lines, or when displayed diff --git a/src/nvim/eval.c b/src/nvim/eval.c index 28ac1f3fbd..35738cdfa9 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -5712,8 +5712,7 @@ void get_xdg_var_list(const XDGVarType xdg, typval_T *rettv) if (dir != NULL && dir_len > 0) { char *dir_with_nvim = xmemdupz(dir, dir_len); dir_with_nvim = concat_fnames_realloc(dir_with_nvim, appname, true); - tv_list_append_string(list, dir_with_nvim, (ssize_t)strlen(dir_with_nvim)); - xfree(dir_with_nvim); + tv_list_append_allocated_string(list, dir_with_nvim); } } while (iter != NULL); xfree(dirs); diff --git a/src/nvim/eval.lua b/src/nvim/eval.lua index 7dbfac80f3..5babfa4809 100644 --- a/src/nvim/eval.lua +++ b/src/nvim/eval.lua @@ -400,6 +400,7 @@ return { strwidth={args=1, base=1, fast=true}, submatch={args={1, 2}, base=1}, substitute={args=4, base=1}, + swapfilelist={}, swapinfo={args=1, base=1}, swapname={args=1, base=1}, synID={args=3}, diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 28901d6e55..d9226214c6 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -44,6 +44,7 @@ #include "nvim/eval/executor.h" #include "nvim/eval/funcs.h" #include "nvim/eval/typval.h" +#include "nvim/eval/typval_defs.h" #include "nvim/eval/userfunc.h" #include "nvim/eval/vars.h" #include "nvim/eval/window.h" @@ -3850,8 +3851,7 @@ static void f_insert(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } /// "interrupt()" function -static void f_interrupt(typval_T *argvars FUNC_ATTR_UNUSED, typval_T *rettv FUNC_ATTR_UNUSED, - EvalFuncData fptr FUNC_ATTR_UNUSED) +static void f_interrupt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { got_int = true; } @@ -8523,6 +8523,13 @@ static void f_substitute(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) } } +/// "swapfilelist()" function +static void f_swapfilelist(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + tv_list_alloc_ret(rettv, kListLenUnknown); + recover_names(NULL, false, rettv->vval.v_list, 0, NULL); +} + /// "swapinfo(swap_filename)" function static void f_swapinfo(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index 63d5f94f11..51e109fdfb 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -3242,12 +3242,24 @@ static void handle_defer_one(funccall_T *funccal) { for (int idx = funccal->fc_defer.ga_len - 1; idx >= 0; idx--) { defer_T *dr = ((defer_T *)funccal->fc_defer.ga_data) + idx; + + if (dr->dr_name == NULL) { + // already being called, can happen if function does ":qa" + continue; + } + funcexe_T funcexe = { .fe_evaluate = true }; + typval_T rettv; rettv.v_type = VAR_UNKNOWN; // tv_clear() uses this - call_func(dr->dr_name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe); + + char *name = dr->dr_name; + dr->dr_name = NULL; + + call_func(name, -1, &rettv, dr->dr_argcount, dr->dr_argvars, &funcexe); + tv_clear(&rettv); - xfree(dr->dr_name); + xfree(name); for (int i = dr->dr_argcount - 1; i >= 0; i--) { tv_clear(&dr->dr_argvars[i]); } @@ -3258,8 +3270,14 @@ static void handle_defer_one(funccall_T *funccal) /// Called when exiting: call all defer functions. void invoke_all_defer(void) { - for (funccall_T *funccal = current_funccal; funccal != NULL; funccal = funccal->fc_caller) { - handle_defer_one(funccal); + for (funccall_T *fc = current_funccal; fc != NULL; fc = fc->fc_caller) { + handle_defer_one(fc); + } + + for (funccal_entry_T *fce = funccal_stack; fce != NULL; fce = fce->next) { + for (funccall_T *fc = fce->top_funccal; fc != NULL; fc = fc->fc_caller) { + handle_defer_one(fc); + } } } diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index b75ff45843..3c7e230ad3 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -23,6 +23,7 @@ #include "nvim/buffer.h" #include "nvim/buffer_defs.h" #include "nvim/buffer_updates.h" +#include "nvim/bufwrite.h" #include "nvim/change.h" #include "nvim/channel.h" #include "nvim/charset.h" diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index 4d71285447..deaa24452b 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -15,6 +15,7 @@ #include "nvim/ascii.h" #include "nvim/autocmd.h" #include "nvim/buffer.h" +#include "nvim/bufwrite.h" #include "nvim/change.h" #include "nvim/channel.h" #include "nvim/eval.h" diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index fa2f72932f..52fafa6a21 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -10,6 +10,7 @@ #include <inttypes.h> #include <limits.h> #include <stdbool.h> +#include <stddef.h> #include <stdio.h> #include <string.h> #include <sys/stat.h> @@ -37,7 +38,6 @@ #include "nvim/globals.h" #include "nvim/highlight_defs.h" #include "nvim/iconv.h" -#include "nvim/input.h" #include "nvim/log.h" #include "nvim/macros.h" #include "nvim/mbyte.h" @@ -76,78 +76,31 @@ # include "nvim/charset.h" #endif -#define BUFSIZE 8192 // size of normal write buffer -#define SMBUFSIZE 256 // size of emergency write buffer - // For compatibility with libuv < 1.20.0 (tested on 1.18.0) #ifndef UV_FS_COPYFILE_FICLONE # define UV_FS_COPYFILE_FICLONE 0 #endif -enum { - FIO_LATIN1 = 0x01, // convert Latin1 - FIO_UTF8 = 0x02, // convert UTF-8 - FIO_UCS2 = 0x04, // convert UCS-2 - FIO_UCS4 = 0x08, // convert UCS-4 - FIO_UTF16 = 0x10, // convert UTF-16 - FIO_ENDIAN_L = 0x80, // little endian - FIO_NOCONVERT = 0x2000, // skip encoding conversion - FIO_UCSBOM = 0x4000, // check for BOM at start of file - FIO_ALL = -1, // allow all formats -}; - -// When converting, a read() or write() may leave some bytes to be converted -// for the next call. The value is guessed... -#define CONV_RESTLEN 30 - -// We have to guess how much a sequence of bytes may expand when converting -// with iconv() to be able to allocate a buffer. -#define ICONV_MULT 8 - -// Structure to pass arguments from buf_write() to buf_write_bytes(). -struct bw_info { - int bw_fd; // file descriptor - char *bw_buf; // buffer with data to be written - int bw_len; // length of data - int bw_flags; // FIO_ flags - uint8_t bw_rest[CONV_RESTLEN]; // not converted bytes - int bw_restlen; // nr of bytes in bw_rest[] - int bw_first; // first write call - char *bw_conv_buf; // buffer for writing converted chars - size_t bw_conv_buflen; // size of bw_conv_buf - int bw_conv_error; // set for conversion error - linenr_T bw_conv_error_lnum; // first line with error or zero - linenr_T bw_start_lnum; // line number at start of buffer - iconv_t bw_iconv_fd; // descriptor for iconv() or -1 -}; - -typedef struct { - const char *num; - char *msg; - int arg; - bool alloc; -} Error_T; - -static char *err_readonly = "is read-only (cannot override: \"W\" in 'cpoptions')"; - #ifdef INCLUDE_GENERATED_DECLARATIONS # include "fileio.c.generated.h" #endif static const char *e_auchangedbuf = N_("E812: Autocommands changed buffer or buffer name"); -static const char e_no_matching_autocommands_for_buftype_str_buffer[] - = N_("E676: No matching autocommands for buftype=%s buffer"); void filemess(buf_T *buf, char *name, char *s, int attr) { int msg_scroll_save; + int prev_msg_col = msg_col; if (msg_silent != 0) { return; } + add_quoted_fname(IObuff, IOSIZE - 100, buf, name); + // Avoid an over-long translation to cause trouble. xstrlcat(IObuff, s, IOSIZE); + // For the first message may have to start a new line. // For further ones overwrite the previous one, reset msg_scroll before // calling filemess(). @@ -159,6 +112,9 @@ void filemess(buf_T *buf, char *name, char *s, int attr) msg_check_for_delay(false); } msg_start(); + if (prev_msg_col != 0 && msg_col == 0) { + msg_putchar('\r'); // overwrite any previous message. + } msg_scroll = msg_scroll_save; msg_scrolled_ign = true; // may truncate the message to avoid a hit-return prompt @@ -1797,6 +1753,9 @@ failed: msg_scrolled_ign = true; if (!read_stdin && !read_buffer) { + if (msg_col > 0) { + msg_putchar('\r'); // overwrite previous message + } p = (uint8_t *)msg_trunc_attr(IObuff, false, 0); } @@ -2096,1558 +2055,9 @@ char *new_file_message(void) return shortmess(SHM_NEW) ? _("[New]") : _("[New File]"); } -static int buf_write_do_autocmds(buf_T *buf, char **fnamep, char **sfnamep, char **ffnamep, - linenr_T start, linenr_T *endp, exarg_T *eap, bool append, - bool filtering, bool reset_changed, bool overwriting, bool whole, - const pos_T orig_start, const pos_T orig_end) -{ - linenr_T old_line_count = buf->b_ml.ml_line_count; - int msg_save = msg_scroll; - - aco_save_T aco; - bool did_cmd = false; - bool nofile_err = false; - bool empty_memline = buf->b_ml.ml_mfp == NULL; - bufref_T bufref; - - char *sfname = *sfnamep; - - // Apply PRE autocommands. - // Set curbuf to the buffer to be written. - // Careful: The autocommands may call buf_write() recursively! - bool buf_ffname = *ffnamep == buf->b_ffname; - bool buf_sfname = sfname == buf->b_sfname; - bool buf_fname_f = *fnamep == buf->b_ffname; - bool buf_fname_s = *fnamep == buf->b_sfname; - - // Set curwin/curbuf to buf and save a few things. - aucmd_prepbuf(&aco, buf); - set_bufref(&bufref, buf); - - if (append) { - did_cmd = apply_autocmds_exarg(EVENT_FILEAPPENDCMD, sfname, sfname, false, curbuf, eap); - if (!did_cmd) { - if (overwriting && bt_nofilename(curbuf)) { - nofile_err = true; - } else { - 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) { - bool was_changed = curbufIsChanged(); - - did_cmd = apply_autocmds_exarg(EVENT_BUFWRITECMD, sfname, sfname, false, curbuf, eap); - if (did_cmd) { - if (was_changed && !curbufIsChanged()) { - // Written everything correctly and BufWriteCmd has reset - // 'modified': Correct the undo information so that an - // undo now sets 'modified'. - u_unchanged(curbuf); - u_update_save_nr(curbuf); - } - } else { - if (overwriting && bt_nofilename(curbuf)) { - nofile_err = true; - } else { - apply_autocmds_exarg(EVENT_BUFWRITEPRE, - sfname, sfname, false, curbuf, eap); - } - } - } else { - did_cmd = apply_autocmds_exarg(EVENT_FILEWRITECMD, sfname, sfname, false, curbuf, eap); - if (!did_cmd) { - if (overwriting && bt_nofilename(curbuf)) { - nofile_err = true; - } else { - apply_autocmds_exarg(EVENT_FILEWRITEPRE, - sfname, sfname, false, curbuf, eap); - } - } - } - - // restore curwin/curbuf and a few other things - aucmd_restbuf(&aco); - - // In three situations we return here and don't write the file: - // 1. the autocommands deleted or unloaded the buffer. - // 2. The autocommands abort script processing. - // 3. If one of the "Cmd" autocommands was executed. - if (!bufref_valid(&bufref)) { - buf = NULL; - } - if (buf == NULL || (buf->b_ml.ml_mfp == NULL && !empty_memline) - || did_cmd || nofile_err - || aborting()) { - if (buf != NULL && (cmdmod.cmod_flags & CMOD_LOCKMARKS)) { - // restore the original '[ and '] positions - buf->b_op_start = orig_start; - buf->b_op_end = orig_end; - } - - no_wait_return--; - msg_scroll = msg_save; - if (nofile_err) { - semsg(_(e_no_matching_autocommands_for_buftype_str_buffer), curbuf->b_p_bt); - } - - if (nofile_err - || aborting()) { - // An aborting error, interrupt or exception in the - // autocommands. - return FAIL; - } - if (did_cmd) { - if (buf == NULL) { - // The buffer was deleted. We assume it was written - // (can't retry anyway). - return OK; - } - if (overwriting) { - // Assume the buffer was written, update the timestamp. - 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)) { - // Buffer still changed, the autocommands didn't work properly. - return FAIL; - } - return OK; - } - if (!aborting()) { - emsg(_("E203: Autocommands deleted or unloaded buffer to be written")); - } - return FAIL; - } - - // The autocommands may have changed the number of lines in the file. - // When writing the whole file, adjust the end. - // When writing part of the file, assume that the autocommands only - // changed the number of lines that are to be written (tricky!). - if (buf->b_ml.ml_line_count != old_line_count) { - if (whole) { // write all - *endp = buf->b_ml.ml_line_count; - } else if (buf->b_ml.ml_line_count > old_line_count) { // more lines - *endp += buf->b_ml.ml_line_count - old_line_count; - } else { // less lines - *endp -= old_line_count - buf->b_ml.ml_line_count; - if (*endp < start) { - no_wait_return--; - msg_scroll = msg_save; - emsg(_("E204: Autocommand changed number of lines in unexpected way")); - return FAIL; - } - } - } - - // The autocommands may have changed the name of the buffer, which may - // be kept in fname, ffname and sfname. - if (buf_ffname) { - *ffnamep = buf->b_ffname; - } - if (buf_sfname) { - *sfnamep = buf->b_sfname; - } - if (buf_fname_f) { - *fnamep = buf->b_ffname; - } - if (buf_fname_s) { - *fnamep = buf->b_sfname; - } - return NOTDONE; -} - -static void buf_write_do_post_autocmds(buf_T *buf, char *fname, exarg_T *eap, bool append, - bool filtering, bool reset_changed, bool whole) -{ - aco_save_T aco; - - curbuf->b_no_eol_lnum = 0; // in case it was set by the previous read - - // Apply POST autocommands. - // Careful: The autocommands may call buf_write() recursively! - 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); - } - - // restore curwin/curbuf and a few other things - aucmd_restbuf(&aco); -} - -static inline Error_T set_err_num(const char *num, const char *msg) -{ - return (Error_T){ .num = num, .msg = (char *)msg, .arg = 0 }; -} - -static inline Error_T set_err_arg(const char *msg, int arg) -{ - return (Error_T){ .num = NULL, .msg = (char *)msg, .arg = arg }; -} - -static inline Error_T set_err(const char *msg) -{ - return (Error_T){ .num = NULL, .msg = (char *)msg, .arg = 0 }; -} - -static void emit_err(Error_T *e) -{ - if (e->num != NULL) { - if (e->arg != 0) { - semsg("%s: %s%s: %s", e->num, IObuff, e->msg, os_strerror(e->arg)); - } else { - semsg("%s: %s%s", e->num, IObuff, e->msg); - } - } else if (e->arg != 0) { - semsg(e->msg, os_strerror(e->arg)); - } else { - emsg(e->msg); - } - if (e->alloc) { - xfree(e->msg); - } -} - -#if defined(UNIX) - -static int get_fileinfo_os(char *fname, FileInfo *file_info_old, bool overwriting, long *perm, - bool *device, bool *newfile, Error_T *err) -{ - *perm = -1; - if (!os_fileinfo(fname, file_info_old)) { - *newfile = true; - } else { - *perm = (long)file_info_old->stat.st_mode; - if (!S_ISREG(file_info_old->stat.st_mode)) { // not a file - if (S_ISDIR(file_info_old->stat.st_mode)) { - *err = set_err_num("E502", _("is a directory")); - return FAIL; - } - if (os_nodetype(fname) != NODE_WRITABLE) { - *err = set_err_num("E503", _("is not a file or writable device")); - return FAIL; - } - // It's a device of some kind (or a fifo) which we can write to - // but for which we can't make a backup. - *device = true; - *newfile = true; - *perm = -1; - } - } - return OK; -} - -#else - -static int get_fileinfo_os(char *fname, FileInfo *file_info_old, bool overwriting, long *perm, - bool *device, bool *newfile, Error_T *err) -{ - // Check for a writable device name. - char nodetype = fname == NULL ? NODE_OTHER : (char)os_nodetype(fname); - if (nodetype == NODE_OTHER) { - *err = set_err_num("E503", _("is not a file or writable device")); - return FAIL; - } - if (nodetype == NODE_WRITABLE) { - *device = true; - *newfile = true; - *perm = -1; - } else { - *perm = os_getperm(fname); - if (*perm < 0) { - *newfile = true; - } else if (os_isdir(fname)) { - *err = set_err_num("E502", _("is a directory")); - return FAIL; - } - if (overwriting) { - os_fileinfo(fname, file_info_old); - } - } - return OK; -} - -#endif - -/// @param buf -/// @param fname File name -/// @param overwriting -/// @param forceit -/// @param[out] file_info_old -/// @param[out] perm -/// @param[out] device -/// @param[out] newfile -/// @param[out] readonly -static int get_fileinfo(buf_T *buf, char *fname, bool overwriting, bool forceit, - FileInfo *file_info_old, long *perm, bool *device, bool *newfile, - bool *readonly, Error_T *err) -{ - if (get_fileinfo_os(fname, file_info_old, overwriting, perm, device, newfile, err) == FAIL) { - return FAIL; - } - - *readonly = false; // overwritten file is read-only - - if (!*device && !*newfile) { - // Check if the file is really writable (when renaming the file to - // make a backup we won't discover it later). - *readonly = !os_file_is_writable(fname); - - if (!forceit && *readonly) { - if (vim_strchr(p_cpo, CPO_FWRITE) != NULL) { - *err = set_err_num("E504", _(err_readonly)); - } else { - *err = set_err_num("E505", _("is read-only (add ! to override)")); - } - return FAIL; - } - - // If 'forceit' is false, check if the timestamp hasn't changed since reading the file. - if (overwriting && !forceit) { - int retval = check_mtime(buf, file_info_old); - if (retval == FAIL) { - return FAIL; - } - } - } - return OK; -} - -static int buf_write_make_backup(char *fname, bool append, FileInfo *file_info_old, vim_acl_T acl, - long perm, unsigned int bkc, bool file_readonly, bool forceit, - int *backup_copyp, char **backupp, Error_T *err) -{ - FileInfo file_info; - const bool no_prepend_dot = false; - - if ((bkc & BKC_YES) || append) { // "yes" - *backup_copyp = true; - } else if ((bkc & BKC_AUTO)) { // "auto" - // Don't rename the file when: - // - it's a hard link - // - it's a symbolic link - // - we don't have write permission in the directory - if (os_fileinfo_hardlinks(file_info_old) > 1 - || !os_fileinfo_link(fname, &file_info) - || !os_fileinfo_id_equal(&file_info, file_info_old)) { - *backup_copyp = true; - } else { - // Check if we can create a file and set the owner/group to - // the ones from the original file. - // First find a file name that doesn't exist yet (use some - // arbitrary numbers). - STRCPY(IObuff, fname); - for (int i = 4913;; i += 123) { - char *tail = path_tail(IObuff); - size_t size = (size_t)(tail - IObuff); - snprintf(tail, IOSIZE - size, "%d", i); - if (!os_fileinfo_link(IObuff, &file_info)) { - break; - } - } - int fd = os_open(IObuff, - O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, (int)perm); - if (fd < 0) { // can't write in directory - *backup_copyp = true; - } else { -#ifdef UNIX - os_fchown(fd, (uv_uid_t)file_info_old->stat.st_uid, (uv_gid_t)file_info_old->stat.st_gid); - if (!os_fileinfo(IObuff, &file_info) - || file_info.stat.st_uid != file_info_old->stat.st_uid - || file_info.stat.st_gid != file_info_old->stat.st_gid - || (long)file_info.stat.st_mode != perm) { - *backup_copyp = true; - } -#endif - // Close the file before removing it, on MS-Windows we - // can't delete an open file. - close(fd); - os_remove(IObuff); - } - } - } - - // Break symlinks and/or hardlinks if we've been asked to. - if ((bkc & BKC_BREAKSYMLINK) || (bkc & BKC_BREAKHARDLINK)) { -#ifdef UNIX - bool file_info_link_ok = os_fileinfo_link(fname, &file_info); - - // Symlinks. - if ((bkc & BKC_BREAKSYMLINK) - && file_info_link_ok - && !os_fileinfo_id_equal(&file_info, file_info_old)) { - *backup_copyp = false; - } - - // Hardlinks. - if ((bkc & BKC_BREAKHARDLINK) - && os_fileinfo_hardlinks(file_info_old) > 1 - && (!file_info_link_ok - || os_fileinfo_id_equal(&file_info, file_info_old))) { - *backup_copyp = false; - } -#endif - } - - // make sure we have a valid backup extension to use - char *backup_ext = *p_bex == NUL ? ".bak" : p_bex; - - if (*backup_copyp) { - int some_error = false; - - // Try to make the backup in each directory in the 'bdir' option. - // - // Unix semantics has it, that we may have a writable file, - // that cannot be recreated with a simple open(..., O_CREAT, ) e.g: - // - the directory is not writable, - // - the file may be a symbolic link, - // - the file may belong to another user/group, etc. - // - // For these reasons, the existing writable file must be truncated - // and reused. Creation of a backup COPY will be attempted. - char *dirp = p_bdir; - while (*dirp) { - // Isolate one directory name, using an entry in 'bdir'. - size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); - char *p = IObuff + dir_len; - bool trailing_pathseps = after_pathsep(IObuff, p) && p[-1] == p[-2]; - if (trailing_pathseps) { - IObuff[dir_len - 2] = NUL; - } - if (*dirp == NUL && !os_isdir(IObuff)) { - int ret; - char *failed_dir; - if ((ret = os_mkdir_recurse(IObuff, 0755, &failed_dir, NULL)) != 0) { - semsg(_("E303: Unable to create directory \"%s\" for backup file: %s"), - failed_dir, os_strerror(ret)); - xfree(failed_dir); - } - } - if (trailing_pathseps) { - // Ends with '//', Use Full path - if ((p = make_percent_swname(IObuff, fname)) - != NULL) { - *backupp = modname(p, backup_ext, no_prepend_dot); - xfree(p); - } - } - - char *rootname = get_file_in_dir(fname, IObuff); - if (rootname == NULL) { - some_error = true; // out of memory - goto nobackup; - } - - FileInfo file_info_new; - { - // - // Make the backup file name. - // - if (*backupp == NULL) { - *backupp = modname(rootname, backup_ext, no_prepend_dot); - } - - if (*backupp == NULL) { - xfree(rootname); - some_error = true; // out of memory - goto nobackup; - } - - // Check if backup file already exists. - if (os_fileinfo(*backupp, &file_info_new)) { - if (os_fileinfo_id_equal(&file_info_new, file_info_old)) { - // - // Backup file is same as original file. - // May happen when modname() gave the same file back (e.g. silly - // link). If we don't check here, we either ruin the file when - // copying or erase it after writing. - // - XFREE_CLEAR(*backupp); // no backup file to delete - } else if (!p_bk) { - // We are not going to keep the backup file, so don't - // delete an existing one, and try to use another name instead. - // Change one character, just before the extension. - // - char *wp = *backupp + strlen(*backupp) - 1 - strlen(backup_ext); - if (wp < *backupp) { // empty file name ??? - wp = *backupp; - } - *wp = 'z'; - while (*wp > 'a' && os_fileinfo(*backupp, &file_info_new)) { - (*wp)--; - } - // They all exist??? Must be something wrong. - if (*wp == 'a') { - XFREE_CLEAR(*backupp); - } - } - } - } - xfree(rootname); - - // Try to create the backup file - if (*backupp != NULL) { - // remove old backup, if present - os_remove(*backupp); - - // set file protection same as original file, but - // strip s-bit. - (void)os_setperm(*backupp, perm & 0777); - -#ifdef UNIX - // - // Try to set the group of the backup same as the original file. If - // this fails, set the protection bits for the group same as the - // protection bits for others. - // - if (file_info_new.stat.st_gid != file_info_old->stat.st_gid - && os_chown(*backupp, (uv_uid_t)-1, (uv_gid_t)file_info_old->stat.st_gid) != 0) { - os_setperm(*backupp, ((int)perm & 0707) | (((int)perm & 07) << 3)); - } -#endif - - // copy the file - if (os_copy(fname, *backupp, UV_FS_COPYFILE_FICLONE) != 0) { - *err = set_err(_("E509: Cannot create backup file (add ! to override)")); - XFREE_CLEAR(*backupp); - *backupp = NULL; - continue; - } - -#ifdef UNIX - os_file_settime(*backupp, - (double)file_info_old->stat.st_atim.tv_sec, - (double)file_info_old->stat.st_mtim.tv_sec); -#endif - os_set_acl(*backupp, acl); - *err = set_err(NULL); - break; - } - } - -nobackup: - if (*backupp == NULL && err->msg == NULL) { - *err = set_err(_("E509: Cannot create backup file (add ! to override)")); - } - // Ignore errors when forceit is true. - if ((some_error || err->msg != NULL) && !forceit) { - return FAIL; - } - *err = set_err(NULL); - } else { - // Make a backup by renaming the original file. - - // If 'cpoptions' includes the "W" flag, we don't want to - // overwrite a read-only file. But rename may be possible - // anyway, thus we need an extra check here. - if (file_readonly && vim_strchr(p_cpo, CPO_FWRITE) != NULL) { - *err = set_err_num("E504", _(err_readonly)); - return FAIL; - } - - // Form the backup file name - change path/fo.o.h to - // path/fo.o.h.bak Try all directories in 'backupdir', first one - // that works is used. - char *dirp = p_bdir; - while (*dirp) { - // Isolate one directory name and make the backup file name. - size_t dir_len = copy_option_part(&dirp, IObuff, IOSIZE, ","); - char *p = IObuff + dir_len; - bool trailing_pathseps = after_pathsep(IObuff, p) && p[-1] == p[-2]; - if (trailing_pathseps) { - IObuff[dir_len - 2] = NUL; - } - if (*dirp == NUL && !os_isdir(IObuff)) { - int ret; - char *failed_dir; - if ((ret = os_mkdir_recurse(IObuff, 0755, &failed_dir, NULL)) != 0) { - semsg(_("E303: Unable to create directory \"%s\" for backup file: %s"), - failed_dir, os_strerror(ret)); - xfree(failed_dir); - } - } - if (trailing_pathseps) { - // path ends with '//', use full path - if ((p = make_percent_swname(IObuff, fname)) - != NULL) { - *backupp = modname(p, backup_ext, no_prepend_dot); - xfree(p); - } - } - - if (*backupp == NULL) { - char *rootname = get_file_in_dir(fname, IObuff); - if (rootname == NULL) { - *backupp = NULL; - } else { - *backupp = modname(rootname, backup_ext, no_prepend_dot); - xfree(rootname); - } - } - - if (*backupp != NULL) { - // If we are not going to keep the backup file, don't - // delete an existing one, try to use another name. - // Change one character, just before the extension. - if (!p_bk && os_path_exists(*backupp)) { - p = *backupp + strlen(*backupp) - 1 - strlen(backup_ext); - if (p < *backupp) { // empty file name ??? - p = *backupp; - } - *p = 'z'; - while (*p > 'a' && os_path_exists(*backupp)) { - (*p)--; - } - // They all exist??? Must be something wrong! - if (*p == 'a') { - XFREE_CLEAR(*backupp); - } - } - } - if (*backupp != NULL) { - // Delete any existing backup and move the current version - // to the backup. For safety, we don't remove the backup - // until the write has finished successfully. And if the - // 'backup' option is set, leave it around. - - // If the renaming of the original file to the backup file - // works, quit here. - /// - if (vim_rename(fname, *backupp) == 0) { - break; - } - - XFREE_CLEAR(*backupp); // don't do the rename below - } - } - if (*backupp == NULL && !forceit) { - *err = set_err(_("E510: Can't make backup file (add ! to override)")); - return FAIL; - } - } - return OK; -} - -/// buf_write() - write to file "fname" lines "start" through "end" -/// -/// We do our own buffering here because fwrite() is so slow. -/// -/// If "forceit" is true, we don't care for errors when attempting backups. -/// In case of an error everything possible is done to restore the original -/// file. But when "forceit" is true, we risk losing it. -/// -/// When "reset_changed" is true and "append" == false and "start" == 1 and -/// "end" == curbuf->b_ml.ml_line_count, reset curbuf->b_changed. -/// -/// This function must NOT use NameBuff (because it's called by autowrite()). -/// -/// -/// @param eap for forced 'ff' and 'fenc', can be NULL! -/// @param append append to the file -/// -/// @return FAIL for failure, OK otherwise -int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T end, exarg_T *eap, - int append, int forceit, int reset_changed, int filtering) -{ - int retval = OK; - int msg_save = msg_scroll; - int prev_got_int = got_int; - // writing everything - int whole = (start == 1 && end == buf->b_ml.ml_line_count); - int write_undo_file = false; - context_sha256_T sha_ctx; - unsigned int bkc = get_bkc_value(buf); - - if (fname == NULL || *fname == NUL) { // safety check - return FAIL; - } - if (buf->b_ml.ml_mfp == NULL) { - // This can happen during startup when there is a stray "w" in the - // vimrc file. - emsg(_(e_emptybuf)); - return FAIL; - } - - // Disallow writing in secure mode. - if (check_secure()) { - return FAIL; - } - - // Avoid a crash for a long name. - if (strlen(fname) >= MAXPATHL) { - emsg(_(e_longname)); - return FAIL; - } - - // must init bw_conv_buf and bw_iconv_fd before jumping to "fail" - struct bw_info write_info; // info for buf_write_bytes() - write_info.bw_conv_buf = NULL; - write_info.bw_conv_error = false; - write_info.bw_conv_error_lnum = 0; - write_info.bw_restlen = 0; - write_info.bw_iconv_fd = (iconv_t)-1; - - // After writing a file changedtick changes but we don't want to display - // the line. - ex_no_reprint = true; - - // If there is no file name yet, use the one for the written file. - // BF_NOTEDITED is set to reflect this (in case the write fails). - // Don't do this when the write is for a filter command. - // Don't do this when appending. - // Only do this when 'cpoptions' contains the 'F' flag. - if (buf->b_ffname == NULL - && reset_changed - && whole - && buf == curbuf - && !bt_nofilename(buf) - && !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; // just in case autocmds made "buf" invalid - } - - if (sfname == NULL) { - sfname = fname; - } - - // For Unix: Use the short file name whenever possible. - // Avoids problems with networks and when directory names are changed. - // Don't do this for Windows, a "cd" in a sub-shell may have moved us to - // another directory, which we don't detect. - char *ffname = fname; // remember full fname -#ifdef UNIX - fname = sfname; -#endif - -// true if writing over original - int overwriting = buf->b_ffname != NULL && path_fnamecmp(ffname, buf->b_ffname) == 0; - - no_wait_return++; // don't wait for return yet - - const pos_T orig_start = buf->b_op_start; - const pos_T orig_end = buf->b_op_end; - - // Set '[ and '] marks to the lines to be written. - buf->b_op_start.lnum = start; - buf->b_op_start.col = 0; - buf->b_op_end.lnum = end; - buf->b_op_end.col = 0; - - int res = buf_write_do_autocmds(buf, &fname, &sfname, &ffname, start, &end, eap, append, - filtering, reset_changed, overwriting, whole, orig_start, - orig_end); - if (res != NOTDONE) { - return res; - } - - if (cmdmod.cmod_flags & CMOD_LOCKMARKS) { - // restore the original '[ and '] positions - buf->b_op_start = orig_start; - buf->b_op_end = orig_end; - } - - if (shortmess(SHM_OVER) && !exiting) { - msg_scroll = false; // overwrite previous file message - } else { - msg_scroll = true; // don't overwrite previous file message - } - if (!filtering) { - // show that we are busy -#ifndef UNIX - filemess(buf, sfname, "", 0); -#else - filemess(buf, fname, "", 0); -#endif - } - msg_scroll = false; // always overwrite the file message now - - char *buffer = verbose_try_malloc(BUFSIZE); - int bufsize; - char smallbuf[SMBUFSIZE]; - // can't allocate big buffer, use small one (to be able to write when out of - // memory) - if (buffer == NULL) { - buffer = smallbuf; - bufsize = SMBUFSIZE; - } else { - bufsize = BUFSIZE; - } - - Error_T err = { 0 }; - long perm; // file permissions - bool newfile = false; // true if file doesn't exist yet - bool device = false; // writing to a device - bool file_readonly = false; // overwritten file is read-only - char *backup = NULL; - char *fenc_tofree = NULL; // allocated "fenc" - - // Get information about original file (if there is one). - FileInfo file_info_old; - - vim_acl_T acl = NULL; // ACL copied from original file to - // backup or new file - - if (get_fileinfo(buf, fname, overwriting, forceit, &file_info_old, &perm, &device, &newfile, - &file_readonly, &err) == FAIL) { - goto fail; - } - - // For systems that support ACL: get the ACL from the original file. - if (!newfile) { - acl = os_get_acl(fname); - } - - // If 'backupskip' is not empty, don't make a backup for some files. - bool dobackup = (p_wb || p_bk || *p_pm != NUL); - if (dobackup && *p_bsk != NUL && match_file_list(p_bsk, sfname, ffname)) { - dobackup = false; - } - - int backup_copy = false; // copy the original file? - - // Save the value of got_int and reset it. We don't want a previous - // interruption cancel writing, only hitting CTRL-C while writing should - // abort it. - prev_got_int = got_int; - got_int = false; - - // Mark the buffer as 'being saved' to prevent changed buffer warnings - buf->b_saving = true; - - // If we are not appending or filtering, the file exists, and the - // 'writebackup', 'backup' or 'patchmode' option is set, need a backup. - // When 'patchmode' is set also make a backup when appending. - // - // Do not make any backup, if 'writebackup' and 'backup' are both switched - // off. This helps when editing large files on almost-full disks. - if (!(append && *p_pm == NUL) && !filtering && perm >= 0 && dobackup) { - if (buf_write_make_backup(fname, append, &file_info_old, acl, perm, bkc, file_readonly, forceit, - &backup_copy, &backup, &err) == FAIL) { - retval = FAIL; - goto fail; - } - } - -#if defined(UNIX) - int made_writable = false; // 'w' bit has been set - - // When using ":w!" and the file was read-only: make it writable - if (forceit && perm >= 0 && !(perm & 0200) - && file_info_old.stat.st_uid == getuid() - && vim_strchr(p_cpo, CPO_FWRITE) == NULL) { - perm |= 0200; - (void)os_setperm(fname, (int)perm); - made_writable = true; - } -#endif - - // When using ":w!" and writing to the current file, 'readonly' makes no - // sense, reset it, unless 'Z' appears in 'cpoptions'. - if (forceit && overwriting && vim_strchr(p_cpo, CPO_KEEPRO) == NULL) { - buf->b_p_ro = false; - need_maketitle = true; // set window title later - status_redraw_all(); // redraw status lines later - } - - 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; - } - - char *wfname = NULL; // name of file to write to - - // If the original file is being overwritten, there is a small chance that - // we crash in the middle of writing. Therefore the file is preserved now. - // This makes all block numbers positive so that recovery does not need - // the original file. - // Don't do this if there is a backup file and we are exiting. - if (reset_changed && !newfile && overwriting - && !(exiting && backup != NULL)) { - ml_preserve(buf, false, !!p_fs); - if (got_int) { - err = set_err(_(e_interr)); - goto restore_backup; - } - } - - // Default: write the file directly. May write to a temp file for - // multi-byte conversion. - wfname = fname; - - char *fenc; // effective 'fileencoding' - - // Check for forced 'fileencoding' from "++opt=val" argument. - 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; - } - - // Check if the file needs to be converted. - int converted = need_conversion(fenc); - int wb_flags = 0; - - // Check if UTF-8 to UCS-2/4 or Latin1 conversion needs to be done. Or - // Latin1 to Unicode conversion. This is handled in buf_write_bytes(). - // Prepare the flags for it and allocate bw_conv_buf when needed. - if (converted) { - wb_flags = get_fio_flags(fenc); - if (wb_flags & (FIO_UCS2 | FIO_UCS4 | FIO_UTF16 | FIO_UTF8)) { - // Need to allocate a buffer to translate into. - if (wb_flags & (FIO_UCS2 | FIO_UTF16 | FIO_UTF8)) { - write_info.bw_conv_buflen = (size_t)bufsize * 2; - } else { // FIO_UCS4 - write_info.bw_conv_buflen = (size_t)bufsize * 4; - } - write_info.bw_conv_buf = verbose_try_malloc(write_info.bw_conv_buflen); - if (!write_info.bw_conv_buf) { - end = 0; - } - } - } - - if (converted && wb_flags == 0) { - // Use iconv() conversion when conversion is needed and it's not done - // internally. - write_info.bw_iconv_fd = (iconv_t)my_iconv_open(fenc, "utf-8"); - if (write_info.bw_iconv_fd != (iconv_t)-1) { - // We're going to use iconv(), allocate a buffer to convert in. - write_info.bw_conv_buflen = (size_t)bufsize * ICONV_MULT; - write_info.bw_conv_buf = verbose_try_malloc(write_info.bw_conv_buflen); - if (!write_info.bw_conv_buf) { - end = 0; - } - write_info.bw_first = true; - } else { - // When the file needs to be converted with 'charconvert' after - // writing, write to a temp file instead and let the conversion - // overwrite the original file. - if (*p_ccv != NUL) { - wfname = vim_tempname(); - if (wfname == NULL) { // Can't write without a tempfile! - err = set_err(_("E214: Can't find temp file for writing")); - goto restore_backup; - } - } - } - } - - int notconverted = false; - - if (converted && wb_flags == 0 - && write_info.bw_iconv_fd == (iconv_t)-1 - && wfname == fname) { - if (!forceit) { - err = set_err(_("E213: Cannot convert (add ! to write without conversion)")); - goto restore_backup; - } - notconverted = true; - } - - int no_eol = false; // no end-of-line written - long nchars; - linenr_T lnum; - int fileformat; - int checking_conversion; - - int fd; - - // If conversion is taking place, we may first pretend to write and check - // for conversion errors. Then loop again to write for real. - // When not doing conversion this writes for real right away. - for (checking_conversion = true;; checking_conversion = false) { - // There is no need to check conversion when: - // - there is no conversion - // - we make a backup file, that can be restored in case of conversion - // failure. - if (!converted || dobackup) { - checking_conversion = false; - } - - if (checking_conversion) { - // Make sure we don't write anything. - fd = -1; - write_info.bw_fd = fd; - } else { - // Open the file "wfname" for writing. - // We may try to open the file twice: If we can't write to the file - // and forceit is true we delete the existing file and try to - // create a new one. If this still fails we may have lost the - // original file! (this may happen when the user reached his - // quotum for number of files). - // Appending will fail if the file does not exist and forceit is - // false. - const int fflags = O_WRONLY | (append - ? (forceit ? (O_APPEND | O_CREAT) : O_APPEND) - : (O_CREAT | O_TRUNC)); - const int mode = perm < 0 ? 0666 : (perm & 0777); - - while ((fd = os_open(wfname, fflags, mode)) < 0) { - // A forced write will try to create a new file if the old one - // is still readonly. This may also happen when the directory - // is read-only. In that case the mch_remove() will fail. - if (err.msg == NULL) { -#ifdef UNIX - FileInfo file_info; - - // Don't delete the file when it's a hard or symbolic link. - if ((!newfile && os_fileinfo_hardlinks(&file_info_old) > 1) - || (os_fileinfo_link(fname, &file_info) - && !os_fileinfo_id_equal(&file_info, &file_info_old))) { - err = set_err(_("E166: Can't open linked file for writing")); - } else { - err = set_err_arg(_("E212: Can't open file for writing: %s"), fd); - if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL && perm >= 0) { - // we write to the file, thus it should be marked - // writable after all - if (!(perm & 0200)) { - made_writable = true; - } - perm |= 0200; - if (file_info_old.stat.st_uid != getuid() - || file_info_old.stat.st_gid != getgid()) { - perm &= 0777; - } - if (!append) { // don't remove when appending - os_remove(wfname); - } - continue; - } - } -#else - err = set_err_arg(_("E212: Can't open file for writing: %s"), fd); - if (forceit && vim_strchr(p_cpo, CPO_FWRITE) == NULL && perm >= 0) { - if (!append) { // don't remove when appending - os_remove(wfname); - } - continue; - } -#endif - } - -restore_backup: - { - // If we failed to open the file, we don't need a backup. Throw it - // away. If we moved or removed the original file try to put the - // backup in its place. - if (backup != NULL && wfname == fname) { - if (backup_copy) { - // There is a small chance that we removed the original, - // try to move the copy in its place. - // This may not work if the vim_rename() fails. - // In that case we leave the copy around. - // If file does not exist, put the copy in its place - if (!os_path_exists(fname)) { - vim_rename(backup, fname); - } - // if original file does exist throw away the copy - if (os_path_exists(fname)) { - os_remove(backup); - } - } else { - // try to put the original file back - vim_rename(backup, fname); - } - } - - // if original file no longer exists give an extra warning - if (!newfile && !os_path_exists(fname)) { - end = 0; - } - } - - if (wfname != fname) { - xfree(wfname); - } - goto fail; - } - write_info.bw_fd = fd; - } - err = set_err(NULL); - - write_info.bw_buf = buffer; - nchars = 0; - - // use "++bin", "++nobin" or 'binary' - int write_bin; - if (eap != NULL && eap->force_bin != 0) { - write_bin = (eap->force_bin == FORCE_BIN); - } else { - write_bin = buf->b_p_bin; - } - - // Skip the BOM when appending and the file already existed, the BOM - // only makes sense at the start of the file. - if (buf->b_p_bomb && !write_bin && (!append || perm < 0)) { - write_info.bw_len = make_bom(buffer, fenc); - if (write_info.bw_len > 0) { - // don't convert - write_info.bw_flags = FIO_NOCONVERT | wb_flags; - if (buf_write_bytes(&write_info) == FAIL) { - end = 0; - } else { - nchars += write_info.bw_len; - } - } - } - write_info.bw_start_lnum = start; - - write_undo_file = (buf->b_p_udf && overwriting && !append - && !filtering && reset_changed && !checking_conversion); - if (write_undo_file) { - // Prepare for computing the hash value of the text. - sha256_start(&sha_ctx); - } - - write_info.bw_len = bufsize; - write_info.bw_flags = wb_flags; - fileformat = get_fileformat_force(buf, eap); - char *s = buffer; - int len = 0; - for (lnum = start; lnum <= end; lnum++) { - // The next while loop is done once for each character written. - // Keep it fast! - char *ptr = ml_get_buf(buf, lnum, false) - 1; - if (write_undo_file) { - sha256_update(&sha_ctx, (uint8_t *)ptr + 1, (uint32_t)(strlen(ptr + 1) + 1)); - } - char c; - while ((c = *++ptr) != NUL) { - if (c == NL) { - *s = NUL; // replace newlines with NULs - } else if (c == CAR && fileformat == EOL_MAC) { - *s = NL; // Mac: replace CRs with NLs - } else { - *s = c; - } - s++; - if (++len != bufsize) { - continue; - } - if (buf_write_bytes(&write_info) == FAIL) { - end = 0; // write error: break loop - break; - } - nchars += bufsize; - s = buffer; - len = 0; - write_info.bw_start_lnum = lnum; - } - // write failed or last line has no EOL: stop here - if (end == 0 - || (lnum == end - && (write_bin || !buf->b_p_fixeol) - && ((write_bin && lnum == buf->b_no_eol_lnum) - || (lnum == buf->b_ml.ml_line_count && !buf->b_p_eol)))) { - lnum++; // written the line, count it - no_eol = true; - break; - } - if (fileformat == EOL_UNIX) { - *s++ = NL; - } else { - *s++ = CAR; // EOL_MAC or EOL_DOS: write CR - if (fileformat == EOL_DOS) { // write CR-NL - if (++len == bufsize) { - if (buf_write_bytes(&write_info) == FAIL) { - end = 0; // write error: break loop - break; - } - nchars += bufsize; - s = buffer; - len = 0; - } - *s++ = NL; - } - } - if (++len == bufsize) { - if (buf_write_bytes(&write_info) == FAIL) { - end = 0; // Write error: break loop. - break; - } - nchars += bufsize; - s = buffer; - len = 0; - - os_breakcheck(); - if (got_int) { - end = 0; // Interrupted, break loop. - break; - } - } - } - if (len > 0 && end > 0) { - write_info.bw_len = len; - if (buf_write_bytes(&write_info) == FAIL) { - end = 0; // write error - } - nchars += len; - } - - if (!buf->b_p_fixeol && buf->b_p_eof) { - // write trailing CTRL-Z - (void)write_eintr(write_info.bw_fd, "\x1a", 1); - } - - // Stop when writing done or an error was encountered. - if (!checking_conversion || end == 0) { - break; - } - - // If no error happened until now, writing should be ok, so loop to - // really write the buffer. - } - - // If we started writing, finish writing. Also when an error was - // encountered. - if (!checking_conversion) { - // On many journalling file systems there is a bug that causes both the - // original and the backup file to be lost when halting the system right - // after writing the file. That's because only the meta-data is - // journalled. Syncing the file slows down the system, but assures it has - // been written to disk and we don't lose it. - // For a device do try the fsync() but don't complain if it does not work - // (could be a pipe). - // If the 'fsync' option is false, don't fsync(). Useful for laptops. - int error; - if (p_fs && (error = os_fsync(fd)) != 0 && !device - // fsync not supported on this storage. - && error != UV_ENOTSUP) { - err = set_err_arg(e_fsync, error); - end = 0; - } - -#ifdef UNIX - // When creating a new file, set its owner/group to that of the original - // file. Get the new device and inode number. - if (backup != NULL && !backup_copy) { - // don't change the owner when it's already OK, some systems remove - // permission or ACL stuff - FileInfo file_info; - if (!os_fileinfo(wfname, &file_info) - || file_info.stat.st_uid != file_info_old.stat.st_uid - || file_info.stat.st_gid != file_info_old.stat.st_gid) { - os_fchown(fd, (uv_uid_t)file_info_old.stat.st_uid, (uv_gid_t)file_info_old.stat.st_gid); - if (perm >= 0) { // Set permission again, may have changed. - (void)os_setperm(wfname, (int)perm); - } - } - buf_set_file_id(buf); - } else if (!buf->file_id_valid) { - // Set the file_id when creating a new file. - buf_set_file_id(buf); - } -#endif - - if ((error = os_close(fd)) != 0) { - err = set_err_arg(_("E512: Close failed: %s"), error); - end = 0; - } - -#ifdef UNIX - if (made_writable) { - perm &= ~0200; // reset 'w' bit for security reasons - } -#endif - if (perm >= 0) { // Set perm. of new file same as old file. - (void)os_setperm(wfname, (int)perm); - } - // Probably need to set the ACL before changing the user (can't set the - // ACL on a file the user doesn't own). - if (!backup_copy) { - os_set_acl(wfname, acl); - } - - if (wfname != fname) { - // The file was written to a temp file, now it needs to be converted - // with 'charconvert' to (overwrite) the output file. - if (end != 0) { - if (eval_charconvert("utf-8", fenc, wfname, fname) == FAIL) { - write_info.bw_conv_error = true; - end = 0; - } - } - os_remove(wfname); - xfree(wfname); - } - } - - if (end == 0) { - // Error encountered. - if (err.msg == NULL) { - if (write_info.bw_conv_error) { - if (write_info.bw_conv_error_lnum == 0) { - err = set_err(_("E513: write error, conversion failed " - "(make 'fenc' empty to override)")); - } else { - err = set_err(xmalloc(300)); - err.alloc = true; - vim_snprintf(err.msg, 300, // NOLINT(runtime/printf) - _("E513: write error, conversion failed in line %" PRIdLINENR - " (make 'fenc' empty to override)"), - write_info.bw_conv_error_lnum); - } - } else if (got_int) { - err = set_err(_(e_interr)); - } else { - err = set_err(_("E514: write error (file system full?)")); - } - } - - // If we have a backup file, try to put it in place of the new file, - // because the new file is probably corrupt. This avoids losing the - // original file when trying to make a backup when writing the file a - // second time. - // When "backup_copy" is set we need to copy the backup over the new - // file. Otherwise rename the backup file. - // If this is OK, don't give the extra warning message. - if (backup != NULL) { - if (backup_copy) { - // This may take a while, if we were interrupted let the user - // know we got the message. - if (got_int) { - msg(_(e_interr)); - ui_flush(); - } - - // copy the file. - if (os_copy(backup, fname, UV_FS_COPYFILE_FICLONE) - == 0) { - end = 1; // success - } - } else { - if (vim_rename(backup, fname) == 0) { - end = 1; - } - } - } - goto fail; - } - - lnum -= start; // compute number of written lines - no_wait_return--; // may wait for return now - -#if !defined(UNIX) - fname = sfname; // use shortname now, for the messages -#endif - if (!filtering) { - add_quoted_fname(IObuff, IOSIZE, buf, fname); - bool insert_space = false; - if (write_info.bw_conv_error) { - STRCAT(IObuff, _(" CONVERSION ERROR")); - insert_space = true; - if (write_info.bw_conv_error_lnum != 0) { - vim_snprintf_add(IObuff, IOSIZE, _(" in line %" PRId64 ";"), - (int64_t)write_info.bw_conv_error_lnum); - } - } else if (notconverted) { - STRCAT(IObuff, _("[NOT converted]")); - insert_space = true; - } else if (converted) { - STRCAT(IObuff, _("[converted]")); - insert_space = true; - } - if (device) { - STRCAT(IObuff, _("[Device]")); - insert_space = true; - } else if (newfile) { - STRCAT(IObuff, new_file_message()); - insert_space = true; - } - if (no_eol) { - msg_add_eol(); - insert_space = true; - } - // may add [unix/dos/mac] - if (msg_add_fileformat(fileformat)) { - insert_space = true; - } - msg_add_lines(insert_space, (long)lnum, nchars); // add line/char count - 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); - } - - // When written everything correctly: reset 'modified'. Unless not - // writing to the original file and '+' is not in 'cpoptions'. - if (reset_changed && whole && !append - && !write_info.bw_conv_error - && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) { - unchanged(buf, true, false); - const varnumber_T changedtick = buf_get_changedtick(buf); - if (buf->b_last_changedtick + 1 == changedtick) { - // b:changedtick may be incremented in unchanged() but that - // should not trigger a TextChanged event. - buf->b_last_changedtick = changedtick; - } - u_unchanged(buf); - u_update_save_nr(buf); - } - - // If written to the current file, update the timestamp of the swap file - // and reset the BF_WRITE_MASK flags. Also sets buf->b_mtime. - if (overwriting) { - ml_timestamp(buf); - if (append) { - buf->b_flags &= ~BF_NEW; - } else { - buf->b_flags &= ~BF_WRITE_MASK; - } - } - - // If we kept a backup until now, and we are in patch mode, then we make - // the backup file our 'original' file. - if (*p_pm && dobackup) { - char *const org = modname(fname, p_pm, false); - - if (backup != NULL) { - // If the original file does not exist yet - // the current backup file becomes the original file - if (org == NULL) { - emsg(_("E205: Patchmode: can't save original file")); - } else if (!os_path_exists(org)) { - vim_rename(backup, org); - XFREE_CLEAR(backup); // don't delete the file -#ifdef UNIX - os_file_settime(org, - (double)file_info_old.stat.st_atim.tv_sec, - (double)file_info_old.stat.st_mtim.tv_sec); -#endif - } - } else { - // If there is no backup file, remember that a (new) file was - // created. - int empty_fd; - - if (org == NULL - || (empty_fd = os_open(org, - O_CREAT | 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) { - os_setperm(org, os_getperm(fname) & 0777); - xfree(org); - } - } - - // Remove the backup unless 'backup' option is set - if (!p_bk && backup != NULL - && !write_info.bw_conv_error - && os_remove(backup) != 0) { - emsg(_("E207: Can't delete backup file")); - } - - goto nofail; - - // Finish up. We get here either after failure or success. -fail: - no_wait_return--; // may wait for return now -nofail: - - // Done saving, we accept changed buffer warnings again - buf->b_saving = false; - - xfree(backup); - if (buffer != smallbuf) { - xfree(buffer); - } - xfree(fenc_tofree); - xfree(write_info.bw_conv_buf); - if (write_info.bw_iconv_fd != (iconv_t)-1) { - iconv_close(write_info.bw_iconv_fd); - write_info.bw_iconv_fd = (iconv_t)-1; - } - os_free_acl(acl); - - if (err.msg != NULL) { - // - 100 to save some space for further error message -#ifndef UNIX - add_quoted_fname(IObuff, IOSIZE - 100, buf, sfname); -#else - add_quoted_fname(IObuff, IOSIZE - 100, buf, fname); -#endif - emit_err(&err); - - retval = FAIL; - if (end == 0) { - const int attr = HL_ATTR(HLF_E); // Set highlight for error messages. - 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); - - // Update the timestamp to avoid an "overwrite changed file" - // prompt when writing again. - if (os_fileinfo(fname, &file_info_old)) { - buf_store_file_info(buf, &file_info_old); - buf->b_mtime_read = buf->b_mtime; - buf->b_mtime_read_ns = buf->b_mtime_ns; - } - } - } - msg_scroll = msg_save; - - // When writing the whole file and 'undofile' is set, also write the undo - // file. - if (retval == OK && write_undo_file) { - uint8_t hash[UNDO_HASH_SIZE]; - - sha256_finish(&sha_ctx, hash); - u_write_undo(NULL, false, buf, hash); - } - - if (!should_abort(retval)) { - buf_write_do_post_autocmds(buf, fname, eap, append, filtering, reset_changed, whole); - if (aborting()) { // autocmds may abort script processing - retval = false; - } - } - - got_int |= prev_got_int; - - return retval; -} - /// Set the name of the current buffer. Use when the buffer doesn't have a /// name and a ":r" or ":w" command with a file name is used. -static int set_rw_fname(char *fname, char *sfname) +int set_rw_fname(char *fname, char *sfname) { buf_T *buf = curbuf; @@ -3697,8 +2107,8 @@ static int set_rw_fname(char *fname, char *sfname) /// @param[in] buf_len ret_buf length. /// @param[in] buf buf_T file name is coming from. /// @param[in] fname File name to write. -static void add_quoted_fname(char *const ret_buf, const size_t buf_len, const buf_T *const buf, - const char *fname) +void add_quoted_fname(char *const ret_buf, const size_t buf_len, const buf_T *const buf, + const char *fname) FUNC_ATTR_NONNULL_ARG(1) { if (fname == NULL) { @@ -3714,7 +2124,7 @@ static void add_quoted_fname(char *const ret_buf, const size_t buf_len, const bu /// @param eol_type line ending type /// /// @return true if something was appended. -static bool msg_add_fileformat(int eol_type) +bool msg_add_fileformat(int eol_type) { #ifndef USE_CRNL if (eol_type == EOL_DOS) { @@ -3758,33 +2168,13 @@ void msg_add_lines(int insert_space, long lnum, off_T nchars) } /// Append message for missing line separator to IObuff. -static void msg_add_eol(void) +void msg_add_eol(void) { STRCAT(IObuff, shortmess(SHM_LAST) ? _("[noeol]") : _("[Incomplete last line]")); } -/// Check modification time of file, before writing to it. -/// The size isn't checked, because using a tool like "gzip" takes care of -/// using the same timestamp but can't set the size. -static int check_mtime(buf_T *buf, FileInfo *file_info) -{ - if (buf->b_mtime_read != 0 - && time_differs(file_info, buf->b_mtime_read, buf->b_mtime_read_ns)) { - msg_scroll = true; // Don't overwrite messages here. - msg_silent = 0; // Must give this prompt. - // Don't use emsg() here, don't want to flush the buffers. - msg_attr(_("WARNING: The file has been changed since reading it!!!"), - HL_ATTR(HLF_E)); - if (ask_yesno(_("Do you really want to write to it"), true) == 'n') { - return FAIL; - } - msg_scroll = false; // Always overwrite the file message now. - } - return OK; -} - -static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) FUNC_ATTR_CONST +bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) FUNC_ATTR_CONST { #if defined(__linux__) || defined(MSWIN) return file_info->stat.st_mtim.tv_nsec != mtime_ns @@ -3799,263 +2189,13 @@ static bool time_differs(const FileInfo *file_info, long mtime, long mtime_ns) F #endif } -static int buf_write_convert_with_iconv(struct bw_info *ip, char **bufp, int *lenp) -{ - const char *from; - size_t fromlen; - size_t tolen; - - int len = *lenp; - - // Convert with iconv(). - if (ip->bw_restlen > 0) { - // Need to concatenate the remainder of the previous call and - // the bytes of the current call. Use the end of the - // conversion buffer for this. - fromlen = (size_t)len + (size_t)ip->bw_restlen; - char *fp = ip->bw_conv_buf + ip->bw_conv_buflen - fromlen; - memmove(fp, ip->bw_rest, (size_t)ip->bw_restlen); - memmove(fp + ip->bw_restlen, *bufp, (size_t)len); - from = fp; - tolen = ip->bw_conv_buflen - fromlen; - } else { - from = *bufp; - fromlen = (size_t)len; - tolen = ip->bw_conv_buflen; - } - char *to = ip->bw_conv_buf; - - if (ip->bw_first) { - size_t save_len = tolen; - - // output the initial shift state sequence - (void)iconv(ip->bw_iconv_fd, NULL, NULL, &to, &tolen); - - // There is a bug in iconv() on Linux (which appears to be - // wide-spread) which sets "to" to NULL and messes up "tolen". - if (to == NULL) { - to = ip->bw_conv_buf; - tolen = save_len; - } - ip->bw_first = false; - } - - // If iconv() has an error or there is not enough room, fail. - 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; - } - - // copy remainder to ip->bw_rest[] to be used for the next call. - if (fromlen > 0) { - memmove(ip->bw_rest, (void *)from, fromlen); - } - ip->bw_restlen = (int)fromlen; - - *bufp = ip->bw_conv_buf; - *lenp = (int)(to - ip->bw_conv_buf); - - return OK; -} - -static int buf_write_convert(struct bw_info *ip, char **bufp, int *lenp) -{ - int flags = ip->bw_flags; // extra flags - - if (flags & FIO_UTF8) { - // Convert latin1 in the buffer to UTF-8 in the file. - char *p = ip->bw_conv_buf; // translate to buffer - for (int wlen = 0; wlen < *lenp; wlen++) { - p += utf_char2bytes((uint8_t)(*bufp)[wlen], p); - } - *bufp = ip->bw_conv_buf; - *lenp = (int)(p - ip->bw_conv_buf); - } else if (flags & (FIO_UCS4 | FIO_UTF16 | FIO_UCS2 | FIO_LATIN1)) { - unsigned c; - int n = 0; - // Convert UTF-8 bytes in the buffer to UCS-2, UCS-4, UTF-16 or - // Latin1 chars in the file. - // translate in-place (can only get shorter) or to buffer - char *p = flags & FIO_LATIN1 ? *bufp : ip->bw_conv_buf; - for (int wlen = 0; wlen < *lenp; wlen += n) { - if (wlen == 0 && ip->bw_restlen != 0) { - // Use remainder of previous call. Append the start of - // buf[] to get a full sequence. Might still be too - // short! - int l = MIN(*lenp, CONV_RESTLEN - ip->bw_restlen); - memmove(ip->bw_rest + ip->bw_restlen, *bufp, (size_t)l); - n = utf_ptr2len_len((char *)ip->bw_rest, ip->bw_restlen + l); - if (n > ip->bw_restlen + *lenp) { - // We have an incomplete byte sequence at the end to - // be written. We can't convert it without the - // remaining bytes. Keep them for the next call. - if (ip->bw_restlen + *lenp > CONV_RESTLEN) { - return FAIL; - } - ip->bw_restlen += *lenp; - break; - } - if (n > 1) { - c = (unsigned)utf_ptr2char((char *)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; - memmove(ip->bw_rest, ip->bw_rest + n, - (size_t)ip->bw_restlen); - n = 0; - } - } else { - n = utf_ptr2len_len(*bufp + wlen, *lenp - wlen); - if (n > *lenp - wlen) { - // We have an incomplete byte sequence at the end to - // be written. We can't convert it without the - // remaining bytes. Keep them for the next call. - if (*lenp - wlen > CONV_RESTLEN) { - return FAIL; - } - ip->bw_restlen = *lenp - wlen; - memmove(ip->bw_rest, *bufp + wlen, - (size_t)ip->bw_restlen); - break; - } - if (n > 1) { - c = (unsigned)utf_ptr2char(*bufp + wlen); - } else { - c = (uint8_t)(*bufp)[wlen]; - } - } - - if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) { - ip->bw_conv_error = true; - ip->bw_conv_error_lnum = ip->bw_start_lnum; - } - if (c == NL) { - ip->bw_start_lnum++; - } - } - if (flags & FIO_LATIN1) { - *lenp = (int)(p - *bufp); - } else { - *bufp = ip->bw_conv_buf; - *lenp = (int)(p - ip->bw_conv_buf); - } - } - - if (ip->bw_iconv_fd != (iconv_t)-1) { - if (buf_write_convert_with_iconv(ip, bufp, lenp) == FAIL) { - return FAIL; - } - } - - return OK; -} - -/// Call write() to write a number of bytes to the file. -/// Handles 'encoding' conversion. -/// -/// @return FAIL for failure, OK otherwise. -static int buf_write_bytes(struct bw_info *ip) -{ - char *buf = ip->bw_buf; // data to write - int len = ip->bw_len; // length of data - int flags = ip->bw_flags; // extra flags - - // Skip conversion when writing the BOM. - if (!(flags & FIO_NOCONVERT)) { - if (buf_write_convert(ip, &buf, &len) == FAIL) { - return FAIL; - } - } - - if (ip->bw_fd < 0) { - // Only checking conversion, which is OK if we get here. - return OK; - } - int wlen = (int)write_eintr(ip->bw_fd, buf, (size_t)len); - return (wlen < len) ? FAIL : OK; -} - -/// Convert a Unicode character to bytes. -/// -/// @param c character to convert -/// @param[in,out] pp pointer to store the result at -/// @param flags FIO_ flags that specify which encoding to use -/// -/// @return true for an error, false when it's OK. -static bool ucs2bytes(unsigned c, char **pp, int flags) - FUNC_ATTR_NONNULL_ALL -{ - uint8_t *p = (uint8_t *)(*pp); - bool error = false; - - if (flags & FIO_UCS4) { - if (flags & FIO_ENDIAN_L) { - *p++ = (uint8_t)c; - *p++ = (uint8_t)(c >> 8); - *p++ = (uint8_t)(c >> 16); - *p++ = (uint8_t)(c >> 24); - } else { - *p++ = (uint8_t)(c >> 24); - *p++ = (uint8_t)(c >> 16); - *p++ = (uint8_t)(c >> 8); - *p++ = (uint8_t)c; - } - } else if (flags & (FIO_UCS2 | FIO_UTF16)) { - if (c >= 0x10000) { - if (flags & FIO_UTF16) { - // Make two words, ten bits of the character in each. First - // word is 0xd800 - 0xdbff, second one 0xdc00 - 0xdfff - c -= 0x10000; - if (c >= 0x100000) { - error = true; - } - int cc = (int)(((c >> 10) & 0x3ff) + 0xd800); - if (flags & FIO_ENDIAN_L) { - *p++ = (uint8_t)cc; - *p++ = (uint8_t)(cc >> 8); - } else { - *p++ = (uint8_t)(cc >> 8); - *p++ = (uint8_t)cc; - } - c = (c & 0x3ff) + 0xdc00; - } else { - error = true; - } - } - if (flags & FIO_ENDIAN_L) { - *p++ = (uint8_t)c; - *p++ = (uint8_t)(c >> 8); - } else { - *p++ = (uint8_t)(c >> 8); - *p++ = (uint8_t)c; - } - } else { // Latin1 - if (c >= 0x100) { - error = true; - *p++ = 0xBF; - } else { - *p++ = (uint8_t)c; - } - } - - *pp = (char *)p; - return error; -} - /// Return true if file encoding "fenc" requires conversion from or to /// 'encoding'. /// /// @param fenc file encoding to check /// /// @return true if conversion is required -static bool need_conversion(const char *fenc) +bool need_conversion(const char *fenc) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT { int same_encoding; @@ -4086,7 +2226,7 @@ static bool need_conversion(const char *fenc) /// use 'encoding'. /// /// @param name string to check for encoding -static int get_fio_flags(const char *name) +int get_fio_flags(const char *name) { if (*name == NUL) { name = p_enc; @@ -4166,30 +2306,6 @@ static char *check_for_bom(const char *p_in, int size, int *lenp, int flags) return name; } -/// Generate a BOM in "buf[4]" for encoding "name". -/// -/// @return the length of the BOM (zero when no BOM). -static int make_bom(char *buf_in, char *name) -{ - uint8_t *buf = (uint8_t *)buf_in; - int flags = get_fio_flags(name); - - // Can't put a BOM in a non-Unicode file. - if (flags == FIO_LATIN1 || flags == 0) { - return 0; - } - - if (flags == FIO_UTF8) { // UTF-8 - buf[0] = 0xef; - buf[1] = 0xbb; - buf[2] = 0xbf; - return 3; - } - char *p = (char *)buf; - (void)ucs2bytes(0xfeff, &p, flags); - return (int)((uint8_t *)p - buf); -} - /// Shorten filename of a buffer. /// /// @param force when true: Use full path from now on for files currently being @@ -4600,7 +2716,7 @@ int vim_rename(const char *from, const char *to) // Avoid xmalloc() here as vim_rename() is called by buf_write() when nvim // is `preserve_exit()`ing. - char *buffer = try_malloc(BUFSIZE); + char *buffer = try_malloc(WRITEBUFSIZE); if (buffer == NULL) { close(fd_out); close(fd_in); @@ -4609,7 +2725,7 @@ int vim_rename(const char *from, const char *to) } int n; - while ((n = (int)read_eintr(fd_in, buffer, BUFSIZE)) > 0) { + while ((n = (int)read_eintr(fd_in, buffer, WRITEBUFSIZE)) > 0) { if (write_eintr(fd_out, buffer, (size_t)n) != n) { errmsg = _("E208: Error writing to \"%s\""); break; diff --git a/src/nvim/fileio.h b/src/nvim/fileio.h index 3e51e361ac..a4566d754b 100644 --- a/src/nvim/fileio.h +++ b/src/nvim/fileio.h @@ -20,8 +20,29 @@ typedef varnumber_T (*CheckItem)(void *expr, const char *name); +enum { + FIO_LATIN1 = 0x01, // convert Latin1 + FIO_UTF8 = 0x02, // convert UTF-8 + FIO_UCS2 = 0x04, // convert UCS-2 + FIO_UCS4 = 0x08, // convert UCS-4 + FIO_UTF16 = 0x10, // convert UTF-16 + FIO_ENDIAN_L = 0x80, // little endian + FIO_NOCONVERT = 0x2000, // skip encoding conversion + FIO_UCSBOM = 0x4000, // check for BOM at start of file + FIO_ALL = -1, // allow all formats +}; + +// When converting, a read() or write() may leave some bytes to be converted +// for the next call. The value is guessed... +#define CONV_RESTLEN 30 + +#define WRITEBUFSIZE 8192 // size of normal write buffer + +// We have to guess how much a sequence of bytes may expand when converting +// with iconv() to be able to allocate a buffer. +#define ICONV_MULT 8 + #ifdef INCLUDE_GENERATED_DECLARATIONS -// Events for autocommands # include "fileio.h.generated.h" #endif #endif // NVIM_FILEIO_H diff --git a/src/nvim/main.c b/src/nvim/main.c index 698c2dcc4f..6ff26b6e96 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -466,7 +466,7 @@ int main(int argc, char **argv) // Recovery mode without a file name: List swap files. // Uses the 'dir' option, therefore it must be after the initializations. if (recoverymode && fname == NULL) { - recover_names(NULL, true, 0, NULL); + recover_names(NULL, true, NULL, 0, NULL); os_exit(0); } diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 0c38f18739..2528b5c0d3 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -762,7 +762,7 @@ void ml_recover(bool checkext) directly = false; // count the number of matching swap files - len = recover_names(fname, false, 0, NULL); + len = recover_names(fname, false, NULL, 0, NULL); if (len == 0) { // no swap files found semsg(_("E305: No swap file found for %s"), fname); goto theend; @@ -772,7 +772,7 @@ void ml_recover(bool checkext) i = 1; } else { // several swap files found, choose // list the names of the swap files - (void)recover_names(fname, true, 0, NULL); + (void)recover_names(fname, true, NULL, 0, NULL); msg_putchar('\n'); msg_puts(_("Enter number of swap file to use (0 to quit): ")); i = get_number(false, NULL); @@ -781,7 +781,7 @@ void ml_recover(bool checkext) } } // get the swap file name that will be used - (void)recover_names(fname, false, i, &fname_used); + (void)recover_names(fname, false, NULL, i, &fname_used); } if (fname_used == NULL) { goto theend; // user chose invalid number. @@ -1200,13 +1200,15 @@ theend: /// - list the swap files for "vim -r" /// - count the number of swap files when recovering /// - list the swap files when recovering +/// - list the swap files for swapfilelist() /// - find the name of the n'th swap file when recovering /// /// @param fname base for swap file name -/// @param list when true, list the swap file names +/// @param do_list when true, list the swap file names +/// @param ret_list when not NULL add file names to it /// @param nr when non-zero, return nr'th swap file name /// @param fname_out result when "nr" > 0 -int recover_names(char *fname, int list, int nr, char **fname_out) +int recover_names(char *fname, bool do_list, list_T *ret_list, int nr, char **fname_out) { int num_names; char *(names[6]); @@ -1230,7 +1232,7 @@ int recover_names(char *fname, int list, int nr, char **fname_out) fname_res = fname; } - if (list) { + if (do_list) { // use msg() to start the scrolling properly msg(_("Swap files found:")); msg_putchar('\n'); @@ -1306,9 +1308,11 @@ int recover_names(char *fname, int list, int nr, char **fname_out) } } - // remove swapfile name of the current buffer, it must be ignored + // Remove swapfile name of the current buffer, it must be ignored. + // But keep it for swapfilelist(). if (curbuf->b_ml.ml_mfp != NULL - && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL) { + && (p = curbuf->b_ml.ml_mfp->mf_fname) != NULL + && ret_list == NULL) { for (int i = 0; i < num_files; i++) { // Do not expand wildcards, on Windows would try to expand // "%tmp%" in "%tmp%file" @@ -1333,7 +1337,7 @@ int recover_names(char *fname, int list, int nr, char **fname_out) *fname_out = xstrdup(files[nr - 1 + num_files - file_count]); dirp = ""; // stop searching } - } else if (list) { + } else if (do_list) { if (dir_name[0] == '.' && dir_name[1] == NUL) { if (fname == NULL) { msg_puts(_(" In current directory:\n")); @@ -1359,6 +1363,13 @@ int recover_names(char *fname, int list, int nr, char **fname_out) msg_puts(_(" -- none --\n")); } ui_flush(); + } else if (ret_list != NULL) { + for (int i = 0; i < num_files; i++) { + char *name = concat_fnames(dir_name, files[i], true); + if (name != NULL) { + tv_list_append_allocated_string(ret_list, name); + } + } } else { file_count += num_files; } diff --git a/test/functional/legacy/088_conceal_tabs_spec.lua b/test/functional/legacy/088_conceal_tabs_spec.lua deleted file mode 100644 index a4c7e26583..0000000000 --- a/test/functional/legacy/088_conceal_tabs_spec.lua +++ /dev/null @@ -1,97 +0,0 @@ --- Tests for correct display (cursor column position) with +conceal and --- tabulators. - -local helpers = require('test.functional.helpers')(after_each) -local feed, insert, clear, feed_command = - helpers.feed, helpers.insert, helpers.clear, helpers.feed_command - -local expect_pos = function(row, col) - return helpers.eq({row, col}, helpers.eval('[screenrow(), screencol()]')) -end - -describe('cursor and column position with conceal and tabulators', function() - setup(clear) - - -- luacheck: ignore 621 (Indentation) - it('are working', function() - insert([[ - start: - .concealed. text - |concealed| text - - .concealed. text - |concealed| text - - .a. .b. .c. .d. - |a| |b| |c| |d|]]) - - -- Conceal settings. - feed_command('set conceallevel=2') - feed_command('set concealcursor=nc') - feed_command('syntax match test /|/ conceal') - -- Start test. - feed_command('/^start:') - feed('ztj') - expect_pos(2, 1) - -- We should end up in the same column when running these commands on the - -- two lines. - feed('ft') - expect_pos(2, 17) - feed('$') - expect_pos(2, 20) - feed('0j') - expect_pos(3, 1) - feed('ft') - expect_pos(3, 17) - feed('$') - expect_pos(3, 20) - feed('j0j') - expect_pos(5, 8) - -- Same for next test block. - feed('ft') - expect_pos(5, 25) - feed('$') - expect_pos(5, 28) - feed('0j') - expect_pos(6, 8) - feed('ft') - expect_pos(6, 25) - feed('$') - expect_pos(6, 28) - feed('0j0j') - expect_pos(8, 1) - -- And check W with multiple tabs and conceals in a line. - feed('W') - expect_pos(8, 9) - feed('W') - expect_pos(8, 17) - feed('W') - expect_pos(8, 25) - feed('$') - expect_pos(8, 27) - feed('0j') - expect_pos(9, 1) - feed('W') - expect_pos(9, 9) - feed('W') - expect_pos(9, 17) - feed('W') - expect_pos(9, 25) - feed('$') - expect_pos(9, 26) - feed_command('set lbr') - feed('$') - expect_pos(9, 26) - feed_command('set list listchars=tab:>-') - feed('0') - expect_pos(9, 1) - feed('W') - expect_pos(9, 9) - feed('W') - expect_pos(9, 17) - feed('W') - expect_pos(9, 25) - feed('$') - expect_pos(9, 26) - end) -end) diff --git a/test/functional/legacy/conceal_spec.lua b/test/functional/legacy/conceal_spec.lua new file mode 100644 index 0000000000..429cf9dc03 --- /dev/null +++ b/test/functional/legacy/conceal_spec.lua @@ -0,0 +1,554 @@ +local helpers = require('test.functional.helpers')(after_each) +local Screen = require('test.functional.ui.screen') +local clear = helpers.clear +local command = helpers.command +local exec = helpers.exec +local feed = helpers.feed + +local expect_pos = function(row, col) + return helpers.eq({row, col}, helpers.eval('[screenrow(), screencol()]')) +end + +describe('Conceal', function() + before_each(function() + clear() + command('set nohlsearch') + end) + + -- oldtest: Test_conceal_two_windows() + it('works', function() + local screen = Screen.new(75, 12) + screen:set_default_attr_ids({ + [0] = {bold = true, foreground = Screen.colors.Blue}, -- NonText + [1] = {bold = true, reverse = true}, -- StatusLine + [2] = {reverse = true}, -- StatusLineNC, IncSearch + [3] = {bold = true}, -- ModeMsg + }) + screen:attach() + exec([[ + let lines = ["one one one one one", "two |hidden| here", "three |hidden| three"] + call setline(1, lines) + syntax match test /|hidden|/ conceal + set conceallevel=2 + set concealcursor= + exe "normal /here\r" + new + call setline(1, lines) + call setline(4, "Second window") + syntax match test /|hidden|/ conceal + set conceallevel=2 + set concealcursor=nc + exe "normal /here\r" + ]]) + + -- Check that cursor line is concealed + screen:expect([[ + one one one one one | + two ^here | + three three | + Second window | + {0:~ }| + {1:[No Name] [+] }| + one one one one one | + two here | + three three | + {0:~ }| + {2:[No Name] [+] }| + /here | + ]]) + + -- Check that with concealed text vertical cursor movement is correct. + feed('k') + screen:expect([[ + one one one o^ne one | + two here | + three three | + Second window | + {0:~ }| + {1:[No Name] [+] }| + one one one one one | + two here | + three three | + {0:~ }| + {2:[No Name] [+] }| + /here | + ]]) + + -- Check that with cursor line is not concealed + feed('j') + command('set concealcursor=') + screen:expect([[ + one one one one one | + two |hidden| ^here | + three three | + Second window | + {0:~ }| + {1:[No Name] [+] }| + one one one one one | + two here | + three three | + {0:~ }| + {2:[No Name] [+] }| + /here | + ]]) + + -- Check that with cursor line is not concealed when moving cursor down + feed('j') + screen:expect([[ + one one one one one | + two here | + three |hidden^| three | + Second window | + {0:~ }| + {1:[No Name] [+] }| + one one one one one | + two here | + three three | + {0:~ }| + {2:[No Name] [+] }| + /here | + ]]) + + -- Check that with cursor line is not concealed when switching windows + feed('<C-W><C-W>') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| ^here | + three three | + {0:~ }| + {1:[No Name] [+] }| + /here | + ]]) + + -- Check that with cursor line is only concealed in Normal mode + command('set concealcursor=n') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two ^here | + three three | + {0:~ }| + {1:[No Name] [+] }| + /here | + ]]) + feed('a') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| h^ere | + three three | + {0:~ }| + {1:[No Name] [+] }| + {3:-- INSERT --} | + ]]) + feed('<Esc>/e') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| h{2:e}re | + three three | + {0:~ }| + {1:[No Name] [+] }| + /e^ | + ]]) + feed('<Esc>v') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| ^here | + three three | + {0:~ }| + {1:[No Name] [+] }| + {3:-- VISUAL --} | + ]]) + feed('<Esc>') + + -- Check that with cursor line is only concealed in Insert mode + command('set concealcursor=i') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| ^here | + three three | + {0:~ }| + {1:[No Name] [+] }| + | + ]]) + feed('a') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two h^ere | + three three | + {0:~ }| + {1:[No Name] [+] }| + {3:-- INSERT --} | + ]]) + feed('<Esc>/e') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| h{2:e}re | + three three | + {0:~ }| + {1:[No Name] [+] }| + /e^ | + ]]) + feed('<Esc>v') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| ^here | + three three | + {0:~ }| + {1:[No Name] [+] }| + {3:-- VISUAL --} | + ]]) + feed('<Esc>') + + -- Check that with cursor line is only concealed in Visual mode + command('set concealcursor=v') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| ^here | + three three | + {0:~ }| + {1:[No Name] [+] }| + | + ]]) + feed('a') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| h^ere | + three three | + {0:~ }| + {1:[No Name] [+] }| + {3:-- INSERT --} | + ]]) + feed('<Esc>/e') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| h{2:e}re | + three three | + {0:~ }| + {1:[No Name] [+] }| + /e^ | + ]]) + feed('<Esc>v') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two ^here | + three three | + {0:~ }| + {1:[No Name] [+] }| + {3:-- VISUAL --} | + ]]) + feed('<Esc>') + + -- Check moving the cursor while in insert mode. + command('set concealcursor=') + feed('a') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two |hidden| h^ere | + three three | + {0:~ }| + {1:[No Name] [+] }| + {3:-- INSERT --} | + ]]) + feed('<Down>') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two here | + three |hidden|^ three | + {0:~ }| + {1:[No Name] [+] }| + {3:-- INSERT --} | + ]]) + feed('<Esc>') + + -- Check the "o" command + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two here | + three |hidden^| three | + {0:~ }| + {1:[No Name] [+] }| + | + ]]) + feed('o') + screen:expect([[ + one one one one one | + two here | + three three | + Second window | + {0:~ }| + {2:[No Name] [+] }| + one one one one one | + two here | + three three | + ^ | + {1:[No Name] [+] }| + {3:-- INSERT --} | + ]]) + feed('<Esc>') + end) + + -- oldtest: Test_conceal_with_cursorcolumn() + it('CursorColumn and ColorColumn on wrapped line', function() + local screen = Screen.new(40, 10) + screen:set_default_attr_ids({ + [0] = {bold = true, foreground = Screen.colors.Blue}, -- NonText + [1] = {background = Screen.colors.Grey90}, -- CursorColumn + [2] = {background = Screen.colors.LightRed}, -- ColorColumn + }) + screen:attach() + -- Check that cursorcolumn and colorcolumn don't get broken in presence of + -- wrapped lines containing concealed text + -- luacheck: push ignore 613 (trailing whitespace in a string) + exec([[ + let lines = ["one one one |hidden| one one one one one one one one", + \ "two two two two |hidden| here two two", + \ "three |hidden| three three three three three three three three"] + call setline(1, lines) + set wrap linebreak + set showbreak=\ >>>\ + syntax match test /|hidden|/ conceal + set conceallevel=2 + set concealcursor= + exe "normal /here\r" + set cursorcolumn + set colorcolumn=50 + ]]) + -- luacheck: pop + + screen:expect([[ + one one one one one one {1:o}ne | + {0: >>> }one {2:o}ne one one | + two two two two |hidden| ^here two two | + three three three three {1:t}hree | + {0: >>> }thre{2:e} three three three | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + /here | + ]]) + + -- move cursor to the end of line (the cursor jumps to the next screen line) + feed('$') + screen:expect([[ + one one one one one one one | + {0: >>> }one {2:o}ne one one | + two two two two |hidden| here two tw^o | + three three three three three | + {0: >>> }thre{2:e} three three three | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + /here | + ]]) + end) + + -- oldtest: Test_conceal_resize_term() + it('resize editor', function() + local screen = Screen.new(75, 6) + screen:set_default_attr_ids({ + [0] = {bold = true, foreground = Screen.colors.Blue}, -- NonText + [1] = {foreground = Screen.colors.Blue}, -- Comment + }) + screen:attach() + exec([[ + call setline(1, '`one` `two` `three` `four` `five`, the backticks should be concealed') + setl cocu=n cole=3 + syn region CommentCodeSpan matchgroup=Comment start=/`/ end=/`/ concealends + normal fb + ]]) + screen:expect([[ + one two three four five, the ^backticks should be concealed | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]) + + screen:try_resize(75, 7) + screen:expect([[ + one two three four five, the ^backticks should be concealed | + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + {0:~ }| + | + ]]) + end) + + -- Tests for correct display (cursor column position) with +conceal and tabulators. + -- oldtest: Test_conceal_cursor_pos() + it('cursor and column position with conceal and tabulators', function() + exec([[ + let l = ['start:', '.concealed. text', "|concealed|\ttext"] + let l += ['', "\t.concealed.\ttext", "\t|concealed|\ttext", ''] + let l += [".a.\t.b.\t.c.\t.d.", "|a|\t|b|\t|c|\t|d|"] + call append(0, l) + call cursor(1, 1) + " Conceal settings. + set conceallevel=2 + set concealcursor=nc + syntax match test /|/ conceal + ]]) + feed('ztj') + expect_pos(2, 1) + -- We should end up in the same column when running these commands on the + -- two lines. + feed('ft') + expect_pos(2, 17) + feed('$') + expect_pos(2, 20) + feed('0j') + expect_pos(3, 1) + feed('ft') + expect_pos(3, 17) + feed('$') + expect_pos(3, 20) + feed('j0j') + expect_pos(5, 8) + -- Same for next test block. + feed('ft') + expect_pos(5, 25) + feed('$') + expect_pos(5, 28) + feed('0j') + expect_pos(6, 8) + feed('ft') + expect_pos(6, 25) + feed('$') + expect_pos(6, 28) + feed('0j0j') + expect_pos(8, 1) + -- And check W with multiple tabs and conceals in a line. + feed('W') + expect_pos(8, 9) + feed('W') + expect_pos(8, 17) + feed('W') + expect_pos(8, 25) + feed('$') + expect_pos(8, 27) + feed('0j') + expect_pos(9, 1) + feed('W') + expect_pos(9, 9) + feed('W') + expect_pos(9, 17) + feed('W') + expect_pos(9, 25) + feed('$') + expect_pos(9, 26) + command('set lbr') + feed('$') + expect_pos(9, 26) + command('set list listchars=tab:>-') + feed('0') + expect_pos(9, 1) + feed('W') + expect_pos(9, 9) + feed('W') + expect_pos(9, 17) + feed('W') + expect_pos(9, 25) + feed('$') + expect_pos(9, 26) + end) +end) diff --git a/test/functional/terminal/channel_spec.lua b/test/functional/terminal/channel_spec.lua index 2ca7cdb0a2..2cd02be321 100644 --- a/test/functional/terminal/channel_spec.lua +++ b/test/functional/terminal/channel_spec.lua @@ -95,15 +95,19 @@ describe('terminal channel is closed and later released if', function() end) it('chansend sends lines to terminal channel in proper order', function() - clear() + clear({args = {'--cmd', 'set laststatus=2'}}) local screen = Screen.new(100, 20) screen:attach() local shells = is_os('win') and {'cmd.exe', 'pwsh.exe -nop', 'powershell.exe -nop'} or {'sh'} for _, sh in ipairs(shells) do - command([[bdelete! | let id = termopen(']] .. sh .. [[')]]) + command([[let id = termopen(']] .. sh .. [[')]]) command([[call chansend(id, ['echo "hello"', 'echo "world"', ''])]]) screen:expect{ any=[[echo "hello".*echo "world"]] } + command('bdelete!') + screen:expect{ + any='%[No Name%]' + } end end) diff --git a/test/old/testdir/Makefile b/test/old/testdir/Makefile index 9511b311e6..b5ca6a17b0 100644 --- a/test/old/testdir/Makefile +++ b/test/old/testdir/Makefile @@ -70,6 +70,7 @@ report: then echo TEST FAILURE; exit 1; \ else echo ALL DONE; \ fi" + @rm -f starttime test1.out: $(NVIM_PRG) @@ -86,10 +87,13 @@ fixff: -$(NVIM_PRG) $(NO_INITS) -u unix.vim "+argdo set ff=dos|upd" +q \ dotest.in +# File to delete when testing starts +CLEANUP_FILES = test.log messages starttime + # Execute an individual new style test, e.g.: # make test_largefile $(NEW_TESTS): - rm -f $@.res test.log messages + rm -f $@.res $(CLEANUP_FILES) @MAKEFLAGS=--no-print-directory $(MAKE) -f Makefile $@.res @cat messages @if test -f test.log; then \ @@ -108,9 +112,8 @@ CLEAN_FILES := *.out \ *.rej \ *.orig \ *.tlog \ - test.log \ test_result.log \ - messages \ + $(CLEANUP_FILES) \ $(RM_ON_RUN) \ $(RM_ON_START) \ valgrind.* \ @@ -132,7 +135,7 @@ test1.out: .gdbinit test1.in nolog: @echo "[OLDTEST-PREP] Removing test.log and messages" - @rm -f test.log messages + @rm -f test_result.log $(CLEANUP_FILES) # New style of tests uses Vim script with assert calls. These are easier diff --git a/test/old/testdir/runtest.vim b/test/old/testdir/runtest.vim index 3c5699af73..5ee9c9fa71 100644 --- a/test/old/testdir/runtest.vim +++ b/test/old/testdir/runtest.vim @@ -61,7 +61,16 @@ if &lines < 24 || &columns < 80 endif if has('reltime') - let s:start_time = reltime() + let s:run_start_time = reltime() + + if !filereadable('starttime') + " first test, store the overall test starting time + let s:test_start_time = localtime() + call writefile([string(s:test_start_time)], 'starttime') + else + " second or later test, read the overall test starting time + let s:test_start_time = readfile('starttime')[0]->str2nr() + endif endif " Always use forward slashes. @@ -121,6 +130,7 @@ if has('mac') let $BASH_SILENCE_DEPRECATION_WARNING = 1 endif + " Prepare for calling test_garbagecollect_now(). let v:testing = 1 @@ -139,10 +149,57 @@ func GetAllocId(name) return lnum - top - 1 endfunc +if has('reltime') + let g:func_start = reltime() +endif + +" Get the list of swap files in the current directory. +func s:GetSwapFileList() + let save_dir = &directory + let &directory = '.' + let files = swapfilelist() + let &directory = save_dir + + " remove a match with runtest.vim + let idx = indexof(files, 'v:val =~ "runtest.vim."') + if idx >= 0 + call remove(files, idx) + endif + + return files +endfunc + +" A previous (failed) test run may have left swap files behind. Delete them +" before running tests again, they might interfere. +for name in s:GetSwapFileList() + call delete(name) +endfor +unlet name + + +" Invoked when a test takes too much time. +func TestTimeout(id) + split test.log + call append(line('$'), '') + call append(line('$'), 'Test timed out: ' .. g:testfunc) + write + call add(v:errors, 'Test timed out: ' . g:testfunc) + + cquit! 42 +endfunc + func RunTheTest(test) - echo 'Executing ' . a:test + let prefix = '' if has('reltime') - let func_start = reltime() + let prefix = strftime('%M:%S', localtime() - s:test_start_time) .. ' ' + let g:func_start = reltime() + endif + echo prefix .. 'Executing ' .. a:test + + if has('timers') + " No test should take longer than 30 seconds. If it takes longer we + " assume we are stuck and need to break out. + let test_timeout_timer = timer_start(30000, 'TestTimeout') endif " Avoid stopping at the "hit enter" prompt @@ -193,6 +250,11 @@ func RunTheTest(test) endif au! VimLeavePre + if a:test =~ '_terminal_' + " Terminal tests sometimes hang, give extra information + echoconsole 'After executing ' .. a:test + endif + " In case 'insertmode' was set and something went wrong, make sure it is " reset to avoid trouble with anything else. set noinsertmode @@ -205,6 +267,10 @@ func RunTheTest(test) endtry endif + if has('timers') + call timer_stop(test_timeout_timer) + endif + " Clear any autocommands and put back the catch-all for SwapExists. au! au SwapExists * call HandleSwapExists() @@ -234,20 +300,54 @@ func RunTheTest(test) exe 'cd ' . save_cwd + if a:test =~ '_terminal_' + " Terminal tests sometimes hang, give extra information + echoconsole 'Finished ' . a:test + endif + let message = 'Executed ' . a:test if has('reltime') let message ..= repeat(' ', 50 - len(message)) - let time = reltime(func_start) - if has('float') && reltimefloat(time) > 0.1 + let time = reltime(g:func_start) + if reltimefloat(time) > 0.1 let message = s:t_bold .. message endif let message ..= ' in ' .. reltimestr(time) .. ' seconds' - if has('float') && reltimefloat(time) > 0.1 + if reltimefloat(time) > 0.1 let message ..= s:t_normal endif endif call add(s:messages, message) let s:done += 1 + + " close any split windows + while winnr('$') > 1 + bwipe! + endwhile + + " May be editing some buffer, wipe it out. Then we may end up in another + " buffer, continue until we end up in an empty no-name buffer without a swap + " file. + while bufname() != '' || execute('swapname') !~ 'No swap file' + let bn = bufnr() + + noswapfile bwipe! + + if bn == bufnr() + " avoid getting stuck in the same buffer + break + endif + endwhile + + " Check if the test has left any swap files behind. Delete them before + " running tests again, they might interfere. + let swapfiles = s:GetSwapFileList() + if len(swapfiles) > 0 + call add(s:messages, "Found swap files: " .. string(swapfiles)) + for name in swapfiles + call delete(name) + endfor + endif endfunc func AfterTheTest(func_name) @@ -314,7 +414,7 @@ func FinishTesting() endif if s:done > 0 && has('reltime') let message = s:t_bold .. message .. repeat(' ', 40 - len(message)) - let message ..= ' in ' .. reltimestr(reltime(s:start_time)) .. ' seconds' + let message ..= ' in ' .. reltimestr(reltime(s:run_start_time)) .. ' seconds' let message ..= s:t_normal endif echo message @@ -434,6 +534,7 @@ for g:testfunc in sort(s:tests) " A test can set g:test_is_flaky to retry running the test. let g:test_is_flaky = 0 + let starttime = strftime("%H:%M:%S") call RunTheTest(g:testfunc) " Repeat a flaky test. Give up when: @@ -445,10 +546,11 @@ for g:testfunc in sort(s:tests) \ && (index(s:flaky_tests, g:testfunc) >= 0 \ || g:test_is_flaky) while 1 - call add(s:messages, 'Found errors in ' . g:testfunc . ':') + call add(s:messages, 'Found errors in ' .. g:testfunc .. ':') call extend(s:messages, v:errors) - call add(total_errors, 'Run ' . g:run_nr . ':') + let endtime = strftime("%H:%M:%S") + call add(total_errors, $'Run {g:run_nr}, {starttime} - {endtime}:') call extend(total_errors, v:errors) if g:run_nr >= 5 || prev_error == v:errors[0] @@ -468,6 +570,7 @@ for g:testfunc in sort(s:tests) let v:errors = [] let g:run_nr += 1 + let starttime = strftime("%H:%M:%S") call RunTheTest(g:testfunc) if len(v:errors) == 0 diff --git a/test/old/testdir/shared.vim b/test/old/testdir/shared.vim index 33f6d9a2d0..35ff434d40 100644 --- a/test/old/testdir/shared.vim +++ b/test/old/testdir/shared.vim @@ -110,16 +110,16 @@ func RunServer(cmd, testfunc, args) try let g:currentJob = RunCommand(pycmd) - " Wait for up to 2 seconds for the port number to be there. + " Wait for some time for the port number to be there. let port = GetPort() if port == 0 - call assert_false(1, "Can't start " . a:cmd) + call assert_report(strftime("%H:%M:%S") .. " Can't start " .. a:cmd) return endif call call(function(a:testfunc), [port]) catch - call assert_false(1, 'Caught exception: "' . v:exception . '" in ' . v:throwpoint) + call assert_report('Caught exception: "' . v:exception . '" in ' . v:throwpoint) finally call s:kill_server(a:cmd) endtry diff --git a/test/old/testdir/test_autocmd.vim b/test/old/testdir/test_autocmd.vim index f91792e36e..decfec4763 100644 --- a/test/old/testdir/test_autocmd.vim +++ b/test/old/testdir/test_autocmd.vim @@ -3156,7 +3156,7 @@ func Test_autocmd_FileReadCmd() \ 'v:cmdarg = ++ff=mac', \ 'v:cmdarg = ++enc=utf-8'], getline(1, '$')) - close! + bwipe! augroup FileReadCmdTest au! augroup END diff --git a/test/old/testdir/test_conceal.vim b/test/old/testdir/test_conceal.vim index bffc2f49d3..e3b8f767b8 100644 --- a/test/old/testdir/test_conceal.vim +++ b/test/old/testdir/test_conceal.vim @@ -24,7 +24,7 @@ func Test_conceal_two_windows() exe "normal /here\r" [CODE] - call writefile(code, 'XTest_conceal') + call writefile(code, 'XTest_conceal', 'D') " Check that cursor line is concealed let buf = RunVimInTerminal('-S XTest_conceal', {}) call VerifyScreenDump(buf, 'Test_conceal_two_windows_01', {}) @@ -106,7 +106,6 @@ func Test_conceal_two_windows() " clean up call StopVimInTerminal(buf) - call delete('XTest_conceal') endfunc func Test_conceal_with_cursorline() @@ -123,7 +122,7 @@ func Test_conceal_with_cursorline() normal M [CODE] - call writefile(code, 'XTest_conceal_cul') + call writefile(code, 'XTest_conceal_cul', 'D') let buf = RunVimInTerminal('-S XTest_conceal_cul', {}) call VerifyScreenDump(buf, 'Test_conceal_cul_01', {}) @@ -135,7 +134,38 @@ func Test_conceal_with_cursorline() " clean up call StopVimInTerminal(buf) - call delete('XTest_conceal_cul') +endfunc + +func Test_conceal_with_cursorcolumn() + CheckScreendump + + " Check that cursorcolumn and colorcolumn don't get broken in presence of + " wrapped lines containing concealed text + let code =<< trim [CODE] + let lines = ["one one one |hidden| one one one one one one one one", + \ "two two two two |hidden| here two two", + \ "three |hidden| three three three three three three three three"] + call setline(1, lines) + set wrap linebreak + set showbreak=\ >>>\ + syntax match test /|hidden|/ conceal + set conceallevel=2 + set concealcursor= + exe "normal /here\r" + set cursorcolumn + set colorcolumn=50 + [CODE] + + call writefile(code, 'XTest_conceal_cuc', 'D') + let buf = RunVimInTerminal('-S XTest_conceal_cuc', {'rows': 10, 'cols': 40}) + call VerifyScreenDump(buf, 'Test_conceal_cuc_01', {}) + + " move cursor to the end of line (the cursor jumps to the next screen line) + call term_sendkeys(buf, "$") + call VerifyScreenDump(buf, 'Test_conceal_cuc_02', {}) + + " clean up + call StopVimInTerminal(buf) endfunc func Test_conceal_resize_term() @@ -147,17 +177,15 @@ func Test_conceal_resize_term() syn region CommentCodeSpan matchgroup=Comment start=/`/ end=/`/ concealends normal fb [CODE] - call writefile(code, 'XTest_conceal_resize') + call writefile(code, 'XTest_conceal_resize', 'D') let buf = RunVimInTerminal('-S XTest_conceal_resize', {'rows': 6}) call VerifyScreenDump(buf, 'Test_conceal_resize_01', {}) call win_execute(buf->win_findbuf()[0], 'wincmd +') - call TermWait(buf) call VerifyScreenDump(buf, 'Test_conceal_resize_02', {}) " clean up call StopVimInTerminal(buf) - call delete('XTest_conceal_resize') endfunc " Tests for correct display (cursor column position) with +conceal and @@ -245,7 +273,7 @@ func Test_conceal_cursor_pos() :q! [CODE] - call writefile(code, 'XTest_conceal_curpos') + call writefile(code, 'XTest_conceal_curpos', 'D') if RunVim([], [], '-s XTest_conceal_curpos') call assert_equal([ @@ -256,7 +284,6 @@ func Test_conceal_cursor_pos() endif call delete('Xconceal_curpos.out') - call delete('XTest_conceal_curpos') endfunc func Test_conceal_eol() diff --git a/test/old/testdir/test_filetype.vim b/test/old/testdir/test_filetype.vim index 150cf50c2a..a3665e310d 100644 --- a/test/old/testdir/test_filetype.vim +++ b/test/old/testdir/test_filetype.vim @@ -248,7 +248,7 @@ let s:filename_checks = { \ 'grads': ['file.gs'], \ 'graphql': ['file.graphql', 'file.graphqls', 'file.gql'], \ 'gretl': ['file.gretl'], - \ 'groovy': ['file.gradle', 'file.groovy'], + \ 'groovy': ['file.gradle', 'file.groovy', 'Jenkinsfile'], \ 'group': ['any/etc/group', 'any/etc/group-', 'any/etc/group.edit', 'any/etc/gshadow', 'any/etc/gshadow-', 'any/etc/gshadow.edit', 'any/var/backups/group.bak', 'any/var/backups/gshadow.bak', '/etc/group', '/etc/group-', '/etc/group.edit', '/etc/gshadow', '/etc/gshadow-', '/etc/gshadow.edit', '/var/backups/group.bak', '/var/backups/gshadow.bak'], \ 'grub': ['/boot/grub/menu.lst', '/boot/grub/grub.conf', '/etc/grub.conf', 'any/boot/grub/grub.conf', 'any/boot/grub/menu.lst', 'any/etc/grub.conf'], \ 'gsp': ['file.gsp'], @@ -733,6 +733,11 @@ func Test_filetype_detection() filetype off endfunc +" Content lines that should not result in filetype detection +let s:false_positive_checks = { + \ '': [['test execve("/usr/bin/pstree", ["pstree"], 0x7ff0 /* 63 vars */) = 0']], + \ } + " Filetypes detected from the file contents by scripts.vim let s:script_checks = { \ 'virata': [['% Virata'], @@ -824,6 +829,7 @@ func Run_script_detection(test_dict) endfunc func Test_script_detection() + call Run_script_detection(s:false_positive_checks) call Run_script_detection(s:script_checks) call Run_script_detection(s:script_env_checks) endfunc diff --git a/test/old/testdir/test_swap.vim b/test/old/testdir/test_swap.vim index 4241f4fd69..fac1893e4c 100644 --- a/test/old/testdir/test_swap.vim +++ b/test/old/testdir/test_swap.vim @@ -115,8 +115,19 @@ func Test_swapinfo() w let fname = s:swapname() call assert_match('Xswapinfo', fname) - let info = fname->swapinfo() + " Check the tail appears in the list from swapfilelist(). The path depends + " on the system. + let tail = fnamemodify(fname, ":t")->fnameescape() + let nr = 0 + for name in swapfilelist() + if name =~ tail .. '$' + let nr += 1 + endif + endfor + call assert_equal(1, nr, 'not found in ' .. string(swapfilelist())) + + let info = fname->swapinfo() let ver = printf('VIM %d.%d', v:version / 100, v:version % 100) call assert_equal(ver, info.version) diff --git a/test/old/testdir/test_syntax.vim b/test/old/testdir/test_syntax.vim index 886c23efa7..c9ad4bb857 100644 --- a/test/old/testdir/test_syntax.vim +++ b/test/old/testdir/test_syntax.vim @@ -460,7 +460,7 @@ func Test_invalid_name() endfunc func Test_ownsyntax() - new Xfoo + new XfooOwnSyntax call setline(1, '#define FOO') syntax on set filetype=c diff --git a/test/old/testdir/test_user_func.vim b/test/old/testdir/test_user_func.vim index 0f9b40814c..dc36ab98cb 100644 --- a/test/old/testdir/test_user_func.vim +++ b/test/old/testdir/test_user_func.vim @@ -609,29 +609,103 @@ func Test_defer_throw() call assert_false(filereadable('XDeleteTwo')) endfunc -func Test_defer_quitall() +func Test_defer_quitall_func() let lines =<< trim END - " vim9script func DeferLevelTwo() - call writefile(['text'], 'XQuitallTwo', 'D') + call writefile(['text'], 'XQuitallFuncTwo', 'D') + call writefile(['quit'], 'XQuitallFuncThree', 'a') qa! endfunc - " def DeferLevelOne() func DeferLevelOne() - call writefile(['text'], 'XQuitallOne', 'D') - call DeferLevelTwo() - " enddef + call writefile(['text'], 'XQuitalFunclOne', 'D') + defer DeferLevelTwo() endfunc - " DeferLevelOne() call DeferLevelOne() END - call writefile(lines, 'XdeferQuitall', 'D') - let res = system(GetVimCommand() .. ' -X -S XdeferQuitall') + call writefile(lines, 'XdeferQuitallFunc', 'D') + call system(GetVimCommand() .. ' -X -S XdeferQuitallFunc') + call assert_equal(0, v:shell_error) + call assert_false(filereadable('XQuitallFuncOne')) + call assert_false(filereadable('XQuitallFuncTwo')) + call assert_equal(['quit'], readfile('XQuitallFuncThree')) + + call delete('XQuitallFuncThree') +endfunc + +func Test_defer_quitall_def() + throw 'Skipped: Vim9 script is N/A' + let lines =<< trim END + vim9script + def DeferLevelTwo() + call writefile(['text'], 'XQuitallDefTwo', 'D') + call writefile(['quit'], 'XQuitallDefThree', 'a') + qa! + enddef + + def DeferLevelOne() + call writefile(['text'], 'XQuitallDefOne', 'D') + defer DeferLevelTwo() + enddef + + DeferLevelOne() + END + call writefile(lines, 'XdeferQuitallDef', 'D') + call system(GetVimCommand() .. ' -X -S XdeferQuitallDef') + call assert_equal(0, v:shell_error) + call assert_false(filereadable('XQuitallDefOne')) + call assert_false(filereadable('XQuitallDefTwo')) + call assert_equal(['quit'], readfile('XQuitallDefThree')) + + call delete('XQuitallDefThree') +endfunc + +func Test_defer_quitall_autocmd() + let lines =<< trim END + func DeferLevelFive() + defer writefile(['5'], 'XQuitallAutocmd', 'a') + qa! + endfunc + + autocmd User DeferAutocmdFive call DeferLevelFive() + + " def DeferLevelFour() + func DeferLevelFour() + defer writefile(['4'], 'XQuitallAutocmd', 'a') + doautocmd User DeferAutocmdFive + " enddef + endfunc + + func DeferLevelThree() + defer writefile(['3'], 'XQuitallAutocmd', 'a') + call DeferLevelFour() + endfunc + + autocmd User DeferAutocmdThree ++nested call DeferLevelThree() + + " def DeferLevelTwo() + func DeferLevelTwo() + defer writefile(['2'], 'XQuitallAutocmd', 'a') + doautocmd User DeferAutocmdThree + " enddef + endfunc + + func DeferLevelOne() + defer writefile(['1'], 'XQuitallAutocmd', 'a') + call DeferLevelTwo() + endfunc + + autocmd User DeferAutocmdOne ++nested call DeferLevelOne() + + doautocmd User DeferAutocmdOne + END + call writefile(lines, 'XdeferQuitallAutocmd', 'D') + call system(GetVimCommand() .. ' -X -S XdeferQuitallAutocmd') call assert_equal(0, v:shell_error) - call assert_false(filereadable('XQuitallOne')) - call assert_false(filereadable('XQuitallTwo')) + call assert_equal(['5', '4', '3', '2', '1'], readfile('XQuitallAutocmd')) + + call delete('XQuitallAutocmd') endfunc func Test_defer_quitall_in_expr_func() @@ -651,7 +725,7 @@ func Test_defer_quitall_in_expr_func() call Test_defer_in_funcref() END call writefile(lines, 'XdeferQuitallExpr', 'D') - let res = system(GetVimCommand() .. ' -X -S XdeferQuitallExpr') + call system(GetVimCommand() .. ' -X -S XdeferQuitallExpr') call assert_equal(0, v:shell_error) call assert_false(filereadable('Xentry0')) call assert_false(filereadable('Xentry1')) diff --git a/test/old/testdir/test_vimscript.vim b/test/old/testdir/test_vimscript.vim index 81ccee9f13..055a2bd2f2 100644 --- a/test/old/testdir/test_vimscript.vim +++ b/test/old/testdir/test_vimscript.vim @@ -5983,6 +5983,9 @@ endfunc " interrupt right before a catch is invoked in a script func Test_ignore_catch_after_intr_1() + " for unknown reasons this test sometimes fails on MS-Windows. + let g:test_is_flaky = 1 + XpathINIT let lines =<< trim [CODE] try @@ -6021,6 +6024,9 @@ endfunc " interrupt right before a catch is invoked inside a function. func Test_ignore_catch_after_intr_2() + " for unknown reasons this test sometimes fails on MS-Windows. + let g:test_is_flaky = 1 + XpathINIT func F() try diff --git a/test/old/testdir/test_window_cmd.vim b/test/old/testdir/test_window_cmd.vim index 34614832a9..88135199fe 100644 --- a/test/old/testdir/test_window_cmd.vim +++ b/test/old/testdir/test_window_cmd.vim @@ -119,10 +119,9 @@ endfunc " Test the ":wincmd ^" and "<C-W>^" commands. func Test_window_split_edit_alternate() - " Test for failure when the alternate buffer/file no longer exists. edit Xfoo | %bw - call assert_fails(':wincmd ^', 'E23') + call assert_fails(':wincmd ^', 'E23:') " Test for the expected behavior when we have two named buffers. edit Xfoo | edit Xbar @@ -152,12 +151,11 @@ endfunc " Test the ":[count]wincmd ^" and "[count]<C-W>^" commands. func Test_window_split_edit_bufnr() - %bwipeout let l:nr = bufnr('%') + 1 - call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92') - call assert_fails(':' . l:nr . 'wincmd ^', 'E16') - call assert_fails(':0wincmd ^', 'E16') + call assert_fails(':execute "normal! ' . l:nr . '\<C-W>\<C-^>"', 'E92:') + call assert_fails(':' . l:nr . 'wincmd ^', 'E16:') + call assert_fails(':0wincmd ^', 'E16:') edit Xfoo | edit Xbar | edit Xbaz let l:foo_nr = bufnr('Xfoo') |