1
0
Fork 0

Update vis config

main
urosm 2024-10-23 18:57:09 +02:00
parent 099fa0751c
commit 00f86727c9
3 changed files with 138 additions and 64 deletions

View File

@ -6,43 +6,33 @@ local P, S, B = lpeg.P, lpeg.S, lpeg.B
local lex = lexer.new(..., {no_user_word_lists = true})
local md = '.markdown'
local ws = lex:get_rule('whitespace')
local nl = P('\n')
local bl = nl * nl
local nws = lexer.any - lexer.space
-- Heading.
lex:add_rule('heading',
lex:tag(lexer.COMMENT, lexer.starts_line(P('#')^-6)) *
P(' ') *
lex:tag(lexer.HEADING, lexer.to_eol(nws) *
#bl))
lex:tag(lexer.HEADING, lexer.to_eol(lexer.any) * #P('\n\n')))
-- List.
lex:add_rule('list',
lex:tag(lexer.COMMENT, lexer.starts_line(P(S('-*') * ' ') + P('(@' * P(lexer.alnum + S('-_'))^0 * ')' + S(' \n')), true)))
-- Blockquote.
lex:add_rule('blockquote',
lex:add_rule('block_quote',
lex:tag(lexer.COMMENT,
lexer.starts_line(P('>' + P(' '))^1)))
lexer.starts_line(P('>' + P(' '))^1, true)))
-- Horizontal rule.
lex:add_rule('hr',
lex:tag(lexer.COMMENT, B(bl) * S('*-_')^3 * #bl))
lex:tag(lexer.COMMENT, B('\n\n') * S('*-_')^3 * #P('\n\n')))
-- Native div.
lex:add_rule("native_div",
lex:tag(lexer.COMMENT, lexer.starts_line(lexer.to_eol(P(':')^3))))
-- Code block.
local code_line = lexer.starts_line(
(B(' ') + B('\t')) * lexer.to_eol(), true)
local code_block = lexer.range(lexer.starts_line('```', true), lexer.starts_line(P('```'))) +
lexer.range(lexer.starts_line('~~~', true), lexer.starts_line(P('~~~')))
local code_inline = lpeg.Cmt(lpeg.C(P('`')^1), function(input, index, bt)
local _, e = input:find('[^`]' .. bt .. '%f[^`]', index)
return (e or #input) + 1
end)
lex:add_rule('block_code', lex:tag(lexer.CODE, code_line + code_block))
lex:add_rule("block_code",
lex:tag(lexer.COMMENT,
lexer.starts_line('~~~' + P('```'))))
-- Escape.
lex:add_rule('escape', lex:tag(lexer.DEFAULT, P('\\') * 1))
@ -87,7 +77,7 @@ local function flanked_range(s, not_inword)
local fl_char = lexer.any - s - lexer.space
local left_fl = B(punct_space - s) * s * #fl_char + s * #(fl_char - lexer.punct)
local right_fl = B(lexer.punct) * s * #(punct_space - s) + B(fl_char) * s
return left_fl * (lexer.any - bl - (not_inword and s * #punct_space or s))^0 * right_fl
return left_fl * (lexer.any - P('\n\n') - (not_inword and s * #punct_space or s))^0 * right_fl
end
local asterisk_strong = flanked_range('**')
@ -104,15 +94,15 @@ local html_start_rule = lexer.starts_line(P(' ')^-3) *
#P('<') *
html:get_rule('tag') +
html:get_rule('comment')
local html_end_rule = #bl * ws
local html_end_rule = #P('\n\n') * lexer.space
lex:embed(html, html_start_rule, html_end_rule)
-- Embedded YAML.
local yaml = lexer.load('yaml')
local doc_bounds_rule = yaml:get_rule('doc_bounds')
local hr_rule = lex:get_rule('hr')
local yaml_start_rule = doc_bounds_rule - hr_rule
local yaml_end_rule = doc_bounds_rule - hr_rule
local yaml_start_rule = doc_bounds_rule * P('\n') - hr_rule
local yaml_end_rule = doc_bounds_rule * P('\n') - hr_rule
lex:embed(yaml, yaml_start_rule, yaml_end_rule)
return lex

View File

@ -42,7 +42,7 @@ lexers.STYLE_CURSOR_LINE = 'underlined'
lexers.STYLE_COLOR_COLUMN = 'fore:black,back:blue'
lexers.STYLE_SELECTION = 'fore:black,back:white'
lexers.STYLE_STATUS = 'fore:blue'
lexers.STYLE_STATUS_FOCUSED = 'fore:blue'
lexers.STYLE_STATUS_FOCUSED = 'fore:magenta'
lexers.STYLE_SEPARATOR = 'fore:blue'
lexers.STYLE_INFO = 'fore:red'
lexers.STYLE_EOF = 'fore:blue'

View File

@ -15,40 +15,66 @@ local e = vis.events
-- init
e.subscribe(e.INIT, function()
-- load theme
require("themes/basic")
local lexers = vis.lexers
lexers.lexers = {}
if lexers.load and not lexers.property then lexers.load("text") end
require('themes/basic')
-- mappings
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>')
if os.getenv('WAYLAND_DISPLAY') then
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>')
end
vis:map(m.NORMAL, 'f', '<vis-selections-save><vis-motion-to-right>')
vis:map(m.OPERATOR_PENDING, 'f', '<vis-motion-to-right>')
vis:map(m.VISUAL, 'f', '<vis-motion-to-right>')
vis:map(m.NORMAL, 'F', '<vis-selections-save><vis-motion-to-left>')
vis:map(m.OPERATOR_PENDING, 'F', '<vis-motion-to-left>')
vis:map(m.VISUAL, 'F', '<vis-motion-to-left>')
vis:map(m.NORMAL, 't', '<vis-selections-save><vis-motion-till-right>')
vis:map(m.OPERATOR_PENDING, 't', '<vis-motion-till-right>')
vis:map(m.VISUAL, 't', '<vis-motion-till-right>')
vis:map(m.NORMAL, 'T', '<vis-selections-save><vis-motion-till-left>')
vis:map(m.OPERATOR_PENDING, 'T', '<vis-motion-till-left>')
vis:map(m.VISUAL, 'T', '<vis-motion-till-left>')
vis:map(m.NORMAL, '?', '<vis-selections-save><vis-search-backward>')
vis:map(m.NORMAL, '/', '<vis-selections-save><vis-search-forward>')
vis:map(m.NORMAL, 'zz', '<vis-window-redraw-bottom><vis-window-redraw-center>') -- fixes a bug with `zz`
vis:map(m.NORMAL, ' w', function() vis:command('w') end)
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)
vis:map(m.NORMAL, ' e', function()
local s, f = vis:pipe('find -type f | vis-menu -l 5 -p "Edit file"')
if s ~= 0 or not f then return end
local s, f = vis:pipe('find -name .git -prune -o -type f | vis-menu -l 5 -p "Edit file"')
if s ~= 0 or f == nil then return end
cmd = ('e "%s"'):format(f:sub(1, -2))
vis:info(cmd)
vis:command(cmd)
end, 'Edit file')
vis:map(m.NORMAL, ' o', function()
local s, f = vis:pipe('find -name .git -prune -o -type f | vis-menu -l 5 -p "Open file"')
if s ~= 0 or f == nil then return end
cmd = ('o "%s"'):format(f:sub(1, -2))
vis:info(cmd)
vis:command(cmd)
end, 'Open file')
vis:map(m.NORMAL, ' cd', function()
local s, f = vis:pipe('find -type d | vis-menu -l 5 -p "Change directory"')
if s ~= 0 or not f then return end
local s, f = vis:pipe('find -name .git -prune -o -type d | vis-menu -l 5 -p "Change directory"')
if s ~= 0 or f == nil then return end
cmd = ('cd "%s"'):format(f:sub(1, -2))
vis:info(cmd)
vis:command(cmd)
@ -64,10 +90,10 @@ e.subscribe(e.WIN_OPEN, function(win)
win.options.numbers = true
if win.syntax == 'markdown' then
win.options.breakat = " ,]_"
win.options.breakat = ' ,]_'
win.options.expandtab = true
win.options.tabwidth = 2
win.options.wrapcolumn = 81
win.options.wrapcolumn = 80
end
if win.syntax == 'css' then
@ -108,7 +134,7 @@ e.subscribe(e.WIN_STATUS, function(win)
-- character under cursor
table.insert(right_parts,
'<'
..(string.byte(file:content(selection.pos, 1)) or '0')
..(file:content(selection and selection.pos or 0, 1):byte() or 'nil')
..'>')
-- line and column count
@ -125,22 +151,80 @@ end)
-- set title
local modified = false
local function set_title(title)
os.execute('printf "\\e];'..title..(modified and '[+]' or '')..'\\e"')
local function set_title(name)
os.execute(('printf "\\e];%s%s\\e"'):format(name, modified and '[+]' or ''))
end
e.subscribe(e.WIN_OPEN, function(win)
set_title(win.file.name or '[No Name]')
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
if os.getenv('TERM') ~= 'linux' then
e.subscribe(e.WIN_OPEN, function(win)
set_title(win.file.name or '[No Name]')
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
set_title(win.file.name or '[No Name]')
end
end)
end
-- cursor position
local cursors_path = os.getenv('XDG_CACHE_HOME')..'/vis-cursors'
local cursors = {}
local files = {}
local function get_cursors()
local f = io.open(cursors_path)
if f == nil then return end
files = {}
for l in f:lines() do
local path, pos = l:match('(.+):(%d+)')
cursors[path] = pos
table.insert(files, path)
end
f:close()
end
e.subscribe(e.INIT, get_cursors)
e.subscribe(e.WIN_OPEN, function(win)
if win.file == nil or win.file.path == nil then return end
if win.file.path:match('%.git/COMMIT_EDITMSG$') then return end
if win.file.path:match('^/tmp/') then return end
local pos = cursors[win.file.path]
if pos == nil then
cursors[win.file.path] = win.selection.pos
return
end
win.selection.pos = tonumber(pos)
vis:feedkeys('zz')
end)
e.subscribe(e.WIN_CLOSE, function(win)
if win.file == nil or win.file.path == nil then return end
get_cursors()
for i, path in ipairs(files) do
if path == win.file.path then table.remove(files, i) end
end
if win.selection.pos == 0 then return end
table.insert(files, 1, win.file.path)
cursors[win.file.path] = win.selection.pos
end)
e.subscribe(e.QUIT, function()
local f = io.open(cursors_path, 'w+')
if f == nil then return end
local buf = {}
for i, path in ipairs(files) do
table.insert(buf, ('%s:%d'):format(path, cursors[path]))
if i > 100 then break end -- remember only 100
end
local out = table.concat(buf, '\n')
f:write(out)
f:close()
end)