configuration/config/vis/visrc.lua

233 lines
6.7 KiB
Lua
Raw Normal View History

2017-10-04 15:11:16 +02:00
-- load standard vis module, providing parts of the Lua API
2020-03-14 13:37:56 +01:00
-- get inspiration from: https://src.adamsgaard.dk/dotfiles/file/.config/vis/visrc.lua.html
2017-10-04 15:11:16 +02:00
require('vis')
require('plugins/complete-word')
require('plugins/myfiletype')
2024-08-02 11:37:09 +02:00
require('plugins/editorconfig')
2017-10-04 15:11:16 +02:00
vis.events.subscribe(vis.events.INIT, function()
-- Your global configuration options
vis:command('set escdelay 0')
vis:command('set autoindent on')
2024-08-02 11:37:09 +02:00
vis:command('set theme base-16')
2017-10-04 15:11:16 +02:00
end)
vis.events.subscribe(vis.events.WIN_OPEN, function(win)
2017-11-29 17:29:06 +01:00
-- Your per window configuration options
2024-08-02 11:37:09 +02:00
vis:command('set tabwidth 4')
vis:command('set showtabs on')
2017-10-04 15:11:16 +02:00
vis:command('set number')
end)
2024-08-02 11:37:09 +02:00
vis.events.subscribe(vis.events.WIN_HIGHLIGHT, function(win)
if win.file.path and win.file.path:match('.*%.tsx') then
win:set_syntax('javascript')
2024-07-24 08:35:41 +02:00
end
end)
2020-08-20 15:55:09 +02:00
vis:map(vis.modes.NORMAL, ";;", "<vis-window-next>")
2024-08-02 11:37:09 +02:00
vis:map(vis.modes.NORMAL, '<C-o>', '<vis-window-next>')
--------------------------------------------------------------------------------
-- Activate open-file-on-enter mode in this window
vis:map(vis.modes.NORMAL, ";e", function()
vis.win:map(vis.modes.NORMAL, "<Enter>", function()
local line = vis.win.file.lines[vis.win.selection.line]
local filename = string.find(line, ':')
local linenumber = string.find(line, ':', filename + 1)
local column = string.find(line, ':', linenumber + 1)
vis:command(string.format('open "%s"', string.sub(line, 0, filename - 1)))
vis.win.selection:to(tonumber(string.sub(line, filename + 1, linenumber - 1)), tonumber(string.sub(line, linenumber + 1, column - 1)))
end)
end)
2020-08-20 15:55:09 +02:00
--------------------------------------------------------------------------------
-- Escape removes counts
2019-12-07 00:06:27 +01:00
vis:map(vis.modes.NORMAL, "<Escape>", function()
if vis.count then
vis.count = nil
else
2020-04-15 12:36:18 +02:00
vis:feedkeys("<vis-selections-remove-all>")
2019-12-07 00:06:27 +01:00
end
end)
2024-07-24 08:35:41 +02:00
--------------------------------------------------------------------------------
2020-08-20 15:55:09 +02:00
-- Fuzzy search filenames to open files
vis:command_register("skim", function(argv, force, cur_win, selection, range)
local out = io.popen("sk"):read()
2017-10-04 15:11:16 +02:00
if out then
2020-08-20 15:55:09 +02:00
vis:command(string.format(argv[1]..' "%s"', out))
2017-11-17 17:37:24 +01:00
vis:feedkeys("<vis-redraw>")
2017-10-04 15:11:16 +02:00
end
2019-12-07 00:06:27 +01:00
end, 'fuzzy file search')
2017-11-17 17:37:24 +01:00
vis:map(vis.modes.NORMAL, ";l", function()
2020-08-20 15:55:09 +02:00
vis:command('skim open')
2017-11-17 17:37:24 +01:00
end)
vis:map(vis.modes.NORMAL, ";o", function()
2020-08-20 15:55:09 +02:00
vis:command('skim e')
end)
vis:map(vis.modes.NORMAL, ";v", function()
vis:command('skim vsplit')
2017-10-04 15:11:16 +02:00
end)
2017-10-20 10:00:58 +02:00
2020-08-20 15:55:09 +02:00
--------------------------------------------------------------------------------
-- Fuzzy search lines to copy them
2020-03-14 13:37:56 +01:00
vis:map(vis.modes.NORMAL, ";r", function()
2022-07-12 11:27:08 +02:00
local choice = io.popen('tac < ' .. vis.win.file.path .. ' | uniq | sk'):read()
2020-03-14 13:37:56 +01:00
if choice then
local line = vis.win.selection.line
table.insert(vis.win.file.lines, line + 1, choice)
vis.win.selection:to(line + 1, 0)
end
2021-09-22 09:09:00 +02:00
vis:feedkeys("<vis-redraw>f€l")
2020-03-14 13:37:56 +01:00
end, 'fuzzy line copy')
2020-08-20 15:55:09 +02:00
--------------------------------------------------------------------------------
-- Fuzzy search lines to open files
vis:command_register("rg", function(argv, force, cur_win, selection, range)
2024-08-02 11:37:09 +02:00
local out = io.popen("sk --ansi -i -c 'rg --color=always --line-number --column --colors column:fg:green "..'"{}"'.."'"):read()
2020-08-20 15:55:09 +02:00
if out then
local filename = string.find(out, ':')
local linenumber = string.find(out, ':', filename + 1)
2024-08-02 11:37:09 +02:00
local column = string.find(out, ':', linenumber + 1)
2020-08-20 15:55:09 +02:00
vis:command(string.format(argv[1]..' "%s"', string.sub(out, 0, filename - 1)))
2024-08-02 11:37:09 +02:00
vis.win.selection:to(tonumber(string.sub(out, filename + 1, linenumber - 1)), tonumber(string.sub(out, linenumber + 1, column - 1)))
2020-08-20 15:55:09 +02:00
vis:feedkeys("<vis-redraw>")
end
end, 'fuzzy grep open')
vis:map(vis.modes.NORMAL, ";g", function()
vis:command('rg open')
end)
vis:map(vis.modes.NORMAL, ";G", function()
vis:command('rg e')
end)
--------------------------------------------------------------------------------
-- Some interactive programs
interactives = {
2020-03-14 13:37:56 +01:00
["python"] = "!python -i ",
2020-10-16 10:35:17 +02:00
["haskell"] = "!stack ghci ",
["lithaskell"] = "!stack ghci ",
2020-03-14 13:37:56 +01:00
["latex"] = "!tectonic ",
2022-01-04 00:24:15 +01:00
["lua"] = "!luajit -i ",
}
2019-06-03 10:22:56 +02:00
vis:map(vis.modes.NORMAL, ";i", function()
local command = interactives[vis.win.syntax]
if command then
2020-03-14 13:37:56 +01:00
vis:command(command.."'"..vis.win.file.name.."'")
2018-06-28 13:46:23 +02:00
end
end)
2020-08-20 15:55:09 +02:00
vis:map(vis.modes.NORMAL, ";s", function()
vis:command("!sent '"..vis.win.file.path.."'")
end)
--------------------------------------------------------------------------------
-- Swap between light and dark themes
vis:map(vis.modes.NORMAL, ";w", function()
vis:command('set theme light-16')
end)
vis:map(vis.modes.NORMAL, ";b", function()
vis:command('set theme dark-16')
end)
2020-08-20 15:55:09 +02:00
--------------------------------------------------------------------------------
-- Backspace removes 4 spaces if need be
vis:map(vis.modes.INSERT, '<Backspace>', function()
local tabwidth = 4
local single_selection = false
for selection in vis.win:selections_iterator() do
if single_selection then
single_selection = false
break
end
single_selection = true
end
local to_stop = (vis.win.selection.col - 1) % tabwidth
if to_stop == 0 then
to_stop = tabwidth
end
if single_selection and to_stop > 1 and vis.win.file:content(vis.win.selection.pos - to_stop, to_stop) == string.rep(' ', to_stop) then
vis:feedkeys(string.rep('<vis-delete-char-prev>', to_stop))
else
vis:feedkeys('<vis-delete-char-prev>')
end
end)
2020-08-20 15:55:09 +02:00
--------------------------------------------------------------------------------
-- Hardwrapping
vis:map(vis.modes.VISUAL, ';a', function()
if vis.count then
vis:command(string.format(':|par %d', vis.count))
2019-12-07 00:07:11 +01:00
vis.count = nil
else
vis:command(':|par')
end
end)
vis:map(vis.modes.NORMAL, ';a', function()
if vis.count then
vis:command(string.format(':1,$|mreflow %d', vis.count))
vis.count = nil
else
vis:command(':1,$|mreflow')
end
end)
2020-08-20 15:55:09 +02:00
--------------------------------------------------------------------------------
-- Strip trailing spaces
vis:command_register("sts", function(argv, force, win, selection, range)
local lines = win.file.lines
for index=1, #lines do
lines[index] = lines[index]:gsub("%s+$", "")
end
return true
end, "Strip line trailing spaces")
2024-08-02 11:37:09 +02:00
--------------------------------------------------------------------------------
-- Git
git_commands = {
["add"] = function(argv, force, win, selection, range)
local output = io.popen('git add ' .. vis.win.file.path):read()
if (output) then
vis:message(output)
end
end,
["status"] = function(argv, force, win, selection, range)
local output = io.popen('git status'):read()
if (output) then
vis:message(output)
end
end,
}
vis:command_register("git", function(argv, force, win, selection, range)
local command = git_commands[argv[1]]
if (command) then
command(argv, force, win, selection, range)
else
vis:message("Unknown git command")
end
end, "Git commands")