diff options
| author | DefaultGen <gravel.justness270@slmail.me> | 2024-06-04 17:13:32 -0400 |
|---|---|---|
| committer | DefaultGen <gravel.justness270@slmail.me> | 2024-06-04 17:13:32 -0400 |
| commit | 9831fe1ec019e07251b209e73b3258d082d0ac94 (patch) | |
| tree | a2443690eebb4e4a162b578f24588196f138be7c /lua/simple-denote | |
| parent | 45c6c2c12f97f91d0550db2aa888f68f68116e9a (diff) | |
| download | simple-denote.nvim-9831fe1ec019e07251b209e73b3258d082d0ac94.tar.gz | |
Refactoring, fixing edge cases
Diffstat (limited to 'lua/simple-denote')
| -rw-r--r-- | lua/simple-denote/api.lua | 31 | ||||
| -rw-r--r-- | lua/simple-denote/cmd.lua | 26 | ||||
| -rw-r--r-- | lua/simple-denote/init.lua | 23 | ||||
| -rw-r--r-- | lua/simple-denote/internal.lua | 122 | ||||
| -rw-r--r-- | lua/simple-denote/util.lua | 42 |
5 files changed, 102 insertions, 142 deletions
diff --git a/lua/simple-denote/api.lua b/lua/simple-denote/api.lua index f835b7b..f99c21d 100644 --- a/lua/simple-denote/api.lua +++ b/lua/simple-denote/api.lua @@ -1,7 +1,14 @@ local M = {} local internal = require("simple-denote.internal") -local util = require("simple-denote.util") + +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 @@ -14,9 +21,7 @@ function M.note(options, name, tags) end if not tags then vim.ui.input({ prompt = "Tags: " }, function(input) - if input ~= "" and input then - tags = util.splitspace(input) - end + tags = input end) end internal.note(options, name, tags) @@ -25,33 +30,31 @@ end ---@param options table ---@param filename string|nil ---@param new_title string|nil -function M.retitle(options, filename, new_title) +function M.retitle(options, filename, title) if not filename then filename = vim.fn.expand("%") end - if not new_title then + if not title then vim.ui.input({ prompt = "New title: " }, function(input) - new_title = input + title = input end) end - internal.retitle(options, filename, new_title) + internal.retitle(options, filename, title) end ---@param options table ---@param filename string|nil ---@param new_tags table|nil -function M.retag(options, filename, new_tags) +function M.retag(options, filename, tags) if not filename then filename = vim.fn.expand("%") end - if not new_tags then + if not tags then vim.ui.input({ prompt = "New tags: " }, function(input) - if input ~= "" and input then - new_tags = util.splitspace(input) - end + tags = input end) end - internal.retag(options, filename, new_tags) + internal.retag(options, filename, tags) end return M diff --git a/lua/simple-denote/cmd.lua b/lua/simple-denote/cmd.lua deleted file mode 100644 index 177db88..0000000 --- a/lua/simple-denote/cmd.lua +++ /dev/null @@ -1,26 +0,0 @@ -local M = { - commands = { "note", "retitle", "retag" }, -} - -local api = require("simple-denote.api") - -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) - else - error("Unsupported operation " .. opts.fargs[1]) - end - end, { - nargs = 1, - complete = function() - return M.commands - end, - }) -end - -return M diff --git a/lua/simple-denote/init.lua b/lua/simple-denote/init.lua index 0d8654d..31abb58 100644 --- a/lua/simple-denote/init.lua +++ b/lua/simple-denote/init.lua @@ -1,15 +1,34 @@ local M = {} -local cmd = require("simple-denote.cmd") +local api = require("simple-denote.api") local config = require("simple-denote.config") +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) + else + error("Unsupported operation " .. opts.fargs[1]) + end + end, { + nargs = 1, + complete = function() + return { "note", "retitle", "retag" } + end, + }) +end + ---@param options? table user configuration function M.setup(options) config.options = vim.tbl_deep_extend("force", config.defaults, options or {}) if config.options.dir:sub(-1) ~= "/" then config.options.dir = config.options.dir .. "/" end - cmd.load_cmd(config.options) + M.load_cmd(config.options) end return M diff --git a/lua/simple-denote/internal.lua b/lua/simple-denote/internal.lua index 7eabe61..319d069 100644 --- a/lua/simple-denote/internal.lua +++ b/lua/simple-denote/internal.lua @@ -1,25 +1,36 @@ local M = {} -local util = require("simple-denote.util") ----@param tags table ---- Create a_tag_string from tags table -function M.make_tag_str(tags) - if not tags then return "" end - local tags_str = "" - for i, tag in pairs(tags) do - tag = util.plain_format(tag) - tags_str = tags_str .. tag - if i < #tags then - tags_str = tags_str .. "_" - end - end - return "__" .. tags_str:lower() +--- Trim whitespace on either end of string +function M.trim(str) + local from = str:match"^%s*()" + return from > #str and "" or str:match(".*%S", from) end ----@param title heading text ----@param ext file extension +--- Make lowercase, remove special chars, remove extraneous spaces +function M.plain_format(string) + if not string then return "" end + string = string:gsub("[^%w%s]","") + string = string:lower() + string = M.trim(string) + string = string:gsub("%s+"," ") + return string +end + +---@param string string +---@param char delimiter that replaces spaces (- for titles, _ for tags) +--- Format the title/tags string of a Denote filename +function M.format_denote_string(string, char) + string = M.plain_format(string) + if string == "" then return "" end + string = char .. char .. string:gsub("%s", char) + return string +end + +---@param title string heading text +---@param ext string file extension +---Set the first line to title, including the appropriate heading char function M.set_heading(title, ext) - prefix = "" + local prefix = "" if ext == "md" then prefix = "# " elseif ext == "org" or ext == "norg" then @@ -28,23 +39,30 @@ function M.set_heading(title, ext) vim.api.nvim_buf_set_lines(0, 0, 1, false, {prefix .. title}) end +---@param new_filename string +---@param old_filename string +---Save new file, delete old file +function M.replace_file(old_filename, new_filename) + if old_filename ~= new_filename then + vim.cmd('saveas ' .. new_filename) + vim.cmd('silent !rm ' .. old_filename) + end +end + ---@param options table ----@param name string +---@param title string ---@param tags table|nil ---- Opens your note -function M.note(options, name, tags) - name = util.trim(name) - local og_name = name - local tags_str = M.make_tag_str(tags) - name = util.plain_format(name) +---Edit a new note with a Denote filename +function M.note(options, title, tags) + title = M.trim(title) + local og_title = title + tags = M.format_denote_string(tags, "_") + title = M.format_denote_string(title, "-") local file = options.dir .. os.date("%Y%m%dT%H%M%S") - if name ~= "" then - file = file .. "--" - end - file = file .. name .. tags_str .. "." .. options.ext - vim.cmd("e " .. file) - if options.add_heading and og_name ~= "" then - M.set_heading(og_name, options.ext) + file = file .. title .. tags .. "." .. options.ext + vim.cmd("edit " .. file) + if options.add_heading and og_title ~= "" then + M.set_heading(og_title, options.ext) vim.cmd("norm! o") end vim.cmd("startinsert") @@ -52,51 +70,39 @@ end ---@param options table ---@param filename string ----@param new_title string +---@param title string --- Retitles the filename and changes the first heading of the note -function M.retitle(options, filename, new_title) - local new_filename = filename:match("^(.*%d%d%d%d%d%d%d%dT%d%d%d%d%d%d).-") - local tags_str = filename:match(".*(__.*)%..*") +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).*") + local tags = filename:match(".-(__.*)%..*") if new_filename == nil then error("This file doesn't look like it has a Denote-style name") end - new_title = util.trim(new_title) - if options.retitle_heading and new_title ~= "" then + 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(new_title, options.ext) + M.set_heading(title, options.ext) end end - new_title = util.plain_format(new_title) - if new_title ~= "" then - new_title = "--" .. new_title - end - new_filename = new_filename .. new_title .. (tags_str or "") .. "." .. options.ext - if filename ~= new_filename then - vim.cmd('saveas ' .. new_filename) - vim.cmd('silent !rm ' .. filename) - end + title = M.format_denote_string(title, "-") + new_filename = new_filename .. title .. (tags or "") .. "." .. options.ext + M.replace_file(filename, new_filename) end ---@param options table ---@param filename string ---@param new_tags table ----Replaces the tags in filename with the tags in new_tags -function M.retag(options, filename, new_tags) +---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("^(.*)%..*$") end - local tags_str = M.make_tag_str(new_tags) - if tags_str ~= "" then - new_filename = new_filename .. tags_str - end - new_filename = new_filename .. "." .. options.ext - if filename ~= new_filename then - vim.cmd('saveas ' .. new_filename) - vim.cmd('silent !rm ' .. filename) - end + tags = M.format_denote_string(tags, "_") + new_filename = new_filename .. tags .. "." .. options.ext + M.replace_file(filename, new_filename) end return M diff --git a/lua/simple-denote/util.lua b/lua/simple-denote/util.lua deleted file mode 100644 index 7c01449..0000000 --- a/lua/simple-denote/util.lua +++ /dev/null @@ -1,42 +0,0 @@ -local M = {} - - -function M.splitspace(str) - local chunks = {} - for substring in str:gmatch("%S+") do - table.insert(chunks, #chunks, substring) - end - return chunks -end - -function M.splitstring(str, delim) - local items = {} - local fulldelim = nil - if delim == "." or delim == "-" then - fulldelim = "%" .. delim - else - fulldelim = delim - end - str = str .. delim - for w in str:gmatch("(.-)" .. fulldelim) do - items[#items + 1] = w - end - return items -end - ---- Trim whitespace on either end of string -function M.trim(str) - local from = str:match"^%s*()" - return from > #str and "" or str:match(".*%S", from) -end - ---- Remove special chars, make lowercase, spaces to dashes -function M.plain_format(str) - str = str:lower() - str = str:gsub("[^%w%s]","") - str = M.trim(str) - str = str:gsub("%s+","-") - return str -end - -return M |
