]> code.delx.au - gnu-emacs-elpa/blob - multishell-list.el
multishell - roughly working draft using multishell-list for completions
[gnu-emacs-elpa] / 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 (or rest "") 'localname)
162 rest))
163 (hops (and dir
164 (file-remote-p rest 'localname)
165 (substring
166 rest 0 (- (length rest) (length dir))))))
167 (when (not name)
168 (setq name (multishell-name-from-entry entry)))
169 (list entry
170 (vector (format "%d" recency)
171 status
172 (multishell-list--decorate-name name)
173 (multishell-list-placeholder hops "-")
174 (multishell-list-placeholder dir "~")))))
175 (multishell-all-entries))))
176
177 (defun multishell-list-goto-item-by-entry (entry)
178 "Position at beginning of line of tabulated list item for ENTRY."
179 (goto-char (point-min))
180 (while (and (not (eobp))
181 (not (string= (tabulated-list-get-id) entry)))
182 (forward-line 1)))
183
184 (defun multishell-collate-row-strings-as-numbers (a b)
185 (let ((a (aref (cadr a) 0))
186 (b (aref (cadr b) 0)))
187 (> (string-to-number a) (string-to-number b))))
188
189 (defun multishell-list--decorate-name (name)
190 (propertize name
191 'font-lock-face 'multishell-list-name
192 'mouse-face 'highlight))
193
194 (defun multishell-list-mouse-select (event)
195 "Select the shell whose line is clicked."
196 (interactive "e")
197 (select-window (posn-window (event-end event)))
198 (let ((entry (tabulated-list-get-id (posn-point (event-end event)))))
199 (multishell-pop-to-shell nil entry 'here)))
200
201 (defvar multishell-list-mode-map
202 (let ((map (make-sparse-keymap)))
203 (set-keymap-parent map tabulated-list-mode-map)
204 (define-key map (kbd "c") 'multishell-list-clone-entry)
205 (define-key map (kbd "d") 'multishell-list-delete)
206 (define-key map (kbd "\C-k") 'multishell-list-delete)
207 (define-key map (kbd "k") 'multishell-list-delete)
208 (define-key map (kbd "e") 'multishell-list-edit-entry)
209 (define-key map (kbd "o") 'multishell-list-open-pop)
210 (define-key map (kbd " ") 'multishell-list-open-pop)
211 (define-key map (kbd "O") 'multishell-list-open-as-default)
212 (define-key map (kbd "RET") 'multishell-list-open-here)
213 (define-key map [mouse-2] 'multishell-list-mouse-select)
214 (define-key map [follow-link] 'mouse-face)
215 map))
216 (define-derived-mode multishell-list-mode
217 tabulated-list-mode "Shells"
218 "Major mode for listing current and historically registered shells.
219
220 Initial sort is from most to least recently used:
221
222 - First active shells, flagged with '+' a plus sign
223 - Then, inactive shells, flagged with '.' a period
224 - Then historical shells that currently have no buffer, flagged with 'x' an ex
225
226 \\{multishell-list-mode-map\}"
227 (setq tabulated-list-format
228 [;; (name width sort '(:right-align nil :pad-right nil))
229 ("#" 0 multishell-collate-row-strings-as-numbers :pad-right 1)
230 ("! " 1 t :pad-right 1)
231 ("Name" 15 t)
232 ("Hops" 30 t)
233 ("Directory" 30 t)]
234 tabulated-list-sort-key '("#" . t)
235 tabulated-list-entries #'multishell-list-entries)
236 (tabulated-list-init-header))
237
238 ;;;###autoload
239 (defun multishell-list (&optional buffer-name)
240 "Edit your current and historic list of shell buffers.
241
242 Hit ? for a list of commands.
243
244 You can get to this shell listing manager by
245 recursively invoking \\[multishell-pop-to-shell] at either of the
246 `multishell-pop-to-shell' universal argument prompts."
247 (interactive)
248 (let ((from-entry (car (multishell-history-entries
249 (multishell-unbracket (buffer-name
250 (current-buffer))))))
251 (buffer (get-buffer-create (or buffer-name "*Shells*"))))
252 (if buffer-name
253 (set-buffer buffer)
254 (pop-to-buffer buffer))
255 (multishell-list-mode)
256 (tabulated-list-print)
257 (when from-entry
258 (multishell-list-goto-item-by-entry from-entry))))
259
260 (provide 'multishell-list)
261 (require 'multishell)
262
263 ;;; multishell-list.el ends here
264 o