aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
Diffstat (limited to 'lua')
-rw-r--r--lua/simple-denote.lua92
-rw-r--r--lua/simple-denote/config.lua1
2 files changed, 92 insertions, 1 deletions
diff --git a/lua/simple-denote.lua b/lua/simple-denote.lua
index f7e24f2..98c6ded 100644
--- a/lua/simple-denote.lua
+++ b/lua/simple-denote.lua
@@ -15,13 +15,15 @@ function M.load_cmd(options)
api.signature()
elseif opts.fargs[1] == "extension" then
api.extension()
+ elseif opts.fargs[1] == "dir" then
+ M.switch_dir()
else
error("Unsupported operation " .. opts.fargs[1])
end
end, {
nargs = 1,
complete = function()
- return { "note", "title", "keywords", "signature", "extension" }
+ return { "note", "title", "keywords", "signature", "extension", "dir" }
end,
})
end
@@ -31,6 +33,14 @@ function M.fix_options()
if config.options.dir:sub(-1) ~= "/" then
config.options.dir = config.options.dir .. "/"
end
+ for i, d in ipairs(config.options.directories) do
+ if d:sub(-1) ~= "/" then
+ config.options.directories[i] = d .. "/"
+ end
+ end
+ if #config.options.directories > 0 then
+ vim.g.denote_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
@@ -38,6 +48,86 @@ function M.fix_options()
end
end
+function M.switch_dir()
+ local dirs = config.options.directories
+ if #dirs == 0 then
+ vim.notify("No directories configured. Add directories = { ... } to your setup.", vim.log.levels.WARN)
+ return
+ end
+
+ local current = config.options.dir
+ local cursor_line = 1
+ for i, d in ipairs(dirs) do
+ if d == current then
+ cursor_line = i
+ break
+ end
+ end
+
+ local max_width = 0
+ for _, d in ipairs(dirs) do
+ max_width = math.max(max_width, #d)
+ end
+
+ local buf = vim.api.nvim_create_buf(false, true)
+ vim.api.nvim_buf_set_lines(buf, 0, -1, false, dirs)
+ vim.api.nvim_buf_set_option(buf, "modifiable", false)
+ vim.api.nvim_buf_set_option(buf, "bufhidden", "wipe")
+
+ local width = max_width + 2
+ local height = #dirs
+ local row = math.floor((vim.o.lines - height) / 2)
+ local col = math.floor((vim.o.columns - width) / 2)
+
+ local win = vim.api.nvim_open_win(buf, true, {
+ relative = "editor",
+ width = width,
+ height = height,
+ row = row,
+ col = col,
+ style = "minimal",
+ border = "single",
+ })
+
+ vim.wo[win].cursorline = true
+ vim.api.nvim_win_set_cursor(win, { cursor_line, 0 })
+
+ local function select()
+ local line = vim.api.nvim_win_get_cursor(win)[1]
+ local choice = dirs[line]
+ if choice then
+ config.options.dir = choice
+ vim.g.denote_dir = choice
+ vim.notify("Notes directory: " .. choice, vim.log.levels.INFO)
+ end
+ vim.api.nvim_win_close(win, true)
+ end
+
+ local function cancel()
+ vim.api.nvim_win_close(win, true)
+ end
+
+ vim.keymap.set("n", "<CR>", select, { buffer = buf, nowait = true })
+ vim.keymap.set("n", "<Esc>", cancel, { buffer = buf, nowait = true })
+ vim.keymap.set("n", "<C-c>", cancel, { buffer = buf, nowait = true })
+ vim.keymap.set("n", "j", function()
+ local line = math.min(vim.api.nvim_win_get_cursor(win)[1] + 1, #dirs)
+ vim.api.nvim_win_set_cursor(win, { line, 0 })
+ end, { buffer = buf, nowait = true })
+ vim.keymap.set("n", "k", function()
+ local line = math.max(vim.api.nvim_win_get_cursor(win)[1] - 1, 1)
+ vim.api.nvim_win_set_cursor(win, { line, 0 })
+ end, { buffer = buf, nowait = true })
+ vim.keymap.set("n", "<Down>", function()
+ local line = math.min(vim.api.nvim_win_get_cursor(win)[1] + 1, #dirs)
+ vim.api.nvim_win_set_cursor(win, { line, 0 })
+ end, { buffer = buf, nowait = true })
+ vim.keymap.set("n", "<Up>", function()
+ local line = math.max(vim.api.nvim_win_get_cursor(win)[1] - 1, 1)
+ vim.api.nvim_win_set_cursor(win, { line, 0 })
+ end, { buffer = buf, nowait = true })
+end
+
---@param options? table user configuration
function M.setup(options)
config.options = vim.tbl_deep_extend("force", config.defaults, options or {})
diff --git a/lua/simple-denote/config.lua b/lua/simple-denote/config.lua
index efd69c0..2b82778 100644
--- a/lua/simple-denote/config.lua
+++ b/lua/simple-denote/config.lua
@@ -3,6 +3,7 @@ local M = {}
M.defaults = {
ext = "md",
dir = "~/notes/",
+ directories = {},
add_heading = true,
retitle_heading = true,
heading_char = "" -- This gets set automatically