]> code.delx.au - gnu-emacs-elpa/blob - packages/multishell/multishell-list.el
Fix some quoting problems in doc strings
[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.5
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-list-dispatch-selected entry t))
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-list-dispatch-selected entry t t)
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-list-dispatch-selected entry nil))
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-list-dispatch-selected revised-name t))
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-list-dispatch-selected new-name t)))))
135
136 (defun multishell-list-mouse-select (event)
137 "Select the shell whose line is clicked."
138 (interactive "e")
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)))
142
143 (defun multishell-list-dispatch-selected (entry pop &optional set-primary)
144 "Go to multishell ENTRY, popping to window if POP is non-nil.
145
146 Optional arg SET-PRIMARY non-nil sets `multishell-primary-name' to entry.
147
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)))))
154
155 (defun multishell-list-placeholder (value default)
156 "Return VALUE if non-empty string, else DEFAULT."
157 (if (or (not value) (string= value ""))
158 default
159 value))
160 (defconst multishell-list-active-flag "+")
161 (defconst multishell-list-inactive-flag ".")
162 (defconst multishell-list-absent-flag "x")
163
164 (defun multishell-list-entries ()
165 "Generate multishell name/path-spec entries list for tabulated-list."
166 (let ((recency 0))
167 (mapcar #'(lambda (entry)
168 (setq recency (1+ recency))
169 (let* ((splat (multishell-split-entry entry))
170 (name (car splat))
171 (buffer (and name
172 (get-buffer
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)))
179 (rest (cadr splat))
180 (dir (and rest (or (file-remote-p rest 'localname)
181 rest)))
182 (hops (and dir
183 (file-remote-p rest 'localname)
184 (substring
185 rest 0 (- (length rest) (length dir))))))
186 (when (not name)
187 (setq name (multishell-name-from-entry entry)))
188 (list entry
189 (vector (format "%d" recency)
190 status
191 (multishell-list--decorate-name name)
192 (multishell-list-placeholder hops "-")
193 (multishell-list-placeholder dir "~")))))
194 (multishell-all-entries))))
195
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)))
201 (forward-line 1)))
202
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))))
207
208 (defun multishell-list--decorate-name (name)
209 (propertize name
210 'font-lock-face 'multishell-list-name
211 'mouse-face 'highlight))
212
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)
227 map))
228 (define-derived-mode multishell-list-mode
229 tabulated-list-mode "Shells"
230 "Major mode for listing current and historically registered shells.
231
232 Initial sort is from most to least recently used:
233
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
237
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)
243 ("Name" 15 t)
244 ("Hops" 30 t)
245 ("Directory" 30 t)]
246 tabulated-list-sort-key '("#" . t)
247 tabulated-list-entries #'multishell-list-entries)
248 (tabulated-list-init-header))
249
250 (defun multishell-list-cull-dups (entries)
251 "Return list of multishell ENTRIES sans ones with duplicate names.
252
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))
259 (when (not already)
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)))
264 entries)
265 (dolist (name name-order-reversed)
266 (push (gethash name tally) got))
267 got))
268
269 ;;;###autoload
270 (defun multishell-list (&optional completing)
271 "Edit your current and historic list of shell buffers.
272
273 If optional COMPLETING is nil, we present the full
274 `multishell-history' list in a popped buffer named `*Shells*'.
275
276 In the buffer, hit ? or h for a list of commands.
277
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.
282
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."
286 (interactive)
287 (let ((from-entry (car (multishell-history-entries
288 (multishell-unbracket (buffer-name
289 (current-buffer))))))
290 (buffer (get-buffer-create (if completing
291 "*Completions*"
292 "*Shells*"))))
293 (if completing
294 (set-buffer buffer)
295 (pop-to-buffer buffer))
296 (multishell-list-mode)
297 (progv
298 ;; Temporarily assign multishell-history only when completing:
299 (when completing '(multishell-history))
300 (when completing
301 (list (multishell-list-cull-dups (mapcar 'substring-no-properties
302 completing))))
303 (tabulated-list-print))
304 (when completing
305 )
306 (when from-entry
307 (multishell-list-goto-item-by-entry from-entry))))
308
309 (provide 'multishell-list)
310 (require 'multishell)
311
312 ;;; multishell-list.el ends here