elisp宏写功能? [英] elisp macro to write function?

查看:110
本文介绍了elisp宏写功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了几个功能,几乎相同,保存名称。例如

 ; x是名称,例如function / paragraph / line / etc。 
(defun my-x-function
(interactive)
(mark-x)(do-more-stuff)(modify-x))

有没有办法自动放置生成函数,段落等功能?我有一种感觉,这是宏做的,但我不知道如何使用它们。你帮助/也许小例子会很棒



谢谢

解决方案

是的,这正是宏做的。这是一个简单的宏,根据您指定的模式构建函数:

 (defmacro make-my-function(name)
(列表'defun(格式my-%s-function))()
(list'interactive)
(list(intern(formatmark-%sname) )
(list'do-more-stuff)
(list(intern(formatmodify-%sname)))))
pre>

您可以将此宏复制到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屋!

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