diff options
author | Yuchen Pei <id@ypei.org> | 2023-10-14 13:08:03 +1100 |
---|---|---|
committer | Yuchen Pei <id@ypei.org> | 2023-10-14 13:08:03 +1100 |
commit | 9d7aabd29f758306343d992489a7caafbf933b86 (patch) | |
tree | ab70136a8679796a4857857102c84533549d6d96 /emacs/.emacs.d/lisp | |
parent | 0383ed224a7708315f3da83d5e809c597b0e888a (diff) |
[emacs] Switch shell buffers by prompt too
Diffstat (limited to 'emacs/.emacs.d/lisp')
-rw-r--r-- | emacs/.emacs.d/lisp/my/my-buffer.el | 34 | ||||
-rw-r--r-- | emacs/.emacs.d/lisp/my/my-prog.el | 41 |
2 files changed, 56 insertions, 19 deletions
diff --git a/emacs/.emacs.d/lisp/my/my-buffer.el b/emacs/.emacs.d/lisp/my/my-buffer.el index af72cfb..2027b5d 100644 --- a/emacs/.emacs.d/lisp/my/my-buffer.el +++ b/emacs/.emacs.d/lisp/my/my-buffer.el @@ -26,7 +26,7 @@ ;;; Code: -;; Much of the following is from prot-emacs +;; Some of the following was 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))) @@ -44,23 +44,19 @@ (defun my-switch-to-buffer-matching-major-mode (mode) (interactive - (list (intern (completing-read "Major mode: " - (mapcar 'prin1-to-string (my-get-buffer-modes)))))) - (switch-to-buffer - (read-buffer "Switch to buffer: " nil t - (lambda (pair) - (with-current-buffer (cdr pair) - (derived-mode-p mode)))))) - -(defun my--buffer-major-mode-prompt () - "Prompt of `my-buffers-major-mode'." - (let ((major major-mode) - (read-buffer-function nil)) - (read-buffer - (format "Buffer for %s: " major) - nil t - (lambda (pair) ; pair is (name-string . buffer-object) - (with-current-buffer (cdr pair) (derived-mode-p major)))))) + (list (intern + (completing-read + "Major mode: " + (mapcar 'prin1-to-string (my-get-buffer-modes)))))) + (switch-to-buffer (my-read-buffer-by-mode))) + +(defun my-read-buffer-by-mode (mode) + (if (eq mode 'shell-mode) + (my-read-shell-buffer) + (read-buffer "Switch to buffer: " nil t + (lambda (pair) + (with-current-buffer (cdr pair) + (derived-mode-p mode)))))) ;;;###autoload (defun my-buffers-major-mode (&optional arg) @@ -70,7 +66,7 @@ With a prefix-arg, prompt for major mode." (interactive "P") (if arg (call-interactively 'my-switch-to-buffer-matching-major-mode) - (switch-to-buffer (my--buffer-major-mode-prompt)))) + (switch-to-buffer (my-read-buffer-by-mode)))) (defun my-buffer-find-mode (mode) "Find a buffer of a given MODE not in a live window." diff --git a/emacs/.emacs.d/lisp/my/my-prog.el b/emacs/.emacs.d/lisp/my/my-prog.el index a0e0ce7..bee132f 100644 --- a/emacs/.emacs.d/lisp/my/my-prog.el +++ b/emacs/.emacs.d/lisp/my/my-prog.el @@ -140,6 +140,47 @@ (defvar my-shell-buffer-list nil "List of shell mode buffers.") +(defun my-shell-dir-filter (pairs) + "Filter function for `my-read-shell-buffer'." + (lambda (str pred flag) + (pcase flag + ('metadata + (list 'metadata + (cons 'annotation-function + (lambda (c) + (alist-get c pairs nil nil #'equal))))) + (t + (if (string-blank-p str) + (all-completions str pairs) + (all-completions + str + (lambda (&rest _) + (seq-map + #'car + (seq-filter + (lambda (x) + (unless (string-blank-p str) + (or + (string-match-p (regexp-quote str) (car x)) + (string-match-p (regexp-quote str) (cdr x))))) + pairs))))))))) + +(defun my-read-shell-buffer () + "Switch to a shell buffer by default directory." + (interactive) + (let ((coll + (seq-map + (lambda (buffer) + (with-current-buffer buffer + (cons (buffer-name buffer) default-directory))) + (seq-filter + (lambda (buffer) + (with-current-buffer buffer + (derived-mode-p 'shell-mode))) + (buffer-list))))) + (completing-read "Shell prompt: " + (my-shell-dir-filter coll)))) + (defun my-shell-open-or-cycle (arg) "Switch to or create a shell buffer. |