guix-devel.el 14 KB

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