vis: take symbolic keys into account when evaluating key prefixes
[vis.git] / lua / vis-std.lua
blob5f41b4df25bb847706ed9d9ac8900ff6abb52133
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 if vis.lexers then vis.lexers.lexers = {} end
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.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
41 if win.syntax == nil or vis.lexers == nil then return end
42 local lexer = vis.lexers.load(win.syntax, nil, true)
43 if lexer == nil then return end
45 -- TODO: improve heuristic for initial style
46 local viewport = win.viewport
47 if not viewport then return end
48 local horizon_max = win.horizon or 32768
49 local horizon = viewport.start < horizon_max and viewport.start or horizon_max
50 local view_start = viewport.start
51 local lex_start = viewport.start - horizon
52 local token_start = lex_start
53 viewport.start = token_start
54 local data = win.file:content(viewport)
55 local token_styles = lexer._TOKENSTYLES
56 local tokens = lexer:lex(data, 1)
58 for i = 1, #tokens, 2 do
59 local token_end = lex_start + tokens[i+1] - 1
60 if token_end >= view_start then
61 local name = tokens[i]
62 local style = token_styles[name]
63 if style ~= nil then
64 win:style(style, token_start, token_end)
65 end
66 end
67 token_start = token_end
68 end
69 end)
71 local modes = {
72 [vis.modes.NORMAL] = '',
73 [vis.modes.OPERATOR_PENDING] = '',
74 [vis.modes.VISUAL] = 'VISUAL',
75 [vis.modes.VISUAL_LINE] = 'VISUAL-LINE',
76 [vis.modes.INSERT] = 'INSERT',
77 [vis.modes.REPLACE] = 'REPLACE',
80 vis.events.subscribe(vis.events.WIN_STATUS, function(win)
81 local left_parts = {}
82 local right_parts = {}
83 local file = win.file
84 local selection = win.selection
86 local mode = modes[vis.mode]
87 if mode ~= '' and vis.win == win then
88 table.insert(left_parts, mode)
89 end
91 table.insert(left_parts, (file.name or '[No Name]') ..
92 (file.modified and ' [+]' or '') .. (vis.recording and ' @' or ''))
94 if #win.selections > 1 then
95 table.insert(right_parts, selection.number..'/'..#win.selections)
96 end
98 local size = file.size
99 local pos = selection.pos
100 if not pos then pos = 0 end
101 table.insert(right_parts, (size == 0 and "0" or math.ceil(pos/size*100)).."%")
103 if not win.large then
104 local col = selection.col
105 table.insert(right_parts, selection.line..', '..col)
106 if size > 33554432 or col > 65536 then
107 win.large = true
111 local left = ' ' .. table.concat(left_parts, " » ") .. ' '
112 local right = ' ' .. table.concat(right_parts, " « ") .. ' '
113 win:status(left, right);
114 end)
116 -- default plugins
118 require('plugins/filetype')
119 require('plugins/textobject-lexer')
120 require('plugins/digraph')
121 require('plugins/number-inc-dec')
122 require('plugins/complete-word')
123 require('plugins/complete-filename')