1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014, 2018 Mark H Weaver <mhw@netris.org>
3 ;;; Copyright © 2014 Alex Kost <alezost@gmail.com>
4 ;;; Copyright © 2018 Maxim Cournoyer <maxim.cournoyer@gmail.com>
6 ;;; This file is part of GNU Guix.
8 ;;; GNU Guix is free software; you can redistribute it and/or modify it
9 ;;; under the terms of the GNU General Public License as published by
10 ;;; the Free Software Foundation; either version 3 of the License, or (at
11 ;;; your option) any later version.
13 ;;; GNU Guix is distributed in the hope that it will be useful, but
14 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;;; GNU General Public License for more details.
18 ;;; You should have received a copy of the GNU General Public License
19 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
21 (define-module (guix build emacs-utils)
22 #:use-module (guix build utils)
26 emacs-generate-autoloads
27 emacs-byte-compile-directory
28 emacs-substitute-sexps
29 emacs-substitute-variables))
33 ;;; Tools to programmatically edit files using Emacs,
34 ;;; e.g. to replace entire s-expressions in elisp files.
39 ;; The `emacs' command.
40 (make-parameter "emacs"))
42 (define (emacs-batch-eval expr)
43 "Run Emacs in batch mode, and execute the elisp code EXPR."
44 (invoke (%emacs) "--quick" "--batch"
45 (format #f "--eval=~S" expr)))
47 (define (emacs-batch-edit-file file expr)
48 "Load FILE in Emacs using batch mode, and execute the elisp code EXPR."
49 (invoke (%emacs) "--quick" "--batch"
50 (string-append "--visit=" file)
51 (format #f "--eval=~S" expr)))
53 (define (emacs-generate-autoloads name directory)
54 "Generate autoloads for Emacs package NAME placed in DIRECTORY."
55 (let* ((file (string-append directory "/" name "-autoloads.el"))
56 (expr `(let ((backup-inhibited t)
57 (generated-autoload-file ,file))
58 (update-directory-autoloads ,directory))))
59 (emacs-batch-eval expr)))
61 (define* (emacs-byte-compile-directory dir)
62 "Byte compile all files in DIR and its sub-directories."
64 (setq byte-compile-debug t) ; for proper exit status
65 (byte-recompile-directory (file-name-as-directory ,dir) 0 1))))
66 (emacs-batch-eval expr)))
68 (define-syntax emacs-substitute-sexps
70 "Substitute the S-expression immediately following the first occurrence of
71 LEADING-REGEXP by the string returned by REPLACEMENT in FILE. For example:
73 (emacs-substitute-sexps \"w3m.el\"
74 (\"defcustom w3m-command\"
75 (string-append w3m \"/bin/w3m\"))
76 (\"defvar w3m-image-viewer\"
77 (string-append imagemagick \"/bin/display\")))
79 This replaces the default values of the `w3m-command' and `w3m-image-viewer'
80 variables declared in `w3m.el' with the results of the `string-append' calls
81 above. Note that LEADING-REGEXP uses Emacs regexp syntax."
82 ((emacs-substitute-sexps file (leading-regexp replacement) ...)
83 (emacs-batch-edit-file file
84 `(progn (progn (goto-char (point-min))
85 (re-search-forward ,leading-regexp)
88 (insert ,(format #f "~S" replacement)))
90 (basic-save-buffer))))))
92 (define-syntax emacs-substitute-variables
94 "Substitute the default value of VARIABLE by the string returned by
95 REPLACEMENT in FILE. For example:
97 (emacs-substitute-variables \"w3m.el\"
98 (\"w3m-command\" (string-append w3m \"/bin/w3m\"))
99 (\"w3m-image-viewer\" (string-append imagemagick \"/bin/display\")))
101 This replaces the default values of the `w3m-command' and `w3m-image-viewer'
102 variables declared in `w3m.el' with the results of the `string-append' calls
104 ((emacs-substitute-variables file (variable replacement) ...)
105 (emacs-substitute-sexps file
106 ((string-append "(def[a-z]+[[:space:]\n]+" variable "\\>")
110 ;;; emacs-utils.scm ends here