From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Artur Malabarba Newsgroups: gmane.emacs.devel Subject: add-hook and defvar Date: Sat, 21 Feb 2015 17:55:07 -0200 Message-ID: Reply-To: bruce.connor.am@gmail.com NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: ger.gmane.org 1424548536 718 80.91.229.3 (21 Feb 2015 19:55:36 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 21 Feb 2015 19:55:36 +0000 (UTC) To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Feb 21 20:55:30 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1YPG9B-0003sE-8Q for ged-emacs-devel@m.gmane.org; Sat, 21 Feb 2015 20:55:29 +0100 Original-Received: from localhost ([::1]:37239 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YPG9A-0008Hr-Oz for ged-emacs-devel@m.gmane.org; Sat, 21 Feb 2015 14:55:28 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:43202) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YPG8t-0008Ha-8V for emacs-devel@gnu.org; Sat, 21 Feb 2015 14:55:12 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YPG8r-0001gv-Dh for emacs-devel@gnu.org; Sat, 21 Feb 2015 14:55:11 -0500 Original-Received: from mail-la0-x22a.google.com ([2a00:1450:4010:c03::22a]:40728) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YPG8r-0001gO-0a for emacs-devel@gnu.org; Sat, 21 Feb 2015 14:55:09 -0500 Original-Received: by labgd6 with SMTP id gd6so11960675lab.7 for ; Sat, 21 Feb 2015 11:55:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:sender:date:message-id:subject:from:to :content-type; bh=Z9EIEgoMjsAT57CwR9TNSM6hOwGUM8hiOXmNq+04rzk=; b=MFAtkZ2J0Vc6kHcNCly2mPBTUB3qNNJ5UJqGhnZmrF7rkSnjC9229UuANfBEw+d2Iv WrKF7N483P5YrwwRH+S514yRz8RP8F1WMhYMJcmoveV0SI6WTRwYS8KCStcXx2/VS97m LK2WozszgnnZ61EP/PV7Bhu288AIx2aDQM/+2HfQj+4Fb0NjcHTjgumMzDYSHKaiucJn jIOBgahAxFPCJERgbHciLUc4SAYRVc4LU2/kxeKocXIHZPfq3kxQNvRjFVIsAfJIQKR2 L5pmSebwARWyc+CteQ565I0zyjgIz1JhOn+yuFC8lpo8YXMaAAf0HwHFOvcOzVWIPFRw RouQ== X-Received: by 10.112.110.231 with SMTP id id7mr3317659lbb.28.1424548507414; Sat, 21 Feb 2015 11:55:07 -0800 (PST) Original-Received: by 10.112.49.9 with HTTP; Sat, 21 Feb 2015 11:55:07 -0800 (PST) X-Google-Sender-Auth: bLgBg8yAjvPd5KoNUg0QYLYBUqU X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4010:c03::22a X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:183361 Archived-At: While it's nice that `add-hook' tries to do the right thing even when the variable isn't defined yet, that's a bit of a false assurance. Although it's documented in the docstring, it seems undesirable that it sets the variable. This means that if the hook's defvar were to specify an initial value, this value won't take effect because of that preceding `add-hook' invocation. In this situation, add-hook doesn't so much "add" as it actually sets the value of the hook. The following patch is one way of having `add-hook' always add, without overriding the "future" initial value of the hook. It is not complete, it's intended as a possible outline (hopefully a conversation starter). The big disadvantage is that it's not backwards compatible. Any code out there that relies on the variable being set by `add-hook' will break. There's also the disadvantage that it adds a symbol-property check to every `defvar' call. Any thoughts? --- lisp/subr.el | 68 ++++++++++++++++++++++++++++++++---------------------------- src/eval.c | 11 +++++++++- 2 files changed, 46 insertions(+), 33 deletions(-) diff --git a/lisp/subr.el b/lisp/subr.el index deadca6..a9a7aef 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -1389,38 +1389,42 @@ functions of the global value as well as in the local value. HOOK should be a symbol, and FUNCTION may be any valid function. If HOOK is void, it is first set to nil. If HOOK's value is a single function, it is changed to a list of functions." - (or (boundp hook) (set hook nil)) - (or (default-boundp hook) (set-default hook nil)) - (if local (unless (local-variable-if-set-p hook) - (set (make-local-variable hook) (list t))) - ;; Detect the case where make-local-variable was used on a hook - ;; and do what we used to do. - (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook))) - (setq local t))) - (let ((hook-value (if local (symbol-value hook) (default-value hook)))) - ;; If the hook value is a single function, turn it into a list. - (when (or (not (listp hook-value)) (functionp hook-value)) - (setq hook-value (list hook-value))) - ;; Do the actual addition if necessary - (unless (member function hook-value) - (when (stringp function) - (setq function (purecopy function))) - (setq hook-value - (if append - (append hook-value (list function)) - (cons function hook-value)))) - ;; Set the actual variable - (if local - (progn - ;; If HOOK isn't a permanent local, - ;; but FUNCTION wants to survive a change of modes, - ;; mark HOOK as partially permanent. - (and (symbolp function) - (get function 'permanent-local-hook) - (not (get hook 'permanent-local)) - (put hook 'permanent-local 'permanent-local-hook)) - (set hook hook-value)) - (set-default hook hook-value)))) + (if (not (boundp hook)) + ;; If the hook is not defined yet, store the value until it is. + (push (list function append (when local (current-buffer))) + (get hook 'pre-hooked-functions)) + + ;; Everything below this line wasn't changed (indentation only). + (if local (unless (local-variable-if-set-p hook) + (set (make-local-variable hook) (list t))) + ;; Detect the case where make-local-variable was used on a hook + ;; and do what we used to do. + (unless (and (consp (symbol-value hook)) (memq t (symbol-value hook))) + (setq local t))) + (let ((hook-value (if local (symbol-value hook) (default-value hook)))) + ;; If the hook value is a single function, turn it into a list. + (when (or (not (listp hook-value)) (functionp hook-value)) + (setq hook-value (list hook-value))) + ;; Do the actual addition if necessary + (unless (member function hook-value) + (when (stringp function) + (setq function (purecopy function))) + (setq hook-value + (if append + (append hook-value (list function)) + (cons function hook-value)))) + ;; Set the actual variable + (if local + (progn + ;; If HOOK isn't a permanent local, + ;; but FUNCTION wants to survive a change of modes, + ;; mark HOOK as partially permanent. + (and (symbolp function) + (get function 'permanent-local-hook) + (not (get hook 'permanent-local)) + (put hook 'permanent-local 'permanent-local-hook)) + (set hook hook-value)) + (set-default hook hook-value))))) (defun remove-hook (hook function &optional local) "Remove from the value of HOOK the function FUNCTION. diff --git a/src/eval.c b/src/eval.c index e828da9..119ffe0 100644 --- a/src/eval.c +++ b/src/eval.c @@ -729,7 +729,7 @@ To define a user option, use `defcustom' instead of `defvar'. usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) (Lisp_Object args) { - Lisp_Object sym, tem, tail; + Lisp_Object sym, tem, tail, pre_hooked; sym = XCAR (args); tail = XCDR (args); @@ -763,6 +763,15 @@ usage: (defvar SYMBOL &optional INITVALUE DOCSTRING) */) tem = Fpurecopy (tem); Fput (sym, Qvariable_documentation, tem); } + /* In the final version, this should probably be + Qpre_hooked_functions. */ + pre_hooked = Fget (sym, intern ("pre-hooked-functions"));; + if (!NILP (pre_hooked)) + { + /* do something like add-hook on each element of the + pre_hooked list. */ + } + LOADHIST_ATTACH (sym); } else if (!NILP (Vinternal_interpreter_environment) -- 2.3.0