【问题标题】:How do you find the name of the current running funciton: current-function, this-function, current-defun, this-defun如何找到当前运行函数的名称:current-function, this-function, current-defun, this-defun
【发布时间】:2011-09-08 19:19:19
【问题描述】:

我正在尝试动态查找正在运行的当前函数(this-function)的名称,即

(defun my-func () (remove-hook 'some-hook this-function) (做一点事))

【问题讨论】:

  • 我认为这是不可能的。但是,如果您描述了您正在尝试做的事情,也许我们可以建议另一种方法。
  • 我试图在运行后从钩子中删除该函数。我想让函数尽可能通用。

标签: elisp


【解决方案1】:

我还没有测试过,但是为什么不写一个宏来封装你想要的呢?可能是以下内容?

(defmacro one-shot-hook (name hook &rest body)
  `(defun ,name ()
     (remove-hook ',hook ',name)
     ,@body)

那么,例如

(macroexpand-all-1
 '(one-shot-hook test c-mode-hook
    (message "Yay!")))

给予

(defun test nil
  (remove-hook (quote c-mode-hook) (quote test))
  (message "Yay!"))

(当我重新格式化它时)。

这消除了需要知道您所在函数名称的问题,无论如何这需要令人讨厌的宏观学(我不确定这是否可能)。

还有一件事,我可能建议只将标志变量设置为 nil 最初您的代码测试以决定是否运行。这样您就不必一直在添加和删除钩子了:结果可能会更容易定制和使用您的代码的任何其他人理解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    相关资源
    最近更新 更多