Skip to content

Instantly share code, notes, and snippets.

@jthaman
Last active April 10, 2024 01:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jthaman/c4eb411defc98f82cfd85c8c0d4c67e0 to your computer and use it in GitHub Desktop.
Save jthaman/c4eb411defc98f82cfd85c8c0d4c67e0 to your computer and use it in GitHub Desktop.
Call ripgrep-all in emacs with consult
;; Note: put `rga' in your PATH. -*- lexical-binding: t; -*-
(require 'consult)
(defcustom consult-ripgrep-all-args
"rga --null --line-buffered --color=never --max-columns=1000 --path-separator /\ --smart-case --no-heading --with-filename --line-number"
"Command line arguments for ripgrep, see `consult-ripgrep-all'.
The dynamically computed arguments are appended.
Can be either a string, or a list of strings or expressions."
:type '(choice string (repeat (choice string expression))))
(defun consult--ripgrep-all-make-builder (paths)
"Create ripgrep command line builder given PATHS."
(let* ((cmd (consult--build-args consult-ripgrep-all-args))
(type (if (consult--grep-lookahead-p (car cmd) "-P") 'pcre 'extended)))
(lambda (input)
(pcase-let* ((`(,arg . ,opts) (consult--command-split input))
(flags (append cmd opts))
(ignore-case
(and (not (or (member "-s" flags) (member "--case-sensitive" flags)))
(or (member "-i" flags) (member "--ignore-case" flags)
(and (or (member "-S" flags) (member "--smart-case" flags))
(let (case-fold-search)
;; Case insensitive if there are no uppercase letters
(not (string-match-p "[[:upper:]]" arg))))))))
(if (or (member "-F" flags) (member "--fixed-strings" flags))
(cons (append cmd (list "-e" arg) opts paths)
(apply-partially #'consult--highlight-regexps
(list (regexp-quote arg)) ignore-case))
(pcase-let ((`(,re . ,hl) (funcall consult--regexp-compiler arg type ignore-case)))
(when re
(cons (append cmd (and (eq type 'pcre) '("-P"))
(list "-e" (consult--join-regexps re type))
opts paths)
hl))))))))
;;;###autoload
(defun consult-ripgrep-all (&optional dir initial)
"Search with `rga' for files in DIR where the content matches a regexp.
The initial input is given by the INITIAL argument. See `consult-grep'
for more details."
(interactive "P")
(consult--grep "Ripgrep-all" #'consult--ripgrep-all-make-builder dir initial))
(provide 'consult-ripgrep-all)
@jthaman
Copy link
Author

jthaman commented Jan 4, 2023

The error means that you don't have `consult--ripgrep-builder' installed on your system. It is because the Consult author renamed it. I tried to fix plz check.

@arstum
Copy link

arstum commented Jan 5, 2023

What the script running version of consult?

@fountainer
Copy link

I tried the code and it works well. However, it would be better if it can jump to the specified page when selecting the candidate pdf file.

@jthaman
Copy link
Author

jthaman commented May 31, 2023

That would definitely be a usability improvement! I'm open to suggestions.

@fountainer
Copy link

That would definitely be a usability improvement! I'm open to suggestions.

I don't have much knowledge of elisp, consult or embark. So I don't have good suggestions. :)

@jthaman
Copy link
Author

jthaman commented Jun 2, 2023

same, same.

@g-simmons
Copy link

ty for posting! probably saved me an hour or more 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment