2024-07-31 12:05:39 +02:00
|
|
|
require('vis')
|
|
|
|
|
2024-08-20 03:41:17 +02:00
|
|
|
local m = vis.modes
|
2024-08-03 15:51:25 +02:00
|
|
|
local mode_strings = {
|
2024-08-20 03:41:17 +02:00
|
|
|
[m.NORMAL] = 'NORMAL',
|
|
|
|
[m.OPERATOR_PENDING] = 'OPERATOR-PENDING',
|
|
|
|
[m.VISUAL] = 'VISUAL',
|
|
|
|
[m.VISUAL_LINE] = 'VISUAL-LINE',
|
|
|
|
[m.INSERT] = 'INSERT',
|
|
|
|
[m.REPLACE] = 'REPLACE',
|
2024-08-03 15:51:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-20 03:41:17 +02:00
|
|
|
local e = vis.events
|
|
|
|
|
2024-08-26 17:03:12 +02:00
|
|
|
-- init
|
2024-08-20 03:41:17 +02:00
|
|
|
e.subscribe(e.INIT, function()
|
2024-08-26 17:03:12 +02:00
|
|
|
-- load theme
|
|
|
|
require("themes/basic")
|
|
|
|
local lexers = vis.lexers
|
|
|
|
lexers.lexers = {}
|
|
|
|
if lexers.load and not lexers.property then lexers.load("text") end
|
2024-08-20 03:41:17 +02:00
|
|
|
|
2024-08-26 17:03:12 +02:00
|
|
|
-- mappings
|
2024-08-20 03:41:17 +02:00
|
|
|
vis:map(m.NORMAL, 'y', '<vis-register>+<vis-operator-yank>')
|
|
|
|
vis:map(m.VISUAL, 'y', '<vis-register>+<vis-operator-yank>')
|
|
|
|
vis:map(m.NORMAL, 'y', '<vis-register>+<vis-operator-yank>')
|
|
|
|
vis:map(m.VISUAL, 'd', '<vis-register>+<vis-operator-delete>')
|
|
|
|
vis:map(m.VISUAL_LINE, 'd', '<vis-register>+<vis-operator-delete>')
|
|
|
|
vis:map(m.VISUAL_LINE, 'd', '<vis-register>+<vis-operator-delete>')
|
|
|
|
vis:map(m.NORMAL, 'p', '<vis-register>+<vis-put-after>')
|
|
|
|
vis:map(m.VISUAL, 'p', '<vis-register>+<vis-put-after>')
|
|
|
|
vis:map(m.VISUAL_LINE, 'p', '<vis-register>+<vis-put-after>')
|
|
|
|
vis:map(m.NORMAL, 'P', '<vis-register>+<vis-put-before>')
|
|
|
|
vis:map(m.VISUAL, 'P', '<vis-register>+<vis-put-before>')
|
|
|
|
vis:map(m.VISUAL_LINE, 'P', '<vis-register>+<vis-put-before>')
|
|
|
|
|
2024-08-26 17:03:12 +02:00
|
|
|
vis:map(m.NORMAL, 'gA', '<vis-motion-screenline-end><vis-motion-char-prev><vis-append-char-next>')
|
|
|
|
|
|
|
|
vis:map(m.NORMAL, '<C-h>', '<vis-jumplist-prev>');
|
|
|
|
vis:map(m.NORMAL, '<C-l>', '<vis-jumplist-next>');
|
|
|
|
|
2024-08-20 03:41:17 +02:00
|
|
|
vis:map(m.NORMAL, '<C-w>o', function()
|
|
|
|
local unclosed
|
|
|
|
for win in vis:windows() do
|
|
|
|
if win == vis.win then goto continue end
|
|
|
|
if win:close() then goto continue end
|
|
|
|
unclosed = win
|
|
|
|
::continue::
|
|
|
|
end
|
|
|
|
if unclosed then
|
|
|
|
vis.win = unclosed
|
|
|
|
vis:info('No write since last change')
|
|
|
|
end
|
2024-08-26 17:03:12 +02:00
|
|
|
end, 'Close all unfocused windows')
|
2024-08-20 03:41:17 +02:00
|
|
|
|
|
|
|
vis:map(m.NORMAL, ' e', function()
|
2024-08-26 17:03:12 +02:00
|
|
|
local s, f = vis:pipe('find -type f | vis-menu -l 5 -p "pick file"')
|
|
|
|
if s ~= 0 or not f then return end
|
2024-08-20 03:41:17 +02:00
|
|
|
cmd = ('e "%s"'):format(f:sub(1, -2))
|
|
|
|
vis:info(cmd)
|
|
|
|
vis:command(cmd)
|
2024-08-26 17:03:12 +02:00
|
|
|
end, 'pick file to :e')
|
2024-08-20 03:41:17 +02:00
|
|
|
|
|
|
|
vis:map(m.NORMAL, ' o', function()
|
2024-08-26 17:03:12 +02:00
|
|
|
local s, f = vis:pipe('find -type f | vis-menu -l 5 -p "pick file"')
|
|
|
|
if s ~= 0 or not f then return end
|
|
|
|
cmd = ('o "%s"'):format(f:sub(1, -2))
|
2024-08-20 03:41:17 +02:00
|
|
|
vis:info(cmd)
|
|
|
|
vis:command(cmd)
|
2024-08-26 17:03:12 +02:00
|
|
|
end, 'pick file to :o')
|
|
|
|
|
|
|
|
vis:map(m.NORMAL, ' b', function()
|
|
|
|
local wins = {}
|
|
|
|
local i, wi = 0, 0
|
|
|
|
for win in vis:windows() do
|
|
|
|
table.insert(wins, ('%q:%q'):format(i,win.file.name or '[No_Name]'))
|
|
|
|
if win == vis.win then wi = i end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
wins = table.concat(wins, '\n')
|
|
|
|
local s, w = vis:pipe(('printf "%s" | vis-menu -l 5 -p "pick window"'):format(wins))
|
|
|
|
if s ~= 0 or not w then return end
|
|
|
|
local si = tonumber(w:match('^(%d+):.*$') or wi)
|
|
|
|
if si == wi then return end
|
|
|
|
local action = si > wi and '<vis-window-next>' or '<vis-window-prev>'
|
|
|
|
for i=1,math.abs(si-wi) do
|
|
|
|
vis:feedkeys(action)
|
|
|
|
end
|
|
|
|
end, 'pick window to move to')
|
2024-08-20 03:41:17 +02:00
|
|
|
|
|
|
|
vis:map(m.NORMAL, ' g', function()
|
2024-08-26 17:03:12 +02:00
|
|
|
local s, word = vis:pipe(
|
2024-08-20 03:41:17 +02:00
|
|
|
vis.win.file or nil,
|
|
|
|
vis.win.viewport.bytes,
|
2024-08-26 17:03:12 +02:00
|
|
|
'grep -Eo "\\w\\w\\w+" | sort -u | vis-menu -l 5 -p "grep"')
|
|
|
|
if s ~= 0 or not word then return end
|
|
|
|
word = word:sub(1, -2)
|
|
|
|
local s, f = vis:pipe(('grep -n "%s" ** | tr "\\t" " " | vis-menu -l 5 -p ":o"'):format(word))
|
|
|
|
if s ~= 0 or not f then return end
|
2024-08-20 03:41:17 +02:00
|
|
|
local file, lineno, line = f:match("^(.+):(%d+):(.*)$")
|
|
|
|
if file == nil then return end -- TODO report error
|
2024-08-26 17:03:12 +02:00
|
|
|
cmd = ('o "%s"'):format(file)
|
2024-08-20 03:41:17 +02:00
|
|
|
vis:info(cmd)
|
|
|
|
vis:command(cmd)
|
2024-08-26 17:03:12 +02:00
|
|
|
vis.win.selection:to(tonumber(lineno), line:find(word))
|
|
|
|
end, 'grep and pick file to :o')
|
2024-07-31 12:05:39 +02:00
|
|
|
|
2024-08-26 17:03:12 +02:00
|
|
|
-- editor options
|
2024-07-31 12:05:39 +02:00
|
|
|
vis.options.autoindent = true
|
|
|
|
end)
|
|
|
|
|
2024-08-26 17:03:12 +02:00
|
|
|
-- window open
|
2024-08-20 03:41:17 +02:00
|
|
|
e.subscribe(e.WIN_OPEN, function(win)
|
2024-07-31 12:05:39 +02:00
|
|
|
win.options.colorcolumn = 81
|
|
|
|
win.options.relativenumbers = true
|
|
|
|
|
2024-08-03 15:51:25 +02:00
|
|
|
if win.syntax == 'markdown' then
|
2024-08-20 03:41:17 +02:00
|
|
|
win.options.breakat = " ])}_"
|
2024-08-03 15:51:25 +02:00
|
|
|
win.options.expandtab = true
|
|
|
|
win.options.tabwidth = 2
|
|
|
|
win.options.wrapcolumn = 81
|
|
|
|
end
|
2024-07-31 12:05:39 +02:00
|
|
|
|
2024-08-20 03:41:17 +02:00
|
|
|
if win.syntax == 'css' then
|
|
|
|
win.options.expandtab = true
|
|
|
|
win.options.tabwidth = 2
|
|
|
|
end
|
2024-08-03 15:51:25 +02:00
|
|
|
end)
|
|
|
|
|
2024-08-20 03:41:17 +02:00
|
|
|
-- statusline
|
2024-08-26 17:03:12 +02:00
|
|
|
e.subscribe(e.WIN_STATUS, function(win)
|
2024-07-31 12:05:39 +02:00
|
|
|
local left_parts = {}
|
|
|
|
local right_parts = {}
|
|
|
|
local file = win.file
|
|
|
|
local selection = win.selection
|
|
|
|
|
|
|
|
local file_info = (file.name or '[No Name]')..(file.modified and '[+]' or '')
|
|
|
|
|
|
|
|
if vis.win == win then
|
|
|
|
-- mode
|
2024-08-26 17:03:12 +02:00
|
|
|
table.insert(left_parts, mode_strings[vis.mode])
|
2024-07-31 12:05:39 +02:00
|
|
|
|
|
|
|
-- selection
|
|
|
|
table.insert(left_parts, selection.number..'/'..#win.selections)
|
|
|
|
|
|
|
|
-- file info
|
|
|
|
file_info =
|
|
|
|
file_info
|
|
|
|
..':'
|
|
|
|
..(vis.count or '')
|
|
|
|
..(vis.input_queue or '')
|
|
|
|
..(vis.recording and '@' or '')
|
|
|
|
|
2024-08-26 17:09:41 +02:00
|
|
|
-- syntax
|
|
|
|
table.insert(right_parts, win.syntax)
|
|
|
|
|
2024-07-31 12:05:39 +02:00
|
|
|
-- character under cursor
|
|
|
|
table.insert(right_parts,
|
|
|
|
'<'
|
|
|
|
..(string.byte(file:content(selection.pos, 1)) or '0')
|
|
|
|
..'>')
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(left_parts, file_info)
|
|
|
|
|
|
|
|
-- line and column count
|
|
|
|
table.insert(right_parts, #file.lines..'/'..selection.line)
|
|
|
|
table.insert(right_parts, selection.col)
|
|
|
|
|
2024-08-20 03:41:17 +02:00
|
|
|
-- fillchars
|
2024-08-26 17:03:12 +02:00
|
|
|
local left = table.concat(left_parts, ' ')
|
|
|
|
local right = table.concat(right_parts, ' ')
|
2024-08-20 03:41:17 +02:00
|
|
|
|
|
|
|
win:status(table.concat({ left, ('^'):rep(win.width - #left - #right - 2)}, ' '), right)
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- title
|
|
|
|
local modified = false
|
|
|
|
local function set_title(title)
|
2024-08-20 03:57:29 +02:00
|
|
|
os.execute('printf "\\e];vis: '..title..(modified and '[+]' or '')..'\\e"')
|
2024-08-03 15:51:25 +02:00
|
|
|
end
|
2024-08-20 03:41:17 +02:00
|
|
|
|
|
|
|
e.subscribe(e.WIN_OPEN, function(win)
|
2024-08-20 03:57:29 +02:00
|
|
|
set_title(win.file.name or '[No Name]')
|
2024-08-20 03:41:17 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
e.subscribe(e.FILE_SAVE_POST, function(file)
|
|
|
|
modified = false
|
|
|
|
set_title(file.name)
|
|
|
|
end)
|
|
|
|
|
|
|
|
e.subscribe(e.WIN_STATUS, function(win)
|
|
|
|
if not modified and win.file.modified then
|
|
|
|
modified = true
|
2024-08-20 03:57:29 +02:00
|
|
|
set_title(win.file.name or '[No Name]')
|
2024-08-20 03:41:17 +02:00
|
|
|
end
|
|
|
|
end)
|