]> code.delx.au - gnu-emacs-elpa/blob - packages/multishell/multishell-list.el
multishell - a bit more for 1.1.2, including missed change log entries.
[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.2
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 (defun multishell-list-open-pop ()
16 "Pop to current entry's shell, and refresh the listing buffer."
17 (interactive)
18 (let ((list-buffer (current-buffer)))
19 (multishell-pop-to-shell nil (tabulated-list-get-id))
20 (with-current-buffer list-buffer
21 (revert-buffer))))
22 (defun multishell-list-open-as-default ()
23 "Pop to current entry's shell, and set as the default shell."
24 (interactive)
25 (let ((list-buffer (current-buffer)))
26 (message "%s <==" (multishell-name-from-entry (tabulated-list-get-id)))
27 (multishell-pop-to-shell '(16) (tabulated-list-get-id))
28 (with-current-buffer list-buffer
29 (revert-buffer))))
30 (defun multishell-list-open-here ()
31 "Switch to current entry's shell buffer."
32 (interactive)
33 (let ((list-buffer (current-buffer)))
34 (multishell-pop-to-shell nil (tabulated-list-get-id) 'here)
35 (with-current-buffer list-buffer
36 ;; In case they use switch-to-buffer or whatever to return.
37 (revert-buffer))))
38
39 (defun multishell-list-delete ()
40 "Remove current shell entry, and prompt for buffer-removal if present.
41
42 \(We depend on intrinsic confirmation prompts for active buffers,
43 supplemented by our own when buffer is inactive.)"
44 (interactive)
45 (let* ((entry (tabulated-list-get-id))
46 (name (multishell-name-from-entry entry))
47 (name-bracketed (multishell-bracket name))
48 (buffer (get-buffer name-bracketed)))
49 (when (multishell-delete-history-name name)
50 (and buffer
51 ;; If the process is live, let shell-mode get confirmation:
52 (or (comint-check-proc (current-buffer))
53 (y-or-n-p (format "Kill buffer %s? " name-bracketed)))
54 (kill-buffer name-bracketed)))
55 (tabulated-list-delete-entry)))
56
57 (defun multishell-list-edit-entry ()
58 "Edit the value of current shell entry."
59 (interactive)
60 (let* ((where (save-excursion (beginning-of-line) (point)))
61 (entry (tabulated-list-get-id))
62 (name (multishell-name-from-entry entry))
63 (revised (multishell-read-unbracketed-entry
64 (format "Edit shell spec for %s: " name)
65 entry
66 'no-record))
67 (revised-name (multishell-name-from-entry revised))
68 buffer)
69 (when (not (string= revised entry))
70 (multishell-replace-entry entry revised)
71 (when (and (not (string= name revised-name))
72 (setq buffer (get-buffer (multishell-bracket name))))
73 (with-current-buffer buffer
74 (rename-buffer (multishell-bracket revised-name))))
75 (revert-buffer)
76 (goto-char where))))
77
78 (defun multishell-list-clone-entry (&optional arg)
79 "Create a new list entry based on editing the current one.
80
81 You will be left in the list at the entry, not yet launched.
82
83 Providing a universal argument will also open the new shell.
84
85 The already existing current entry is left untouched."
86 (interactive "P")
87 (let* ((prototype (tabulated-list-get-id))
88 (name (multishell-name-from-entry prototype))
89 (new (multishell-read-unbracketed-entry
90 (format "Clone new shell spec from %s: " name)
91 prototype
92 'no-record))
93 (new-name (multishell-name-from-entry new))
94 (new-path (cadr (multishell-split-entry new))))
95 (when (not (string= new prototype))
96 (multishell-register-name-to-path new-name new-path)
97 (revert-buffer)
98 (goto-char (point-min))
99 (re-search-forward (format "^ . \\b%s\\b"
100 (regexp-quote new-name)))
101 (beginning-of-line))))
102
103 (defun multishell-list-placeholder (value default)
104 "Return VALUE if non-empty string, else DEFAULT."
105 (if (or (not value) (string= value ""))
106 default
107 value))
108 (defconst multishell-list-active-buffer-flag "+")
109 (defconst multishell-list-inactive-buffer-flag ".")
110 (defconst multishell-list-absent-buffer-flag "x")
111
112 (defun multishell-list-entries ()
113 "Generate multishell name/path-spec entries list for tabulated-list."
114 (let ((recency 0))
115 (mapcar #'(lambda (entry)
116 (setq recency (1+ recency))
117 (let* ((splat (multishell-split-entry entry))
118 (name (car splat))
119 (buffer (and name
120 (get-buffer
121 (multishell-bracket name))))
122 (status (cond ((not buffer)
123 multishell-list-absent-buffer-flag)
124 ((comint-check-proc buffer)
125 multishell-list-active-buffer-flag)
126 (t multishell-list-inactive-buffer-flag)))
127 (rest (cadr splat))
128 (dir (or (file-remote-p rest 'localname)
129 rest))
130 (hops (and (file-remote-p rest 'localname)
131 (substring
132 rest 0 (- (length rest) (length dir))))))
133 (when (not name)
134 (setq name (multishell-name-from-entry entry)))
135 (list entry
136 (vector (format "%d" recency)
137 status
138 name
139 (multishell-list-placeholder hops "-")
140 (multishell-list-placeholder dir "~")))))
141 (multishell-all-entries))))
142
143 (defun compare-strings-as-numbers (a b)
144 (let ((a (aref (cadr a) 0))
145 (b (aref (cadr b) 0)))
146 (> (string-to-number a) (string-to-number b))))
147
148 (defvar multishell-list-mode-map
149 (let ((map (make-sparse-keymap)))
150 (define-key map (kbd "c") 'multishell-list-clone-entry)
151 (define-key map (kbd "d") 'multishell-list-delete)
152 (define-key map (kbd "\C-k") 'multishell-list-delete)
153 (define-key map (kbd "k") 'multishell-list-delete)
154 (define-key map (kbd "e") 'multishell-list-edit-entry)
155 (define-key map (kbd "o") 'multishell-list-open-pop)
156 (define-key map (kbd " ") 'multishell-list-open-pop)
157 (define-key map (kbd "O") 'multishell-list-open-as-default)
158 (define-key map (kbd "RET") 'multishell-list-open-here)
159 map))
160 (define-derived-mode multishell-list-mode
161 tabulated-list-mode "Shells"
162 "Major mode for listing current and historically registered shells.
163 \\{multishell-list-mode-map\}"
164 (setq tabulated-list-format
165 [;; (name width sort '(:right-align nil :pad-right nil))
166 ("#" 0 compare-strings-as-numbers :pad-right 1)
167 ("! " 1 t :pad-right 1)
168 ("Name" 15 t)
169 ("Hops" 30 t)
170 ("Directory" 30 t)]
171 tabulated-list-sort-key '("#" . t)
172 tabulated-list-entries #'multishell-list-entries)
173 (tabulated-list-init-header))
174
175 ;;;###autoload
176 (defun multishell-list ()
177 "Edit your current and historic list of shell buffers.
178
179 Hit ? for a list of commands.
180
181 You can get to this shell listing manager by
182 recursively invoking \\[multishell-pop-to-shell] at either of the
183 `multishell-pop-to-shell' universal argument prompts."
184 (interactive)
185 (let ((buffer (get-buffer-create "*Shells*")))
186 (pop-to-buffer buffer)
187 (multishell-list-mode)
188 (tabulated-list-print)))
189
190 (provide 'multishell-list)
191 (require 'multishell)
192
193 ;;; multishell-list.el ends here