elisp宏写功能? [英] elisp macro to write function?
问题描述
; x是名称,例如function / paragraph / line / etc。
(defun my-x-function
(interactive)
(mark-x)(do-more-stuff)(modify-x))
有没有办法自动放置生成函数,段落等功能?我有一种感觉,这是宏做的,但我不知道如何使用它们。你帮助/也许小例子会很棒
谢谢
是的,这正是宏做的。这是一个简单的宏,根据您指定的模式构建函数:
(defmacro make-my-function(name)
pre>
(列表'defun(格式my-%s-function))()
(list'interactive)
(list(intern(formatmark-%sname) )
(list'do-more-stuff)
(list(intern(formatmodify-%sname)))))
您可以将此宏复制到Emacs中的
* scratch *
缓冲区,并对其进行评估,然后检查它的工作原理如下:(make-my-function x); type control-J here
my-x-function; < - Emacs的输出
(symbol-function'my-x-function); type control-J here
(lambda nil(interactive)(mark-x)(do-more-stuff)(modify-x)); < - Emacs的输出
更常见的是使用 backquote 工具更简洁地写宏,但所有的宏基本上都是以与上述相同的方式工作的例如。
I have written few functions, which nearly identical, save for names. For example
; x is name, such as function/paragraph/line/etc. (defun my-x-function (interactive) (mark-x) (do-more-stuff) (modify-x))
is there a way to put it automatically to generate function, paragraph, etc. functions? I have a feeling this is what macros do, but I am not sure how to use them. you help/maybe small example would be great
Thanks
解决方案Yep, that's exactly what macros do. Here's a straightforward macro that builds functions according to the pattern you specified:
(defmacro make-my-function (name) (list 'defun (intern (format "my-%s-function" name)) () (list 'interactive) (list (intern (format "mark-%s" name))) (list 'do-more-stuff) (list (intern (format "modify-%s" name)))))
You can copy this macro to a
*scratch*
buffer in Emacs and evaluate it, and then check that it works like this:(make-my-function x) ; type control-J here my-x-function ; <-- Emacs's output (symbol-function 'my-x-function) ; type control-J here (lambda nil (interactive) (mark-x) (do-more-stuff) (modify-x)) ; <-- Emacs's output
More commonly one would use the backquote facility to write macros more concisely, but all macros essentially work in the same manner as the above example.
这篇关于elisp宏写功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!