multishell-list.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. ;;; multishell-list.el --- tabulated-list-mode for multishell shell buffers
  2. ;; Copyright (C) 2016 Free Software Foundation, Inc. and Ken Manheimer
  3. ;; Author: Ken Manheimer <ken.manheimer@gmail.com>
  4. ;; Version: 1.1.5
  5. ;; Created: 2016 -- first public availability
  6. ;; Keywords: processes
  7. ;; URL: https://github.com/kenmanheimer/EmacsMultishell
  8. ;; See multishell.el for commentary, change log, etc.
  9. (require 'tabulated-list)
  10. (defgroup multishell-list nil
  11. "Show a menu of all shell buffers in a buffer."
  12. :group 'multishell)
  13. (defface multishell-list-name
  14. '((t (:weight bold)))
  15. "Face for shell names in the Multishell List."
  16. :group 'multishell-list)
  17. (defun multishell-list-open-pop (&optional arg)
  18. "Pop to current entry's shell in separate window.
  19. The shell is started if it's not already going, unless this is
  20. invoked with optional `universal-argument'. In that case we
  21. pop to the buffer but don't change its run state."
  22. (interactive "P")
  23. (let ((list-buffer (current-buffer))
  24. (entry (tabulated-list-get-id)))
  25. (if arg
  26. (pop-to-buffer
  27. (multishell-bracket (multishell-name-from-entry entry)))
  28. (multishell-list-dispatch-selected entry t))
  29. (with-current-buffer list-buffer
  30. (revert-buffer)
  31. (multishell-list-goto-item-by-entry entry))))
  32. (defun multishell-list-open-as-default ()
  33. "Pop to current entry's shell, and set as the default shell."
  34. (interactive)
  35. (let ((list-buffer (current-buffer))
  36. (entry (tabulated-list-get-id)))
  37. (message "%s <==" (multishell-name-from-entry entry))
  38. (multishell-list-dispatch-selected entry t t)
  39. (with-current-buffer list-buffer
  40. (revert-buffer)
  41. (multishell-list-goto-item-by-entry entry))))
  42. (defun multishell-list-open-here (&optional arg)
  43. "Switch to current entry's shell buffer.
  44. The shell is started if it's not already going, unless this is
  45. invoked with optional `universal-argument'. In that case we
  46. switch to the buffer but don't activate (or deactivate) it it."
  47. (interactive "P")
  48. (let* ((list-buffer (current-buffer))
  49. (entry (tabulated-list-get-id)))
  50. (if arg
  51. (switch-to-buffer
  52. (multishell-bracket (multishell-name-from-entry entry)))
  53. (multishell-list-dispatch-selected entry nil))
  54. (with-current-buffer list-buffer
  55. (revert-buffer))))
  56. (defun multishell-list-delete (&optional arg)
  57. "Remove current shell entry, and prompt for buffer-removal if present."
  58. (interactive "P")
  59. (let* ((entry (tabulated-list-get-id))
  60. (name (multishell-name-from-entry entry))
  61. (name-bracketed (multishell-bracket name))
  62. (buffer (get-buffer name-bracketed)))
  63. (when (multishell-delete-history-name name)
  64. (and buffer
  65. ;; If the process is live, let shell-mode get confirmation:
  66. (or (comint-check-proc (current-buffer))
  67. (y-or-n-p (format "Kill buffer %s? " name-bracketed)))
  68. (kill-buffer name-bracketed)))
  69. (tabulated-list-delete-entry)))
  70. (defun multishell-list-edit-entry (&optional arg)
  71. "Edit the value of current shell entry.
  72. Submitting the change will not launch the entry, unless this is
  73. invoked with optional `universal-argument'. In the latter case,
  74. submitting the entry will pop to the shell in a new window,
  75. starting it if it's not already going."
  76. (interactive "P")
  77. (let* ((list-buffer (current-buffer))
  78. (entry (tabulated-list-get-id))
  79. (name (multishell-name-from-entry entry))
  80. (revised (multishell-read-unbracketed-entry
  81. (format "Edit shell spec for %s: " name)
  82. entry
  83. 'no-record))
  84. (revised-name (multishell-name-from-entry revised))
  85. buffer)
  86. (when (not (string= revised entry))
  87. (multishell-replace-entry entry revised)
  88. (when (and (not (string= name revised-name))
  89. (setq buffer (get-buffer (multishell-bracket name))))
  90. (with-current-buffer buffer
  91. (rename-buffer (multishell-bracket revised-name)))))
  92. (when arg
  93. (multishell-list-dispatch-selected revised-name t))
  94. (with-current-buffer list-buffer
  95. (revert-buffer)
  96. (multishell-list-goto-item-by-entry revised))))
  97. (defun multishell-list-clone-entry (&optional arg)
  98. "Create a new list entry, edited from the current one, ready to launch.
  99. If you provide an optional `universal-argument', the new entry
  100. will be launched when it's created.
  101. The already existing original entry is left untouched."
  102. (interactive "P")
  103. (let* ((prototype (tabulated-list-get-id))
  104. (name (multishell-name-from-entry prototype))
  105. (new (multishell-read-unbracketed-entry
  106. (format "Clone new shell spec from %s: " name)
  107. prototype
  108. 'no-record))
  109. (new-name (multishell-name-from-entry new))
  110. (new-path (cadr (multishell-split-entry new))))
  111. (when (not (string= new prototype))
  112. (multishell-register-name-to-path new-name new-path)
  113. (revert-buffer)
  114. (multishell-list-goto-item-by-entry new)
  115. (when arg
  116. (multishell-list-dispatch-selected new-name t)))))
  117. (defun multishell-list-mouse-select (event)
  118. "Select the shell whose line is clicked."
  119. (interactive "e")
  120. (select-window (posn-window (event-end event)))
  121. (let ((entry (tabulated-list-get-id (posn-point (event-end event)))))
  122. (multishell-list-dispatch-selected entry nil)))
  123. (defun multishell-list-dispatch-selected (entry pop &optional set-primary)
  124. "Go to multishell ENTRY, popping to window if POP is non-nil.
  125. Optional arg SET-PRIMARY non-nil sets `multishell-primary-name' to entry.
  126. Provide for concluding minibuffer interaction if we're in completing mode."
  127. (let ((set-primary-as-arg (and set-primary '(16))))
  128. (if multishell-completing-read
  129. ;; In multishell completing-read, arrange to conclude minibuffer input:
  130. (throw 'multishell-minibuffer-exit (list entry pop set-primary-as-arg))
  131. (multishell-pop-to-shell set-primary-as-arg entry (not pop)))))
  132. (defun multishell-list-placeholder (value default)
  133. "Return VALUE if non-empty string, else DEFAULT."
  134. (if (or (not value) (string= value ""))
  135. default
  136. value))
  137. (defconst multishell-list-active-flag "+")
  138. (defconst multishell-list-inactive-flag ".")
  139. (defconst multishell-list-absent-flag "x")
  140. (defun multishell-list-entries ()
  141. "Generate multishell name/path-spec entries list for tabulated-list."
  142. (let ((recency 0))
  143. (mapcar #'(lambda (entry)
  144. (setq recency (1+ recency))
  145. (let* ((splat (multishell-split-entry entry))
  146. (name (car splat))
  147. (buffer (and name
  148. (get-buffer
  149. (multishell-bracket name))))
  150. (status (cond ((not buffer)
  151. multishell-list-absent-flag)
  152. ((comint-check-proc buffer)
  153. multishell-list-active-flag)
  154. (t multishell-list-inactive-flag)))
  155. (rest (cadr splat))
  156. (dir (and rest (or (file-remote-p rest 'localname)
  157. rest)))
  158. (hops (and dir
  159. (file-remote-p rest 'localname)
  160. (substring
  161. rest 0 (- (length rest) (length dir))))))
  162. (when (not name)
  163. (setq name (multishell-name-from-entry entry)))
  164. (list entry
  165. (vector (format "%d" recency)
  166. status
  167. (multishell-list--decorate-name name)
  168. (multishell-list-placeholder hops "-")
  169. (multishell-list-placeholder dir "~")))))
  170. (multishell-all-entries))))
  171. (defun multishell-list-goto-item-by-entry (entry)
  172. "Position at beginning of line of tabulated list item for ENTRY."
  173. (goto-char (point-min))
  174. (while (and (not (eobp))
  175. (not (string= (tabulated-list-get-id) entry)))
  176. (forward-line 1)))
  177. (defun multishell-collate-row-strings-as-numbers (a b)
  178. (let ((a (aref (cadr a) 0))
  179. (b (aref (cadr b) 0)))
  180. (> (string-to-number a) (string-to-number b))))
  181. (defun multishell-list--decorate-name (name)
  182. (propertize name
  183. 'font-lock-face 'multishell-list-name
  184. 'mouse-face 'highlight))
  185. (defvar multishell-list-mode-map
  186. (let ((map (make-sparse-keymap)))
  187. (set-keymap-parent map tabulated-list-mode-map)
  188. (define-key map (kbd "c") 'multishell-list-clone-entry)
  189. (define-key map (kbd "d") 'multishell-list-delete)
  190. (define-key map (kbd "\C-k") 'multishell-list-delete)
  191. (define-key map (kbd "k") 'multishell-list-delete)
  192. (define-key map (kbd "e") 'multishell-list-edit-entry)
  193. (define-key map (kbd "o") 'multishell-list-open-pop)
  194. (define-key map (kbd " ") 'multishell-list-open-pop)
  195. (define-key map (kbd "O") 'multishell-list-open-as-default)
  196. (define-key map (kbd "RET") 'multishell-list-open-here)
  197. (define-key map [mouse-2] 'multishell-list-mouse-select)
  198. (define-key map [follow-link] 'mouse-face)
  199. map))
  200. (define-derived-mode multishell-list-mode
  201. tabulated-list-mode "Shells"
  202. "Major mode for listing current and historically registered shells.
  203. Initial sort is from most to least recently used:
  204. - First active shells, flagged with '+' a plus sign
  205. - Then, inactive shells, flagged with '.' a period
  206. - Then historical shells that currently have no buffer, flagged with 'x' an ex
  207. \\{multishell-list-mode-map\}"
  208. (setq tabulated-list-format
  209. [;; (name width sort '(:right-align nil :pad-right nil))
  210. ("#" 0 multishell-collate-row-strings-as-numbers :pad-right 1)
  211. ("! " 1 t :pad-right 1)
  212. ("Name" 15 t)
  213. ("Hops" 30 t)
  214. ("Directory" 30 t)]
  215. tabulated-list-sort-key '("#" . t)
  216. tabulated-list-entries #'multishell-list-entries)
  217. (tabulated-list-init-header))
  218. (defun multishell-list-cull-dups (entries)
  219. "Return list of multishell ENTRIES sans ones with duplicate names.
  220. For duplicates, we prefer the ones that have paths."
  221. (let ((tally (make-hash-table :test #'equal))
  222. got name name-order-reversed already)
  223. (mapcar #'(lambda (entry)
  224. (setq name (multishell-name-from-entry entry)
  225. already (gethash name tally nil))
  226. (when (not already)
  227. (push name name-order-reversed))
  228. (when (or (not already) (< (length already) (length entry)))
  229. ;; Add new or replace shorter prior entry for name:
  230. (puthash name entry tally)))
  231. entries)
  232. (dolist (name name-order-reversed)
  233. (push (gethash name tally) got))
  234. got))
  235. ;;;###autoload
  236. (defun multishell-list (&optional completing)
  237. "Edit your current and historic list of shell buffers.
  238. If optional COMPLETING is nil, we present the full
  239. `multishell-history' list in a popped buffer named '*Shells*'.
  240. In the buffer, hit ? or h for a list of commands.
  241. When optional COMPLETING is non-nil, it must be a list of
  242. multishell-history completion candidate entries, as provided by
  243. `completing-read'. Then we present the list as a part of
  244. minibuffer completion.
  245. You can get to the shells listing by recursively invoking
  246. \\[multishell-pop-to-shell] at the `multishell-pop-to-shell'
  247. `universal-argument' prompts."
  248. (interactive)
  249. (let ((from-entry (car (multishell-history-entries
  250. (multishell-unbracket (buffer-name
  251. (current-buffer))))))
  252. (buffer (get-buffer-create (if completing
  253. "*Completions*"
  254. "*Shells*"))))
  255. (if completing
  256. (set-buffer buffer)
  257. (pop-to-buffer buffer))
  258. (multishell-list-mode)
  259. (progv
  260. ;; Temporarily assign multishell-history only when completing:
  261. (when completing '(multishell-history))
  262. (when completing
  263. (list (multishell-list-cull-dups (mapcar 'substring-no-properties
  264. completing))))
  265. (tabulated-list-print))
  266. (when completing
  267. )
  268. (when from-entry
  269. (multishell-list-goto-item-by-entry from-entry))))
  270. (provide 'multishell-list)
  271. (require 'multishell)
  272. ;;; multishell-list.el ends here