Skip to content

Commit bccf110

Browse files
committed
Use shell.vim plug-in to execute Lua subprocesses (issue #2)
1 parent 0814a1b commit bccf110

File tree

3 files changed

+79
-52
lines changed

3 files changed

+79
-52
lines changed

README.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ The [Lua][lua] file type plug-in for [Vim][vim] makes it easier to work with Lua
2424

2525
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.
2626

27+
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!
28+
2729
## Options
2830

2931
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:
@@ -105,20 +107,22 @@ This software is licensed under the [MIT license](http://en.wikipedia.org/wiki/M
105107
© 2011 Peter Odding &lt;<peter@peterodding.com>&gt;.
106108

107109

108-
[vim]: http://www.vim.org/
109-
[lua]: http://www.lua.org/
110-
[inex]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27includeexpr%27
110+
[cfu]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27completefunc%27
111+
[dll]: http://en.wikipedia.org/wiki/Dynamic-link_library
112+
[dof]: http://www.lua.org/manual/5.1/manual.html#pdf-dofile
111113
[gf]: http://vimdoc.sourceforge.net/htmldoc/editing.html#gf
112-
[pp]: http://www.lua.org/manual/5.1/manual.html#pdf-package.path
113114
[inc]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27include%27
114-
[dof]: http://www.lua.org/manual/5.1/manual.html#pdf-dofile
115+
[inex]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27includeexpr%27
116+
[ll]: http://lua-users.org/wiki/LuaLint
115117
[lof]: http://www.lua.org/manual/5.1/manual.html#pdf-loadfile
116-
[req]: http://www.lua.org/manual/5.1/manual.html#pdf-require
117118
[lrv]: http://www.vim.org/scripts/script.php?script_id=1291
118-
[cfu]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27completefunc%27
119+
[lua]: http://www.lua.org/
120+
[mit]: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#matchit-install
119121
[ofu]: http://vimdoc.sourceforge.net/htmldoc/options.html#%27omnifunc%27
122+
[pp]: http://www.lua.org/manual/5.1/manual.html#pdf-package.path
123+
[req]: http://www.lua.org/manual/5.1/manual.html#pdf-require
124+
[script]: http://www.vim.org/scripts/script.php?script_id=3625
125+
[shell]: http://peterodding.com/code/vim/shell/
120126
[tob]: http://vimdoc.sourceforge.net/htmldoc/motion.html#text-objects
121-
[mit]: http://vimdoc.sourceforge.net/htmldoc/usr_05.html#matchit-install
127+
[vim]: http://www.vim.org/
122128
[zip]: http://peterodding.com/code/vim/downloads/lua-ftplugin.zip
123-
[ll]: http://lua-users.org/wiki/LuaLint
124-
[script]: http://www.vim.org/scripts/script.php?script_id=3625

autoload/xolox/lua.vim

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
" Last Change: November 25, 2011
44
" URL: http://peterodding.com/code/vim/lua-ftplugin
55

6-
let g:xolox#lua#version = '0.6.29'
6+
let g:xolox#lua#version = '0.7'
77
let s:miscdir = expand('<sfile>:p:h:h:h') . '/misc/lua-ftplugin'
88
let s:omnicomplete_script = s:miscdir . '/omnicomplete.lua'
99
let s:globals_script = s:miscdir . '/globals.lua'
@@ -41,12 +41,12 @@ function! xolox#lua#getsearchpath(envvar, luavar) " {{{1
4141
if !empty(path)
4242
call xolox#misc#msg#debug("lua.vim %s: Got %s from %s", g:xolox#lua#version, a:luavar, a:envvar)
4343
else
44-
let path = system('lua -e "io.write(' . a:luavar . ')"')
45-
if v:shell_error
46-
call xolox#misc#msg#warn("lua.vim %s: Failed to get %s from external Lua interpreter: %s", g:xolox#lua#version, a:luavar, path)
47-
else
44+
try
45+
let path = xolox#misc#os#exec('lua -e "io.write(' . a:luavar . ')"')[0]
4846
call xolox#misc#msg#debug("lua.vim %s: Got %s from external Lua interpreter", g:xolox#lua#version, a:luavar)
49-
endif
47+
catch
48+
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)
49+
endtry
5050
endif
5151
endif
5252
return split(xolox#misc#str#trim(path), ';')
@@ -73,27 +73,45 @@ function! xolox#lua#checksyntax() " {{{1
7373
let message .= " automatic syntax checking for Lua scripts."
7474
let g:lua_check_syntax = 0
7575
call xolox#misc#msg#warn(message, g:xolox#lua#version)
76-
else
77-
let mp_save = &makeprg
78-
let efm_save = &errorformat
79-
try
80-
let &makeprg = compiler_name
81-
let &errorformat = error_format
82-
let winnr = winnr()
83-
let filename = expand('%:t')
84-
execute 'silent make!' compiler_args xolox#misc#escape#shell(expand('%'))
85-
cwindow
86-
if winnr() != winnr
87-
let message = ['Syntax errors reported by', compiler_name, compiler_args, filename]
88-
let w:quickfix_title = join(message)
89-
endif
90-
execute winnr . 'wincmd w'
91-
call s:highlighterrors()
92-
finally
93-
let &makeprg = mp_save
94-
let &errorformat = efm_save
95-
endtry
76+
return
9677
endif
78+
" Check for errors using my shell.vim plug-in so that executing
79+
" luac.exe on Windows doesn't pop up the nasty console window.
80+
let command = [compiler_name, compiler_args, xolox#misc#escape#shell(expand('%'))]
81+
let lines = xolox#misc#os#exec(join(command))
82+
if empty(lines)
83+
" Clear location list.
84+
call setloclist(winnr(), [], 'r')
85+
lclose
86+
return
87+
endif
88+
" Save the errors to a file we can load with :lgetfile.
89+
let errorfile = tempname()
90+
call writefile(lines, errorfile)
91+
" Remember the original values of these options.
92+
let mp_save = &makeprg
93+
let efm_save = &errorformat
94+
try
95+
" Temporarily change the options.
96+
let &makeprg = compiler_name
97+
let &errorformat = error_format
98+
let winnr = winnr()
99+
let filename = expand('%:t')
100+
execute 'lgetfile' fnameescape(errorfile)
101+
lwindow
102+
if winnr() != winnr
103+
let message = ['Syntax errors reported by', compiler_name, compiler_args, filename]
104+
let w:quickfix_title = join(message)
105+
execute winnr . 'wincmd w'
106+
endif
107+
call s:highlighterrors()
108+
finally
109+
" Restore the options.
110+
let &makeprg = mp_save
111+
let &errorformat = efm_save
112+
" Cleanup the file with errors.
113+
call delete(errorfile)
114+
endtry
97115
endfunction
98116

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

113131
function! xolox#lua#checkglobals(verbose) " {{{1
114132
let output = xolox#lua#dofile(s:globals_script, [expand('%'), a:verbose])
115-
let qflist = eval('[' . substitute(output, '\n', ',', 'g') . ']')
116-
call setqflist(qflist, 'r')
133+
call setqflist(eval('[' . join(output, ',') . ']'), 'r')
117134
cwindow
118135
endfunction
119136

@@ -402,9 +419,9 @@ endfunction
402419
function! xolox#lua#getomnivariables(modules) " {{{1
403420
let starttime = xolox#misc#timer#start()
404421
let output = xolox#lua#dofile(s:omnicomplete_script, a:modules)
405-
let variables = eval('[' . substitute(output, '\_s\+', ',', 'g') . ']')
422+
let variables = eval('[' . join(output, ',') . ']')
406423
call sort(variables, 1)
407-
let msg = "lua.vim %s: Collected %i variables for omni completion in %s"
424+
let msg = "lua.vim %s: Collected %i variables for omni completion in %s."
408425
call xolox#misc#timer#stop(msg, g:xolox#lua#version, len(variables), starttime)
409426
return variables
410427
endfunction
@@ -453,15 +470,14 @@ function! xolox#lua#dofile(pathname, arguments) " {{{1
453470
lua arg = vim.eval('a:arguments')
454471
execute 'silent luafile' fnameescape(a:pathname)
455472
redir END
473+
return split(output, "\n")
456474
else
457475
" Use the command line Lua interpreter.
458-
let output = xolox#misc#str#trim(system(join(['lua', a:pathname] + a:arguments)))
459-
if v:shell_error
460-
let msg = "lua.vim %s: Failed to retrieve omni completion candidates (output: '%s')"
461-
call xolox#misc#msg#warn(msg, g:xolox#lua#version, output)
462-
endif
476+
let qpath = xolox#misc#escape#shell(a:pathname)
477+
let qargs = join(map(a:arguments, 'xolox#misc#escape#shell(v:val)'))
478+
" TODO Make name of Lua executable configurable!
479+
return xolox#misc#os#exec(printf('lua %s %s', qpath, qargs))
463480
endif
464-
return xolox#misc#str#trim(output)
465481
endfunction
466482

467483
" vim: ts=2 sw=2 et

doc/ft_lua.txt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ restart Vim and execute the command ':helptags ~/.vim/doc' (use ':helptags
5050
~\vimfiles\doc' instead on Windows). Now try it out: Edit a Lua script and try
5151
any of the features documented above.
5252

53+
Note that on Windows a command prompt window pops up whenever Lua is run as an
54+
external process. If this bothers you then you can install my shell.vim [9]
55+
plug-in which includes a DLL [10] that works around this issue. Once you've
56+
installed both plug-ins it should work out of the box!
57+
5358
===============================================================================
5459
*ft_lua-options*
5560
Options ~
@@ -101,7 +106,7 @@ The *lua_compiler_name* option
101106

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

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

172177
===============================================================================
173178
*ft_lua-license*
174179
License ~
175180

176-
This software is licensed under the MIT license [11]. Copyright 2011 Peter
181+
This software is licensed under the MIT license [13]. Copyright 2011 Peter
177182
Odding <peter@peterodding.com>.
178183

179184
===============================================================================
@@ -188,8 +193,10 @@ References ~
188193
[6] http://www.vim.org/scripts/script.php?script_id=1291
189194
[7] http://peterodding.com/code/vim/lua-ftplugin/screenshots/omni-completion.png
190195
[8] http://peterodding.com/code/vim/downloads/lua-ftplugin.zip
191-
[9] http://lua-users.org/wiki/LuaLint
192-
[10] http://www.vim.org/scripts/script.php?script_id=3625
193-
[11] http://en.wikipedia.org/wiki/MIT_License
196+
[9] http://peterodding.com/code/vim/shell/
197+
[10] http://en.wikipedia.org/wiki/Dynamic-link_library
198+
[11] http://lua-users.org/wiki/LuaLint
199+
[12] http://www.vim.org/scripts/script.php?script_id=3625
200+
[13] http://en.wikipedia.org/wiki/MIT_License
194201

195202
vim: ft=help

0 commit comments

Comments
 (0)