Skip to content

Commit

Permalink
lua: expose vis:module_exist method, load lexer and lpeg module durin…
Browse files Browse the repository at this point in the history
…g startup
  • Loading branch information
martanne committed Feb 22, 2017
1 parent 335814c commit b8b3747
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
20 changes: 1 addition & 19 deletions lua/vis-std.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
-- standard vis event handlers

vis.events.subscribe(vis.events.INIT, function()
local package_exist = function(name)
for _, searcher in ipairs(package.searchers or package.loaders) do
local loader = searcher(name)
if type(loader) == 'function' then
return true
end
end
return false
end

if not package_exist('lpeg') then
vis:info('WARNING: could not find lpeg module')
elseif not package_exist('lexer') then
vis:info('WARNING: could not find lexer module')
else
vis.lexers = require('lexer')
end

if os.getenv("TERM_PROGRAM") == "Apple_Terminal" then
vis:command("set change-256colors false");
end
Expand All @@ -41,7 +23,7 @@ end)

vis.events.subscribe(vis.events.WIN_SYNTAX, function(win, name)
local lexers = vis.lexers
if not lexers.load then return false end
if not lexers then return false end

win:style_define(win.STYLE_DEFAULT, lexers.STYLE_DEFAULT or '')
win:style_define(win.STYLE_CURSOR, lexers.STYLE_CURSOR or '')
Expand Down
26 changes: 24 additions & 2 deletions lua/vis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
---
-- @type Vis

vis.lexers = {}

--- Map a new motion.
--
-- Sets up a mapping in normal, visual and operator pending mode.
Expand Down Expand Up @@ -64,6 +62,30 @@ vis.textobject_new = function(vis, key, textobject, help)
return true
end

--- Check whether a Lua module exists
--
-- Checks whether a subsequent @{require} call will succeed.
-- @tparam string name the module name to check
-- @treturn bool whether the module was found
vis.module_exist = function(vis, name)
for _, searcher in ipairs(package.searchers or package.loaders) do
local loader = searcher(name)
if type(loader) == 'function' then
return true
end
end
return false
end

if not vis:module_exist('lpeg') then
vis:info('WARNING: could not find lpeg module')
elseif not vis:module_exist('lexer') then
vis:info('WARNING: could not find lexer module')
else
vis.lexers = require('lexer')
vis.lpeg = require('lpeg')
end

--- Events.
--
-- User scripts can subscribe Lua functions to certain events. Multiple functions
Expand Down
10 changes: 6 additions & 4 deletions vis-lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -543,11 +543,13 @@ static const char *keymapping(Vis *vis, const char *keys, const Arg *arg) {
* @field registers array to access the register by single letter name
*/
/***
* LPeg lexer support module.
* @field lexers
* Scintillua lexer module.
* @field lexers might be `nil` if module is not found
*/
/***
* LPeg lexer module.
* @field lpeg might be `nil` if module is not found
*/

// TODO vis.events

/***
* Create an iterator over all windows.
Expand Down

0 comments on commit b8b3747

Please sign in to comment.