guix-help.el 13 KB

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