8

Projectile is pretty great, but I'm finding it difficult to have it turned on only in places where it would be effective.

As I understand it, the two standard choices are to turn it on manually, as in major mode hooks, or to enable it globally with projectile-global-mode. I find the former not broad enough; I want to be able to access projectile commands while visting any file in a project, regardless of its type. But turning the mode on globally is too broad; it gets turned on even when visiting such manifestly non-project directories as my home directory, /tmp, /etc, etc.

Is there a standard way to activate projectile-mode only when visiting a file that lives in an actual project, as might be determined by, say, checking for the existence of a .git, .svn, etc, directory in some ancestor directory of the current file?

Sean
  • 929
  • 4
  • 13
  • Checking for the existence of `.git`, `.svn`... should be the default. Maybe you have having `projectile-require-project-root` setting to `nil`. – Tu Do Apr 02 '15 at 04:44
  • I just checked, and its value is `t`. But the mode is still activated in `/etc` for example. – Sean Apr 02 '15 at 05:10
  • 1
    What's your Projectile version? When you run `projectile-find-files` on such non-project directories, Projectile simply gives you a message "You're no on a project". Are you sure you run the right command? – Tu Do Apr 02 '15 at 05:13
  • 2
    Ah, OK, I do see that. But I still see `Projectile[etc]` in the mode line when I open `/etc`. It takes up valuable mode line real estate, and I'd rather just not activate the mode at all in non-project directories. – Sean Apr 02 '15 at 05:29

1 Answers1

11

So, it's about the modeline. You can set projectile-mode-line to check if you are in a project root; if so, displays in the modeline, otherwise displays nothing:

(setq projectile-mode-line
      '(:eval (if (projectile-project-p)
                  (format " Projectile[%s]"
                          (projectile-project-name))
                "")))
xuchunyang
  • 14,302
  • 1
  • 18
  • 39
Tu Do
  • 6,772
  • 20
  • 39
  • 4
    That change should be in projectile itself. – abo-abo Apr 02 '15 at 06:41
  • This is definitely useful, but the mode would still technically be active, right? Ideally the mode would not even be activated in such cases. – Sean Apr 02 '15 at 06:55
  • 2
    @Sean what's for? There's a reason it's a global minor mode. Is there something get in the way if the global mode is active, aside from the modeline? When the global mode is active, all it does is giving you the Projectile key bindings. – Tu Do Apr 02 '15 at 07:22
  • 2
    @abo-abo I also think it should work like this rather than then showing the current `default-directory`. I will make a PR. – Tu Do Apr 02 '15 at 07:24
  • I put this in my init file, and eventually noticed a ton of errors in the `*Messages*` buffer, because `projectile-project-root` raises an exception if called from a non-project directory. So that function call needs to be wrapped in a `condition-case`, and also `projectile-require-project-root` needs to be bound to `t`. – Sean Apr 10 '15 at 21:49
  • @Sean it's done – Tu Do Apr 11 '15 at 04:50