aboutsummaryrefslogtreecommitdiff
path: root/lua/simple-denote/internal.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua/simple-denote/internal.lua')
-rw-r--r--lua/simple-denote/internal.lua81
1 files changed, 40 insertions, 41 deletions
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