我试图在 elisp 中将一种方法传递给另一种方法,然后让该方法执行它。这是一个例子:
(defun t1 ()
"t1")
(defun t2 ()
"t1")
(defun call-t (t)
; how do I execute "t"?
(t))
; How do I pass in method reference?
(call-t 't1)
首先,我不确定命名你的函数t
是否有帮助,因为 't' 被用作 lisp 中的真值。
也就是说,以下代码对我有用:
(defun test-func-1 () "test-func-1"
(interactive "*")
(insert-string "testing callers"))
(defun func-caller (callee)
"Execute callee"
(funcall callee))
(func-caller 'test-func-1)
请注意“funcall”的使用,它会触发实际的函数调用。
Emacs Lisp 手册中“ §13.7 Anonymous Functions ”末尾的注释说,您可以引用函数而不是向字节编译器发出符号始终命名函数的信号。#'
'
Above answers are okey, but you can do something more interesting with defmacro, wich evaluates functions later for some reason:
(defun n1 ()
"n1")
(defmacro call-n (n)
(apply n))
(call-n (n1))
A practical example with a for loop that takes any amount of functions and their arguments:
(defmacro for (i &optional i++ &rest body)
"c-like for-loop"
(unless (numberp i++) (push i++ body) (setq i++ 1))
(while (/= i 0)
(let ((args 0))
(while (nth args body)
(apply (car (nth args body))
(cdr (nth args body)))
(setq args (1+ args))))
(setq i (- i i++))
)
)