Skip to content

Commit

Permalink
Add support for including roam origin in capture/node_insert/node_find
Browse files Browse the repository at this point in the history
  • Loading branch information
chipsenkbeil committed Apr 14, 2024
1 parent c49f03e commit d66e098
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lua/org-roam/api/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ local io = require("org-roam.core.utils.io")
local log = require("org-roam.core.log")
local notify = require("org-roam.core.ui.notify")
local path_utils = require("org-roam.core.utils.path")
local Promise = require("orgmode.utils.promise")
local select_node = require("org-roam.ui.select-node")
local utils = require("org-roam.utils")

Expand Down Expand Up @@ -57,6 +58,27 @@ local function string_contains_one_of(s, ...)
return false
end

---Retrieves the id of the noude under cursor.
---@param opts? {win?:integer}
---@return OrgPromise<string|nil>
local function node_id_under_cursor(opts)
opts = opts or {}

return Promise.new(function(resolve)
utils.node_under_cursor(function(node)
resolve(node and node.id)
end, { win = opts.win })
end)
end

---Retrieves the id of the noude under cursor.
---@param opts? {timeout?:integer, win?:integer}
---@return string|nil
local function node_id_under_cursor_sync(opts)
opts = opts or {}
return node_id_under_cursor(opts):wait(opts.timeout)
end

---@param file? OrgFile
---@param opts? {title?:string}
---@return fun(target:string):string
Expand Down Expand Up @@ -240,6 +262,10 @@ function M.capture(opts, cb)
opts = opts or {}
cb = cb or function() end

if CONFIG.capture.include_origin and not opts.origin then
opts.origin = node_id_under_cursor_sync()
end

if opts.immediate then
M.__capture_immediate(opts, cb)
else
Expand Down Expand Up @@ -389,6 +415,10 @@ function M.insert(opts)
return
end

if CONFIG.capture.include_origin and not opts.origin then
opts.origin = node_id_under_cursor_sync({ win = winnr })
end

M.capture({
immediate = opts.immediate,
origin = opts.origin,
Expand Down Expand Up @@ -433,6 +463,10 @@ function M.find(opts)
return
end

if CONFIG.capture.include_origin and not opts.origin then
opts.origin = node_id_under_cursor_sync({ win = winnr })
end

M.capture({ origin = opts.origin, title = node.label }, function(id)
if id then
visit_node(id)
Expand Down
9 changes: 9 additions & 0 deletions lua/org-roam/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ local config = setmetatable({
toggle_roam_buffer_fixed = "<Leader>nb",
},

---Settings associated with org-roam capture logic.
---@class org-roam.config.Capture
capture = {
---If true, will include the origin in the capture buffer if the
---capture originated from an org-roam node.
---@type boolean
include_origin = true,
},

---Settings associated with org-roam's database.
---@class org-roam.config.Database
database = {
Expand Down

0 comments on commit d66e098

Please sign in to comment.