Elisp with examples
- advice advice :around 和 python 的 decorator 很像。
(defun foo (num)
(message "the number is %d" num))
(defun testadvice (fn num)
(let ((num (+ num 1)))
(funcall fn num)))
(advice-add 'foo :around 'testadvice)
(advice-remove 'foo 'testadvice)
- macro
(setq x 1)
(defmacro inc (var)
(list 'setq var (list '1+ var)))
(defmacro ++ (var)
(list 'setq var (list '1+ inc x)
(inc x)
(++ x)
- &optional
&optional 后的参数都为可选参数
(defun myfun (aa bb &optional cc dd)
"test optional arguments"
(insert aa bb cc dd)
)
;; call it
(myfun "1" "2" "3" "4")
如果想跨过某个参数,可以设置为 nil。
(myfun "myaa" "mybb" nil "mydd")
- &rest
类似于 python 中的*args。
(defun ff (aa bb &rest cc)
"test rest arguments"
(message "%s" cc) ; cc is a list
)
;; test
(ff "1" "2" "3" "4")
;; ("3" "4")
https://cestlaz.github.io/posts/using-emacs-31-elfeed-3/ https://nullprogram.com/blog/2016/12/27/ https://www.reddit.com/r/emacs/comments/6w45aj/what_is_the_use_case_for_elisp_macros/ https://www.emacswiki.org/emacs/MacroBasics https://www.emacswiki.org/emacs/BackquoteSyntax