From bb3b3229afdad13662342ead7c3440766c6a3f64 Mon Sep 17 00:00:00 2001 From: JSDurand Date: Thu, 20 May 2021 17:56:13 +0800 Subject: 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. --- ibuffer.el | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) 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) -- cgit v1.2.3-18-g5258