Subject: Re: bug#33567: Syntactic fontification of diff hunks

From: Juri Linkov <[email-masked]>
Date: Wed, 12 Dec 2018 02:28:08 +0200

>  . it doesn't use the following facilities for determining the right
>    encoding, where you use buffer-file-coding-system:
>     - auto-coding-function, which is where we detect the 'coding:'
>       cookies in the first line and in the local vars, and use the
>       data in auto-coding-alist and auto-coding-regexp-alist, and also
>       call auto-coding-functions if needed
>     - find-operation-coding-system by file name, which uses the data
>       in file-coding-system-alist to determine the appropriate
>       encoding given the file's name
>
> The hard problem here is to determine what coding-system to use for
> decoding a region that was inserted without any conversions; once the
> encoding is determined, the rest boils down to calling
> decode-coding-region with that encoding.  The method used by
> archive-set-buffer-as-visiting-file solves that very problem, whereas
> recode-region does not, because it is a command that relies on the
> caller to specify the encoding, and is otherwise nothing more than a
> thin wrapper around decode-coding-region.

Thanks for the explanation.  I explored more on this subject, and found
the most suitable existing function: `decode-coding-inserted-region'.
I tested it with different codings and everything works well:

diff --git a/lisp/vc/vc.el b/lisp/vc/vc.el
index 5ff9f4d5be..127661a039 100644
--- a/lisp/vc/vc.el
+++ b/lisp/vc/vc.el
@@ -2042,6 +2042,8 @@ vc-find-revision-no-save
 		      (if backend
 			  (vc-call-backend backend 'find-revision file revision outbuf)
 			(vc-call find-revision file revision outbuf))))
+                  (decode-coding-inserted-region (point-min) (point-max) file)
+                  (after-insert-file-set-coding (- (point-max) (point-min)))
                   (goto-char (point-min))
                   (if buffer (let ((buffer-file-name file)) (normal-mode)) (normal-mode))
 	          (set-buffer-modified-p nil)