1 ;;; multishell-list.el --- tabulated-list-mode for multishell shell buffers
3 ;; Copyright (C) 2016 Free Software Foundation, Inc. and Ken Manheimer
5 ;; Author: Ken Manheimer <ken.manheimer@gmail.com>
7 ;; Created: 2016 -- first public availability
9 ;; URL: https://github.com/kenmanheimer/EmacsMultishell
11 (require 'tabulated-list)
13 (defun multishell-list-open-pop ()
14 "Pop to current entry's shell, and refresh the listing buffer."
16 (multishell-pop-to-shell nil (tabulated-list-get-id)))
17 (defun multishell-list-open-here ()
18 "Switch to current entry's shell buffer."
20 (multishell-pop-to-shell nil (tabulated-list-get-id) 'here))
22 (defun multishell-list-delete ()
23 "Remove current environment variable value."
25 (let* ((where (save-excursion (beginning-of-line) (point)))
26 (entry (tabulated-list-get-id))
27 (name (multishell-name-from-entry entry))
28 (name-bracketed (multishell-bracket name))
29 (buffer (get-buffer name-bracketed)))
30 (when (multishell-delete-history-name name)
32 ;; If the process is live, let shell-mode get confirmation:
33 (or (comint-check-proc (current-buffer))
34 (y-or-n-p (format "Kill buffer %s? " name-bracketed)))
35 (kill-buffer name-bracketed)))
37 (if (not tabulated-list-sort-key)
41 (defun multishell-list-edit-entry ()
42 "Edit the value of current shell entry."
44 (let* ((where (save-excursion (beginning-of-line) (point)))
45 (entry (tabulated-list-get-id))
46 (name (multishell-name-from-entry entry))
47 (revised (multishell-read-unbracketed-entry
48 (format "Edit shell spec for %s: " name)
51 (revised-pair (when revised (multishell-split-entry revised))))
53 (multishell-delete-history-name name)
54 (multishell-register-name-to-path (car revised-pair) (cadr revised-pair))
56 (if (not tabulated-list-sort-key)
60 (defun multishell-list-placeholder (value default)
61 "Return VALUE if non-empty string, else DEFAULT."
62 (if (or (not value) (string= value ""))
65 (defun multishell-list-entries ()
66 "Generate multishell name/path entries list for tabulated-list."
68 (mapcar #'(lambda (entry)
69 (setq recency (1+ recency))
70 (let* ((splat (multishell-split-entry entry))
74 (multishell-bracket name))))
75 (status (cond ((not buffer) "x")
76 ((comint-check-proc buffer) "+")
79 (dissected (and rest (file-remote-p rest)
80 (tramp-dissect-file-name rest t)))
81 (path (or (and dissected (aref dissected 3))
85 rest 0 (- (length rest) (length path))))))
87 (setq name (multishell-name-from-entry entry)))
89 (vector (format "%d" recency)
92 (multishell-list-placeholder hops "-")
93 (multishell-list-placeholder path ":")))))
94 (multishell-all-entries))))
96 (defun compare-strings-as-numbers (a b)
97 (let ((a (aref (cadr a) 0))
98 (b (aref (cadr b) 0)))
99 (> (string-to-number a) (string-to-number b))))
100 (define-derived-mode multishell-list-mode
101 tabulated-list-mode "Shells"
102 "Major mode for listing current and historically registered shells..
103 \\{multishell-list-mode-map\}"
104 (setq tabulated-list-format [("#" 0 compare-strings-as-numbers)
109 tabulated-list-sort-key nil
110 tabulated-list-entries #'multishell-list-entries)
111 (tabulated-list-init-header))
113 (define-key multishell-list-mode-map (kbd "d") 'multishell-list-delete)
114 (define-key multishell-list-mode-map (kbd "e") 'multishell-list-edit-entry)
115 (define-key multishell-list-mode-map (kbd "o") 'multishell-list-open-pop)
116 (define-key multishell-list-mode-map
117 (kbd "<return>") 'multishell-list-open-here)
120 (defun multishell-list ()
121 "Edit your current and historic list of shell buffers.
123 Hit ? for a list of commands.
125 You can get to this shell listing manager by
126 recursively invoking \\[multishell-pop-to-shell] at either of the
127 `multishell-pop-to-shell' universal argument prompts."
129 (let ((buffer (get-buffer-create "*Shells*")))
130 (pop-to-buffer buffer)
131 (multishell-list-mode)
132 (tabulated-list-print)))
134 (provide 'multishell-list)
135 (require 'multishell)
137 ;;; multishell-list.el ends here