【分享】使用`find-dired-with-command`构建自己的`fd-dired`

最近发现emacs29版本中的find-dired包中多了个命令find-dired-with-command,这个命令对构建shell command与生成dired界面进行了解耦,那么使用fd(或其它可生成path列表的命令)+find-dired-with-command可以很简单的配置出类似fd-dired包中的fd-dired命令。 以下是我的配置,仅供参考。

(use-package find-dired
  :init
  (defvar dired:fd-args nil
    "Last arguments given to `fd' by \\[dired:fd-dired]")
  ;; History of fd-args values entered in the minibuffer.
  (defvar dired:fd-args-history nil)
  
  (defconst dired:fd-dired-fd-pre-args
    (concat " --color never"
            (if def:win-p
                (concat " --path-separator "
                        (shell-quote-argument "/")))
            " "))
  
  (defconst dired:fd-dired-rg-pre-args
    (concat " --color never"
            " --files-with-matches"
            " -0"
            " --regexp"
            " "))
  :config
  (defun dired:fd-dired (dir args)
    "Run `fd' and go into Dired mode on a buffer of the output.
The default command run is
fd ARGS -l
."
    (interactive (list
                  (read-directory-name "Run fd in directory: " nil "" t)
                  (read-string "Run fd (with args): " dired:fd-args
                               (if dired:fd-args
                                   '(dired:fd-args-history . 1)
                                 'dired:fd-args-history))))
    (setq dired:fd-args args
          args (concat "fd"
                       dired:fd-dired-fd-pre-args
                       (unless (string= args "")
                         (concat args " "))
                       "-l"))
    (find-dired-with-command dir args))

  (defun dired:fd-dired-dwim (args)
    "Run `fd' and go into Dired mode on a buffer of the output in `default-directory'."
    (interactive (list
                  (read-string "Run fd (with args): " fd-args
                               (if fd-args
                                   '(fd-args-history . 1)
                                 'fd-args-history))))
    (dired:fd-dired default-directory args))

  (defun dired:fd-name-dired (dir pattern)
    "Search DIR recursively for files matching the globbing PATTERN.
and run Dired on those files.
PATTERN is a shell wildcard (not an Emacs regexp) and need not be quoted.
The default command run is
fd -g PATTERN -l
."
    (interactive
     "DFd-name (directory): \nsFd-name (filename wildcard): ")
    (dired:fd-dired dir (concat "-g" " " (shell-quote-argument pattern))))

  (defun dired:fd-rg-dired (dir regexp)
    "Find files in DIR that contain matches for REGEXP and Dired on output.
The default command run is
fd -X rg -l0 --regexp REGEXP | xargs -0 ls
."
    (interactive "DFd-rg (directory): \nsFd-rg (rg regexp): ")
    (find-dired-with-command dir
                             (concat "fd"
                                     dired:fd-dired-fd-pre-args
                                     "-X"
                                     " "
                                     "rg"
                                     dired:fd-dired-rg-pre-args
                                     (shell-quote-argument regexp)
                                     " "
                                     "|"
                                     " "
                                     "xargs -0"
                                     " "
                                     "ls"
                                     " "
                                     (cdr find-ls-option))))
  :bind
  (("M-s d" . dired:fd-dired)
   :map dired-mode-map
   ("r" . dired:fd-dired-dwim))
  :commands dired:fd-rg-dired
  :custom
  (find-ls-option find-ls-option-default-ls))

配置用到了xargsls命令,Windows系统用户可将git下的usr/bin目录加入系统路径中来获取相关命令。

2 个赞