From 67b156258219833599209d2e499c230c13d6e86e Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Tue, 20 Apr 2021 20:23:49 +0800 Subject: [PATCH] fix completions for roam: links --- org-roam-completion.el | 6 +++--- org-roam.el | 44 +++++++++--------------------------------- 2 files changed, 12 insertions(+), 38 deletions(-) diff --git a/org-roam-completion.el b/org-roam-completion.el index 5ac15f2..53ca850 100644 --- a/org-roam-completion.el +++ b/org-roam-completion.el @@ -99,10 +99,10 @@ This is a `completion-at-point' function, and is active when (pcase (org-element-lineage context '(link) t) (`nil nil) (link - (let ((link-type (org-element-property :type link))) - (when (member link-type '("roam" "fuzzy")) + (setq link-type (org-element-property :type link)) + (when (member link-type '("roam" "fuzzy")) (when (string= link-type "roam") (setq start (+ start (length "roam:")))) - (setq collection #'org-roam--get-titles))))))) + (setq collection #'org-roam--get-titles)))))) (when collection (let ((prefix (buffer-substring-no-properties start end))) (list start end diff --git a/org-roam.el b/org-roam.el index bca5e99..a87227c 100644 --- a/org-roam.el +++ b/org-roam.el @@ -586,9 +586,9 @@ instead." (defun org-roam-node-from-id (id) "Return an `org-roam-node' for the node containing ID. Return nil if a node with ID does not exist." - (when (org-roam-db-query [:select (funcall count) :from nodes - :where (= id $s1)] - id) + (when (> (caar (org-roam-db-query [:select (funcall count) :from nodes + :where (= id $s1)] + id)) 0) (org-roam-populate (org-roam-node-create :id id)))) (defun org-roam-node-from-title-or-alias (s) @@ -1063,39 +1063,13 @@ the link." (let* ((title (org-element-property :path link)) (node (org-roam-node-from-title-or-alias title))) (when org-roam-link-auto-replace - (org-roam-link--replace-link (org-roam-node-id node) path)) + (save-excursion + (save-match-data + (org-in-regexp org-link-bracket-re 1) + (replace-match (org-link-make-string + (concat "id:" (org-roam-node-id node)) + title))))) (org-id-goto (org-roam-node-id node))))))) -(defun org-roam-link--replace-link (id &optional desc) - "Replace link at point with a vanilla Org link. -LINK-TYPE is the Org link type, typically \"file\" or \"id\". -ID is id for the Org-roam node. -DESC is the link description." - (save-excursion - (save-match-data - (unless (org-in-regexp org-link-bracket-re 1) - (user-error "No link at point")) - (replace-match "") - (insert (org-link-make-string - (concat "id:" id) - desc))))) - -;;; Retrieval Functions -(defun org-roam-link--get-node-from-title (title) - "Return the node id for a given TITLE." - (let ((nodes (seq-uniq - (append - (mapcar #'car (org-roam-db-query [:select [id] :from nodes - :where (= title $s1)] - title)) - (mapcar #'car (org-roam-db-query [:select [node-id] :from aliases - :where (= node-id $s1)] - title)))))) - (pcase nodes - ('nil nil) - (`(,node) node) - (_ - (completing-read "Select node: " nodes))))) - (provide 'org-roam) ;;; org-roam.el ends here