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

Question: How to change default-directory when in consult-ripgrep #596

Closed
lynnux opened this issue Jun 22, 2022 · 2 comments
Closed

Question: How to change default-directory when in consult-ripgrep #596

lynnux opened this issue Jun 22, 2022 · 2 comments

Comments

@lynnux
Copy link

lynnux commented Jun 22, 2022

Hi @minad, I use consult-ripgrep only search current dir not the whole project, code by this:

(defun my-consult-ripgrep(&optional dir)
             (interactive "P") 
             (require 'consult)
             (let  ((consult-ripgrep-args (concat consult-ripgrep-args " --no-ignore")))
               (consult-ripgrep (or dir default-directory)))
             )

and there is a function binding to C-l to change the directory to parent, code like this:

           (defun is-consult-ripgrep()
             (eq 'consult-grep
                 (completion-metadata-get
                  (completion-metadata (minibuffer-contents)
                                       minibuffer-completion-table
                                       minibuffer-completion-predicate)
                  'category)))
           (defun is-consult-line()
             (eq 'consult-location ;; 
                 (completion-metadata-get
                  (completion-metadata (minibuffer-contents)
                                       minibuffer-completion-table
                                       minibuffer-completion-predicate)
                  'category)))
           (defun my/vertico-C-l ()
	     (interactive)
             (if (is-consult-ripgrep)
                 (progn
                   (setq default-directory (file-name-directory (directory-file-name default-directory)))
                   (let ((text (substring-no-properties (or (car-safe vertico--input) ""))))
                     (delete-minibuffer-contents)
                     (insert text)
                     ))
               (call-interactively 'vertico-directory-delete-word)))
(define-key vertico-map (kbd "C-l") 'my/vertico-C-l)

When I invoke C-l, the search result is fine, but the C-n(vertico-next) not work as expect, and RET also not work.
22

@minad
Copy link
Owner

minad commented Jun 22, 2022

I don't understand what you are trying to achieve but searching a specific directory is possible by pressing C-u first (prefix argument). See the docstring of consult-grep. There shouldn't be the need to write code.

EDIT: I reread your issue again. The problem is that consult-ripgrep does not allow changing the default-directory on the fly. This breaks preview as you discovered since consult--temporary-files captures the default directory in the closure. I don't recall the reasons for why it does that, but I am sure there was a reason. :)

However there is an alternative better way to achieve what you want, which is less fragile. We simply restart the entire command.

(defun consult-ripgrep-up-directory ()
  (interactive)
  (run-at-time 0 nil #'consult-ripgrep
               (file-name-directory (directory-file-name default-directory))
               (ignore-errors
                 (buffer-substring-no-properties
                  (1+ (minibuffer-prompt-end)) (point-max))))
  (minibuffer-quit-recursive-edit))

(consult-customize
 consult-ripgrep
 :keymap (let ((map (make-sparse-keymap)))
           (define-key map (kbd "M-l") #'consult-ripgrep-up-directory)
           map))

(Added to the wiki: https://github.com/minad/consult/wiki#restart-consult-ripgrep-in-parent-directory)

@minad minad closed this as completed Jun 22, 2022
@lynnux
Copy link
Author

lynnux commented Jun 23, 2022

Thank you @minad , works like a charm :)

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

2 participants