aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhistoria <gravel.justness270@slmail.me>2024-08-14 15:52:14 -0400
committerhistoria <gravel.justness270@slmail.me>2024-08-14 15:52:14 -0400
commit64b58e667d0d255439b055227f1f11012fcc9006 (patch)
tree9ce5c173ff05af0e647a4a00b04c59e54c701fa3
parent8da26902f526a54c8873d16d1b7b6e7820fb293b (diff)
downloadsimple-denote.nvim-64b58e667d0d255439b055227f1f11012fcc9006.tar.gz
Feature: Command to change file extension
-rw-r--r--README.md6
-rw-r--r--lua/simple-denote.lua6
-rw-r--r--lua/simple-denote/api.lua25
-rw-r--r--lua/simple-denote/internal.lua18
4 files changed, 42 insertions, 13 deletions
diff --git a/README.md b/README.md
index 24fd0db..fea44da 100644
--- a/README.md
+++ b/README.md
@@ -43,8 +43,9 @@ Maybe you want to set keymaps for the commands as well
```lua
vim.keymap.set({'n','v'}, '<leader>nn', ":Denote note<cr>", { desc = "New note" })
vim.keymap.set({'n','v'}, '<leader>nt', ":Denote title<cr>", { desc = "Change title" })
-vim.keymap.set({'n','v'}, '<leader>nk', ":Denote keywords<cr>", { desc = "Change keywords" })
+vim.keymap.set({'n','v'}, '<leader>nk', ":Denote keywords<cr>", { desc = "Change keywords" })
vim.keymap.set({'n','v'}, '<leader>nz', ":Denote signature<cr>", { desc = "Change signature" })
+vim.keymap.set({'n','v'}, '<leader>ne', ":Denote extension<cr>", { desc = "Change extension" })
```
## Manual Install
@@ -85,6 +86,9 @@ require('simple-denote').setup({
" Rename the current note with a signature
" This has a user-defined meaning and no particular purpose
:Denote signature
+
+" Rename the current file to a new extension
+:Denote extension
```
# Credits
diff --git a/lua/simple-denote.lua b/lua/simple-denote.lua
index 8ac2193..5709e3a 100644
--- a/lua/simple-denote.lua
+++ b/lua/simple-denote.lua
@@ -12,14 +12,16 @@ function M.load_cmd(options)
elseif opts.fargs[1] == "keywords" then
api.keywords(options)
elseif opts.fargs[1] == "signature" then
- api.signature(options)
+ api.signature()
+ elseif opts.fargs[1] == "extension" then
+ api.extension()
else
error("Unsupported operation " .. opts.fargs[1])
end
end, {
nargs = 1,
complete = function()
- return { "note", "title", "keywords", "signature" }
+ return { "note", "title", "keywords", "signature", "extension" }
end,
})
end
diff --git a/lua/simple-denote/api.lua b/lua/simple-denote/api.lua
index c26bafd..a03c9f2 100644
--- a/lua/simple-denote/api.lua
+++ b/lua/simple-denote/api.lua
@@ -4,7 +4,7 @@ local internal = require("simple-denote.internal")
---@param options table
---@param title string|nil
----@param keywords table|nil
+---@param keywords string|nil
function M.note(options, title, keywords)
if not title then
vim.ui.input({ prompt = "Note title: " }, function(input)
@@ -38,7 +38,7 @@ end
---@param options table
---@param filename string|nil
----@param keywords table|nil
+---@param keywords string|nil
function M.keywords(options, filename, keywords)
if not filename then
filename = vim.fn.expand("%")
@@ -52,10 +52,9 @@ function M.keywords(options, filename, keywords)
internal.keyword(options, filename, keywords)
end
----@param options table
---@param filename string|nil
----@param sig string
-function M.signature(options, filename, sig)
+---@param sig string|nil
+function M.signature(filename, sig)
filename = filename or vim.fn.expand("%")
if not sig then
vim.ui.input({ prompt = "Signature: " }, function(input)
@@ -63,7 +62,21 @@ function M.signature(options, filename, sig)
end)
end
if not sig then return end
- internal.signature(options, filename, sig)
+ internal.signature(filename, sig)
+end
+
+---@param filename string|nil
+---@param ext string|nil
+function M.extension(filename, ext)
+ filename = filename or vim.fn.expand("%")
+ if not ext then
+ vim.ui.input({ prompt = "Extension: " }, function(input)
+ ext = input
+ end)
+ end
+ if not ext then return end
+ internal.extension(filename, ext)
end
+
return M
diff --git a/lua/simple-denote/internal.lua b/lua/simple-denote/internal.lua
index 73ba71f..8821689 100644
--- a/lua/simple-denote/internal.lua
+++ b/lua/simple-denote/internal.lua
@@ -18,7 +18,7 @@ function M.plain_format(str)
end
---@param str string
----@param char delimiter that replaces spaces (- for titles, _ for keywords, = for sigs)
+---@param char string delimiter that replaces spaces (- for titles, _ for keywords, = for sigs)
---Format the title/keywords/sig string of a Denote filename
function M.format_denote_string(str, char)
str = M.plain_format(str)
@@ -28,7 +28,6 @@ function M.format_denote_string(str, char)
end
---@param title string heading text
----@param ext string file extension
---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
@@ -108,11 +107,10 @@ function M.keyword(options, filename, keywords)
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)
+function M.signature(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")
@@ -123,4 +121,16 @@ function M.signature(options, filename, sig)
M.replace_file(filename, new_filename)
end
+---@param filename string
+---@param ext string
+---Replace the extension in the file with ext
+function M.extension(filename, ext)
+ 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 file")
+ end
+ local new_filename = prefix .. ext
+ M.replace_file(filename, new_filename)
+end
+
return M