jwiegley / use-package

A use-package declaration for simplifying your .emacs
https://jwiegley.github.io/use-package
GNU General Public License v3.0
4.39k stars 259 forks source link

Setting `:after (projectile)` for counsel-projectile makes projectile not load on keybind pressed. #976

Closed AlynxZhou closed 2 years ago

AlynxZhou commented 2 years ago

I have the following code in init.el:

(use-package counsel
  :ensure t
  :config
  (counsel-mode 1)
  :bind (("C-c g" . counsel-git)
     ("C-c j" . counsel-git-grep)
     ("C-c k" . counsel-rg)
     ("C-c l" . counsel-locate)
     ;; ("C-S-o" . counsel-rhythmbox)
         :map minibuffer-local-map
         ("C-r" . counsel-minibuffer-history)))

(use-package counsel-projectile
  :ensure t
  :after (counsel projectile)
  :config
  (counsel-projectile-mode 1))

;; others

(use-package projectile
  :ensure t
  :config
  (projectile-mode 1)
  :bind (:map projectile-mode-map
              ("C-c p" . projectile-command-map)
              ("M-s" . projectile-ripgrep))
  :custom
  (projectile-cache-file
   (locate-user-emacs-file ".local/cache/projectile.cache"))
  (projectile-known-projects-file
   (locate-user-emacs-file ".local/projectile-bookmarks.eld")))

Because I put counsel-projectile before projectile, I use :after to make it loaded after projectile. But with those config, after I start Emacs and press C-c p, projectile is not loaded, but the internal project keybindings is shown by which-key.

If I comment out :after (counsel projectile), projectile is loaded correctly, but my custom for projectile won't be used for counsel-projectile, because it is loaded before projectile.

AlynxZhou commented 2 years ago

Sorry, my mistake, there is already document for this: https://github.com/jwiegley/use-package#binding-to-keymaps.

AlynxZhou commented 2 years ago
(use-package counsel
  :ensure t
  :config
  (counsel-mode 1)
  :bind (("C-c g" . counsel-git)
     ("C-c j" . counsel-git-grep)
     ("C-c k" . counsel-rg)
     ("C-c l" . counsel-locate)
     ;; ("C-S-o" . counsel-rhythmbox)
         :map minibuffer-local-map
         ("C-r" . counsel-minibuffer-history)))

(use-package counsel-projectile
  :ensure t
  :after (counsel projectile)
  :config
  (counsel-projectile-mode 1))

;; others

(use-package projectile
  :ensure t
  :config
  (projectile-mode 1)
  :bind (("M-s" . projectile-ripgrep))
  ;; See <https://github.com/jwiegley/use-package#binding-to-keymaps>.
  :bind-keymap (("C-c p" . projectile-command-map))
  :custom
  (projectile-completion-system 'ivy)
  (projectile-cache-file
   (locate-user-emacs-file ".local/cache/projectile.cache"))
  (projectile-known-projects-file
   (locate-user-emacs-file ".local/projectile-bookmarks.eld")))

After I changed my config to this, projectile is loaded, but counsel-projectile is still not loaded after I press C-c p, is there something wrong in my config?

AlynxZhou commented 2 years ago

OK, I think the problem is :after (counsel projectile), because I have :bind for counsel and projectile, they will be loaded after first key press, and I only after those two packages are loaded, counsel-projectile will be loaded, which means I need to press both C-c p and C-c k to load projectile and counsel, then counsel-projectile will be loaded.

To fix this I can added :demand t to counsel to make it loaded after startup, or just remove counsel in :after, since counsel-projectile only depends on projectile's custom.