Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Use shell.vim plug-in to execute Lua subprocesses (issue #2)

  • Loading branch information...
commit bccf110e32b97bf3c4c0e28baf5559c098c318c4 1 parent 0814a1b
Peter Odding authored November 25, 2011
24  README.md
Source Rendered
@@ -24,6 +24,8 @@ The [Lua][lua] file type plug-in for [Vim][vim] makes it easier to work with Lua
24 24
 
25 25
 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.
26 26
 
  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
+
27 29
 ## Options
28 30
 
29 31
 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
105 107
 © 2011 Peter Odding &lt;<peter@peterodding.com>&gt;.
106 108
 
107 109
 
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
111 113
 [gf]: http://vimdoc.sourceforge.net/htmldoc/editing.html#gf
112  
-[pp]: http://www.lua.org/manual/5.1/manual.html#pdf-package.path
113 114
 [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
115 117
 [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
117 118
 [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
119 121
 [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/
120 126
 [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/
122 128
 [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
88  autoload/xolox/lua.vim
@@ -3,7 +3,7 @@
3 3
 " Last Change: November 25, 2011
4 4
 " URL: http://peterodding.com/code/vim/lua-ftplugin
5 5
 
6  
-let g:xolox#lua#version = '0.6.29'
  6
+let g:xolox#lua#version = '0.7'
7 7
 let s:miscdir = expand('<sfile>:p:h:h:h') . '/misc/lua-ftplugin'
8 8
 let s:omnicomplete_script = s:miscdir . '/omnicomplete.lua'
9 9
 let s:globals_script = s:miscdir . '/globals.lua'
@@ -41,12 +41,12 @@ function! xolox#lua#getsearchpath(envvar, luavar) " {{{1
41 41
     if !empty(path)
42 42
       call xolox#misc#msg#debug("lua.vim %s: Got %s from %s", g:xolox#lua#version, a:luavar, a:envvar)
43 43
     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]
48 46
         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
50 50
     endif
51 51
   endif
52 52
   return split(xolox#misc#str#trim(path), ';')
@@ -73,27 +73,45 @@ function! xolox#lua#checksyntax() " {{{1
73 73
     let message .= " automatic syntax checking for Lua scripts."
74 74
     let g:lua_check_syntax = 0
75 75
     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
96 77
   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
97 115
 endfunction
98 116
 
99 117
 function! s:highlighterrors()
@@ -112,8 +130,7 @@ endfunction
112 130
 
113 131
 function! xolox#lua#checkglobals(verbose) " {{{1
114 132
   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')
117 134
   cwindow
118 135
 endfunction
119 136
 
@@ -402,9 +419,9 @@ endfunction
402 419
 function! xolox#lua#getomnivariables(modules) " {{{1
403 420
   let starttime = xolox#misc#timer#start()
404 421
   let output = xolox#lua#dofile(s:omnicomplete_script, a:modules)
405  
-  let variables = eval('[' . substitute(output, '\_s\+', ',', 'g') . ']')
  422
+  let variables = eval('[' . join(output, ',') . ']')
406 423
   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."
408 425
   call xolox#misc#timer#stop(msg, g:xolox#lua#version, len(variables), starttime)
409 426
   return variables
410 427
 endfunction
@@ -453,15 +470,14 @@ function! xolox#lua#dofile(pathname, arguments) " {{{1
453 470
     lua arg = vim.eval('a:arguments')
454 471
     execute 'silent luafile' fnameescape(a:pathname)
455 472
     redir END
  473
+    return split(output, "\n")
456 474
   else
457 475
     " 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))
463 480
   endif
464  
-  return xolox#misc#str#trim(output)
465 481
 endfunction
466 482
 
467 483
 " vim: ts=2 sw=2 et
19  doc/ft_lua.txt
@@ -50,6 +50,11 @@ restart Vim and execute the command ':helptags ~/.vim/doc' (use ':helptags
50 50
 ~\vimfiles\doc' instead on Windows). Now try it out: Edit a Lua script and try
51 51
 any of the features documented above.
52 52
 
  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
+
53 58
 ===============================================================================
54 59
                                                                 *ft_lua-options*
55 60
 Options ~
@@ -101,7 +106,7 @@ The *lua_compiler_name* option
101 106
 
102 107
 The name or path of the Lua compiler used to check for syntax errors (defaults
103 108
 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].
105 110
 
106 111
 -------------------------------------------------------------------------------
107 112
 The *lua_compiler_args* option
@@ -167,13 +172,13 @@ Contact ~
167 172
 If you have questions, bug reports, suggestions, etc. the author can be
168 173
 contacted at peter@peterodding.com. The latest version is available at
169 174
 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].
171 176
 
172 177
 ===============================================================================
173 178
                                                                 *ft_lua-license*
174 179
 License ~
175 180
 
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
177 182
 Odding <peter@peterodding.com>.
178 183
 
179 184
 ===============================================================================
@@ -188,8 +193,10 @@ References ~
188 193
 [6] http://www.vim.org/scripts/script.php?script_id=1291
189 194
 [7] http://peterodding.com/code/vim/lua-ftplugin/screenshots/omni-completion.png
190 195
 [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
194 201
 
195 202
 vim: ft=help

0 notes on commit bccf110

Please sign in to comment.
Something went wrong with that request. Please try again.