guix-devel.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. ;;; guix-devel.el --- Development tools -*- lexical-binding: t -*-
  2. ;; Copyright © 2015–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 `guix-devel-mode' (minor mode for `scheme-mode'
  18. ;; buffers) that provides highlighting and indentation rules for Guix
  19. ;; Guile code, as well as some tools to work with Guix (or even an
  20. ;; arbitrary Guile code) with Geiser.
  21. ;;; Code:
  22. (require 'lisp-mode)
  23. (require 'bui-utils)
  24. (require 'guix nil t)
  25. (require 'guix-utils)
  26. (require 'guix-guile)
  27. (require 'guix-geiser)
  28. (require 'guix-misc)
  29. (defgroup guix-devel nil
  30. "Settings for Guix development utils."
  31. :group 'guix)
  32. (defgroup guix-devel-faces nil
  33. "Faces for `guix-devel-mode'."
  34. :group 'guix-devel
  35. :group 'guix-faces)
  36. (defface guix-devel-modify-phases-keyword
  37. '((t :inherit font-lock-preprocessor-face))
  38. "Face for a `modify-phases' keyword ('delete', 'replace', etc.)."
  39. :group 'guix-devel-faces)
  40. (defface guix-devel-gexp-symbol
  41. '((t :inherit font-lock-keyword-face))
  42. "Face for gexp symbols ('#~', '#$', etc.).
  43. See Info node `(guix) G-Expressions'."
  44. :group 'guix-devel-faces)
  45. (defun guix-devel-use-modules (&rest modules)
  46. "Use guile MODULES."
  47. (apply #'guix-geiser-call "use-modules" modules))
  48. (defun guix-devel-use-module (&optional module)
  49. "Use guile MODULE in the current Geiser REPL.
  50. MODULE is a string with the module name - e.g., \"(ice-9 match)\".
  51. Interactively, use the module defined by the current scheme file."
  52. (interactive (list (guix-guile-current-module)))
  53. (guix-devel-use-modules module)
  54. (message "Using %s module." module))
  55. (defun guix-devel-copy-module-as-kill ()
  56. "Put the name of the current guile module into `kill-ring'."
  57. (interactive)
  58. (bui-copy-as-kill (guix-guile-current-module)))
  59. (defun guix-devel-setup-repl (&optional repl)
  60. "Setup REPL for using `guix-devel-...' commands."
  61. (guix-devel-use-modules "(guix monad-repl)"
  62. "(guix scripts)"
  63. "(guix store)"
  64. "(guix ui)")
  65. ;; Without this workaround, the warning/build output disappears. See
  66. ;; <https://github.com/jaor/geiser/issues/83> for details.
  67. (guix-geiser-eval-in-repl-synchronously
  68. "(begin
  69. (guix-warning-port (current-warning-port))
  70. (current-build-output-port (current-error-port)))"
  71. repl 'no-history 'no-display))
  72. (defvar guix-devel-repl-processes nil
  73. "List of REPL processes configured by `guix-devel-setup-repl'.")
  74. (defun guix-devel-setup-repl-maybe (&optional repl)
  75. "Setup (if needed) REPL for using `guix-devel-...' commands."
  76. (let ((process (get-buffer-process (or repl (guix-geiser-repl)))))
  77. (when (and process
  78. (not (memq process guix-devel-repl-processes)))
  79. (guix-devel-setup-repl repl)
  80. (push process guix-devel-repl-processes))))
  81. (defmacro guix-devel-with-definition (def-var &rest body)
  82. "Run BODY with the current guile definition bound to DEF-VAR.
  83. Bind DEF-VAR variable to the name of the current top-level
  84. definition, setup the current REPL, use the current module, and
  85. run BODY."
  86. (declare (indent 1) (debug (symbolp body)))
  87. `(let ((,def-var (guix-guile-current-definition)))
  88. (guix-devel-setup-repl-maybe)
  89. (guix-devel-use-modules (guix-guile-current-module))
  90. ,@body))
  91. (defun guix-devel-build-package-definition ()
  92. "Build a package defined by the current top-level variable definition."
  93. (interactive)
  94. (guix-devel-with-definition def
  95. (when (or (not guix-operation-confirm)
  96. (guix-operation-prompt (format "Build '%s'?" def)))
  97. (guix-geiser-eval-in-repl
  98. (concat ",run-in-store "
  99. (guix-guile-make-call-expression
  100. "build-package" def
  101. "#:use-substitutes?" (guix-guile-boolean
  102. guix-use-substitutes)
  103. "#:dry-run?" (guix-guile-boolean guix-dry-run)))))))
  104. (defun guix-devel-build-package-source ()
  105. "Build the source of the current package definition."
  106. (interactive)
  107. (guix-devel-with-definition def
  108. (when (or (not guix-operation-confirm)
  109. (guix-operation-prompt
  110. (format "Build '%s' package source?" def)))
  111. (guix-geiser-eval-in-repl
  112. (concat ",run-in-store "
  113. (guix-guile-make-call-expression
  114. "build-package-source" def
  115. "#:use-substitutes?" (guix-guile-boolean
  116. guix-use-substitutes)
  117. "#:dry-run?" (guix-guile-boolean guix-dry-run)))))))
  118. (defun guix-devel-download-package-source ()
  119. "Download the source of the current package.
  120. Use this function to compute SHA256 hash of the package source."
  121. (interactive)
  122. (guix-devel-with-definition def
  123. (guix-devel-use-modules "(guix packages)"
  124. "(guix scripts download)")
  125. (when (or (not guix-operation-confirm)
  126. (y-or-n-p (format "Download '%s' package source?" def)))
  127. (guix-geiser-eval-in-repl
  128. (format "(guix-download (origin-uri (package-source %s)))"
  129. def)))))
  130. (defun guix-devel-lint-package ()
  131. "Check the current package.
  132. See Info node `(guix) Invoking guix lint' for details."
  133. (interactive)
  134. (guix-devel-with-definition def
  135. (guix-devel-use-modules "(guix scripts lint)")
  136. (when (or (not guix-operation-confirm)
  137. (y-or-n-p (format "Lint '%s' package?" def)))
  138. (guix-geiser-eval-in-repl
  139. (format "(run-checkers %s)" def)))))
  140. ;;; Font-lock
  141. (defvar guix-devel-modify-phases-keyword-regexp
  142. (rx (or "delete" "replace" "add-before" "add-after"))
  143. "Regexp for a 'modify-phases' keyword.")
  144. (defun guix-devel-modify-phases-font-lock-matcher (limit)
  145. "Find a 'modify-phases' keyword.
  146. This function is used as a MATCHER for `font-lock-keywords'."
  147. (ignore-errors
  148. (down-list)
  149. (or (re-search-forward guix-devel-modify-phases-keyword-regexp
  150. limit t)
  151. (set-match-data nil))
  152. (up-list)
  153. t))
  154. (defun guix-devel-modify-phases-font-lock-pre ()
  155. "Skip the next sexp, and return the end point of the current list.
  156. This function is used as a PRE-MATCH-FORM for `font-lock-keywords'
  157. to find 'modify-phases' keywords."
  158. (let ((in-comment? (nth 4 (syntax-ppss))))
  159. ;; If 'modify-phases' is commented, do not try to search for its
  160. ;; keywords.
  161. (unless in-comment?
  162. (ignore-errors (forward-sexp))
  163. (save-excursion (up-list) (point)))))
  164. (defconst guix-devel-keywords
  165. '("call-with-compressed-output-port"
  166. "call-with-container"
  167. "call-with-decompressed-port"
  168. "call-with-derivation-narinfo"
  169. "call-with-derivation-substitute"
  170. "call-with-error-handling"
  171. "call-with-gzip-input-port"
  172. "call-with-gzip-output-port"
  173. "call-with-temporary-directory"
  174. "call-with-temporary-output-file"
  175. "define-enumerate-type"
  176. "define-gexp-compiler"
  177. "define-lift"
  178. "define-monad"
  179. "define-operation"
  180. "define-record-type*"
  181. "emacs-substitute-sexps"
  182. "emacs-substitute-variables"
  183. "mbegin"
  184. "mlambda"
  185. "mlambdaq"
  186. "mlet"
  187. "mlet*"
  188. "modify-services"
  189. "munless"
  190. "mwhen"
  191. "run-with-state"
  192. "run-with-store"
  193. "signature-case"
  194. "substitute*"
  195. "substitute-keyword-arguments"
  196. "test-assertm"
  197. "use-package-modules"
  198. "use-service-modules"
  199. "use-system-modules"
  200. "with-atomic-file-output"
  201. "with-atomic-file-replacement"
  202. "with-derivation-narinfo"
  203. "with-derivation-substitute"
  204. "with-directory-excursion"
  205. "with-error-handling"
  206. "with-imported-modules"
  207. "with-monad"
  208. "with-mutex"
  209. "with-store"))
  210. (defvar guix-devel-font-lock-keywords
  211. `((,(rx (or "#~" "#$" "#$@" "#+" "#+@")) .
  212. 'guix-devel-gexp-symbol)
  213. (,(guix-guile-keyword-regexp (regexp-opt guix-devel-keywords))
  214. (1 'font-lock-keyword-face))
  215. (,(guix-guile-keyword-regexp "modify-phases")
  216. (1 'font-lock-keyword-face)
  217. (guix-devel-modify-phases-font-lock-matcher
  218. (guix-devel-modify-phases-font-lock-pre)
  219. nil
  220. (0 'guix-devel-modify-phases-keyword nil t))))
  221. "A list of `font-lock-keywords' for `guix-devel-mode'.")
  222. ;;; Indentation
  223. (defmacro guix-devel-scheme-indent (&rest rules)
  224. "Set `scheme-indent-function' according to RULES.
  225. Each rule should have a form (SYMBOL VALUE). See `put' for details."
  226. (declare (indent 0))
  227. `(progn
  228. ,@(mapcar (lambda (rule)
  229. `(put ',(car rule) 'scheme-indent-function ,(cadr rule)))
  230. rules)))
  231. (defun guix-devel-indent-package (state indent-point normal-indent)
  232. "Indentation rule for 'package' form."
  233. (let* ((package-eol (line-end-position))
  234. (count (if (and (ignore-errors (down-list) t)
  235. (< (point) package-eol)
  236. (looking-at "inherit\\>"))
  237. 1
  238. 0)))
  239. (lisp-indent-specform count state indent-point normal-indent)))
  240. (defun guix-devel-indent-modify-phases-keyword (count)
  241. "Return indentation function for 'modify-phases' keywords."
  242. (lambda (state indent-point normal-indent)
  243. (when (ignore-errors
  244. (goto-char (nth 1 state)) ; start of keyword sexp
  245. (backward-up-list)
  246. (looking-at "(modify-phases\\>"))
  247. (lisp-indent-specform count state indent-point normal-indent))))
  248. (defalias 'guix-devel-indent-modify-phases-keyword-1
  249. (guix-devel-indent-modify-phases-keyword 1))
  250. (defalias 'guix-devel-indent-modify-phases-keyword-2
  251. (guix-devel-indent-modify-phases-keyword 2))
  252. (guix-devel-scheme-indent
  253. (bag 0)
  254. (build-system 0)
  255. (call-with-compressed-output-port 2)
  256. (call-with-container 1)
  257. (call-with-gzip-input-port 1)
  258. (call-with-gzip-output-port 1)
  259. (call-with-decompressed-port 2)
  260. (call-with-error-handling 0)
  261. (container-excursion 1)
  262. (emacs-batch-edit-file 1)
  263. (emacs-batch-eval 0)
  264. (emacs-substitute-sexps 1)
  265. (emacs-substitute-variables 1)
  266. (file-system 0)
  267. (graft 0)
  268. (manifest-entry 0)
  269. (manifest-pattern 0)
  270. (mbegin 1)
  271. (mlambda 1)
  272. (mlambdaq 1)
  273. (mlet 2)
  274. (mlet* 2)
  275. (modify-phases 1)
  276. (modify-services 1)
  277. (munless 1)
  278. (mwhen 1)
  279. (operating-system 0)
  280. (origin 0)
  281. (package 'guix-devel-indent-package)
  282. (run-with-state 1)
  283. (run-with-store 1)
  284. (signature-case 1)
  285. (substitute* 1)
  286. (substitute-keyword-arguments 1)
  287. (test-assertm 1)
  288. (with-atomic-file-output 1)
  289. (with-derivation-narinfo 1)
  290. (with-derivation-substitute 2)
  291. (with-directory-excursion 1)
  292. (with-error-handling 0)
  293. (with-imported-modules 1)
  294. (with-monad 1)
  295. (with-mutex 1)
  296. (with-store 1)
  297. (wrap-program 1)
  298. ;; 'modify-phases' keywords:
  299. (replace 'guix-devel-indent-modify-phases-keyword-1)
  300. (add-after 'guix-devel-indent-modify-phases-keyword-2)
  301. (add-before 'guix-devel-indent-modify-phases-keyword-2))
  302. (defvar guix-devel-keys-map
  303. (let ((map (make-sparse-keymap)))
  304. (define-key map (kbd "b") 'guix-devel-build-package-definition)
  305. (define-key map (kbd "s") 'guix-devel-build-package-source)
  306. (define-key map (kbd "d") 'guix-devel-download-package-source)
  307. (define-key map (kbd "l") 'guix-devel-lint-package)
  308. (define-key map (kbd "k") 'guix-devel-copy-module-as-kill)
  309. (define-key map (kbd "u") 'guix-devel-use-module)
  310. map)
  311. "Keymap with subkeys for `guix-devel-mode-map'.")
  312. (defvar guix-devel-mode-map
  313. (let ((map (make-sparse-keymap)))
  314. (define-key map (kbd "C-c .") guix-devel-keys-map)
  315. map)
  316. "Keymap for `guix-devel-mode'.")
  317. ;;;###autoload
  318. (define-minor-mode guix-devel-mode
  319. "Minor mode for `scheme-mode' buffers.
  320. With a prefix argument ARG, enable the mode if ARG is positive,
  321. and disable it otherwise. If called from Lisp, enable the mode
  322. if ARG is omitted or nil.
  323. When Guix Devel mode is enabled, it highlights various Guix
  324. keywords. This mode can be enabled programmatically using hooks,
  325. like this:
  326. (add-hook 'scheme-mode-hook 'guix-devel-mode)
  327. \\{guix-devel-mode-map}"
  328. :init-value nil
  329. :lighter " Guix"
  330. :keymap guix-devel-mode-map
  331. (if guix-devel-mode
  332. (progn
  333. (setq-local font-lock-multiline t)
  334. (font-lock-add-keywords nil guix-devel-font-lock-keywords))
  335. (setq-local font-lock-multiline nil)
  336. (font-lock-remove-keywords nil guix-devel-font-lock-keywords))
  337. (guix-font-lock-flush))
  338. (defvar guix-devel-emacs-font-lock-keywords
  339. (eval-when-compile
  340. `((,(rx "(" (group "guix-devel-with-definition") symbol-end) . 1))))
  341. (font-lock-add-keywords 'emacs-lisp-mode
  342. guix-devel-emacs-font-lock-keywords)
  343. (provide 'guix-devel)
  344. ;;; guix-devel.el ends here