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 ;; See multishell.el for commentary, change log, etc.
13 (require 'tabulated-list)
15 (defgroup multishell-list nil
16 "Show a menu of all shell buffers in a buffer."
19 (defface multishell-list-name
21 "Face for shell names in the Multishell List."
22 :group 'multishell-list)
24 (defun multishell-list-open-pop (&optional arg)
25 "Pop to current entry's shell in separate window.
27 The shell is started if it's not already going, unless this is
28 invoked with optional `universal-argument'. In that case we
29 pop to the buffer but don't change its run state."
31 (let ((list-buffer (current-buffer))
32 (entry (tabulated-list-get-id)))
35 (multishell-bracket (multishell-name-from-entry entry)))
36 (multishell-list-selected entry t))
37 (with-current-buffer list-buffer
39 (multishell-list-goto-item-by-entry entry))))
41 (defun multishell-list-open-as-default ()
42 "Pop to current entry's shell, and set as the default shell."
44 (let ((list-buffer (current-buffer))
45 (entry (tabulated-list-get-id)))
46 (message "%s <==" (multishell-name-from-entry entry))
47 (multishell-list-selected entry t t)
48 (with-current-buffer list-buffer
50 (multishell-list-goto-item-by-entry entry))))
52 (defun multishell-list-open-here (&optional arg)
53 "Switch to current entry's shell buffer.
55 The shell is started if it's not already going, unless this is
56 invoked with optional `universal-argument'. In that case we
57 switch to the buffer but don't activate (or deactivate) it it."
59 (let* ((list-buffer (current-buffer))
60 (entry (tabulated-list-get-id)))
63 (multishell-bracket (multishell-name-from-entry entry)))
64 (multishell-list-selected entry nil))
65 (with-current-buffer list-buffer
68 (defun multishell-list-delete (&optional arg)
69 "Remove current shell entry, and prompt for buffer-removal if present."
71 (let* ((entry (tabulated-list-get-id))
72 (name (multishell-name-from-entry entry))
73 (name-bracketed (multishell-bracket name))
74 (buffer (get-buffer name-bracketed)))
75 (when (multishell-delete-history-name name)
77 ;; If the process is live, let shell-mode get confirmation:
78 (or (comint-check-proc (current-buffer))
79 (y-or-n-p (format "Kill buffer %s? " name-bracketed)))
80 (kill-buffer name-bracketed)))
81 (tabulated-list-delete-entry)))
83 (defun multishell-list-edit-entry (&optional arg)
84 "Edit the value of current shell entry.
86 Submitting the change will not launch the entry, unless this is
87 invoked with optional `universal-argument'. In the latter case,
88 submitting the entry will pop to the shell in a new window,
89 starting it if it's not already going."
92 (let* ((list-buffer (current-buffer))
93 (entry (tabulated-list-get-id))
94 (name (multishell-name-from-entry entry))
95 (revised (multishell-read-unbracketed-entry
96 (format "Edit shell spec for %s: " name)
99 (revised-name (multishell-name-from-entry revised))
101 (when (not (string= revised entry))
102 (multishell-replace-entry entry revised)
103 (when (and (not (string= name revised-name))
104 (setq buffer (get-buffer (multishell-bracket name))))
105 (with-current-buffer buffer
106 (rename-buffer (multishell-bracket revised-name)))))
108 (multishell-list-selected revised-name t))
109 (with-current-buffer list-buffer
111 (multishell-list-goto-item-by-entry revised))))
113 (defun multishell-list-clone-entry (&optional arg)
114 "Create a new list entry, edited from the current one, ready to launch.
116 If you provide an optional `universal-argument', the new entry
117 will be launched when it's created.
119 The already existing original entry is left untouched."
121 (let* ((prototype (tabulated-list-get-id))
122 (name (multishell-name-from-entry prototype))
123 (new (multishell-read-unbracketed-entry
124 (format "Clone new shell spec from %s: " name)
127 (new-name (multishell-name-from-entry new))
128 (new-path (cadr (multishell-split-entry new))))
129 (when (not (string= new prototype))
130 (multishell-register-name-to-path new-name new-path)
132 (multishell-list-goto-item-by-entry new)
134 (multishell-list-selected new-name t)))))
136 (defun multishell-list-mouse-select (event)
137 "Select the shell whose line is clicked."
139 (select-window (posn-window (event-end event)))
140 (let ((entry (tabulated-list-get-id (posn-point (event-end event)))))
141 (multishell-list-selected entry nil)))
143 (defun multishell-list-selected (entry pop &optional set-primary)
144 "Select multishell ENTRY, popping to window if POP is non-nil.
146 Optional arg SET-PRIMARY non-nil sets `multishell-primary-name' to entry.
148 Provide for concluding minibuffer interaction if we're in completing mode."
149 (if multishell-completing
150 (throw 'multishell-minibuffer-exit entry)
151 (multishell-pop-to-shell (and set-primary '(16)) entry (not pop))))
153 (defun multishell-list-placeholder (value default)
154 "Return VALUE if non-empty string, else DEFAULT."
155 (if (or (not value) (string= value ""))
158 (defconst multishell-list-active-flag "+")
159 (defconst multishell-list-inactive-flag ".")
160 (defconst multishell-list-absent-flag "x")
162 (defun multishell-list-entries ()
163 "Generate multishell name/path-spec entries list for tabulated-list."
165 (mapcar #'(lambda (entry)
166 (setq recency (1+ recency))
167 (let* ((splat (multishell-split-entry entry))
171 (multishell-bracket name))))
172 (status (cond ((not buffer)
173 multishell-list-absent-flag)
174 ((comint-check-proc buffer)
175 multishell-list-active-flag)
176 (t multishell-list-inactive-flag)))
178 (dir (and rest (or (file-remote-p rest 'localname)
181 (file-remote-p rest 'localname)
183 rest 0 (- (length rest) (length dir))))))
185 (setq name (multishell-name-from-entry entry)))
187 (vector (format "%d" recency)
189 (multishell-list--decorate-name name)
190 (multishell-list-placeholder hops "-")
191 (multishell-list-placeholder dir "~")))))
192 (multishell-all-entries))))
194 (defun multishell-list-goto-item-by-entry (entry)
195 "Position at beginning of line of tabulated list item for ENTRY."
196 (goto-char (point-min))
197 (while (and (not (eobp))
198 (not (string= (tabulated-list-get-id) entry)))
201 (defun multishell-collate-row-strings-as-numbers (a b)
202 (let ((a (aref (cadr a) 0))
203 (b (aref (cadr b) 0)))
204 (> (string-to-number a) (string-to-number b))))
206 (defun multishell-list--decorate-name (name)
208 'font-lock-face 'multishell-list-name
209 'mouse-face 'highlight))
211 (defvar multishell-list-mode-map
212 (let ((map (make-sparse-keymap)))
213 (set-keymap-parent map tabulated-list-mode-map)
214 (define-key map (kbd "c") 'multishell-list-clone-entry)
215 (define-key map (kbd "d") 'multishell-list-delete)
216 (define-key map (kbd "\C-k") 'multishell-list-delete)
217 (define-key map (kbd "k") 'multishell-list-delete)
218 (define-key map (kbd "e") 'multishell-list-edit-entry)
219 (define-key map (kbd "o") 'multishell-list-open-pop)
220 (define-key map (kbd " ") 'multishell-list-open-pop)
221 (define-key map (kbd "O") 'multishell-list-open-as-default)
222 (define-key map (kbd "RET") 'multishell-list-open-here)
223 (define-key map [mouse-2] 'multishell-list-mouse-select)
224 (define-key map [follow-link] 'mouse-face)
226 (define-derived-mode multishell-list-mode
227 tabulated-list-mode "Shells"
228 "Major mode for listing current and historically registered shells.
230 Initial sort is from most to least recently used:
232 - First active shells, flagged with '+' a plus sign
233 - Then, inactive shells, flagged with '.' a period
234 - Then historical shells that currently have no buffer, flagged with 'x' an ex
236 \\{multishell-list-mode-map\}"
237 (setq tabulated-list-format
238 [;; (name width sort '(:right-align nil :pad-right nil))
239 ("#" 0 multishell-collate-row-strings-as-numbers :pad-right 1)
240 ("! " 1 t :pad-right 1)
244 tabulated-list-sort-key '("#" . t)
245 tabulated-list-entries #'multishell-list-entries)
246 (tabulated-list-init-header))
249 (defun multishell-list (&optional completing)
250 "Edit your current and historic list of shell buffers.
252 If optional COMPLETING is nil, we present the full
253 `multishell-history' list in a popped buffer named '*Shells*'.
255 In the buffer, hit ? or h for a list of commands.
257 When optional COMPLETING is non-nil, it must be a list of
258 multishell-history completion candidate entries, as provided by
259 `completing-read'. Then we present the list as a part of
260 minibuffer completion.
262 You can get to the shells listing by recursively invoking
263 \\[multishell-pop-to-shell] at the `multishell-pop-to-shell'
264 `universal-argument' prompts."
266 (let ((from-entry (car (multishell-history-entries
267 (multishell-unbracket (buffer-name
268 (current-buffer))))))
269 (buffer (get-buffer-create (if completing
274 (pop-to-buffer buffer))
275 (multishell-list-mode)
277 ;; Temporarily assign multishell-history only when completing:
278 (when completing '(multishell-history))
279 (when completing (list (mapcar 'substring-no-properties completing)))
280 (tabulated-list-print))
282 ;; XXX Do proper completion prep.
283 ;; - looks like stuff in minibuffer.el.gz is too hairy w/too little gain.
284 ;; - Add a buffer-change hook that throws a 'multishell-completion-done,
285 ;; and revise the catch in multishell-pop-to-shell
288 (multishell-list-goto-item-by-entry from-entry))))
290 (provide 'multishell-list)
291 (require 'multishell)
293 ;;; multishell-list.el ends here