unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* My read-buffer-function doesn't work when called by switch-to-buffer
@ 2023-03-10 21:10 Tassilo Horn
  2023-03-11  0:32 ` Michael Heerdegen
  0 siblings, 1 reply; 13+ messages in thread
From: Tassilo Horn @ 2023-03-10 21:10 UTC (permalink / raw)
  To: Users of the GNU Emacs text editor

Hi all,

I would like that switch-to-buffer also suggests recent files.  Given
that it uses read-buffer which can be customized using a
read-buffer-function, I thought that should be easy.  That's what I have
come up with:

--8<---------------cut here---------------start------------->8---
(defun th/read-buffer-or-recentf (prompt &optional
                                         def require-match predicate)
  (when-let ((result (completing-read
                      prompt
                      (completion-table-in-turn #'internal-complete-buffer
                                                (completion-table-dynamic
                                                 (lambda (s) recentf-list)))
                      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---

The function th/read-buffer-or-recentf works fine when called
standalone, i.e., when I evaluate

  (th/read-buffer-or-recentf "test: ")

and type something which won't complete to an existing buffer, recent
files pop up as completion candidates.  Yay!

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?

Thanks,
Tassilo



^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2023-03-17  7:04 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-10 21:10 My read-buffer-function doesn't work when called by switch-to-buffer Tassilo Horn
2023-03-11  0:32 ` Michael Heerdegen
2023-03-11  8:58   ` Tassilo Horn
2023-03-11 20:40     ` `when-let' (was: Re: My read-buffer-function doesn't work when called by switch-to-buffer) Emanuel Berg
2023-03-12  3:32     ` My read-buffer-function doesn't work when called by switch-to-buffer Michael Heerdegen
2023-03-12 18:51       ` Tassilo Horn
2023-03-13  0:32         ` Michael Heerdegen
2023-03-15  9:06           ` Tassilo Horn
2023-03-15 18:57             ` Michael Heerdegen
2023-03-16  9:18               ` Tassilo Horn
2023-03-16 16:59                 ` Tassilo Horn
2023-03-16 23:46                 ` Michael Heerdegen
2023-03-17  7:04                   ` Tassilo Horn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).