]> code.delx.au - gnu-emacs-elpa/blob - multishell-list.el
63c6f120065313341073e4ec9b54dd5ddbca3912
[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.0.10
7 ;; Created: 2016 -- first public availability
8 ;; Keywords: processes
9 ;; URL: https://github.com/kenmanheimer/EmacsMultishell
10
11 (require 'tabulated-list)
12
13 (defun multishell-list-open-pop ()
14 "Pop to current entry's shell, and refresh the listing buffer."
15 (interactive)
16 (let ((list-buffer (current-buffer)))
17 (multishell-pop-to-shell nil (tabulated-list-get-id))
18 (with-current-buffer list-buffer
19 (revert-buffer))))
20 (defun multishell-list-open-as-default ()
21 "Pop to current entry's shell, and set as the default shell."
22 (interactive)
23 (let ((list-buffer (current-buffer)))
24 (message "%s <==" (multishell-name-from-entry (tabulated-list-get-id)))
25 (multishell-pop-to-shell '(16) (tabulated-list-get-id))
26 (with-current-buffer list-buffer
27 (revert-buffer))))
28 (defun multishell-list-open-here ()
29 "Switch to current entry's shell buffer."
30 (interactive)
31 (let ((list-buffer (current-buffer)))
32 (multishell-pop-to-shell nil (tabulated-list-get-id) 'here)
33 (with-current-buffer list-buffer
34 ;; In case they use switch-to-buffer or whatever to return.
35 (revert-buffer))))
36
37 (defun multishell-list-delete ()
38 "Remove current shell entry, and prompt for buffer-removal if present.
39
40 \(We depend on intrinsic confirmation prompts for active buffers,
41 supplemented by our own when buffer is inactive.)"
42 (interactive)
43 (let* ((entry (tabulated-list-get-id))
44 (name (multishell-name-from-entry entry))
45 (name-bracketed (multishell-bracket name))
46 (buffer (get-buffer name-bracketed)))
47 (when (multishell-delete-history-name name)
48 (and buffer
49 ;; If the process is live, let shell-mode get confirmation:
50 (or (comint-check-proc (current-buffer))
51 (y-or-n-p (format "Kill buffer %s? " name-bracketed)))
52 (kill-buffer name-bracketed)))
53 (tabulated-list-delete-entry)))
54
55 (defun multishell-list-edit-entry ()
56 "Edit the value of current shell entry."
57 (interactive)
58 (let* ((where (save-excursion (beginning-of-line) (point)))
59 (entry (tabulated-list-get-id))
60 (name (multishell-name-from-entry entry))
61 (revised (multishell-read-unbracketed-entry
62 (format "Edit shell spec for %s: " name)
63 nil
64 entry))
65 (revised-path (and revised (cadr (multishell-split-entry revised))))
66 (revised-name (multishell-name-from-entry revised))
67 buffer)
68 (when (not (string= revised entry))
69 (multishell-delete-history-name name)
70 (when (and (not (string= name revised-name))
71 (setq buffer (get-buffer (multishell-bracket name))))
72 (with-current-buffer buffer
73 (rename-buffer (multishell-bracket revised-name))))
74 (multishell-register-name-to-path revised-name revised-path)
75 (revert-buffer)
76 (goto-char where))))
77
78 (defun multishell-list-placeholder (value default)
79 "Return VALUE if non-empty string, else DEFAULT."
80 (if (or (not value) (string= value ""))
81 default
82 value))
83 (defconst multishell-list-active-buffer-flag "+")
84 (defconst multishell-list-inactive-buffer-flag ".")
85 (defconst multishell-list-absent-buffer-flag "x")
86
87 (defun multishell-list-entries ()
88 "Generate multishell name/path entries list for tabulated-list."
89 (let ((recency 0))
90 (mapcar #'(lambda (entry)
91 (setq recency (1+ recency))
92 (let* ((splat (multishell-split-entry entry))
93 (name (car splat))
94 (buffer (and name
95 (get-buffer
96 (multishell-bracket name))))
97 (status (cond ((not buffer)
98 multishell-list-absent-buffer-flag)
99 ((comint-check-proc buffer)
100 multishell-list-active-buffer-flag)
101 (t multishell-list-inactive-buffer-flag)))
102 (rest (cadr splat))
103 (dissected (and rest (file-remote-p rest)
104 (tramp-dissect-file-name rest t)))
105 (path (or (and dissected (aref dissected 3))
106 rest))
107 (hops (and dissected
108 (substring
109 rest 0 (- (length rest) (length path))))))
110 (when (not name)
111 (setq name (multishell-name-from-entry entry)))
112 (list entry
113 (vector (format "%d" recency)
114 status
115 name
116 (multishell-list-placeholder hops "-")
117 (multishell-list-placeholder path "~")))))
118 (multishell-all-entries))))
119
120 (defun compare-strings-as-numbers (a b)
121 (let ((a (aref (cadr a) 0))
122 (b (aref (cadr b) 0)))
123 (> (string-to-number a) (string-to-number b))))
124 (define-derived-mode multishell-list-mode
125 tabulated-list-mode "Shells"
126 "Major mode for listing current and historically registered shells..
127 \\{multishell-list-mode-map\}"
128 (setq tabulated-list-format
129 [;; (name width sort '(:right-align nil :pad-right nil))
130 ("#" 0 compare-strings-as-numbers :pad-right 1)
131 ("! " 1 t :pad-right 1)
132 ("Name" 15 t)
133 ("Hops" 30 t)
134 ("Path" 30 t)]
135 tabulated-list-sort-key '("#" . t)
136 tabulated-list-entries #'multishell-list-entries)
137 (tabulated-list-init-header))
138
139 (defvar multishell-list-already-re-reverting nil
140 "Don't set - internal for `multishell-list-revert-buffer-kludge'.")
141 (defun multishell-list-revert-buffer-kludge ()
142 "Double revert for kludge workaround of untable sorting."
143 (if (not multishell-list-already-re-reverting)
144 (let ((multishell-list-already-re-reverting t))
145 (revert-buffer))))
146 (add-hook 'tabulated-list-revert-hook 'multishell-list-revert-buffer-kludge)
147
148 (define-key multishell-list-mode-map (kbd "d") 'multishell-list-delete)
149 (define-key multishell-list-mode-map (kbd "\C-k") 'multishell-list-delete)
150 (define-key multishell-list-mode-map (kbd "k") 'multishell-list-delete)
151 (define-key multishell-list-mode-map (kbd "e") 'multishell-list-edit-entry)
152 (define-key multishell-list-mode-map (kbd "o") 'multishell-list-open-pop)
153 (define-key multishell-list-mode-map (kbd " ") 'multishell-list-open-pop)
154 (define-key multishell-list-mode-map (kbd "O") 'multishell-list-open-as-default)
155 (define-key multishell-list-mode-map
156 (kbd "<return>") 'multishell-list-open-here)
157
158 ;;;###autoload
159 (defun multishell-list ()
160 "Edit your current and historic list of shell buffers.
161
162 Hit ? for a list of commands.
163
164 You can get to this shell listing manager by
165 recursively invoking \\[multishell-pop-to-shell] at either of the
166 `multishell-pop-to-shell' universal argument prompts."
167 (interactive)
168 (let ((buffer (get-buffer-create "*Shells*")))
169 (pop-to-buffer buffer)
170 (multishell-list-mode)
171 (tabulated-list-print)))
172
173 (provide 'multishell-list)
174 (require 'multishell)
175
176 ;;; multishell-list.el ends here