feat: Limit link replacement scope

Test for `roam:` link prefix to limit the number of times we need to
call `org-roam-link-replace-at-point`.
This commit is contained in:
Cash Prokop-Weaver
2025-06-29 15:25:41 -07:00
committed by Liam Hupfer
parent 1958e035fc
commit fc8638759b

View File

@ -167,6 +167,10 @@ This path is relative to `org-roam-directory'."
:group 'org-roam :group 'org-roam
:type 'string) :type 'string)
(defvar org-roam-link-type "roam"
"Link type for org-roam nodes.
Replaced by `id' automatically when `org-roam-link-auto-replace' is non-nil.")
(defvar org-roam-node-history nil (defvar org-roam-node-history nil
"Minibuffer history of nodes.") "Minibuffer history of nodes.")
@ -768,7 +772,7 @@ The INFO, if provided, is passed to the underlying `org-roam-capture-'."
(deactivate-mark))) (deactivate-mark)))
;;;;; [roam:] link ;;;;; [roam:] link
(org-link-set-parameters "roam" :follow #'org-roam-link-follow-link) (org-link-set-parameters org-roam-link-type :follow #'org-roam-link-follow-link)
(defun org-roam-link-follow-link (title-or-alias) (defun org-roam-link-follow-link (title-or-alias)
"Navigate \"roam:\" link to find and open the node with TITLE-OR-ALIAS. "Navigate \"roam:\" link to find and open the node with TITLE-OR-ALIAS.
Assumes that the cursor was put where the link is." Assumes that the cursor was put where the link is."
@ -797,7 +801,7 @@ Assumes that the cursor was put where the link is."
node) node)
(goto-char (org-element-property :begin link)) (goto-char (org-element-property :begin link))
(when (and (org-in-regexp org-link-any-re 1) (when (and (org-in-regexp org-link-any-re 1)
(string-equal type "roam") (string-equal type org-roam-link-type)
(setq node (save-match-data (org-roam-node-from-title-or-alias path)))) (setq node (save-match-data (org-roam-node-from-title-or-alias path))))
(replace-match (org-link-make-string (replace-match (org-link-make-string
(concat "id:" (org-roam-node-id node)) (concat "id:" (org-roam-node-id node))
@ -806,9 +810,10 @@ Assumes that the cursor was put where the link is."
(defun org-roam-link-replace-all () (defun org-roam-link-replace-all ()
"Replace all \"roam:\" links in buffer with \"id:\" links." "Replace all \"roam:\" links in buffer with \"id:\" links."
(interactive) (interactive)
(org-with-point-at 1 (let ((org-roam-link-prefix (concat "[[" org-roam-link-type ":")))
(while (re-search-forward org-link-bracket-re nil t) (org-with-point-at 1
(org-roam-link-replace-at-point)))) (while (re-search-forward org-roam-link-prefix nil t)
(org-roam-link-replace-at-point)))))
(add-hook 'org-roam-find-file-hook #'org-roam--replace-roam-links-on-save-h) (add-hook 'org-roam-find-file-hook #'org-roam--replace-roam-links-on-save-h)
(defun org-roam--replace-roam-links-on-save-h () (defun org-roam--replace-roam-links-on-save-h ()