]> code.delx.au - gnu-emacs-elpa/blob - packages/multishell/multishell-list.el
Use font-lock-fontify-region instead of jit-lock-fontify-now
[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.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-here ()
18 "Switch to current entry's shell buffer."
19 (interactive)
20 (multishell-pop-to-shell nil (tabulated-list-get-id) 'here))
21
22 (defun multishell-list-delete ()
23 "Remove current environment variable value."
24 (interactive)
25 (let* ((where (save-excursion (beginning-of-line) (point)))
26 (entry (tabulated-list-get-id))
27 (name (multishell-name-from-entry entry))
28 (name-bracketed (multishell-bracket name))
29 (buffer (get-buffer name-bracketed)))
30 (when (multishell-delete-history-name name)
31 (and buffer
32 ;; If the process is live, let shell-mode get confirmation:
33 (or (comint-check-proc (current-buffer))
34 (y-or-n-p (format "Kill buffer %s? " name-bracketed)))
35 (kill-buffer name-bracketed)))
36 (revert-buffer)
37 (if (not tabulated-list-sort-key)
38 (revert-buffer))
39 (goto-char where)))
40
41 (defun multishell-list-edit-entry ()
42 "Edit the value of current shell entry."
43 (interactive)
44 (let* ((where (save-excursion (beginning-of-line) (point)))
45 (entry (tabulated-list-get-id))
46 (name (multishell-name-from-entry entry))
47 (revised (multishell-read-unbracketed-entry
48 (format "Edit shell spec for %s: " name)
49 nil
50 entry))
51 (revised-pair (when revised (multishell-split-entry revised))))
52 (when revised-pair
53 (multishell-delete-history-name name)
54 (multishell-register-name-to-path (car revised-pair) (cadr revised-pair))
55 (revert-buffer)
56 (if (not tabulated-list-sort-key)
57 (revert-buffer))
58 (goto-char where))))
59
60 (defun multishell-list-placeholder (value default)
61 "Return VALUE if non-empty string, else DEFAULT."
62 (if (or (not value) (string= value ""))
63 default
64 value))
65 (defun multishell-list-entries ()
66 "Generate multishell name/path entries list for tabulated-list."
67 (let ((recency 0))
68 (mapcar #'(lambda (entry)
69 (setq recency (1+ recency))
70 (let* ((splat (multishell-split-entry entry))
71 (name (car splat))
72 (buffer (and name
73 (get-buffer
74 (multishell-bracket name))))
75 (status (cond ((not buffer) "x")
76 ((comint-check-proc buffer) "+")
77 (t ".")))
78 (rest (cadr splat))
79 (dissected (and rest (file-remote-p rest)
80 (tramp-dissect-file-name rest t)))
81 (path (or (and dissected (aref dissected 3))
82 rest))
83 (hops (and dissected
84 (substring
85 rest 0 (- (length rest) (length path))))))
86 (when (not name)
87 (setq name (multishell-name-from-entry entry)))
88 (list entry
89 (vector (format "%d" recency)
90 status
91 name
92 (multishell-list-placeholder hops "-")
93 (multishell-list-placeholder path ":")))))
94 (multishell-all-entries))))
95
96 (defun compare-strings-as-numbers (a b)
97 (let ((a (aref (cadr a) 0))
98 (b (aref (cadr b) 0)))
99 (> (string-to-number a) (string-to-number b))))
100 (define-derived-mode multishell-list-mode
101 tabulated-list-mode "Shells"
102 "Major mode for listing current and historically registered shells..
103 \\{multishell-list-mode-map\}"
104 (setq tabulated-list-format [("#" 0 compare-strings-as-numbers)
105 ("! " 1 t)
106 ("Name" 15 t)
107 ("Hops" 30 t)
108 ("Path" 30 t)]
109 tabulated-list-sort-key nil
110 tabulated-list-entries #'multishell-list-entries)
111 (tabulated-list-init-header))
112
113 (define-key multishell-list-mode-map (kbd "d") 'multishell-list-delete)
114 (define-key multishell-list-mode-map (kbd "e") 'multishell-list-edit-entry)
115 (define-key multishell-list-mode-map (kbd "o") 'multishell-list-open-pop)
116 (define-key multishell-list-mode-map
117 (kbd "<return>") 'multishell-list-open-here)
118
119 ;;;###autoload
120 (defun multishell-list ()
121 "Edit your current and historic list of shell buffers.
122
123 Hit ? for a list of commands.
124
125 You can get to this shell listing manager by
126 recursively invoking \\[multishell-pop-to-shell] at either of the
127 `multishell-pop-to-shell' universal argument prompts."
128 (interactive)
129 (let ((buffer (get-buffer-create "*Shells*")))
130 (pop-to-buffer buffer)
131 (multishell-list-mode)
132 (tabulated-list-print)))
133
134 (provide 'multishell-list)
135 (require 'multishell)
136
137 ;;; multishell-list.el ends here