forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvis.lua
285 lines (261 loc) · 7.35 KB
/
vis.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
-- Vis Lua plugin API standard library
local ok, msg = pcall(function()
vis.lexers = {}
vis.lexers = require('lexer')
end)
if not ok then
vis:info('WARNING: could not load lexer module, is lpeg installed?')
end
vis.events = {}
vis.motion_new = function(vis, key, motion)
local id = vis:motion_register(motion)
if id < 0 then
return false
end
local binding = function()
vis:motion(id)
end
vis:map(vis.MODE_NORMAL, key, binding)
vis:map(vis.MODE_VISUAL, key, binding)
vis:map(vis.MODE_OPERATOR_PENDING, key, binding)
return true
end
vis.textobject_new = function(vis, key, textobject)
local id = vis:textobject_register(textobject)
if id < 0 then
return false
end
local binding = function()
vis:textobject(id)
end
vis:map(vis.MODE_VISUAL, key, binding)
vis:map(vis.MODE_OPERATOR_PENDING, key, binding)
return true
end
vis:textobject_new("ii", function(win, pos)
if win.syntax == nil then
return pos, pos
end
local before, after = pos - 4096, pos + 4096
if before < 0 then
before = 0
end
-- TODO make sure we start at a line boundary?
local lexer = vis.lexers.load(win.syntax)
local data = win.file:content(before, after - before)
local tokens = lexer:lex(data)
local cur = before
-- print(before..", "..pos..", ".. after)
for i = 1, #tokens, 2 do
local name = tokens[i]
local token_next = before + tokens[i+1] - 1
-- print(name..": ["..cur..", "..token_next.."] pos: "..pos)
if cur <= pos and pos < token_next then
return cur, token_next
end
cur = token_next
end
return pos, pos
end)
vis.filetypes = {
[".1|.2|.3|.4|.5|.6|.7|.8|.9|.1x|.2x|.3x|.4x|.5x|.6x|.7x|.8x|.9x"] = "man",
[".adb|.ads"] = "ada",
[".al|.perl|.pl|.pm|.pod"] = "perl",
[".ans|.inp|.mac"] = "apdl",
[".apl"] = "apl",
[".applescript"] = "applescript",
[".asa|.asp|.hta"] = "asp",
[".asa|.bas|.cls|.ctl|.dob|.dsm|.dsr|.frm|.pag|.vb|.vba|.vbs"] = "vb",
[".as|.asc"] = "actionscript",
[".asm|.ASM|.s|.S"] = "asm",
[".automount|.device|.mount|.path|.scope|.service|.slice|.socket|.swap|.target|.timer"] = "systemd",
[".au3|.a3x"] = "autoit",
[".awk"] = "awk",
[".bash|.bashrc|.bash_profile|.configure|.csh|.sh|.zsh"] = "bash",
[".bat|.cmd"] = "batch",
[".bbl|.dtx|.ins|.ltx|.tex|.sty"] = "latex",
[".bib"] = "bibtex",
[".boo"] = "boo",
[".bsh|.java"] = "java",
[".caml|.ml|.mli|.mll|.mly"] = "caml",
[".c|.C|.h"] = "ansi_c",
[".cfg|.cnf|.inf|.ini|.reg"] = "ini",
[".changes|.st|.sources"] = "smalltalk",
[".ck"] = "chuck",
[".cl|.el|.lisp|.lsp"] = "lisp",
[".cmake|.cmake.in|.ctest|.ctest.in"] = "cmake",
[".coffee"] = "coffeescript",
[".cpp|.cxx|.c++|.cc|.hh|.hpp|.hxx|.h++"] = "cpp",
[".cr"] = "crystal",
[".cs"] = "csharp",
[".css"] = "css",
[".cu|.cuh"] = "cuda",
[".dart"] = "dart",
[".d|.di"] = "dmd",
[".ddl|.sql"] = "sql",
[".dem|.plt"] = "gnuplot",
[".desktop"] = "desktop",
[".diff|.patch"] = "diff",
["Dockerfile"] = "dockerfile",
[".dot"] = "dot",
[".dpk|.dpr|.p|.pas"] = "pascal",
[".dsp"] = "faust",
[".dtd|.svg|.xml|.xsd|.xsl|.xslt|.xul"] = "xml",
[".e|.eif"] = "eiffel",
[".eps|.ps"] = "ps",
[".erb|.rhtml"] = "rhtml",
[".erl|.hrl"] = "erlang",
[".ex|.exs"] = "elixir",
[".feature"] = "gherkin",
[".f|.for|.ftn|.fpp|.f77|.f90|.f95|.f03|.f08"] = "fortran",
[".fish"] = "fish",
[".forth|.frt|.fs"] = "forth",
[".fs"] = "fsharp",
["fstab"] = "fstab",
[".g|.g4"] = "antlr",
[".g|.gd|.gi|.gap"] = "gap",
[".glslf|.glslv"] = "glsl",
["GNUmakefile|.iface|.mak|.mk|makefile|Makefile"] = "makefile",
[".go"] = "go",
[".groovy|.gvy"] = "groovy",
["group|gshadow|passwd|shadow"] = "dsv",
[".gtkrc"] = "gtkrc",
[".hs"] = "haskell",
[".htm|.html|.shtm|.shtml|.xhtml"] = "html",
[".icn"] = "icon",
[".idl|.odl"] = "idl",
[".inc|.php|.php3|.php4|.phtml"] = "php",
[".inf|.ni"] = "inform",
[".io"] = "io_lang",
[".js|.jsfl"] = "javascript",
[".json"] = "json",
[".jsp"] = "jsp",
[".ledger|.journal"] = "ledger",
[".less"] = "less",
[".lily|.ly"] = "lilypond",
[".link|.network|.netdev"] = "networkd",
[".litcoffee"] = "litcoffee",
[".lua"] = "lua",
[".md|.markdown"] = "markdown",
[".m|.mm|.objc"] = "objective_c",
[".moon"] = "moonscript",
[".nim"] = "nim",
[".n"] = "nemerle",
[".nsh|.nsi|.nsis"] = "nsis",
[".orx|.rex"] = "rexx",
[".p8"] = "pico8",
[".pike|.pmod"] = "pike",
["PKGBUILD"] = "pkgbuild",
[".po|.pot"] = "gettext",
[".prolog"] = "prolog",
[".props|.properties"] = "props",
[".ps1"] = "powershell",
[".pure"] = "pure",
[".Rakefile|.rake|.rb|.rbw"] = "ruby",
[".r|.reb"] = "rebol",
[".R|.Rout|.Rhistory|.Rt|Rout.save|Rout.fail"] = "rstats",
[".rs"] = "rust",
[".rst"] = "rest",
[".sass|.scss"] = "sass",
[".scala"] = "scala",
[".sch|.scm"] = "scheme",
[".sc|.py|.pyw"] = "python",
[".sno|.SNO"] = "snobol4",
[".tcl|.tk"] = "tcl",
[".texi"] = "texinfo",
[".toml"] = "toml",
[".vala"] = "vala",
[".vcf|.vcard"] = "vcard",
[".vh|.vhd|.vhdl"] = "vhdl",
[".v|.ver"] = "verilog",
[".wsf"] = "wsf",
[".xtend"] = "xtend",
[".yaml"] = "yaml",
}
vis.filetype_detect = function(win)
local filename = win.file.name
if filename ~= nil then
-- filename = string.lower(filename)
for patterns, lang in pairs(vis.filetypes) do
for pattern in string.gmatch(patterns, '[^|]+') do
if #filename >= #pattern then
local s, e = string.find(filename, pattern, -#pattern, true)
if s ~= e and e == #filename then
win.syntax = lang
return
end
end
end
end
end
win.syntax = nil
end
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)
if name == nil then
return true
end
local lexers = vis.lexers
if lexers == nil then
return false
end
local lexer = lexers.load(name)
if not lexer 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)
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)
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
local horizon_max = 32768
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