guix-devel.el 16 KB

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