guix-help.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. ;;; guix-help.el --- Help commands -*- lexical-binding: t -*-
  2. ;; Copyright © 2016–2018 Alex Kost <alezost@gmail.com>
  3. ;; This file is part of Emacs-Guix.
  4. ;; Emacs-Guix is free software; you can redistribute it and/or modify
  5. ;; it under the terms of the GNU General Public License as published by
  6. ;; the Free Software Foundation, either version 3 of the License, or
  7. ;; (at your option) any later version.
  8. ;;
  9. ;; Emacs-Guix is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with Emacs-Guix. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This file provides auxiliary help commands for Emacs-Guix.
  18. ;;; Code:
  19. (require 'dash)
  20. (require 'bui)
  21. (require 'guix nil t)
  22. (require 'guix-utils)
  23. ;;;###autoload
  24. (defun guix-info (&optional arg)
  25. "Show Emacs-Guix info manual.
  26. If ARG is non-nil (interactively with prefix), show Guix info manual."
  27. (interactive "P")
  28. (info (if arg "guix" "emacs-guix")))
  29. ;;; "Help" buffer
  30. (guix-define-groups help
  31. :group-doc "Settings for '\\[guix-help]'."
  32. :faces-group-doc "Faces for '\\[guix-help]'.")
  33. (defcustom guix-help-buffer-name "*Guix Help*"
  34. "Buffer name for '\\[guix-help]'."
  35. :type 'string
  36. :group 'guix-help)
  37. (defcustom guix-help-doc-column 40
  38. "Column at which 'doc' button is inserted."
  39. :type 'integer
  40. :group 'guix-help)
  41. (defcustom guix-help-key-column (+ 12 guix-help-doc-column)
  42. "Column at which command key bindings are inserted."
  43. :type 'integer
  44. :group 'guix-help)
  45. (defface guix-help-heading
  46. '((t :inherit bui-info-heading))
  47. "Face for headings in `guix-help-buffer-name' buffer."
  48. :group 'guix-help-faces)
  49. (defface guix-help-key
  50. '((t :inherit bui-hint-key))
  51. "Face for key bindings in `guix-help-buffer-name' buffer."
  52. :group 'guix-help-faces)
  53. (defvar guix-help-specifications
  54. '("Show packages"
  55. guix-all-available-packages
  56. guix-newest-available-packages
  57. guix-installed-user-packages
  58. guix-installed-system-packages
  59. guix-installed-packages
  60. guix-obsolete-packages
  61. guix-superseded-packages
  62. guix-packages-by-name
  63. guix-packages-by-license
  64. guix-packages-by-location
  65. guix-package-from-file
  66. guix-search-by-name
  67. guix-search-by-regexp
  68. guix-packages-from-system-config-file
  69. "Show profiles"
  70. guix-profiles
  71. "Show profile generations"
  72. guix-generations
  73. guix-last-generations
  74. guix-generations-by-time
  75. guix-system-generations
  76. guix-last-system-generations
  77. guix-system-generations-by-time
  78. "Show/browse package licenses"
  79. guix-licenses
  80. guix-browse-license-url
  81. guix-find-license-definition
  82. "Show/find package locations"
  83. guix-locations
  84. guix-find-location
  85. guix-edit
  86. "Other package related commands"
  87. guix-package-graph
  88. guix-package-size
  89. guix-package-lint
  90. "Show services"
  91. (guix-services-from-system-config-file t nil)
  92. "Magit-like interface"
  93. guix
  94. "Show Hydra builds and jobsets"
  95. guix-hydra-latest-builds
  96. guix-hydra-queued-builds
  97. guix-hydra-jobsets
  98. "Hide hash parts in \"/gnu/store/…-foo\" file names"
  99. (guix-prettify-mode nil t)
  100. global-guix-prettify-mode
  101. "Modes for package build logs and derivations"
  102. (guix-build-log-mode nil t)
  103. (guix-build-log-minor-mode nil t)
  104. (guix-derivation-mode nil nil)
  105. "Modes for Guile files"
  106. (guix-devel-mode nil t)
  107. (guix-scheme-mode nil nil)
  108. "Miscellaneous commands"
  109. ;; `guix-emacs-autoload-packages' is available in Emacs installed
  110. ;; with Guix.
  111. (guix-emacs-autoload-packages t nil)
  112. guix-set-current-profile
  113. guix-pull
  114. guix-hash
  115. guix-apply-manifest
  116. guix-switch-to-buffer
  117. guix-extended-command
  118. guix-info
  119. guix-help
  120. (guix-about t nil)
  121. (guix-version t nil))
  122. "List of command specifications for '\\[guix-help]'.
  123. Each specification can have one of the following forms:
  124. TITLE
  125. COMMAND-NAME
  126. (COMMAND-NAME COMMAND-BUTTON? INFO-BUTTON?)
  127. TITLE is a string.
  128. COMMAND-NAME is a symbol.
  129. COMMAND-BUTTON? is a boolean value; it defines whether
  130. COMMAND-NAME is buttonized or not.
  131. INFO-BUTTON? is a boolean value; it defines whether 'info' button
  132. should be displayed or not.")
  133. (defvar guix-help-mode-map
  134. (let ((map (make-sparse-keymap)))
  135. (set-keymap-parent map (make-composed-keymap button-buffer-map
  136. special-mode-map)))
  137. "Keymap for Emacs-Guix Help and About buffers.")
  138. (define-derived-mode guix-help-mode special-mode "Help"
  139. "Major mode for '\\[guix-about]' and '\\[guix-help]' buffers.
  140. \\{help-mode-map}")
  141. (defun guix-insert-info-button (label info-node)
  142. "Insert button with LABEL to open texinfo manual.
  143. INFO-NODE is the name passed to `info' function."
  144. (bui-insert-button
  145. label 'button
  146. 'action (lambda (button)
  147. (info (button-get button 'node)))
  148. 'node info-node))
  149. (defun guix-insert-info-command-button (label name)
  150. "Insert button with LABEL to open texinfo manual for command NAME."
  151. (bui-insert-button
  152. label 'button
  153. 'help-echo (format "Display info manual for '%S'" name)
  154. 'action (lambda (button)
  155. (guix-goto-command-index-topic
  156. (symbol-name (button-get button 'name))))
  157. 'name name))
  158. (defun guix-insert-doc-button (label symbol)
  159. "Insert button with LABEL to open the docstring of SYMBOL."
  160. (bui-insert-button
  161. label 'button
  162. 'help-echo (format "Display documentation of '%S'" symbol)
  163. 'action (lambda (button)
  164. (describe-symbol (button-get button 'symbol)))
  165. 'symbol symbol))
  166. (defun guix-insert-command-button (command)
  167. "Insert button to run 'M-x COMMAND'."
  168. (let ((command-string (symbol-name command)))
  169. (bui-insert-button
  170. command-string 'button
  171. 'help-echo (format "Call 'M-x %s'" command-string)
  172. 'action (lambda (button)
  173. (call-interactively (button-get button 'command)))
  174. 'command command)))
  175. (declare-function Info-follow-nearest-node "info" t)
  176. (defun guix-goto-index-topic (index-node topic)
  177. "Open TOPIC of INDEX-NODE in the Emacs-Guix manual."
  178. (require 'info)
  179. (info (concat "(emacs-guix)" index-node))
  180. (goto-char (point-min))
  181. (unless (re-search-forward (concat "\\* +" (regexp-quote topic))
  182. nil t)
  183. (user-error "No such index topic: %s" topic))
  184. (Info-follow-nearest-node))
  185. (defun guix-goto-command-index-topic (topic)
  186. "Open TOPIC of Command index in the Emacs-Guix manual."
  187. (guix-goto-index-topic "Command Index" topic))
  188. (defun guix-help-insert-doc-buttons (command &optional info-button?)
  189. "Insert 'doc' button for COMMAND at `guix-help-doc-column'.
  190. If INFO-BUTTON? is non-nil, insert 'info' button as well."
  191. (indent-to guix-help-doc-column 2)
  192. (guix-insert-doc-button "doc" command)
  193. (when info-button?
  194. (insert " ")
  195. (guix-insert-info-command-button "info" command)))
  196. (defun guix-help-insert-keys (command)
  197. "Insert key bindings for COMMAND at `guix-help-key-column'."
  198. (let ((keys (where-is-internal command)))
  199. (when keys
  200. (indent-to guix-help-key-column 2)
  201. (insert "(")
  202. (bui-format-insert (mapcar #'key-description keys)
  203. 'guix-help-key)
  204. (insert ")"))))
  205. (defun guix-help-insert-specification (spec)
  206. "Insert command specification SPEC at point.
  207. See `guix-help-specifications' for the meaning of SPEC."
  208. (pcase spec
  209. ((pred symbolp)
  210. (guix-help-insert-specification (list spec t t)))
  211. ((pred stringp)
  212. (bui-newline)
  213. (bui-format-insert spec 'guix-help-heading)
  214. (bui-newline 2))
  215. (`(,name ,command-button? ,info-button?)
  216. (when (fboundp name)
  217. (bui-with-indent bui-indent
  218. (if command-button?
  219. (guix-insert-command-button name)
  220. (insert (symbol-name name)))
  221. (if (< guix-help-doc-column guix-help-key-column)
  222. (progn
  223. (guix-help-insert-doc-buttons name info-button?)
  224. (guix-help-insert-keys name))
  225. (guix-help-insert-keys name)
  226. (guix-help-insert-doc-buttons name info-button?)))
  227. (bui-newline)))
  228. (_
  229. (insert "<unknown specification>")
  230. (bui-newline))))
  231. (defun guix-help-reinsert-content (content-function)
  232. "Erase the current buffer and call CONTENT-FUNCTION to fill it."
  233. (let ((inhibit-read-only t))
  234. (erase-buffer)
  235. (funcall content-function)))
  236. (defun guix-help-make-revert-function (content-function)
  237. "Return a revert function for `revert-buffer-function'."
  238. (lambda (_ignore-auto noconfirm)
  239. (when (or noconfirm
  240. (y-or-n-p (format "Revert %s buffer? " (buffer-name))))
  241. (guix-help-reinsert-content content-function))))
  242. (defun guix-help-display-buffer (buffer-name content-function)
  243. "Display BUFFER-NAME buffer and call CONTENT-FUNCTION to fill it."
  244. (with-current-buffer (get-buffer-create buffer-name)
  245. (guix-help-mode)
  246. (setq-local revert-buffer-function
  247. (guix-help-make-revert-function content-function))
  248. (guix-help-reinsert-content content-function))
  249. (switch-to-buffer buffer-name))
  250. (defun guix-help-insert-content ()
  251. "Insert summary of Emacs-Guix commands into the current buffer."
  252. (setq header-line-format
  253. " Summary of the available M-x commands")
  254. (mapc #'guix-help-insert-specification
  255. guix-help-specifications)
  256. ;; Remove an extra newline in the beginning of buffer.
  257. (goto-char (point-min))
  258. (delete-char 1))
  259. (defun guix-help-show ()
  260. "Display a summary of the available Emacs-Guix commands.
  261. Unlike `guix-help', this command always recreates
  262. `guix-help-buffer-name' buffer."
  263. (interactive)
  264. (guix-help-display-buffer guix-help-buffer-name
  265. #'guix-help-insert-content))
  266. ;;;###autoload
  267. (defun guix-help ()
  268. "Display a summary of the available Emacs-Guix commands.
  269. Switch to `guix-help-buffer-name' buffer if it already exists."
  270. (interactive)
  271. (guix-switch-to-buffer-or-funcall
  272. guix-help-buffer-name #'guix-help-show))
  273. ;;; Guix buffers
  274. (defcustom guix-define-buffer-function #'guix-define-buffer-by-name
  275. "Function used to define a Guix buffer.
  276. This function is used by `guix-switch-to-buffer'. It is called
  277. with a buffer as a single argument and should return non-nil if
  278. the buffer is the Guix one."
  279. :type '(choice (function-item guix-define-buffer-by-name)
  280. (function-item guix-define-buffer-by-mode)
  281. (function :tag "Other function"))
  282. :group 'guix)
  283. (defun guix-define-buffer-by-name (buffer)
  284. "Return non-nil if BUFFER name matches Guix buffer names."
  285. (string-match-p "\\`*Guix" (buffer-name buffer)))
  286. (defun guix-define-buffer-by-mode (buffer)
  287. "Return non-nil if BUFFER major mode is one of the Guix major modes."
  288. (with-current-buffer buffer
  289. (string-match-p "\\`guix-" (symbol-name major-mode))))
  290. (defun guix-buffer? (buffer)
  291. "Return non-nil if BUFFER is a Guix buffer.
  292. This is a wrapper for `guix-define-buffer-function'."
  293. (funcall guix-define-buffer-function buffer))
  294. (defun guix-buffers ()
  295. "Return all Guix buffers matching `guix-define-buffer-function'."
  296. (-filter #'guix-buffer? (buffer-list)))
  297. ;;;###autoload
  298. (defun guix-switch-to-buffer (buffer)
  299. "Switch to BUFFER.
  300. Interactively, prompt for BUFFER completing only Guix buffer names.
  301. Guix buffers are defined using `guix-define-buffer-function'."
  302. (interactive
  303. (let ((buffers (guix-buffers)))
  304. (if (null buffers)
  305. (user-error "No Guix buffers found")
  306. (list (completing-read "Buffer: "
  307. (mapcar #'buffer-name buffers))))))
  308. (switch-to-buffer buffer))
  309. ;;; Guix commands
  310. (defun guix-extended-command-prompt ()
  311. "Return prompt string for `guix-extended-command'."
  312. ;; Taken from `read-extended-command'.
  313. (concat (cond
  314. ((eq current-prefix-arg '-) "- ")
  315. ((and (consp current-prefix-arg)
  316. (eq (car current-prefix-arg) 4)) "C-u ")
  317. ((and (consp current-prefix-arg)
  318. (integerp (car current-prefix-arg)))
  319. (format "%d " (car current-prefix-arg)))
  320. ((integerp current-prefix-arg)
  321. (format "%d " current-prefix-arg)))
  322. "M-x "))
  323. (defun guix-extended-commands ()
  324. "Return a list of global Guix commands."
  325. (delq nil
  326. (mapcar (lambda (spec)
  327. (cond
  328. ((symbolp spec) spec)
  329. ((listp spec) (car spec))))
  330. guix-help-specifications)))
  331. ;;;###autoload
  332. (defun guix-extended-command (command)
  333. "Run Emacs-Guix COMMAND.
  334. This is like '\\[execute-extended-command]' but only global Guix
  335. commands are completed (commands displayed with '\\[guix-help]')."
  336. (interactive
  337. (list (completing-read (guix-extended-command-prompt)
  338. (guix-extended-commands)
  339. nil t)))
  340. (let ((cmd (if (stringp command)
  341. (intern-soft command)
  342. command)))
  343. (if (commandp cmd)
  344. (call-interactively cmd)
  345. (error "`%S' is not a valid command" cmd))))
  346. (provide 'guix-help)
  347. ;;; guix-help.el ends here