开发者

How do you find the name of the current running funciton: current-function, this-function, current-defun, this-defun

I am trying to dynamically find the name of the current function (this-function) running i.e.

  (defun my-func ()
      (remove-hook 'some-hook this-开发者_如何学Cfunction)
      (do-something))


I haven't tested this, but why not write a macro to encapsulate what you want? Something like the following, maybe?

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

Then, for example

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

gives

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

(when I've reformatted it).

This removes the problem of needing to know the name of the function you're in, which would need nasty macrology anyway (I'm not sure whether it's possible).

One more thing, I'd probably suggest just having a flag variable set to nil initially which your code tests to decide whether to run. Then you don't have to mess around adding and removing hooks all the time: the result will probably be much easier to customise and understand for anyone else using your code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜