0

how would I pass (in interactive mode for rg) the --files-without-match flag.. There is no short option, and C-x l doesn't show a possibility either. I can do a M-x customize-group on rg to set it for the session, but would like a more imperative way to do so.

RoyM
  • 123
  • 4

2 Answers2

1

M-x rg-menu is your friend.

Here I demonstrate

  • how to add new entries to rg-menu
  • define your custom rg commands with rg-define-search

How to customize rg-menu (Variant 1)

  1. Copy the following snippet to your *scratch* file, and do M-x eval-buffer.
(with-eval-after-load 'rg-menu
  (plist-put (symbol-plist 'rg-menu) 'transient--layout
             (append
              (list [2 transient-column
                       (:description "More Switches")
                       (
                        (4 transient-switch
                           (:key "!"
                                 :description "Files without match"
                                 :argument "--files-without-match"
                                 :command transient:rg-menu:--files-without-match
                                 ))
                        )])
              (plist-get (symbol-plist 'rg-menu) 'transient--layout))))
  1. Invoke rg with C-u M-x rg. rg will run the following command
/usr/bin/rg --color=always --colors=match:fg:red --colors=path:fg:magenta --colors=line:fg:green --colors=column:none -n --column --type-add=gyp\:\*.gyp --type-add=gyp\:\*.gypi -i --heading --no-config --type=elisp -e rg-menu

C-u M-x rg C-u M-x rg

  1. Let the default search finish. You will get this

Hits for rg-menu

Hits for rg-menu

  1. C-x -b *rg*, and press m. This will pop-up a menu. There will be a ! switch. Press it. Then press g to start the search again. This is what you will see.

Select !. Pessg to search again Select !.  Pessg to search again

  1. This is how the resulting buffer looks like ...

Files that don't have the search term rg-menu Files that don't have the search term rg-menu

  1. In the resulting buffer, you can do C-u recompile to see what command was executed previously. You will see
/usr/bin/rg --color=always --colors=match:fg:red --colors=path:fg:magenta --colors=line:fg:green --colors=column:none -n --column --type-add=gyp\:\*.gyp --type-add=gyp\:\*.gypi --files-without-match --heading --no-config --type=elisp -e rg-menu

C-u M-x recompile shows the search command

C-u M-x recompile shows the search command

How to customize rg-menu (Variant 2)

  1. Install the following snippet
(rg-define-search rg-files-without-match
  :format literal
  :flags ("--files-without-match")
  :menu ("Custom" "@" "Files without matches"))

and you will get an entry like this

A custom command rg-files-without-match defined with rg-define-search A custom command rg-files-without-match defined with rg-define-search

  • phew.. nicely done. The first option worked for me.. thanks! With the second "Custom" option, I found I would have to re-enter the query params.. any way to simply have it re-use the params of the existing query? – RoyM Oct 14 '22 at 11:15
  • This question deserved another answer ... So added it here https://emacs.stackexchange.com/a/74102/31220 –  Oct 16 '22 at 09:45
1

To get this

Add a header-line switch for --files-without-match; bound to !~

Add a header-line switch for --files-without-match; bound to !~

Results after pressing ! in the *rg* buffer Results after pressing ! in the *rg* buffer

do this

(with-eval-after-load 'rg-result
  (define-key rg-mode-map "!"
              (defun rg-rerun-toggle-files-without-match ()
                "Rerun last search with toggled '--files-without-match' flag."
                (interactive)
                (rg-rerun-toggle-flag "--files-without-match"))))

(advice-add
 'rg-create-header-line
 :around
 (defun rg-create-header-line--around
     (orig-fun &rest orig-args)
   (pcase-let*
       ((`(,search ,full-command)
         orig-args)
        (retval (apply orig-fun orig-args))
        (itemspace "  "))
     (if full-command retval
       (setq header-line-format
             (append header-line-format
                     (list
                      itemspace
                      (rg-header-render-label "nomatch (!)")
                      (rg-header-mouse-action
                       'rg-rerun-toggle-files-without-match "Toggle Files Without Match"
                       (rg-header-render-toggle
                        `(member "--files-without-match" (rg-search-flags ,search)))))))))))

;; (advice-remove 'rg-create-header-line 'rg-create-header-line--around)
  • Thanks @whitetrillium - I guess I had hoped for a simpler way.. but I really appreciate the update. – RoyM Oct 16 '22 at 14:15