[Top][All Lists]

[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: Thu, 16 Mar 2023 10:18:15 +0100
User-agent: mu4e 1.9.22; emacs 30.0.50

Michael Heerdegen <> writes:

>> Hm, but then it would be pretty specific to `switch-to-buffer'
>> instead of a general `read-buffer-function'.  E.g., I just found out
>> it's quite convienient to be able to use `append-to-buffer' to append
>> to a buffer whose file I haven't found already but is in
>> recentf-list.
> Depends on what you want of course.  It doesn't make sense for
> everything (not for `kill-buffer' for example).

Good point (although it doesn't really hurt).  I now made my function a
bit customizable depending on `this-command'.

--8<---------------cut here---------------start------------->8---
(defconst th/read-buffer-or-recentf-command-alist
  "Alist with entries of the form (CMD . COMPLETES).
COMPLETES is a list defining what's completed where entries can

- `buffers':        completion for all buffers
- `buffers-except': completion for all buffers except the current one
- `recentf':        completion for recent files which will be found on demand"
  '((kill-buffer buffers)
    (switch-to-buffer buffers-except recentf)
    (pop-to-buffer buffers-except recentf)))

(defun th/read-buffer-or-recentf (prompt &optional
                                         def require-match predicate)
  (let* ((tables (or
                   (lambda (syms)
                     (pcase syms
                       ('buffers #'internal-complete-buffer)
                       ('buffers-except (internal-complete-buffer-except
                       ('recentf (completion-table-dynamic
                                  (lambda (s) recentf-list)))
                       (unknown  (error "Unknown case %S" unknown))))
                   (cdr (assoc this-command
                  (list #'internal-complete-buffer
                         (lambda (s) recentf-list)))))
         (completion-table (apply #'completion-table-in-turn tables)))
    ;; Bind minibuffer-setup-hook to nil so that we can be sure our completion
    ;; table is used.  `read-buffer-to-switch' (called by `switch-to-buffer')
    ;; sets `internal-complete-buffer' using `minibuffer-with-setup-hook'
    ;; before `read-buffer-function' is invoked by `read-buffer', so we'd be
    ;; restricted to buffers.
    (let ((minibuffer-setup-hook nil))
      (when-let ((result (completing-read prompt completion-table
                                          predicate require-match nil
                                          'buffer-name-history def)))
         ((get-buffer result) result)
         ((file-exists-p result) (buffer-name (find-file-noselect result)))
         (t result))))))

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


reply via email to

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