aboutsummaryrefslogtreecommitdiff
path: root/format.el
blob: 48c2df63ffbdc110c1122640dec93b6164390988 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
;;; format.el — batch scheme formatter for downstroke.
;;;
;;; Reformats every tracked .scm file using `indent-region` under
;;; `scheme-mode`, with the user's full Emacs init loaded so the indent
;;; rules match exactly what interactive Emacs does (notably the
;;; geiser-chicken / scheme-mode `module` indent).
;;;
;;; Usage (from the Makefile):
;;;     make format
;;;
;;; Or directly:
;;;     emacs --batch \
;;;       --init-directory=$HOME/.emacs-perso \
;;;       --eval "(add-to-list 'load-path (expand-file-name \"emacs-substrate/\" \"~/.emacs-perso\"))" \
;;;       -l ~/.emacs-perso/init.el \
;;;       -l format.el \
;;;       --eval "(downstroke-format-tracked-scm-files)"

(require 'subr-x)

(defun downstroke--project-root ()
  "Directory containing this script — the downstroke project root."
  (file-name-directory (or load-file-name buffer-file-name default-directory)))

(defun downstroke--tracked-scm-files ()
  "Return a list of all .scm files tracked by git in the project."
  (let ((default-directory (downstroke--project-root)))
    (split-string
     (string-trim
      (shell-command-to-string "git ls-files -- '*.scm'"))
     "\n" t)))

(defun downstroke-format-file (path)
  "Reformat a single .scm file with `indent-region'."
  (let ((inhibit-message t))
    (find-file path)
    (scheme-mode)
    (indent-region (point-min) (point-max))
    (save-buffer)
    (kill-buffer)))

(defun downstroke-format-tracked-scm-files ()
  "Reformat every tracked .scm file in the project."
  (let ((default-directory (downstroke--project-root))
        (files (downstroke--tracked-scm-files)))
    (message "downstroke-format: reindenting %d files" (length files))
    (dolist (p files)
      (message "indent: %s" p)
      (downstroke-format-file p))
    (message "downstroke-format: done")))