summary refs log tree commit diff stats
path: root/lisp/+apheleia.el
blob: df651b8969a04359f6e4fbf8e466d7dcb6e4085e (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
51
52
53
54
55
56
57
58
59
60
61
62
;;; +apheleia.el -*- lexical-binding: t; -*-

;;; Code:

(require 'apheleia)
(require 'cl-lib)

;; https://github.com/raxod502/apheleia/pull/63#issue-1077529623
(cl-defun +apheleia-indent-region (&key buffer scratch formatter callback &allow-other-keys)
  (with-current-buffer scratch
    (setq-local indent-line-function
                (buffer-local-value 'indent-line-function buffer))
    (indent-region (point-min)
                   (point-max))
    (funcall callback)))


;;; `setup' integration

(require 'setup)

(setup-define :apheleia
  (lambda (name formatter &optional mode -pend)
    (let* ((mode (or mode (setup-get 'mode)))
           (current-formatters (and -pend
                                    (alist-get mode apheleia-formatters))))
      `(progn
         (setf (alist-get ',name apheleia-formatters)
               ,formatter)
         (setf (alist-get ',mode apheleia-mode-alist)
               ',(pcase -pend
                   (:append (append (ensure-list current-formatters)
                                    (list name)))
                   (:prepend (cons name (ensure-list current-formatters)))
                   ('nil name)
                   (_ (error "Improper `:apheleia' -PEND argument")))))))
  :documentation
   "Register a formatter to `apheleia''s lists.
NAME is the name given to the formatter in `apheleia-formatters'
and `apheleia-mode-alist'.  FORMATTER is the command paired with
NAME in `apheleia-formatters'.  MODE is the mode or modes to add
NAME to in `apheleia-mode-alist'.  If MODE is not given or nil,
use the setup form's MODE.  Optional argument -PEND can be one of
`:append' or `:prepend', and if given will append or prepend the
given NAME to the current formatters for the MODE in
`apheleia-mode-alist', rather than replace them (the default).

Example:
(setup
 (:apheleia isort (\"isort\" \"--stdout\" \"-\")
                  python-mode))
; =>
(progn
  (setf (alist-get 'isort apheleia-formatters)
        '(\"isort\" \"--stdout\" \"-\"))
  (setf (alist-get 'python-mode apheleia-mode-alist)
        'isort))

This form cannot be repeated, and it cannot be used as HEAD.")

(provide '+apheleia)
;;; +apheleia.el ends here