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 (require 'tabulated-list)
13 (defun multishell-list-open-pop ()
14 "Pop to current entry's shell, and refresh the listing buffer."
16 (let ((start-buffer (current-buffer)))
17 (multishell-pop-to-shell nil (tabulated-list-get-id))
18 (with-current-buffer start-buffer
20 (defun multishell-list-open-as-default ()
21 "Pop to current entry's shell, and set as the default shell."
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
28 (defun multishell-list-open-here ()
29 "Switch to current entry's shell buffer."
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.
37 (defun multishell-list-delete ()
38 "Remove current shell entry, and prompt for buffer-removal if present.
40 \(We depend on intrinsic confirmation prompts for active buffers,
41 supplemented by our own when buffer is inactive.)"
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)
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)))
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)
67 (revised-path (and revised (cadr (multishell-split-entry revised))))
68 (revised-name (multishell-name-from-entry revised))
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)
80 (defun multishell-list-placeholder (value default)
81 "Return VALUE if non-empty string, else DEFAULT."
82 (if (or (not value) (string= value ""))
85 (defun multishell-list-entries ()
86 "Generate multishell name/path entries list for tabulated-list."
88 (mapcar #'(lambda (entry)
89 (setq recency (1+ recency))
90 (let* ((splat (multishell-split-entry entry))
94 (multishell-bracket name))))
95 (status (cond ((not buffer) "x")
96 ((comint-check-proc buffer) "+")
99 (dissected (and rest (file-remote-p rest)
100 (tramp-dissect-file-name rest t)))
101 (path (or (and dissected (aref dissected 3))
105 rest 0 (- (length rest) (length path))))))
107 (setq name (multishell-name-from-entry entry)))
109 (vector (format "%d" recency)
112 (multishell-list-placeholder hops "-")
113 (multishell-list-placeholder path "~")))))
114 (multishell-all-entries))))
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)
131 tabulated-list-sort-key '( "#" . t)
132 tabulated-list-entries #'multishell-list-entries)
133 (tabulated-list-init-header))
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))
142 (add-hook 'tabulated-list-revert-hook 'multishell-list-revert-buffer-kludge)
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)
152 (defun multishell-list ()
153 "Edit your current and historic list of shell buffers.
155 Hit ? for a list of commands.
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."
161 (let ((buffer (get-buffer-create "*Shells*")))
162 (pop-to-buffer buffer)
163 (multishell-list-mode)
164 (tabulated-list-print)))
166 (provide 'multishell-list)
167 (require 'multishell)
169 ;;; multishell-list.el ends here