aboutsummaryrefslogtreecommitdiff
path: root/lua/simple-denote
diff options
context:
space:
mode:
Diffstat (limited to 'lua/simple-denote')
-rw-r--r--lua/simple-denote/api.lua16
-rw-r--r--lua/simple-denote/config.lua1
-rw-r--r--lua/simple-denote/init.lua18
-rw-r--r--lua/simple-denote/internal.lua81
-rw-r--r--lua/simple-denote/test.lua3
5 files changed, 65 insertions, 54 deletions
diff --git a/lua/simple-denote/api.lua b/lua/simple-denote/api.lua
index d986469..05d645a 100644
--- a/lua/simple-denote/api.lua
+++ b/lua/simple-denote/api.lua
@@ -3,12 +3,12 @@ local M = {}
local internal = require("simple-denote.internal")
---@param options table
----@param name string|nil
+---@param title string|nil
---@param tags table|nil
-function M.note(options, name, tags)
- if not name then
+function M.note(options, title, tags)
+ if not title then
vim.ui.input({ prompt = "Note title: " }, function(input)
- name = input
+ title = input
end)
end
if not tags then
@@ -16,12 +16,13 @@ function M.note(options, name, tags)
tags = input
end)
end
- internal.note(options, name, tags)
+ if not title or not tags then return end
+ internal.note(options, title, tags)
end
---@param options table
---@param filename string|nil
----@param new_title string|nil
+---@param title string|nil
function M.title(options, filename, title)
if not filename then
filename = vim.fn.expand("%")
@@ -31,6 +32,7 @@ function M.title(options, filename, title)
title = input
end)
end
+ if not title then return end
internal.title(options, filename, title)
end
@@ -46,6 +48,7 @@ function M.tag(options, filename, tags)
tags = input
end)
end
+ if not tags then return end
internal.tag(options, filename, tags)
end
@@ -59,6 +62,7 @@ function M.signature(options, filename, sig)
sig = input
end)
end
+ if not sig then return end
internal.signature(options, filename, sig)
end
diff --git a/lua/simple-denote/config.lua b/lua/simple-denote/config.lua
index e41c0a9..ad7dffc 100644
--- a/lua/simple-denote/config.lua
+++ b/lua/simple-denote/config.lua
@@ -5,6 +5,7 @@ M.defaults = {
dir = "~/notes/",
add_heading = true,
retitle_heading = true,
+ heading_char = ""
}
M.options = M.defaults
diff --git a/lua/simple-denote/init.lua b/lua/simple-denote/init.lua
index b01ca7a..3f199fe 100644
--- a/lua/simple-denote/init.lua
+++ b/lua/simple-denote/init.lua
@@ -19,17 +19,27 @@ function M.load_cmd(options)
end, {
nargs = 1,
complete = function()
- return { "note", "retitle", "retag", "signature" }
+ return { "note", "title", "tag", "signature" }
end,
})
end
----@param options? table user configuration
-function M.setup(options)
- config.options = vim.tbl_deep_extend("force", config.defaults, options or {})
+---Add / to directory if necessary and set the heading_char based on the ext
+function M.fix_options()
if config.options.dir:sub(-1) ~= "/" then
config.options.dir = config.options.dir .. "/"
end
+ if config.options.ext == "md" then
+ config.options.heading_char = "#"
+ elseif config.options.ext == "org" or config.options.ext == "norg" then
+ config.options.heading_char = "*"
+ end
+end
+
+---@param options? table user configuration
+function M.setup(options)
+ config.options = vim.tbl_deep_extend("force", config.defaults, options or {})
+ M.fix_options()
M.load_cmd(config.options)
end
diff --git a/lua/simple-denote/internal.lua b/lua/simple-denote/internal.lua
index 5966ac6..c2f8046 100644
--- a/lua/simple-denote/internal.lua
+++ b/lua/simple-denote/internal.lua
@@ -1,51 +1,42 @@
local M = {}
---- Trim whitespace on either end of string
+---@param str string
+---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
---- 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
+---@param str string
+---Make lowercase, remove special chars, remove extraneous spaces
+function M.plain_format(str)
+ str = str:gsub("[^%w%s]","")
+ str = str:lower()
+ str = M.trim(str)
+ str = str:gsub("%s+"," ")
+ return str
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
+---@param str string
+---@param char delimiter that replaces spaces (- for titles, _ for tags, = for sigs)
+---Format the title/tags/sig string of a Denote filename
+function M.format_denote_string(str, char)
+ str = M.plain_format(str)
+ if str == "" then return "" end
+ str = char .. char .. str:gsub("%s", char)
+ return str
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)
- local prefix = ""
- if ext == "md" then
- prefix = "# "
- elseif ext == "org" or ext == "norg" then
- prefix = "* "
- end
- 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)
+---Set the first line to title if it's an empty buffer or the first line is a heading
+function M.set_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)
+ if first_char == "" or first_char == options.heading_char then
+ local prefix = options.heading_char
+ if prefix ~= "" then prefix = prefix .. " " end
+ vim.api.nvim_buf_set_lines(0, 0, 1, false, {prefix .. title})
end
end
@@ -61,7 +52,7 @@ end
---@param options table
---@param title string
----@param tags table|nil
+---@param tags string
---Edit a new note with a Denote filename
function M.note(options, title, tags)
title = M.trim(title)
@@ -72,7 +63,7 @@ function M.note(options, title, tags)
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)
+ M.set_heading(options, og_title)
vim.cmd("norm! 2o")
end
vim.cmd("startinsert")
@@ -84,19 +75,22 @@ end
--- Retitles the filename and changes the first heading of the note
function M.title(options, filename, title)
local prefix = 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
+ local sig = filename:match(".-(==[^%-%_%.]*)")
local tags = filename:match(".-(__.*)%..*")
- if not prefix then error("This doesn't look like a Denote filename") end
+ if not tags then tags = "" end
+ if not sig then sig = "" end
title = M.trim(title)
if options.retitle_heading then
- M.replace_heading(options, title)
+ M.set_heading(options, title)
end
title = M.format_denote_string(title, "-")
- local new_filename = prefix .. title .. (tags or "") .. "." .. options.ext
+ local new_filename = prefix .. sig .. title .. tags .. "." .. 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 tags string
@@ -106,6 +100,9 @@ function M.tag(options, filename, tags)
if not prefix then
prefix = filename:match("^(.*)%..-$")
end
+ if not prefix:match("%d%d%d%d%d%d%d%dT%d%d%d%d%d%d") then
+ error("This doesn't look like a Denote filename")
+ end
tags = M.format_denote_string(tags, "_")
local new_filename = prefix .. tags .. "." .. options.ext
M.replace_file(filename, new_filename)
@@ -117,7 +114,9 @@ end
---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
+ 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
diff --git a/lua/simple-denote/test.lua b/lua/simple-denote/test.lua
deleted file mode 100644
index 1b4748a..0000000
--- a/lua/simple-denote/test.lua
+++ /dev/null
@@ -1,3 +0,0 @@
-s = "/home/admin/123456T123456==1a1--title.md"
-m = match("__") or ""
-print(m)