guix-help.el 13 KB

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