Add action module

Action module contains the interactions the user can take directly with
the zettelkasten, and which in turn act on the editor.

First action to be taken is the opening of zettel links.
`action.open(mytext)` allows the user to pass in a md/wikilink-formatted
string from which the function will open the first found in the current
buffer.

`action.open_selected()` does the same, but looks at the cursor context
to get its string (the link under current cursor position as of now; in
the future probably also the next link on current line, and the first
link in visual selection)
This commit is contained in:
Marty Oehme 2020-10-31 14:17:26 +01:00
parent 972845505f
commit aef7d29997
Signed by: Marty
GPG key ID: B7538B8F50A1C800
3 changed files with 112 additions and 3 deletions

View file

@ -0,0 +1,79 @@
local A = {}
-- Return only the link reference portion of a markdown/wiki style link
function A.extract_link(input)
if not input then return end
return input:match("%[%[(.+)|?.*%]%]") or input:match("%[.*%]%((.+)%)")
end
-- 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.
function A.open_selected()
-- TODO decide between currentword/restofline option
A.open(A.get_link_under_cursor())
end
-- 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
-- -- 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()
-- local linenr = vim.api.nvim_win_get_cursor(0)[1]
-- print(line, linenr)
-- return ""
-- end
return {open = A.open, open_selected = A.open_selected}
-- local function get_selection()
-- s_start = vim.fn.line("'<") - 1
-- s_end = vim.fn.line("'>")
-- return vim.api.nvim_buf_get_lines(0, s_start, s_end, true)
-- end
-- -- UGLY HACKS ABOUND
-- function ZK.create_zettel()
-- -- get line and its number
-- local selection
-- local line = vim.api.nvim_get_current_line()
-- local linenr = vim.api.nvim_win_get_cursor(0)[1]
-- -- get words under cursor / selected
-- local mode = vim.api.nvim_get_mode()['mode']
-- if mode == "n" then
-- print(vim.fn.line("'<'") - 1)
-- selection = vim.fn.expand("<cWORD>")
-- -- NOT WORKING YET
-- elseif mode == "v" then
-- selection = get_selection()
-- else
-- return
-- end
-- -- get valid link
-- local link = l.create(nil, selection)
-- -- create new line with selection replaced in middle
-- local st, en = line:find(selection, 0, true)
-- local repl_line = line:sub(1, st - 1) .. link .. line:sub(en + 1)
-- -- replace existing line in favor of new one
-- vim.api.nvim_buf_set_lines(0, linenr - 1, linenr, true, {repl_line})
-- end

View file

@ -0,0 +1,32 @@
action = require 'zettelkasten.action'
before_each(function() _G.vim = {g = {}, b = {}} end)
after_each(function() _G.vim = nil end)
describe("open", function()
it("should open file in editor if it is a valid link", function()
vim.api = {nvim_command = mock(function() end)}
action.open("[some text](1910271456_link-to-my-file.md)")
assert.spy(vim.api.nvim_command).was_called_with(
"edit 1910271456_link-to-my-file.md")
end)
it("should not fail when no input is available", function()
vim.fn = {expand = function() end}
assert.is_not_error(action.open)
end)
end)
describe("open_selected", function()
it("should open the next link found if no argument passed in", function()
vim.api = {nvim_command = mock(function() end)}
vim.fn = {
expand = function(sure)
return "[" .. sure .. "](1910271456_link-to-my-file.md)"
end
}
action.open_selected()
assert.spy(vim.api.nvim_command).was_called_with(
"edit 1910271456_link-to-my-file.md")
end)
end)

View file

@ -16,6 +16,4 @@ end
-- composed of yymmddHHMM.
function ZK.create_anchor() return a.create() end
return {
get_zettel_list = ZK.get_zettel_list
}
return {get_zettel_list = ZK.get_zettel_list, create_anchor = ZK.create_anchor}