]> code.delx.au - gnu-emacs-elpa/blob - packages/multishell/multishell-list.el
Merge commit 'd827bb511203a64da3ae5cc6910b87b7c99d233b'
[gnu-emacs-elpa] / packages / multishell / multishell-list.el
1 ;;; multishell-list.el --- tabulated-list-mode for multishell shell buffers
2
3 ;; Copyright (C) 2016 Free Software Foundation, Inc. and Ken Manheimer
4
5 ;; Author: Ken Manheimer <ken.manheimer@gmail.com>
6 ;; Version: 1.1.3
7 ;; Created: 2016 -- first public availability
8 ;; Keywords: processes
9 ;; URL: https://github.com/kenmanheimer/EmacsMultishell
10
11 ;; See multishell.el for commentary, change log, etc.
12
13 (require 'tabulated-list)
14
15 (defgroup multishell-list nil
16 "Show a menu of all shell buffers in a buffer."
17 :group 'multishell)
18
19 (defface multishell-list-name
20 '((t (:weight bold)))
21 "Face for shell names in the Multishell List."
22 :group 'multishell-list)
23
24 (defun multishell-list-open-pop (&optional arg)
25 "Pop to current entry's shell in separate window.
26
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."
30 (interactive "P")
31 (let ((list-buffer (current-buffer))
32 (entry (tabulated-list-get-id)))
33 (if arg
34 (pop-to-buffer
35 (multishell-bracket (multishell-name-from-entry entry)))
36 (multishell-pop-to-shell nil entry))
37 (with-current-buffer list-buffer
38 (revert-buffer)
39 (multishell-list-goto-item-by-entry entry))))
40
41 (defun multishell-list-open-as-default ()
42 "Pop to current entry's shell, and set as the default shell."
43 (interactive)
44 (let ((list-buffer (current-buffer))
45 (entry (tabulated-list-get-id)))
46 (message "%s <==" (multishell-name-from-entry entry))
47 (multishell-pop-to-shell '(16) entry)
48 (with-current-buffer list-buffer
49 (revert-buffer)
50 (multishell-list-goto-item-by-entry entry))))
51
52 (defun multishell-list-open-here (&optional arg)
53 "Switch to current entry's shell buffer.
54
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."
58 (interactive "P")
59 (let* ((list-buffer (current-buffer))
60 (entry (tabulated-list-get-id)))
61 (if arg
62 (switch-to-buffer
63 (multishell-bracket (multishell-name-from-entry entry)))
64 (multishell-pop-to-shell nil entry 'here))
65 (with-current-buffer list-buffer
66 (revert-buffer))))
67
68 (defun multishell-list-delete (&optional arg)
69 "Remove current shell entry, and prompt for buffer-removal if present."
70 (interactive "P")
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)
76 (and buffer
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)))
82
83 (defun multishell-list-edit-entry (&optional arg)
84 "Edit the value of current shell entry.
85
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."
90
91 (interactive "P")
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)
97 entry
98 'no-record))
99 (revised-name (multishell-name-from-entry revised))
100 buffer)
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)))))
107 (when arg
108 (multishell-pop-to-shell nil revised-name))
109 (with-current-buffer list-buffer
110 (revert-buffer)
111 (multishell-list-goto-item-by-entry revised))))
112
113 (defun multishell-list-clone-entry (&optional arg)
114 "Create a new list entry, edited from the current one, ready to launch.
115
116 If you provide an optional `universal-argument', the new entry
117 will be launched when it's created.
118
119 The already existing original entry is left untouched."
120 (interactive "P")
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)
125 prototype
126 'no-record))
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)
131 (revert-buffer)
132 (multishell-list-goto-item-by-entry new)
133 (when arg
134 (multishell-pop-to-shell nil new-name)))))
135
136 (defun multishell-list-placeholder (value default)
137 "Return VALUE if non-empty string, else DEFAULT."
138 (if (or (not value) (string= value ""))
139 default
140 value))
141 (defconst multishell-list-active-flag "+")
142 (defconst multishell-list-inactive-flag ".")
143 (defconst multishell-list-absent-flag "x")
144
145 (defun multishell-list-entries ()
146 "Generate multishell name/path-spec entries list for tabulated-list."
147 (let ((recency 0))
148 (mapcar #'(lambda (entry)
149 (setq recency (1+ recency))
150 (let* ((splat (multishell-split-entry entry))
151 (name (car splat))
152 (buffer (and name
153 (get-buffer
154 (multishell-bracket name))))
155 (status (cond ((not buffer)
156 multishell-list-absent-flag)
157 ((comint-check-proc buffer)
158 multishell-list-active-flag)
159 (t multishell-list-inactive-flag)))
160 (rest (cadr splat))
161 (dir (or (file-remote-p rest 'localname)
162 rest))
163 (hops (and (file-remote-p rest 'localname)
164 (substring
165 rest 0 (- (length rest) (length dir))))))
166 (when (not name)
167 (setq name (multishell-name-from-entry entry)))
168 (list entry
169 (vector (format "%d" recency)
170 status
171 (multishell-list--decorate-name name)
172 (multishell-list-placeholder hops "-")
173 (multishell-list-placeholder dir "~")))))
174 (multishell-all-entries))))
175
176 (defun multishell-list-goto-item-by-entry (entry)
177 "Position at beginning of line of tabulated list item for ENTRY."
178 (goto-char (point-min))
179 (while (and (not (eobp))
180 (not (string= (tabulated-list-get-id) entry)))
181 (forward-line 1)))
182
183 (defun multishell-collate-row-strings-as-numbers (a b)
184 (let ((a (aref (cadr a) 0))
185 (b (aref (cadr b) 0)))
186 (> (string-to-number a) (string-to-number b))))
187
188 (defun multishell-list--decorate-name (name)
189 (propertize name
190 'font-lock-face 'multishell-list-name
191 'mouse-face 'highlight))
192
193 (defun multishell-list-mouse-select (event)
194 "Select the shell whose line is clicked."
195 (interactive "e")
196 (select-window (posn-window (event-end event)))
197 (let ((entry (tabulated-list-get-id (posn-point (event-end event)))))
198 (multishell-pop-to-shell nil entry 'here)))
199
200 (defvar multishell-list-mode-map
201 (let ((map (make-sparse-keymap)))
202 (set-keymap-parent map tabulated-list-mode-map)
203 (define-key map (kbd "c") 'multishell-list-clone-entry)
204 (define-key map (kbd "d") 'multishell-list-delete)
205 (define-key map (kbd "\C-k") 'multishell-list-delete)
206 (define-key map (kbd "k") 'multishell-list-delete)
207 (define-key map (kbd "e") 'multishell-list-edit-entry)
208 (define-key map (kbd "o") 'multishell-list-open-pop)
209 (define-key map (kbd " ") 'multishell-list-open-pop)
210 (define-key map (kbd "O") 'multishell-list-open-as-default)
211 (define-key map (kbd "RET") 'multishell-list-open-here)
212 (define-key map [mouse-2] 'multishell-list-mouse-select)
213 (define-key map [follow-link] 'mouse-face)
214 map))
215 (define-derived-mode multishell-list-mode
216 tabulated-list-mode "Shells"
217 "Major mode for listing current and historically registered shells.
218
219 Initial sort is from most to least recently used:
220
221 - First active shells, flagged with '+' a plus sign
222 - Then, inactive shells, flagged with '.' a period
223 - Then historical shells that currently have no buffer, flagged with 'x' an ex
224
225 \\{multishell-list-mode-map\}"
226 (setq tabulated-list-format
227 [;; (name width sort '(:right-align nil :pad-right nil))
228 ("#" 0 multishell-collate-row-strings-as-numbers :pad-right 1)
229 ("! " 1 t :pad-right 1)
230 ("Name" 15 t)
231 ("Hops" 30 t)
232 ("Directory" 30 t)]
233 tabulated-list-sort-key '("#" . t)
234 tabulated-list-entries #'multishell-list-entries)
235 (tabulated-list-init-header))
236
237 ;;;###autoload
238 (defun multishell-list ()
239 "Edit your current and historic list of shell buffers.
240
241 Hit ? for a list of commands.
242
243 You can get to this shell listing manager by
244 recursively invoking \\[multishell-pop-to-shell] at either of the
245 `multishell-pop-to-shell' universal argument prompts."
246 (interactive)
247 (let ((from-entry (car (multishell-history-entries
248 (multishell-unbracket (buffer-name
249 (current-buffer))))))
250 (buffer (get-buffer-create "*Shells*")))
251 (pop-to-buffer buffer)
252 (multishell-list-mode)
253 (tabulated-list-print)
254 (when from-entry
255 (multishell-list-goto-item-by-entry from-entry))))
256
257 (provide 'multishell-list)
258 (require 'multishell)
259
260 ;;; multishell-list.el ends here