guix-help.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. ;;; guix-help.el --- Help commands -*- lexical-binding: t -*-
  2. ;; Copyright © 2016–2019, 2021 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. '("Popup interface for the rest commands"
  55. guix
  56. "Show packages and their definitions"
  57. guix-all-packages
  58. guix-installed-user-packages
  59. guix-installed-system-packages
  60. guix-installed-packages
  61. guix-obsolete-packages
  62. guix-superseded-packages
  63. guix-hidden-packages
  64. guix-dependent-packages
  65. guix-packages-by-name
  66. guix-packages-by-regexp
  67. guix-packages-by-name-regexp
  68. guix-packages-by-license
  69. guix-packages-by-location
  70. guix-package-from-file
  71. guix-packages-from-system-config-file
  72. nil
  73. guix-package-locations
  74. guix-find-package-location-file
  75. guix-find-package-definition
  76. "Show profiles and profile generations"
  77. guix-profiles
  78. nil
  79. guix-current-profile
  80. guix-generations
  81. guix-last-generations
  82. guix-generations-by-time
  83. nil
  84. guix-system-profile
  85. guix-system-generations
  86. guix-last-system-generations
  87. guix-system-generations-by-time
  88. "Show services and their definitions"
  89. guix-all-services
  90. guix-default-services
  91. guix-services-by-name
  92. guix-services-by-regexp
  93. guix-services-by-location
  94. guix-services-from-system-config-file
  95. nil
  96. guix-service-locations
  97. guix-find-service-location-file
  98. guix-find-service-definition
  99. "Show store items"
  100. guix-store-item
  101. guix-store-item-referrers
  102. guix-store-item-references
  103. guix-store-item-requisites
  104. guix-store-item-derivers
  105. guix-store-failures
  106. guix-store-live-items
  107. guix-store-dead-items
  108. "Interface for 'operating-system'"
  109. guix-system-from-file
  110. "Show/browse package licenses"
  111. guix-licenses
  112. guix-browse-license-url
  113. guix-find-license-location-file
  114. guix-find-license-definition
  115. "Other package related commands"
  116. guix-number-of-packages
  117. guix-package-graph
  118. guix-package-size
  119. guix-package-lint
  120. guix-lint-checkers
  121. "Hide hash parts in \"/gnu/store/…-foo\" file names"
  122. (guix-prettify-mode nil t)
  123. global-guix-prettify-mode
  124. "Modes for package build logs and derivations"
  125. (guix-build-log-mode nil t)
  126. (guix-build-log-minor-mode nil t)
  127. (guix-derivation-mode nil nil)
  128. "Modes for Guile files"
  129. (guix-devel-mode nil t)
  130. (guix-scheme-mode nil nil)
  131. "Miscellaneous commands"
  132. ;; `guix-emacs-autoload-packages' is available in Emacs installed
  133. ;; with Guix.
  134. (guix-emacs-autoload-packages t nil)
  135. guix-set-emacs-environment
  136. guix-set-current-profile
  137. guix-pull
  138. guix-hash
  139. guix-apply-manifest
  140. guix-switch-to-buffer
  141. guix-extended-command
  142. guix-info
  143. guix-help
  144. (guix-about t nil)
  145. (guix-version t nil)
  146. (guix-report-bug t nil))
  147. "List of command specifications for '\\[guix-help]'.
  148. Each specification can have one of the following forms:
  149. TITLE
  150. COMMAND-NAME
  151. (COMMAND-NAME COMMAND-BUTTON? INFO-BUTTON?)
  152. TITLE is a string.
  153. COMMAND-NAME is a symbol.
  154. COMMAND-BUTTON? is a boolean value; it defines whether
  155. COMMAND-NAME is buttonized or not.
  156. INFO-BUTTON? is a boolean value; it defines whether 'info' button
  157. should be displayed or not.")
  158. (defvar guix-help-mode-map
  159. (let ((map (make-sparse-keymap)))
  160. (set-keymap-parent map (make-composed-keymap button-buffer-map
  161. special-mode-map)))
  162. "Keymap for Emacs-Guix Help and About buffers.")
  163. (define-derived-mode guix-help-mode special-mode "Help"
  164. "Major mode for '\\[guix-about]' and '\\[guix-help]' buffers.
  165. \\{help-mode-map}")
  166. (defun guix-insert-info-button (label info-node)
  167. "Insert button with LABEL to open texinfo manual.
  168. INFO-NODE is the name passed to `info' function."
  169. (bui-insert-button
  170. label 'button
  171. 'action (lambda (button)
  172. (info (button-get button 'node)))
  173. 'node info-node))
  174. (defun guix-insert-info-command-button (label name)
  175. "Insert button with LABEL to open texinfo manual for command NAME."
  176. (bui-insert-button
  177. label 'button
  178. 'help-echo (format "Display info manual for '%S'" name)
  179. 'action (lambda (button)
  180. (guix-goto-command-index-topic
  181. (symbol-name (button-get button 'name))))
  182. 'name name))
  183. (defun guix-insert-doc-button (label symbol)
  184. "Insert button with LABEL to open the docstring of SYMBOL."
  185. (bui-insert-button
  186. label 'button
  187. 'help-echo (format "Display documentation of '%S'" symbol)
  188. 'action (lambda (button)
  189. (describe-symbol (button-get button 'symbol)))
  190. 'symbol symbol))
  191. (defun guix-insert-command-button (command)
  192. "Insert button to run 'M-x COMMAND'."
  193. (let ((command-string (symbol-name command)))
  194. (bui-insert-button
  195. command-string 'button
  196. 'help-echo (format "Call 'M-x %s'" command-string)
  197. 'action (lambda (button)
  198. (call-interactively (button-get button 'command)))
  199. 'command command)))
  200. (declare-function Info-follow-nearest-node "info" t)
  201. (defun guix-goto-index-topic (index-node topic)
  202. "Open TOPIC of INDEX-NODE in the Emacs-Guix manual."
  203. (require 'info)
  204. (info (concat "(emacs-guix)" index-node))
  205. (goto-char (point-min))
  206. (unless (re-search-forward (concat "\\* +" (regexp-quote topic))
  207. nil t)
  208. (user-error "No such index topic: %s" topic))
  209. (Info-follow-nearest-node))
  210. (defun guix-goto-command-index-topic (topic)
  211. "Open TOPIC of Command index in the Emacs-Guix manual."
  212. (guix-goto-index-topic "Command Index" topic))
  213. (defun guix-help-insert-doc-buttons (command &optional info-button?)
  214. "Insert 'doc' button for COMMAND at `guix-help-doc-column'.
  215. If INFO-BUTTON? is non-nil, insert 'info' button as well."
  216. (indent-to guix-help-doc-column 2)
  217. (guix-insert-doc-button "doc" command)
  218. (when info-button?
  219. (insert " ")
  220. (guix-insert-info-command-button "info" command)))
  221. (defun guix-help-insert-keys (command)
  222. "Insert key bindings for COMMAND at `guix-help-key-column'."
  223. (let ((keys (where-is-internal command)))
  224. (when keys
  225. (indent-to guix-help-key-column 2)
  226. (insert "(")
  227. (bui-format-insert (mapcar #'key-description keys)
  228. 'guix-help-key)
  229. (insert ")"))))
  230. (defun guix-help-insert-specification (spec)
  231. "Insert command specification SPEC at point.
  232. See `guix-help-specifications' for the meaning of SPEC."
  233. (pcase spec
  234. ((pred null)
  235. (bui-newline))
  236. ((pred symbolp)
  237. (guix-help-insert-specification (list spec t t)))
  238. ((pred stringp)
  239. (bui-newline)
  240. (bui-format-insert spec 'guix-help-heading)
  241. (bui-newline 2))
  242. (`(,name ,command-button? ,info-button?)
  243. (when (fboundp name)
  244. (bui-with-indent bui-indent
  245. (if command-button?
  246. (guix-insert-command-button name)
  247. (insert (symbol-name name)))
  248. (if (< guix-help-doc-column guix-help-key-column)
  249. (progn
  250. (guix-help-insert-doc-buttons name info-button?)
  251. (guix-help-insert-keys name))
  252. (guix-help-insert-keys name)
  253. (guix-help-insert-doc-buttons name info-button?)))
  254. (bui-newline)))
  255. (_
  256. (insert "<unknown specification>")
  257. (bui-newline))))
  258. (defun guix-help-reinsert-content (content-function)
  259. "Erase the current buffer and call CONTENT-FUNCTION to fill it."
  260. (let ((inhibit-read-only t))
  261. (erase-buffer)
  262. (funcall content-function)))
  263. (defun guix-help-make-revert-function (content-function)
  264. "Return a revert function for `revert-buffer-function'."
  265. (lambda (_ignore-auto noconfirm)
  266. (when (or noconfirm
  267. (y-or-n-p (format "Revert %s buffer? " (buffer-name))))
  268. (guix-help-reinsert-content content-function))))
  269. (defun guix-help-display-buffer (buffer-name content-function)
  270. "Display BUFFER-NAME buffer and call CONTENT-FUNCTION to fill it."
  271. (with-current-buffer (get-buffer-create buffer-name)
  272. (guix-help-mode)
  273. (setq-local revert-buffer-function
  274. (guix-help-make-revert-function content-function))
  275. (guix-help-reinsert-content content-function))
  276. (switch-to-buffer buffer-name))
  277. (defun guix-help-insert-content ()
  278. "Insert summary of Emacs-Guix commands into the current buffer."
  279. (setq header-line-format
  280. '(" Summary of the available "
  281. (:propertize "M-x" face guix-help-key)
  282. " commands"))
  283. (mapc #'guix-help-insert-specification
  284. guix-help-specifications)
  285. ;; Remove an extra newline in the beginning of buffer.
  286. (goto-char (point-min))
  287. (delete-char 1))
  288. (defun guix-help-show ()
  289. "Display a summary of the available Emacs-Guix commands.
  290. Unlike `guix-help', this command always recreates
  291. `guix-help-buffer-name' buffer."
  292. (interactive)
  293. (guix-help-display-buffer guix-help-buffer-name
  294. #'guix-help-insert-content))
  295. ;;;###autoload
  296. (defun guix-help ()
  297. "Display a summary of the available Emacs-Guix commands.
  298. Switch to `guix-help-buffer-name' buffer if it already exists."
  299. (interactive)
  300. (guix-switch-to-buffer-or-funcall
  301. guix-help-buffer-name #'guix-help-show))
  302. ;;; Guix buffers
  303. (defcustom guix-define-buffer-function #'guix-define-buffer-by-name
  304. "Function used to define a Guix buffer.
  305. This function is used by `guix-switch-to-buffer'. It is called
  306. with a buffer as a single argument and should return non-nil if
  307. the buffer is the Guix one."
  308. :type '(choice (function-item guix-define-buffer-by-name)
  309. (function-item guix-define-buffer-by-mode)
  310. (function :tag "Other function"))
  311. :group 'guix)
  312. (defun guix-define-buffer-by-name (buffer)
  313. "Return non-nil if BUFFER name matches Guix buffer names."
  314. (string-match-p "\\`*Guix" (buffer-name buffer)))
  315. (defun guix-define-buffer-by-mode (buffer)
  316. "Return non-nil if BUFFER major mode is one of the Guix major modes."
  317. (with-current-buffer buffer
  318. (string-match-p "\\`guix-" (symbol-name major-mode))))
  319. (defun guix-buffer? (buffer)
  320. "Return non-nil if BUFFER is a Guix buffer.
  321. This is a wrapper for `guix-define-buffer-function'."
  322. (funcall guix-define-buffer-function buffer))
  323. (defun guix-buffers ()
  324. "Return all Guix buffers matching `guix-define-buffer-function'."
  325. (-filter #'guix-buffer? (buffer-list)))
  326. ;;;###autoload
  327. (defun guix-switch-to-buffer (buffer)
  328. "Switch to BUFFER.
  329. Interactively, prompt for BUFFER completing only Guix buffer names.
  330. Guix buffers are defined using `guix-define-buffer-function'."
  331. (interactive
  332. (let ((buffers (guix-buffers)))
  333. (if (null buffers)
  334. (user-error "No Guix buffers found")
  335. (list (completing-read "Buffer: "
  336. (mapcar #'buffer-name buffers))))))
  337. (switch-to-buffer buffer))
  338. ;;; Guix commands
  339. (defun guix-extended-command-prompt ()
  340. "Return prompt string for `guix-extended-command'."
  341. ;; Taken from `read-extended-command'.
  342. (concat (cond
  343. ((eq current-prefix-arg '-) "- ")
  344. ((and (consp current-prefix-arg)
  345. (eq (car current-prefix-arg) 4)) "C-u ")
  346. ((and (consp current-prefix-arg)
  347. (integerp (car current-prefix-arg)))
  348. (format "%d " (car current-prefix-arg)))
  349. ((integerp current-prefix-arg)
  350. (format "%d " current-prefix-arg)))
  351. "M-x "))
  352. (defun guix-extended-commands ()
  353. "Return a list of global Guix commands."
  354. (delq nil
  355. (mapcar (lambda (spec)
  356. (cond
  357. ((symbolp spec) spec)
  358. ((listp spec) (car spec))))
  359. guix-help-specifications)))
  360. ;;;###autoload
  361. (defun guix-extended-command (command)
  362. "Run Emacs-Guix COMMAND.
  363. This is like '\\[execute-extended-command]' but only global Guix
  364. commands are completed (commands displayed with '\\[guix-help]')."
  365. (interactive
  366. (list (completing-read (guix-extended-command-prompt)
  367. (guix-extended-commands)
  368. nil t)))
  369. (let ((cmd (if (stringp command)
  370. (intern-soft command)
  371. command)))
  372. (if (commandp cmd)
  373. (call-interactively cmd)
  374. (error "`%S' is not a valid command" cmd))))
  375. (provide 'guix-help)
  376. ;;; guix-help.el ends here