From fc16d5c0e72d8ba4112d239f211cc0576640aaa3 Mon Sep 17 00:00:00 2001 From: Peter Odding Date: Tue, 10 Aug 2010 05:13:59 +0200 Subject: [PATCH] Enable passing stdin to xolox#shell#execute() This enables integration with my luainspect.vim plug-in. --- TODO.md | 2 ++ autoload.vim | 23 +++++++++++++++-------- shell.vim | 4 ++-- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/TODO.md b/TODO.md index dbe00e1..076a7e3 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,7 @@ * Test the `shell.dll` library on as many configurations as possible! (different Windows versions, 32 vs. 64 bit, etc.) + * Document how to pass standard input to shell commands using xolox#shell#execute(). + * Replace the temporary file hack with a proper silent `popen()` implementation? * I've [announced this plug-in](http://groups.google.com/group/vim_dev/browse_frm/thread/2cdeb5709fbfc0a0) on the vim-dev mailing list but received some criticism. So for future reference: The problem solved by `xolox#shell#execute()` really does exist, see for example the [Syntastic script page](http://www.vim.org/scripts/script.php?script_id=2736) which says: "This plugin is currently only recommended for UNIX users. It is functional on Windows, but since the syntax checking plugins shell out, the command window briefly appears whenever one is executed." diff --git a/autoload.vim b/autoload.vim index ec07c20..d9a2612 100644 --- a/autoload.vim +++ b/autoload.vim @@ -1,6 +1,6 @@ " Vim auto-load script " Author: Peter Odding -" Last Change: August 9, 2010 +" Last Change: August 10, 2010 " URL: http://peterodding.com/code/vim/shell/ if !exists('s:script') @@ -142,12 +142,18 @@ function! xolox#shell#highlight_urls() " -- highlight URLs and e-mail addresses endif endfunction -function! xolox#shell#execute(command, synchronous) " -- execute external commands asynchronously {{{1 +function! xolox#shell#execute(command, synchronous, ...) " -- execute external commands asynchronously {{{1 try let cmd = a:command + let has_input = a:0 > 0 + if has_input + let tempin = tempname() + call writefile(type(a:1) == type([]) ? a:1 : split(a:1, "\n"), tempin) + let cmd .= ' < ' . shellescape(tempin) + endif if a:synchronous - let tempfile = tempname() - let cmd .= ' > ' . shellescape(tempfile) . ' 2>&1' + let tempout = tempname() + let cmd .= ' > ' . shellescape(tempout) . ' 2>&1' endif if s:is_windows() && s:has_dll() let fn = 'execute_' . (a:synchronous ? '' : 'a') . 'synchronous' @@ -165,18 +171,19 @@ function! xolox#shell#execute(command, synchronous) " -- execute external comman call s:handle_error(cmd, output) endif if a:synchronous - if !filereadable(tempfile) + if !filereadable(tempout) let msg = '%s: Failed to execute %s!' throw printf(msg, s:script, strtrans(cmd)) endif - let output = readfile(tempfile) - call delete(tempfile) - return output + return readfile(tempout) else return 1 endif catch call xolox#warning("%s: %s at %s", s:script, v:exception, v:throwpoint) + finally + if exists('tempin') | call delete(tempin) | endif + if exists('tempout') | call delete(tempout) | endif endtry endfunction diff --git a/shell.vim b/shell.vim index bc83e20..baf9a27 100644 --- a/shell.vim +++ b/shell.vim @@ -1,9 +1,9 @@ " Vim plug-in " Author: Peter Odding -" Last Change: August 9, 2010 +" Last Change: August 10, 2010 " URL: http://peterodding.com/code/vim/shell/ " License: MIT -" Version: 0.7 +" Version: 0.7.1 " Support for automatic update using the GLVS plug-in. " GetLatestVimScripts: 3123 1 :AutoInstall: shell.zip