]> code.delx.au - gnu-emacs-elpa/blob - multishell-list.el
multishell-list-open-as-default - new operation, on "O"
[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.9
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 (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."
19 (interactive)
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."
24 (interactive)
25 (multishell-pop-to-shell nil (tabulated-list-get-id) 'here))
26
27 (defun multishell-list-delete ()
28 "Remove current environment variable value."
29 (interactive)
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)
36 (and buffer
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)))
41 (revert-buffer)
42 (if (not tabulated-list-sort-key)
43 (revert-buffer))
44 (goto-char where)))
45
46 (defun multishell-list-edit-entry ()
47 "Edit the value of current shell entry."
48 (interactive)
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)
54 nil
55 entry))
56 (revised-path (and revised (cadr (multishell-split-entry revised))))
57 (revised-name (multishell-name-from-entry revised))
58 buffer)
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)
66 (revert-buffer)
67 (if (not tabulated-list-sort-key)
68 (revert-buffer))
69 (goto-char where))))
70
71 (defun multishell-list-placeholder (value default)
72 "Return VALUE if non-empty string, else DEFAULT."
73 (if (or (not value) (string= value ""))
74 default
75 value))
76 (defun multishell-list-entries ()
77 "Generate multishell name/path entries list for tabulated-list."
78 (let ((recency 0))
79 (mapcar #'(lambda (entry)
80 (setq recency (1+ recency))
81 (let* ((splat (multishell-split-entry entry))
82 (name (car splat))
83 (buffer (and name
84 (get-buffer
85 (multishell-bracket name))))
86 (status (cond ((not buffer) "x")
87 ((comint-check-proc buffer) "+")
88 (t ".")))
89 (rest (cadr splat))
90 (dissected (and rest (file-remote-p rest)
91 (tramp-dissect-file-name rest t)))
92 (path (or (and dissected (aref dissected 3))
93 rest))
94 (hops (and dissected
95 (substring
96 rest 0 (- (length rest) (length path))))))
97 (when (not name)
98 (setq name (multishell-name-from-entry entry)))
99 (list entry
100 (vector (format "%d" recency)
101 status
102 name
103 (multishell-list-placeholder hops "-")
104 (multishell-list-placeholder path ":")))))
105 (multishell-all-entries))))
106
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)
116 ("! " 1 t)
117 ("Name" 15 t)
118 ("Hops" 30 t)
119 ("Path" 30 t)]
120 tabulated-list-sort-key nil
121 tabulated-list-entries #'multishell-list-entries)
122 (tabulated-list-init-header))
123
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)
130
131 ;;;###autoload
132 (defun multishell-list ()
133 "Edit your current and historic list of shell buffers.
134
135 Hit ? for a list of commands.
136
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."
140 (interactive)
141 (let ((buffer (get-buffer-create "*Shells*")))
142 (pop-to-buffer buffer)
143 (multishell-list-mode)
144 (tabulated-list-print)))
145
146 (provide 'multishell-list)
147 (require 'multishell)
148
149 ;;; multishell-list.el ends here