dotfiles

personal dotfiles
git clone anongit@rnpnr.xyz:dotfiles.git
Log | Files | Refs | Feed | Submodules

visrc.lua (4403B)


      1 require('vis')
      2 require('build')
      3 require('macros')
      4 require('set-title')
      5 require('plugins/vis-gpg')
      6 require('plugins/vis-spellcheck')
      7 
      8 local lint = require('plugins/vis-lint')
      9 local gf   = require('goto-ref')
     10 local util = require('util')
     11 local highlight = require('highlight')
     12 highlight.keywords = {
     13 	NOCOMMIT  = 'fore:cyan,underlined,bold,blink',
     14 	FIXME     = 'fore:red,underlined,bold',
     15 	NOTE      = 'fore:green,underlined,bold',
     16 	TODO      = 'fore:magenta,underlined,bold',
     17 	IMPORTANT = 'fore:yellow,underlined,bold',
     18 }
     19 
     20 local tabwidth = 2
     21 
     22 local function min(a, b) if a < b then return a else return b end end
     23 
     24 -- detect matlab with %.m not objective_c
     25 vis.ftdetect.filetypes.matlab = { ext = { "%.m$" } }
     26 assert(table.remove(vis.ftdetect.filetypes.objective_c.ext, 1) == "%.m$")
     27 
     28 -- use smaller tabs for heavily nested matlab classes and latex
     29 vis.ftdetect.filetypes.latex.cmd  = {"set tw " .. tostring(min(tabwidth, 4))}
     30 vis.ftdetect.filetypes.matlab.cmd = {"set tw " .. tostring(min(tabwidth, 4))}
     31 
     32 vis.ftdetect.filetypes.haskell.cmd = { "set tw 4", "set expandtab true" }
     33 lint.fixers["haskell"] = { "hindent --indent-size 4 --sort-imports" }
     34 
     35 lint.fixers["python"] = {} -- {"black -l 80 -q -"}
     36 vis.ftdetect.filetypes.python.cmd = {"set tw " .. tostring(min(tabwidth, 4))}
     37 
     38 vis.ftdetect.filetypes.yaml.cmd = { "set tw 2", "set expandtab true" }
     39 
     40 local ag = function(argv)
     41 	util.message_clear(vis)
     42 	local outstr = ""
     43 	for _, arg in ipairs(argv) do
     44 		local _, out = vis:pipe("ag -Q --column " .. arg)
     45 		if out then
     46 			vis:message(out)
     47 			outstr = outstr .. out
     48 		end
     49 	end
     50 	gf.setup_iterators_from_text(outstr)
     51 end
     52 
     53 local file_search = function(goto_ref)
     54 	local sel  = vis.win.selection
     55 	local data = vis.win.file:content(sel.range)
     56 	vis.mode   = vis.modes.NORMAL
     57 
     58 	if goto_ref then
     59 		local pairs = gf.generate_line_indices(data)
     60 		if pairs and pairs[1] then gf.focus_file_at(table.unpack(pairs[1])) end
     61 	else
     62 		ag({data})
     63 	end
     64 end
     65 
     66 vis.events.subscribe(vis.events.INIT, function()
     67 	vis:command("set theme term")
     68 
     69 	vis.options = { autoindent = true }
     70 
     71 	local m, cmd = vis.modes, util.command
     72 	vis:map(m.NORMAL, " f", "v$:|furigana<Enter><Escape>")
     73 	vis:map(m.NORMAL, " j", "<vis-window-next>")
     74 	vis:map(m.NORMAL, " k", "<vis-window-prev>")
     75 	vis:map(m.NORMAL, "gq", "vip=<Escape>")
     76 	vis:map(m.VISUAL, " s", cmd("|sort"))
     77 
     78 	vis:map(m.NORMAL, "vo", cmd("x/[ \t\r]+$/ d"),
     79 	        "remove whitespace from end of all lines")
     80 
     81 	vis:map(m.NORMAL, " l", function()
     82 		local ui = vis.ui
     83 		if ui.layout == ui.layouts.HORIZONTAL then
     84 			ui.layout = ui.layouts.VERTICAL
     85 		else
     86 			ui.layout = ui.layouts.HORIZONTAL
     87 		end
     88 	end, "swap ui layout")
     89 
     90 	vis:map(m.NORMAL, " i", function()
     91 		local fn = vis.win.file.name
     92 		vis:message("syntax = " .. tostring(vis.win.syntax))
     93 		vis:message("file.name = " .. (fn and fn or "nil"))
     94 	end, "dump info to message window")
     95 
     96 	vis:map(m.VISUAL, "gf", function() file_search(true)  end, 'Goto selected "file:line:[col:]"')
     97 	vis:map(m.VISUAL, "gr", function() file_search(false) end, 'Generate references for selected')
     98 end)
     99 
    100 vis:command_register("ag", function(argv)
    101 	ag(argv)
    102 end, "Search for each literal in argv with the_silver_searcher")
    103 
    104 vis:command_register("cp", function(argv)
    105 	local text = vis.win.file:content(vis.win.selection.range)
    106 	vis:pipe(text, "vis-clipboard --copy")
    107 end, "Copy Selection to Clipboard")
    108 
    109 local extra_word_lists = {}
    110 extra_word_lists['c'] = {
    111 	keyword = {'alignof', 'countof', 'force_inline', 'function', 'global', 'local_persist',
    112 	           'thread_static', 'no_return', 'read_only', 'typeof'},
    113 	-- type = {'Arena', 'str8', ...},
    114 }
    115 
    116 vis.events.subscribe(vis.events.WIN_OPEN, function(win)
    117 	win.options = {
    118 		colorcolumn = 100,
    119 		relativenumbers = true,
    120 		tabwidth = tabwidth,
    121 	}
    122 
    123 	local m, cmd = vis.modes, util.command
    124 	-- pass some args to fmt(1)
    125 	local fmtcmd = ":|fmt -l %d -w 66"
    126 	local fmt = cmd(fmtcmd:format(win.options.tabwidth))
    127 	win:map(m.NORMAL, "=", fmt)
    128 	win:map(m.VISUAL, "=", fmt)
    129 
    130 	if vis.lexers.load and win.syntax and extra_word_lists[win.syntax] then
    131 		local lexer = vis.lexers.load(win.syntax, nil, true)
    132 		if lexer then
    133 			for key, word_list in pairs(extra_word_lists[win.syntax]) do
    134 				lexer:set_word_list(key, word_list, true)
    135 			end
    136 		end
    137 	end
    138 end)
    139 
    140 vis.events.subscribe(vis.events.WIN_CLOSE, function(win)
    141 	local f, e = util.splitext(win.file.name)
    142 	if e == '.tex' then
    143 		vis:command("!texclean " .. f .. e)
    144 	end
    145 end)