From ca0e564884d6db277a43770a1c05261b4be12a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Volbers?= Date: Sat, 20 Nov 2021 09:38:06 +0100 Subject: [PATCH] Add command "collect" This commit introduces a new command verb "collect", which basically passes its arguments to a function which can be defined by the user. Per default, the new command passes its arguments to `delve-insert-nodes-by-id' if the package `Delve' is present. Also rewrites the command dispatcher so that it uses the more idiomatic pcase statement. Aside from being nicer to read, pcase allows us to introduce more complex command patterns if simple one word strings won't suffice. --- org-roam-ui.el | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/org-roam-ui.el b/org-roam-ui.el index 61b8ea65..227cd1cd 100644 --- a/org-roam-ui.el +++ b/org-roam-ui.el @@ -37,6 +37,7 @@ (require 'org-roam) (require 'org-roam-dailies) (require 'websocket) +(require 'cl-lib) ;; for implicit cl-block around dolist (defgroup org-roam-ui nil "UI in Org-roam." @@ -131,6 +132,15 @@ Defaults to #'browse-url." :group 'org-roam-ui :type 'function) +(defcustom org-roam-ui-collect-function '((delve . delve-insert-nodes-by-id)) + "Alist defining functions which collects nodes by passing their ids. +The key is the name of the package which contains that function. +The first package which is installed will be used. The +associated function must accept two arguments: A collection name +and a list of node ids." + :group 'org-roam-ui + :type 'alist) + ;;Hooks (defcustom org-roam-ui-before-open-node-functions nil @@ -210,16 +220,29 @@ Takes _WS and FRAME as arguments." (websocket-frame-text frame) :object-type 'alist)) (command (alist-get 'command msg)) (data (alist-get 'data msg))) - (cond ((string= command "open") - (org-roam-ui--on-msg-open-node data)) - ((string= command "delete") - (org-roam-ui--on-msg-delete-node data)) - ((string= command "create") - (org-roam-ui--on-msg-create-node data)) - (t - (message + (pcase command + ("open" (org-roam-ui--on-msg-open-node data)) + ("delete" (org-roam-ui--on-msg-delete-node data)) + ("create" (org-roam-ui--on-msg-create-node data)) + ("collect" (org-roam-ui--on-msg-collect-node)) + (_ (message "Something went wrong when receiving a message from org-roam-ui"))))) +(defun org-roam-ui--on-msg-collect-node (data) + "Add a node or nodes specified in DATA to a collection. +Assumes DATA to be an alist with the following keys: `id', +`collection'. ID is a node ID or a list of IDs; COLLECTION is +the name of the collection (a string). Pass both arguments to +the first available function found in +`org-roam-ui-collect-function'." + (let* ((collection (alist-get 'collection data)) + (ids (alist-get 'id data))) + (or (pcase-dolist (`(,package-name . ,fn) org-roam-ui-collect-function) + (when (featurep package-name) + (funcall fn collection (if (listp ids) ids (list ids))) + (cl-return t))) + (error "No function defined for collecting nodes, see `org-roam-ui-collect-function'")))) + (defun org-roam-ui--on-msg-open-node (data) "Open a node when receiving DATA from the websocket." (let* ((node (org-roam-node-from-id (alist-get'id data)))