zsome · 2022-02-23 12:55
Hi,
I'd like to write an embark function for the consult-ripgrep.
This function can add the current file name to the search prompt.
What I mean:
The consult ripgrep prompt is: #whatilookfor
and after the embark action the prompt is: #whatilookfor -- -g filename\*.differentextension
I can image that if we add this "-- -g filename\*.differentextension" to the consult-history and I can reach it with M-p maybe it can be a different good solution
I would like to use it to search the string in the files with same name but different extensions.
It would be useful under the angular development for me ...
If you can send me an example where an embark function modify the consult prompt could be enough for me
sorry for the question but I have no a lot of time to write code for emacs ... :(
oantolin · 2022-02-23 14:16
I'm a little confused about what you are trying to do, specifically, what do you mean by "the current file"? Are you talking about the file in the buffer where you were when you ran `consult-ripgrep`? Or do you mean that in `consult-ripgrep` you put your cursor on a particular result and now want results only from that same file?
Also, do you want a particular `differentextension`? Do you want to be prompted for the extension? Or do you want to match any extension?
zsome · 2022-02-23 14:55
What I mean:
if I have a buffer open from this file: something.html
I use the consult-ripgrep function and I'm look for the "word" word.
I would like to see in the consult prompt this:
\#word -- -g something\*.\*
I would like that the embark add the " -- -g something\*.\*" to the prompt after the "#word"
oantolin · 2022-02-23 15:08
Oh, I see. I'm not sure why you want to get Embark involved here. I would just write a command that inserts that for me and bind it to a key in the minibuffer, like this:
(defun restrict-to-current-file ()
(interactive)
(if-let ((file (with-minibuffer-selected-window
(buffer-file-name))))
(save-excursion
(goto-char (point-max))
(insert " -- -g " (file-name-base file) "*.*"))
(user-error "Buffer is not visiting a file")))
(define-key minibuffer-local-map (kbd "C-!") #'restrict-to-current-file)
(That same command won't work if called from `embark-act` because, called from the minibuffer, Embark runs commands in the buffer you were in when you opened the minibuffer. If you really want to do this from Embark, we can adapt the function.)
zsome · 2022-02-23 15:45
Thank you so much! It works well and I understand the embark a little more :)
But maybe I have a little problem with it, can I use the buffer's filename where I started the consult-ripgrep ?
It works well but it use the filename from the consult result list
zsome · 2022-02-23 16:15
I understand what happen here, when I type something then the consult open it and it is why I see a different buffer name here. In this case maybe I need to store the original buffer name in a variable...
What do you think?
oantolin · 2022-02-23 17:09
Oh, right! Consult preview changes the `minibuffer-selected-window`! :D
You know what? It's probably much simpler to just use a specialized `consult-ripgrep` variant that you'd call directly from the buffer you start in, instead of these acrobatics to add the input after the `consult-ripgrep` is already running. Try this:
(defun consult-ripgrep-related-files ()
"Seearch in files whose base name is the same as the current file's."
(interactive)
(minibuffer-with-setup-hook
(lambda () (goto-char (1+ (minibuffer-prompt-end))))
(consult-ripgrep default-directory
(if-let ((file (buffer-file-name)))
(format " -- -g %s*.*" (file-name-base file))
(user-error "Buffer is not visiting a file")))))
zsome · 2022-02-23 17:26
Thanks, I've learnt a lot about it!