Merge branch 'master' of https://github.com/KaneRoot/vis
[vis.git] / lua / vis-std.lua
blob470872fa9406131243b8ad9a0bc9bd19db7d9d2f
1 -- standard vis event handlers
3 vis.events.subscribe(vis.events.INIT, function()
4 if os.getenv("TERM_PROGRAM") == "Apple_Terminal" then
5 vis:command("set change-256colors false");
6 end
7 vis:command("set theme ".. (vis.ui.colors <= 16 and "default-16" or "default-256"))
8 end)
10 vis:option_register("theme", "string", function(name)
11 if name ~= nil then
12 local theme = 'themes/'..name
13 package.loaded[theme] = nil
14 require(theme)
15 end
17 vis.lexers.lexers = {}
19 for win in vis:windows() do
20 win:set_syntax(win.syntax)
21 end
22 return true
23 end, "Color theme to use, filename without extension")
25 vis:option_register("syntax", "string", function(name)
26 if not vis.win then return false end
27 if not vis.win:set_syntax(name) then
28 vis:info(string.format("Unknown syntax definition: `%s'", name))
29 return false
30 end
31 return true
32 end, "Syntax highlighting lexer to use")
34 vis:option_register("horizon", "number", function(horizon)
35 if not vis.win then return false end
36 vis.win.horizon = horizon
37 return true
38 end, "Number of bytes to consider for syntax highlighting")
40 vis:option_register("redrawtime", "string", function(redrawtime)
41 if not vis.win then return false end
42 local value = tonumber(redrawtime)
43 if not value or value <= 0 then
44 vis:info("A positive real number expected")
45 return false
46 end
47 vis.win.redrawtime = value
48 return true
49 end, "Seconds to wait for syntax highlighting before aborting it")
51 vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
52 if not win.syntax or not vis.lexers.load then return end
53 local lexer = vis.lexers.load(win.syntax, nil, true)
54 if not lexer then return end
56 -- TODO: improve heuristic for initial style
57 local viewport = win.viewport
58 if not viewport then return end
59 local redrawtime_max = win.redrawtime or 1.0
60 local horizon_max = win.horizon or 32768
61 local horizon = viewport.start < horizon_max and viewport.start or horizon_max
62 local view_start = viewport.start
63 local lex_start = viewport.start - horizon
64 viewport.start = lex_start
65 local data = win.file:content(viewport)
66 local token_styles = lexer._TOKENSTYLES
67 local tokens, timedout = lexer:lex(data, 1, redrawtime_max)
68 local token_end = lex_start + (tokens[#tokens] or 1) - 1
70 if timedout then return end
72 for i = #tokens - 1, 1, -2 do
73 local token_start = lex_start + (tokens[i-1] or 1) - 1
74 if token_end < view_start then
75 break
76 end
77 local name = tokens[i]
78 local style = token_styles[name]
79 if style ~= nil then
80 win:style(style, token_start, token_end)
81 end
82 token_end = token_start - 1
83 end
84 end)
86 local modes = {
87 [vis.modes.NORMAL] = '',
88 [vis.modes.OPERATOR_PENDING] = '',
89 [vis.modes.VISUAL] = 'VISUAL',
90 [vis.modes.VISUAL_LINE] = 'VISUAL-LINE',
91 [vis.modes.INSERT] = 'INSERT',
92 [vis.modes.REPLACE] = 'REPLACE',
95 vis.events.subscribe(vis.events.WIN_STATUS, function(win)
96 local left_parts = {}
97 local right_parts = {}
98 local file = win.file
99 local selection = win.selection
101 local mode = modes[vis.mode]
102 if mode ~= '' and vis.win == win then
103 table.insert(left_parts, mode)
106 table.insert(left_parts, (file.name or '[No Name]') ..
107 (file.modified and ' [+]' or '') .. (vis.recording and ' @' or ''))
109 local count = vis.count
110 local keys = vis.input_queue
111 if keys ~= '' then
112 table.insert(right_parts, keys)
113 elseif count then
114 table.insert(right_parts, count)
117 if #win.selections > 1 then
118 table.insert(right_parts, selection.number..'/'..#win.selections)
121 local size = file.size
122 local pos = selection.pos
123 if not pos then pos = 0 end
124 table.insert(right_parts, (size == 0 and "0" or math.ceil(pos/size*100)).."%")
126 if not win.large then
127 local col = selection.col
128 table.insert(right_parts, selection.line..', '..col)
129 if size > 33554432 or col > 65536 then
130 win.large = true
134 local left = ' ' .. table.concat(left_parts, " » ") .. ' '
135 local right = ' ' .. table.concat(right_parts, " « ") .. ' '
136 win:status(left, right);
137 end)
139 -- default plugins
141 require('plugins/filetype')
142 require('plugins/textobject-lexer')
143 require('plugins/digraph')
144 require('plugins/number-inc-dec')
145 require('plugins/complete-word')
146 require('plugins/complete-filename')