Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Support included dependencies

I've changed my custom packaging script to include the required Lua
libraries in the ZIP archives I publish on www.vim.org and I've also
added the code required to reliably set Lua's "package.path" whether
using the Lua interface for Vim or an external process. This makes the
plug-in self contained: Just unzip the archive and you're off!
  • Loading branch information...
commit 43307cbb3a99b3a6075640151626f50846ced474 1 parent ce2b578
Peter Odding authored August 10, 2010
6  README.md
Source Rendered
@@ -6,11 +6,7 @@ The Vim plug-in `luainspect.vim` uses the [LuaInspect](http://lua-users.org/wiki
6 6
 
7 7
 ## Installation
8 8
 
9  
-1. Unzip the most recent [ZIP archive](http://peterodding.com/code/vim/downloads/lua-inspect) file inside your Vim profile directory (usually this is `~/.vim` on UNIX and `%USERPROFILE%\vimfiles` on Windows) and move or symlink the file `luainspect4vim.lua` somewhere where Lua's `require()` function can find it.
10  
-
11  
-2. Download the latest [LuaInspect sources](http://github.com/davidm/lua-inspect/zipball/master) and unpack the contents of the `luainspectlib/` and `metalualib/` directories where Lua's `require()` function can find them.
12  
-
13  
-3. Restart Vim and edit any Lua file. Within a few seconds semantic highlighting should be enabled automatically.
  9
+Unzip the most recent [ZIP archive](http://peterodding.com/code/vim/downloads/lua-inspect) 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 file and within a few seconds semantic highlighting should be enabled automatically!
14 10
 
15 11
 ## Usage
16 12
 
142  luainspect.vim
@@ -17,6 +17,15 @@ if !exists('g:lua_inspect_events')
17 17
   let g:lua_inspect_events = 'CursorHold,CursorHoldI,BufWritePost'
18 18
 endif
19 19
 
  20
+if !exists('g:lua_inspect_path')
  21
+  " Change this if you want to move the Lua modules somewhere else.
  22
+  if has('win32') || has('win64')
  23
+    let g:lua_inspect_path = '~\vimfiles\misc\luainspect'
  24
+  else
  25
+    let g:lua_inspect_path = '~/.vim/misc/luainspect'
  26
+  endif
  27
+endif
  28
+
20 29
 if !exists('g:lua_inspect_internal')
21 30
   " Set this to false (0) to run LuaInspect as an external process instead of
22 31
   " using the Lua interface for Vim. This makes it slower but might make it
@@ -42,95 +51,84 @@ let s:groups['SelectedVariable'] = 'Folded'
42 51
 
43 52
 " (Automatic) command definitions. {{{1
44 53
 
45  
-command! -bar -bang LuaInspect call s:RunLuaInspect(<q-bang> == '!')
  54
+command! -bar -bang LuaInspect call s:run_lua_inspect(<q-bang> != '!')
46 55
 
47 56
 augroup PluginLuaInspect
48 57
   " Clear existing automatic commands.
49 58
   autocmd!
50 59
   " Disable easytags.vim because it doesn't play nice with luainspect.vim!
51  
-  autocmd BufReadPost * if s:IsEnabled() | let b:easytags_nohl = 1 | endif
  60
+  autocmd BufReadPost * if s:check_plugin_enabled() | let b:easytags_nohl = 1 | endif
52 61
   " Define the configured automatic commands.
53 62
   for s:event in split(g:lua_inspect_events, ',')
54  
-    execute 'autocmd' s:event '* if s:IsEnabled() | LuaInspect | endif'
  63
+    execute 'autocmd' s:event '* if s:check_plugin_enabled() | LuaInspect | endif'
55 64
   endfor
56 65
 augroup END
57 66
 
58 67
 " Script local functions. {{{1
59 68
 
60  
-function! s:IsEnabled()
  69
+function! s:check_plugin_enabled()
61 70
   return &ft == 'lua' && !&diff && !exists('b:luainspect_disabled')
62 71
 endfunction
63 72
 
64  
-function! s:RunLuaInspect(disable) " {{{2
65  
-  if a:disable
66  
-    call s:ClearPreviousMatches()
  73
+function! s:run_lua_inspect(enabled) " {{{2
  74
+  if s:set_plugin_enabled(a:enabled)
  75
+    let lines = getline(1, "$")
  76
+    call insert(lines, col('.'))
  77
+    call insert(lines, line('.'))
  78
+    call s:parse_text(join(lines, "\n"), s:prepare_search_path())
  79
+    call s:define_default_styles()
  80
+    call s:clear_previous_matches()
  81
+    call s:highlight_variables()
  82
+  endif
  83
+endfunction
  84
+
  85
+function! s:set_plugin_enabled(enabled) " {{{2
  86
+  if a:enabled
  87
+    unlet! b:luainspect_disabled
  88
+    return 1
  89
+  else
  90
+    call s:clear_previous_matches()
67 91
     unlet! b:luainspect_input b:luainspect_output
68 92
     let b:luainspect_disabled = 1
69  
-    return
70  
-  else
71  
-    unlet! b:luainspect_disabled
  93
+    return 0
  94
+  endif
  95
+endfunction
  96
+
  97
+function! s:prepare_search_path() " {{{2
  98
+  let code = ''
  99
+  if !(has('lua') && g:lua_inspect_internal && exists('s:changed_path'))
  100
+    let template = 'package.path = ''%s/?.lua;'' .. package.path'
  101
+    let code = printf(template, escape(expand(g:lua_inspect_path), '"\'))
  102
+    if has('lua') && g:lua_inspect_internal
  103
+      execute 'lua' code
  104
+      let s:changed_path = 1
  105
+    endif
72 106
   endif
73  
-  let lines = getline(1, "$")
74  
-  call insert(lines, col('.'))
75  
-  call insert(lines, line('.'))
76  
-  let l:input = join(lines, "\n")
77  
-  " Don't parse the text when it hasn't been changed.
78  
-  if !(exists('b:luainspect_input') && b:luainspect_input == l:input)
  107
+  return code
  108
+endfunction
  109
+
  110
+function! s:parse_text(input, search_path) " {{{2
  111
+  if !(exists('b:luainspect_input') && b:luainspect_input == a:input)
79 112
     if !(has('lua') && g:lua_inspect_internal)
80  
-      " Run LuaInspect as an external program.
81  
-      let b:luainspect_output = system('lua -e "require ''luainspect4vim'' (io.read ''*a'')"', l:input)
  113
+      let template = 'lua -e "%s; require ''luainspect4vim'' (io.read ''*a'')"'
  114
+      let b:luainspect_output = system(printf(template, a:search_path), a:input)
82 115
     else
83  
-      " Run LuaInspect using the Lua interface for Vim.
84 116
       redir => b:luainspect_output
85 117
       silent lua << EOF
86 118
       if io == nil then
87 119
         -- The Lua interface for Vim previously didn't include io.*!
88 120
         io = { type = function() end }
89 121
       end
90  
-      require 'luainspect4vim' (vim.eval 'l:input')
  122
+      require 'luainspect4vim' (vim.eval 'a:input')
91 123
 EOF
92 124
       redir END
93 125
     endif
94 126
     " Remember the text that was just parsed.
95  
-    let b:luainspect_input = l:input
  127
+    let b:luainspect_input = a:input
96 128
   endif
97  
-  " Clear previously created highlighting.
98  
-  call s:LoadDefaultStyles()
99  
-  call s:ClearPreviousMatches()
100  
-  " Highlight variables in buffer based on positions.
101  
-  let did_warning = 0
102  
-  for line in split(b:luainspect_output, "\n")
103  
-    let fields = split(line)
104  
-    if len(fields) != 4
105  
-      if !did_warning
106  
-        try
107  
-          echohl WarningMsg
108  
-          echomsg "Invalid output from luainspect4vim.lua:"
109  
-        finally
110  
-          echohl None
111  
-          let did_warning = 1
112  
-        endtry
113  
-      endif
114  
-      echomsg strtrans(line)
115  
-    else
116  
-      let [type, lnum, start, end] = fields
117  
-      let command = 'syntax match %s /\%%%il\%%>%ic\<\w\+\>\%%<%ic/'
118  
-      execute printf(command, type, lnum, start - 1, end + 2)
119  
-    endif
120  
-  endfor
121 129
 endfunction
122 130
 
123  
-function! s:ClearPreviousMatches() " {{{2
124  
-  " Clear existing highlighting.
125  
-  for group in keys(s:groups)
126  
-    let group = 'luaInspect' . group
127  
-    if hlexists(group)
128  
-      execute 'syntax clear' group
129  
-    endif
130  
-  endfor
131  
-endfunction
132  
-
133  
-function! s:LoadDefaultStyles() " {{{2
  131
+function! s:define_default_styles() " {{{2
134 132
   " Always define the default highlighting styles
135 133
   " (copied from /luainspect/scite.lua for consistency).
136 134
   " TODO Consider the &background?
@@ -148,6 +146,38 @@ function! s:LoadDefaultStyles() " {{{2
148 146
   endfor
149 147
 endfunction
150 148
 
  149
+function! s:clear_previous_matches() " {{{2
  150
+  " Clear existing highlighting.
  151
+  for group in keys(s:groups)
  152
+    let group = 'luaInspect' . group
  153
+    if hlexists(group)
  154
+      execute 'syntax clear' group
  155
+    endif
  156
+  endfor
  157
+endfunction
  158
+
  159
+function! s:highlight_variables() " {{{2
  160
+  let did_warning = 0
  161
+  for line in split(b:luainspect_output, "\n")
  162
+    if match(line, '^\w\+\(\s\+\d\+\)\{3}$') == -1
  163
+      if !did_warning
  164
+        try
  165
+          echohl WarningMsg
  166
+          echomsg "Invalid output from luainspect4vim.lua:"
  167
+        finally
  168
+          echohl None
  169
+          let did_warning = 1
  170
+        endtry
  171
+      endif
  172
+      echomsg strtrans(line)
  173
+    else
  174
+      let [type, lnum, start, end] = split(line)
  175
+      let syntax_cmd = 'syntax match %s /\%%%il\%%>%ic\<\w\+\>\%%<%ic/'
  176
+      execute printf(syntax_cmd, type, lnum, start - 1, end + 2)
  177
+    endif
  178
+  endfor
  179
+endfunction
  180
+
151 181
 " }}}1
152 182
 
153 183
 " Make sure the plug-in is only loaded once.
1  luainspect4vim.lua
@@ -39,7 +39,6 @@ end
39 39
 return function(src)
40 40
   local LI = require 'luainspect.init'
41 41
   local LA = require 'luainspect.ast'
42  
-  -- Split input into current position and source text.
43 42
   local line, column, src = src:match '^(%d+)\n(%d+)\n(.*)$'
44 43
   line = tonumber(line)
45 44
   column = tonumber(column)

0 notes on commit 43307cb

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