aboutsummaryrefslogtreecommitdiff
path: root/lua/vis-std.lua
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2016-12-07 19:50:43 +0100
committerMarc André Tanner <mat@brain-dump.org>2016-12-07 23:44:25 +0100
commit065a804d282ac99dd93e9ebaf7bc986ccf53e75b (patch)
treeffba38ecb6dad1852fb79e9ea4f67a3333c9f376 /lua/vis-std.lua
parent1b896d13b8384958e7b898d85869468eee729961 (diff)
downloadvis-065a804d282ac99dd93e9ebaf7bc986ccf53e75b.tar.gz
vis-065a804d282ac99dd93e9ebaf7bc986ccf53e75b.tar.xz
lua: move non-core code out of vis.lua
The following structure is adapted: * visrc.lua entry point for all Lua code * vis.lua only implements the Lua part of the core API * vis-std.lua registers standard event handlers (e.g. syntax highlighting, statusbar handling, theme changes etc). It is sourced from vis.lua. * plugins/* non essential editor functionality, needs to be explicitly enabled by loading it from visrc.lua
Diffstat (limited to 'lua/vis-std.lua')
-rw-r--r--lua/vis-std.lua119
1 files changed, 119 insertions, 0 deletions
diff --git a/lua/vis-std.lua b/lua/vis-std.lua
new file mode 100644
index 0000000..d8a32ad
--- /dev/null
+++ b/lua/vis-std.lua
@@ -0,0 +1,119 @@
+-- standard vis event handlers
+
+vis.events.theme_change = function(name)
+ if name ~= nil then
+ local theme = 'themes/'..name
+ package.loaded[theme] = nil
+ require(theme)
+ end
+
+ if vis.lexers ~= nil then vis.lexers.lexers = {} end
+
+ for win in vis:windows() do
+ win.syntax = win.syntax;
+ end
+end
+
+vis.events.win_syntax = function(win, name)
+ local lexers = vis.lexers
+ if not lexers.load then return false end
+
+ win:style_define(win.STYLE_DEFAULT, lexers.STYLE_DEFAULT)
+ win:style_define(win.STYLE_CURSOR, lexers.STYLE_CURSOR)
+ win:style_define(win.STYLE_CURSOR_PRIMARY, lexers.STYLE_CURSOR_PRIMARY)
+ win:style_define(win.STYLE_CURSOR_LINE, lexers.STYLE_CURSOR_LINE)
+ win:style_define(win.STYLE_SELECTION, lexers.STYLE_SELECTION)
+ win:style_define(win.STYLE_LINENUMBER, lexers.STYLE_LINENUMBER)
+ win:style_define(win.STYLE_COLOR_COLUMN, lexers.STYLE_COLOR_COLUMN)
+
+ if name == nil then return true end
+
+ local lexer = lexers.load(name)
+ if not lexer then return false end
+
+ for token_name, id in pairs(lexer._TOKENSTYLES) do
+ local style = lexers['STYLE_'..string.upper(token_name)] or lexer._EXTRASTYLES[token_name]
+ win:style_define(id, style)
+ end
+
+ return true
+end
+
+vis.events.win_highlight = function(win, horizon_max)
+ if win.syntax == nil or vis.lexers == nil then return end
+ local lexer = vis.lexers.load(win.syntax)
+ if lexer == nil then return end
+
+ -- TODO: improve heuristic for initial style
+ local viewport = win.viewport
+ if not viewport then return end
+ local horizon = viewport.start < horizon_max and viewport.start or horizon_max
+ local view_start = viewport.start
+ local lex_start = viewport.start - horizon
+ local token_start = lex_start
+ viewport.start = token_start
+ local data = win.file:content(viewport)
+ local token_styles = lexer._TOKENSTYLES
+ local tokens = lexer:lex(data, 1)
+
+ for i = 1, #tokens, 2 do
+ local token_end = lex_start + tokens[i+1] - 1
+ if token_end >= view_start then
+ local name = tokens[i]
+ local style = token_styles[name]
+ if style ~= nil then
+ win:style(style, token_start, token_end)
+ end
+ end
+ token_start = token_end
+ end
+end
+
+local modes = {
+ [vis.MODE_NORMAL] = '',
+ [vis.MODE_OPERATOR_PENDING] = '',
+ [vis.MODE_VISUAL] = 'VISUAL',
+ [vis.MODE_VISUAL_LINE] = 'VISUAL-LINE',
+ [vis.MODE_INSERT] = 'INSERT',
+ [vis.MODE_REPLACE] = 'REPLACE',
+}
+
+vis.events.win_status = function(win)
+ local left_parts = {}
+ local right_parts = {}
+ local file = win.file
+ local cursor = win.cursor
+
+ local mode = modes[vis.mode]
+ if mode ~= '' and vis.win == win then
+ table.insert(left_parts, mode)
+ end
+
+ table.insert(left_parts, (file.name or '[No Name]') ..
+ (file.modified and ' [+]' or '') .. (vis.recording and ' @' or ''))
+
+ if file.newlines == "crlf" then
+ table.insert(right_parts, "␍␊")
+ end
+
+ if #win.cursors > 1 then
+ table.insert(right_parts, cursor.number..'/'..#win.cursors)
+ end
+
+ local size = file.size
+ table.insert(right_parts, (size == 0 and "0" or math.ceil(cursor.pos/size*100)).."%")
+
+ if not win.large then
+ local col = cursor.col
+ table.insert(right_parts, cursor.line..', '..col)
+ if size > 33554432 or col > 65536 then
+ win.large = true
+ end
+ end
+
+ local left = ' ' .. table.concat(left_parts, " » ") .. ' '
+ local right = ' ' .. table.concat(right_parts, " « ") .. ' '
+ win:status(left, right);
+end
+
+vis:command("set theme ".. (vis.ui.colors <= 16 and "default-16" or "default-256"))