From 453fa79ea92070df959e6e913ee89737663b8d95 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Thu, 17 Nov 2022 23:07:13 +0800 Subject: basic: stop creating a scratch buffer when not demanded * basic.el (durand-read-buffer-to-switch, #'read-buffer-to-switch): The default behaviour of read-buffer-to-switch creates a scratch buffer, if there is no other good buffer to switch to. But this is not what I demand, and is oft a surprise to me. If there is no other buffer to switch to, just present me with the current buffer. The modified version does just this. --- basic.el | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/basic.el b/basic.el index b2ab6d0..aa087f1 100644 --- a/basic.el +++ b/basic.el @@ -527,6 +527,43 @@ Don't ask me to confirm my choice. --- Durand" (define-key global-map (vector ?\s-Z) #'undo-only) +;;; Don't create a scratch buffer + +(defun durand-read-buffer-to-switch (prompt) + "Read the name of a buffer to switch to, prompting with PROMPT. +Return the name of the buffer as a string. + +This function is intended for the `switch-to-buffer' family of +commands since these need to omit the name of the current buffer +from the list of completions and default values. + +Modified to stop creating a buffer, when there is only one buffer +left. --- 2022-11-17 22:56:20.746698" + (let ((rbts-completion-table (internal-complete-buffer-except)) + (default-buffer (other-buffer (current-buffer)))) + (cond + ((string= (buffer-name default-buffer) "*scratch*") + (kill-buffer default-buffer) + (setq default-buffer (current-buffer)))) + (minibuffer-with-setup-hook + (lambda () + (setq minibuffer-completion-table rbts-completion-table) + ;; Since rbts-completion-table is built dynamically, we + ;; can't just add it to the default value of + ;; icomplete-with-completion-tables, so we add it + ;; here manually. + (if (and (boundp 'icomplete-with-completion-tables) + (listp icomplete-with-completion-tables)) + (setq-local icomplete-with-completion-tables + (cons rbts-completion-table + icomplete-with-completion-tables)))) + (read-buffer + prompt default-buffer + (confirm-nonexistent-file-or-buffer))))) + +(advice-add #'read-buffer-to-switch :override + #'durand-read-buffer-to-switch) + ;;; Save close ;;;###autoload -- cgit v1.2.3-18-g5258