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

From: Juri Linkov <[email-masked]>
Date: Tue, 04 Dec 2018 01:59:07 +0200

>> As a general solution is should be possible to specify the default
>> directory in the local variables at the first line of the diff files
>> as currently already is used in compilation/grep buffers like
>> 
>> -*- mode: diff-mode; default-directory: "..." -*-
>
> This is all fine, but I think we should document that files are
> visited relative to default-directory of the buffer, so that users
> could invoke "cd" to change that as needed.

For more safety, I propose to set a new buffer-local variable
`diff-default-directory' by such commands as diff, diff-backup,
dired-diff, dired-backup-diff.  The existence of such variable
should guarantee that the referenced files really exist.
This variable will be like `diff-vc-backend' that says that
the diff-mode buffer is created by the VCS command.
Then anyone who want to visit a diff file in another directory,
could add it to the first line:

-*- mode: diff-mode; diff-default-directory: "..." -*-

>> > Also, if the diffs are from Git, they begin with a/, b/, etc. dummy
>> > directories, which usually don't exist in the file system.
>> 
>> This is not a problem because diff-find-file-name used in the patch
>> strips such a/, b/ prefixes to get the existing file name.
>
> Not in my testing, but maybe I tried in the wrong Emacs version.  Is
> this feature new with Emacs 27?

For testing better try to eval e.g. `(diff-find-file-name nil t)'
on a hunk in a diff-mode buffer created by git.

>> +(defcustom diff-font-lock-syntax 'vc
>> +  "If non-nil, diff hunk's font-lock includes language syntax highlighting.
>> +This highlighting is the same as added by `font-lock-mode' when
>> +corresponding source files are visited from the diff buffer.
>
> Thanks, this is much better than the original text, but there are
> still unclear corners.  One such corner is the "visited from the diff
> buffer" part -- what is its significance?  Can we just say "when the
> corresponding source files are visited normally"?

Changed, will send a patch with more changes later.

>> +In diff hunks syntax highlighting is added over diff own
>> +highlighted changes.
>
> What is the significance of the "In diff hunks" part here?  Apart of
> diff hunks, we have just headers, where this feature is irrelevant,
> right?

Removed "In diff hunks".

>> +If `vc', highlight syntax only in Diff buffers created by a version control
>> +system that provides all necessary context for reliable highlighting.
>
> I would use "in Diff buffers created by VC commands" instead.  I would
> also add this text (assuming it is correct):
>
>   This value requires support from a VC backend to find the files
>   being compared.
>
> This should tell users that they could in principle set up things
> manually even for buffers that were not created by VC commands.

Changed.

> Please also indicate that `vc' is the default.

After adding another safe option that uses `diff-default-directory',
we could combine it with `vc' for the safe default.

>> +For working revisions get highlighting according to the working
>> +copy of the file.
>
> I don't understand the significance of this comment.  If you want to
> say that the produced highlighting might be wrong if the working
> version has changed since it was compared, then let's say that
> explicitly.

This means that working revisions can't be extracted from the repository.
Until committed, they reside in files that are visited with find-file.

>> +If t, additionally to trying to use a version control system to get
>> +old revisions for fontification, also try to get fontification based
>> +on existing files, and on failure get fontification from hunk alone."
>
> What is the difference between using a VCS to get old revisions, and
> using existing files?

This means that when a diff-mode buffer is not created by a VCS,
then it tries to read files with find-file.

> Also, does it mean `vc' will not fall back to `hunk-only'?  Why not?

Actually, it already falls back to `hunk-only', this is what
"on failure get fontification from hunk alone." tries to say.