]> code.delx.au - gnu-emacs-elpa/blob - packages/multishell/multishell-list.el
multishell - merge 1.1.1
[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.1
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 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 (defconst multishell-list-active-buffer-flag "+")
86 (defconst multishell-list-inactive-buffer-flag ".")
87 (defconst multishell-list-absent-buffer-flag "x")
88
89 (defun multishell-list-entries ()
90 "Generate multishell name/path entries list for tabulated-list."
91 (let ((recency 0))
92 (mapcar #'(lambda (entry)
93 (setq recency (1+ recency))
94 (let* ((splat (multishell-split-entry entry))
95 (name (car splat))
96 (buffer (and name
97 (get-buffer
98 (multishell-bracket name))))
99 (status (cond ((not buffer)
100 multishell-list-absent-buffer-flag)
101 ((comint-check-proc buffer)
102 multishell-list-active-buffer-flag)
103 (t multishell-list-inactive-buffer-flag)))
104 (rest (cadr splat))
105 (dissected (and rest (file-remote-p rest)
106 (tramp-dissect-file-name rest t)))
107 (path (or (and dissected (aref dissected 3))
108 rest))
109 (hops (and dissected
110 (substring
111 rest 0 (- (length rest) (length path))))))
112 (when (not name)
113 (setq name (multishell-name-from-entry entry)))
114 (list entry
115 (vector (format "%d" recency)
116 status
117 name
118 (multishell-list-placeholder hops "-")
119 (multishell-list-placeholder path "~")))))
120 (multishell-all-entries))))
121
122 (defun compare-strings-as-numbers (a b)
123 (let ((a (aref (cadr a) 0))
124 (b (aref (cadr b) 0)))
125 (> (string-to-number a) (string-to-number b))))
126 (define-derived-mode multishell-list-mode
127 tabulated-list-mode "Shells"
128 "Major mode for listing current and historically registered shells..
129 \\{multishell-list-mode-map\}"
130 (setq tabulated-list-format
131 [;; (name width sort '(:right-align nil :pad-right nil))
132 ("#" 0 compare-strings-as-numbers :pad-right 1)
133 ("! " 1 t :pad-right 1)
134 ("Name" 15 t)
135 ("Hops" 30 t)
136 ("Path" 30 t)]
137 tabulated-list-sort-key '("#" . t)
138 tabulated-list-entries #'multishell-list-entries)
139 (tabulated-list-init-header))
140
141 (defvar multishell-list-already-re-reverting nil
142 "Don't set - internal for `multishell-list-revert-buffer-kludge'.")
143 (defun multishell-list-revert-buffer-kludge ()
144 "Double revert for kludge workaround of untable sorting."
145 (if (not multishell-list-already-re-reverting)
146 (let ((multishell-list-already-re-reverting t))
147 (revert-buffer))))
148 (add-hook 'tabulated-list-revert-hook 'multishell-list-revert-buffer-kludge)
149
150 (define-key multishell-list-mode-map (kbd "d") 'multishell-list-delete)
151 (define-key multishell-list-mode-map (kbd "\C-k") 'multishell-list-delete)
152 (define-key multishell-list-mode-map (kbd "k") 'multishell-list-delete)
153 (define-key multishell-list-mode-map (kbd "e") 'multishell-list-edit-entry)
154 (define-key multishell-list-mode-map (kbd "o") 'multishell-list-open-pop)
155 (define-key multishell-list-mode-map (kbd " ") 'multishell-list-open-pop)
156 (define-key multishell-list-mode-map (kbd "O") 'multishell-list-open-as-default)
157 (define-key multishell-list-mode-map
158 (kbd "<return>") 'multishell-list-open-here)
159
160 ;;;###autoload
161 (defun multishell-list ()
162 "Edit your current and historic list of shell buffers.
163
164 Hit ? for a list of commands.
165
166 You can get to this shell listing manager by
167 recursively invoking \\[multishell-pop-to-shell] at either of the
168 `multishell-pop-to-shell' universal argument prompts."
169 (interactive)
170 (let ((buffer (get-buffer-create "*Shells*")))
171 (pop-to-buffer buffer)
172 (multishell-list-mode)
173 (tabulated-list-print)))
174
175 (provide 'multishell-list)
176 (require 'multishell)
177
178 ;;; multishell-list.el ends here