2018-08-29 12:48:38 +02:00
|
|
|
;;; editor/format/autoload/settings.el -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;;;###autodef
|
2023-09-13 10:20:39 +01:00
|
|
|
(cl-defun set-formatter! (name args &key modes)
|
2018-09-08 23:44:40 -04:00
|
|
|
"Define (or modify) a formatter named NAME.
|
|
|
|
|
2023-09-14 16:44:43 +01:00
|
|
|
Supported keywords: :modes
|
2018-09-08 23:44:40 -04:00
|
|
|
|
|
|
|
NAME is a symbol that identifies this formatter.
|
|
|
|
|
|
|
|
FORMATTER can be a symbol referring to another formatter, a function, string or
|
|
|
|
nested list.
|
2018-08-29 12:48:38 +02:00
|
|
|
|
|
|
|
If a function, it should be a formatter function that
|
2022-08-16 08:13:55 +01:00
|
|
|
`apheleia--run-formatter-function' will accept.
|
2018-08-29 15:56:59 +02:00
|
|
|
If a string, it is assumed to be a shell command that the buffer's text will
|
|
|
|
be piped to (through stdin).
|
|
|
|
If a list, it should represent a shell command as a list of arguments. Each
|
|
|
|
element is either a string or list (STRING ARG) where STRING is a format
|
2018-08-29 12:48:38 +02:00
|
|
|
string and ARG is both a predicate and argument for STRING. If ARG is nil,
|
|
|
|
STRING will be omitted from the vector.
|
|
|
|
|
2023-09-14 16:44:43 +01:00
|
|
|
If you're trying to override this, ensure that you wrap the call in `after!' and
|
|
|
|
whichever package sets the initial formatter. See the ':editor format' README
|
|
|
|
for more.
|
|
|
|
|
2023-09-14 17:29:41 +02:00
|
|
|
For more information on how to structure the list to be compatible, see
|
|
|
|
`apheleia--run-formatter-function'.
|
2022-08-16 08:13:55 +01:00
|
|
|
|
2018-09-08 23:44:40 -04:00
|
|
|
MODES is a major mode, a list thereof, or a list of two-element sublists with
|
|
|
|
the structure: (MAJOR-MODE FORM). FORM is evaluated when the buffer is formatted
|
|
|
|
and its return value serves two purposes:
|
2018-08-29 15:56:59 +02:00
|
|
|
|
2018-09-08 23:44:40 -04:00
|
|
|
1. It is a predicate for this formatter. Assuming the MAJOR-MODE matches the
|
|
|
|
current mode, if FORM evaluates to nil, the formatter is skipped.
|
|
|
|
2. It's return value is made available to FORMATTER if it is a function or
|
|
|
|
list of shell arguments via the `mode-result' variable.
|
2018-08-29 15:56:59 +02:00
|
|
|
|
2018-08-29 12:48:38 +02:00
|
|
|
Basic examples:
|
2023-09-14 17:29:41 +02:00
|
|
|
(set-formatter! \\='asmfmt \"asmfmt\" :modes \\='(asm-mode nasm-mode))
|
|
|
|
(set-formatter! \\='black \"black -q -\")
|
|
|
|
(set-formatter! \\='html-tidy \"tidy -q -indent\" :modes \\='(html-mode web-mode))
|
2018-08-29 12:48:38 +02:00
|
|
|
|
|
|
|
Advanced examples:
|
|
|
|
(set-formatter!
|
2023-09-14 17:29:41 +02:00
|
|
|
\\='clang-format
|
|
|
|
\\='(\"clang-format\"
|
2018-09-08 23:44:40 -04:00
|
|
|
(\"-assume-filename=%S\" (or buffer-file-name mode-result \"\")))
|
|
|
|
:modes
|
2023-09-14 17:29:41 +02:00
|
|
|
\\='((c-mode \".c\")
|
2018-08-29 12:48:38 +02:00
|
|
|
(c++-mode \".cpp\")
|
|
|
|
(java-mode \".java\")
|
|
|
|
(objc-mode \".m\")
|
2018-09-08 23:44:40 -04:00
|
|
|
(protobuf-mode \".proto\")))
|
2018-08-29 12:48:38 +02:00
|
|
|
|
2023-09-14 17:29:41 +02:00
|
|
|
(set-formatter! \\='html-tidy
|
|
|
|
\\='(\"tidy\" \"-q\" \"-indent\"
|
|
|
|
(\"-xml\" (memq major-mode \\='(nxml-mode xml-mode))))
|
2018-09-08 23:44:40 -04:00
|
|
|
:modes
|
2023-09-14 17:29:41 +02:00
|
|
|
\\='(html-mode
|
2018-08-29 12:48:38 +02:00
|
|
|
(web-mode (and (equal \"none\" web-mode-engine)
|
2023-09-14 17:29:41 +02:00
|
|
|
(car (member web-mode-content-type \\='(\"xml\" \"html\")))))))
|
2018-08-29 12:48:38 +02:00
|
|
|
|
2023-09-14 17:29:41 +02:00
|
|
|
(set-formatter! \\='html-tidy ; overwrite predefined html-tidy formatter
|
|
|
|
\\='(\"tidy\" \"-q\" \"-indent\"
|
2018-09-03 03:53:07 +02:00
|
|
|
\"--tidy-mark\" \"no\"
|
|
|
|
\"--drop-empty-elements\" \"no\"
|
|
|
|
\"--show-body-only\" \"auto\"
|
|
|
|
(\"--indent-spaces\" \"%d\" tab-width)
|
|
|
|
(\"--indent-with-tabs\" \"%s\" (if indent-tabs-mode \"yes\" \"no\"))
|
2023-09-14 17:29:41 +02:00
|
|
|
(\"-xml\" (memq major-mode \\='(nxml-mode xml-mode)))))
|
2018-09-03 03:53:07 +02:00
|
|
|
|
2023-09-14 17:29:41 +02:00
|
|
|
(set-formatter! \\='elm-format
|
|
|
|
\"elm-format --yes --stdin\")"
|
2018-08-29 12:48:38 +02:00
|
|
|
(declare (indent defun))
|
2018-09-08 23:44:40 -04:00
|
|
|
(cl-check-type name symbol)
|
2023-11-29 17:27:06 +01:00
|
|
|
(after! apheleia
|
2022-08-16 08:13:55 +01:00
|
|
|
(if (null args)
|
|
|
|
(progn
|
|
|
|
(setq apheleia-formatters
|
|
|
|
(assq-delete-all name apheleia-formatters))
|
|
|
|
(while (rassoc name apheleia-mode-alist)
|
|
|
|
(setq apheleia-mode-alist
|
|
|
|
(assq-delete-all (car (rassoc name apheleia-mode-alist)) apheleia-mode-alist))))
|
2022-08-14 16:17:36 +01:00
|
|
|
(let ((formatter (cond
|
|
|
|
((listp args) `(,@args))
|
|
|
|
(t args))))
|
|
|
|
(setf (alist-get name apheleia-formatters) formatter))
|
2022-08-16 08:13:55 +01:00
|
|
|
(when modes
|
|
|
|
(dolist (mode modes)
|
|
|
|
(setf (alist-get mode apheleia-mode-alist) name))))))
|