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/my/my-prog.el | |
parent | 0383ed224a7708315f3da83d5e809c597b0e888a (diff) |
[emacs] Switch shell buffers by prompt too
Diffstat (limited to 'emacs/.emacs.d/lisp/my/my-prog.el')
-rw-r--r-- | emacs/.emacs.d/lisp/my/my-prog.el | 41 |
1 files changed, 41 insertions, 0 deletions
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. |