]> granicus.if.org Git - vim/commitdiff
patch 8.0.0151: passing buffer content to system() is clumsy v8.0.0151
authorBram Moolenaar <Bram@vim.org>
Sun, 8 Jan 2017 12:26:03 +0000 (13:26 +0100)
committerBram Moolenaar <Bram@vim.org>
Sun, 8 Jan 2017 12:26:03 +0000 (13:26 +0100)
Problem:    To pass buffer content to system() and systemlist() one has to
            first create a string or list.
Solution:   Allow passing a buffer number. (LemonBoy, closes #1240)

runtime/doc/eval.txt
src/Makefile
src/evalfunc.c
src/testdir/Make_all.mak
src/testdir/test_system.vim [new file with mode: 0644]
src/version.c

index 1f54372e97d095d2bf5b51968f50b1ccf094f83a..f5ea0780a9038b735b29089701d193b297e99fb7 100644 (file)
@@ -7561,7 +7561,11 @@ system({expr} [, {input}])                               *system()* *E677*
                If {input} is given and is a |List| it is written to the file
                in a way |writefile()| does with {binary} set to "b" (i.e.
                with a newline between each list item with newlines inside
-               list items converted to NULs).  
+               list items converted to NULs).
+               When {input} is given and is a number that is a valid id for
+               an existing buffer then the content of the buffer is written
+               to the file line by line, each line terminated by a NL and
+               NULs characters where the text has a NL.
 
                Pipes are not used, the 'shelltemp' option is not used.
 
index 4b29ae118fc6f6d153b314b3579195cb659e7fe8..3f61de652642046c06acea727309c4d9b237c6ea 100644 (file)
@@ -2164,6 +2164,7 @@ test_arglist \
        test_substitute \
        test_syn_attr \
        test_syntax \
+       test_system \
        test_tabline \
        test_tabpage \
        test_tagcase \
index 10f6b182e7f18d45c3e8582f6231c0348611af73..f66fa9a8e1b0070e10ea3c2143063d02924d8735 100644 (file)
@@ -11817,7 +11817,6 @@ get_cmd_output_as_rettv(
     char_u     *res = NULL;
     char_u     *p;
     char_u     *infile = NULL;
-    char_u     buf[NUMBUFLEN];
     int                err = FALSE;
     FILE       *fd;
     list_T     *list = NULL;
@@ -11831,7 +11830,7 @@ get_cmd_output_as_rettv(
     if (argvars[1].v_type != VAR_UNKNOWN)
     {
        /*
-        * Write the string to a temp file, to be used for input of the shell
+        * Write the text to a temp file, to be used for input of the shell
         * command.
         */
        if ((infile = vim_tempname('i', TRUE)) == NULL)
@@ -11846,14 +11845,42 @@ get_cmd_output_as_rettv(
            EMSG2(_(e_notopen), infile);
            goto errret;
        }
-       if (argvars[1].v_type == VAR_LIST)
+       if (argvars[1].v_type == VAR_NUMBER)
+       {
+           linenr_T    lnum;
+           buf_T       *buf;
+
+           buf = buflist_findnr(argvars[1].vval.v_number);
+           if (buf == NULL)
+           {
+               EMSGN(_(e_nobufnr), argvars[1].vval.v_number);
+               goto errret;
+           }
+
+           for (lnum = 1; lnum <= buf->b_ml.ml_line_count; lnum++)
+           {
+               for (p = ml_get_buf(buf, lnum, FALSE); *p != NUL; ++p)
+                   if (putc(*p == '\n' ? NUL : *p, fd) == EOF)
+                   {
+                       err = TRUE;
+                       break;
+                   }
+               if (putc(NL, fd) == EOF)
+               {
+                   err = TRUE;
+                   break;
+               }
+           }
+       }
+       else if (argvars[1].v_type == VAR_LIST)
        {
            if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
                err = TRUE;
        }
        else
        {
-           size_t len;
+           size_t      len;
+           char_u      buf[NUMBUFLEN];
 
            p = get_tv_string_buf_chk(&argvars[1], buf);
            if (p == NULL)
index 072c61c7968a5f4759edd5c4469df9eca039c2c3..63a1b051f0fa02bad008a8816d05a4ec4985e047 100644 (file)
@@ -184,6 +184,7 @@ NEW_TESTS = test_arglist.res \
            test_stat.res \
            test_substitute.res \
            test_syntax.res \
+           test_system.res \
            test_textobjects.res \
            test_undo.res \
            test_usercommands.res \
diff --git a/src/testdir/test_system.vim b/src/testdir/test_system.vim
new file mode 100644 (file)
index 0000000..5e8d68b
--- /dev/null
@@ -0,0 +1,20 @@
+" Tests for system() and systemlist()
+
+function! Test_System()
+  if !executable('echo') || !executable('cat') || !executable('wc')
+    return
+  endif
+  call assert_equal("123\n", system('echo 123'))
+  call assert_equal(['123'], systemlist('echo 123'))
+  call assert_equal('123',   system('cat', '123'))
+  call assert_equal(['123'], systemlist('cat', '123'))
+  call assert_equal(["as\<NL>df"], systemlist('cat', ["as\<NL>df"]))
+  new Xdummy
+  call setline(1, ['asdf', "pw\<NL>er", 'xxxx'])
+  call assert_equal("3\n",  system('wc -l', bufnr('%')))
+  call assert_equal(['3'],  systemlist('wc -l', bufnr('%')))
+  call assert_equal(['asdf', "pw\<NL>er", 'xxxx'],  systemlist('cat', bufnr('%')))
+  bwipe!
+
+  call assert_fails('call system("wc -l", 99999)', 'E86:')
+endfunction
index 0c16c28977d7a979575df01c38c994d50563ba33..3be6cbbfc0deb62086d940c7830ca2684aad962a 100644 (file)
@@ -764,6 +764,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    151,
 /**/
     150,
 /**/