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-dispatch-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-dispatch-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-dispatch-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-dispatch-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-dispatch-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-dispatch-selected entry nil)))
143 (defun multishell-list-dispatch-selected (entry pop &optional set-primary)
144 "Go to 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 (let ((set-primary-as-arg (and set-primary '(16))))
150 (if multishell-completing-read
151 ;; In multishell completing-read, arrange to conclude minibuffer input:
152 (throw 'multishell-minibuffer-exit (list entry pop set-primary-as-arg))
153 (multishell-pop-to-shell set-primary-as-arg entry (not pop)))))
155 (defun multishell-list-placeholder (value default)
156 "Return VALUE if non-empty string, else DEFAULT."
157 (if (or (not value) (string= value ""))
160 (defconst multishell-list-active-flag "+")
161 (defconst multishell-list-inactive-flag ".")
162 (defconst multishell-list-absent-flag "x")
164 (defun multishell-list-entries ()
165 "Generate multishell name/path-spec entries list for tabulated-list."
167 (mapcar #'(lambda (entry)
168 (setq recency (1+ recency))
169 (let* ((splat (multishell-split-entry entry))
173 (multishell-bracket name))))
174 (status (cond ((not buffer)
175 multishell-list-absent-flag)
176 ((comint-check-proc buffer)
177 multishell-list-active-flag)
178 (t multishell-list-inactive-flag)))
180 (dir (and rest (or (file-remote-p rest 'localname)
183 (file-remote-p rest 'localname)
185 rest 0 (- (length rest) (length dir))))))
187 (setq name (multishell-name-from-entry entry)))
189 (vector (format "%d" recency)
191 (multishell-list--decorate-name name)
192 (multishell-list-placeholder hops "-")
193 (multishell-list-placeholder dir "~")))))
194 (multishell-all-entries))))
196 (defun multishell-list-goto-item-by-entry (entry)
197 "Position at beginning of line of tabulated list item for ENTRY."
198 (goto-char (point-min))
199 (while (and (not (eobp))
200 (not (string= (tabulated-list-get-id) entry)))
203 (defun multishell-collate-row-strings-as-numbers (a b)
204 (let ((a (aref (cadr a) 0))
205 (b (aref (cadr b) 0)))
206 (> (string-to-number a) (string-to-number b))))
208 (defun multishell-list--decorate-name (name)
210 'font-lock-face 'multishell-list-name
211 'mouse-face 'highlight))
213 (defvar multishell-list-mode-map
214 (let ((map (make-sparse-keymap)))
215 (set-keymap-parent map tabulated-list-mode-map)
216 (define-key map (kbd "c") 'multishell-list-clone-entry)
217 (define-key map (kbd "d") 'multishell-list-delete)
218 (define-key map (kbd "\C-k") 'multishell-list-delete)
219 (define-key map (kbd "k") 'multishell-list-delete)
220 (define-key map (kbd "e") 'multishell-list-edit-entry)
221 (define-key map (kbd "o") 'multishell-list-open-pop)
222 (define-key map (kbd " ") 'multishell-list-open-pop)
223 (define-key map (kbd "O") 'multishell-list-open-as-default)
224 (define-key map (kbd "RET") 'multishell-list-open-here)
225 (define-key map [mouse-2] 'multishell-list-mouse-select)
226 (define-key map [follow-link] 'mouse-face)
228 (define-derived-mode multishell-list-mode
229 tabulated-list-mode "Shells"
230 "Major mode for listing current and historically registered shells.
232 Initial sort is from most to least recently used:
234 - First active shells, flagged with '+' a plus sign
235 - Then, inactive shells, flagged with '.' a period
236 - Then historical shells that currently have no buffer, flagged with 'x' an ex
238 \\{multishell-list-mode-map\}"
239 (setq tabulated-list-format
240 [;; (name width sort '(:right-align nil :pad-right nil))
241 ("#" 0 multishell-collate-row-strings-as-numbers :pad-right 1)
242 ("! " 1 t :pad-right 1)
246 tabulated-list-sort-key '("#" . t)
247 tabulated-list-entries #'multishell-list-entries)
248 (tabulated-list-init-header))
250 (defun multishell-list-cull-dups (entries)
251 "Return list of multishell ENTRIES sans ones with duplicate names.
253 For duplicates, we prefer the ones that have paths."
254 (let ((tally (make-hash-table :test #'equal))
255 got name name-order-reversed already)
256 (mapcar #'(lambda (entry)
257 (setq name (multishell-name-from-entry entry)
258 already (gethash name tally nil))
260 (push name name-order-reversed))
261 (when (or (not already) (< (length already) (length entry)))
262 ;; Add new or replace shorter prior entry for name:
263 (puthash name entry tally)))
265 (dolist (name name-order-reversed)
266 (push (gethash name tally) got))
270 (defun multishell-list (&optional completing)
271 "Edit your current and historic list of shell buffers.
273 If optional COMPLETING is nil, we present the full
274 `multishell-history' list in a popped buffer named '*Shells*'.
276 In the buffer, hit ? or h for a list of commands.
278 When optional COMPLETING is non-nil, it must be a list of
279 multishell-history completion candidate entries, as provided by
280 `completing-read'. Then we present the list as a part of
281 minibuffer completion.
283 You can get to the shells listing by recursively invoking
284 \\[multishell-pop-to-shell] at the `multishell-pop-to-shell'
285 `universal-argument' prompts."
287 (let ((from-entry (car (multishell-history-entries
288 (multishell-unbracket (buffer-name
289 (current-buffer))))))
290 (buffer (get-buffer-create (if completing
295 (pop-to-buffer buffer))
296 (multishell-list-mode)
298 ;; Temporarily assign multishell-history only when completing:
299 (when completing '(multishell-history))
301 (list (multishell-list-cull-dups (mapcar 'substring-no-properties
303 (tabulated-list-print))
307 (multishell-list-goto-item-by-entry from-entry))))
309 (provide 'multishell-list)
310 (require 'multishell)
312 ;;; multishell-list.el ends here