From ed43c29090b74e85ba35c0453cf67c4a6fa5076b Mon Sep 17 00:00:00 2001 From: David Morgan Date: Fri, 17 Sep 2021 16:38:57 +0100 Subject: Add functions for switching to buffers using the same major mode --- .emacs.d/lisp/init-project.el | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to '.emacs.d') diff --git a/.emacs.d/lisp/init-project.el b/.emacs.d/lisp/init-project.el index fcc0e09..be90d8e 100644 --- a/.emacs.d/lisp/init-project.el +++ b/.emacs.d/lisp/init-project.el @@ -64,6 +64,28 @@ ;; Don't restart persp-mode when re-evaluating, as it clears the current persp list :init (or persp-mode (persp-mode)) :config + ;; Based on jao-buffer-same-mode (https://jao.io/blog/2021-09-08-high-signal-to-noise-emacs-command.html) + (defun persp-switch-buffer-same-mode () + "Switch to a buffer with the same major mode as the current buffer, respecting the current perspective." + (interactive) + (let* ((mode major-mode) + (pred (lambda (b) + (let ((b (get-buffer (if (consp b) (car b) b)))) + (eq (buffer-local-value 'major-mode b) mode))))) + (pop-to-buffer (persp-read-buffer "Buffer: " nil t pred)))) + (defun persp-previous-buffer-same-mode () + "Switch to the previous buffer in the current perspective, with the same major mode as the current buffer (or do nothing)" + (interactive) + (let* ((persp-buffers (seq-filter 'persp-is-current-buffer (buffer-list))) + (mode major-mode) + (mode-pred (lambda (b) + (let ((b (get-buffer (if (consp b) (car b) b)))) + (and (eq (buffer-local-value 'major-mode b) mode) + (not (eq b (current-buffer))) + (not (get-buffer-window b)))))) + (persp-buffers-in-mode (seq-filter mode-pred persp-buffers))) + (when (not (seq-empty-p persp-buffers-in-mode)) + (switch-to-buffer (car persp-buffers-in-mode))))) (defun switch-project (proj) "Switch to project or already open project perspective." (interactive (list (project-prompt-project-dir))) @@ -77,6 +99,8 @@ (project-switch-project proj))))) :bind ("C-x p p" . switch-project) + ("C-x C-b" . persp-previous-buffer-same-mode) + ("C-x C-S-b" . persp-switch-buffer-same-mode) ("C-x x x" . persp-switch-last) ("C-x x ." . persp-switch-quick)) -- cgit 1.4.1-2-gfad0