summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2014-11-05 18:06:01 +0100
committerBram Moolenaar <Bram@vim.org>2014-11-05 18:06:01 +0100
commit6b2e938f1307f840165d9049d743161b01af811c (patch)
tree5aa05c5723205df857bd2f14379881ef13de8d45
parent4391cf98ec3b94f33dfd053cab25ed56c787bea9 (diff)
downloadvim-git-6b2e938f1307f840165d9049d743161b01af811c.tar.gz
updated for version 7.4.503v7.4.503
Problem: Cannot append a list of lines to a file. Solution: Add the append option to writefile(). (Yasuhiro Matsumoto)
-rw-r--r--runtime/doc/eval.txt14
-rw-r--r--src/Makefile4
-rw-r--r--src/eval.c14
-rw-r--r--src/testdir/test_writefile.in18
-rw-r--r--src/testdir/test_writefile.ok5
-rw-r--r--src/version.c2
6 files changed, 49 insertions, 8 deletions
diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt
index eb7fcbcaa..4e349a6e0 100644
--- a/runtime/doc/eval.txt
+++ b/runtime/doc/eval.txt
@@ -2041,7 +2041,7 @@ winrestcmd() String returns command to restore window sizes
winrestview( {dict}) none restore view of current window
winsaveview() Dict save view of current window
winwidth( {nr}) Number width of window {nr}
-writefile( {list}, {fname} [, {binary}])
+writefile( {list}, {fname} [, {flags}])
Number write list of lines to file {fname}
xor( {expr}, {expr}) Number bitwise XOR
@@ -6555,14 +6555,20 @@ winwidth({nr}) *winwidth()*
:endif
<
*writefile()*
-writefile({list}, {fname} [, {binary}])
+writefile({list}, {fname} [, {flags}])
Write |List| {list} to file {fname}. Each list item is
separated with a NL. Each list item must be a String or
Number.
- When {binary} is equal to "b" binary mode is used: There will
+ When {flags} contains "b" then binary mode is used: There will
not be a NL after the last list item. An empty item at the
end does cause the last line in the file to end in a NL.
- All NL characters are replaced with a NUL character.
+
+ When {flags} contains "a" then append mode is used, lines are
+ append to the file: >
+ :call writefile(["foo"], "event.log", "a")
+ :call writefile(["bar"], "event.log", "a")
+>
+< All NL characters are replaced with a NUL character.
Inserting CR characters needs to be done before passing {list}
to writefile().
An existing file is overwritten, if possible.
diff --git a/src/Makefile b/src/Makefile
index cfdca44fc..39e2de99f 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1899,8 +1899,12 @@ test1 test2 test3 test4 test5 test6 test7 test8 test9 \
test_insertcount \
test_listlbr \
test_listlbr_utf8 \
+ test_mapping \
test_options \
test_qf_title \
+ test_signs \
+ test_utf8 \
+ test_writefile \
test10 test11 test12 test13 test14 test15 test16 test17 test18 test19 \
test20 test21 test22 test23 test24 test25 test26 test27 test28 test29 \
test30 test31 test32 test33 test34 test35 test36 test37 test38 test39 \
diff --git a/src/eval.c b/src/eval.c
index ecc5e3a57..ba456f2bc 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -19689,6 +19689,7 @@ f_writefile(argvars, rettv)
typval_T *rettv;
{
int binary = FALSE;
+ int append = FALSE;
char_u *fname;
FILE *fd;
int ret = 0;
@@ -19704,14 +19705,19 @@ f_writefile(argvars, rettv)
if (argvars[0].vval.v_list == NULL)
return;
- if (argvars[2].v_type != VAR_UNKNOWN
- && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
- binary = TRUE;
+ if (argvars[2].v_type != VAR_UNKNOWN)
+ {
+ if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
+ binary = TRUE;
+ if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
+ append = TRUE;
+ }
/* Always open the file in binary mode, library functions have a mind of
* their own about CR-LF conversion. */
fname = get_tv_string(&argvars[1]);
- if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
+ if (*fname == NUL || (fd = mch_fopen((char *)fname,
+ append ? APPENDBIN : WRITEBIN)) == NULL)
{
EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
ret = -1;
diff --git a/src/testdir/test_writefile.in b/src/testdir/test_writefile.in
new file mode 100644
index 000000000..f2dc7d50b
--- /dev/null
+++ b/src/testdir/test_writefile.in
@@ -0,0 +1,18 @@
+Tests for writefile()
+
+STARTTEST
+:source small.vim
+:%delete _
+:let f = tempname()
+:call writefile(["over","written"], f, "b")
+:call writefile(["hello","world"], f, "b")
+:call writefile(["!", "good"], f, "a")
+:call writefile(["morning"], f, "ab")
+:call writefile(["", "vimmers"], f, "ab")
+:bwipeout!
+:$put =readfile(f)
+:1 delete _
+:w! test.out
+:qa!
+ENDTEST
+
diff --git a/src/testdir/test_writefile.ok b/src/testdir/test_writefile.ok
new file mode 100644
index 000000000..dfb6a2b29
--- /dev/null
+++ b/src/testdir/test_writefile.ok
@@ -0,0 +1,5 @@
+hello
+world!
+good
+morning
+vimmers
diff --git a/src/version.c b/src/version.c
index 7bc7153f7..f9c0ece55 100644
--- a/src/version.c
+++ b/src/version.c
@@ -742,6 +742,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 503,
+/**/
502,
/**/
501,