记录学习

2008年7月17日星期四

Writing GNU Emacs Extension Note Day 3

定义别名
(defalias 'command1 'command2)

(progn 'command1 'command2)
progn函数表示执行参数的命令,并返回第二个参数的函数所返回的值

(add-hook 'command1 'command2)
表示把command2函数加入到command1的Hook中
比如
(add-hook 'find-file-hook '自定义的函数)
那么在执行打开文件的时候,会自动执行自己的函数做处理

hook是一个函数的列表

定义匿名函数
lambda()
例:
(lambda ()
(if (file-symlink-p buffer-file-name)
(progn
(setq buffer-read-only t)
(message "File is a symlink"))))
匿名函数没有参数

移除hook的函数方法
(remove-hook 'find-file-hooks 'read-only-if-symlink)

一般不建议在hook里面使用匿名函数,因为移除的时候不方便


定义局部变量
(let ((var1 value1)
(var2 value2)
. . .
(varn valuen))
body1 body2 . . . bodyn)


(if (yes-or-no-p . . . ) . . . )
The function yes-or-no-p asks the user a yes or no question and returns true if the answer
was "yes," false otherwise. The question, in this case, is:

(format "Replace %s with %s?
buffer-file-name
target)
This constructs a string in a fashion similar to C's printf.

关于Advice
一系列绑定到一个Lisp函数的Advice,表示每次在调用这个函数之前或之后这些Advice将会得到执行
有点类似于Hook

(defadvice switch-to-buffer (before existing-buffer
activate compile)
"When interactive, switch to existing buffers only."
(interactive "b"))

没有评论:

博客归档