Skip to content

Commit

Permalink
Use shell.vim plug-in to execute Lua subprocesses (issue #2)
Browse files Browse the repository at this point in the history
  • Loading branch information
xolox committed Nov 25, 2011
1 parent 0814a1b commit bccf110
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 52 deletions.
24 changes: 14 additions & 10 deletions README.md
Expand Up @@ -24,6 +24,8 @@ The [Lua][lua] file type plug-in for [Vim][vim] makes it easier to work with Lua

Unzip the most recent [ZIP archive][zip] file inside your Vim profile directory (usually this is `~/.vim` on UNIX and `%USERPROFILE%\vimfiles` on Windows), restart Vim and execute the command `:helptags ~/.vim/doc` (use `:helptags ~\vimfiles\doc` instead on Windows). Now try it out: Edit a Lua script and try any of the features documented above.

Note that on Windows a command prompt window pops up whenever Lua is run as an external process. If this bothers you then you can install my [shell.vim][shell] plug-in which includes a [DLL][dll] that works around this issue. Once you've installed both plug-ins it should work out of the box!

## Options

The Lua file type plug-in handles options as follows: First it looks at buffer local variables, then it looks at global variables and if neither exists a default is chosen. This means you can change how the plug-in works for individual buffers. For example to change the location of the Lua compiler used to check the syntax:
Expand Down Expand Up @@ -105,20 +107,22 @@ This software is licensed under the [MIT license](http://en.wikipedia.org/wiki/M
© 2011 Peter Odding &lt;<peter@peterodding.com>&gt;.


[vim]: http://www.vim.org/
[lua]: http://www.lua.org/
[inex]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27includeexpr%27
[cfu]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27completefunc%27
[dll]: http://en.wikipedia.org/wiki/Dynamic-link_library
[dof]: http://www.lua.org/manual/5.1/manual.html#pdf-dofile
[gf]: http://vimdoc.sourceforge.net/htmldoc/editing.html#gf
[pp]: http://www.lua.org/manual/5.1/manual.html#pdf-package.path
[inc]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27include%27
[dof]: http://www.lua.org/manual/5.1/manual.html#pdf-dofile
[inex]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27includeexpr%27
[ll]: http://lua-users.org/wiki/LuaLint
[lof]: http://www.lua.org/manual/5.1/manual.html#pdf-loadfile
[req]: http://www.lua.org/manual/5.1/manual.html#pdf-require
[lrv]: http://www.vim.org/scripts/script.php?script_id=1291
[cfu]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27completefunc%27
[lua]: http://www.lua.org/
[mit]: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#matchit-install
[ofu]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27omnifunc%27
[pp]: http://www.lua.org/manual/5.1/manual.html#pdf-package.path
[req]: http://www.lua.org/manual/5.1/manual.html#pdf-require
[script]: http://www.vim.org/scripts/script.php?script_id=3625
[shell]: http://peterodding.com/code/vim/shell/
[tob]: http://vimdoc.sourceforge.net/htmldoc/motion.html#text-objects
[mit]: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#matchit-install
[vim]: http://www.vim.org/
[zip]: http://peterodding.com/code/vim/downloads/lua-ftplugin.zip
[ll]: http://lua-users.org/wiki/LuaLint
[script]: http://www.vim.org/scripts/script.php?script_id=3625
88 changes: 52 additions & 36 deletions autoload/xolox/lua.vim
Expand Up @@ -3,7 +3,7 @@
" Last Change: November 25, 2011
" URL: http://peterodding.com/code/vim/lua-ftplugin

let g:xolox#lua#version = '0.6.29'
let g:xolox#lua#version = '0.7'
let s:miscdir = expand('<sfile>:p:h:h:h') . '/misc/lua-ftplugin'
let s:omnicomplete_script = s:miscdir . '/omnicomplete.lua'
let s:globals_script = s:miscdir . '/globals.lua'
Expand Down Expand Up @@ -41,12 +41,12 @@ function! xolox#lua#getsearchpath(envvar, luavar) " {{{1
if !empty(path)
call xolox#misc#msg#debug("lua.vim %s: Got %s from %s", g:xolox#lua#version, a:luavar, a:envvar)
else
let path = system('lua -e "io.write(' . a:luavar . ')"')
if v:shell_error
call xolox#misc#msg#warn("lua.vim %s: Failed to get %s from external Lua interpreter: %s", g:xolox#lua#version, a:luavar, path)
else
try
let path = xolox#misc#os#exec('lua -e "io.write(' . a:luavar . ')"')[0]
call xolox#misc#msg#debug("lua.vim %s: Got %s from external Lua interpreter", g:xolox#lua#version, a:luavar)
endif
catch
call xolox#misc#msg#warn("lua.vim %s: Failed to get %s from external Lua interpreter: %s", g:xolox#lua#version, a:luavar, v:exception)
endtry
endif
endif
return split(xolox#misc#str#trim(path), ';')
Expand All @@ -73,27 +73,45 @@ function! xolox#lua#checksyntax() " {{{1
let message .= " automatic syntax checking for Lua scripts."
let g:lua_check_syntax = 0
call xolox#misc#msg#warn(message, g:xolox#lua#version)
else
let mp_save = &makeprg
let efm_save = &errorformat
try
let &makeprg = compiler_name
let &errorformat = error_format
let winnr = winnr()
let filename = expand('%:t')
execute 'silent make!' compiler_args xolox#misc#escape#shell(expand('%'))
cwindow
if winnr() != winnr
let message = ['Syntax errors reported by', compiler_name, compiler_args, filename]
let w:quickfix_title = join(message)
endif
execute winnr . 'wincmd w'
call s:highlighterrors()
finally
let &makeprg = mp_save
let &errorformat = efm_save
endtry
return
endif
" Check for errors using my shell.vim plug-in so that executing
" luac.exe on Windows doesn't pop up the nasty console window.
let command = [compiler_name, compiler_args, xolox#misc#escape#shell(expand('%'))]
let lines = xolox#misc#os#exec(join(command))
if empty(lines)
" Clear location list.
call setloclist(winnr(), [], 'r')
lclose
return
endif
" Save the errors to a file we can load with :lgetfile.
let errorfile = tempname()
call writefile(lines, errorfile)
" Remember the original values of these options.
let mp_save = &makeprg
let efm_save = &errorformat
try
" Temporarily change the options.
let &makeprg = compiler_name
let &errorformat = error_format
let winnr = winnr()
let filename = expand('%:t')
execute 'lgetfile' fnameescape(errorfile)
lwindow
if winnr() != winnr
let message = ['Syntax errors reported by', compiler_name, compiler_args, filename]
let w:quickfix_title = join(message)
execute winnr . 'wincmd w'
endif
call s:highlighterrors()
finally
" Restore the options.
let &makeprg = mp_save
let &errorformat = efm_save
" Cleanup the file with errors.
call delete(errorfile)
endtry
endfunction

function! s:highlighterrors()
Expand All @@ -112,8 +130,7 @@ endfunction

function! xolox#lua#checkglobals(verbose) " {{{1
let output = xolox#lua#dofile(s:globals_script, [expand('%'), a:verbose])
let qflist = eval('[' . substitute(output, '\n', ',', 'g') . ']')
call setqflist(qflist, 'r')
call setqflist(eval('[' . join(output, ',') . ']'), 'r')
cwindow
endfunction

Expand Down Expand Up @@ -402,9 +419,9 @@ endfunction
function! xolox#lua#getomnivariables(modules) " {{{1
let starttime = xolox#misc#timer#start()
let output = xolox#lua#dofile(s:omnicomplete_script, a:modules)
let variables = eval('[' . substitute(output, '\_s\+', ',', 'g') . ']')
let variables = eval('[' . join(output, ',') . ']')
call sort(variables, 1)
let msg = "lua.vim %s: Collected %i variables for omni completion in %s"
let msg = "lua.vim %s: Collected %i variables for omni completion in %s."
call xolox#misc#timer#stop(msg, g:xolox#lua#version, len(variables), starttime)
return variables
endfunction
Expand Down Expand Up @@ -453,15 +470,14 @@ function! xolox#lua#dofile(pathname, arguments) " {{{1
lua arg = vim.eval('a:arguments')
execute 'silent luafile' fnameescape(a:pathname)
redir END
return split(output, "\n")
else
" Use the command line Lua interpreter.
let output = xolox#misc#str#trim(system(join(['lua', a:pathname] + a:arguments)))
if v:shell_error
let msg = "lua.vim %s: Failed to retrieve omni completion candidates (output: '%s')"
call xolox#misc#msg#warn(msg, g:xolox#lua#version, output)
endif
let qpath = xolox#misc#escape#shell(a:pathname)
let qargs = join(map(a:arguments, 'xolox#misc#escape#shell(v:val)'))
" TODO Make name of Lua executable configurable!
return xolox#misc#os#exec(printf('lua %s %s', qpath, qargs))
endif
return xolox#misc#str#trim(output)
endfunction

" vim: ts=2 sw=2 et
19 changes: 13 additions & 6 deletions doc/ft_lua.txt
Expand Up @@ -50,6 +50,11 @@ restart Vim and execute the command ':helptags ~/.vim/doc' (use ':helptags
~\vimfiles\doc' instead on Windows). Now try it out: Edit a Lua script and try
any of the features documented above.

Note that on Windows a command prompt window pops up whenever Lua is run as an
external process. If this bothers you then you can install my shell.vim [9]
plug-in which includes a DLL [10] that works around this issue. Once you've
installed both plug-ins it should work out of the box!

===============================================================================
*ft_lua-options*
Options ~
Expand Down Expand Up @@ -101,7 +106,7 @@ The *lua_compiler_name* option

The name or path of the Lua compiler used to check for syntax errors (defaults
to 'luac'). You can set this option to run the Lua compiler from a
non-standard location or to run a dedicated syntax checker like lualint [9].
non-standard location or to run a dedicated syntax checker like lualint [11].

-------------------------------------------------------------------------------
The *lua_compiler_args* option
Expand Down Expand Up @@ -167,13 +172,13 @@ Contact ~
If you have questions, bug reports, suggestions, etc. the author can be
contacted at peter@peterodding.com. The latest version is available at
http://peterodding.com/code/vim/lua-ftplugin and http://github.com/xolox/vim-lua-ftplugin.
If you like this plug-in please vote for it on Vim Online [10].
If you like this plug-in please vote for it on Vim Online [12].

===============================================================================
*ft_lua-license*
License ~

This software is licensed under the MIT license [11]. Copyright 2011 Peter
This software is licensed under the MIT license [13]. Copyright 2011 Peter
Odding <peter@peterodding.com>.

===============================================================================
Expand All @@ -188,8 +193,10 @@ References ~
[6] http://www.vim.org/scripts/script.php?script_id=1291
[7] http://peterodding.com/code/vim/lua-ftplugin/screenshots/omni-completion.png
[8] http://peterodding.com/code/vim/downloads/lua-ftplugin.zip
[9] http://lua-users.org/wiki/LuaLint
[10] http://www.vim.org/scripts/script.php?script_id=3625
[11] http://en.wikipedia.org/wiki/MIT_License
[9] http://peterodding.com/code/vim/shell/
[10] http://en.wikipedia.org/wiki/Dynamic-link_library
[11] http://lua-users.org/wiki/LuaLint
[12] http://www.vim.org/scripts/script.php?script_id=3625
[13] http://en.wikipedia.org/wiki/MIT_License

vim: ft=help

0 comments on commit bccf110

Please sign in to comment.