6

I have a key binding that's bounded to helm-for-files.

(define-key evil-normal-state-map (kbd "<f9>") 'helm-for-files)

But when I enter a directory which is a project (to being recognized with .projectile file), then I would like to change the key binding to the following:

(define-key evil-normal-state-map (kbd "<f9>") 'helm-projectile).

But when I enter another directory without any project files, then revert back to the default helm-for-files key binding.

I looked if there a hook for Projectile exists, but there isn't any hook available. Any other suggestions, to detect if I'm currently in a Projectile directory?

Drew
  • 77,472
  • 10
  • 114
  • 243
ReneFroger
  • 3,850
  • 23
  • 66

2 Answers2

8

You can do it like this:

(defun contextual-f9 ()
  (if (projectile-project-p) ;; detect if current buffer is in a project
      (define-key evil-normal-state-map (kbd "<f9>") 'helm-projectile)
    (define-key evil-normal-state-map (kbd "<f9>") 'helm-for-files)))

(add-hook 'find-file-hook #'contextual-f9)
(add-hook 'dired-mode-hook #'contextual-f9)
(add-hook 'window-configuration-change-hook #'contextual-f9)

With window-configuration-change-hook, it is possible to update the keymap if you change window configuration, since it is always the case if you use Helm to switch buffer.

Another simpler solution is that you can just use helm-for-files, but add Helm Projectile sources to helm-for-files-preferred-list:

(add-to-list 'helm-for-files-preferred-list helm-source-projectile-projects)
(add-to-list 'helm-for-files-preferred-list helm-source-projectile-files-list)
(add-to-list 'helm-for-files-preferred-list helm-source-projectile-directories-list)

When not in a project, you will get a normal helm-find-files and when in a project, you get candidates from Helm Projectile.

Tu Do
  • 6,812
  • 21
  • 39
  • Thanks Tu Do, it works, but not completely. When I switch back to a buffer, which is not in a project, I still got helm-projectile when pressing <f9>? I'm asking it, because helm-for-files-preffered-list will not work with projectile sources if I'm not inside a project. – ReneFroger Jun 05 '15 at 10:00
  • @ReneFrogertjuh yes, Helm Projectile should not work when you are outside of a project. That's the intended behaviour, since projectile is used for navigating a project. – Tu Do Jun 05 '15 at 10:26
  • Or, are you getting some error from Helm Projectile that prevents you from running helm-for-files? EDIT: I updated the answer to use window-configuration-change-hook. With that hook, it should work when you switch buffer with Helm. – Tu Do Jun 05 '15 at 10:29
  • I agree with your comments and edits, thanks for your addition. Integrate projectile-sources into helm-for-files would be ideal. But I wonder why I can't add helm-source-projectile-buffer-list, hence my question to switch the keybindings. – ReneFroger Jun 05 '15 at 10:51
  • @ReneFrogertjuh how do you add? Btw, did the edit solve the problem for you? – Tu Do Jun 05 '15 at 10:56
  • I add it like this: http://pastebin.com/U47gNBd1 , every source works fine, even in non-project directories, except helm-source-projectile-buffer-list. I tried your edit, but I prefer my solution, because it allows you to order your sources list in helm-for-files more consistently. – ReneFroger Jun 05 '15 at 12:28
8

Actually you can do it by using a wrapper of those two functions:

(defun smart-for-files ()
  "Call `helm-projectile' if `projectile-project-p', otherwise
fallback to `helm-for-files'."
  (interactive)
  (if (projectile-project-p)
      (helm-projectile)
    (helm-for-files)))

then set the keybinding as you want:

(define-key evil-normal-state-map (kbd "<f9>") 'smart-for-files)
zyguan
  • 81
  • 1
  • 2