2020-10-31 13:17:26 +00:00
|
|
|
local A = {}
|
|
|
|
|
2020-11-02 20:51:14 +00:00
|
|
|
local o = require 'zettelkasten.options'
|
|
|
|
|
|
|
|
local parsers = {markdown = "%[.-%]%((.-)%)", wiki = "%[%[(.+)|?.-%]%]"}
|
|
|
|
|
2020-10-31 13:17:26 +00:00
|
|
|
-- Extracts a file name from a link and opens the corresponding file
|
|
|
|
-- in the current buffer.
|
|
|
|
-- Takes an optional input parameter
|
|
|
|
function A.open(input)
|
|
|
|
local fname = A.extract_link(input)
|
|
|
|
if not fname then return end
|
|
|
|
-- TODO follow: go to anchor, fall back to filename
|
|
|
|
vim.api.nvim_command(string.format("edit %s", fname))
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Gets the input at the current buffer cursor and opens it
|
|
|
|
-- in the current buffer.
|
2020-11-02 20:51:14 +00:00
|
|
|
-- Takes an optional style of link following to use,
|
|
|
|
-- superseding the one set in options.
|
|
|
|
function A.open_selected(style)
|
|
|
|
local style = style or o.zettel().link_following
|
|
|
|
if style == 'line' then
|
|
|
|
A.open(A.get_next_link_on_line())
|
|
|
|
elseif style == 'cursor' then
|
|
|
|
A.open(A.get_link_under_cursor())
|
|
|
|
end
|
2020-10-31 13:17:26 +00:00
|
|
|
end
|
|
|
|
|
2020-11-02 20:51:14 +00:00
|
|
|
-- Return only the link reference portion of a markdown/wiki style link.
|
|
|
|
-- For example, for a markdown link [my text](my-link.md)
|
|
|
|
-- it would only return my-link.md
|
2020-11-01 21:18:40 +00:00
|
|
|
function A.extract_link(input)
|
|
|
|
if not input then return end
|
2020-11-02 20:51:14 +00:00
|
|
|
for _, parser in pairs(parsers) do return input:match(parser) end
|
|
|
|
return
|
2020-11-01 21:18:40 +00:00
|
|
|
end
|
|
|
|
|
2020-10-31 13:17:26 +00:00
|
|
|
-- Returns the word currently under cursor, the vim equivalent of yiW.
|
|
|
|
-- Takes an optional boolean flag to set the word being caught
|
|
|
|
-- to the vim equivalent of doing yiw, a more exclusive version.
|
|
|
|
function A.get_link_under_cursor(small)
|
|
|
|
local c = "<cWORD>"
|
|
|
|
if small then c = "<cword>" end
|
|
|
|
local word = vim.fn.expand(c)
|
|
|
|
return word
|
|
|
|
end
|
2020-11-01 21:18:40 +00:00
|
|
|
|
2020-11-02 20:51:14 +00:00
|
|
|
-- Returns the content of the line from the cursor onwards.
|
|
|
|
function A.get_next_link_on_line()
|
|
|
|
local line = vim.api.nvim_get_current_line()
|
|
|
|
return line:sub(vim.api.nvim_win_get_cursor(0)[2])
|
|
|
|
end
|
2020-10-31 13:17:26 +00:00
|
|
|
|
|
|
|
return {open = A.open, open_selected = A.open_selected}
|