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 (multishell-pop-to-shell nil (tabulated-list-get-id)))
17 (defun multishell-list-open-as-default ()
18 "Pop to current entry's shell, and set as the default shell."
20 (message "%s <==" (multishell-name-from-entry (tabulated-list-get-id)))
21 (multishell-pop-to-shell '(16) (tabulated-list-get-id)))
22 (defun multishell-list-open-here ()
23 "Switch to current entry's shell buffer."
25 (multishell-pop-to-shell nil (tabulated-list-get-id) 'here))
27 (defun multishell-list-delete ()
28 "Remove current environment variable value."
30 (let* ((where (save-excursion (beginning-of-line) (point)))
31 (entry (tabulated-list-get-id))
32 (name (multishell-name-from-entry entry))
33 (name-bracketed (multishell-bracket name))
34 (buffer (get-buffer name-bracketed)))
35 (when (multishell-delete-history-name name)
37 ;; If the process is live, let shell-mode get confirmation:
38 (or (comint-check-proc (current-buffer))
39 (y-or-n-p (format "Kill buffer %s? " name-bracketed)))
40 (kill-buffer name-bracketed)))
42 (if (not tabulated-list-sort-key)
46 (defun multishell-list-edit-entry ()
47 "Edit the value of current shell entry."
49 (let* ((where (save-excursion (beginning-of-line) (point)))
50 (entry (tabulated-list-get-id))
51 (name (multishell-name-from-entry entry))
52 (revised (multishell-read-unbracketed-entry
53 (format "Edit shell spec for %s: " name)
56 (revised-path (and revised (cadr (multishell-split-entry revised))))
57 (revised-name (multishell-name-from-entry revised))
59 (when (not (string= revised entry))
60 (multishell-delete-history-name name)
61 (when (and (not (string= name revised-name))
62 (setq buffer (get-buffer (multishell-bracket name))))
63 (with-current-buffer buffer
64 (rename-buffer (multishell-bracket revised-name))))
65 (multishell-register-name-to-path revised-name revised-path)
67 (if (not tabulated-list-sort-key)
71 (defun multishell-list-placeholder (value default)
72 "Return VALUE if non-empty string, else DEFAULT."
73 (if (or (not value) (string= value ""))
76 (defun multishell-list-entries ()
77 "Generate multishell name/path entries list for tabulated-list."
79 (mapcar #'(lambda (entry)
80 (setq recency (1+ recency))
81 (let* ((splat (multishell-split-entry entry))
85 (multishell-bracket name))))
86 (status (cond ((not buffer) "x")
87 ((comint-check-proc buffer) "+")
90 (dissected (and rest (file-remote-p rest)
91 (tramp-dissect-file-name rest t)))
92 (path (or (and dissected (aref dissected 3))
96 rest 0 (- (length rest) (length path))))))
98 (setq name (multishell-name-from-entry entry)))
100 (vector (format "%d" recency)
103 (multishell-list-placeholder hops "-")
104 (multishell-list-placeholder path ":")))))
105 (multishell-all-entries))))
107 (defun compare-strings-as-numbers (a b)
108 (let ((a (aref (cadr a) 0))
109 (b (aref (cadr b) 0)))
110 (> (string-to-number a) (string-to-number b))))
111 (define-derived-mode multishell-list-mode
112 tabulated-list-mode "Shells"
113 "Major mode for listing current and historically registered shells..
114 \\{multishell-list-mode-map\}"
115 (setq tabulated-list-format [("#" 0 compare-strings-as-numbers)
120 tabulated-list-sort-key nil
121 tabulated-list-entries #'multishell-list-entries)
122 (tabulated-list-init-header))
124 (define-key multishell-list-mode-map (kbd "d") 'multishell-list-delete)
125 (define-key multishell-list-mode-map (kbd "e") 'multishell-list-edit-entry)
126 (define-key multishell-list-mode-map (kbd "o") 'multishell-list-open-pop)
127 (define-key multishell-list-mode-map (kbd "O") 'multishell-list-open-as-default)
128 (define-key multishell-list-mode-map
129 (kbd "<return>") 'multishell-list-open-here)
132 (defun multishell-list ()
133 "Edit your current and historic list of shell buffers.
135 Hit ? for a list of commands.
137 You can get to this shell listing manager by
138 recursively invoking \\[multishell-pop-to-shell] at either of the
139 `multishell-pop-to-shell' universal argument prompts."
141 (let ((buffer (get-buffer-create "*Shells*")))
142 (pop-to-buffer buffer)
143 (multishell-list-mode)
144 (tabulated-list-print)))
146 (provide 'multishell-list)
147 (require 'multishell)
149 ;;; multishell-list.el ends here