cosmicexplorer / helm-rg

ripgrep is nice
GNU General Public License v3.0
101 stars 21 forks source link

How to specify additional ripgrep arguments? #13

Closed cpbotha closed 4 years ago

cpbotha commented 5 years ago

When searching through my orgmode notes database with helm-rg, it makes the most sense seeing the results in reverse chronological order, i.e. most recently modified first. To accomplish this, I made the following function in my init.el that temporarily modifies helm-rg--ripgrep-argv-format-alist by inserting the extra ripgrep argument I need:

(defun cpb/helm-rg-notes ()
  (interactive)
  (let ((default-directory notes-dir)
        ;; modify alist const (ahem) to return results sorted last modified to first modified
        (helm-rg--ripgrep-argv-format-alist (append
                                             (subseq helm-rg--ripgrep-argv-format-alist 0 2)
                                             (cons '("--sortr=modified" :face helm-rg-inactive-arg-face)
                                                   (nthcdr 2 helm-rg--ripgrep-argv-format-alist)))))
    ;; rg-pattern nil because interactive
    ;; pfx nil so that it uses helm-rg-default-directory which by default means default-directory
    ;; third arg PATHS -- all relative to search directory, i.e. default-directory in our case
    (helm-rg nil nil)))

Although this works great, it does not feel very elegant modifying a defconst like this. Looking through the helm-rg code, I did not see any other more convenient places for doing this. Did I miss something?

cosmicexplorer commented 5 years ago

Thanks a ton for the very detailed report and investigation!

So I tried adding this in c6d76ef7717b6d59ca432a29dac62c42c3002726, but then had to revert it, then fixed the issue and pushed 32c1c3ff3ec8fb1f59ac686575968e9f603ca00b, which introduces helm-rg-default-extra-args and helm-rg--set-extra-args, which is by default mapped to M-m. I tried to add a test for this but was running up against the same general difficulties testing against helm as usual, (although I've had more success recently addressing testing for #14 and #15, so this should be fixed soon).

Please let me know if this addresses your use case!

cosmicexplorer commented 5 years ago

And to override this, you could let-bind helm-rg-default-extra-args before invoking helm-rg in your your defun there, or you could set it as a directory-local variable!

cpbotha commented 5 years ago

I just changed my defun to use the new helm-rg-default-extra-args -- looks like it works perfectly. Thank you!