aboutsummaryrefslogtreecommitdiff
path: root/lua/simple-denote.lua
blob: 12ba1f6b2f395b135ed8b3853120a0240a8ca967 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
local M = {}

local config = require("simple-denote.config")
local api = require("simple-denote.api")
local search = require ("simple-denote.search")

function M.load_cmd(options)
  vim.api.nvim_create_user_command("Denote", function(opts)
    if opts.fargs[1] == "note" then
      api.note(options)
    elseif opts.fargs[1] == "title" then
      api.title(options)
    elseif opts.fargs[1] == "tag" then
      api.tag(options)
    elseif opts.fargs[1] == "signature" then
      api.signature(options)
    elseif opts.fargs[1] == "search" then
      search.search(options)
    elseif opts.fargs[1] == "test" then
      search.test()
    else
      error("Unsupported operation " .. opts.fargs[1])
    end
  end, {
    nargs = 1,
    complete = function()
      return { "note", "title", "tag", "signature" }
    end,
  })
end

---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

return M