From 516d8dfbf88a3e5e96ebea524cc7a1b8569f1221 Mon Sep 17 00:00:00 2001 From: Jethro Kuan Date: Sun, 11 Apr 2021 17:10:57 +0800 Subject: [PATCH] pass node around instead --- org-roam-capture.el | 54 ++++++++++++++++++-------------------------- org-roam-dailies.el | 10 +++----- org-roam-protocol.el | 6 +++-- org-roam.el | 8 +++++-- 4 files changed, 35 insertions(+), 43 deletions(-) diff --git a/org-roam-capture.el b/org-roam-capture.el index c58c49e..6491939 100644 --- a/org-roam-capture.el +++ b/org-roam-capture.el @@ -40,7 +40,6 @@ ;; Declarations (defvar org-roam-directory) -(defvar org-roam-title-to-slug-function) (defvar org-roam-capture--info nil "A property-list of additional information passed to the Org-roam template. @@ -186,21 +185,17 @@ This function is to be called in the Org-capture finalization process." "Expand the template STR, returning the string. This is an extension of org-capture's template expansion. -First, it expands ${var} occurrences in STR, using `org-roam-capture--info'. -If there is a ${var} with no matching var in the alist, the value -of var is prompted for via `completing-read'. - -Next, it expands the remaining template string using -`org-capture-fill-template'." - (org-capture-fill-template - (s-format str - (lambda (key) - (let ((plist-key (intern (concat ":" key)))) - (or (plist-get org-roam-capture--info plist-key) - (when-let ((val (completing-read (format "%s: " key) nil))) - (setq org-roam-capture--info - (plist-put org-roam-capture--info plist-key val)) - val))))))) +First, it expands ${var} occurrences in STR, using the node in +`org-roam-capture--info'. Next, it expands the remaining template +string using `org-capture-fill-template'." + (let ((node (plist-get org-roam-capture--info :node))) + (org-capture-fill-template + (s-format str + (lambda (key) + (let ((fn (intern (concat "org-roam-node-" key)))) + (if (fboundp fn) + (funcall fn node) + (completing-read (format "%s: " key) nil)))))))) (defun org-roam-capture--save-file-maybe () "Save the file conditionally. @@ -310,18 +305,17 @@ you can catch it with `condition-case'." "Return exact point to file for org-capture-template. This function is used solely in Org-roam's capture templates: see `org-roam-capture-templates'." - (when-let ((time (plist-get org-roam-capture--info :time))) - (org-capture-put :default-time time)) - (let ((file-path - (cond ((plist-get org-roam-capture--info :file) - (plist-get org-roam-capture--info :file)) - ((plist-get org-roam-capture--info :ref) - (or (caar (org-roam-db-query [:select [file] - :from refs - :where (= ref $s1) - :limit 1] - (plist-get org-roam-capture--info :ref))) + (let* ((node (plist-get org-roam-capture--info :node)) + (file-path + (cond ((plist-get org-roam-capture--info :ref) + (or (caar (org-roam-db-query [:select [file] + :from refs + :where (= ref $s1) + :limit 1] + (plist-get org-roam-capture--info :ref))) (org-roam-capture--new-file))) + ((org-roam-node-file node) + (org-roam-node-file node)) (t (org-roam-capture--new-file))))) (org-capture-put :template @@ -383,9 +377,6 @@ TEMPLATES is a list of org-roam templates." (mapcar (lambda (t) (org-roam-capture--convert-template t props)) (or templates org-roam-capture-templates))) - (info (plist-put info :slug - (funcall org-roam-title-to-slug-function - (plist-get info :title)))) (org-roam-capture--info info)) (when (and (not keys) (= (length org-capture-templates) 1)) @@ -401,8 +392,7 @@ Arguments GOTO and KEYS see `org-capture'." (let ((node (org-roam-node-read))) (org-roam-capture- :goto goto :keys keys - :info (list :title (org-roam-node-title node) - :file (org-roam-node-file node)) + :info (list :node node) :props '(:immediate-finish nil)))) (provide 'org-roam-capture) diff --git a/org-roam-dailies.el b/org-roam-dailies.el index af62d53..f022378 100644 --- a/org-roam-dailies.el +++ b/org-roam-dailies.el @@ -98,13 +98,9 @@ If FILE is not specified, use the current buffer's file-path." "Capture an entry in a daily-note for TIME, creating it if necessary. When GOTO is non-nil, go the note without creating an entry." - (let ((org-roam-capture-templates (--> org-roam-dailies-capture-templates - (if goto (list (car it)) it))) - (org-roam-capture--info (list (cons 'time time))) - (org-roam-capture--context 'dailies)) - (org-roam-capture- :goto (when goto '(4)) - :info (list (cons 'time time)) - :context 'dailies))) + (org-roam-capture- :goto (when goto '(4)) + :templates org-roam-dailies-capture-templates + :props (list :default-time time))) ;;;; Commands ;;; Today diff --git a/org-roam-protocol.el b/org-roam-protocol.el index 48957a9..009a591 100644 --- a/org-roam-protocol.el +++ b/org-roam-protocol.el @@ -77,8 +77,10 @@ It opens or creates a note with the given ref. (raise-frame) (org-roam-capture- :keys (plist-get info :template) - :info info - :props `(:ref ,(plist-get info :ref)) + :info (list :node (org-roam-node-create :title (plist-get info :title)) + :ref (plist-get info :ref) + :body (plist-get info :body)) + :props (list :ref (plist-get info :ref)) :templates org-roam-capture-ref-templates) nil) diff --git a/org-roam.el b/org-roam.el index 3c89437..83c8426 100644 --- a/org-roam.el +++ b/org-roam.el @@ -448,6 +448,10 @@ OLD-FILE is cleared from the database, and NEW-FILE-OR-DIR is added." id file level point todo priority scheduled deadline title tags aliases refs) +(cl-defmethod org-roam-node-slug ((node org-roam-node)) + "Return the slug of NODE." + (funcall org-roam-title-to-slug-function (org-roam-node-title node))) + (defvar org-roam-node-map (let ((map (make-sparse-keymap))) (set-keymap-parent map org-roam-mode-map) @@ -688,7 +692,7 @@ If OTHER-WINDOW, visit the NODE in another window." (if (org-roam-node-file node) (org-roam-node-visit node other-window) (org-roam-capture- - :info `(:title ,(org-roam-node-title node)) + :info (list :node node) :props '(:finalize find-file))))) (defun org-roam-node-insert (&optional filter-fn) @@ -720,7 +724,7 @@ which takes as its argument an alist of path-completions." (concat "id:" (org-roam-node-id node)) description))) (org-roam-capture- - :info `(:title ,(org-roam-node-title node)) + :info (list :node node) :props (list :region (when (and beg end) (cons beg end)) :insert-at (point-marker)