Commit | Line | Data |
---|---|---|
75f3b1a1 | 1 | ;;; GNU Guix --- Functional package management for GNU |
78374500 | 2 | ;;; Copyright © 2014, 2018 Mark H Weaver <mhw@netris.org> |
0b08600c | 3 | ;;; Copyright © 2014 Alex Kost <alezost@gmail.com> |
afc6b1c0 | 4 | ;;; Copyright © 2018, 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com> |
9ce3f7f6 | 5 | ;;; Copyright © 2019 Leo Prikler <leo.prikler@student.tugraz.at> |
75f3b1a1 MW |
6 | ;;; |
7 | ;;; This file is part of GNU Guix. | |
8 | ;;; | |
9 | ;;; GNU Guix is free software; you can redistribute it and/or modify it | |
10 | ;;; under the terms of the GNU General Public License as published by | |
11 | ;;; the Free Software Foundation; either version 3 of the License, or (at | |
12 | ;;; your option) any later version. | |
13 | ;;; | |
14 | ;;; GNU Guix is distributed in the hope that it will be useful, but | |
15 | ;;; WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;;; GNU General Public License for more details. | |
18 | ;;; | |
19 | ;;; You should have received a copy of the GNU General Public License | |
20 | ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>. | |
21 | ||
22 | (define-module (guix build emacs-utils) | |
78374500 | 23 | #:use-module (guix build utils) |
afc6b1c0 | 24 | #:use-module (ice-9 format) |
75f3b1a1 MW |
25 | #:export (%emacs |
26 | emacs-batch-eval | |
27 | emacs-batch-edit-file | |
9ce3f7f6 | 28 | emacs-batch-disable-compilation |
0b08600c | 29 | emacs-generate-autoloads |
1d44b4e5 | 30 | emacs-byte-compile-directory |
75f3b1a1 MW |
31 | emacs-substitute-sexps |
32 | emacs-substitute-variables)) | |
33 | ||
34 | ;;; Commentary: | |
35 | ;;; | |
36 | ;;; Tools to programmatically edit files using Emacs, | |
37 | ;;; e.g. to replace entire s-expressions in elisp files. | |
38 | ;;; | |
39 | ;;; Code: | |
40 | ||
41 | (define %emacs | |
42 | ;; The `emacs' command. | |
43 | (make-parameter "emacs")) | |
44 | ||
9659459f MC |
45 | (define (expr->string expr) |
46 | "Converts EXPR, an expression, into a string." | |
47 | (if (string? expr) | |
48 | expr | |
49 | (format #f "~s" expr))) | |
50 | ||
afc6b1c0 MC |
51 | (define* (emacs-batch-eval expr #:key dynamic?) |
52 | "Run Emacs in batch mode, and execute the Elisp code EXPR. If DYNAMIC? is | |
53 | true, evaluate using dynamic scoping." | |
78374500 | 54 | (invoke (%emacs) "--quick" "--batch" |
afc6b1c0 MC |
55 | (format #f "--eval=(eval '~a ~:[t~;nil~])" |
56 | (expr->string expr) dynamic?))) | |
75f3b1a1 MW |
57 | |
58 | (define (emacs-batch-edit-file file expr) | |
59 | "Load FILE in Emacs using batch mode, and execute the elisp code EXPR." | |
78374500 MW |
60 | (invoke (%emacs) "--quick" "--batch" |
61 | (string-append "--visit=" file) | |
9659459f | 62 | (string-append "--eval=" (expr->string expr)))) |
75f3b1a1 | 63 | |
9ce3f7f6 LP |
64 | (define (emacs-batch-disable-compilation file) |
65 | (emacs-batch-edit-file file | |
66 | '(progn | |
67 | (add-file-local-variable 'no-byte-compile t) | |
68 | (basic-save-buffer)))) | |
69 | ||
0b08600c AK |
70 | (define (emacs-generate-autoloads name directory) |
71 | "Generate autoloads for Emacs package NAME placed in DIRECTORY." | |
72 | (let* ((file (string-append directory "/" name "-autoloads.el")) | |
73 | (expr `(let ((backup-inhibited t) | |
74 | (generated-autoload-file ,file)) | |
75 | (update-directory-autoloads ,directory)))) | |
afc6b1c0 | 76 | (emacs-batch-eval expr #:dynamic? #t))) |
0b08600c | 77 | |
b7dee6a0 MC |
78 | (define* (emacs-byte-compile-directory dir) |
79 | "Byte compile all files in DIR and its sub-directories." | |
c1b4ad2e MC |
80 | (let ((expr `(progn |
81 | (setq byte-compile-debug t) ; for proper exit status | |
82 | (byte-recompile-directory (file-name-as-directory ,dir) 0 1)))) | |
1d44b4e5 FB |
83 | (emacs-batch-eval expr))) |
84 | ||
75f3b1a1 MW |
85 | (define-syntax emacs-substitute-sexps |
86 | (syntax-rules () | |
87 | "Substitute the S-expression immediately following the first occurrence of | |
88 | LEADING-REGEXP by the string returned by REPLACEMENT in FILE. For example: | |
89 | ||
90 | (emacs-substitute-sexps \"w3m.el\" | |
91 | (\"defcustom w3m-command\" | |
92 | (string-append w3m \"/bin/w3m\")) | |
93 | (\"defvar w3m-image-viewer\" | |
94 | (string-append imagemagick \"/bin/display\"))) | |
95 | ||
96 | This replaces the default values of the `w3m-command' and `w3m-image-viewer' | |
97 | variables declared in `w3m.el' with the results of the `string-append' calls | |
98 | above. Note that LEADING-REGEXP uses Emacs regexp syntax." | |
99 | ((emacs-substitute-sexps file (leading-regexp replacement) ...) | |
100 | (emacs-batch-edit-file file | |
101 | `(progn (progn (goto-char (point-min)) | |
102 | (re-search-forward ,leading-regexp) | |
103 | (kill-sexp) | |
104 | (insert " ") | |
105 | (insert ,(format #f "~S" replacement))) | |
106 | ... | |
107 | (basic-save-buffer)))))) | |
108 | ||
109 | (define-syntax emacs-substitute-variables | |
110 | (syntax-rules () | |
111 | "Substitute the default value of VARIABLE by the string returned by | |
112 | REPLACEMENT in FILE. For example: | |
113 | ||
114 | (emacs-substitute-variables \"w3m.el\" | |
115 | (\"w3m-command\" (string-append w3m \"/bin/w3m\")) | |
116 | (\"w3m-image-viewer\" (string-append imagemagick \"/bin/display\"))) | |
117 | ||
118 | This replaces the default values of the `w3m-command' and `w3m-image-viewer' | |
119 | variables declared in `w3m.el' with the results of the `string-append' calls | |
120 | above." | |
121 | ((emacs-substitute-variables file (variable replacement) ...) | |
122 | (emacs-substitute-sexps file | |
123 | ((string-append "(def[a-z]+[[:space:]\n]+" variable "\\>") | |
124 | replacement) | |
125 | ...)))) | |
126 | ||
127 | ;;; emacs-utils.scm ends here |