如何建议将不带参数的函数添加到带参数的函数?

How to advice-add a function with no arguments to a function that takes arguments?

假设我有一个函数如下:

  (defun my/test-a (n)
    (interactive)
    (message n))
    
  (defun my/test-b ()
    (interactive)
    (sleep-for .5)
    (message "Message - B.")
    (sleep-for .5))

然后我建议 my/test-a 使用 mytest-b,如下所示:(advice-add 'my/test-a :after #'my/test-b).

然而,当我调用 (my/test-a "Message - A.") 时,出现“参数数量错误”错误。我的理解是 add-advice 正在将参数输入 my/test-b,它不需要任何参数。

如何将不带参数的函数添加到带参数的函数中?

我可以更改 my/test-b 所以它需要一个参数而不使用它,但这感觉很乱。

相关后续行动 - 我如何建议 find-file 使用不带参数的函数(如 my/test-b)?我知道 find-file 是一种不寻常的情况,因为如果以交互方式调用它不需要参数。但是如果我 运行 (advice-add 'find-file :after #'my/test-b) 然后 (call-interactively 'find-file) 我会再次收到“错误的参数数量”错误。

TIA。

你不能那样做。

您的建议函数必须接受原始函数的参数。

C-hig (elisp)Advice Combinators 说:

:after
Call FUNCTION after the old function. Both functions receive the same arguments, and the return value of the composition is the return value of the old function. More specifically, the composition of the two functions behaves like:
(lambda (&rest r) (prog1 (apply OLDFUN r) (apply FUNCTION r)))

一种接受任意参数并忽略它们的方法是:

(defun foo (&rest _args) ...)

下划线告诉字节编译器参数在函数体中是故意未使用的。