guix-misc.el 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. ;;; guix-misc.el --- Miscellaneous definitions -*- lexical-binding: t -*-
  2. ;; Copyright © 2014–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 some miscellaneous code that does not find its
  18. ;; home in any other file (in a perfect world this file wouldn't exist).
  19. ;;; Code:
  20. (require 'cl-lib)
  21. (require 'guix nil t)
  22. (require 'guix-repl)
  23. (require 'guix-guile)
  24. (require 'guix-read)
  25. (require 'guix-utils)
  26. (require 'guix-ui)
  27. (require 'guix-profiles)
  28. ;;; Actions on packages and generations
  29. (defface guix-operation-option-key
  30. '((t :inherit font-lock-warning-face))
  31. "Face used for the keys of operation options."
  32. :group 'guix-faces)
  33. (defcustom guix-operation-confirm t
  34. "If nil, do not prompt to confirm an operation."
  35. :type 'boolean
  36. :group 'guix)
  37. (defcustom guix-use-substitutes t
  38. "If non-nil, use substitutes for the Guix packages."
  39. :type 'boolean
  40. :group 'guix)
  41. (defvar guix-dry-run nil
  42. "If non-nil, do not perform the real actions, just simulate.")
  43. (defvar guix-temp-buffer-name " *Guix temp*"
  44. "Name of a buffer used for displaying info before executing operation.")
  45. (defvar guix-operation-option-true-string "yes"
  46. "String displayed in the mode-line when operation option is t.")
  47. (defvar guix-operation-option-false-string "no "
  48. "String displayed in the mode-line when operation option is nil.")
  49. (defvar guix-operation-option-separator " | "
  50. "String used in the mode-line to separate operation options.")
  51. (defvar guix-operation-options
  52. '((?s "substitutes" guix-use-substitutes)
  53. (?d "dry-run" guix-dry-run))
  54. "List of available operation options.
  55. Each element of the list has a form:
  56. (KEY NAME VARIABLE)
  57. KEY is a character that may be pressed during confirmation to
  58. toggle the option.
  59. NAME is a string displayed in the mode-line.
  60. VARIABLE is a name of an option variable.")
  61. (defun guix-operation-option-by-key (key)
  62. "Return operation option by KEY (character)."
  63. (assq key guix-operation-options))
  64. (defun guix-operation-option-key (option)
  65. "Return key (character) of the operation OPTION."
  66. (car option))
  67. (defun guix-operation-option-name (option)
  68. "Return name of the operation OPTION."
  69. (nth 1 option))
  70. (defun guix-operation-option-variable (option)
  71. "Return name of the variable of the operation OPTION."
  72. (nth 2 option))
  73. (defun guix-operation-option-value (option)
  74. "Return boolean value of the operation OPTION."
  75. (symbol-value (guix-operation-option-variable option)))
  76. (defun guix-operation-option-string-value (option)
  77. "Convert boolean value of the operation OPTION to string and return it."
  78. (if (guix-operation-option-value option)
  79. guix-operation-option-true-string
  80. guix-operation-option-false-string))
  81. (defun guix-operation-prompt (&optional prompt)
  82. "Prompt a user for continuing the current operation.
  83. Return non-nil, if the operation should be continued; nil otherwise.
  84. Ask a user with PROMPT for continuing an operation."
  85. (let* ((option-keys (mapcar #'guix-operation-option-key
  86. guix-operation-options))
  87. (keys (append '(?y ?n) option-keys))
  88. (prompt (concat (propertize (or prompt "Continue operation?")
  89. 'face 'minibuffer-prompt)
  90. " ("
  91. (mapconcat
  92. (lambda (key)
  93. (propertize (string key)
  94. 'face 'guix-operation-option-key))
  95. keys
  96. ", ")
  97. ") ")))
  98. (let ((mode-line mode-line-format))
  99. (prog1 (guix-operation-prompt-1 prompt keys)
  100. (setq mode-line-format mode-line)
  101. ;; Clear the minibuffer after prompting.
  102. (message "")))))
  103. (defun guix-operation-prompt-1 (prompt keys)
  104. "This function is internal for `guix-operation-prompt'."
  105. (guix-operation-set-mode-line)
  106. (let ((key (read-char-choice prompt (cons ?\C-g keys) t)))
  107. (cl-case key
  108. (?y t)
  109. ((?n ?\C-g) nil)
  110. (t (let* ((option (guix-operation-option-by-key key))
  111. (var (guix-operation-option-variable option)))
  112. (set var (not (symbol-value var)))
  113. (guix-operation-prompt-1 prompt keys))))))
  114. (defun guix-operation-set-mode-line ()
  115. "Display operation options in the mode-line of the current buffer."
  116. (setq mode-line-format
  117. (concat (propertize " Options: "
  118. 'face 'mode-line-buffer-id)
  119. (mapconcat
  120. (lambda (option)
  121. (let ((key (guix-operation-option-key option))
  122. (name (guix-operation-option-name option))
  123. (val (guix-operation-option-string-value option)))
  124. (concat name
  125. " ("
  126. (propertize (string key)
  127. 'face 'guix-operation-option-key)
  128. "): " val)))
  129. guix-operation-options
  130. guix-operation-option-separator)))
  131. (force-mode-line-update))
  132. ;;;###autoload
  133. (defun guix-apply-manifest (profile file &optional operation-buffer)
  134. "Apply manifest from FILE to PROFILE.
  135. This function has the same meaning as 'guix package --manifest' command.
  136. See Info node `(guix) Invoking guix package' for details.
  137. Interactively, use the current profile and prompt for manifest
  138. FILE. With a prefix argument, also prompt for PROFILE."
  139. (interactive
  140. (let* ((current-profile (guix-ui-current-profile))
  141. (profile (if current-prefix-arg
  142. (guix-read-package-profile)
  143. (or current-profile guix-current-profile)))
  144. (file (guix-read-manifest-file-name))
  145. (buffer (and current-profile (current-buffer))))
  146. (list profile file buffer)))
  147. (guix-assert-non-system-profile profile)
  148. (when (or (not guix-operation-confirm)
  149. (y-or-n-p (format "Apply manifest from '%s' to profile '%s'? "
  150. file profile)))
  151. (guix-eval-in-repl
  152. (guix-make-guile-expression
  153. 'guix-command
  154. "package"
  155. (concat "--profile=" (expand-file-name profile))
  156. (concat "--manifest=" (expand-file-name file)))
  157. operation-buffer)))
  158. (defcustom guix-search-paths-buffer-name "*Guix Search Paths*"
  159. "Name of a buffer for displaying 'search paths' environment variables."
  160. :type 'string
  161. :group 'guix)
  162. (defun guix-show-search-paths (profiles &optional type)
  163. "Display 'search paths' environment variables for PROFILES."
  164. (let* ((profiles (mapcar #'guix-package-profile profiles))
  165. (type (or type "exact"))
  166. (type-symbol (intern type))
  167. (paths (guix-eval-read
  168. (guix-make-guile-expression
  169. 'search-paths profiles :type type-symbol))))
  170. (with-current-buffer (get-buffer-create guix-search-paths-buffer-name)
  171. (let ((inhibit-read-only t))
  172. (erase-buffer)
  173. (insert
  174. "# \"Search paths\" environment variables for the Guix "
  175. (if (cdr profiles) "profiles" "profile")
  176. ".
  177. #
  178. # Shell command to reproduce:
  179. #
  180. # guix package --search-paths=" type " "
  181. (mapconcat (lambda (p)
  182. (concat "--profile=" (shell-quote-argument p)))
  183. profiles
  184. " ")
  185. "\n\n"
  186. (mapconcat #'identity paths "\n")
  187. "\n"))
  188. (sh-mode))
  189. (guix-display-buffer guix-search-paths-buffer-name)))
  190. ;;; Executing guix commands
  191. (defcustom guix-run-in-shell-function #'guix-run-in-shell
  192. "Function used to run guix command.
  193. The function is called with a single argument - a command line string."
  194. :type '(choice (function-item guix-run-in-shell)
  195. (function-item guix-run-in-eshell)
  196. (function :tag "Other function"))
  197. :group 'guix)
  198. (defcustom guix-shell-buffer-name "*shell*"
  199. "Default name of a shell buffer used for running guix commands."
  200. :type 'string
  201. :group 'guix)
  202. (declare-function comint-send-input "comint" t)
  203. (defun guix-run-in-shell (string)
  204. "Run command line STRING in `guix-shell-buffer-name' buffer."
  205. (shell guix-shell-buffer-name)
  206. (goto-char (point-max))
  207. (insert string)
  208. (comint-send-input))
  209. (declare-function eshell-send-input "esh-mode" t)
  210. (defun guix-run-in-eshell (string)
  211. "Run command line STRING in eshell buffer."
  212. (eshell)
  213. (goto-char (point-max))
  214. (insert string)
  215. (eshell-send-input))
  216. (defun guix-run-command-in-shell (args)
  217. "Execute 'guix ARGS ...' command in a shell buffer."
  218. (funcall guix-run-in-shell-function
  219. (guix-command-string args)))
  220. (defun guix-run-command-in-repl (args)
  221. "Execute 'guix ARGS ...' command in Guix REPL."
  222. (guix-eval-in-repl
  223. (apply #'guix-make-guile-expression
  224. 'guix-command args)))
  225. (defun guix-command-output (args)
  226. "Return string with 'guix ARGS ...' output."
  227. (cl-multiple-value-bind (output error)
  228. (guix-eval (apply #'guix-make-guile-expression
  229. 'guix-command-output args))
  230. ;; Remove trailing new space from the error string.
  231. (message (replace-regexp-in-string "\n\\'" "" (read error)))
  232. (read output)))
  233. (defun guix-help-string (&optional commands)
  234. "Return string with 'guix COMMANDS ... --help' output."
  235. (guix-eval-read
  236. (apply #'guix-make-guile-expression
  237. 'help-string commands)))
  238. ;;; Pull
  239. (defcustom guix-update-after-pull t
  240. "If non-nil, update Guix buffers after performing \\[guix-pull]."
  241. :type 'boolean
  242. :group 'guix)
  243. (defvar guix-after-pull-hook
  244. '(guix-restart-repl-after-pull guix-update-buffers-maybe-after-pull)
  245. "Hook run after successful performing `guix-pull' operation.")
  246. (defun guix-restart-repl-after-pull ()
  247. "Restart Guix REPL after `guix-pull' operation."
  248. (guix-repl-exit)
  249. (guix-start-process-maybe
  250. "Restarting Guix REPL after pull operation ..."))
  251. (defun guix-update-buffers-maybe-after-pull ()
  252. "Update buffers depending on `guix-update-after-pull'."
  253. (when guix-update-after-pull
  254. ;; No need to update "generation" buffers.
  255. (dolist (buffer (guix-operation-buffers
  256. '(guix-package-list-mode
  257. guix-package-info-mode
  258. guix-output-list-mode)))
  259. (with-current-buffer buffer
  260. (revert-buffer nil t)))
  261. (message "Guix buffers have been updated.")))
  262. ;;;###autoload
  263. (defun guix-pull (&optional verbose)
  264. "Run Guix pull operation.
  265. If VERBOSE is non-nil (with prefix argument), produce verbose output."
  266. (interactive "P")
  267. (let ((args (and verbose '("--verbose"))))
  268. (guix-eval-in-repl
  269. (apply #'guix-make-guile-expression
  270. 'guix-command "pull" args)
  271. nil 'pull)))
  272. (provide 'guix-misc)
  273. ;;; guix-misc.el ends here