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-09-13 20:30:01 +02:00
|
|
|
vis:map(m.NORMAL, ' w', function() vis:command('w') end)
|
|
|
|
vis:map(m.NORMAL, ' q', function() vis:command('q') end)
|
|
|
|
vis:map(m.NORMAL, ' Q', function() vis:command('q!') end)
|
2024-08-20 03:41:17 +02:00
|
|
|
|
|
|
|
vis:map(m.NORMAL, ' e', function()
|
2024-09-13 20:30:01 +02:00
|
|
|
local s, f = vis:pipe('find -type f | vis-menu -l 5 -p "Edit file"')
|
2024-08-26 17:03:12 +02:00
|
|
|
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-09-13 20:30:01 +02:00
|
|
|
end, 'Edit file')
|
2024-08-20 03:41:17 +02:00
|
|
|
|
2024-09-13 20:30:01 +02:00
|
|
|
vis:map(m.NORMAL, ' cd', function()
|
|
|
|
local s, f = vis:pipe('find -type d | vis-menu -l 5 -p "Change directory"')
|
2024-08-26 17:03:12 +02:00
|
|
|
if s ~= 0 or not f then return end
|
2024-09-13 20:30:01 +02:00
|
|
|
cmd = ('cd "%s"'):format(f:sub(1, -2))
|
2024-08-20 03:41:17 +02:00
|
|
|
vis:info(cmd)
|
|
|
|
vis:command(cmd)
|
2024-09-13 20:30:01 +02:00
|
|
|
end, 'Change directory')
|
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
|
2024-09-13 20:30:23 +02:00
|
|
|
win.options.numbers = true
|
2024-07-31 12:05:39 +02:00
|
|
|
|
2024-08-03 15:51:25 +02:00
|
|
|
if win.syntax == 'markdown' then
|
2024-09-13 20:30:23 +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
|
|
|
|
|
2024-09-13 20:30:45 +02:00
|
|
|
-- file info
|
|
|
|
table.insert(left_parts,
|
|
|
|
(file.name or '[No Name]')..(file.modified and '[+]' or ''))
|
|
|
|
|
|
|
|
-- selection
|
|
|
|
table.insert(right_parts, selection.number..'/'..#win.selections)
|
2024-07-31 12:05:39 +02:00
|
|
|
|
|
|
|
if vis.win == win then
|
|
|
|
-- mode
|
2024-09-13 20:30:45 +02:00
|
|
|
table.insert(left_parts, 1, mode_strings[vis.mode])
|
2024-07-31 12:05:39 +02:00
|
|
|
|
2024-09-13 20:30:45 +02:00
|
|
|
-- syntax
|
|
|
|
table.insert(left_parts, win.syntax)
|
2024-07-31 12:05:39 +02:00
|
|
|
|
2024-09-13 20:30:45 +02:00
|
|
|
-- input info
|
|
|
|
table.insert(left_parts,
|
|
|
|
'<'
|
2024-07-31 12:05:39 +02:00
|
|
|
..(vis.count or '')
|
|
|
|
..(vis.input_queue or '')
|
|
|
|
..(vis.recording and '@' or '')
|
2024-09-13 20:30:45 +02:00
|
|
|
..'>')
|
2024-08-26 17:09:41 +02:00
|
|
|
|
2024-07-31 12:05:39 +02:00
|
|
|
-- character under cursor
|
|
|
|
table.insert(right_parts,
|
|
|
|
'<'
|
|
|
|
..(string.byte(file:content(selection.pos, 1)) or '0')
|
|
|
|
..'>')
|
|
|
|
|
2024-09-13 20:30:45 +02:00
|
|
|
-- line and column count
|
|
|
|
table.insert(right_parts, #file.lines..'/'..selection.line)
|
|
|
|
table.insert(right_parts, selection.col)
|
|
|
|
end
|
2024-07-31 12:05:39 +02:00
|
|
|
|
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)
|
|
|
|
|
2024-09-13 20:30:55 +02:00
|
|
|
-- set title
|
2024-08-20 03:41:17 +02:00
|
|
|
local modified = false
|
|
|
|
local function set_title(title)
|
2024-09-13 20:30:55 +02:00
|
|
|
os.execute('printf "\\e];'..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)
|
2024-09-13 20:29:20 +02:00
|
|
|
|
|
|
|
-- find root
|
|
|
|
e.subscribe(e.WIN_OPEN, function(win)
|
|
|
|
if not win.file.path then return end
|
|
|
|
local dir = win.file.path
|
|
|
|
local home = os.getenv('HOME')
|
|
|
|
if not dir:find(home) then return end
|
|
|
|
while true do
|
|
|
|
dir = dir:match('^(.+)/[^/]+$') or home
|
|
|
|
local _, find = vis:pipe(('find %s ! -path %s -prune -type d -name .git'):format(dir, dir))
|
|
|
|
if find or dir == home then
|
|
|
|
local cmd = ('cd "%s"'):format(dir)
|
|
|
|
vis:info(cmd)
|
|
|
|
vis:command(cmd)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|