                      ((car command-list) (intern (car command-list)))
                      ((symbolp formatter) formatter)
                      ((user-error "Anonymous formatter requires a :name"))))
-         (fn (lambda (executable mode-result)
-               (let ((result
-                      (cond ((commandp formatter)
-                             (let ((mode major-mode)
-                                   (file buffer-file-name)
-                                   (dir default-directory))
-                               (format-all-buffer-thunk
-                                (lambda (input)
-                                  (with-silent-modifications
-                                    (setq buffer-file-name file
-                                          default-directory dir)
-                                    (delay-mode-hooks (funcall mode))
-                                    (insert input)
-                                    (condition-case e
-                                        (progn
-                                          (call-interactively formatter)
-                                          (list nil ""))
-                                      (error (list t (error-message-string e)))))))))
-                            ((functionp formatter)
-                             (format-all-buffer-thunk formatter))
-                            ((cl-loop for arg in command-list
-                                      if (stringp arg)
-                                      collect arg into args
-                                      else if (eval (cadr arg) t)
-                                      collect (format (car arg) it) into args
-                                      finally do
-                                      (if (or ok-statuses error-regexp)
-                                          (apply #'format-all-buffer-hard ok-statuses error-regexp args)
-                                        (apply #'format-all-buffer-easy args)))))))
-                 (if filter
-                     (apply filter result)
-                   result))))
+         (formatter
+          (cond ((commandp formatter)
+                 `(format-all-buffer-thunk
+                   (lambda (input)
+                     (with-silent-modifications
+                       (setq buffer-file-name ,(buffer-file-name (buffer-base-buffer))
+                             default-directory ,default-directory)
+                       (delay-mode-hooks (funcall ',major-mode))
+                       (insert input)
+                       (condition-case e
+                           (progn (call-interactively #',formatter)
+                                  (list nil ""))
+                         (error (list t (error-message-string e))))))))
+                ((functionp formatter)
+                 `(format-all-buffer-thunk #',formatter))
+                (`(let (args)
+                    (dolist (arg ',command-list)
+                      (cond ((stringp arg) (push arg args))
+                            ((listp arg)
+                             (catch 'skip
+                               (let (subargs this)
+                                 (while (setq this (pop arg))
+                                   (cond ((not (stringp (car arg)))
+                                          (let ((val (eval (pop arg) t)))
+                                            (unless val (throw 'skip nil))
+                                            (push (format this val) subargs)))
+                                         ((stringp this)
+                                          (push this subargs))))
+                                 (setq args (append subargs args)))))))
+                    (if ,(and (or ok-statuses error-regexp) t)
+                        (apply #'format-all-buffer-hard
+                               ',ok-statuses ,error-regexp
+                               (reverse args))
+                      (apply #'format-all-buffer-easy (reverse args)))))))
+         (fn
+          `(lambda (executable mode-result)
+             (let ((result ,formatter))
+               ,(if filter
+                    `(apply #',filter result)
+                  'result))))
          (install (cond ((null install) install)
                         ((listp install)
                          (cdr (assq (+format--resolve-system) install)))
                         (install))))
     (after! format-all
-      (puthash name fn format-all-format-table)
-      (puthash name install format-all-install-table)
+      (puthash name (eval fn t) format-all-format-table)
       (puthash name (car command-list) format-all-executable-table)
+      (puthash name (or install (gethash name format-all-install-table))
+               format-all-install-table)
       (dolist (mode (doom-enlist modes))
         (cl-destructuring-bind (m &optional probe)
             (doom-enlist mode)

           return (cadr choice)))

;;;###autodef
(cl-defun set-formatter! (modes formatter &key
                                name
                                install
                                filter
                                ok-statuses
                                error-regexp)
  "Define a FORMATTER for MODES.

MODES can be a major mode symbol, a list of major modes, or a list of
two-element lists made up of (MAJOR-MODE FORM). FORM is evaluated when the
buffer is formatted and its return value serves two roles:

1. It is a predicate for this formatter. If it returns non-nil (and MAJOR-MODE
   matches the current mode), that formatter is used.
2. Its return value is stored in the `mode-result' variable for FORMATTER (if
   it's a function).

FORMATTER can be a function, string or nested list.


    :ok-statuses '(0 1)
    :install '(macos \"brew install tidy-html5\"))

  (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\"))
      (\"-xml\" (memq major-mode '(nxml-mode xml-mode))))
    :ok-statuses '(0 1)))

  (set-formatter! 'elm-mode
    \"elm-format --yes --stdin\"
    :install '(macos \"brew install elm\")
