about summary refs log tree commit diff stats
path: root/.emacs.d/lisp/init-project.el
diff options
context:
space:
mode:
authorDavid Morgan <djm_uk@protonmail.com>2021-09-17 16:38:57 +0100
committerDavid Morgan <djm_uk@protonmail.com>2021-09-17 16:38:57 +0100
commited43c29090b74e85ba35c0453cf67c4a6fa5076b (patch)
treeb52f5707c1b1edaf24c05d7a8116835fed6394b5 /.emacs.d/lisp/init-project.el
parent1447feb491454649da2994b3d18e19869ba65645 (diff)
downloaddotfiles-ed43c29090b74e85ba35c0453cf67c4a6fa5076b.tar.gz
Add functions for switching to buffers using the same major mode
Diffstat (limited to '.emacs.d/lisp/init-project.el')
-rw-r--r--.emacs.d/lisp/init-project.el24
1 files changed, 24 insertions, 0 deletions
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))