Fix #2036: Copy local variables into formatting buffer.

Make +format-buffer copy all buffer-local variables from the original buffer into the temporary buffer. When resolving a symbol inside of a formatter, the value it is bound to in the formatted buffer will now be used instead of the default value. Fixes #2036.
This commit is contained in:
Janfel 2020-01-19 02:50:44 +01:00 committed by Jan Felix Langenbach
parent 787f656820
commit 2e82f297f6

View file

@ -131,6 +131,7 @@ See `+format/buffer' for the interactive version of this function, and
;; like `gofmt') widen the buffer, in order to only format a region of ;; like `gofmt') widen the buffer, in order to only format a region of
;; text, we must make a copy of the buffer to apply formatting to. ;; text, we must make a copy of the buffer to apply formatting to.
(let ((output (buffer-substring-no-properties (point-min) (point-max))) (let ((output (buffer-substring-no-properties (point-min) (point-max)))
(origin-buffer (or (buffer-base-buffer) (current-buffer)))
(origin-buffer-file-name (buffer-file-name (buffer-base-buffer))) (origin-buffer-file-name (buffer-file-name (buffer-base-buffer)))
(origin-default-directory default-directory)) (origin-default-directory default-directory))
(with-temp-buffer (with-temp-buffer
@ -138,8 +139,12 @@ See `+format/buffer' for the interactive version of this function, and
(insert output) (insert output)
;; Ensure this temp buffer _seems_ as much like the origin ;; Ensure this temp buffer _seems_ as much like the origin
;; buffer as possible. ;; buffer as possible.
(setq default-directory origin-default-directory (cl-loop for (var . val) in (buffer-local-variables origin-buffer)
buffer-file-name origin-buffer-file-name) ;; Making enable-multibyte-characters buffer-local
;; causes an error.
unless (eq var 'enable-multibyte-characters)
;; Using setq-local would quote var.
do (set (make-local-variable var) val))
;; Since we're piping a region of text to the formatter, remove ;; Since we're piping a region of text to the formatter, remove
;; any leading indentation to make it look like a file. ;; any leading indentation to make it look like a file.
(when preserve-indent-p (when preserve-indent-p