guix-read.el 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. ;;; guix-read.el --- Minibuffer readers
  2. ;; Copyright © 2015–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 functions to prompt a user for packages, system
  18. ;; types, hash formats and other guix related stuff.
  19. ;;; Code:
  20. (require 'guix-help-vars)
  21. (require 'guix-utils)
  22. (require 'guix-repl)
  23. (require 'guix nil t)
  24. ;;; Receivable lists of packages, lint checkers, etc.
  25. (defcustom guix-package-names-use-duplicates t
  26. "Whether a list of package names is allowed to have duplicates or not.
  27. These names are used as completions by several commands (for
  28. example, by '\\[guix-packages-by-name]').
  29. In short: `t' is fast but you'll see unwanted duplicates of
  30. package names; `nil' is slow (only the first time) but there will
  31. be no duplicates. Read further for details.
  32. To get a list of package names, Emacs-Guix walks through all of
  33. the Guix packages. And since Guix may have multiple packages
  34. with the same name (like `gcc', `linux-libre', etc.), the default
  35. list of all names contains duplicates (like 6 times of `gcc'
  36. name). Removing these duplicates from the list is a time
  37. consuming operation, so it is not enabled by default, but you can
  38. enable it by setting this variable to `nil'.
  39. Note that Emacs-Guix caches the list of names when it is used the
  40. first time, so the potential slowness will happen only during the
  41. first call. For the same reason (because of cashing) the new
  42. value of this variable will take effect only after you restart
  43. Emacs."
  44. :type 'boolean
  45. :group 'guix)
  46. (guix-memoized-defun guix-system-types ()
  47. "Return a list of supported systems."
  48. (guix-eval-read "%supported-systems"))
  49. (guix-memoized-defun guix-graph-backend-names ()
  50. "Return a list of names of available graph backends."
  51. (guix-eval-read "(graph-backend-names)"))
  52. (guix-memoized-defun guix-graph-node-type-names ()
  53. "Return a list of names of available graph node types."
  54. (guix-eval-read "(graph-node-type-names)"))
  55. (guix-memoized-defun guix-refresh-updater-names ()
  56. "Return a list of names of available refresh updater types."
  57. (guix-eval-read "(refresh-updater-names)"))
  58. (guix-memoized-defun guix-lint-checker-names ()
  59. "Return a list of names of available lint checkers."
  60. (guix-eval-read "(lint-checker-names)"))
  61. (guix-memoized-defun guix-compressor-names ()
  62. "Return a list of names of available compressors."
  63. (guix-eval-read "(compressor-names)"))
  64. (guix-memoized-defun guix-pack-format-names ()
  65. "Return a list of names of available pack formats."
  66. (guix-eval-read "(pack-format-names)"))
  67. (guix-memoized-defun guix-package-names ()
  68. "Return a list of names of available packages.
  69. See also `guix-package-names-use-duplicates' variable."
  70. (let ((names (guix-eval-read "(package-names*)")))
  71. (sort (if guix-package-names-use-duplicates
  72. names
  73. (cl-delete-duplicates names :test #'string=))
  74. #'string<)))
  75. (guix-memoized-defun guix-license-names ()
  76. "Return a list of names of available licenses."
  77. (guix-eval-read "(license-names)"))
  78. (guix-memoized-defun guix-package-location-files ()
  79. "Return a list of available package locations."
  80. (sort (guix-eval-read "(package-location-files)")
  81. #'string<))
  82. (guix-memoized-defun guix-service-names ()
  83. "Return a list of names of available services."
  84. (sort (guix-eval-read "(service-names*)")
  85. #'string<))
  86. (guix-memoized-defun guix-service-location-files ()
  87. "Return a list of available service locations."
  88. (sort (guix-eval-read "(service-location-files)")
  89. #'string<))
  90. ;;; Readers
  91. (defcustom guix-read-package-name-function #'guix-read-package-name-default
  92. "Function used to read a package name from minibuffer.
  93. The function is called with 2 strings as arguments: a prompt and
  94. initial-contents."
  95. :type '(choice (function-item guix-read-package-name-default)
  96. (function-item guix-read-package-name-at-point)
  97. (function :tag "Other function"))
  98. :group 'guix)
  99. (defun guix-read-package-name-at-point (&optional prompt initial-contents)
  100. "Read symbol at point and if it is a package name, return it.
  101. If it is not a package name or if current command has a prefix
  102. argument, read the name from minibuffer."
  103. (if current-prefix-arg
  104. (guix-read-package-name-default prompt initial-contents)
  105. (let* ((at-point (thing-at-point 'symbol))
  106. (name (and at-point
  107. (substring-no-properties at-point))))
  108. (if (and name
  109. (member name (guix-package-names)))
  110. name
  111. (guix-read-package-name-default prompt
  112. initial-contents)))))
  113. (defun guix-read-package-name (&optional prompt initial-contents)
  114. "Read a package name using `guix-read-package-name-function'."
  115. (funcall guix-read-package-name-function
  116. prompt initial-contents))
  117. (guix-define-readers
  118. :completions-getter guix-system-types
  119. :single-reader guix-read-system-type
  120. :single-prompt "System type: ")
  121. (guix-define-readers
  122. :completions-var guix-help-source-types
  123. :single-reader guix-read-source-type
  124. :single-prompt "Source type: ")
  125. (guix-define-readers
  126. :completions-var guix-help-hash-formats
  127. :default guix-default-hash-format
  128. :single-reader guix-read-hash-format
  129. :single-prompt "Hash format: ")
  130. (guix-define-readers
  131. :completions-var guix-help-refresh-subsets
  132. :single-reader guix-read-refresh-subset
  133. :single-prompt "Refresh subset: ")
  134. (guix-define-readers
  135. :completions-getter guix-refresh-updater-names
  136. :multiple-reader guix-read-refresh-updater-names
  137. :multiple-prompt "Refresh updater,s: "
  138. :multiple-separator ",")
  139. (guix-define-readers
  140. :completions-var guix-help-key-policies
  141. :default guix-default-key-policy
  142. :single-reader guix-read-key-policy
  143. :single-prompt "Key policy: ")
  144. (guix-define-readers
  145. :completions-var guix-help-elpa-archives
  146. :default guix-default-elpa-archive
  147. :single-reader guix-read-elpa-archive
  148. :single-prompt "ELPA archive: ")
  149. (guix-define-readers
  150. :completions-var guix-help-verify-options
  151. :multiple-reader guix-read-verify-options
  152. :multiple-prompt "Verify option,s: "
  153. :multiple-separator ",")
  154. (guix-define-readers
  155. :completions-var guix-help-size-sort-keys
  156. :default guix-default-size-sort-key
  157. :single-reader guix-read-size-sort-key
  158. :single-prompt "Sort key: ")
  159. (guix-define-readers
  160. :completions-var guix-help-search-paths-types
  161. :default guix-default-search-paths-type
  162. :single-reader guix-read-search-paths-type
  163. :single-prompt "Search paths type: ")
  164. (guix-define-readers
  165. :completions-var guix-help-repl-types
  166. :default guix-default-repl-type
  167. :single-reader guix-read-repl-type
  168. :single-prompt "REPL type: ")
  169. (guix-define-readers
  170. :completions-var guix-help-describe-formats
  171. :default guix-default-describe-format
  172. :single-reader guix-read-describe-format
  173. :single-prompt "'guix describe' format: ")
  174. (guix-define-readers
  175. :completions-var guix-help-on-error-strategies
  176. :default guix-default-on-error-strategy
  177. :single-reader guix-read-on-error-strategy
  178. :single-prompt "On error strategy: ")
  179. (guix-define-readers
  180. :completions-getter guix-graph-backend-names
  181. :default guix-default-graph-backend
  182. :single-reader guix-read-graph-backend
  183. :single-prompt "Graph backend: ")
  184. (guix-define-readers
  185. :completions-getter guix-graph-node-type-names
  186. :default guix-default-graph-node-type
  187. :single-reader guix-read-graph-node-type
  188. :single-prompt "Graph node type: ")
  189. (guix-define-readers
  190. :completions-getter guix-lint-checker-names
  191. :multiple-reader guix-read-lint-checker-names
  192. :multiple-prompt "Linter,s: "
  193. :multiple-separator ",")
  194. (guix-define-readers
  195. :completions-getter guix-compressor-names
  196. :single-reader guix-read-compressor-name
  197. :single-prompt "Compressor: ")
  198. (guix-define-readers
  199. :completions-getter guix-pack-format-names
  200. :single-reader guix-read-pack-format-name
  201. :single-prompt "Pack format: ")
  202. (guix-define-readers
  203. :completions-getter guix-package-names
  204. :require-match nil
  205. :single-reader guix-read-package-name-default
  206. :single-prompt "Package: "
  207. :multiple-reader guix-read-package-names
  208. :multiple-prompt "Package,s: "
  209. :multiple-separator " ")
  210. (guix-define-readers
  211. :completions-getter guix-license-names
  212. :single-reader guix-read-license-name
  213. :single-prompt "License: ")
  214. (guix-define-readers
  215. :completions-getter guix-package-location-files
  216. :single-reader guix-read-package-location-file
  217. :single-prompt "Package location file: ")
  218. (guix-define-readers
  219. :completions-getter guix-service-names
  220. :require-match nil
  221. :single-reader guix-read-service-name
  222. :single-prompt "Service: ")
  223. (guix-define-readers
  224. :completions-getter guix-service-location-files
  225. :single-reader guix-read-service-location-file
  226. :single-prompt "Service location file: ")
  227. (provide 'guix-read)
  228. ;;; guix-read.el ends here