From 1148d375898652ce5dec986d11bec46bb8ac0e6d Mon Sep 17 00:00:00 2001 From: Leo Liu Date: Tue, 18 Nov 2014 23:57:01 +0800 Subject: [PATCH] New macro define-advice * doc/lispref/functions.texi (Advising Named Functions): Document define-advice. * lisp/emacs-lisp/nadvice.el (define-advice): New macro. * lisp/emacs-lisp/lisp-mode.el (lisp-imenu-generic-expression): Add define-advice. (lisp-font-lock-keywords-1): Add define-advice. --- doc/lispref/ChangeLog | 5 +++++ doc/lispref/functions.texi | 7 +++++++ lisp/ChangeLog | 7 +++++++ lisp/emacs-lisp/lisp-mode.el | 4 ++-- lisp/emacs-lisp/nadvice.el | 24 ++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 2 deletions(-) diff --git a/doc/lispref/ChangeLog b/doc/lispref/ChangeLog index 0a8a0a88cce..6706f936c5e 100644 --- a/doc/lispref/ChangeLog +++ b/doc/lispref/ChangeLog @@ -1,3 +1,8 @@ +2014-11-18 Leo Liu + + * functions.texi (Advising Named Functions): Document + define-advice. + 2014-11-17 Paul Eggert Improve time stamp handling, and be more consistent about it. diff --git a/doc/lispref/functions.texi b/doc/lispref/functions.texi index 023175e3632..50849d4228c 100644 --- a/doc/lispref/functions.texi +++ b/doc/lispref/functions.texi @@ -1360,6 +1360,13 @@ called directly from C, and such calls ignore advice; hence, one ends up in a confusing situation where some calls (occurring from Lisp code) obey the advice and other calls (from C code) do not. +@defmac define-advice symbol (where lambda-list &optional name depth) &rest body +This macro defines an advice and adds it to the function named +@var{symbol}. The advice is an anonymous function if @var{name} is +nil or a function named @code{symbol@@name}. See @code{advice-add} +for explanation of other arguments. +@end defmac + @defun advice-add symbol where function &optional props Add the advice @var{function} to the named function @var{symbol}. @var{where} and @var{props} have the same meaning as for @code{add-function} diff --git a/lisp/ChangeLog b/lisp/ChangeLog index e885c924487..a1635bc475c 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,10 @@ +2014-11-18 Leo Liu + + * emacs-lisp/nadvice.el (define-advice): New macro. + * emacs-lisp/lisp-mode.el (lisp-imenu-generic-expression): Add + define-advice. + (lisp-font-lock-keywords-1): Add define-advice. + 2014-11-18 Daiki Ueno * epg.el (epg-context): New slot EDIT-CALLBACK. diff --git a/lisp/emacs-lisp/lisp-mode.el b/lisp/emacs-lisp/lisp-mode.el index a13baf0ee22..d84113b418a 100644 --- a/lisp/emacs-lisp/lisp-mode.el +++ b/lisp/emacs-lisp/lisp-mode.el @@ -96,7 +96,7 @@ '("defun" "defmacro" ;; Elisp. "defun*" "defsubst" - "defadvice" "define-skeleton" + "define-advice" "defadvice" "define-skeleton" "define-compilation-mode" "define-minor-mode" "define-global-minor-mode" "define-globalized-minor-mode" @@ -195,7 +195,7 @@ "ignore-errors" "dotimes" "dolist" "declare")) (lisp-errs '("warn" "error" "signal")) ;; Elisp constructs. FIXME: update dynamically from obarray. - (el-fdefs '("defadvice" "defalias" + (el-fdefs '("define-advice" "defadvice" "defalias" "define-derived-mode" "define-minor-mode" "define-generic-mode" "define-global-minor-mode" "define-globalized-minor-mode" "define-skeleton" diff --git a/lisp/emacs-lisp/nadvice.el b/lisp/emacs-lisp/nadvice.el index bfd939d69e2..a81d3e43de3 100644 --- a/lisp/emacs-lisp/nadvice.el +++ b/lisp/emacs-lisp/nadvice.el @@ -441,6 +441,30 @@ of the piece of advice." (fset symbol (car (get symbol 'advice--saved-rewrite))))))) nil) +;;;###autoload +(defmacro define-advice (symbol args &rest body) + "Define an advice and add it to function named SYMBOL. +See `advice-add' and `add-function' for explanation on the +arguments. Note if NAME is nil the advice is anonymous; +otherwise it is named `SYMBOL@NAME'. + +\(fn SYMBOL (WHERE LAMBDA-LIST &optional NAME DEPTH) &rest BODY)" + (declare (indent 2) (doc-string 3) (debug (sexp sexp body))) + (or (listp args) (signal 'wrong-type-argument (list 'listp args))) + (or (<= 2 (length args) 4) + (signal 'wrong-number-of-arguments (list 2 4 (length args)))) + (let* ((where (nth 0 args)) + (lambda-list (nth 1 args)) + (name (nth 2 args)) + (depth (nth 3 args)) + (props (and depth `((depth . ,depth)))) + (advice (cond ((null name) `(lambda ,lambda-list ,@body)) + ((or (stringp name) (symbolp name)) + (intern (format "%s@%s" symbol name))) + (t (error "Unrecognized name spec `%S'" name))))) + `(prog1 ,@(and (symbolp advice) `((defun ,advice ,lambda-list ,@body))) + (advice-add ',symbol ,where #',advice ,@(and props `(',props)))))) + (defun advice-mapc (fun symbol) "Apply FUN to every advice function in SYMBOL. FUN is called with a two arguments: the function that was added, and the -- 2.39.5