gnu: Add armips.
[jackhill/guix/guix.git] / etc / indent-code.el
1 :;exec emacs --batch --quick --load="$0" --funcall=main "$@"
2 ;;; indent-code.el --- Run Emacs to indent a package definition.
3
4 ;; Copyright © 2017 Alex Kost <alezost@gmail.com>
5 ;; Copyright © 2017 Ludovic Courtès <ludo@gnu.org>
6 ;; Copyright © 2020 Maxim Cournoyer <maxim.cournoyer@gmail.com>
7
8 ;; This file is part of GNU Guix.
9
10 ;; GNU Guix is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Guix is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; This scripts indents the given file or package definition in the specified
26 ;; file using Emacs.
27
28 ;;; Code:
29
30 ;; Load Scheme indentation rules from ".dir-locals.el".
31 (with-temp-buffer
32 (scheme-mode)
33 (let ((default-directory (file-name-as-directory load-file-name))
34 (enable-local-variables :all))
35 (hack-dir-local-variables)
36 (hack-local-variables-apply)))
37
38 ;; Add indentation info for Scheme constructs that are not Guix-specific.
39 ;; This is normally provided by Geiser but this file is for people who may not
40 ;; be running Geiser, so we just copy it here (from 'geiser-syntax.el').
41 (defmacro guix-syntax--scheme-indent (&rest pairs)
42 `(progn ,@(mapcar (lambda (p)
43 `(put ',(car p) 'scheme-indent-function ',(cadr p)))
44 pairs)))
45
46 (guix-syntax--scheme-indent
47 (and-let* 1)
48 (case-lambda 0)
49 (catch defun)
50 (class defun)
51 (dynamic-wind 0)
52 (guard 1)
53 (let*-values 1)
54 (let-values 1)
55 (let/ec 1)
56 (letrec* 1)
57 (match 1)
58 (match-lambda 0)
59 (match-lambda* 0)
60 (match-let scheme-let-indent)
61 (match-let* 1)
62 (match-letrec 1)
63 (opt-lambda 1)
64 (parameterize 1)
65 (parameterize* 1)
66 (receive 2)
67 (require-extension 0)
68 (syntax-case 2)
69 (test-approximate 1)
70 (test-assert 1)
71 (test-eq 1)
72 (test-equal 1)
73 (test-eqv 1)
74 (test-group-with-cleanup 1)
75 (test-runner-on-bad-count! 1)
76 (test-runner-on-bad-end-name! 1)
77 (test-runner-on-final! 1)
78 (test-runner-on-group-begin! 1)
79 (test-runner-on-group-end! 1)
80 (test-runner-on-test-begin! 1)
81 (test-runner-on-test-end! 1)
82 (test-with-runner 1)
83 (unless 1)
84 (when 1)
85 (while 1)
86 (with-exception-handler 1)
87 (with-syntax 1))
88
89 \f
90 (defun main ()
91 (pcase command-line-args-left
92 (`(,file-name ,package-name)
93 ;; Indent the definition of PACKAGE-NAME in FILE-NAME.
94 (find-file file-name)
95 (goto-char (point-min))
96 (if (re-search-forward (concat "^(define\\(-public\\) +"
97 package-name)
98 nil t)
99 (let ((indent-tabs-mode nil))
100 (beginning-of-defun)
101 (indent-sexp)
102 (save-buffer)
103 (message "Done!"))
104 (error "Package '%s' not found in '%s'"
105 package-name file-name)))
106 (`(,file-name)
107 ;; Indent all of FILE-NAME.
108 (find-file file-name)
109 (let ((indent-tabs-mode nil))
110 (indent-region (point-min) (point-max))
111 (save-buffer)
112 (message "Done!")))
113 (x
114 (error "Usage: indent-code.el FILE [PACKAGE]"))))
115
116 ;;; indent-code.el ends here