- (copy-to-buffer copy-buf from to)
- (set-buffer copy-buf)
- (format-insert-annotations write-region-annotations-so-far from)
- (format-encode-run-method to-fn (point-min) (point-max) orig-buf)
+ (let ((inhibit-read-only t)) ; bug#14887
+ (copy-to-buffer copy-buf from to)
+ (set-buffer copy-buf)
+ (format-insert-annotations write-region-annotations-so-far from)
+ (format-encode-run-method to-fn (point-min) (point-max)
+ orig-buf))
+ (when (buffer-live-p copy-buf)
+ (with-current-buffer copy-buf
+ ;; Set write-region-post-annotation-function to
+ ;; delete the buffer once the write is done, but do
+ ;; it after running to-fn so it doesn't affect
+ ;; write-region calls in to-fn.
+ (set (make-local-variable
+ 'write-region-post-annotation-function)
+ 'kill-buffer)))