还是找到了解决方案,详情可看 maple-emacs/lisp/init-completion.el at master · honmaple/maple-emacs · GitHub
- 有两个方案,一个是自定义命令修改
this-command
(defun maple/consult-grep (&optional dir initial)
(interactive "P")
(let ((this-command 'consult-ripgrep))
(consult-ripgrep (or dir default-directory) initial)))
另外一种方案比较hack,直接修改 completing-read
函数,这种方案可能会导致所有minibuffer相关的命令都添加默认输入,虽然可以立即删除
(defun maple/consult-read (oldfunc &rest args)
(minibuffer-with-setup-hook
(lambda() (add-hook 'after-change-functions 'maple/consult-initial nil t))
(let ((initial (nth 4 args)))
(while (< (- (length args) 5) 0)
(setq args (append args (list nil))))
(when (or (not initial) (string= initial ""))
(setf (nth 4 args) (propertize (maple-region-string) 'face 'shadow)))
(apply oldfunc args))))
(defun maple/consult-initial(&optional beg end len string)
(remove-hook 'after-change-functions 'maple/consult-initial t)
(when (member this-command '(self-insert-command))
(let ((str (buffer-substring-no-properties beg end)))
(when (eq 'file (vertico--metadata-get 'category))
(setq str (minibuffer-contents-no-properties)))
(delete-minibuffer-contents)
(insert str))))
(advice-add 'completing-read :around 'maple/consult-read)
- 除上面的
git ls-files
外,我还是喜欢使用rg --files
,这样就能不限制是否是git管理的目录
(defun maple/file-completion-table (cands)
(lambda (string pred action)
(cond
((eq action 'metadata)
'(metadata . ((category . file))))
(t
(complete-with-action action cands string pred)))))
(defun maple/find-file ()
(interactive)
(let* ((cmd "rg --files --color=never")
(cands (split-string (shell-command-to-string cmd) "\n" t))
(file (completing-read "Find file: " (maple/file-completion-table cands) nil t)))
(find-file file)))
ok,问题解决后多试几天,看看和ivy有啥差距