aboutsummaryrefslogtreecommitdiff
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
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.
-rw-r--r--emacs/.emacs.d/init/ycp-buffer.el2
-rw-r--r--emacs/.emacs.d/init/ycp-complete.el2
-rw-r--r--emacs/.emacs.d/init/ycp-org.el2
-rw-r--r--emacs/.emacs.d/init/ycp-prog.el2
-rw-r--r--emacs/.emacs.d/lisp/my/my-buffer.el36
-rw-r--r--emacs/.emacs.d/lisp/my/my-org.el47
-rw-r--r--emacs/.emacs.d/lisp/my/my-prog.el29
-rw-r--r--emacs/.emacs.d/lisp/my/my-utils.el5
8 files changed, 96 insertions, 29 deletions
diff --git a/emacs/.emacs.d/init/ycp-buffer.el b/emacs/.emacs.d/init/ycp-buffer.el
index 6096869..3629efe 100644
--- a/emacs/.emacs.d/init/ycp-buffer.el
+++ b/emacs/.emacs.d/init/ycp-buffer.el
@@ -37,7 +37,7 @@
"M-s v" #'my-buffers-vc-root
;; F7: Cycle or create buffers with the same base buffer as the
;; current buffer
- "<f7>" #'my-create-or-switch-indirect-buffers
+ "<f10>" #'my-create-or-switch-indirect-buffers
;; F8: Cycle or create buffers with the same major mode as the
;; current buffer
"<f8>" #'my-buffer-create-or-cycle-same-mode
diff --git a/emacs/.emacs.d/init/ycp-complete.el b/emacs/.emacs.d/init/ycp-complete.el
index f76ebbb..285c6c9 100644
--- a/emacs/.emacs.d/init/ycp-complete.el
+++ b/emacs/.emacs.d/init/ycp-complete.el
@@ -241,6 +241,8 @@
(:install t)
(:delay 10)
(setq marginalia-max-relative-age 0)
+ (my-keybind minibuffer-local-map
+ "M-A" #'marginalia-cycle)
(marginalia-mode 1))
(setq tempel-path
diff --git a/emacs/.emacs.d/init/ycp-org.el b/emacs/.emacs.d/init/ycp-org.el
index 221eaf7..b20f33e 100644
--- a/emacs/.emacs.d/init/ycp-org.el
+++ b/emacs/.emacs.d/init/ycp-org.el
@@ -440,7 +440,7 @@
(my-package my-org
(:delay 10)
(my-keybind global-map
- "<f6>" #'my-org-open-default-notes-file)
+ "<f6>" #'my-org-open-or-cycle-notes)
(my-keybind org-mode-map
"C-c 1" #'my-org-insert-date-range
"C-c ns" #'my-org-substitute-gnus-link-after-archiving
diff --git a/emacs/.emacs.d/init/ycp-prog.el b/emacs/.emacs.d/init/ycp-prog.el
index 6696aaf..222e912 100644
--- a/emacs/.emacs.d/init/ycp-prog.el
+++ b/emacs/.emacs.d/init/ycp-prog.el
@@ -120,7 +120,7 @@
'(shell-mode . my-shell-with-directory))
(my-keybind comint-mode-map "<f2>" #'my-comint-restart)
(add-hook 'shell-mode-hook 'my-shell-disable-company-if-remote)
- (my-keybind global-map "<f10>" #'my-switch-or-create-shell-buffer)
+ (my-keybind global-map "<f7>" #'my-shell-open-or-cycle)
)
(my-package eshell
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 ()
diff --git a/emacs/.emacs.d/lisp/my/my-org.el b/emacs/.emacs.d/lisp/my/my-org.el
index 8a6991e..01082d8 100644
--- a/emacs/.emacs.d/lisp/my/my-org.el
+++ b/emacs/.emacs.d/lisp/my/my-org.el
@@ -156,9 +156,50 @@ Otherwise deactivate mark and move point to after the block."
(save-excursion (my-org-beginning-of-line-or-indent) (point))
(point)))))
-(defun my-org-open-default-notes-file ()
- (interactive)
- (find-file org-default-notes-file))
+(defvar my-shell-buffer-list nil
+ "List of default org notes file buffers.")
+
+(defun my-org-open-or-cycle-notes (arg)
+ "Open default notes file.
+
+Switch to the first buffer with the notes file that's not in a
+live window, or find the notes file.
+
+If repeated, cycle through indirect buffers of the default notes.
+
+With a prefix ARG, creates a new indirect buffer of the default
+notes file."
+ (interactive "P")
+ (if arg
+ (with-current-buffer (find-file-noselect org-default-notes-file)
+ (clone-indirect-buffer nil t)
+ (setq my-notes-buffer-list
+ (setq-filter 'my-buffer-with-same-base-p
+ (buffer-list))))
+ (if (eq last-command 'my-org-open-or-cycle-notes)
+ (progn
+ (setq my-notes-buffer-list
+ (my-list-cycle my-notes-buffer-list))
+ (switch-to-buffer (car my-notes-buffer-list)))
+ (let ((buffer
+ (find-file-noselect org-default-notes-file)))
+ (with-current-buffer buffer
+ (setq my-notes-buffer-list
+ (seq-filter 'my-buffer-with-same-base-p (buffer-list))))
+ ;; Find first buffer that is not in a live window. If no such
+ ;; buffer exists, fall back to `find-file-noselect'.
+ (setq buffer
+ (or
+ (seq-find
+ (lambda (buffer)
+ (not (my-buffer-live-window-p buffer)))
+ my-notes-buffer-list)
+ (find-file-noselect org-default-notes-file)))
+ (while (not (eq buffer (car my-notes-buffer-list)))
+ (setq my-notes-buffer-list
+ (my-list-cycle my-notes-buffer-list))))
+ (switch-to-buffer (car my-notes-buffer-list))
+ )))
;; links
(defun my-org-substitute-gnus-link-after-archiving ()
diff --git a/emacs/.emacs.d/lisp/my/my-prog.el b/emacs/.emacs.d/lisp/my/my-prog.el
index 798b4cd..a0e0ce7 100644
--- a/emacs/.emacs.d/lisp/my/my-prog.el
+++ b/emacs/.emacs.d/lisp/my/my-prog.el
@@ -137,20 +137,33 @@
(require 'my-buffer)
-(defun my-switch-or-create-shell-buffer (arg)
+(defvar my-shell-buffer-list nil
+ "List of shell mode buffers.")
+
+(defun my-shell-open-or-cycle (arg)
"Switch to or create a shell buffer.
If there's no buffer with shell mode, or with a prefix-arg,
create a shell buffer using `my-shell-with-directory'"
(interactive "P")
(if (or arg
- (not (seq-filter
- (lambda (buffer)
- (with-current-buffer buffer
- (derived-mode-p 'shell-mode)))
- (buffer-list))))
- (call-interactively 'my-shell-with-directory)
- (my-buffer-quick-major-mode 'shell-mode)))
+ (not (my-buffer-find-mode 'shell-mode)))
+ (progn
+ (call-interactively 'my-shell-with-directory)
+ (setq my-shell-buffer-list
+ (seq-filter 'my-buffer-with-same-major-mode-p
+ (buffer-list))))
+ (if (eq last-command 'my-shell-open-or-cycle)
+ (progn
+ (setq my-shell-buffer-list
+ (my-list-cycle my-shell-buffer-list))
+ (switch-to-buffer (car my-shell-buffer-list)))
+ (let ((buffer
+ (my-buffer-find-mode 'shell-mode)))
+ (switch-to-buffer buffer)
+ (setq my-shell-buffer-list
+ (seq-filter 'my-buffer-with-same-major-mode-p
+ (buffer-list)))))))
;;; gdb
(require 'gdb-mi)
diff --git a/emacs/.emacs.d/lisp/my/my-utils.el b/emacs/.emacs.d/lisp/my/my-utils.el
index f80fb13..db7e804 100644
--- a/emacs/.emacs.d/lisp/my/my-utils.el
+++ b/emacs/.emacs.d/lisp/my/my-utils.el
@@ -45,6 +45,11 @@
(defun my-delete-http-header ()
(delete-region (point-min) (progn (my-skip-http-header) (point))))
+;; lists
+(defun my-list-cycle (xs)
+ "Cycle a list."
+ (cdr (append xs (list (car xs)))))
+
(defun my-get-current-line-no-properties ()
(save-excursion
(let ((beg (progn (beginning-of-line)