Merge pull request #2388 from Janfel/patch-1

Fix #2036: Copy local variables into formatting buffer.
This commit is contained in:
Henrik Lissner 2020-01-23 01:37:16 -05:00 committed by GitHub
commit 4e28e3b840
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -131,15 +131,18 @@ See `+format/buffer' for the interactive version of this function, and
;; 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.
(let ((output (buffer-substring-no-properties (point-min) (point-max)))
(origin-buffer-file-name (buffer-file-name (buffer-base-buffer)))
(origin-default-directory default-directory))
(origin-buffer (or (buffer-base-buffer) (current-buffer))))
(with-temp-buffer
(with-silent-modifications
(insert output)
;; Ensure this temp buffer _seems_ as much like the origin
;; buffer as possible.
(setq default-directory origin-default-directory
buffer-file-name origin-buffer-file-name)
(cl-loop for (var . val) in (buffer-local-variables origin-buffer)
;; 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
;; any leading indentation to make it look like a file.
(when preserve-indent-p