1

I am trying to make projectile-multi-occur to work with the symbol at point.

I have done that for occur already, this works:

(defun occur-symbol-at-point ()
  (interactive)
  (let ((sym (thing-at-point 'symbol)))
    (funcall 'occur sym)))

So now I tried to do the exact same thing with projectile-multi-occur, this does NOT work:

(defun projectile-multi-occur-symbol-at-point ()
  (interactive)
  (let ((sym (thing-at-point 'symbol)))
    (funcall 'projectile-multi-occur sym)))

Calling projectile-multi-occur with M-x works as I expect.

With the function above, I only get an *Occur* buffer, with only the search term, the same amount of times as there are hits.

What is wrong with my function definition?

Drew
  • 75,699
  • 9
  • 109
  • 225
Gauthier
  • 499
  • 2
  • 13

1 Answers1

1

Check the documentation for projectile-multi-occur:

projectile-multi-occur is an autoloaded, interactive and byte-compiled
function defined in projectile.el.

Signature
(projectile-multi-occur &optional NLINES)

Documentation
Do a multi-occur in the project's buffers.

With a prefix argument, show NLINES of context.

You’ll see that the only argument it accepts is the number of lines of context to show.

Edit: Since you asked how you might accomplish this even though projectile-multi-occur doesn’t take the argument you wanted to supply, I recommend reading the source of projectile-multi-occur:

(defun projectile-multi-occur (&optional nlines)
  "Do a `multi-occur' in the project's buffers.
With a prefix argument, show NLINES of context."
  (interactive "P")
  (let ((project (projectile-acquire-root)))
    (multi-occur (projectile-project-buffers project)
                 (car (occur-read-primary-args))
                 nlines)))

This is very straight–forward. The second argument to multi-occur is the regexp to search for, so you could just call this yourself, supplying the thing at point instead:

(defun projectile-multi-occur-symbol-at-point ()
  (interactive)
  (let ((sym (thing-at-point 'symbol))
        (project (projectile-acquire-root)))
    (multi-occur (projectile-project-buffers project)
                 sym
                 nlines)))

Just as a side note, you don’t need to use funcall in occur-symbol-at-point either. (funcall 'occur sym) is exactly equivalent to writing (occur sym).

db48x
  • 15,741
  • 1
  • 19
  • 23
  • It also says "Do a multi-occur", and a multi-occur requires the string to be searched. I wrongly expected it to be replaceable as is, thanks. Then the follow-up question is: can I make the function call to `projectile-multi-occur` use the symbol at point as default input? – Gauthier Feb 25 '22 at 10:48
  • Thanks for the edit, this works. This is also simple enough to skip the `let` altogether. Now I just need to make it work when I replace `projectile-project-buffers project` with `projectile-current-project-files`, which somehow results in "Searched 0 buffers (11255 killed); no matches for xxxx". – Gauthier Feb 25 '22 at 11:32