]> code.delx.au - gnu-emacs-elpa/blob - multishell-list.el
f80ed4bc39398b46f66e7358cc96965ca4843d40
[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 ((start-buffer (current-buffer)))
17 (multishell-pop-to-shell nil (tabulated-list-get-id))
18 (with-current-buffer start-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 ((start-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 start-buffer
27 (revert-buffer)))
28 (defun multishell-list-open-here ()
29 "Switch to current entry's shell buffer."
30 (interactive)
31 (let ((start-buffer (current-buffer)))
32 (multishell-pop-to-shell nil (tabulated-list-get-id) 'here))
33 (with-current-buffer start-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* ((where (save-excursion (beginning-of-line) (point)))
44 (entry (tabulated-list-get-id))
45 (name (multishell-name-from-entry entry))
46 (name-bracketed (multishell-bracket name))
47 (buffer (get-buffer name-bracketed)))
48 (when (multishell-delete-history-name name)
49 (and buffer
50 ;; If the process is live, let shell-mode get confirmation:
51 (or (comint-check-proc (current-buffer))
52 (y-or-n-p (format "Kill buffer %s? " name-bracketed)))
53 (kill-buffer name-bracketed)))
54 (revert-buffer)
55 (goto-char where)))
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 nil
66 entry))
67 (revised-path (and revised (cadr (multishell-split-entry revised))))
68 (revised-name (multishell-name-from-entry revised))
69 buffer)
70 (when (not (string= revised entry))
71 (multishell-delete-history-name name)
72 (when (and (not (string= name revised-name))
73 (setq buffer (get-buffer (multishell-bracket name))))
74 (with-current-buffer buffer
75 (rename-buffer (multishell-bracket revised-name))))
76 (multishell-register-name-to-path revised-name revised-path)
77 (revert-buffer)
78 (goto-char where))))
79
80 (defun multishell-list-placeholder (value default)
81 "Return VALUE if non-empty string, else DEFAULT."
82 (if (or (not value) (string= value ""))
83 default
84 value))
85 (defun multishell-list-entries ()
86 "Generate multishell name/path entries list for tabulated-list."
87 (let ((recency 0))
88 (mapcar #'(lambda (entry)
89 (setq recency (1+ recency))
90 (let* ((splat (multishell-split-entry entry))
91 (name (car splat))
92 (buffer (and name
93 (get-buffer
94 (multishell-bracket name))))
95 (status (cond ((not buffer) "x")
96 ((comint-check-proc buffer) "+")
97 (t ".")))
98 (rest (cadr splat))
99 (dissected (and rest (file-remote-p rest)
100 (tramp-dissect-file-name rest t)))
101 (path (or (and dissected (aref dissected 3))
102 rest))
103 (hops (and dissected
104 (substring
105 rest 0 (- (length rest) (length path))))))
106 (when (not name)
107 (setq name (multishell-name-from-entry entry)))
108 (list entry
109 (vector (format "%d" recency)
110 status
111 name
112 (multishell-list-placeholder hops "-")
113 (multishell-list-placeholder path "~")))))
114 (multishell-all-entries))))
115
116 (defun compare-strings-as-numbers (a b)
117 (let ((a (aref (cadr a) 0))
118 (b (aref (cadr b) 0)))
119 (> (string-to-number a) (string-to-number b))))
120 (define-derived-mode multishell-list-mode
121 tabulated-list-mode "Shells"
122 "Major mode for listing current and historically registered shells..
123 \\{multishell-list-mode-map\}"
124 (setq tabulated-list-format
125 [;; (name width sort '(:right-align nil :pad-right nil))
126 ("#" 0 compare-strings-as-numbers)
127 ("! " 1 t)
128 ("Name" 15 t)
129 ("Hops" 30 t)
130 ("Path" 30 t)]
131 tabulated-list-sort-key '( "#" . t)
132 tabulated-list-entries #'multishell-list-entries)
133 (tabulated-list-init-header))
134
135 (defvar multishell-list-already-re-reverting nil
136 "Don't set - internal for `multishell-list-revert-buffer-kludge'.")
137 (defun multishell-list-revert-buffer-kludge ()
138 "Double revert for kludge workaround of untable sorting."
139 (if (not multishell-list-already-re-reverting)
140 (let ((multishell-list-already-re-reverting t))
141 (revert-buffer))))
142 (add-hook 'tabulated-list-revert-hook 'multishell-list-revert-buffer-kludge)
143
144 (define-key multishell-list-mode-map (kbd "d") 'multishell-list-delete)
145 (define-key multishell-list-mode-map (kbd "e") 'multishell-list-edit-entry)
146 (define-key multishell-list-mode-map (kbd "o") 'multishell-list-open-pop)
147 (define-key multishell-list-mode-map (kbd "O") 'multishell-list-open-as-default)
148 (define-key multishell-list-mode-map
149 (kbd "<return>") 'multishell-list-open-here)
150
151 ;;;###autoload
152 (defun multishell-list ()
153 "Edit your current and historic list of shell buffers.
154
155 Hit ? for a list of commands.
156
157 You can get to this shell listing manager by
158 recursively invoking \\[multishell-pop-to-shell] at either of the
159 `multishell-pop-to-shell' universal argument prompts."
160 (interactive)
161 (let ((buffer (get-buffer-create "*Shells*")))
162 (pop-to-buffer buffer)
163 (multishell-list-mode)
164 (tabulated-list-print)))
165
166 (provide 'multishell-list)
167 (require 'multishell)
168
169 ;;; multishell-list.el ends here