projectile icon indicating copy to clipboard operation
projectile copied to clipboard

projectile-grep-finished-hook is only called once, then ignored on subsequent calls

Open dadinn opened this issue 3 years ago • 2 comments

I am trying to configure projectile to jump cursor to the popped grep buffer by setting the projectile-grep-finished-hook.

Expected behavior

I would expect the projectile grep buffer to pop up, and become active by moving the the cursor over to it every time.

Actual behavior

The projectile grep buffer pops up, but it doesn't become active (i.e. the cursor stays on the source buffer).

Steps to reproduce the problem

I use the following config in my init.el via use-package:

(use-package projectile
  :hook
  (projectile-grep-finished . (lambda () (message "HOOK CALLED!") (pop-to-buffer next-error-last-buffer)))
  :bind-keymap
  ("C-c p" . projectile-command-map)
  :config (projectile-mode))

After I start up a fresh new Emacs instance, I can confirm that the projectile-grep-finished-hook has applied my config from the init file.

I then open my init.el file, and try grepping (keymap C-c p s g) for the word "projectile". The projectile grep buffer pops up, and it becomes the active buffer, with the cursor moved over to it. I can verify in the *Messages* buffer that the hook function has been called, by seeing the echo message "HOOK CALLED!" printed. This is the expected behaviour, and it works correctly the first time after a new Emacs instance is started.

I then close down the grep buffer by pressing q, and try to repeat the same grepping instructions again. This time the buffer pops up, but it doesn't become active, and the cursor stays in the original source buffer. The Messages buffer doesn't show the expected echo message, instead I only see the following:

apply: Buffer name ‘*grep </home/dadinn/.emacs.d/>*’ is in use
Grep finished with 4204 matches found

The large number of matches is due to other files in my .emacs.d folder, including a clone of this repository itself (as I'm using straight.el as package manager).

Environment & Version information

Projectile version information

Projectile version: 2.4.0 projectile commit-hash: 5e6fdabd59ec5507c1d54f3b11ee16ddc05821d3

Emacs version

GNU Emacs 27.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.24, cairo version 1.16.0) of 2021-03-27, modified by Debian

Operating system

Debian Bullseye (testing)

dadinn avatar Jul 07 '21 11:07 dadinn

I have the same problems (grep buffer not being selected and error about buffer name) with Projectile version 20210811.435.

For me, the following hack seems to fix both problems.

(defvar *nomis/in-projectile-grep?* nil)

(advice-add
 'projectile-grep
 :around
 (lambda (orig-fun &rest args)
   (let* ((*nomis/in-projectile-grep?* t))
     (apply orig-fun args)))
 '((name . nomis/hack-projectile-grep)))

(advice-add
 'rename-buffer
 :around
 (lambda (orig-fun newname &optional unique)
   (when *nomis/in-projectile-grep?*
     (pop-to-buffer "*grep*"))
   (funcall orig-fun
            newname
            (if *nomis/in-projectile-grep?* t unique)))
 '((name . nomis/hack-projectile-grep)))

simon-katz avatar Sep 30 '21 21:09 simon-katz

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!

stale[bot] avatar Apr 16 '22 10:04 stale[bot]