dgutov/diff-hl

Activate diff-hl-mode in file buffers using `project.el`

josephmturner opened this issue · 8 comments

Thank you for this useful package!

The Commentary section suggests activating diff-hl-mode globally or inside prog-mode buffers with (add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)

I'd rather not enable diff-hl-mode globally, since the margin in the terminal takes up space in buffers where I don't need diff-hl-mode. The prog-mode hook doesn't enable diff-hl-mode in non-prog-mode buffers which are managed by git (like ledger files).

Is there a way to check if a file is version-controlled? That might serve as a better hook than prog-mode. In lieu of a hook like that, I thought it might work to turn on diff-hl-mode in file buffers inside a project.

My elisp-fu is weak, and my attempt doesn't work:

(add-hook 'find-file-hook
          '(lambda () (if (project-current) 'turn-on-diff-hl-mode)))

Any advice would be appreciated. Thank you!

How about

(if (vc-backend buffer-file-name) ...)

?

And you can also remove the quote before (lambda.

Thank you for your help! I keep running into trouble, and I think that the issue lies somewhere besides the conditional that I wrote.

As a simpler example, I put the following in test.el and ran emacs -Q --load test.el:

(require 'diff-hl)
(add-hook 'prog-mode-hook 'turn-on-diff-hl-mode)

I navigate to git-managed file, make a change, and save the file. Nothing shows up in the fringe. If I run (diff-hl-mode), then the changes appear.

I'm testing this in GUI Emacs 27.2. Am I missing something super obvious?

I navigate to git-managed file, make a change, and save the file

What kind of file do you navigate to? Does its major mode derive from prog-mode (which would normally be the case for programming source code files)?

If not, that's the expected behavior because you used prog-mode-hook.

You're right. I must have made a mistake like that, because the config from my previous comment is working fine.
If you have a moment, please try the following with emacs -Q:

(require 'diff-hl)
(add-hook 'find-file-hook (lambda () (if (vc-backend buffer-file-name) (message "hi"))))
(add-hook 'find-file-hook (lambda () (if (vc-backend buffer-file-name) 'turn-on-diff-hl-mode)))

After open a file that's git-managed, I see the "hi" message, but diff-hl-mode is off. Does this config work for you?

Here's the problem: you need to call turn-on-diff-hl-mode. Not just return its symbol. Missed that previously.

Replace 'turn-on-diff-hl-mode with (turn-on-diff-hl-mode).

Success!! Thank you for your patience. If you like, I can submit a PR suggesting this hook in the Commentary section.

It's okay for now. Let's see if others have similar requests first.