unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Artur Malabarba <bruce.connor.am@gmail.com>
To: emacs-devel <emacs-devel@gnu.org>
Subject: add-hook and defvar
Date: Sat, 21 Feb 2015 17:55:07 -0200	[thread overview]
Message-ID: <CAAdUY-Kn_VOOASJGh8WN5cc8jY1kmXJ5GpG=JDzYDahbOkzmfg@mail.gmail.com> (raw)

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



             reply	other threads:[~2015-02-21 19:55 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-02-21 19:55 Artur Malabarba [this message]
2015-02-21 21:05 ` add-hook and defvar Drew Adams
2015-02-21 22:35 ` Robin Templeton
2015-02-23  5:00   ` Artur Malabarba
2015-02-22 19:18 ` Stefan Monnier
2015-02-23  5:10   ` Artur Malabarba
2015-02-23 22:31     ` Stefan Monnier
2015-02-24  6:29       ` Philipp Stephani
2015-02-25  2:56         ` Stefan Monnier
2015-03-05 20:28           ` Philipp Stephani
2015-03-05 21:45             ` Artur Malabarba
2015-03-06 19:05               ` Philipp Stephani
2015-03-06 20:27                 ` Artur Malabarba
2015-03-10  2:14             ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAAdUY-Kn_VOOASJGh8WN5cc8jY1kmXJ5GpG=JDzYDahbOkzmfg@mail.gmail.com' \
    --to=bruce.connor.am@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).