summaryrefslogtreecommitdiff
path: root/basic.el
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2022-11-17 23:07:13 +0800
committerJSDurand <mmemmew@gmail.com>2022-11-17 23:07:13 +0800
commit453fa79ea92070df959e6e913ee89737663b8d95 (patch)
tree6e29981c7f8cbf4059356a77d1980f709b006669 /basic.el
parent983bf3a1725ed7eb8df6973ca1393e75d3eb68e4 (diff)
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.
Diffstat (limited to 'basic.el')
-rw-r--r--basic.el37
1 files changed, 37 insertions, 0 deletions
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