vis: add note inserts empty title, current time and file

This commit is contained in:
2025-09-30 22:37:52 +02:00
parent 869482ba91
commit 0874c4ad9a

View File

@@ -241,6 +241,39 @@ local function close_next_win()
end
end
local function ensure_last_line_empty()
local size = vis.win.file.size
if size > 0 then
if vis.win.file:content(size - 1, size) ~= '\n' then
vis.win.file:insert(size, '\n')
end
local lines = vis.win.file.lines
if string.find(lines[#lines], '%S') then
lines[#lines + 1] = ''
end
end
end
local function add_note()
local path = vis.win.file.path
local line = path and path .. ':' .. vis.win.selection.line .. ':' .. vis.win.selection.col .. ': ' .. vis.win.file.lines[vis.win.selection.line]
vis:command('o ~/notes/notes.md')
ensure_last_line_empty()
local lines = vis.win.file.lines
lines[#lines + 1] = '## '
local pos = vis.win.file.size - 1
local code, date, err = vis:pipe("date '+%F %T'")
if code ~= 0 then
vis:message(err)
else
lines[#lines + 1] = '* time: ' .. date:gsub('%s+$', '')
end
if line then
lines[#lines + 1] = '* line: ' .. line
end
vis.win.selection.pos = pos
end
vis.events.subscribe(vis.events.INIT, function()
vis:command('set autoindent')
@@ -346,9 +379,7 @@ vis.events.subscribe(vis.events.INIT, function()
vis:command('o ~/.config/vis/global-marks.txt')
end, 'global marks: edit')
vis:map(vis.modes.NORMAL, ' n', function()
vis:command('o ~/notes/notes.md')
end, 'add note')
vis:map(vis.modes.NORMAL, ' n', add_note)
vis:map(vis.modes.NORMAL, ' N', function()
search(fzf_reload(ripgrep .. ' --with-filename {q} ~/notes'))