在elisp中,如何将函数放在变量中? [英] In elisp, how do I put a function in a variable?

查看:87
本文介绍了在elisp中,如何将函数放在变量中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想允许用户在自定义emacs后端中选择自己的命令(通常可以在变量中存储一个可执行的表单名称),但这不起作用:

 (defun dumb-f()
(messageI'm a function))

(defvar my-函数dumb-f)

(my-function)
==>调试器输入 - Lisp错误:(无效函数dumb-f)

(setq my-function'dumb-f)

(my-function)
==>调试器输入 - Lisp错误:(无效函数dumb-f)

但是还是没有运气,而且我很难找到它,我得到关于功能和变量的结果的k,,但是没有一个关于如何把前者放在后者中。?

解决方案

请注意,在Emacs Lisp中,符号具有值单元格和功能单元格,它们是不同的。当你评估一个符号时,你得到它的。当您评估从该符号开始的列表时,您将调用其函数。这就是为什么你可以使用同名的变量和函数。



大多数类型的赋值将设置值(例如 let setq defvar defcustom ,etc ...) - 和ryuslash显示,您可以分配一个值作为一个值,并通过 funcall 调用它 - 但也有一些方法可以分配给一个符号函数单元格直接使用 fset (或 flet defalias 等等)

 (fset'my-function'dumb-f)
功能)

在你的情况下,我会使用ryuslash的答案(除了你需要使用 defcustom 而不是 defvar ,如果你想通过定制可用。



另外,关于我正在努力地搜索它;永远记住,Emacs是自我记录,其中包含一个良好的手册,完整的索引(实际上,有一个以上的手册)。所以即使Google仍然是您的第一个通话端口,如果您找不到所需的内容,也不应该是您的最后一个。从elisp手册的内容页面,您可以导航到功能,然后导航到调用功能,您将被马上被告知有关 funcall


I want to allow the user to choose their own command in the "customize" emacs backend (and generally be able to store an executable form name in a variable) but this does not work :

    (defun dumb-f ()
    (message "I'm a function"))

    (defvar my-function "dumb-f")

    (my-function)
    ==> Debugger entered--Lisp error: (invalid-function "dumb-f")

    (setq my-function 'dumb-f)

    (my-function)
    ==> Debugger entered--Lisp error: (invalid-function "dumb-f")

I tried various forms, but still no luck, and I'm having a hard time searching for it, I get kilopages of results about functions and variables, but none about how to put the former in the latter..?

解决方案

Note that in Emacs Lisp, symbols have a value cell and a function cell, which are distinct. When you evaluate a symbol, you get its value. When you evaluate a list beginning with that symbol, you call its function. This is why you can have a variable and a function with the same name.

Most kinds of assignment will set the value (e.g. let, setq, defvar, defcustom, etc...) -- and as ryuslash shows you can assign a function as a value and call it via funcall -- but there are also ways to assign to a symbol's function cell directly using fset (or flet, defalias, etc)

(fset 'my-function 'dumb-f)
(my-function)

In your case I would use ryuslash's answer (except you need to use defcustom rather than defvar if you want it to be available via customize).

Also, regarding "I'm having a hard time googling for it"; always remember that Emacs is self-documenting, and among other things contains a good manual complete with index (well, more than one manual, in fact). So even if Google remains your first port of call, it shouldn't also be your last if you can't find what you're looking for. From the contents page of the elisp manual you can navigate to "Functions" and then "Calling functions" and you will be told about funcall almost immediately.

这篇关于在elisp中,如何将函数放在变量中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆