diff options
Diffstat (limited to 'lua/simple-denote')
| -rw-r--r-- | lua/simple-denote/api.lua | 31 | ||||
| -rw-r--r-- | lua/simple-denote/init.lua | 12 | ||||
| -rw-r--r-- | lua/simple-denote/internal.lua | 57 | ||||
| -rw-r--r-- | lua/simple-denote/test.lua | 3 |
4 files changed, 66 insertions, 37 deletions
diff --git a/lua/simple-denote/api.lua b/lua/simple-denote/api.lua index f99c21d..d986469 100644 --- a/lua/simple-denote/api.lua +++ b/lua/simple-denote/api.lua @@ -2,14 +2,6 @@ local M = {} local internal = require("simple-denote.internal") -function M.splitspace(str) - local chunks = {} - for substring in str:gmatch("%S+") do - table.insert(chunks, #chunks, substring) - end - return chunks -end - ---@param options table ---@param name string|nil ---@param tags table|nil @@ -30,7 +22,7 @@ end ---@param options table ---@param filename string|nil ---@param new_title string|nil -function M.retitle(options, filename, title) +function M.title(options, filename, title) if not filename then filename = vim.fn.expand("%") end @@ -39,13 +31,13 @@ function M.retitle(options, filename, title) title = input end) end - internal.retitle(options, filename, title) + internal.title(options, filename, title) end ---@param options table ---@param filename string|nil ----@param new_tags table|nil -function M.retag(options, filename, tags) +---@param tags table|nil +function M.tag(options, filename, tags) if not filename then filename = vim.fn.expand("%") end @@ -54,7 +46,20 @@ function M.retag(options, filename, tags) tags = input end) end - internal.retag(options, filename, tags) + internal.tag(options, filename, tags) +end + +---@param options table +---@param filename string|nil +---@param sig string +function M.signature(options, filename, sig) + filename = filename or vim.fn.expand("%") + if not sig then + vim.ui.input({ prompt = "Signature: " }, function(input) + sig = input + end) + end + internal.signature(options, filename, sig) end return M diff --git a/lua/simple-denote/init.lua b/lua/simple-denote/init.lua index 31abb58..b01ca7a 100644 --- a/lua/simple-denote/init.lua +++ b/lua/simple-denote/init.lua @@ -7,17 +7,19 @@ function M.load_cmd(options) vim.api.nvim_create_user_command("Denote", function(opts) if opts.fargs[1] == "note" then api.note(options) - elseif opts.fargs[1] == "retitle" then - api.retitle(options) - elseif opts.fargs[1] == "retag" then - api.retag(options) + elseif opts.fargs[1] == "title" then + api.title(options) + elseif opts.fargs[1] == "tag" then + api.tag(options) + elseif opts.fargs[1] == "signature" then + api.signature(options) else error("Unsupported operation " .. opts.fargs[1]) end end, { nargs = 1, complete = function() - return { "note", "retitle", "retag" } + return { "note", "retitle", "retag", "signature" } end, }) end diff --git a/lua/simple-denote/internal.lua b/lua/simple-denote/internal.lua index 5f42b44..5966ac6 100644 --- a/lua/simple-denote/internal.lua +++ b/lua/simple-denote/internal.lua @@ -39,6 +39,16 @@ function M.set_heading(title, ext) vim.api.nvim_buf_set_lines(0, 0, 1, false, {prefix .. title}) end +--- TODO: Set a heading char option to clean this up. Probably combine these functions. +function M.replace_heading(options, title) + if title == "" then return end + local first_char = vim.api.nvim_buf_get_lines(0, 0, 1, false)[1]:sub(1, 1) + if (first_char == "#" and options.ext == "md") or + (first_char == "*" and (options.ext == "org" or options.ext == "norg")) then + M.set_heading(title, options.ext) + end +end + ---@param new_filename string ---@param old_filename string ---Save new file, delete old file @@ -72,36 +82,45 @@ end ---@param filename string ---@param title string --- Retitles the filename and changes the first heading of the note -function M.retitle(options, filename, title) - local new_filename = filename:match("^(.-%d%d%d%d%d%d%d%dT%d%d%d%d%d%d).*") +function M.title(options, filename, title) + local prefix = filename:match("^(.-%d%d%d%d%d%d%d%dT%d%d%d%d%d%d).*") local tags = filename:match(".-(__.*)%..*") - if new_filename == nil then - error("This file doesn't look like it has a Denote-style name") - end + if not prefix then error("This doesn't look like a Denote filename") end title = M.trim(title) - if options.retitle_heading and title ~= "" then - local first_char = vim.api.nvim_buf_get_lines(0, 0, 1, false)[1]:sub(1, 1) - if (first_char == "#" and options.ext == "md") or - (first_char == "*" and (options.ext == "org" or options.ext == "norg")) then - M.set_heading(title, options.ext) - end + if options.retitle_heading then + M.replace_heading(options, title) end title = M.format_denote_string(title, "-") - new_filename = new_filename .. title .. (tags or "") .. "." .. options.ext + local new_filename = prefix .. title .. (tags or "") .. "." .. options.ext M.replace_file(filename, new_filename) end +---TODO: Probably can simplify the match to one +---TODO: Need to validate denote format so you don't rename random files ---@param options table ---@param filename string ----@param new_tags table ----Replaces the tags in filename -function M.retag(options, filename, tags) - local new_filename = filename:match("^(.*)__.*$") - if new_filename == nil then - new_filename = filename:match("^(.*)%..*$") +---@param tags string +---Replaces the __tags in filename +function M.tag(options, filename, tags) + local prefix = filename:match("^(.*)__.*$") + if not prefix then + prefix = filename:match("^(.*)%..-$") end tags = M.format_denote_string(tags, "_") - new_filename = new_filename .. tags .. "." .. options.ext + local new_filename = prefix .. tags .. "." .. options.ext + M.replace_file(filename, new_filename) +end + +---@param options table +---@param filename string +---@param sig string +---Add/change the ==signature in the filename +function M.signature(options, filename, sig) + local prefix, suffix = filename:match("^(.-%d%d%d%d%d%d%d%dT%d%d%d%d%d%d)(.*)") + if not prefix then error("This doesn't look like a Denote filename") end + suffix = suffix:gsub("==[^%-%_%.]*", "") + sig = M.format_denote_string(sig, "=") + local new_filename = prefix .. sig .. suffix M.replace_file(filename, new_filename) end diff --git a/lua/simple-denote/test.lua b/lua/simple-denote/test.lua new file mode 100644 index 0000000..1b4748a --- /dev/null +++ b/lua/simple-denote/test.lua @@ -0,0 +1,3 @@ +s = "/home/admin/123456T123456==1a1--title.md" +m = match("__") or "" +print(m) |
