convert info to plist

This commit is contained in:
Jethro Kuan
2021-04-11 16:32:59 +08:00
parent 1c831dcd73
commit 866368bfee
4 changed files with 69 additions and 60 deletions

View File

@ -43,7 +43,7 @@
(defvar org-roam-title-to-slug-function) (defvar org-roam-title-to-slug-function)
(defvar org-roam-capture--info nil (defvar org-roam-capture--info nil
"An alist of additional information passed to the Org-roam template. "A property-list of additional information passed to the Org-roam template.
This variable is populated dynamically, and is only non-nil This variable is populated dynamically, and is only non-nil
during the Org-roam capture process.") during the Org-roam capture process.")
@ -195,10 +195,12 @@ Next, it expands the remaining template string using
(org-capture-fill-template (org-capture-fill-template
(s-format str (s-format str
(lambda (key) (lambda (key)
(or (s--aget org-roam-capture--info key) (let ((plist-key (intern (concat ":" key))))
(when-let ((val (completing-read (format "%s: " key) nil))) (or (plist-get org-roam-capture--info plist-key)
(push (cons key val) org-roam-capture--info) (when-let ((val (completing-read (format "%s: " key) nil)))
val)))))) (setq org-roam-capture--info
(plist-put org-roam-capture--info plist-key val))
val)))))))
(defun org-roam-capture--save-file-maybe () (defun org-roam-capture--save-file-maybe ()
"Save the file conditionally. "Save the file conditionally.
@ -308,18 +310,18 @@ you can catch it with `condition-case'."
"Return exact point to file for org-capture-template. "Return exact point to file for org-capture-template.
This function is used solely in Org-roam's capture templates: see This function is used solely in Org-roam's capture templates: see
`org-roam-capture-templates'." `org-roam-capture-templates'."
(when-let ((time (cdr (assoc 'time org-roam-capture--info)))) (when-let ((time (plist-get org-roam-capture--info :time)))
(org-capture-put :default-time time)) (org-capture-put :default-time time))
(let ((file-path (let ((file-path
(cond ((assoc 'file org-roam-capture--info) (cond ((plist-get org-roam-capture--info :file)
(cdr (assoc 'file org-roam-capture--info))) (plist-get org-roam-capture--info :file))
((assoc 'ref org-roam-capture--info) ((plist-get org-roam-capture--info :ref)
(let ((ref (cdr (assoc 'ref org-roam-capture--info)))) (or (caar (org-roam-db-query [:select [file]
(or (caar (org-roam-db-query [:select [file] :from refs
:from refs :where (= ref $s1)
:where (= ref $s1) :limit 1]
:limit 1] ref)) (plist-get org-roam-capture--info :ref)))
(org-roam-capture--new-file)))) (org-roam-capture--new-file)))
(t (t
(org-roam-capture--new-file))))) (org-roam-capture--new-file)))))
(org-capture-put :template (org-capture-put :template
@ -377,11 +379,14 @@ GOTO and KEYS correspond to `org-capture' arguments.
INFO is an alist for filling up Org-roam's capture templates. INFO is an alist for filling up Org-roam's capture templates.
PROPS is a plist containing additional Org-roam properties for each template. PROPS is a plist containing additional Org-roam properties for each template.
TEMPLATES is a list of org-roam templates." TEMPLATES is a list of org-roam templates."
(let ((org-capture-templates (let* ((org-capture-templates
(mapcar (lambda (t) (mapcar (lambda (t)
(org-roam-capture--convert-template t props)) (org-roam-capture--convert-template t props))
(or templates org-roam-capture-templates))) (or templates org-roam-capture-templates)))
(org-roam-capture--info info)) (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) (when (and (not keys)
(= (length org-capture-templates) 1)) (= (length org-capture-templates) 1))
(setq keys (caar org-capture-templates))) (setq keys (caar org-capture-templates)))
@ -396,10 +401,8 @@ Arguments GOTO and KEYS see `org-capture'."
(let ((node (org-roam-node-read))) (let ((node (org-roam-node-read)))
(org-roam-capture- :goto goto (org-roam-capture- :goto goto
:keys keys :keys keys
:info `((title . ,(org-roam-node-title node)) :info (list :title (org-roam-node-title node)
(slug . ,(funcall org-roam-title-to-slug-function :file (org-roam-node-file node))
(org-roam-node-title node)))
(file . ,(org-roam-node-file node)))
:props '(:immediate-finish nil)))) :props '(:immediate-finish nil))))
(provide 'org-roam-capture) (provide 'org-roam-capture)

View File

@ -30,6 +30,18 @@
;; This library implements macros used throughout org-roam. ;; This library implements macros used throughout org-roam.
;; ;;
;;; Code: ;;; Code:
(defmacro org-roam-plist-map! (fn plist)
"Map FN over PLIST, modifying it in-place."
(declare (indent 1))
(let ((plist-var (make-symbol "plist"))
(k (make-symbol "k"))
(v (make-symbol "v")))
`(let ((,plist-var (copy-sequence ,plist)))
(while ,plist-var
(setq ,k (pop ,plist-var))
(setq ,v (pop ,plist-var))
(setq ,plist (plist-put ,plist ,k (funcall ,fn ,k ,v)))))))
(defmacro org-roam-with-file (file keep-buf-p &rest body) (defmacro org-roam-with-file (file keep-buf-p &rest body)
"Execute BODY within FILE. "Execute BODY within FILE.
If FILE is nil, execute BODY in the current buffer. If FILE is nil, execute BODY in the current buffer.

View File

@ -37,6 +37,8 @@
;;; Code: ;;; Code:
(require 'org-protocol) (require 'org-protocol)
(require 'org-roam) (require 'org-roam)
(eval-when-compile
(require 'org-roam-macs))
(require 'ol) ;; for org-link-decode (require 'ol) ;; for org-link-decode
(defcustom org-roam-protocol-store-links nil (defcustom org-roam-protocol-store-links nil
@ -54,37 +56,31 @@ It opens or creates a note with the given ref.
encodeURIComponent(location.href) + \\='&title=\\=' + \\ encodeURIComponent(location.href) + \\='&title=\\=' + \\
encodeURIComponent(document.title) + \\='&body=\\=' + \\ encodeURIComponent(document.title) + \\='&body=\\=' + \\
encodeURIComponent(window.getSelection())" encodeURIComponent(window.getSelection())"
(when-let* ((alist (org-roam--plist-to-alist info)) (unless (plist-get info :ref)
(decoded-alist (mapcar (lambda (k.v) (user-error "No ref key provided"))
(let ((key (car k.v)) (org-roam-plist-map! (lambda (k v)
(val (cdr k.v))) (when (equal k :ref)
(cons key (org-link-decode val)))) alist))) (setq v (org-protocol-sanitize-uri v)))
(unless (assoc 'ref decoded-alist) (org-link-decode v)) info)
(error "No ref key provided")) (when org-roam-protocol-store-links
(when-let ((title (cdr (assoc 'title decoded-alist)))) (push (list (plist-get info :ref)
(push (cons 'slug (funcall org-roam-title-to-slug-function title)) decoded-alist)) (plist-get info :title)) org-stored-links))
(let-alist decoded-alist (org-link-store-props :type (and (string-match org-link-plain-re
(let* ((ref (org-protocol-sanitize-uri .ref)) (plist-get info :ref))
(type (and (string-match org-link-plain-re ref) (match-string 1 (plist-get info :ref)))
(match-string 1 ref))) :link (plist-get info :ref)
(title (or .title "")) :annotation (org-link-make-string (plist-get info :ref)
(body (or .body "")) (or (plist-get info :title)
(orglink (plist-get info :ref)))
(org-link-make-string ref (or (org-string-nw-p title) ref))) :initial (or (plist-get info :body)
(org-capture-link-is-already-stored t)) ""))
(when org-roam-protocol-store-links (raise-frame)
(push (list ref title) org-stored-links)) (org-roam-capture-
(org-link-store-props :type type :keys (plist-get info :template)
:link ref :info info
:annotation orglink :props `(:ref ,(plist-get info :ref))
:initial body) :templates org-roam-capture-ref-templates)
(raise-frame) nil)
(org-roam-capture-
:keys (cdr (assoc 'template decoded-alist))
:info decoded-alist
:props (list :ref ref)
:templates org-roam-capture-ref-templates)))
nil))
(defun org-roam-protocol-open-file (info) (defun org-roam-protocol-open-file (info)
"This handler simply opens the file with emacsclient. "This handler simply opens the file with emacsclient.

View File

@ -688,9 +688,8 @@ If OTHER-WINDOW, visit the NODE in another window."
(if (org-roam-node-file node) (if (org-roam-node-file node)
(org-roam-node-visit node other-window) (org-roam-node-visit node other-window)
(org-roam-capture- (org-roam-capture-
:info `((title . ,(org-roam-node-title node)) :info `(:title ,(org-roam-node-title node))
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node)))) :props '(:finalize find-file)))))
:props (list :finalize 'find-file)))))
(defun org-roam-node-insert (&optional filter-fn) (defun org-roam-node-insert (&optional filter-fn)
"Find an Org-roam file, and insert a relative org link to it at point. "Find an Org-roam file, and insert a relative org link to it at point.
@ -721,8 +720,7 @@ which takes as its argument an alist of path-completions."
(concat "id:" (org-roam-node-id node)) (concat "id:" (org-roam-node-id node))
description))) description)))
(org-roam-capture- (org-roam-capture-
:info `((title . ,(org-roam-node-title node)) :info `(:title ,(org-roam-node-title node))
(slug . ,(funcall org-roam-title-to-slug-function (org-roam-node-title node))))
:props (list :region (when (and beg end) :props (list :region (when (and beg end)
(cons beg end)) (cons beg end))
:insert-at (point-marker) :insert-at (point-marker)