Files
dotfiles/vis/.config/vis/visrc.lua

309 lines
8.5 KiB
Lua

require('vis')
local plug = (function()
if not pcall(require, 'plugins/vis-plug') then
os.execute('git clone --quiet https://github.com/erf/vis-plug ' ..
(os.getenv('XDG_CONFIG_HOME') or os.getenv('HOME') .. '/.config')
.. '/vis/plugins/vis-plug')
end
return require('plugins/vis-plug')
end)()
plug = require('plugins/vis-plug')
local plugins = {
{ 'https://gitlab.com/muhq/vis-lspc', alias = 'lspc' },
{ 'lutobler/vis-commentary' },
{ 'https://repo.or.cz/vis-surround.git' },
{ 'peaceant/vis-fzf-mru', file = 'fzf-mru', alias = 'fzfmru' },
{ 'https://gitlab.com/muhq/vis-build' },
{ 'erf/vis-cursors' },
}
plug.init(plugins, true)
vis.ftdetect.filetypes.templ = {
ext = { "%.templ$" },
}
plug.plugins.lspc.message_level = 2
plug.plugins.lspc.ls_map.templ = {
name = 'templ-lsp',
cmd = 'templ lsp',
}
plug.plugins.fzfmru.fzfmru_history = 60
local function open_file(file, cmd)
vis:command((cmd or 'o') .. ' ' .. file)
end
local function nil_or_tonumber(s)
return s and tonumber(s)
end
local function open_file_pos(line, open_cmd)
local iter = string.gmatch(line, '[^:]+')
local file = iter()
local line_num = nil_or_tonumber(iter())
local col = nil_or_tonumber(iter()) or 1
if open_cmd ~= 'e' or vis.win.file ~= file then
open_file(file, open_cmd)
end
if line_num ~= nil then
vis.win.selection:to(line_num, col)
end
end
local function open_file_current_line(open_cmd)
local line = vis.win.file.lines[vis.win.selection.line]
vis:info(line)
open_file_pos(line)
end
local function escape_and_quoted(s)
return "'" .. s:gsub("'", "\\'") .. "'"
end
local function search(cmd, action)
if action == nil then
action = function(out)
open_file_pos(out, 'e')
end
end
if cmd:match('^fzf ') and vis.register ~= '"' then
local reg = string.gsub(vis.registers[vis.register][1], '%z', '')
if reg ~= '' then
cmd = cmd .. ' --query=' .. escape_and_quoted(reg)
end
end
local code, out, err = vis:pipe(cmd, true)
if code == 0 then
action(out)
elseif err ~= nil then
vis:info(err)
elseif code ~= 0 then
vis:info('Program exit code ' .. code)
end
end
local file_slots = {}
local function set_file_slot(num)
local file = vis.win.file.path
if file ~= nil then
file_slots[num] = file
vis:info('File slot [' .. num .. '] updated')
else
vis:info('Window has no file')
end
end
local function open_file_slot(num, open_cmd)
local file = file_slots[num]
if file == nil then
vis:info('File slot [' .. num .. '] empty')
elseif file == vis.win.file.path then
vis:info('File slot [' .. num .. '] is the same file, no file change')
else
vis:info('File slot [' .. num .. '] open')
open_file(file, open_cmd)
end
end
local function fzf_reload(cmd)
local prompt = escape_and_quoted('1. ' .. cmd:match('^%w*') .. '> ')
if not cmd:match('{q}') then
cmd = cmd .. ' {q}'
end
return 'fzf --ansi --bind "start:reload:' .. cmd .. '" --bind "change:reload:' .. cmd .. ' || true"' ..
' --prompt ' .. prompt ..
' --bind "alt-enter:unbind(change,alt-enter)+change-prompt(2. fzf> )+enable-search+clear-query"'
end
local function add_global_mark()
local file = vis.win.file.path
if file ~= nil then
local code, out, err = vis:pipe('vis-menu -p "global mark comment:"', true)
if code == 0 then
local prefix = file .. ':' .. vis.win.selection.line .. ':' .. vis.win.selection.col .. ': '
local line = vis.win.file.lines[vis.win.selection.line]
out = out:gsub('\n$', '')
if out ~= '' then
prefix = prefix .. '(' .. out .. '): '
end
out = io.open(os.getenv('HOME') .. '/.config/vis/global-marks.txt', 'a')
out:write(prefix .. line .. '\n')
out:close()
elseif err ~= nil then
vis:info(err)
elseif code ~= 0 then
vis:info('Program exit code ' .. code)
end
else
vis:info('Save file first')
end
end
local ripgrep = 'rg --column --line-number --color=always --smart-case'
vis.events.subscribe(vis.events.INIT, function()
vis:command('set autoindent')
vis:command_register('search', function(argv, force, win, selection, range)
search(argv[1])
end)
vis:map(vis.modes.NORMAL, '<M-k>', '<vis-window-prev>')
vis:map(vis.modes.NORMAL, '<M-j>', '<vis-window-next>')
vis:map(vis.modes.NORMAL, ' K', '<vis-window-prev>:q<Enter>', 'close previous window')
vis:map(vis.modes.NORMAL, ' J', '<vis-window-next>:q<Enter><vis-window-prev>', 'close next window')
vis:map(vis.modes.NORMAL, ' l[', ':lspc-prev-diagnostic<Enter>')
vis:map(vis.modes.NORMAL, ' l]', ':lspc-next-diagnostic<Enter>')
vis:map(vis.modes.NORMAL, ' l=', ':lspc-format<Enter>')
for num = 1, 9 do
vis:map(vis.modes.NORMAL, ' s' .. num, function() set_file_slot(num) end, 'set file slot ' .. num)
vis:map(vis.modes.NORMAL, ' ' .. num, function() open_file_slot(num, 'e') end, 'open file slot ' .. num)
vis:map(vis.modes.NORMAL, ' h' .. num, function() open_file_slot(num, 'o') end, 'open file slot ' .. num)
vis:map(vis.modes.NORMAL, ' v' .. num, function() open_file_slot(num, 'vsplit') end,
'open file slot ' .. num)
end
vis:map(vis.modes.NORMAL, ' fg', function()
search(fzf_reload(ripgrep))
end, 'fzf: rg')
vis:map(vis.modes.NORMAL, ' hg', function()
search(fzf_reload(ripgrep .. ' -.'))
end, 'fzf: rg with hidden files')
vis:map(vis.modes.NORMAL, ' /', function()
search(fzf_reload(ripgrep .. ' --with-filename {q} ' .. escape_and_quoted(vis.win.file.path)))
end, 'fzf: rg current file')
vis:map(vis.modes.NORMAL, ' ff', function()
search('fd --type f | fzf')
end, 'fzf: files')
vis:map(vis.modes.NORMAL, ' fh', function()
search('fd -H --type f | fzf')
end, 'fzf: files with hidden files')
vis:map(vis.modes.NORMAL, ' df', function()
local path = vis.win.file.path
if path then
local dir = path:match('^.*/')
local arg = escape_and_quoted(dir)
search('fd --type f "" ' .. arg .. ' | fzf')
else
search('fd --type f | fzf')
end
end, 'fzf in file directory')
vis:map(vis.modes.NORMAL, ' dh', function()
local path = vis.win.file.path
if path then
local dir = path:match('^.*/')
local arg = escape_and_quoted(dir)
search('fd -H --type f "" ' .. arg .. ' | fzf')
else
search('fd -H --type f | fzf')
end
end, 'fzf in file directory with hidden files')
vis:map(vis.modes.NORMAL, ' sh', function()
local shell = os.getenv('SHELL')
vis:command('!' .. shell)
end, 'run shell')
vis:map(vis.modes.NORMAL, ' ds', function()
local shell = os.getenv('SHELL')
local path = vis.win.file.path
if path then
local dir = path:match('^.*/')
local arg = escape_and_quoted(dir)
vis:command('!cd ' .. arg .. ' && ' .. shell)
else
vis:command('!' .. shell)
end
end, 'run shell in file directory')
vis:map(vis.modes.NORMAL, ' gf', function()
search('git ls-files | fzf')
end, 'fzf: git files')
vis:map(vis.modes.NORMAL, ' gg', function()
search(fzf_reload('git grep --column --line-number --color=always'))
end, 'fzf: jj grep')
vis:map(vis.modes.NORMAL, ' gl', function()
vis:command('!lazygit')
end, 'lazygit')
vis:map(vis.modes.NORMAL, ' jf', function()
search('jj file list | fzf')
end, 'fzf: jj files')
vis:map(vis.modes.NORMAL, ' jg', function()
search(fzf_reload(ripgrep .. ' {q} $(jj file list | xargs)'))
end, 'fzf: jj grep')
vis:map(vis.modes.NORMAL, ' jl', function()
vis:command('!lazyjj')
end, 'fzf open')
vis:map(vis.modes.NORMAL, ' ', function()
vis:command('fzfmru')
end, 'fzf recent')
vis:map(vis.modes.NORMAL, 'gf', function()
open_file_current_line('o')
end, 'open file from current line (with line and col')
vis:map(vis.modes.NORMAL, ' cd', function()
search('zoxide query -l | fzf', function(path)
vis:command('cd ' .. path)
end)
end, 'fzf change directory')
vis:map(vis.modes.NORMAL, ' yy', function()
vis:command('!yazi')
end, 'yazi')
vis:map(vis.modes.NORMAL, ' y.', function()
local path = vis.win.file.path
if path then
local dir = path:match('^.*/')
local arg = escape_and_quoted(dir)
vis:command('!cd ' .. arg .. ' && yazi')
else
vis:command('!yazi')
end
end, 'yazi in file directory')
vis:map(vis.modes.NORMAL, ' ma', add_global_mark, 'global marks: add')
vis:map(vis.modes.NORMAL, ' mj', function()
search('cat ~/.config/vis/global-marks.txt | fzf --tac')
end, 'global marks: jump')
vis:map(vis.modes.NORMAL, ' me', function()
vis:command('o ~/.config/vis/global-marks.txt')
end, 'global marks: edit')
vis:map(vis.modes.NORMAL, ' td', function()
vis:command('set theme lupan-dark')
end, 'change to dark theme')
vis:map(vis.modes.NORMAL, ' tl', function()
vis:command('set theme lupan-light')
end, 'change to light theme')
end)
vis.events.subscribe(vis.events.WIN_OPEN, function(win)
vis:command('set relativenumber')
end)