Skip to content

Commit

Permalink
lua: fix theme loading when lexer module is not available
Browse files Browse the repository at this point in the history
The color settings are currently stored in the `vis.lexers` table,
make sure it is not nil even when loading the lexer module (or one
of its dependencies e.g. lpeg) failed.
  • Loading branch information
martanne committed Jan 22, 2018
1 parent a94b52f commit 5b8042a
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lua/plugins/number-inc-dec.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- increment/decrement number in dec/hex/oct format
local lexer = vis.lexers
local lpeg = vis.lpeg
if not lexer or not lpeg then return end
if not lexer.load or not lpeg then return end

local Cp = lpeg.Cp()
local dec_num = lpeg.S('+-')^-1 * lexer.dec_num
Expand Down
2 changes: 1 addition & 1 deletion lua/plugins/textobject-lexer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local MAX_CONTEXT = 32768

vis:textobject_new("ii", function(win, pos)

if win.syntax == nil or not vis.lexers then
if not win.syntax or not vis.lexers.load then
return nil
end

Expand Down
6 changes: 3 additions & 3 deletions lua/vis-std.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ vis:option_register("theme", "string", function(name)
require(theme)
end

if vis.lexers then vis.lexers.lexers = {} end
vis.lexers.lexers = {}

for win in vis:windows() do
win:set_syntax(win.syntax)
Expand All @@ -38,9 +38,9 @@ vis:option_register("horizon", "number", function(horizon)
end, "Number of bytes to consider for syntax highlighting")

vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
if win.syntax == nil or vis.lexers == nil then return end
if not win.syntax or not vis.lexers.load then return end
local lexer = vis.lexers.load(win.syntax, nil, true)
if lexer == nil then return end
if not lexer then return end

-- TODO: improve heuristic for initial style
local viewport = win.viewport
Expand Down
4 changes: 3 additions & 1 deletion lua/vis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ vis.module_exist = function(vis, name)
return false
end

vis.lexers = {}

if not vis:module_exist('lpeg') then
vis:info('WARNING: could not find lpeg module')
elseif not vis:module_exist('lexer') then
Expand Down Expand Up @@ -235,7 +237,6 @@ vis.events = events
vis.types.window.set_syntax = function(win, syntax)

local lexers = vis.lexers
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 All @@ -256,6 +257,7 @@ vis.types.window.set_syntax = function(win, syntax)
return true
end

if not lexers.load then return false end
local lexer = lexers.load(syntax)
if not lexer then return false end

Expand Down

0 comments on commit 5b8042a

Please sign in to comment.