From 9d7aabd29f758306343d992489a7caafbf933b86 Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Sat, 14 Oct 2023 13:08:03 +1100 Subject: [emacs] Switch shell buffers by prompt too --- emacs/.emacs.d/lisp/my/my-prog.el | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'emacs/.emacs.d/lisp/my/my-prog.el') 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. -- cgit v1.2.3