dajva / rg.el

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

Quote search pattern #103

Closed egorenar closed 3 years ago

egorenar commented 3 years ago

To prevent shell expansion.

Example that fails: rg -n --column --engine auto -i --heading --no-config --type=c -e rcu_read_unlock\( regex could not be compiled with either the default regex engine or with PCRE2.

default regex engine error:

  regex parse error:
      rcu_read_unlock(
                     ^
  error: unclosed group

PCRE2 regex engine error: PCRE2: error compiling pattern at offset 16: missing closing parenthesis

Example that works: rg -n --column --engine auto -i --heading --no-config --type=c -e 'rcu_read_unlock\('

Signed-off-by: Alexander Egorenkov egorenar-dev@posteo.net

coveralls commented 3 years ago

Coverage Status

Coverage remained the same at 80.462% when pulling c2bf2a91c292073863747a89c91c309c7c7d282b on egorenar:quote-search-pattern into 8a8ae4d269ac407c1343044dde9135cbddb6816d on dajva:master.

dajva commented 3 years ago

This should not be needed. You should be able to use syntax "inside quotes" in the regexp minibuffer dialog and it should be properly escaped for you. So entering rcu_read_unlock\( in the minibuffer should give you rcu_read_unlock\\\( on the command line for a posix shell. What environment is this? Can you do M-x rg-print-environment and post the output here? Are you using an unusual shell?

egorenar commented 3 years ago

Hmm, entering rcu_read_unlock\( leads to this:

-*- mode: rg; default-directory: "~/Repositories/linux/" -*-
rg started at Sun Oct 11 10:00:54

rg --color=always --colors=match:fg:red --colors=path:fg:magenta --colors=line:fg:green --colors=column:none -n --column --engine auto -i --heading --no-config --type=c -e rcu_read_unlock\(

regex could not be compiled with either the default regex engine or with PCRE2.

default regex engine error:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
regex parse error:
    rcu_read_unlock(
                   ^
error: unclosed group
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

PCRE2 regex engine error:
PCRE2: error compiling pattern at offset 16: missing closing parenthesis

rg exited abnormally with code 2 at Sun Oct 11 10:00:54

rg-print-environment:

--------- RG environment ---------
emacs-version: GNU Emacs 27.1 (build 1, x86_64-unknown-linux-gnu, GTK+ Version 3.24.21)
ripgrep-version: ripgrep 12.1.1
compilation-filter-hook: (my/colorize-compilation)
compilation-filter-advised: nil
rg-use-transient-menu: t
rg-show-columns: t
rg-group-result: t
rg-show-header: t
rg-hide-command: nil
rg-align-position-numbers: t
rg-align-line-number-field-length: 5
rg-align-column-number-field-length: 5
rg-align-line-column-separator: "#"
rg-align-position-content-separator: "|"
rg-custom-type-aliases: nil
rg-executable: "rg"
rg-command-line-flags: ("--engine auto")
rg-ignore-case: case-fold-search
rg-keymap-prefix: "s"
rg-default-alias-fallback: "all"
rg-buffer-name: my/rg-buffer-name
rg-ignore-ripgreprc: t
------------------ END ------------------

My rg config:

(use-package rg
  :pin melpa
  :config
  (setq rg-executable "rg")
  (setq rg-group-result t)
  (setq rg-show-columns t)
  (setq rg-align-position-numbers t)
  (setq rg-align-line-number-field-length 5)
  (setq rg-align-column-number-field-length 5)
  (setq rg-align-line-column-separator "#")
  (setq rg-align-position-content-separator "|")
  (setq rg-command-line-flags '("--engine auto"))
  (setq rg-custom-type-aliases nil)
  (setq rg-default-alias-fallback "all")
  (setq rg-hide-command nil)
  (defun my/rg-buffer-name ()
    (let ((p (project-current)))
      (if p
      (format "rg %s" (abbreviate-file-name (cdr p)))
    "rg")))
  (setq rg-buffer-name #'my/rg-buffer-name)
  :bind (:map rg-global-map
          ("c" . rg-dwim-current-dir)
          ("f" . rg-dwim-current-file)
          ("m" . rg-menu)
          :map rg-mode-map
          ("m" . rg-menu))
  :hook (after-init . rg-enable-default-bindings))

And i'm using zsh. But the same happens with bash.

dajva commented 3 years ago

Thanks. I can't see anything obvious there. The escaping is done with shell-quote-argument, so I think you need to play with that and see if you can figure out what is wrong. For instance: (shell-quote-argument "foo\\(") -> "foo\\\\\\("

egorenar commented 3 years ago

Hmm, i see shell-quote-argument for --type-add in rg.el:rg-build-command but nothing for -e, that's where i added quotes myself.

dajva commented 3 years ago

The search pattern should be quoted by grep-expand-template so no need to add that here. Is there something with your grep-expand-template that doesn't do what's expected?

(grep-expand-template "<R>" "foo\\(") -> "foo\\\\\\("
egorenar commented 3 years ago

The search pattern should be quoted by grep-expand-template so no need to add that here. Is there something with your grep-expand-template that doesn't do what's expected?

(grep-expand-template "<R>" "foo\\(") -> "foo\\\\\\("

This works as above for me too.

egorenar commented 3 years ago

i set a breakpoint in rg-build-command and pattern parameter evaluated to "rcu_read_unlock(" although i entered rcu_read_unlock\( in the minibuffer.

egorenar commented 3 years ago

oh, i think i found the culprit, disabling the package pcre2el helps, hmm

egorenar commented 3 years ago

ok, i'm closing it then, now it works, disabled pcre-mode, sorry for wasting your time, sometimes emacs packages interfere with each other in a bad way :/

dajva commented 3 years ago

Yes, that is a common source of problems. Maybe there is a problem with pcre-mode then...