aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-buffer.el
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2023-10-14 12:18:38 +1100
committerYuchen Pei <id@ypei.org>2023-10-14 12:18:38 +1100
commit0383ed224a7708315f3da83d5e809c597b0e888a (patch)
tree8e2c180e2e9b41dc7c51b0bbe210d79c840944a5 /emacs/.emacs.d/lisp/my/my-buffer.el
parentad1cdc1f55bf90f635365b728b91888edf31fa6e (diff)
[emacs] Fix notes and shell cycle and creation.
One function to switch or cycle default notes indirect buffers. One to switch or cycle shell mode buffers.
Diffstat (limited to 'emacs/.emacs.d/lisp/my/my-buffer.el')
-rw-r--r--emacs/.emacs.d/lisp/my/my-buffer.el36
1 files changed, 21 insertions, 15 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-buffer.el b/emacs/.emacs.d/lisp/my/my-buffer.el
index fa51abf..af72cfb 100644
--- a/emacs/.emacs.d/lisp/my/my-buffer.el
+++ b/emacs/.emacs.d/lisp/my/my-buffer.el
@@ -28,6 +28,7 @@
;; Much of the following is from prot-emacs
(defun my-get-major-mode-for-buffer (buffer)
+ "Returns the major mode for BUFFER."
(buffer-local-value 'major-mode (get-buffer buffer)))
;;; Copied from mastering emacs
@@ -71,16 +72,21 @@ With a prefix-arg, prompt for major mode."
(call-interactively 'my-switch-to-buffer-matching-major-mode)
(switch-to-buffer (my--buffer-major-mode-prompt))))
+(defun my-buffer-find-mode (mode)
+ "Find a buffer of a given MODE not in a live window."
+ (seq-find
+ (lambda (buffer)
+ (and
+ (with-current-buffer buffer
+ (derived-mode-p mode))
+ (not (my-buffer-live-window-p buffer))))
+ (buffer-list)))
+
(defun my-buffer-quick-major-mode (mode)
- "Switch to the first buffer of a given mode."
- (let ((buffers (buffer-list)))
- (while (and buffers
- (with-current-buffer (car buffers)
- (not (derived-mode-p mode))))
- (setq buffers (cdr buffers)))
- (if buffers
- (switch-to-buffer (car buffers))
- (message "No buffers in %S" mode))))
+ "Switch to the first buffer of a given MODE not in a live window."
+ (if-let ((buffer_ (my-buffer-find-mode mode)))
+ (switch-to-buffer buffer)
+ (message "No non-live-window buffers in %S" mode)))
(defun my-buffer-switch-or-create-major-mode (mode)
"Switch to or create a buffer with a chosen major mode.
@@ -208,7 +214,7 @@ Do not try to make a new directory or anything fancy."
(switch-to-buffer (my--buffer-vc-root-prompt)))
(defun my-bookmark-save-no-prompt (&rest _)
- "Run `bookmark-save' without prompts.
+ "Run `bookmark-save' without prompts.
The intent of this function is to be added as an :after advice to
`bookmark-set-internal'. Concretely, this means that when
@@ -216,7 +222,11 @@ The intent of this function is to be added as an :after advice to
afterwards. We set this up because there is no hook after
setting a bookmark and we want to automatically save bookmarks at
that point."
- (funcall 'bookmark-save))
+ (funcall 'bookmark-save))
+
+(defun my-buffer-live-window-p (buffer)
+ "Whether BUFFER is in a live window."
+ (window-live-p (get-buffer-window buffer)))
(defun my-cycle-windows ()
"Cycle all windows."
@@ -425,10 +435,6 @@ for the given MAJOR-MODE, any text is appended to it."
(not (equal (cdr buffer) current)))))))
(switch-to-buffer buffer)))
-(defun my-list-cycle (xs)
- "Cycle a list."
- (cdr (append xs (list (car xs)))))
-
(defvar my-indirect-buffer-list nil)
(defun my-cycle-indirect-buffer ()