Subject: bug#32278: 27.0.50; replace-buffer-contents calls change functions with wrong arguments

From: Michał Kondraciuk <[email-masked]>
Date: Fri, 27 Jul 2018 13:24:30 +0200

On 07/27/2018 12:06 PM, Eli Zaretskii wrote:
> Thanks.  I just threw away the attempt to be smarter about where the
> changes are done, and went back to the original code that announces
> changes in the entire region.
> 
> If anyone wants to add smarter code, they should do this on master.

Thanks. Can you tell me if the workaround below (calling change functions manually) is 
correct/safe for emacs versions where this is not fixed? It's a function that 
non-destructively replaces region contents.

(defun my-replace-region (beg end text)
   "Replace region BEG END with TEXT.
As far as possible the replacement is non-destructive."
   (let ((source (current-buffer)))
     (with-temp-buffer
       (insert text)
       (let ((replacement (current-buffer)))
         (with-current-buffer source
           (save-restriction
             (widen)
             (narrow-to-region beg end)
             (let ((inhibit-modification-hooks t))
               (run-hook-with-args 'before-change-functions beg end)
               (replace-buffer-contents replacement)
               (run-hook-with-args 'after-change-functions beg
                                   (+ beg (length text)) (- end beg)))))))))