Skip to content

Commit

Permalink
Updated miscellaneous scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed May 13, 2013
2 parents a523c28 + 9c88b0e commit 09120cd
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 26 deletions.
4 changes: 2 additions & 2 deletions autoload/xolox/misc/compat.vim
@@ -1,13 +1,13 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: April 28, 2013
" Last Change: May 13, 2013
" URL: http://peterodding.com/code/vim/misc/

" The following integer will be bumped whenever a change in the miscellaneous
" scripts breaks backwards compatibility. This enables my Vim plug-ins to fail
" early when they detect an incompatible version, instead of breaking at the
" worst possible moments :-).
let g:xolox#misc#compat#version = 2
let g:xolox#misc#compat#version = 3

" Remember the directory where the miscellaneous scripts are loaded from
" so the user knows which plug-in to update if incompatibilities arise.
Expand Down
120 changes: 104 additions & 16 deletions autoload/xolox/misc/os.vim
@@ -1,29 +1,117 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: November 24, 2011
" Last Change: May 13, 2013
" URL: http://peterodding.com/code/vim/misc/

let g:xolox#misc#os#version = '0.1'
let g:xolox#misc#os#version = '0.2'

" Check whether Vim is running on Microsoft Windows.

function! xolox#misc#os#is_win()
function! xolox#misc#os#is_win() " {{{1
" Check whether Vim is running on Microsoft Windows.
return has('win16') || has('win32') || has('win64')
endfunction

" Execute an external command (hiding the console on Windows when possible).

function! xolox#misc#os#exec(cmdline, ...)
function! xolox#misc#os#exec(options) " {{{1
" Execute an external command (hiding the console on Windows when possible).
" NB: Everything below is wrapped in a try/finally block to guarantee
" cleanup of temporary files.
try
" Try using my shell.vim plug-in.
return call('xolox#shell#execute', [a:cmdline, 1] + a:000)
catch /^Vim\%((\a\+)\)\=:E117/
" Fall back to system() when we get an "unknown function" error.
let output = call('system', [a:cmdline] + a:000)
if v:shell_error
throw printf("os.vim %s: Command %s failed: %s", g:xolox#misc#os#version, a:cmdline, xolox#misc#str#trim(output))

" Unpack the options.
let cmd = a:options['command']
let async = get(a:options, 'async', 0)

" Write the input for the external command to a temporary file?
if has_key(a:options, 'stdin')
let tempin = tempname()
if type(a:options['stdin']) == type([])
let lines = a:options['stdin']
else
let lines = split(a:options['stdin'], "\n")
endif
call writefile(lines, tempin)
let cmd .= ' < ' . xolox#misc#escape#shell(tempin)
endif

" Redirect the standard output and standard error streams of the external
" process to temporary files? (only in synchronous mode, which is the
" default).
if !async
let tempout = tempname()
let temperr = tempname()
let cmd = printf('(%s) 1>%s 2>%s', cmd,
\ xolox#misc#escape#shell(tempout),
\ xolox#misc#escape#shell(temperr))
endif

" If A) we're on Windows, B) the vim-shell plug-in is installed and C) the
" compiled DLL works, we'll use that because it's the most user friendly
" method. If the plug-in is not installed Vim will raise the exception
" "E117: Unknown function" which is caught and handled below.
try
if xolox#shell#can_use_dll()
" Let the user know what's happening (in case they're interested).
call xolox#misc#msg#debug("os.vim %s: Executing external command using compiled DLL: %s", g:xolox#misc#os#version, cmd)
let exit_code = xolox#shell#execute_with_dll(cmd, async)
endif
catch /^Vim\%((\a\+)\)\=:E117/
call xolox#misc#msg#debug("os.vim %s: The vim-shell plug-in is not installed, falling back to system() function.", g:xolox#misc#os#version)
endtry

" If we cannot use the DLL, we fall back to the default and generic
" implementation using Vim's system() function.
if !exists('exit_code')

" Enable asynchronous mode (very platform specific).
if async
if xolox#misc#os#is_win()
let cmd = 'start /b ' . cmd
elseif has('unix')
let cmd = '(' . cmd . ') &'
else
call xolox#misc#msg#warn("os.vim %s: I don't know how to run commands asynchronously on your platform! Falling back to synchronous mode.", g:xolox#misc#os#version)
endif
endif

" Let the user know what's happening (in case they're interested).
call xolox#misc#msg#debug("os.vim %s: Executing external command using system() function: %s", g:xolox#misc#os#version, cmd)
call system(cmd)
let exit_code = v:shell_error

endif
return split(output, "\n")

let result = {}
if !async
" If we just executed a synchronous command and the caller didn't
" specifically ask us *not* to check the exit code of the external
" command, we'll do so now.
if get(a:options, 'check', 1) && exit_code != 0
let msg = "os.vim %s: External command failed with exit code %d: %s"
throw printf(msg, g:xolox#misc#os#version, result['exit_code'], result['command'])
endif
" Return the results as a dictionary with three key/value pairs.
let result['exit_code'] = exit_code
let result['stdout'] = s:readfile(tempout)
let result['stderr'] = s:readfile(temperr)
endif
return result

finally
" Cleanup any temporary files we created.
for name in ['tempin', 'tempout', 'temperr']
if exists(name)
call delete({name})
endif
endfor
endtry

endfunction

function! s:readfile(fname) " {{{1
" readfile() that swallows errors.
try
return readfile(a:fname)
catch
return []
endtry
endfunction

Expand Down
16 changes: 8 additions & 8 deletions autoload/xolox/notes.vim
@@ -1,15 +1,15 @@
" Vim auto-load script
" Author: Peter Odding <peter@peterodding.com>
" Last Change: May 12, 2013
" Last Change: May 13, 2013
" URL: http://peterodding.com/code/vim/notes/

" Note: This file is encoded in UTF-8 including a byte order mark so
" that Vim loads the script using the right encoding transparently.

let g:xolox#notes#version = '0.19'
let g:xolox#notes#version = '0.20'
let s:scriptdir = expand('<sfile>:p:h')

call xolox#misc#compat#check('notes', 2)
call xolox#misc#compat#check('notes', 3)

function! xolox#notes#init() " {{{1
" Initialize the configuration of the notes plug-in. This is a bit tricky:
Expand Down Expand Up @@ -809,16 +809,16 @@ function! s:python_command(...) " {{{2
if !filereadable(xolox#misc#path#absolute(g:notes_indexfile))
call xolox#misc#msg#info("notes.vim %s: Building keyword index (this might take a while) ..", g:xolox#notes#version)
endif
let output = xolox#misc#str#trim(system(command))
if v:shell_error
call xolox#misc#msg#warn("notes.vim %s: Search script failed with output: %s", g:xolox#notes#version, output)
let result = xolox#misc#os#exec({'command': command, 'check': 0})
if result['exit_code'] != 0
call xolox#misc#msg#warn("notes.vim %s: Search script failed!", g:xolox#notes#version)
else
let lines = split(output, "\n")
let lines = result['stdout']
call xolox#misc#msg#debug("notes.vim %s: Search script output: %s", g:xolox#notes#version, string(lines))
if !empty(lines) && lines[0] == 'Python works fine!'
return lines[1:]
endif
call xolox#misc#msg#debug("notes.vim %s: Search script returned invalid output :-(", g:xolox#notes#version)
call xolox#misc#msg#warn("notes.vim %s: Search script returned invalid output :-(", g:xolox#notes#version)
endif
endif
endfunction
Expand Down

0 comments on commit 09120cd

Please sign in to comment.