mirror of
https://github.com/org-roam/org-roam
synced 2025-08-03 12:27:23 -05:00
(feat): simplify format of INFO
for find-ref and add FILTER
(#567)
* (feat): simplify format of `INFO` for find-ref The format of the `INFO` argument of `org-roam-find` has been simplified, thanks to the split of `type` and `ref` in the db permitted by #547. The non-interactive behaviour of `org-roam-find-ref` has also been isolated into `org-roam--find-ref`. Even if it makes us repeat ourselves a bit, it avoids having to nil the interactive argument just to get the non-interactive us, which I think is better syntax. * (feat): implement get-ref-path-completions filtering
This commit is contained in:
72
org-roam.el
72
org-roam.el
@ -598,34 +598,64 @@ See `org-roam--get-ref-path-completions' for details."
|
|||||||
:type 'boolean
|
:type 'boolean
|
||||||
:group 'org-roam)
|
:group 'org-roam)
|
||||||
|
|
||||||
(defun org-roam--get-ref-path-completions (&optional interactive)
|
(defun org-roam--get-ref-path-completions (&optional interactive filter)
|
||||||
"Return a list of cons pairs for refs to absolute path of Org-roam files.
|
"Return a list of cons pairs for refs to absolute path of Org-roam files.
|
||||||
When INTERACTIVE `org-roam-include-type-in-ref-path-completions'
|
When `org-roam-include-type-in-ref-path-completions' and
|
||||||
are non-nil, format the car of the completion-candidates as
|
INTERACTIVE are non-nil, format the car of the
|
||||||
'type:ref'."
|
completion-candidates as 'type:ref'.
|
||||||
|
FILTER can either be a string or a function:
|
||||||
|
- If it is a string, it should be the type of refs to include as
|
||||||
|
candidates (e.g. \"cite\" ,\"website\" ,etc.)
|
||||||
|
- If it is a function, it should be the name of a function that
|
||||||
|
takes three arguments: the type, the ref, and the file of the
|
||||||
|
current candidate. It should return t if that candidate is to be
|
||||||
|
included as a candidate."
|
||||||
(let ((rows (org-roam-db-query [:select [type ref file] :from refs]))
|
(let ((rows (org-roam-db-query [:select [type ref file] :from refs]))
|
||||||
(include-type (and interactive
|
(include-type (and interactive
|
||||||
org-roam-include-type-in-ref-path-completions)))
|
org-roam-include-type-in-ref-path-completions))
|
||||||
(mapcar (lambda (row)
|
candidates)
|
||||||
(cl-destructuring-bind (type ref file) row
|
(dolist (row rows (nreverse candidates))
|
||||||
(cons (if include-type
|
(cl-destructuring-bind (type ref file) row
|
||||||
(format "%s:%s" type ref)
|
(when (pcase filter
|
||||||
ref)
|
('nil t)
|
||||||
file)))
|
((pred stringp) (string= type filter))
|
||||||
rows)))
|
((pred functionp) (funcall filter type ref file))
|
||||||
|
(wrong-type (signal 'wrong-type-argument
|
||||||
|
`((stringp functionp)
|
||||||
|
,wrong-type))))
|
||||||
|
(let ((candidate (cons (if include-type
|
||||||
|
(format "%s:%s" type ref)
|
||||||
|
ref)
|
||||||
|
file)))
|
||||||
|
(push candidate candidates)))))))
|
||||||
|
|
||||||
(defun org-roam-find-ref (arg &optional info)
|
(defun org-roam--find-ref (ref)
|
||||||
|
"Find and open and Org-roam file from REF if it exists.
|
||||||
|
REF should be the value of '#+ROAM_KEY:' without any
|
||||||
|
type-information (e.g. 'cite:').
|
||||||
|
Return nil if the file does not exist."
|
||||||
|
(when-let* ((completions (org-roam--get-ref-path-completions))
|
||||||
|
(file (cdr (assoc ref completions))))
|
||||||
|
(find-file file)))
|
||||||
|
|
||||||
|
(defun org-roam-find-ref (arg &optional filter)
|
||||||
"Find and open an Org-roam file from a ref.
|
"Find and open an Org-roam file from a ref.
|
||||||
INFO is an alist containing additional information.
|
|
||||||
ARG is used to forward interactive calls to
|
ARG is used to forward interactive calls to
|
||||||
`org-roam--get-ref-path-completions'"
|
`org-roam--get-ref-path-completions'
|
||||||
|
FILTER can either be a string or a function:
|
||||||
|
- If it is a string, it should be the type of refs to include as
|
||||||
|
candidates (e.g. \"cite\" ,\"website\" ,etc.)
|
||||||
|
- If it is a function, it should be the name of a function that
|
||||||
|
takes three arguments: the type, the ref, and the file of the
|
||||||
|
current candidate. It should return t if that candidate is to be
|
||||||
|
included as a candidate."
|
||||||
(interactive "p")
|
(interactive "p")
|
||||||
(let* ((completions (org-roam--get-ref-path-completions arg))
|
(let* ((completions (org-roam--get-ref-path-completions arg filter))
|
||||||
(ref (or (cdr (assoc 'ref info))
|
(ref (org-roam-completion--completing-read "Ref: "
|
||||||
(org-roam-completion--completing-read "Ref: "
|
completions
|
||||||
completions
|
:require-match t))
|
||||||
:require-match t))))
|
(file (cdr (assoc ref completions))))
|
||||||
(find-file (cdr (assoc ref completions)))))
|
(find-file file)))
|
||||||
|
|
||||||
(defun org-roam--get-roam-buffers ()
|
(defun org-roam--get-roam-buffers ()
|
||||||
"Return a list of buffers that are Org-roam files."
|
"Return a list of buffers that are Org-roam files."
|
||||||
|
Reference in New Issue
Block a user