From fb619c1c22b986841964b09b7effe42785f3fed8 Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Wed, 27 Dec 2017 17:49:39 -0500 Subject: [PATCH] (delayed-after-hook-functions): Tighten the code a bit * lisp/subr.el (delayed-mode-hooks, delayed-after-hook-functions): Use defvar-local. (run-mode-hooks): Reset delayed-after-hook-functions before running those functions, in case an error is signaled. * lisp/emacs-lisp/derived.el (define-derived-mode): Don't duplicate code. --- lisp/emacs-lisp/derived.el | 10 ++++------ lisp/subr.el | 12 +++++------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/lisp/emacs-lisp/derived.el b/lisp/emacs-lisp/derived.el index c0ef199424b..3bb8eb4f2f2 100644 --- a/lisp/emacs-lisp/derived.el +++ b/lisp/emacs-lisp/derived.el @@ -281,12 +281,10 @@ No problems result if this variable is not bound. ; Splice in the body (if any). ,@body ) - ;; Run the hooks, if any. - (run-mode-hooks ',hook) - ,@(when after-hook - `((if delay-mode-hooks - (push (lambda () ,after-hook) delayed-after-hook-functions) - ,after-hook))))))) + ,@(when after-hook + `((push (lambda () ,after-hook) delayed-after-hook-functions))) + ;; Run the hooks (and delayed-after-hook-functions), if any. + (run-mode-hooks ',hook))))) ;; PUBLIC: find the ultimate class of a derived mode. diff --git a/lisp/subr.el b/lisp/subr.el index 67209b4d4f6..76d7a1a556d 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -1839,15 +1839,13 @@ if it is empty or a duplicate." (defvar delay-mode-hooks nil "If non-nil, `run-mode-hooks' should delay running the hooks.") -(defvar delayed-mode-hooks nil +(defvar-local delayed-mode-hooks nil "List of delayed mode hooks waiting to be run.") -(make-variable-buffer-local 'delayed-mode-hooks) (put 'delay-mode-hooks 'permanent-local t) -(defvar delayed-after-hook-functions nil +(defvar-local delayed-after-hook-functions nil "List of delayed :after-hook forms waiting to be run. These forms come from `define-derived-mode'.") -(make-variable-buffer-local 'delayed-after-hook-functions) (defvar change-major-mode-after-body-hook nil "Normal hook run in major mode functions, before the mode hooks.") @@ -1889,9 +1887,9 @@ running their FOO-mode-hook." (with-demoted-errors "File local-variables error: %s" (hack-local-variables 'no-mode))) (run-hooks 'after-change-major-mode-hook) - (dolist (fun (nreverse delayed-after-hook-functions)) - (funcall fun)) - (setq delayed-after-hook-functions nil))) + (dolist (fun (prog1 (nreverse delayed-after-hook-functions) + (setq delayed-after-hook-functions nil))) + (funcall fun)))) (defmacro delay-mode-hooks (&rest body) "Execute BODY, but delay any `run-mode-hooks'. -- 2.39.2