From cd79edf134ed03abbc3190bb56d22c229e011bc9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ellis=20Keny=C5=91?= Date: Sun, 14 Aug 2022 16:26:33 +0100 Subject: [PATCH] feat(format): add :lang emacs-lisp formatter Introduce a formatter function for emacs-lisp (tracked upstream here [1]) that tries to preserve as much of what the user wants as possible [1]: https://github.com/radian-software/apheleia/pull/102 --- modules/editor/format/config.el | 19 +++++++++++++++++-- modules/lang/emacs-lisp/config.el | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/modules/editor/format/config.el b/modules/editor/format/config.el index 1075a98f2..d08e51373 100644 --- a/modules/editor/format/config.el +++ b/modules/editor/format/config.el @@ -49,6 +49,21 @@ select buffers.") ;; ;;; Additional formatters -(after! apheleia-mode +(after! apheleia ;; TODO html-tidy - ) + (cl-defun apheleia--indent-lisp-buffer + (&key buffer scratch callback &allow-other-keys) + "Format a Lisp BUFFER. Use SCRATCH as a temporary buffer and CALLBACK to +apply the transformation. For more implementation detail, see +`apheleia--run-formatter-function'." + (with-current-buffer scratch + (setq-local indent-line-function + (buffer-local-value 'indent-line-function buffer)) + (setq-local lisp-indent-function + (buffer-local-value 'lisp-indent-function buffer)) + (funcall (with-current-buffer buffer major-mode)) + (goto-char (point-min)) + (let ((inhibit-message t) + (message-log-max nil)) + (indent-region (point-min) (point-max))) + (funcall callback)))) diff --git a/modules/lang/emacs-lisp/config.el b/modules/lang/emacs-lisp/config.el index b089ff53c..609168518 100644 --- a/modules/lang/emacs-lisp/config.el +++ b/modules/lang/emacs-lisp/config.el @@ -39,6 +39,7 @@ See `+emacs-lisp-non-package-mode' for details.") :documentation #'+emacs-lisp-lookup-documentation) (set-docsets! '(emacs-lisp-mode lisp-interaction-mode) "Emacs Lisp") (set-ligatures! 'emacs-lisp-mode :lambda "lambda") + (set-formatter! 'lisp-indent #'apheleia--indent-lisp-buffer :modes '(emacs-lisp-mode)) (set-rotate-patterns! 'emacs-lisp-mode :symbols '(("t" "nil") ("let" "let*")