Add (guix build emacs-utils).
[jackhill/guix/guix.git] / guix / build / emacs-utils.scm
1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2014 Mark H Weaver <mhw@netris.org>
3 ;;;
4 ;;; This file is part of GNU Guix.
5 ;;;
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
10 ;;;
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; 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 GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19 (define-module (guix build emacs-utils)
20 #:export (%emacs
21 emacs-batch-eval
22 emacs-batch-edit-file
23 emacs-substitute-sexps
24 emacs-substitute-variables))
25
26 ;;; Commentary:
27 ;;;
28 ;;; Tools to programmatically edit files using Emacs,
29 ;;; e.g. to replace entire s-expressions in elisp files.
30 ;;;
31 ;;; Code:
32
33 (define %emacs
34 ;; The `emacs' command.
35 (make-parameter "emacs"))
36
37 (define (emacs-batch-eval expr)
38 "Run Emacs in batch mode, and execute the elisp code EXPR."
39 (unless (zero? (system* (%emacs) "--quick" "--batch"
40 (format #f "--eval=~S" expr)))
41 (error "emacs-batch-eval failed!" expr)))
42
43 (define (emacs-batch-edit-file file expr)
44 "Load FILE in Emacs using batch mode, and execute the elisp code EXPR."
45 (unless (zero? (system* (%emacs) "--quick" "--batch"
46 (string-append "--visit=" file)
47 (format #f "--eval=~S" expr)))
48 (error "emacs-batch-edit-file failed!" file expr)))
49
50 (define-syntax emacs-substitute-sexps
51 (syntax-rules ()
52 "Substitute the S-expression immediately following the first occurrence of
53 LEADING-REGEXP by the string returned by REPLACEMENT in FILE. For example:
54
55 (emacs-substitute-sexps \"w3m.el\"
56 (\"defcustom w3m-command\"
57 (string-append w3m \"/bin/w3m\"))
58 (\"defvar w3m-image-viewer\"
59 (string-append imagemagick \"/bin/display\")))
60
61 This replaces the default values of the `w3m-command' and `w3m-image-viewer'
62 variables declared in `w3m.el' with the results of the `string-append' calls
63 above. Note that LEADING-REGEXP uses Emacs regexp syntax."
64 ((emacs-substitute-sexps file (leading-regexp replacement) ...)
65 (emacs-batch-edit-file file
66 `(progn (progn (goto-char (point-min))
67 (re-search-forward ,leading-regexp)
68 (kill-sexp)
69 (insert " ")
70 (insert ,(format #f "~S" replacement)))
71 ...
72 (basic-save-buffer))))))
73
74 (define-syntax emacs-substitute-variables
75 (syntax-rules ()
76 "Substitute the default value of VARIABLE by the string returned by
77 REPLACEMENT in FILE. For example:
78
79 (emacs-substitute-variables \"w3m.el\"
80 (\"w3m-command\" (string-append w3m \"/bin/w3m\"))
81 (\"w3m-image-viewer\" (string-append imagemagick \"/bin/display\")))
82
83 This replaces the default values of the `w3m-command' and `w3m-image-viewer'
84 variables declared in `w3m.el' with the results of the `string-append' calls
85 above."
86 ((emacs-substitute-variables file (variable replacement) ...)
87 (emacs-substitute-sexps file
88 ((string-append "(def[a-z]+[[:space:]\n]+" variable "\\>")
89 replacement)
90 ...))))
91
92 ;;; emacs-utils.scm ends here