Skip to content

Commit

Permalink
Support included dependencies
Browse files Browse the repository at this point in the history
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
xolox committed Aug 10, 2010
1 parent ce2b578 commit 43307cb
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 62 deletions.
6 changes: 1 addition & 5 deletions README.md
Expand Up @@ -6,11 +6,7 @@ The Vim plug-in `luainspect.vim` uses the [LuaInspect](http://lua-users.org/wiki

## Installation

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.

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.

3. Restart Vim and edit any Lua file. Within a few seconds semantic highlighting should be enabled automatically.
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!

## Usage

Expand Down
142 changes: 86 additions & 56 deletions luainspect.vim
Expand Up @@ -17,6 +17,15 @@ if !exists('g:lua_inspect_events')
let g:lua_inspect_events = 'CursorHold,CursorHoldI,BufWritePost'
endif

if !exists('g:lua_inspect_path')
" Change this if you want to move the Lua modules somewhere else.
if has('win32') || has('win64')
let g:lua_inspect_path = '~\vimfiles\misc\luainspect'
else
let g:lua_inspect_path = '~/.vim/misc/luainspect'
endif
endif

if !exists('g:lua_inspect_internal')
" Set this to false (0) to run LuaInspect as an external process instead of
" using the Lua interface for Vim. This makes it slower but might make it
Expand All @@ -42,95 +51,84 @@ let s:groups['SelectedVariable'] = 'Folded'

" (Automatic) command definitions. {{{1

command! -bar -bang LuaInspect call s:RunLuaInspect(<q-bang> == '!')
command! -bar -bang LuaInspect call s:run_lua_inspect(<q-bang> != '!')

augroup PluginLuaInspect
" Clear existing automatic commands.
autocmd!
" Disable easytags.vim because it doesn't play nice with luainspect.vim!
autocmd BufReadPost * if s:IsEnabled() | let b:easytags_nohl = 1 | endif
autocmd BufReadPost * if s:check_plugin_enabled() | let b:easytags_nohl = 1 | endif
" Define the configured automatic commands.
for s:event in split(g:lua_inspect_events, ',')
execute 'autocmd' s:event '* if s:IsEnabled() | LuaInspect | endif'
execute 'autocmd' s:event '* if s:check_plugin_enabled() | LuaInspect | endif'
endfor
augroup END

" Script local functions. {{{1

function! s:IsEnabled()
function! s:check_plugin_enabled()
return &ft == 'lua' && !&diff && !exists('b:luainspect_disabled')
endfunction

function! s:RunLuaInspect(disable) " {{{2
if a:disable
call s:ClearPreviousMatches()
function! s:run_lua_inspect(enabled) " {{{2
if s:set_plugin_enabled(a:enabled)
let lines = getline(1, "$")
call insert(lines, col('.'))
call insert(lines, line('.'))
call s:parse_text(join(lines, "\n"), s:prepare_search_path())
call s:define_default_styles()
call s:clear_previous_matches()
call s:highlight_variables()
endif
endfunction

function! s:set_plugin_enabled(enabled) " {{{2
if a:enabled
unlet! b:luainspect_disabled
return 1
else
call s:clear_previous_matches()
unlet! b:luainspect_input b:luainspect_output
let b:luainspect_disabled = 1
return
else
unlet! b:luainspect_disabled
return 0
endif
endfunction

function! s:prepare_search_path() " {{{2
let code = ''
if !(has('lua') && g:lua_inspect_internal && exists('s:changed_path'))
let template = 'package.path = ''%s/?.lua;'' .. package.path'
let code = printf(template, escape(expand(g:lua_inspect_path), '"\'))
if has('lua') && g:lua_inspect_internal
execute 'lua' code
let s:changed_path = 1
endif
endif
let lines = getline(1, "$")
call insert(lines, col('.'))
call insert(lines, line('.'))
let l:input = join(lines, "\n")
" Don't parse the text when it hasn't been changed.
if !(exists('b:luainspect_input') && b:luainspect_input == l:input)
return code
endfunction

function! s:parse_text(input, search_path) " {{{2
if !(exists('b:luainspect_input') && b:luainspect_input == a:input)
if !(has('lua') && g:lua_inspect_internal)
" Run LuaInspect as an external program.
let b:luainspect_output = system('lua -e "require ''luainspect4vim'' (io.read ''*a'')"', l:input)
let template = 'lua -e "%s; require ''luainspect4vim'' (io.read ''*a'')"'
let b:luainspect_output = system(printf(template, a:search_path), a:input)
else
" Run LuaInspect using the Lua interface for Vim.
redir => b:luainspect_output
silent lua << EOF
if io == nil then
-- The Lua interface for Vim previously didn't include io.*!
io = { type = function() end }
end
require 'luainspect4vim' (vim.eval 'l:input')
require 'luainspect4vim' (vim.eval 'a:input')
EOF
redir END
endif
" Remember the text that was just parsed.
let b:luainspect_input = l:input
let b:luainspect_input = a:input
endif
" Clear previously created highlighting.
call s:LoadDefaultStyles()
call s:ClearPreviousMatches()
" Highlight variables in buffer based on positions.
let did_warning = 0
for line in split(b:luainspect_output, "\n")
let fields = split(line)
if len(fields) != 4
if !did_warning
try
echohl WarningMsg
echomsg "Invalid output from luainspect4vim.lua:"
finally
echohl None
let did_warning = 1
endtry
endif
echomsg strtrans(line)
else
let [type, lnum, start, end] = fields
let command = 'syntax match %s /\%%%il\%%>%ic\<\w\+\>\%%<%ic/'
execute printf(command, type, lnum, start - 1, end + 2)
endif
endfor
endfunction

function! s:ClearPreviousMatches() " {{{2
" Clear existing highlighting.
for group in keys(s:groups)
let group = 'luaInspect' . group
if hlexists(group)
execute 'syntax clear' group
endif
endfor
endfunction

function! s:LoadDefaultStyles() " {{{2
function! s:define_default_styles() " {{{2
" Always define the default highlighting styles
" (copied from /luainspect/scite.lua for consistency).
" TODO Consider the &background?
Expand All @@ -148,6 +146,38 @@ function! s:LoadDefaultStyles() " {{{2
endfor
endfunction

function! s:clear_previous_matches() " {{{2
" Clear existing highlighting.
for group in keys(s:groups)
let group = 'luaInspect' . group
if hlexists(group)
execute 'syntax clear' group
endif
endfor
endfunction

function! s:highlight_variables() " {{{2
let did_warning = 0
for line in split(b:luainspect_output, "\n")
if match(line, '^\w\+\(\s\+\d\+\)\{3}$') == -1
if !did_warning
try
echohl WarningMsg
echomsg "Invalid output from luainspect4vim.lua:"
finally
echohl None
let did_warning = 1
endtry
endif
echomsg strtrans(line)
else
let [type, lnum, start, end] = split(line)
let syntax_cmd = 'syntax match %s /\%%%il\%%>%ic\<\w\+\>\%%<%ic/'
execute printf(syntax_cmd, type, lnum, start - 1, end + 2)
endif
endfor
endfunction

" }}}1

" Make sure the plug-in is only loaded once.
Expand Down
1 change: 0 additions & 1 deletion luainspect4vim.lua
Expand Up @@ -39,7 +39,6 @@ end
return function(src)
local LI = require 'luainspect.init'
local LA = require 'luainspect.ast'
-- Split input into current position and source text.
local line, column, src = src:match '^(%d+)\n(%d+)\n(.*)$'
line = tonumber(line)
column = tonumber(column)
Expand Down

0 comments on commit 43307cb

Please sign in to comment.