1 ;;; multishell-list.el --- tabulated-list-mode for multishell shell buffers
3 ;; Copyright (C) 2016 Free Software Foundation, Inc. and Ken Manheimer
5 ;; Author: Ken Manheimer <ken.manheimer@gmail.com>
7 ;; Created: 2016 -- first public availability
9 ;; URL: https://github.com/kenmanheimer/EmacsMultishell
11 ;; See multishell.el for commentary, change log, etc.
13 (require 'tabulated-list)
15 (defun multishell-list-open-pop ()
16 "Pop to current entry's shell, and refresh the listing buffer."
18 (let ((list-buffer (current-buffer)))
19 (multishell-pop-to-shell nil (tabulated-list-get-id))
20 (with-current-buffer list-buffer
22 (defun multishell-list-open-as-default ()
23 "Pop to current entry's shell, and set as the default shell."
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
30 (defun multishell-list-open-here ()
31 "Switch to current entry's shell buffer."
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.
39 (defun multishell-list-delete ()
40 "Remove current shell entry, and prompt for buffer-removal if present.
42 \(We depend on intrinsic confirmation prompts for active buffers,
43 supplemented by our own when buffer is inactive.)"
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)
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)))
57 (defun multishell-list-edit-entry ()
58 "Edit the value of current shell entry."
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)
68 (revised-name (multishell-name-from-entry revised))
70 (when (not (string= revised entry))
71 (multishell-replace-entry entry revised)
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))))
79 (defun multishell-list-placeholder (value default)
80 "Return VALUE if non-empty string, else DEFAULT."
81 (if (or (not value) (string= value ""))
84 (defconst multishell-list-active-buffer-flag "+")
85 (defconst multishell-list-inactive-buffer-flag ".")
86 (defconst multishell-list-absent-buffer-flag "x")
88 (defun multishell-list-entries ()
89 "Generate multishell name/path entries list for tabulated-list."
91 (mapcar #'(lambda (entry)
92 (setq recency (1+ recency))
93 (let* ((splat (multishell-split-entry entry))
97 (multishell-bracket name))))
98 (status (cond ((not buffer)
99 multishell-list-absent-buffer-flag)
100 ((comint-check-proc buffer)
101 multishell-list-active-buffer-flag)
102 (t multishell-list-inactive-buffer-flag)))
104 (dissected (and rest (file-remote-p rest)
105 (tramp-dissect-file-name rest t)))
106 (path (or (and dissected (aref dissected 3))
110 rest 0 (- (length rest) (length path))))))
112 (setq name (multishell-name-from-entry entry)))
114 (vector (format "%d" recency)
117 (multishell-list-placeholder hops "-")
118 (multishell-list-placeholder path "~")))))
119 (multishell-all-entries))))
121 (defun compare-strings-as-numbers (a b)
122 (let ((a (aref (cadr a) 0))
123 (b (aref (cadr b) 0)))
124 (> (string-to-number a) (string-to-number b))))
125 (define-derived-mode multishell-list-mode
126 tabulated-list-mode "Shells"
127 "Major mode for listing current and historically registered shells..
128 \\{multishell-list-mode-map\}"
129 (setq tabulated-list-format
130 [;; (name width sort '(:right-align nil :pad-right nil))
131 ("#" 0 compare-strings-as-numbers :pad-right 1)
132 ("! " 1 t :pad-right 1)
136 tabulated-list-sort-key '("#" . t)
137 tabulated-list-entries #'multishell-list-entries)
138 (tabulated-list-init-header))
140 (define-key multishell-list-mode-map (kbd "d") 'multishell-list-delete)
141 (define-key multishell-list-mode-map (kbd "\C-k") 'multishell-list-delete)
142 (define-key multishell-list-mode-map (kbd "k") 'multishell-list-delete)
143 (define-key multishell-list-mode-map (kbd "e") 'multishell-list-edit-entry)
144 (define-key multishell-list-mode-map (kbd "o") 'multishell-list-open-pop)
145 (define-key multishell-list-mode-map (kbd " ") 'multishell-list-open-pop)
146 (define-key multishell-list-mode-map (kbd "O") 'multishell-list-open-as-default)
147 (define-key multishell-list-mode-map
148 (kbd "<return>") 'multishell-list-open-here)
151 (defun multishell-list ()
152 "Edit your current and historic list of shell buffers.
154 Hit ? for a list of commands.
156 You can get to this shell listing manager by
157 recursively invoking \\[multishell-pop-to-shell] at either of the
158 `multishell-pop-to-shell' universal argument prompts."
160 (let ((buffer (get-buffer-create "*Shells*")))
161 (pop-to-buffer buffer)
162 (multishell-list-mode)
163 (tabulated-list-print)))
165 (provide 'multishell-list)
166 (require 'multishell)
168 ;;; multishell-list.el ends here