diff --git a/modules/editor/format/autoload/format.el b/modules/editor/format/autoload/format.el index 42642a3c3..44498cfa2 100644 --- a/modules/editor/format/autoload/format.el +++ b/modules/editor/format/autoload/format.el @@ -18,44 +18,47 @@ (cur-buffer (current-buffer)) (formatted-buffer (get-buffer-create " *apheleia-formatted*")) (indent 0)) - (with-current-buffer formatted-buffer - (erase-buffer) - (unless (featurep :system 'windows) - (setq-local coding-system-for-read 'utf-8) - (setq-local coding-system-for-write 'utf-8)) - ;; Ensure this temp buffer seems as much like the origin buffer as - ;; possible, in case the formatter is an elisp function, like `gofmt'. - (cl-loop for (var . val) - in (cl-remove-if-not #'listp (buffer-local-variables cur-buffer)) - ;; `enable-multibyte-characters' can change how Emacs reads the - ;; buffer's contents (or writes them to the formatters), which - ;; can cause errors. - unless (eq var 'enable-multibyte-characters) - do (set (make-local-variable var) val)) - ;; - (insert-buffer-substring-no-properties cur-buffer start end) - ;; Since we're piping a region of text to the formatter, remove any - ;; leading indentation to make it look like a file. - (setq indent (+format--current-indentation)) - (when (> indent 0) - (indent-rigidly (point-min) (point-max) (- indent))) - ;; - (apheleia-format-buffer - command - (lambda () - (with-current-buffer formatted-buffer - (when (> indent 0) - ;; restore indentation without affecting new indentation - (indent-rigidly (point-min) (point-max) - (max 0 (- indent (+format--current-indentation))))) - (set-buffer-modified-p nil)) - (with-current-buffer cur-buffer - (delete-region start end) - (goto-char start) - (save-excursion - (insert-buffer-substring-no-properties formatted-buffer) - (when callback (funcall callback))) - (kill-buffer formatted-buffer))))))) + (unwind-protect + (with-current-buffer formatted-buffer + (erase-buffer) + (unless (featurep :system 'windows) + (setq-local coding-system-for-read 'utf-8) + (setq-local coding-system-for-write 'utf-8)) + ;; Ensure this temp buffer seems as much like the origin buffer as + ;; possible, in case the formatter is an elisp function, like `gofmt'. + (cl-loop for (var . val) + in (cl-remove-if-not #'listp (buffer-local-variables cur-buffer)) + ;; `enable-multibyte-characters' can change how Emacs reads the + ;; buffer's contents (or writes them to the formatters), which + ;; can cause errors. + unless (eq var 'enable-multibyte-characters) + do (set (make-local-variable var) val)) + ;; + (insert-buffer-substring-no-properties cur-buffer start end) + ;; Since we're piping a region of text to the formatter, remove any + ;; leading indentation to make it look like a file. + (setq indent (+format--current-indentation)) + (when (> indent 0) + (indent-rigidly (point-min) (point-max) (- indent))) + ;; + (apheleia-format-buffer + command + (lambda () + (with-current-buffer formatted-buffer + (when (> indent 0) + ;; restore indentation without affecting new indentation + (indent-rigidly (point-min) (point-max) + (max 0 (- indent (+format--current-indentation))))) + (set-buffer-modified-p nil)) + (with-current-buffer cur-buffer + (delete-region start end) + (goto-char start) + (save-excursion + (insert-buffer-substring-no-properties formatted-buffer) + (when callback (funcall callback))) + (kill-buffer formatted-buffer))))) + (when (doom-region-active-p) + (setq deactivate-mark t))))) ;;