vis: also lookup Lua support files relative to the binary location
[vis.git] / visrc.lua
blob66766d37557afaf2394ee2dbc9a239a267c230b0
1 local ok, msg = pcall(function()
2 vis.lexers = {}
3 vis.lexers = require('lexer')
4 end)
6 if not ok then
7 vis:info('WARNING: could not load lexer module, is lpeg installed?')
8 end
10 vis.events = {}
12 vis.motion_new = function(vis, key, motion)
13 local id = vis:motion_register(motion)
14 if id < 0 then
15 return false
16 end
17 local binding = function()
18 vis:motion(id)
19 end
20 vis:map(vis.MODE_NORMAL, key, binding)
21 vis:map(vis.MODE_VISUAL, key, binding)
22 vis:map(vis.MODE_OPERATOR_PENDING, key, binding)
23 return true
24 end
26 vis.textobject_new = function(vis, key, textobject)
27 local id = vis:textobject_register(textobject)
28 if id < 0 then
29 return false
30 end
31 local binding = function()
32 vis:textobject(id)
33 end
34 vis:map(vis.MODE_VISUAL, key, binding)
35 vis:map(vis.MODE_OPERATOR_PENDING, key, binding)
36 return true
37 end
39 vis:textobject_new("ii", function(win, pos)
41 if win.syntax == nil then
42 return pos, pos
43 end
45 local before, after = pos - 4096, pos + 4096;
46 if before < 0 then
47 before = 0
48 end
49 -- TODO make sure we start at a line boundary?
51 local lexer = vis.lexers.load(win.syntax)
52 local data = win.file:content(before, after - before)
53 local tokens = lexer:lex(data)
54 local cur = before
55 -- print(before..", "..pos..", ".. after)
57 for i = 1, #tokens, 2 do
58 local name = tokens[i]
59 local token_next = before + tokens[i+1] - 1
60 -- print(name..": ["..cur..", "..token_next.."] pos: "..pos)
61 if cur <= pos and pos < token_next then
62 return cur, token_next
63 end
64 cur = token_next
65 end
67 return pos, pos
68 end)
70 vis.events.win_open = function(win)
72 local files = {
73 [".1|.2|.3|.4|.5|.6|.7|.8|.9|.1x|.2x|.3x|.4x|.5x|.6x|.7x|.8x|.9x"] = "man",
74 [".au3|.a3x"] = "autoit",
75 [".as|.asc"] = "actionscript",
76 [".adb|.ads"] = "ada",
77 [".g|.g4"] = "antlr",
78 [".ans|.inp|.mac"] = "apdl",
79 [".apl"] = "apl",
80 [".applescript"] = "applescript",
81 [".asm|.ASM|.s|.S"] = "asm",
82 [".asa|.asp|.hta"] = "asp",
83 [".awk"] = "awk",
84 [".bat|.cmd"] = "batch",
85 [".bib"] = "bibtex",
86 [".boo"] = "boo",
87 [".cs"] = "csharp",
88 [".c|.cc|.C"] = "ansi_c",
89 [".cpp|.cxx|.c++|.h|.hh|.hpp|.hxx|.h++"] = "cpp",
90 [".ck"] = "chuck",
91 [".cmake|.cmake.in|.ctest|.ctest.in"] = "cmake",
92 [".coffee"] = "coffeescript",
93 [".css"] = "css",
94 [".cu|.cuh"] = "cuda",
95 [".d|.di"] = "dmd",
96 [".dart"] = "dart",
97 [".desktop"] = "desktop",
98 [".diff|.patch"] = "diff",
99 ["Dockerfile"] = "dockerfile",
100 [".dot"] = "dot",
101 [".e|.eif"] = "eiffel",
102 [".ex|.exs"] = "elixir",
103 [".erl|.hrl"] = "erlang",
104 [".dsp"] = "faust",
105 [".feature"] = "gherkin",
106 [".fs"] = "fsharp",
107 [".fish"] = "fish",
108 [".forth|.frt|.fs"] = "forth",
109 [".f|.for|.ftn|.fpp|.f77|.f90|.f95|.f03|.f08"] = "fortran",
110 [".g|.gd|.gi|.gap"] = "gap",
111 [".po|.pot"] = "gettext",
112 [".glslf|.glslv"] = "glsl",
113 [".dem|.plt"] = "gnuplot",
114 [".go"] = "go",
115 [".groovy|.gvy"] = "groovy",
116 [".gtkrc"] = "gtkrc",
117 [".hs"] = "haskell",
118 [".htm|.html|.shtm|.shtml|.xhtml"] = "html",
119 [".icn"] = "icon",
120 [".idl|.odl"] = "idl",
121 [".inf|.ni"] = "inform",
122 [".cfg|.cnf|.inf|.ini|.reg"] = "ini",
123 [".io"] = "io_lang",
124 [".bsh|.java"] = "java",
125 [".js|.jsfl"] = "javascript",
126 [".json"] = "json",
127 [".jsp"] = "jsp",
128 [".bbl|.dtx|.ins|.ltx|.tex|.sty"] = "latex",
129 [".less"] = "less",
130 [".lily|.ly"] = "lilypond",
131 [".ledger|.journal"] = "ledger",
132 [".cl|.el|.lisp|.lsp"] = "lisp",
133 [".litcoffee"] = "litcoffee",
134 [".lua"] = "lua",
135 ["GNUmakefile|.iface|.mak|.mk|makefile|Makefile"] = "makefile",
136 [".md"] = "markdown",
137 [".moon"] = "moonscript",
138 [".n"] = "nemerle",
139 [".nim"] = "nim",
140 [".nsh|.nsi|.nsis"] = "nsis",
141 [".m|.mm|.objc"] = "objective_c",
142 [".caml|.ml|.mli|.mll|.mly"] = "caml",
143 [".dpk|.dpr|.p|.pas"] = "pascal",
144 [".al|.perl|.pl|.pm|.pod"] = "perl",
145 [".inc|.php|.php3|.php4|.phtml"] = "php",
146 [".p8"] = "pico8",
147 [".pike|.pmod"] = "pike",
148 ["PKGBUILD"] = "pkgbuild",
149 [".ps1"] = "powershell",
150 [".eps|.ps"] = "ps",
151 [".prolog"] = "prolog",
152 [".props|.properties"] = "props",
153 [".pure"] = "pure",
154 [".sc|.py|.pyw"] = "python",
155 [".R|.Rout|.Rhistory|.Rt|Rout.save|Rout.fail"] = "rstats",
156 [".r|.reb"] = "rebol",
157 [".rst"] = "rest",
158 [".orx|.rex"] = "rexx",
159 [".erb|.rhtml"] = "rhtml",
160 [".Rakefile|.rake|.rb|.rbw"] = "ruby",
161 [".rs"] = "rust",
162 [".sass|.scss"] = "sass",
163 [".scala"] = "scala",
164 [".sch|.scm"] = "scheme",
165 [".sno|.SNO"] = "snobol4",
166 [".bash|.bashrc|.bash_profile|.configure|.csh|.sh|.zsh"] = "bash",
167 [".changes|.st|.sources"] = "smalltalk",
168 [".ddl|.sql"] = "sql",
169 [".tcl|.tk"] = "tcl",
170 [".texi"] = "texinfo",
171 [".toml"] = "toml",
172 [".vala"] = "vala",
173 [".vcf|.vcard"] = "vcard",
174 [".v|.ver"] = "verilog",
175 [".vh|.vhd|.vhdl"] = "vhdl",
176 [".asa|.bas|.cls|.ctl|.dob|.dsm|.dsr|.frm|.pag|.vb|.vba|.vbs"] = "vb",
177 [".wsf"] = "wsf",
178 [".dtd|.svg|.xml|.xsd|.xsl|.xslt|.xul"] = "xml",
179 [".xtend"] = "xtend",
180 [".yaml"] = "yaml",
183 local filename = win.file.name
185 if filename ~= nil then
186 -- filename = string.lower(filename)
187 for patterns, lang in pairs(files) do
188 for pattern in string.gmatch(patterns, '[^|]+') do
189 if #filename >= #pattern then
190 local s, e = string.find(filename, pattern, -#pattern, true)
191 if s ~= e and e == #filename then
192 win.syntax = lang
193 return;
200 win.syntax = nil