2020-10-31 13:17:26 +00:00
|
|
|
local A = {}
|
|
|
|
|
2021-05-03 15:29:42 +00:00
|
|
|
local f = require 'zettelkasten.files'
|
2021-05-04 07:46:18 +00:00
|
|
|
local l = require 'zettelkasten.link'
|
|
|
|
local o = require 'zettelkasten.options'
|
2021-05-03 21:21:58 +00:00
|
|
|
local t = require 'zettelkasten.text'
|
2020-11-02 20:51:14 +00:00
|
|
|
|
2020-11-04 20:29:23 +00:00
|
|
|
-- Opens the link passed in in the editor's current buffer.
|
|
|
|
-- Requires a link object passed in.
|
2021-05-03 15:29:42 +00:00
|
|
|
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
|
2020-11-06 16:28:05 +00:00
|
|
|
vim.api.nvim_command(string.format("edit %s", fname))
|
2020-10-31 13:17:26 +00:00
|
|
|
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)
|
2021-05-04 07:46:18 +00:00
|
|
|
style = style or o.link().following
|
2020-11-06 15:42:09 +00:00
|
|
|
|
|
|
|
local curpos = vim.api.nvim_win_get_cursor(0)[2]
|
2021-05-03 15:29:42 +00:00
|
|
|
local links = l.extract_all(vim.api.nvim_get_current_line())
|
2020-11-06 15:42:09 +00:00
|
|
|
|
2021-04-30 15:32:03 +00:00
|
|
|
local ln
|
2021-05-04 07:46:18 +00:00
|
|
|
if style == 'line' then
|
2021-05-03 21:21:58 +00:00
|
|
|
ln = t.get_next_link_on_line(links, curpos)
|
2021-05-04 07:46:18 +00:00
|
|
|
elseif style == 'cursor' then
|
2021-05-03 21:21:58 +00:00
|
|
|
ln = t.get_link_under_cursor(links, curpos)
|
2020-11-02 20:51:14 +00:00
|
|
|
end
|
2021-04-30 15:32:03 +00:00
|
|
|
|
|
|
|
A.open(ln)
|
2020-10-31 13:17:26 +00:00
|
|
|
end
|
|
|
|
|
2021-05-03 15:31:28 +00:00
|
|
|
-- 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.
|
2021-05-04 07:46:18 +00:00
|
|
|
function A.make_link(visual)
|
2021-05-03 21:02:59 +00:00
|
|
|
local selection, start_col
|
|
|
|
if visual or vim.api.nvim_get_mode()['mode'] == "v" then
|
2021-05-03 21:21:58 +00:00
|
|
|
selection, start_col = t.get_current_selection()
|
2021-05-03 21:02:59 +00:00
|
|
|
else
|
2021-05-03 21:21:58 +00:00
|
|
|
selection, start_col = t.get_current_word()
|
2021-05-03 21:02:59 +00:00
|
|
|
end
|
2021-05-03 21:21:58 +00:00
|
|
|
vim.api.nvim_set_current_line(t.replace_text(selection, l.new(selection),
|
|
|
|
start_col))
|
2020-11-02 20:51:14 +00:00
|
|
|
end
|
2020-10-31 13:17:26 +00:00
|
|
|
|
2021-05-04 07:46:18 +00:00
|
|
|
return {open = A.open, open_selected = A.open_selected, make_link = A.make_link}
|