;;; +apheleia.el -*- lexical-binding: t; -*- ;;; Code: (require 'cl-lib) (require 'el-patch) (require 'user-save) ;; 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))) ;;; Why does the original function have to check for `apheleia-mode' ? (el-patch-defun apheleia--format-after-save () "Run code formatter for current buffer if any configured, then save." (unless apheleia--format-after-save-in-progress (when (el-patch-swap apheleia-mode (or apheleia-mode +apheleia/user-save-mode)) (when-let ((formatters (apheleia--get-formatters))) (apheleia-format-buffer formatters (lambda () (with-demoted-errors "Apheleia: %s" (when buffer-file-name (let ((apheleia--format-after-save-in-progress t)) (apheleia--save-buffer-silently))) (run-hooks 'apheleia-post-format-hook)))))))) (define-minor-mode +apheleia/user-save-mode "Minor mode for reformatting code on `user-save'. Customize with `apheleia-mode-alist' and `apheleia-formatters'." :lighter " Apheleia/US" (if +apheleia/user-save-mode (add-hook 'user-save-after-save-hook #'apheleia--format-after-save nil 'local) (remove-hook 'user-save-after-save-hook #'apheleia--format-after-save 'local))) (define-globalized-minor-mode +apheleia/user-save-global-mode +apheleia/user-save-mode +apheleia/user-save-mode) (put '+apheleia/user-save-mode 'safe-local-variable #'booleanp) (provide '+apheleia) ;;; +apheleia.el ends here