I found a weird behaviour with rgrep. The following is tested under emacs 26.3 with emacs -Q.

  1. Evaluate (rgrep "test" "*.org" "~/Org/wiki") gives me a string-match("<C>" nil) error:
Debugger entered--Lisp error: (wrong-type-argument stringp nil)
  string-match("<C>" nil)
  grep-expand-template(nil "test" "\\( -name \\*.org \\)" nil "-type d \\( -path \\*/SCCS -o -path \\*/RCS -o -path \\*/CVS -o -path \\*/MCVS -o -path \\*/.src -o -path \\*/.svn -o -path \\*/.git -o -path \\*/.hg -o -path \\*/.bzr -o -path \\*/_MTN -o -path \\*/_darcs -o -path \\*/\\{arch\\} \\) -prune -o \\! -type d \\( -name .\\#\\* -o -name \\*.pho -o -name \\*.phi -o -name \\*.glob -o -name \\*.vo -o -name \\*.o -o -name \\*\\~ -o -name \\*.bin -o -name \\*.lbin -o -name \\*.so -o -name \\*.a -o -name \\*.ln -o -name \\*.blg -o -name \\*.bbl -o -name \\*.elc -o -name \\*.lof -o -name \\*.glo -o -name \\*.idx -o -name \\*.lot -o -name \\*.fmt -o -name \\*.tfm -o -name \\*.class -o -name \\*.fas -o -name \\*.lib -o -name \\*.mem -o -name \\*.x86f -o -name \\*.sparcf -o -name \\*.dfsl -o -name \\*.pfsl -o -name \\*.d64fsl -o -name \\*.p64fsl -o -name \\*.lx64fsl -o -name \\*.lx32fsl -o -name \\*.dx64fsl -o -name \\*.dx32fsl -o -name \\*.fx64fsl -o -name \\*.fx32fsl -o -name \\*.sx64fsl -o -name \\*.sx32fsl -o -name \\*.wx64fsl -o -name \\*.wx32fsl -o -name \\*.fasl -o -name \\*.ufsl -o -name \\*.fsl -o -name \\*.dxl -o -name \\*.lo -o -name \\*.la -o -name \\*.gmo -o -name \\*.mo -o -name \\*.toc -o -name \\*.aux -o -name \\*.cp -o -name \\*.fn -o -name \\*.ky -o -name \\*.pg -o -name \\*.tp -o -name \\*.vr -o -name \\*.cps -o -name \\*.fns -o -name \\*.kys -o -name \\*.pgs -o -name \\*.tps -o -name \\*.vrs -o -name \\*.pyc -o -name \\*.pyo \\) -prune -o ")
  rgrep-default-command("test" "*.org" nil)
  rgrep("test" "*.org" "~/Org/wiki")
  eval((rgrep "test" "*.org" "~/Org/wiki") nil)
  eval-expression((rgrep "test" "*.org" "~/Org/wiki") nil nil 127)
  funcall-interactively(eval-expression (rgrep "test" "*.org" "~/Org/wiki") nil nil 127)
  call-interactively(eval-expression nil nil)
  read-from-minibuffer("Eval: " nil (keymap (9 . completion-at-point) (27 keymap (9 . completion-at-point)) keymap (18 . counsel-minibuffer-history) (menu-bar keymap (minibuf "Minibuf" keymap (previous menu-item "Previous History Item" previous-history-element :help "Put previous minibuffer history element in the minibuffer") (next menu-item "Next History Item" next-history-element :help "Put next minibuffer history element in the minibuffer") (isearch-backward menu-item "Isearch History Backward" isearch-backward :help "Incrementally search minibuffer history backward") (isearch-forward menu-item "Isearch History Forward" isearch-forward :help "Incrementally search minibuffer history forward") (return menu-item "Enter" exit-minibuffer :key-sequence "\015" :help "Terminate input and exit minibuffer") (quit menu-item "Quit" abort-recursive-edit :help "Abort input and exit minibuffer") "Minibuf")) (10 . exit-minibuffer) (13 . exit-minibuffer) (7 . minibuffer-keyboard-quit) (C-tab . file-cache-minibuffer-complete) (9 . self-insert-command) (XF86Back . previous-history-element) (up . previous-line-or-history-element) (prior . previous-history-element) (XF86Forward . next-history-element) (down . next-line-or-history-element) (next . next-history-element) (27 keymap (114 . previous-matching-history-element) (115 . next-matching-history-element) (112 . previous-history-element) (110 . next-history-element))) t read-expression-history)
  read--expression("Eval: ")
  byte-code("\301\302!\303\010!B\207" [current-prefix-arg read--expression "Eval: " eval-expression-get-print-arguments] 3)
  call-interactively(eval-expression nil nil)
  1. Then evaluate (rgrep "test" "*.org") gives the same error.
  2. BUT evaluate (rgrep "test") works without any error.
  3. The most weird comes: (rgrep "test" "*.org" "~/Org/wiki") now works correctly.

    This is the problematic line in lisp/progmodes/grep.el.


It seems that you need to give a non-nil value to variable grep-find-template. So customize that user option or bind it around your (rgrep "test" "*.org" "~/Org/wiki") or call (grep-compute-defaults) before evaluating calling rgrep.

I found this out by looking at the backtrace. The first argument passed to grep-expand-template is nil. rgrep-default-command is what invokes grep-find-template.

In general, when you want to invoke a command from Lisp instead of interactively, you need to check the command's interactive spec, to see whatever it might do to come up with appropriate arguments for the command's body.

Added from OP's comment confirming that (grep-compute-defaults) takes care of it:

Just to complete the answer a bit: calling the function (grep-compute-defaults) set grep-find-template to its default value. So (progn (grep-compute-defaults) (rgrep "test" "*.org" "~/Org/wiki")) fix the issue.

  • Thank you! Just to complete the answer a bit: calling the function (grep-compute-defaults) set grep-find-template to its default value. So (progn (grep-compute-defaults) (rgrep "test" "*.org" "~/Org/wiki")) fix the issue. – Firmin Martin May 1 '20 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.