From 3b23e6a405a941af2882c49177f3a30a43245096 Mon Sep 17 00:00:00 2001 From: Chris Pecunies Date: Fri, 17 Jan 2025 19:53:24 -0800 Subject: [PATCH] cmds --- lua/down.lua | 8 +- lua/down/mod.lua | 2 + lua/down/mod/cmd/init.lua | 130 +++++++++++++-------- lua/down/mod/code/init.lua | 1 + lua/down/mod/data/history/init.lua | 102 ++++++++++++----- lua/down/mod/link/init.lua | 158 ++++++++++++++++---------- lua/down/mod/log/cmd.lua | 47 -------- lua/down/mod/log/init.lua | 103 ++++++++--------- lua/down/mod/{cmd => }/mod/README.md | 0 lua/down/mod/{cmd => }/mod/init.lua | 8 +- lua/down/mod/note/init.lua | 163 ++++++++++++++------------- lua/down/mod/template/init.lua | 8 ++ lua/down/mod/tool/telescope/init.lua | 4 +- lua/down/mod/ui/icon/init.lua | 17 ++- lua/down/mod/workspace/init.lua | 2 + lua/init.lua | 96 ++++++++++++++++ 16 files changed, 520 insertions(+), 329 deletions(-) delete mode 100644 lua/down/mod/log/cmd.lua rename lua/down/mod/{cmd => }/mod/README.md (100%) rename lua/down/mod/{cmd => }/mod/init.lua (94%) create mode 100644 lua/init.lua diff --git a/lua/down.lua b/lua/down.lua index da11ba8..8b56115 100644 --- a/lua/down.lua +++ b/lua/down.lua @@ -15,7 +15,9 @@ Down = { } Down.default = { - ['lsp'] = {}, + -- ['lsp'] = {}, + ['template'] = {}, + ['mod'] = {}, ['cmd'] = {}, ['link'] = {}, ['tool.telescope'] = {}, @@ -30,8 +32,8 @@ Down.default = { --- @param ... string The arguments to pass into an optional user hook function Down.setup(user, ...) Down.util.log.trace('Setting up Down') - Down.config:setup(user, Down.default, ...) - Down:start() + Down.config.setup(Down.config, user, Down.default, ...) + Down.start(Down) end function Down:start() diff --git a/lua/down/mod.lua b/lua/down/mod.lua index 4579173..d305e37 100644 --- a/lua/down/mod.lua +++ b/lua/down/mod.lua @@ -382,7 +382,9 @@ Mod.handle_cmd = function(self, e, cmd, cmds, ...) if not cmds or type(cmds) ~= 'table' or not cmds[cmd] then return false end + if cmds.enabled ~= nil and cmds.enabled == false then return false end local cc = cmds[cmd] + if cc.enabled ~= nil and cc.enabled == false then return false end if cc.name and cc.name == cmd and cc.callback then if not self.handle then self.handle = {} diff --git a/lua/down/mod/cmd/init.lua b/lua/down/mod/cmd/init.lua index 4a41594..10a4344 100644 --- a/lua/down/mod/cmd/init.lua +++ b/lua/down/mod/cmd/init.lua @@ -13,15 +13,58 @@ M.setup = function() return { loaded = true, dependencies = {} } end -M.commands = {} +M.commands = { + cmd = { + enabled = true, + name = 'cmd', + args = 0, + max_args = 1, + callback = function(e) + log.trace 'Cmd callback' + end, + subcommands = { + add = { + name = 'cmd.add', + args = 0, + max_args = 1, + callback = function(e) + log.trace 'Cmd add callback' + end, + }, + edit = { + name = 'cmd.edit', + args = 0, + max_args = 1, + callback = function(e) + log.trace 'Cmd edit callback' + end, + }, + remove = { + name = 'cmd.remove', + args = 0, + max_args = 1, + callback = function(e) + log.trace 'Cmd remove callback' + end, + }, + update = { + name = 'cmd.update', + args = 0, + max_args = 1, + callback = function(e) + log.trace 'Cmd update callback' + end, + }, + }, + + }, +} --- Handles the calling of the appropriate function based on the command the user entered M.cb = function(data) - -- vim.print(data) local args = data.fargs - - local current_buf = vim.api.nvim_get_current_buf() - local is_down = vim.bo[current_buf].filetype == 'markdown' + local buf = vim.api.nvim_get_current_buf() + local is_down = vim.bo[buf].filetype == 'markdown' local function check_condition(condition) if condition == nil then @@ -33,7 +76,7 @@ M.cb = function(data) end if type(condition) == 'function' then - return condition(current_buf, is_down) + return condition(buf, is_down) end return condition @@ -50,6 +93,7 @@ M.cb = function(data) end ref = ref.subcommands[cmd] + if ref.enabled == false then return end if not ref then log.error( @@ -126,10 +170,10 @@ M.cb = function(data) vim.list_slice(args, argument_index + 1) ) if ref.callback then - log.trace('Cmd..cb: Running ', ref.name, ' callback') + log.trace('Cmd.cb: Running ', ref.name, ' callback') ref.callback(e) else - log.trace('Cmd..cb: Running ', ref.name, ' broadcast') + log.trace('Cmd.cb: Running ', ref.name, ' broadcast') mod.broadcast(e) end end @@ -173,6 +217,7 @@ M.generate_completions = function(_, command) local last_completion_level = 0 for _, cmd in ipairs(splitcmd) do + if ref.enabled ~= nil and ref.enabled == false then return end if not ref or not M.check_condition(ref.condition) then break end @@ -224,7 +269,9 @@ M.generate_completions = function(_, command) local subcommands = (ref and ref.subcommands or last_valid_ref.subcommands) or {} return vim.tbl_filter(function(key) - return M.check_condition(subcommands[key].condition) + if type(subcommands[key]) == 'table' then + return M.check_condition(subcommands[key].condition) + end end, keys) end end @@ -249,6 +296,7 @@ M.select_next_cmd_arg = function(qargs, choices) query({ prompt = current, + }, function(choice) if choice ~= nil then vim.cmd(('%s %s'):format(current, choice)) @@ -262,7 +310,9 @@ end M.add_commands = function(mod_name) local mod_config = mod.get_mod(mod_name) - if not mod_config or not mod_config.commands then + if not mod_config or not mod_config.commands + or (mod_config.commands.enabled ~= nil and mod_config.commands.enabled == false) + then return end @@ -270,34 +320,21 @@ M.add_commands = function(mod_name) end --- Recursively merges the provided table with the mod.config.commands table. ----@param functions down.Commands #A table that follows the mod.config.commands structure -M.add_commands_from_table = function(functions) - M.commands = vim.tbl_extend('force', M.commands, functions) -end - ---- Takes a relative path (e.g "list.mod") and loads it from the commands/ directory ----@param name string #The relative path of the init we want to load -M.add_commands_from_file = function(name) - -- Attempt to require the file - local err, ret = pcall(require, 'down.mod.cmd.' .. name) - - -- If we've failed bail out - if not err then - log.warn( - 'Could not load command' - .. name - .. 'for init base.cmd - the corresponding mod.lua file does not exist.' - ) - return - end - mod.load_mod_from_table(ret) +---@param f down.Commands #A table that follows the mod.config.commands structure +M.add_commands_from_table = function(f) + M.commands = vim.tbl_extend('force', M.commands, f) end --- Rereads data from all mod and rebuild the list of available autocompletiinitinitons and commands M.sync = function() for _, lm in pairs(mod.mods) do if lm.commands then - M.add_commands_from_table(lm.commands) + if lm.commands.enabled ~= nil and lm.commands.enabled == false then + return + end + M.commands = vim.tbl_extend('force', M.commands, lm.commands) + -- lm.commands = M.load_cmds(lm) + -- M.add_commands_from_table(lm.commands) end end end @@ -317,32 +354,25 @@ M.load = function() nargs = '*', complete = M.generate_completions, }) - for _, command in ipairs(M.config.load) do - if command == 'default' then - for _, basecmd in ipairs(M.config.base) do - M.add_commands_from_file(basecmd) - end - end - end end ---@class down.mod.cmd.Config M.config = { - load = { - 'default', - }, - - base = { - 'mod', - }, } ---@class cmd -M.post_load = function() - for _, l in pairs(mod.mods) do - M.commands = vim.tbl_extend('force', M.commands, l.commands or {}) - M.add_commands_from_table(l.commands or {}) +M.load_cmds = function(m) + for _, c in pairs(m.commands or {}) do + local cmds = {} + if m.commands.enabled ~= false then + cmds = vim.tbl_extend('force', cmds, c) + end + l = vim.tbl_extend('force', l, cmds) end + return l +end + +M.post_load = function() M.sync() end diff --git a/lua/down/mod/code/init.lua b/lua/down/mod/code/init.lua index 1e74b0a..21a73c5 100644 --- a/lua/down/mod/code/init.lua +++ b/lua/down/mod/code/init.lua @@ -15,6 +15,7 @@ Code.config = { Code.code = {} Code.commands = { + enabled = true, code = { name = 'code', condition = 'markdown', diff --git a/lua/down/mod/data/history/init.lua b/lua/down/mod/data/history/init.lua index 6551f0d..f01aa24 100644 --- a/lua/down/mod/data/history/init.lua +++ b/lua/down/mod/data/history/init.lua @@ -1,40 +1,83 @@ local tbl = require 'table' local clear = require 'table.clear' local new = require 'table.new' ----@class down.Mod -local M = require 'down.mod'.new('data.history', {}) +local mod = require 'down.mod' +---@class down.mod.data.History: down.Mod +local M = mod.new 'data.history' + +---@class down.mod.data.history.Config: down.mod.Config M.config = { silent = true, + path = '', } +---@class down.mod.data.history.Commands M.commands = { + next = { + args = 0, + enabled = true, + condition = 'markdown', + name = 'data.history.forward', + subcommands = { + list = { + enabled = true, + args = 0, + condition = 'markdown', + name = 'data.history.forward.list', + }, + }, + }, back = { args = 0, condition = 'markdown', + enabled = true, name = 'data.history.back', + subcommands = { + list = { + args = 0, + condition = 'markdown', + enabled = true, + name = 'data.history.back.list', + }, + }, }, } ---- Buffer queue ---- @type table -M.history = {} +--- @type integer[] +M.history = { + ---@type integer[] + hist = {}, + --- @type integer[] + buf = {}, + ---@type string[] + file = {}, +} ---- @type table -M.buf = {} +M.history.buf = {} --- Clear the stacks M.clear = function() - clear(M.history) - clear(M.buf) + clear(M.history.hist) + clear(M.history.file) + clear(M.history.buf) +end + +M.add = {} + +M.add.file = function(buf) + table.insert(M.history.file, buf or vim.api.nvim_get_current_buf()) +end +M.add.current = function(buf) + table.insert(M.history.buf, buf or vim.api.nvim_get_current_buf()) end M.push = function(stack, buf) - table.insert(stack or M.buf, 1, buf or vim.api.nvim_get_current_buf()) + table.insert(stack or M.history.buf, 1, buf or vim.api.nvim_get_current_buf()) end M.pop = function(stack, buf) - table.remove(stack or M.buf, 1) + table.remove(stack or M.history.buf, 1) end M.print = function(self) @@ -45,11 +88,11 @@ end M.back = function() local bn = vim.api.nvim_get_current_buf() - if bn > 1 and #M.buf > 0 then - M.push(M.history, bn) - local prev = M.buf[1] + if bn > 1 and #M.history.buf > 0 then + M.push(M.history.hist, bn) + local prev = M.history.buf[1] or 0 vim.api.nvim_command('buffer ' .. prev) - M.pop(M.buf) + M.pop(M.history.buf) return true else if M.config.silent then @@ -61,11 +104,11 @@ end M.forward = function() local cb = vim.api.nvim_get_current_buf() - local hb = M.history[1] + local hb = M.history.hist[1] if hb then - M.push(M.buf, cb) + M.push(M.history.buf, cb) vim.api.nvim_command('buffer ' .. hb) - M.pop(M.history) + M.pop(M.history.hist) return true else if not M.config.silent then @@ -75,10 +118,6 @@ M.forward = function() end end ----@alias down..history.Store down.Store Store ----@type down..history.Store Store -M.store = {} - ---@class down..history.Config M.config = { @@ -96,16 +135,23 @@ M.setup = function() } end -M.handle = function(event) - if event.id == 'cmd.events..history.back' then - -- Get all the buffers - end -end +M.commands = { + prev = { + args = 0, + condition = 'markdown', + name = 'data.history.back', + }, + next = { + args = 0, + condition = 'markdown', + name = 'data.history.forward', + }, +} M.handle = { cmd = { ['data.history.back'] = function(e) - local buffers = vim.api.nvim_list_bufs() + local buffers = vim.api.nvim_list_buf() local to_delete = {} for buffer in vim.iter(buffers):rev() do diff --git a/lua/down/mod/link/init.lua b/lua/down/mod/link/init.lua index 3b0eca5..33c56be 100644 --- a/lua/down/mod/link/init.lua +++ b/lua/down/mod/link/init.lua @@ -39,49 +39,16 @@ Link.maps = { -- { -- 'n', -- '', - -- ':lua require("down.mod.link").goto_prev_link()', + -- ':lua require("down.mod.link").goto.prev()', -- { desc = 'Previous link', silent = true, noremap = false, nowait = true }, -- }, -- { -- 'n', -- '', - -- ':lua require("down.mod.link").goto_next_link()', + -- ':lua require("down.mod.link").goto.next()', -- { desc = 'Next link', silent = true, noremap = false, nowait = true }, -- }, } - -Link.commands = { - link = { - name = 'link', - args = 1, - condition = 'markdown', - callback = function(e) - local cmd = e.body[1] - log.trace('Link.commands.link: Callback', cmd) - end, - subcommands = { - next = { - name = 'link.next', - args = 0, - condition = 'markdown', - callback = Link.goto_next_link, - }, - previous = { - name = 'link.previous', - args = 0, - condition = 'markdown', - callback = Link.goto_prev_link, - }, - select = { - name = 'link.select', - args = 0, - condition = 'markdown', - callback = Link.select_link, - }, - }, - }, -} - Link.load = function() end Link.parser = function() end @@ -93,7 +60,7 @@ Link.follow = {} ---| "web" ---| "heading" Link.type = { - ['local'] = true, + ['file'] = true, ['heading'] = true, ['web'] = true, } @@ -127,15 +94,15 @@ end Link.resolve = function(ln) local ch = ln:sub(1, 1) if ch == config.pathsep then - return ln, 'local' + return ln, 'file' elseif ch == '#' then return ln:sub(2), 'heading' elseif ch == '~' then - return os.getenv 'HOME' .. config.pathsep .. ln:sub(2), 'local' + return os.getenv 'HOME' .. config.pathsep .. ln:sub(2), 'file' elseif ln:sub(1, 8) == 'https://' or ln:sub(1, 7) == 'http://' then return ln, 'web' else - return vim.fn.expand '%:p:h' .. config.pathsep .. ln, 'local' + return vim.fn.expand '%:p:h' .. config.pathsep .. ln, 'file' end end @@ -153,12 +120,18 @@ Link.parent = function(node) return parent, parent:type() end -Link.next_node = function(node) +Link.node = { +prev = function(node) + local next = tsu.get_prev_node(node) + return next, next:type() +end, +next = function(node) local next = tsu.get_next_node(node) return next, next:type() end +} -Link.next_link = function(node) +Link.next = function(node) local next = tsu.get_next_node(node) if not next then return @@ -166,10 +139,10 @@ Link.next_link = function(node) if Link.destination(next) ~= nil then return next end - return Link.next_link(next) + return Link.next(next) end -Link.prev_link = function(node) +Link.prev = function(node) local prev = tsu.get_prev_node(node) if not prev then return @@ -177,27 +150,30 @@ Link.prev_link = function(node) if Link.destination(prev) ~= nil then return prev end - return Link.prev_link(prev) + return Link.prev(prev) end +Link.goto = { -Link.goto_next_link = function() - local node, nodety = Link.cursor() - local next = Link.next_link(node) +next = function() + local node, _ = Link.cursor() + local next = Link.next(node) if next then tsu.goto_node(next) end -end +end, -Link.goto_prev_link = function() - local node, nodety = Link.cursor() - local prev = Link.prev_link(node) +prev = function() + local node, _ = Link.cursor() + local prev = Link.prev(node) if prev then tsu.goto_node(prev) end end -Link.select_link = function() - local node, nodety = Link.cursor() +} + +Link.select = function() + local node, _ = Link.cursor() local dest = Link.destination(node) if dest then vim.fn.setreg('*', dest) @@ -278,7 +254,7 @@ Link.destination = function(nd) end return Link.text(node) end - local next, nextty = Link.next_node(node) + local next, nextty = Link.node.next(node) if nextty == 'link_destination' then return Link.text(next) elseif nextty == 'link_label' then @@ -297,13 +273,12 @@ Link.destination = function(nd) end end end - return end ---@class down.mod.link.Config Link.config = {} -Link.follow.loc = function(ln) +Link.follow.file = function(ln) local mod_ln, path_ln = nil, vim.split(ln, ':') local path, line = path_ln[1], path_ln[2] if path:sub(-1) == config.pathsep then @@ -312,7 +287,7 @@ Link.follow.loc = function(ln) if vim.fn.glob(path) == '' then local dir, file = vim.fn.fnameescape(path), vim.fn.fnameescape(ix) vim.fn.mkdir(dir, 'p') - Link.dep['data.history'].push(file) + Link.dep['data.history'].add.file(file) return vim.cmd(('edit %s'):format(file)) else return vim.cmd(('edit %s'):format(vim.fn.fnameescape(ix))) @@ -353,8 +328,8 @@ Link.follow.link = function() local ld = Link.destination() if ld then local res, lty = Link.resolve(ld) - if lty == 'local' then - Link.follow.loc(res) + if lty == 'file' then + Link.follow.file(res) elseif lty == 'heading' then Link.follow.heading(res) elseif lty == 'web' then @@ -362,4 +337,69 @@ Link.follow.link = function() end end end + +Link.commands = { + link = { + name = "link", + enabled = false, + min_args = 0, + max_args = 1, + condition = "markdown", + callback = function(e) + log.trace("Link.commands.link: Callback", e.body[1]) + end, + subcommands = { + backlink = { + name = "backlink", + args = 0, + min_args = 0, + enabled = true, + max_args = 1, + condition = "markdown", + callback = function(e) + log.trace("Link.commands.backlink: Callback", e.body[1]) + end, + subcommands = { + list = { + name = "backlink.list", + args = 0, + condition = "markdown", + callback = function() + local hg = Link.dep['data.history'].get() + if hg then + for _, hi in ipairs(hg.get) do + print(hi, hg) + end + end + end, + }, + }, + }, + next = { + name = "link.next", + min_args = 0, + max_args = 1, + condition = "markdown", + subcommands = {}, + callback = Link.goto.next, + }, + previous = { + name = "link.previous", + min_args = 0, + max_args = 1, + condition = "markdown", + callback = Link.goto.prev, + subcommands = {}, + }, + select = { + name = "link.select", + min_args = 0, + max_args = 1, + condition = "markdown", + subcommands = {}, + callback = Link.select, + }, + }, + }, +} return Link diff --git a/lua/down/mod/log/cmd.lua b/lua/down/mod/log/cmd.lua deleted file mode 100644 index 3ad4442..0000000 --- a/lua/down/mod/log/cmd.lua +++ /dev/null @@ -1,47 +0,0 @@ -return { - log = { - min_args = 1, - max_args = 2, - name = 'log', - callback = function(e) - log.trace 'log' - end, - subcommands = { - index = { - args = 0, - name = 'log.index', - callback = M.open_index, - }, - month = { - max_args = 1, - name = 'log.month', - callback = M.open_month, - }, - tomorrow = { - args = 0, - name = 'log.tomorrow', - callback = M.log_tomorrow, - }, - yesterday = { - args = 0, - callback = M.log_yesterday, - name = 'log.yesterday', - }, - new = { - args = 0, - callback = M.log_new, - name = 'log.new', - }, - custom = { - callback = M.calendar_months, - max_args = 1, - name = 'log.custom', - }, -- format :yyyy-mm-dd - template = { - args = 0, - name = 'log.template', - callback = M.create_template, - }, - }, - }, -} diff --git a/lua/down/mod/log/init.lua b/lua/down/mod/log/init.lua index 04f03a3..2494e41 100644 --- a/lua/down/mod/log/init.lua +++ b/lua/down/mod/log/init.lua @@ -12,6 +12,7 @@ M.commands = { log = { min_args = 1, max_args = 2, + enabled = false, name = 'log', callback = function(e) log.trace(('log %s'):format(e.body)) @@ -119,10 +120,10 @@ M.calendar_months = function(e) M.open_log( nil, ('%04d'):format(osdate.year) - .. '-' - .. ('%02d'):format(osdate.month) - .. '-' - .. ('%02d'):format(osdate.day) + .. '-' + .. ('%02d'):format(osdate.month) + .. '-' + .. ('%02d'):format(osdate.day) ) end), }) @@ -160,7 +161,7 @@ M.open_log = function(time, custom_date) local path = os.date( type(M.config.strategy) == 'function' and M.config.strategy(os.date('*t', time)) - or M.config.strategy, + or M.config.strategy, time ) @@ -173,20 +174,20 @@ M.open_log = function(time, custom_date) M.dep['workspace'].new_file(folder_name .. config.pathsep .. path, workspace) if - not log_file_exists - and M.config.use_template - and M.dep['workspace'].file_exists( - workspace_path .. config.pathsep .. folder_name .. config.pathsep .. template_name - ) + not log_file_exists + and M.config.use_template + and M.dep['workspace'].file_exists( + workspace_path .. config.pathsep .. folder_name .. config.pathsep .. template_name + ) then vim.cmd( '$read ' - .. workspace_path - .. config.pathsep - .. folder_name - .. config.pathsep - .. template_name - .. '| silent! w' + .. workspace_path + .. config.pathsep + .. folder_name + .. config.pathsep + .. template_name + .. '| silent! w' ) end end @@ -255,7 +256,7 @@ M.create_toc = function() local get_fs_handle = function(path) path = path or '' local handle = - vim.loop.fs_scandir(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. path) + vim.loop.fs_scandir(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. path) if type(handle) ~= 'userdata' then error(lib.lazy_string_concat("Failed to scan directory '", workspace, path, "': ", handle)) @@ -267,7 +268,7 @@ M.create_toc = function() -- Gets the title from the metadata of a file, must be called in a vim.schedule local get_title = function(file) local buffer = - vim.fn.bufadd(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. file) + vim.fn.bufadd(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. file) local meta = M.dep['workspace'].get_document_metadata(buffer) return meta.title end @@ -326,16 +327,16 @@ M.create_toc = function() tonumber(mname), tonumber(file[1]), '{:$' - .. workspace_name_for_link - .. config.pathsep - .. M.config.log_folder - .. config.pathsep - .. name - .. config.pathsep - .. mname - .. config.pathsep - .. file[1] - .. ':}', + .. workspace_name_for_link + .. config.pathsep + .. M.config.log_folder + .. config.pathsep + .. name + .. config.pathsep + .. mname + .. config.pathsep + .. file[1] + .. ':}', title, }) end) @@ -366,12 +367,12 @@ M.create_toc = function() parts[2], parts[3], '{:$' - .. workspace_name_for_link - .. config.pathsep - .. M.config.log_folder - .. config.pathsep - .. file[1] - .. ':}', + .. workspace_name_for_link + .. config.pathsep + .. M.config.log_folder + .. config.pathsep + .. file[1] + .. ':}', title, }) end) @@ -381,25 +382,25 @@ M.create_toc = function() vim.schedule(function() -- Gets a base format for the entries local format = M.config.toc_format - or function(entries) - local months_text = require 'down.mod.note.util'.months - local output = {} - local current_year, current_month - for _, entry in ipairs(entries) do - if not current_year or current_year < entry[1] then - current_year = entry[1] - current_month = nil - output:insert('* ' .. current_year) - end - if not current_month or current_month < entry[2] then - current_month = entry[2] - output:insert('** ' .. months_text[current_month]) - end - -- Prints the file link - output:insert(' ' .. entry[4] .. ('[%s]'):format(entry[5])) + or function(entries) + local months_text = require 'down.mod.note.util'.months + local output = {} + local current_year, current_month + for _, entry in ipairs(entries) do + if not current_year or current_year < entry[1] then + current_year = entry[1] + current_month = nil + output:insert('* ' .. current_year) + end + if not current_month or current_month < entry[2] then + current_month = entry[2] + output:insert('** ' .. months_text[current_month]) end - return output + -- Prints the file link + output:insert(' ' .. entry[4] .. ('[%s]'):format(entry[5])) end + return output + end M.dep['workspace'].new_file( folder_name .. config.pathsep .. index, diff --git a/lua/down/mod/cmd/mod/README.md b/lua/down/mod/mod/README.md similarity index 100% rename from lua/down/mod/cmd/mod/README.md rename to lua/down/mod/mod/README.md diff --git a/lua/down/mod/cmd/mod/init.lua b/lua/down/mod/mod/init.lua similarity index 94% rename from lua/down/mod/cmd/mod/init.lua rename to lua/down/mod/mod/init.lua index 0914612..5ec2968 100644 --- a/lua/down/mod/cmd/mod/init.lua +++ b/lua/down/mod/mod/init.lua @@ -2,11 +2,13 @@ local map = require 'down.util.maps' local mod = require 'down.mod' local log = require 'down.util.log' ----@class down.mod.cmd.Mod: down.Mod -local M = mod.new 'cmd.mod' +---@class down.mod.Mod: down.Mod +local M = mod.new 'mod' M.commands = { + -- enabled = false, mod = { + enabled = true, name = 'mod', args = 1, callback = function(e) @@ -16,6 +18,7 @@ M.commands = { new = { args = 1, name = 'mod.new', + enabled = false, callback = function() log.trace 'Mod.commands.new: Callback' end, @@ -23,6 +26,7 @@ M.commands = { load = { name = 'mod.load', args = 1, + enabled = false, callback = function(e) local ok = pcall(mod.load_mod, e.body[1]) if not ok then diff --git a/lua/down/mod/note/init.lua b/lua/down/mod/note/init.lua index 467ef27..2255835 100644 --- a/lua/down/mod/note/init.lua +++ b/lua/down/mod/note/init.lua @@ -11,10 +11,10 @@ local map = util.maps local M = mod.new 'note' M.maps = { - { 'n', ',dn', 'Down note today', 'Down today note' }, + { 'n', ',dn', 'Down note today', 'Down today note' }, { 'n', ',dy', 'Down note yesterday', 'Down yesterday note' }, - { 'n', ',dt', 'Down note tomorrow', 'Down tomorrow note' }, - { 'n', ',dc', 'Down note capture', 'Down capture note' }, + { 'n', ',dt', 'Down note tomorrow', 'Down tomorrow note' }, + { 'n', ',dc', 'Down note capture', 'Down capture note' }, } ---@class down.mod.note.Data @@ -39,12 +39,12 @@ M.month_index = function() local ws = M.config.workspace or M.dep['workspace'].get_current_workspace()[1] local ws_path = M.dep['workspace'].get_workspace(ws) local ix = M.config.note_folder - .. config.pathsep - .. yr - .. config.pathsep - .. mo - .. config.pathsep - .. M.config.index + .. config.pathsep + .. yr + .. config.pathsep + .. mo + .. config.pathsep + .. M.config.index local path = ws_path .. config.pathsep .. ix local index_exists = M.dep['workspace'].file_exists(path) if index_exists then @@ -89,7 +89,7 @@ M.open_year = function(time, custom_date) local path = os.date( type(M.config.strategy) == 'function' and M.config.strategy(os.date('*t', time)) - or M.config.strategy, + or M.config.strategy, time ) @@ -100,20 +100,20 @@ M.open_year = function(time, custom_date) M.dep['workspace'].new_file(folder_name .. config.pathsep .. path, workspace) if - not note_file_exists - and M.config.template.enable - and M.dep['workspace'].file_exists( - workspace_path .. config.pathsep .. folder_name .. config.pathsep .. tmpl - ) + not note_file_exists + and M.config.template.enable + and M.dep['workspace'].file_exists( + workspace_path .. config.pathsep .. folder_name .. config.pathsep .. tmpl + ) then vim.cmd( '$read ' - .. workspace_path - .. config.pathsep - .. folder_name - .. config.pathsep - .. tmpl - .. '| silent! w' + .. workspace_path + .. config.pathsep + .. folder_name + .. config.pathsep + .. tmpl + .. '| silent! w' ) end end @@ -142,7 +142,7 @@ M.open_month = function(time, custom_date) local path = os.date( type(M.config.strategy) == 'function' and M.config.strategy(os.date('*t', time)) - or M.config.strategy, + or M.config.strategy, time ) @@ -153,20 +153,20 @@ M.open_month = function(time, custom_date) M.dep['workspace'].new_file(folder_name .. config.pathsep .. path, workspace) if - not note_file_exists - and M.config.template.enable - and M.dep['workspace'].file_exists( - workspace_path .. config.pathsep .. folder_name .. config.pathsep .. tmpl - ) + not note_file_exists + and M.config.template.enable + and M.dep['workspace'].file_exists( + workspace_path .. config.pathsep .. folder_name .. config.pathsep .. tmpl + ) then vim.cmd( '$read ' - .. workspace_path - .. config.pathsep - .. folder_name - .. config.pathsep - .. tmpl - .. '| silent! w' + .. workspace_path + .. config.pathsep + .. folder_name + .. config.pathsep + .. tmpl + .. '| silent! w' ) end end @@ -205,19 +205,19 @@ M.open_note = function(time, custom_date) local path = os.date( type(M.config.strategy) == 'function' and M.config.strategy(os.date('*t', time)) - or M.config.strategy, + or M.config.strategy, time ) local note_file_exists = - M.dep['workspace'].file_exists(workspace_path .. '/' .. folder_name .. config.pathsep .. path) + M.dep['workspace'].file_exists(workspace_path .. '/' .. folder_name .. config.pathsep .. path) M.dep['workspace'].new_file(folder_name .. config.pathsep .. path, workspace) if - not note_file_exists - and M.config.template.enable - and M.dep['workspace'].file_exists(workspace_path .. '/' .. folder_name .. '/' .. tmpl) + not note_file_exists + and M.config.template.enable + and M.dep['workspace'].file_exists(workspace_path .. '/' .. folder_name .. '/' .. tmpl) then vim.cmd('$read ' .. workspace_path .. '/' .. folder_name .. '/' .. tmpl .. '| silent! w') end @@ -320,7 +320,7 @@ M.create_toc = function() local get_fs_handle = function(path) path = path or '' local handle = - vim.loop.fs_scandir(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. path) + vim.loop.fs_scandir(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. path) if type(handle) ~= 'userdata' then error(lib.lazy_string_concat("Failed to scan directory '", workspace, path, "': ", handle)) @@ -332,7 +332,7 @@ M.create_toc = function() -- Gets the title from the metadata of a file, must be called in a vim.schedule local get_title = function(file) local buffer = - vim.fn.bufadd(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. file) + vim.fn.bufadd(workspace_path .. config.pathsep .. folder_name .. config.pathsep .. file) local meta = M.dep['workspace'].get_document_metadata(buffer) return meta.title end @@ -384,16 +384,16 @@ M.create_toc = function() tonumber(mname), tonumber(file[1]), '{:$' - .. workspace_name_for_link - .. config.pathsep - .. M.config.note_folder - .. config.pathsep - .. name - .. config.pathsep - .. mname - .. config.pathsep - .. file[1] - .. ':}', + .. workspace_name_for_link + .. config.pathsep + .. M.config.note_folder + .. config.pathsep + .. name + .. config.pathsep + .. mname + .. config.pathsep + .. file[1] + .. ':}', title, }) end) @@ -427,12 +427,12 @@ M.create_toc = function() parts[2], parts[3], '{:$' - .. workspace_name_for_link - .. config.pathsep - .. M.config.note_folder - .. config.pathsep - .. file[1] - .. ':}', + .. workspace_name_for_link + .. config.pathsep + .. M.config.note_folder + .. config.pathsep + .. file[1] + .. ':}', title, }) end) @@ -441,28 +441,28 @@ M.create_toc = function() vim.schedule(function() local format = M.config.toc_format - or function(entries) - local months_text = M.months - local output = {} - local current_year, current_month - for _, entry in ipairs(entries) do - if not current_year or current_year < entry[1] then - current_year = entry[1] - current_month = nil - output:insert('* ' .. current_year) - end - if not current_month or current_month < entry[2] then - current_month = entry[2] - output:insert('** ' .. months_text[current_month]) + or function(entries) + local months_text = M.months + local output = {} + local current_year, current_month + for _, entry in ipairs(entries) do + if not current_year or current_year < entry[1] then + current_year = entry[1] + current_month = nil + output:insert('* ' .. current_year) + end + if not current_month or current_month < entry[2] then + current_month = entry[2] + output:insert('** ' .. months_text[current_month]) + end + + -- Prints the file link + output:insert(' ' .. entry[4] .. ('[%s]'):format(entry[5])) end - -- Prints the file link - output:insert(' ' .. entry[4] .. ('[%s]'):format(entry[5])) + return output end - return output - end - M.dep['workspace'].new_file( folder_name .. config.pathsep .. index, workspace or M.dep['workspace'].get_current_workspace()[1] @@ -531,10 +531,10 @@ M.open_month_calendar = function(event) M.open_note( nil, ('%04d'):format(osdate.year) - .. '-' - .. ('%02d'):format(osdate.month) - .. '-' - .. ('%02d'):format(osdate.day) + .. '-' + .. ('%02d'):format(osdate.month) + .. '-' + .. ('%02d'):format(osdate.day) ) end), }) @@ -549,9 +549,11 @@ M.commands = { max_args = 1, callback = M.open_month_calendar, name = 'calendar', + enabled = true, }, -- format :yyyy-mm-dd note = { name = 'note', + enabled = true, callback = function(e) log.trace 'note' end, @@ -561,16 +563,19 @@ M.commands = { index = { callback = M.note_index, args = 0, + enabled = true, name = 'note.index', }, month = { max_args = 1, + enabled = true, name = 'note.month', callback = M.month_index, subcommands = { index = { callback = M.month_index, args = 0, + enabled = true, name = 'note.month.index', }, previous = { @@ -632,6 +637,7 @@ M.commands = { capture = { callback = M.capture, args = 0, + enabled = false, name = 'note.capture', }, tomorrow = { @@ -682,6 +688,7 @@ M.commands = { name = 'note.template', }, toc = { + enabled = false, args = 1, name = 'note.toc', callback = M.open_toc, diff --git a/lua/down/mod/template/init.lua b/lua/down/mod/template/init.lua index da9870f..4fb8956 100644 --- a/lua/down/mod/template/init.lua +++ b/lua/down/mod/template/init.lua @@ -8,27 +8,32 @@ local M = mod.new 'template' M.commands = { template = { min_args = 1, + enabled = false, max_args = 2, name = 'template', callback = M.create_template, subcommands = { index = { + enabled = true, callback = M.open_index, args = 0, name = 'template.index', }, month = { max_args = 1, + enabled = false, name = 'template.month', callback = M.open_month, }, tomorrow = { callback = M.template_tomorrow, + enabled = false, args = 0, name = 'template.tomorrow', }, yesterday = { args = 0, + enabled = false, name = 'template.yesterday', M.template_yesterday, }, @@ -36,14 +41,17 @@ M.commands = { args = 0, name = 'template.today', callback = M.template_today, + enabled = false, }, custom = { callback = M.create_template, max_args = 1, + enabled = false, name = 'template.custom', }, -- format :yyyy-mm-dd template = { callback = M.create_template, + enabled = false, args = 0, name = 'template.template', }, diff --git a/lua/down/mod/tool/telescope/init.lua b/lua/down/mod/tool/telescope/init.lua index c4d5de8..183a056 100644 --- a/lua/down/mod/tool/telescope/init.lua +++ b/lua/down/mod/tool/telescope/init.lua @@ -13,7 +13,7 @@ M.setup = function() } end ----@class down.mod.Data +---@class down.mod.tool.telescope.Config: down.Mod.Config M.config = { enabled = { 'files', @@ -31,7 +31,7 @@ M.config = { -- "backlinks.header_backlinks", }, } ----@class down.mod.Config +---@class down.mod.tool.telescope.Config: down.mod.Config M.config = { enabled = { ['backlinks'] = true, diff --git a/lua/down/mod/ui/icon/init.lua b/lua/down/mod/ui/icon/init.lua index b368c9e..d460f65 100644 --- a/lua/down/mod/ui/icon/init.lua +++ b/lua/down/mod/ui/icon/init.lua @@ -1383,15 +1383,14 @@ M.load = function() -- M.dep["core.autocommands"].enable_autocommand("BufNewFile") - mod.await('cmd', function(downcmd) - downcmd.add_commands_from_table({ - toggle = { - name = 'icon.toggle', - args = 0, - condition = 'markdown', - }, - }) - end) + M.commands = { + toggle = { + enabled = false, + name = 'icon.toggle', + args = 0, + condition = 'markdown', + }, + } vim.api.nvim_create_autocmd('OptionSet', { pattern = 'conceallevel', diff --git a/lua/down/mod/workspace/init.lua b/lua/down/mod/workspace/init.lua index 7b2b2a6..9439725 100644 --- a/lua/down/mod/workspace/init.lua +++ b/lua/down/mod/workspace/init.lua @@ -525,7 +525,9 @@ M.is_subpath = function(p, wsname) end M.commands = { + -- enabled = false, index = { + -- enabled = false, args = 0, max_args = 1, name = 'workspace.index', diff --git a/lua/init.lua b/lua/init.lua new file mode 100644 index 0000000..372c712 --- /dev/null +++ b/lua/init.lua @@ -0,0 +1,96 @@ +local map = require 'down.util.maps' +local mod = require 'down.mod' +local log = require 'down.util.log' + +---@class down.mod.cmd.Mod: down.Mod +local M = mod.new 'mod' + +M.commands = { + mod = { + name = 'mod', + args = 1, + callback = function(e) + log.trace 'Mod.commands.mod: Callback' + end, + subcommands = { + new = { + args = 1, + name = 'mod.new', + callback = function() + log.trace 'Mod.commands.new: Callback' + end, + }, + load = { + name = 'mod.load', + args = 1, + callback = function(e) + local ok = pcall(mod.load_mod, e.body[1]) + if not ok then + vim.notify(('mod `%s` does not exist!'):format(e.body[1]), vim.log.levels.ERROR, {}) + end + vim.print(ok) + end, + }, + unload = { + name = 'mod.unload', + args = 1, + callback = function(e) + log.trace 'Mod.commands.unload: Callback' + end, + }, + list = { + args = 0, + name = 'mod.list', + callback = function(e) + local mods_popup = require 'nui.popup' { + position = '50%', + size = { width = '50%', height = '80%' }, + enter = true, + buf_options = { + filetype = 'markdown', + modifiable = true, + readonly = false, + }, + win_options = { + conceallevel = 3, + concealcursor = 'nvic', + }, + } + mods_popup:on('VimResized', function() + mods_popup:update_layout() + end) + + local function close() + mods_popup:unmount() + end + + mods_popup:map('n', '', close, {}) + mods_popup:map('n', 'q', close, {}) + local lines = {} + table.insert(lines, '# Mods loaded') + table.insert(lines, '') + table.insert(lines, '') + table.insert(lines, '## Mods:') + table.insert(lines, '') + table.insert(lines, '') + for name, _ in pairs(mod.mods) do + table.insert(lines, '1. `' .. name .. '`') + end + vim.api.nvim_buf_set_lines(mods_popup.bufnr, 0, -1, true, lines) + vim.bo[mods_popup.bufnr].modifiable = false + mods_popup:mount() + end, + }, + }, + }, +} +M.maps = { + { 'n', ',dml', 'Down mod list', 'List mods' }, + { 'n', ',dmL', 'Down mod load', 'Load mod' }, + { 'n', ',dmu', 'Down mod unload', 'Unload mod' }, +} +M.setup = function() + return { loaded = true, dependencies = { 'cmd' } } +end + +return M