dotfiles

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

visrc.lua (3946B)


      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.extensions.m     = "matlab"
     26 
     27 vis.ftdetect.extensions.odin  = "c"
     28 vis.ftdetect.extensions.slang = "cpp"
     29 
     30 lint.fixers.python = {}
     31 
     32 local ag = function(argv)
     33 	util.message_clear(vis)
     34 	local outstr = ""
     35 	for _, arg in ipairs(argv) do
     36 		local _, out = vis:pipe("ag -Q --column " .. arg)
     37 		if out then
     38 			vis:message(out)
     39 			outstr = outstr .. out
     40 		end
     41 	end
     42 	gf.setup_iterators_from_text(outstr)
     43 end
     44 
     45 local file_search = function(goto_ref)
     46 	local sel  = vis.win.selection
     47 	local data = vis.win.file:content(sel.range)
     48 	vis.mode   = vis.modes.NORMAL
     49 
     50 	if goto_ref then
     51 		local pairs = gf.generate_line_indices(data)
     52 		if pairs and pairs[1] then gf.focus_file_at(table.unpack(pairs[1])) end
     53 	else
     54 		ag({data})
     55 	end
     56 end
     57 
     58 vis.events.subscribe(vis.events.INIT, function()
     59 	vis:command("set theme term")
     60 
     61 	vis.options = { autoindent = true }
     62 
     63 	local m, cmd = vis.modes, util.command
     64 	vis:map(m.NORMAL, " f", "v$:|furigana<Enter><Escape>")
     65 	vis:map(m.NORMAL, " j", "<vis-window-next>")
     66 	vis:map(m.NORMAL, " k", "<vis-window-prev>")
     67 	vis:map(m.NORMAL, "gq", "vip=<Escape>")
     68 	vis:map(m.VISUAL, " s", cmd("|sort"))
     69 
     70 	vis:map(m.NORMAL, "vo", cmd("x/[ \t\r]+$/ d"),
     71 	        "remove whitespace from end of all lines")
     72 
     73 	vis:map(m.NORMAL, " l", function()
     74 		local ui = vis.ui
     75 		if ui.layout == ui.layouts.HORIZONTAL then
     76 			ui.layout = ui.layouts.VERTICAL
     77 		else
     78 			ui.layout = ui.layouts.HORIZONTAL
     79 		end
     80 	end, "swap ui layout")
     81 
     82 	vis:map(m.NORMAL, " i", function()
     83 		local fn = vis.win.file.name
     84 		vis:message("syntax = " .. tostring(vis.win.syntax))
     85 		vis:message("file.name = " .. (fn and fn or "nil"))
     86 	end, "dump info to message window")
     87 
     88 	vis:map(m.VISUAL, "gf", function() file_search(true)  end, 'Goto selected "file:line:[col:]"')
     89 	vis:map(m.VISUAL, "gr", function() file_search(false) end, 'Generate references for selected')
     90 end)
     91 
     92 vis:command_register("ag", function(argv)
     93 	ag(argv)
     94 end, "Search for each literal in argv with the_silver_searcher")
     95 
     96 vis:command_register("cp", function(argv)
     97 	local text = vis.win.file:content(vis.win.selection.range)
     98 	vis:pipe(text, "vis-clipboard --copy")
     99 end, "Copy Selection to Clipboard")
    100 
    101 local extra_word_lists = {}
    102 extra_word_lists['c'] = {
    103 	keyword = {
    104 		'alignof',
    105 		'assert',
    106 		'assume',
    107 		'countof',
    108 		'debugbreak',
    109 		'force_inline',
    110 		'function',
    111 		'global',
    112 		'likely',
    113 		'local_persist',
    114 		'no_return',
    115 		'read_only',
    116 		'static_assert',
    117 		'thread_static',
    118 		'typeof',
    119 		'unlikely',
    120 	},
    121 	-- type = {'Arena', 'str8', ...},
    122 }
    123 
    124 vis.events.subscribe(vis.events.WIN_OPEN, function(win)
    125 	win.options = {
    126 		colorcolumn = 100,
    127 		relativenumbers = true,
    128 		tabwidth = tabwidth,
    129 	}
    130 
    131 	local m, cmd = vis.modes, util.command
    132 	-- pass some args to fmt(1)
    133 	local fmtcmd = ":|fmt -l %d -w 66"
    134 	local fmt = cmd(fmtcmd:format(win.options.tabwidth))
    135 	win:map(m.NORMAL, "=", fmt)
    136 	win:map(m.VISUAL, "=", fmt)
    137 
    138 	if vis.lexers.load and win.syntax and extra_word_lists[win.syntax] then
    139 		local lexer = vis.lexers.load(win.syntax, nil, true)
    140 		if lexer then
    141 			for key, word_list in pairs(extra_word_lists[win.syntax]) do
    142 				lexer:set_word_list(key, word_list, true)
    143 			end
    144 		end
    145 	end
    146 end)
    147 
    148 vis.events.subscribe(vis.events.WIN_CLOSE, function(win)
    149 	local f, e = util.splitext(win.file.name)
    150 	if e == '.tex' then
    151 		vis:command("!texclean " .. f .. e)
    152 	end
    153 end)