实现一个行为像`project-find-file`的`read-file-name`函数

阅读了project-find-file的源码之后,我发现实现这个行为只需要更改find-file函数

(defun mk/project-read-file-name ()
  (let ((find-file-advice (lambda (file-name) file-name))
         res)
    (advice-add 'find-file :override find-file-advice)
    (setq res (project-find-file t))
    (advice-remove 'find-file find-file-advice)
    res))

为了确保 advice-remove 会被执行,可以用 unwind-protect

(advice-add 'find-file :override find-file-advice)
(unwind-protect
    (setq res (project-find-file t))
  (advice-remove 'find-file find-file-advice))

或者 cl-letf

(cl-letf (((symbol-function 'find-file) find-file-advice))
  (setq res (project-find-file t)))
2 个赞