summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2021-05-20 17:56:13 +0800
committerJSDurand <mmemmew@gmail.com>2021-05-20 17:56:13 +0800
commitbb3b3229afdad13662342ead7c3440766c6a3f64 (patch)
treeff158f968f7196c33e71df88cf5690458772e9ad
parent1fda186b1d2a78ef820ce1400c7e87a5fcb3255e (diff)
Add a function to switch to buffers of the same major mode
* ibuffer.el (switch-to-buffer-same-mode): This turns out to be convenient, contrary to what I thought at first.
-rw-r--r--ibuffer.el46
1 files changed, 45 insertions, 1 deletions
diff --git a/ibuffer.el b/ibuffer.el
index 49e6a29..7f172ac 100644
--- a/ibuffer.el
+++ b/ibuffer.el
@@ -30,9 +30,53 @@ If ARG is non-nil, then display the last buffer in a new window."
(interactive)
(switch-to-buffer-other-window nil))
+;;;###autoload
+(defun switch-to-buffer-same-mode (&optional arg)
+ "Switch to a buffer with the same major mode as the current
+ buffer.
+If the optional ARG is non-nil, then produce an IBUFFER buffer
+ listing all buffers of the same mode as the current buffer."
+ (interactive "P")
+ (cond
+ ((null arg)
+ (let ((def (buffer-name
+ (car
+ (delete nil
+ (mapcar
+ (lambda (buf)
+ (and
+ (provided-mode-derived-p
+ (buffer-local-value 'major-mode buf)
+ (buffer-local-value 'major-mode (current-buffer)))
+ (not (= (aref (buffer-name buf) 0) 32))
+ (not (eq buf (current-buffer)))
+ buf))
+ (buffer-list)))))))
+ (switch-to-buffer
+ (read-buffer (format
+ "Switch to buffer with major mode %S:"
+ (buffer-local-value 'major-mode (current-buffer)))
+ def t
+ (lambda (name-or-cons)
+ (provided-mode-derived-p
+ (buffer-local-value
+ 'major-mode
+ (cond ((consp name-or-cons)
+ (car name-or-cons))
+ ((get-buffer name-or-cons))))
+ (buffer-local-value
+ 'major-mode
+ (current-buffer))))))))
+ ((ibuffer
+ nil
+ (format "*Buffers for %S*"
+ (buffer-local-value 'major-mode (current-buffer)))
+ (list (cons 'used-mode
+ (buffer-local-value 'major-mode (current-buffer))))))))
+
(define-key global-map (vector 24 2) #'ibuffer)
(define-key global-map (vector ?\s-h) #'ibuffer)
-(define-key global-map (vector ?\M-\s-b) #'ibuffer)
+(define-key global-map (vector ?\M-\s-b) #'switch-to-buffer-same-mode)
(define-key global-map (vector ?\s-b) #'switch-to-buffer)
(define-key global-map (vector ?\s-B) #'switch-to-buffer-other-window)
(define-key global-map (vector ?\s-n) #'durand-switch-to-last-buffer)