4

I have a keybinding 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 keybinding 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 keybinding.

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

7

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.

  • 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 5 '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 5 '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 5 '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 5 '15 at 10:51
  • @ReneFrogertjuh how do you add? Btw, did the edit solve the problem for you? – Tu Do Jun 5 '15 at 10:56
6

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.