minad / consult

:mag: consult.el - Consulting completing-read
GNU General Public License v3.0
1.12k stars 98 forks source link

how to use consult-fd on a list of files? #957

Closed justrajdeep closed 2 months ago

justrajdeep commented 2 months ago

Hi

Firstly i want to thank you for the amazing plugin!!!

i want to reuse consult-fd to find files in a list of files instead of using fd. the list of valid file names are present in /home/justrajdeep/filelist_alpha2

So i created

(defcustom consult-bat-args
  '((if (executable-find "cat" 'remote) "cat"
      (if (executable-find "bat" 'remote) "bat"
        (error "Executable not found")))
    "/home/justrajdeep/filelist_alpha2"
    )
  "Command line arguments for cat."
  :type '(choice string (repeat (choice string sexp))))

After that i modified consult--fd-make-builder


(defun consult--fd-make-builder (paths)
  "Build find command line, finding across PATHS."
  (let ((cmd (consult--build-args consult-bat-args)))
  ;; (let ((cmd (consult--build-args consult-fd-args)))
    (lambda (input)
      (pcase-let* ((`(,arg . ,opts) (consult--command-split input))
                   (flags (append cmd opts))
                   (ignore-case
                    (and (not (or (member "-s" flags) (member "--case-sensitive" flags)))
                         (or (member "-i" flags) (member "--ignore-case" flags)
                             (let (case-fold-search)
                               ;; Case insensitive if there are no uppercase letters
                               (not (string-match-p "[[:upper:]]" arg)))))))
        (if (or (member "-F" flags) (member "--fixed-strings" flags))
            (cons (append cmd (list arg) opts paths)
                  (apply-partially #'consult--highlight-regexps
                                   (list (regexp-quote arg)) ignore-case))
          (pcase-let ((`(,re . ,hl) (funcall consult--regexp-compiler arg 'pcre ignore-case)))
            (when re
              (cons (append cmd
                            (cdr (mapcan (lambda (x) `("--and" ,x)) re))
                            opts paths)
                    hl)
              )))))))

after that i can get the list of files from /home/justrajdeep/filelist_alpha2 but i am not able to filter it ... Am i doing something wrong?

Sorry maybe this is a very basic question ... and i am new to emacs lisp.

oantolin commented 2 months ago

Maybe something simple non-consulty like this would be good enough:

(defun open-my-file ()
  "Open one of the files listed in ~/filelist_alpha2."
  (find-file
   (completing-read
    "Open: "
    (with-temp-buffer
      (insert-file-contents "~/filelist_alpha2")
      (string-lines (buffer-string))))))
justrajdeep commented 2 months ago

Wow that was so simple ... why i was trying to use consult was for the next part where i want to modify consult-ripgrep to take this list ~/filelist_alpha2 and perform ripgrep only on files in that list ...

justrajdeep commented 2 months ago

i think my defcustom was not right

(defcustom consult-bat-args
  '(
    ;; (if (executable-find "cat" 'remote) "cat"
    ;;   (if (executable-find "bat" 'remote) "bat"
    ;;     (error "Executable not found")))
    "bat /home/rmondal/filelist_alpha2 | rg"  "--color=never"
    )
  "Command line arguments for fd, see `consult-fd'.
The dynamically computed arguments are appended.
Can be either a string, or a list of strings or expressions."
  :type '(choice string (repeat (choice string sexp))))

this gives me the list of files from /home/rmondal/filelist_alpha2 but i get this error

[bat error]: '|': No such file or directory (os error 2)
justrajdeep commented 2 months ago

My end goal is to do something like this?

https://emacs.stackexchange.com/a/47447/20230

(defun swiper-files (&rest files)
  (let* ((swiper-multi-buffers nil)
     (swiper-multi-candidates nil)
     (this-command 'ivy-done))
    (mapc 'swiper-multi-action-1 (mapcar
                  (lambda (f)
                    (buffer-name (find-file-noselect f)))
                  files))
    (ivy-read "Swiper: " swiper-multi-candidates
              :action #'swiper-multi-action-2
              :unwind #'swiper--cleanup
              :caller 'swiper-multi)))

(swiper-files "f1.org" "f2.org")

where those two files are computed somehow.

and i want to do that with consult-ripgrep ...

oantolin commented 2 months ago

Wow that was so simple ... why i was trying to use consult was for the next part where i want to modify consult-ripgrep to take this list ~/filelist_alpha2 and perform ripgrep only on files in that list ...

consult-grep and similar functions can take a list of files to search. (It's not obvious from the docstring of consult-ripgrep, but at least it tells you to read the docstring for consult-grep that explains that DIR can also be a list of files.)

justrajdeep commented 2 months ago

Thanks ...