projectile icon indicating copy to clipboard operation
projectile copied to clipboard

projectile-mode-line-function doesn't play nicely with magit

Open mx opened this issue 2 years ago • 6 comments

I have a custom projectile-mode-line-function that puts PR[name] in the modeline when there's an active project, and nothing at all in the modeline when there isn't. Normally this works fine. However, when in the magit status buffer for the project, it leaves "Projectile" in the modeline. This is pretty interesting, because (projectile-project-name) still returns the correct name of the project (in my case, "surge").

Here is the function I'm using, and how it gets configured:

  (setq projectile-mode-line-function
	(lambda ()
	  (let ((name (projectile-project-name)))
	    (when (not (string= name "-"))
	      (format " PR[%s]" name)))))

Expected behavior

In the modeline in the magit status buffer for this project, you should see "PR[surge]"

Actual behavior

What you see in the modeline in the magit status buffer is "Projectile".

Steps to reproduce the problem

Set the above function to your projectile-mode-line-function. Don't have any other custom settings for projectile. Run magit status in an active/open project.

Environment & Version information

Projectile version information

Projectile version: 20230224.2116

Emacs version

28.2

Operating system

Arch Linux.

mx avatar Feb 26 '23 21:02 mx

As updating the mode-line on each keystroke (what most modes do) might be pretty slow for something like Projectile, the mode-line is updated only by find-file-hook. See https://github.com/bbatsov/projectile/issues/1232 for context and the changes I made to address it. If you revert locally some of them it will result in the behavior you're looking for.

bbatsov avatar Feb 26 '23 22:02 bbatsov

Thanks for the info. Looks like explicitly calling (projectile-update-mode-line) will do the right thing. So I can just add a hook to the appropriate spot in magit (I used magit-status-sections-hook; magit-status-refresh-hook did not work):

(add-hook 'magit-status-sections-hook #'projectile-update-mode-line)

Not really sure if there's anything Projectile should do here to support magit automatically, or if it should be left to the user like this.

mx avatar Feb 26 '23 22:02 mx

Probably we can just write some documentation about integration with modes like Magit.

bbatsov avatar Feb 26 '23 22:02 bbatsov

Sounds good. I think the cleanest way to integrate it with magit, since magit uses the hooks for actual display purposes, would be this:

(advice-add 'magit-status :after
	    (lambda (&rest _) (projectile-update-mode-line))
	    '((name . "projectile-modeline")))

mx avatar Feb 26 '23 22:02 mx

Sorry for the blizzard of messages. This possibility might be better

(add-hook 'window-configuration-change-hook #'projectile-update-mode-line)

Thus, updating the modeline whenever a buffer appears in a window when it wasn't already displayed. Should work for any mode that doesn't actually open files (which includes magit). Still isn't running on every keypress, so hopefully the original speed issue with TRAMP and friends wouldn't reappear here.

mx avatar Feb 26 '23 22:02 mx

@mx Sounds like a good idea to me. PR welcome!

bbatsov avatar Feb 27 '23 06:02 bbatsov