aboutsummaryrefslogtreecommitdiff
path: root/vis.lua
blob: 9dbfb7494a4738b049624d38752ce6664d3e9f3d (plain) (blame)
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
-- 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",
	[".au3|.a3x"] = "autoit",
	[".as|.asc"] = "actionscript",
	[".adb|.ads"] = "ada",
	[".g|.g4"] = "antlr",
	[".ans|.inp|.mac"] = "apdl",
	[".apl"] = "apl",
	[".applescript"] = "applescript",
	[".asm|.ASM|.s|.S"] = "asm",
	[".asa|.asp|.hta"] = "asp",
	[".awk"] = "awk",
	[".bat|.cmd"] = "batch",
	[".bib"] = "bibtex",
	[".boo"] = "boo",
	[".cs"] = "csharp",
	[".c|.cc|.C|.h"] = "ansi_c",
	[".cpp|.cxx|.c++|.hh|.hpp|.hxx|.h++"] = "cpp",
	[".ck"] = "chuck",
	[".cmake|.cmake.in|.ctest|.ctest.in"] = "cmake",
	[".coffee"] = "coffeescript",
	[".css"] = "css",
	[".cu|.cuh"] = "cuda",
	[".d|.di"] = "dmd",
	[".dart"] = "dart",
	[".desktop"] = "desktop",
	[".diff|.patch"] = "diff",
	["Dockerfile"] = "dockerfile",
	[".dot"] = "dot",
	[".e|.eif"] = "eiffel",
	[".ex|.exs"] = "elixir",
	[".erl|.hrl"] = "erlang",
	[".dsp"] = "faust",
	[".feature"] = "gherkin",
	[".fs"] = "fsharp",
	[".fish"] = "fish",
	[".forth|.frt|.fs"] = "forth",
	[".f|.for|.ftn|.fpp|.f77|.f90|.f95|.f03|.f08"] = "fortran",
	[".g|.gd|.gi|.gap"] = "gap",
	[".po|.pot"] = "gettext",
	[".glslf|.glslv"] = "glsl",
	[".dem|.plt"] = "gnuplot",
	[".go"] = "go",
	[".groovy|.gvy"] = "groovy",
	[".gtkrc"] = "gtkrc",
	[".hs"] = "haskell",
	[".htm|.html|.shtm|.shtml|.xhtml"] = "html",
	[".icn"] = "icon",
	[".idl|.odl"] = "idl",
	[".inf|.ni"] = "inform",
	[".cfg|.cnf|.inf|.ini|.reg"] = "ini",
	[".io"] = "io_lang",
	[".bsh|.java"] = "java",
	[".js|.jsfl"] = "javascript",
	[".json"] = "json",
	[".jsp"] = "jsp",
	[".bbl|.dtx|.ins|.ltx|.tex|.sty"] = "latex",
	[".less"] = "less",
	[".lily|.ly"] = "lilypond",
	[".ledger|.journal"] = "ledger",
	[".cl|.el|.lisp|.lsp"] = "lisp",
	[".litcoffee"] = "litcoffee",
	[".lua"] = "lua",
	["GNUmakefile|.iface|.mak|.mk|makefile|Makefile"] = "makefile",
	[".md|.markdown"] = "markdown",
	[".moon"] = "moonscript",
	[".n"] = "nemerle",
	[".nim"] = "nim",
	[".nsh|.nsi|.nsis"] = "nsis",
	[".m|.mm|.objc"] = "objective_c",
	[".caml|.ml|.mli|.mll|.mly"] = "caml",
	[".dpk|.dpr|.p|.pas"] = "pascal",
	[".al|.perl|.pl|.pm|.pod"] = "perl",
	[".inc|.php|.php3|.php4|.phtml"] = "php",
	[".p8"] = "pico8",
	[".pike|.pmod"] = "pike",
	["PKGBUILD"] = "pkgbuild",
	[".ps1"] = "powershell",
	[".eps|.ps"] = "ps",
	[".prolog"] = "prolog",
	[".props|.properties"] = "props",
	[".pure"] = "pure",
	[".sc|.py|.pyw"] = "python",
	[".R|.Rout|.Rhistory|.Rt|Rout.save|Rout.fail"] = "rstats",
	[".r|.reb"] = "rebol",
	[".rst"] = "rest",
	[".orx|.rex"] = "rexx",
	[".erb|.rhtml"] = "rhtml",
	[".Rakefile|.rake|.rb|.rbw"] = "ruby",
	[".rs"] = "rust",
	[".sass|.scss"] = "sass",
	[".scala"] = "scala",
	[".sch|.scm"] = "scheme",
	[".sno|.SNO"] = "snobol4",
	[".bash|.bashrc|.bash_profile|.configure|.csh|.sh|.zsh"] = "bash",
	[".changes|.st|.sources"] = "smalltalk",
	[".ddl|.sql"] = "sql",
	[".tcl|.tk"] = "tcl",
	[".texi"] = "texinfo",
	[".toml"] = "toml",
	[".vala"] = "vala",
	[".vcf|.vcard"] = "vcard",
	[".v|.ver"] = "verilog",
	[".vh|.vhd|.vhdl"] = "vhdl",
	[".asa|.bas|.cls|.ctl|.dob|.dsm|.dsr|.frm|.pag|.vb|.vba|.vbs"] = "vb",
	[".wsf"] = "wsf",
	[".dtd|.svg|.xml|.xsd|.xsl|.xslt|.xul"] = "xml",
	[".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