Subject: bug#33567: Syntactic fontification of diff hunks

From: Eli Zaretskii <[email-masked]>
Date: Tue, 04 Dec 2018 09:36:55 +0200

> From: Juri Linkov <[email-masked]>
> Cc: [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: "..." -*-

I'm not sure this is a step in the right direction.  What is the
advantage of having a separate variable?  How is it "safer"?

The advantage of default-directory is that it is well-known, and the
command to change it, 'cd', is easier to type and invoke than setting
diff-default-directory manually.

> 
> >> > 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.

I did, but I guess this must be done inside the repository to work,
does it?  If I put the output of "git diff" on a file in some
arbitrary directory, then visit that file and evaluate
(diff-find-file-name nil t), I get nil.

> >> +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.

We need to describe the implications of that to the users.  Does the
following text capture the issue?

  For diffs against the working-tree version of a file, the
  highlighting is based on the current file contents, which could be
  different from the contents when the diffs were taken.  In such
  cases, the produced highlighting might be wrong.

> >> +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.

If so, I suggest the following wording:

  If t, try to infer fontification from the compared files, if they
  exist in the filesystem, when accessing their contents via VC
  fails.

> > 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.

There's no such text in the description of 'vc', only in the
description of t, which is why I asked.