branch: externals/denote
commit d47e0df0a54c8ffd32219ec1efe801b0126929d3
Author: Protesilaos Stavrou <i...@protesilaos.com>
Commit: Protesilaos Stavrou <i...@protesilaos.com>

    Add check that SILO is among denote-silo-extras-directories
    
    This way, we do not operate on arbitrary paths. Users can still do
    that with custom code, though it is better for us to focus on the
    intent of the code.
---
 denote-silo-extras.el | 36 +++++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 9 deletions(-)

diff --git a/denote-silo-extras.el b/denote-silo-extras.el
index 1a9976bc9a..d6a8d580d9 100644
--- a/denote-silo-extras.el
+++ b/denote-silo-extras.el
@@ -69,25 +69,41 @@ as the variable `denote-directory'."
      (denote--completion-table 'file denote-silo-extras-directories)
      nil :require-match nil 'denote-silo-extras-directory-history)))
 
+(defun denote-silo-extras-path-is-silo-p (path)
+  "Return non-nil if PATH is among `denote-silo-extras-directories'."
+  (member path denote-silo-extras-directories))
+
+(defmacro denote-silo-extras-with-silo (silo &rest args)
+  "Run ARGS with SILO bound, if SILO satisfies 
`denote-silo-extras-path-is-silo-p'."
+  (declare (indent defun))
+  `(if (denote-silo-extras-path-is-silo-p ,silo)
+       (progn
+         ,@args)
+     (user-error "`%s' is not among the `denote-silo-extras-directories'" 
,silo)))
+
 ;;;###autoload
 (defun denote-silo-extras-create-note (silo)
   "Select SILO and run `denote' in it.
 SILO is a file path from `denote-silo-extras-directories'.
 
-When called from Lisp, SILO is a file system path to a directory."
+When called from Lisp, SILO is a file system path to a directory that
+conforms with `denote-silo-extras-path-is-silo-p'."
   (interactive (list (denote-silo-extras-directory-prompt)))
-  (let ((denote-directory silo))
-    (call-interactively #'denote)))
+  (denote-silo-extras-with-silo silo
+    (let ((denote-directory silo))
+      (call-interactively #'denote))))
 
 ;;;###autoload
 (defun denote-silo-extras-open-or-create (silo)
   "Select SILO and run `denote-open-or-create' in it.
 SILO is a file path from `denote-silo-extras-directories'.
 
-When called from Lisp, SILO is a file system path to a directory."
+When called from Lisp, SILO is a file system path to a directory that
+conforms with `denote-silo-extras-path-is-silo-p'."
   (interactive (list (denote-silo-extras-directory-prompt)))
-  (let ((denote-directory silo))
-    (call-interactively #'denote-open-or-create)))
+  (denote-silo-extras-with-silo silo
+    (let ((denote-directory silo))
+      (call-interactively #'denote-open-or-create))))
 
 ;;;###autoload
 (defun denote-silo-extras-select-silo-then-command (silo command)
@@ -95,13 +111,15 @@ When called from Lisp, SILO is a file system path to a 
directory."
 SILO is a file path from `denote-silo-extras-directories', while
 COMMAND is one among `denote-silo-extras-commands'.
 
-When called from Lisp, SILO is a file system path to a directory."
+When called from Lisp, SILO is a file system path to a directory that
+conforms with `denote-silo-extras-path-is-silo-p'."
   (interactive
    (list
     (denote-silo-extras-directory-prompt)
     (denote-command-prompt)))
-  (let ((denote-directory silo))
-    (call-interactively command)))
+  (denote-silo-extras-with-silo silo
+    (let ((denote-directory silo))
+      (call-interactively command))))
 
 (provide 'denote-silo-extras)
 ;;; denote-silo-extras.el ends here

Reply via email to