2018-08-29 12:48:38 +02:00
|
|
|
;;; editor/format/autoload/settings.el -*- lexical-binding: t; -*-
|
|
|
|
|
|
|
|
;;;###autodef
|
2022-08-14 16:17:36 +01:00
|
|
|
(cl-defun set-formatter! (name args &key modes filter)
|
2018-09-08 23:44:40 -04:00
|
|
|
"Define (or modify) a formatter named NAME.
|
|
|
|
|
2022-08-16 08:13:55 +01:00
|
|
|
Supported keywords: :modes :filter
|
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.
|
|
|
|
|
2022-08-16 08:13:55 +01:00
|
|
|
For more information on how to structure the list to be
|
|
|
|
compatible, see `apheleia--run-formatter-function'.
|
|
|
|
|
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:
|
2018-09-08 23:44:40 -04: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!
|
2018-09-08 23:44:40 -04:00
|
|
|
'clang-format
|
|
|
|
'(\"clang-format\"
|
|
|
|
(\"-assume-filename=%S\" (or buffer-file-name mode-result \"\")))
|
|
|
|
:modes
|
2018-08-29 12:48:38 +02:00
|
|
|
'((c-mode \".c\")
|
|
|
|
(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
|
|
|
|
2018-09-08 23:44:40 -04:00
|
|
|
(set-formatter! 'html-tidy
|
|
|
|
'(\"tidy\" \"-q\" \"-indent\"
|
|
|
|
(\"-xml\" (memq major-mode '(nxml-mode xml-mode))))
|
|
|
|
:modes
|
2018-08-29 12:48:38 +02:00
|
|
|
'(html-mode
|
|
|
|
(web-mode (and (equal \"none\" web-mode-engine)
|
2022-08-16 08:13:55 +01:00
|
|
|
(car (member web-mode-content-type '(\"xml\" \"html\")))))))
|
2018-08-29 12:48:38 +02:00
|
|
|
|
2018-09-03 03:53:07 +02:00
|
|
|
(set-formatter! 'html-tidy ; overwrite predefined html-tidy formatter
|
|
|
|
'(\"tidy\" \"-q\" \"-indent\"
|
|
|
|
\"--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\"))
|
2022-08-16 08:13:55 +01:00
|
|
|
(\"-xml\" (memq major-mode '(nxml-mode xml-mode)))))
|
2018-09-03 03:53:07 +02:00
|
|
|
|
2018-09-08 23:44:40 -04:00
|
|
|
(set-formatter! 'elm-format
|
2022-08-16 08:13:55 +01:00
|
|
|
\"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-04-14 21:11:34 +01:00
|
|
|
(after! apheleia-core
|
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))))))
|