Skip to content

Commit

Permalink
Use replace-buffer-contents to reformat buffer
Browse files Browse the repository at this point in the history
According to `replace-buffer-contents` documentation this function
replaces buffer contents in smarter way. Especially interesting part:

    As far as possible the replacement is non-destructive,
    i.e. existing buffer contents, markers, properties, and overlays
    in the current buffer stay intact.

One example where some properties are preserved is when origami-mode
is used on the buffer to collapse some regions. After this change
collapsed regions stay collapsed after reformatting
  • Loading branch information
BlankSpruce committed Feb 19, 2021
1 parent 4aa0ff6 commit 074d6e6
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions format-all.el
Original file line number Diff line number Diff line change
Expand Up @@ -1077,9 +1077,11 @@ LANGUAGE is the language ID of the current buffer, from
((not (equal f-output t))
(setq reformatted-by
(append reformatted-by (list f-name)))
(let ((inhibit-read-only t))
(erase-buffer)
(insert f-output))
(let ((helper-buffer (generate-new-buffer "*format-all-output*")))
(with-current-buffer helper-buffer
(insert f-output))
(replace-buffer-contents helper-buffer)
(kill-buffer helper-buffer))
(setq f-status :reformatted)))
(run-hook-with-args 'format-all-after-format-functions
f-name f-status)
Expand Down

0 comments on commit 074d6e6

Please sign in to comment.