help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: My read-buffer-function doesn't work when called by switch-to-buffer


From: Tassilo Horn
Subject: Re: My read-buffer-function doesn't work when called by switch-to-buffer
Date: Sat, 11 Mar 2023 09:58:11 +0100
User-agent: mu4e 1.9.22; emacs 30.0.50

Michael Heerdegen <michael_heerdegen@web.de> writes:

Hi Michael,

>> However, when I do C-x b (switch-to-buffer), no matter what, recent
>> files are not provided as completion candidates.  But edebug
>> convinces me that my function th/read-buffer-or-recentf is called.
>> It just seems that the same completing-read call behaves differently
>> when called directly and when being called by switch-to-buffer.  Why
>> is that and what can I do against it?
>
> Seems this happens because `read-buffer-to-switch' sets
> `minibuffer-completion-table'.

Ah, that's it!  This code works:

--8<---------------cut here---------------start------------->8---
(defun th/read-buffer-or-recentf (prompt &optional
                                         def require-match predicate)
  (let ((completion-table (completion-table-in-turn
                           #'internal-complete-buffer
                           (completion-table-dynamic
                            (lambda (s) recentf-list)))))
    (minibuffer-with-setup-hook
        (:append (lambda ()
                   (setq-local minibuffer-completion-table completion-table)))
      (when-let ((result (completing-read prompt completion-table
                                          predicate require-match nil
                                          'buffer-name-history def)))
        (cond
         ((get-buffer result) result)
         ((file-exists-p result) (buffer-name (find-file-noselect result)))
         (t result))))))

(setq read-buffer-function #'th/read-buffer-or-recentf)
--8<---------------cut here---------------end--------------->8---

But why is that needed?  I had expected that when I give a completion
table to completing-read, it would be used...

And a bonus question: I use vertico + marginalia, so the buffers (and
files in C-x C-f, variables/functions in C-h v/f,...) are nicely
annotated, e.g., with the buffer's mode or the file's permissions.  What
do I have to do that the recentf candidates also get those niceties?
Wrap the recentf completion table so that it responds to metadata
requests?

Bye,
Tassilo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]