New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rough idea: add source for consult-buffer #12
Comments
Hi @apc, thank you for drafting this idea. I really like and would like to incorporate such a capability into 1. Addressing the "duplicate title issue": 2. Addressing annotation: The resulting code mainly building up on you initial effort is then as follows: ;;; consult-org-roam-buffer.el --- Consult-buffer integration for org-roam -*- lexical-binding: t; -*-
(defun consult-org-roam-buffer--state ()
(let ((preview (consult--buffer-preview)))
(lambda
(action cand)
(funcall preview action
(consult-org-roam-buffer--with-title cand))
(when
(and cand
(eq action 'return))
(consult--buffer-action
(consult-org-roam-buffer--with-title cand))))))
(defun consult-org-roam-buffer--get-title (buffer)
"Select from list of all notes that link to the current note."
(if (org-roam-buffer-p buffer)
(let* ((title
(with-current-buffer buffer
(org-roam-db--file-title)))
(filename (buffer-file-name buffer))
(fhash (org-roam-db--file-hash filename)))
(progn (concat title " [" (substring fhash 0 7) "]")))))
(defun consult-org-roam-buffer--add-title (buffer)
"Build a cons consisting of the BUFFER title and the BUFFER name"
(cons (consult-org-roam-buffer--get-title buffer) buffer))
(defun consult-org-roam-buffer--update-open-buffer-list ()
"Generate an alist of the form `(TITLE . BUF)’, where TITLE is the title of an open org-roam buffer"
(setq org-roam-buffer-open-buffer-list
(mapcar #'consult-org-roam-buffer--add-title
(org-roam-buffer-list))))
(defun consult-org-roam-buffer--with-title (title)
"Find buffer name with TITLE from among the list of open org-roam buffers"
(consult-org-roam-buffer--update-open-buffer-list)
(cdr (assoc title org-roam-buffer-open-buffer-list)))
(autoload 'org-roam-buffer-list "org-roam")
(add-to-list 'consult-buffer-sources 'org-roam-buffer-source 'append)
(defun consult-org-roam-buffer--get-roam-bufs ()
"Return list of currently open org-roam buffers"
(consult--buffer-query
:sort 'visibility
:as #'consult-org-roam-buffer--get-title
:filter t
:predicate (lambda (buf) (org-roam-buffer-p buf))))
(defvar org-roam-buffer-source
`(:name "Org-roam"
:hidden nil
:narrow ?r
:category org-roam-buffer
:annotate ,(lambda (cand)
(f-filename
(buffer-file-name
(consult-org-roam-buffer--with-title cand))))
:state ,#'consult-org-roam-buffer--state
:items ,#'consult-org-roam-buffer--get-roam-bufs))
(add-to-list 'consult-buffer-sources 'org-roam-buffer-source 'append)
;; Customize consult--source-buffer to show org-roam buffers only in
;; their dedicated section
(consult-customize
consult--source-buffer
:items (lambda ()
(consult--buffer-query
:sort 'visibility
:as #'buffer-name
:predicate (lambda (buf) (not (org-roam-buffer-p buf))))))
;;; consult-org-roam-buffer.el ends here What do you think about these changes? Do we need to change anything else here? Best regards, |
Great! I like this. Good to know there is some way of dealing with the title issue. Quick question: what's the rationale for adding the hash of the file to the annotation? Not that I mind either way, just wondering. |
Oh, and one more question (maybe @minad can chime in here): is there some clean way of making this source be listed right after I tried manually setting the value of |
Glad to hear!
The rationale is that the hash is unique and by using
(We could also use the datetime-prefix here, but since a hash is recorded in the DB I chose that.)
I would propose to do it like so: ;; Find index to put
(setq idx
(cl-position 'consult--source-buffer consult-buffer-sources :test 'equal))
;; Store the elements coming after the insertion point
(setq tail (nthcdr idx consult-buffer-sources))
;; Insert element and set its cdr
(setcdr
(nthcdr (1- idx) consult-buffer-sources)
(append (list 'org-roam-buffer-source) tail)) Better solutions welcome. So, do you think this new capability should be pushed as Best regards, |
Of course! I was somehow expecting the titles to be 'uniquified' like buffers sometimes are, but this is probably a better option. Now: would it be possible to make the hash be less prominent? In a way, it's not very helpful information to the user, but only something being used under the hood to go back and forth between titles and buffer names. Does that seem right to you?
That looks good enough to me!
That'd be great. Happy to write the PR if you want, or just let you do it yourself. Best, A.
|
Another thing: can you try deleting an org-roam file (using dired) that was open and see what happens if you call (file-missing "Opening input file" "No such file or directory" "/Users/apc/org/notebook/varia/20221006133747.org") On my end, it looks like the problem is that Not sure what's best here: moving the call to Thoughts? |
Good catch! You are right (defun consult-org-roam-buffer--get-title (buffer)
"Select from list of all notes that link to the current note."
(if (org-roam-buffer-p buffer)
(let* ((title
(with-current-buffer buffer
(org-roam-db--file-title)))
(filename (buffer-file-name buffer))
(fhash (consult-org-roam-db--file-hash filename)))
(if fhash
(concat title " [" (substring fhash 0 7) "]")
(concat title " [not persisted]")))))
(defun consult-org-roam-db--file-hash (fname)
"docstring"
(let* ((fhash (org-roam-db-query
[:select [hash]
:from files
:where (like file $s1)
]
(concat "%" fname))))
(car (car fhash)))) I added this functionality as PR #13. Please a have look and let me know what you think. Thank you very much for working on this. Best regards, |
This is related to this issue over at the consult repo.
Feature request
I'm looking for a way of getting
consult-buffer
to treat org-roam buffers in a way that's more useful (to me at least). Basically, I'd want org-roam buffers to be listed among the candidates generated byconsult-buffers
using the title rather than the buffer name. The main reason is that I name all my org-roam files using just a timestamp, and that isn't especially informative when trying to find a particular buffer.A start
As a starting point, I came up with something that sort of works. The main idea is simple and I think on the right track, but the implementation is not quite there yet.
The main idea
The proposal is to define a new source to add to
consult-buffer-sources
which will consist of those buffers listed in(org-roam-buffer-list)
. This source will list items by passing toconsult--buffer-query
' a conversion function that will extract the title of the buffer. The key is to make sure that the state function knows to find the corresponding buffer given a title.A few things are needed:
consult--buffer-preview
andconsult--buffer-action
but which will work on titles of org-roam buffers rather than buffer names.Where I am at
Thanks to @minad, I have what is at least a good enough solution to 2.
As for 3, I haven't figured out how to do it in a consult only way. But using marginalia it's fairly straightforward.
The sticking point is 1. I have a solution that sort of works but it's probably not good enough. For one thing, it presupposes no too org-roam buffers have the same title, which is not a reasonable presupposition to make.
Annotated proposal
Here at last is what I got. I'm sure it can be improved upon in many ways, so take this just as a starting point.
First, we need a translation mechanism. The buffer-to-title translation is easy. The title-to-buffer translation needs some work.
I could not find a sort of 'inverse' of
org-roam-db--tile-title
, but if there is one that may be a better bet than what I did to defineorg-roam-buffer-with-title
.We then need to define the analogue of
consult--buffer-state
(I think @minad had something even more efficient in mind, but I couldn't get his suggestion to work somehow):We can now define a new source for
consult-buffer
. Note that I'm using a custom category, which will be useful later when adding an annotator function for marginalia:Finally, an annotator to use with marginalia:
If we put all of this together, we will still see the org-roam buffers included in
consult--source-buffer
:In my case, I can filter them out using
consult-buffer-filter
, since all and only my org-roam files match a specific regexp. A more sophisticated approach would perhaps involve modifying the value of the:items
key inconsult--source-buffer
so as to exclude org-roam-buffers. I haven't tested this, but presumably:None of this will work without enabling lexical binding. I learned it the hard way.
The text was updated successfully, but these errors were encountered: