(feat): add optional FILTER-FN argument for org-roam-find-file and org-roam-insert (#481)

This commit is contained in:
Leo Vivier
2020-04-17 22:54:17 +02:00
committed by GitHub
parent 6175739b33
commit ba80a31fe0

View File

@ -316,9 +316,12 @@ specified via the #+ROAM_ALIAS property."
(concat "file:" (file-relative-name target here)) (concat "file:" (file-relative-name target here))
description))) description)))
(defun org-roam-insert (prefix) (defun org-roam-insert (prefix &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.
If PREFIX, downcase the title before insertion." If PREFIX, downcase the title before insertion.
FILTER-FN is the name of a function to apply on the candidates
which takes as its argument an alist of path-completions. See
`org-roam--get-title-path-completions' for details."
(interactive "P") (interactive "P")
(unless (org-roam--org-roam-file-p (unless (org-roam--org-roam-file-p
(buffer-file-name (buffer-base-buffer))) (buffer-file-name (buffer-base-buffer)))
@ -329,7 +332,10 @@ If PREFIX, downcase the title before insertion."
(region-text (when region (region-text (when region
(buffer-substring-no-properties (buffer-substring-no-properties
(car region) (cdr region)))) (car region) (cdr region))))
(completions (org-roam--get-title-path-completions)) (completions (--> (org-roam--get-title-path-completions)
(if filter-fn
(funcall filter-fn it)
it)))
(title (org-roam-completion--completing-read "File: " completions (title (org-roam-completion--completing-read "File: " completions
:initial-input region-text)) :initial-input region-text))
(region-or-title (or region-text title)) (region-or-title (or region-text title))
@ -369,11 +375,17 @@ If PREFIX, downcase the title before insertion."
file-path) res)))) file-path) res))))
res)) res))
(defun org-roam-find-file (&optional initial-prompt) (defun org-roam-find-file (&optional initial-prompt filter-fn)
"Find and open an Org-roam file. "Find and open an Org-roam file.
INITIAL-PROMPT is the initial title prompt." INITIAL-PROMPT is the initial title prompt.
FILTER-FN is the name of a function to apply on the candidates
which takes as its argument an alist of path-completions. See
`org-roam--get-title-path-completions' for details."
(interactive) (interactive)
(let* ((completions (org-roam--get-title-path-completions)) (let* ((completions (--> (org-roam--get-title-path-completions)
(if filter-fn
(funcall filter-fn it)
it)))
(title (org-roam-completion--completing-read "File: " completions (title (org-roam-completion--completing-read "File: " completions
:initial-input initial-prompt)) :initial-input initial-prompt))
(file-path (cdr (assoc title completions)))) (file-path (cdr (assoc title completions))))