zettelkasten.nvim/lua/zettelkasten/action.lua
Marty Oehme c9e490e042
Escape file name for file to edit
Use `fnameescape` before trying to edit a file when you open it through
a link.
2022-01-21 17:13:44 +01:00

70 lines
2.2 KiB
Lua

local A = {}
local f = require 'zettelkasten.files'
local l = require 'zettelkasten.link'
local o = require 'zettelkasten.options'
local t = require 'zettelkasten.text'
-- Opens the link passed in in the editor's current buffer.
-- Requires a link object passed in.
function A.open(link)
if not link or not link.ref then return end
local fname = f.get_zettel_by_anchor(link.anchor) or
f.get_zettel_by_ref(link.ref) or link.ref
vim.api.nvim_command(string.format("edit %s", vim.fn.fnameescape(fname)))
end
-- Gets the input at the current buffer cursor and opens it
-- in the current buffer.
-- Takes an optional style of link following to use,
-- superseding the one set in options.
function A.open_selected(style)
style = style or o.link().following
local curpos = vim.api.nvim_win_get_cursor(0)[2]
local links = l.extract_all(t.get_line())
local ln
if style == 'line' then
ln = t.get_next_link_on_line(links, curpos)
elseif style == 'cursor' then
ln = t.get_link_under_cursor(links, curpos)
end
if not ln then return false end
A.open(ln)
return true
end
-- Replaces the current text context with a link to a new zettel.
-- The current context is the visual selection (if called from visual mode)
-- or the (big) word under the cursor if called from any other mode.
function A.make_link(visual)
local selection, start_col
if visual then
selection, start_col = t.get_current_selection()
else
selection, start_col = t.get_current_word()
end
vim.api.nvim_set_current_line(t.replace_text_in_current_line(selection,
l.new(selection),
start_col))
end
-- Opens an index.md file at the zettelkasten root directory whether it
-- exists or not.
function A.open_index_file(name)
local link = {
ref = o.zettel().rootdir .. "/" .. (name or "index") ..
o.zettel().extension
}
A.open(link)
end
return {
open = A.open,
open_selected = A.open_selected,
open_index_file = A.open_index_file,
make_link = A.make_link
}