dajva / rg.el

Emacs search tool based on ripgrep
https://rgel.readthedocs.io
GNU General Public License v3.0
484 stars 41 forks source link

What about adding rg-dwim-current-file? #54

Closed kidd closed 5 years ago

kidd commented 5 years ago

I love the way rg-dwim works, the fact that I can narrow down the search to the current directory with c-u is awesome.

Trying to go a step further, I wanted to implement something like:

;;;###autoload
(defun rg-dwim (&optional curdir)
  "Run ripgrep without user interaction figuring out the intention by magic(!).
The default magic searches for thing at
point in files matching current file under project root
directory.  With \\[universal-argument] prefix (CURDIR), search is
done in current dir instead of project root."
  (interactive "P")
  (case curdir
    (4  (rg-dwim-current-dir))
    (16 (rg-dwim-current-file))
    (t (rg-dwim-project-dir))))

but the "files" option in rg-define-search is thought for aliases only. Would there be an easy way to search only (buffer-file-name)?

dajva commented 5 years ago

Good idea. I never thought of that usage but seems pretty handy.

The :files option handles custom forms as well. If the evaluated result of the form doesn't match an existing alias this is added as a custom alias with --type-add custom:non_macthing_pattern.el --type: custom. So we create a temp alias with the name custom which is then selected.

I think the problem is that (buffer-file-name) will return the absolute path and the default-directory will be set to watever the :directory options specifies. So, I think something like this should work:

(rg-define-search rg-dwim-current-file
    :query point
    :format literal
    :files (file-name-nondirectory (buffer-file-name))
    :dir current)
kidd commented 5 years ago

aha, nice!

Yes, adding :files (buffer-file-name) but didn't work and I just chickened away and asked here :).

Would you accept a PR for that?

dajva commented 5 years ago

Closed by pr #55