guix-misc.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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-file-name "File with manifest: "))
  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. ;;; Executing guix commands
  159. (defcustom guix-run-in-shell-function #'guix-run-in-shell
  160. "Function used to run guix command.
  161. The function is called with a single argument - a command line string."
  162. :type '(choice (function-item guix-run-in-shell)
  163. (function-item guix-run-in-eshell)
  164. (function :tag "Other function"))
  165. :group 'guix)
  166. (defcustom guix-shell-buffer-name "*shell*"
  167. "Default name of a shell buffer used for running guix commands."
  168. :type 'string
  169. :group 'guix)
  170. (declare-function comint-send-input "comint" t)
  171. (defun guix-run-in-shell (string)
  172. "Run command line STRING in `guix-shell-buffer-name' buffer."
  173. (shell guix-shell-buffer-name)
  174. (goto-char (point-max))
  175. (insert string)
  176. (comint-send-input))
  177. (declare-function eshell-send-input "esh-mode" t)
  178. (defun guix-run-in-eshell (string)
  179. "Run command line STRING in eshell buffer."
  180. (eshell)
  181. (goto-char (point-max))
  182. (insert string)
  183. (eshell-send-input))
  184. (defun guix-run-command-in-shell (args)
  185. "Execute 'guix ARGS ...' command in a shell buffer."
  186. (funcall guix-run-in-shell-function
  187. (guix-command-string args)))
  188. (defun guix-run-command-in-repl (args)
  189. "Execute 'guix ARGS ...' command in Guix REPL."
  190. (guix-eval-in-repl
  191. (apply #'guix-make-guile-expression
  192. 'guix-command args)))
  193. (defun guix-command-output (args)
  194. "Return string with 'guix ARGS ...' output."
  195. (cl-multiple-value-bind (output error)
  196. (guix-eval (apply #'guix-make-guile-expression
  197. 'guix-command-output args))
  198. ;; Remove trailing new space from the error string.
  199. (message (replace-regexp-in-string "\n\\'" "" (read error)))
  200. (read output)))
  201. (defun guix-help-string (&optional commands)
  202. "Return string with 'guix COMMANDS ... --help' output."
  203. (guix-eval-read
  204. (apply #'guix-make-guile-expression
  205. 'help-string commands)))
  206. ;;; Pull
  207. (defcustom guix-update-after-pull t
  208. "If non-nil, update Guix buffers after performing \\[guix-pull]."
  209. :type 'boolean
  210. :group 'guix)
  211. (defvar guix-after-pull-hook
  212. '(guix-restart-repl-after-pull guix-update-buffers-maybe-after-pull)
  213. "Hook run after successful performing `guix-pull' operation.")
  214. (defun guix-restart-repl-after-pull ()
  215. "Restart Guix REPL after `guix-pull' operation."
  216. (guix-repl-exit)
  217. (guix-start-process-maybe
  218. "Restarting Guix REPL after pull operation ..."))
  219. (defun guix-update-buffers-maybe-after-pull ()
  220. "Update buffers depending on `guix-update-after-pull'."
  221. (when guix-update-after-pull
  222. ;; No need to update "generation" buffers.
  223. (dolist (buffer (guix-operation-buffers
  224. '(guix-package-list-mode
  225. guix-package-info-mode
  226. guix-output-list-mode)))
  227. (with-current-buffer buffer
  228. (revert-buffer nil t)))
  229. (message "Guix buffers have been updated.")))
  230. ;;;###autoload
  231. (defun guix-pull (&optional verbose)
  232. "Run Guix pull operation.
  233. If VERBOSE is non-nil (with prefix argument), produce verbose output."
  234. (interactive "P")
  235. (let ((args (and verbose '("--verbose"))))
  236. (guix-eval-in-repl
  237. (apply #'guix-make-guile-expression
  238. 'guix-command "pull" args)
  239. nil 'pull)))
  240. (provide 'guix-misc)
  241. ;;; guix-misc.el ends here