Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select the *rg* buffer after running a search #142

Open
hmelman opened this issue Jul 7, 2022 · 14 comments
Open

select the *rg* buffer after running a search #142

hmelman opened this issue Jul 7, 2022 · 14 comments

Comments

@hmelman
Copy link

hmelman commented Jul 7, 2022

After running an rg search, I'd like an option to have the results buffer (typically *rg*) be selected. I know about using next-error, but this way I could easily navigate and also quit, edit (via wgrep) or rerun the search.

@dajva
Copy link
Owner

dajva commented Oct 9, 2022

This should be a compilation-mode feature since that is what is used in rg. I have not seen any way of customizing that in compilation-mode itself unfortunately but I think next-error-last-buffer should be set so you can use that in an rg-mode-hook to do what you need.

A naive setup may be:

(defun select-rg-buffer ()
  (switch-to-buffer next-error-last-buffer))
(add-hook 'rg-mode-hook select-rg-buffer)

Not sure how you get that to play well with how compilation buffers are displayed in general but works ok for me that have the default setup.

@hmelman
Copy link
Author

hmelman commented Oct 9, 2022

This is what I have been using:

(with-eval-after-load 'rg
  (advice-add 'rg-run :after
	      #'(lambda (_pattern _files _dir &optional _literal _confirm _flags) (pop-to-buffer (rg-buffer-name)))))

It's seems reasonable that it should be a compilation-mode feature, though I'd want this for rg but not for M-x compile where next-error would be fine for me.

Your suggestion didn't work for me as next-error-last-buffer wasn't correct, also the add-hook call should be:

(add-hook 'rg-mode-hook #'select-rg-buffer)

@ktetzlaff
Copy link

ktetzlaff commented Dec 31, 2022

Here's another variant:

(eval-after-load 'rg
  (defun maybe-pop-to-rg-buffer(&optional buffer _)
    "Switch to BUFFER (default: rg buffer) and select first match."
    (with-current-buffer (or buffer (rg-buffer-name))
      (when (string= mode-name "rg")
        (pop-to-buffer (current-buffer))
        (condition-case nil
            (compilation-next-error 1 nil (point-min))
          (error nil)))))
  (add-to-list 'compilation-finish-functions #'maybe-pop-to-rg-buffer))

@dajva
Copy link
Owner

dajva commented Mar 2, 2023

I guess I can add an rg-finish-functions variable that can be used instead of compilation-finish-functions. In that way the last example should work without finding the rg buffer. Something like this:

(add-to-list 'rg-finish-functions (lambda (buffer _) (pop-to-buffer buffer)))

Would this work for your use cases?
I guess one problem for long running searches is that the buffer will not be selected until the search is finished.

@hmelman
Copy link
Author

hmelman commented Mar 2, 2023

Yeah, with the advice I'm using, it switches to the rg buffer at the start of a long running search, which is nice. Maybe rg-finish-functions could be run at the end of rg-run like my advice? I don't have another use for such a hook so if rg itself could select the buffer via user option I'm fine with that.

dajva added a commit that referenced this issue Mar 18, 2023
This would allow the same functionality as
compilation-finish-functions, i.e. add hooks called when the ripgrep
process finishes.

Could for instance be used to select the *rg* buffer after a search:
(add-to-list 'rg-finish-functions (lambda (buffer _) (pop-to-buffer buffer)))
@dajva
Copy link
Owner

dajva commented Mar 18, 2023

Implemented the rg-finish-functions solution for now.

@hmelman
Copy link
Author

hmelman commented Mar 23, 2023

I see the defvar in the commit but I don't see where they are called. Am I missing something?

@dajva
Copy link
Owner

dajva commented Mar 23, 2023

Probably the define-compilation-mode inner workings. If defined it's picked up by compilation mode and handled from there. So the defvar isn't strictly needed (any setq would work) but will allow for more flexible usage plus visibility in the package.

@hmelman
Copy link
Author

hmelman commented Mar 23, 2023

Seems to be working :)

@jeremyf
Copy link

jeremyf commented Mar 2, 2024

I just verified with the rg-finish-functions. @hmelman would you be able to close this issue as resolved?

@hmelman
Copy link
Author

hmelman commented Mar 2, 2024

I think so. It has been working fine for me though I have one minor issue, but I don't think this has to stay open for it.

I have global-hl-line-mode enabled and the line in the *rg* buffer is not highlighted until I move the cursor in that buffer. Highlighting is run on change-major-mode-hook and post-command-hook so I get why moving point highlights a line, but I hadn't looked into why it's not highlighted initially. Seems like if I put (global-hl-line-highlight) in the rg finish function it works, though if you know of a cleaner approach it would be nice.

@hmelman
Copy link
Author

hmelman commented Mar 2, 2024

Also, should rg-finish-functions be a defcustom instead of a defvar?

@dajva
Copy link
Owner

dajva commented Mar 3, 2024

Also, should rg-finish-functions be a defcustom instead of a defvar?

Possibly. I have followed conventions of compilation-mode here which is using defvar for for hooks. Personally I wouldn't use customizing system for this kind of configuration but I don't mind changing this either. Please submit a PR if you want this.

@hmelman
Copy link
Author

hmelman commented Mar 3, 2024

Also, should rg-finish-functions be a defcustom instead of a defvar?

Possibly. I have followed conventions of compilation-mode here which is using defvar for for hooks. Personally I wouldn't use customizing system for this kind of configuration but I don't mind changing this either. Please submit a PR if you want this.

For me the defcustom is more about discoverability and documentation to see it in customize-group of rg and then setting it in code. Searching the Emacs source base there's a mix of defcustom vs defvar for hooks, though defvar is used more often for -functions, so I think it's your choice and defvar is fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants