[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Customize and autoloaded libraries

From: Per Abrahamsen
Subject: Re: Customize and autoloaded libraries
Date: Wed, 05 Dec 2001 12:20:20 +0100
User-agent: Gnus/5.090004 (Oort Gnus v0.04) Emacs/21.1 (i686-pc-linux-gnu)

Actually, I changed my mind.  Stefan's suggestion seems superior.

Make defcustom (or defhook) merge the saved or standard value for the
hook into the current value on initialization.  The implementation
could even use "add-hook" to do the merging.

The change is trivial and localized, it is simply a question of having
a different default value for :initialize for options where the type
is 'hook. 

Here is some sample code:

(defun custom-initialize-hook (symbol value)
  "Initialize SYMBOL with VALUE.

If symbol has a `saved-value' property, it will evaluate the car of
that.  The result should be a list of hook functions, which will be
added to SYMBOL using `add-hook'.  If there is no `saved-value'
property, VALUE will be evaluated instead to get the list of hook
functions to add to the symbol.

This is for initialization hook variables.  It is different from the
other initialize functions in that the saved or initial value is
merged into the existing value, allowing the user to use `add-hook'
before the declaration of the hook variable."
  (let ((hooks (if (get symbol 'saved-value)
                   (eval (car (get symbol 'saved-value)))
                 (eval value))))
    (mapc (lambda (fun) (add-hook symbol fun)) hooks)))

(defmacro defhook (symbol value doc &rest args)
  "Declare SYMBOL as a customizable hook that defaults to VALUE.
DOC is the variable documentation.

This macro is identical to `defcustom', except the default
value for :type is `hook' and the default value for :initialize is
  ;; It is better not to use backquote in this file,
  ;; because that makes a bootstrapping problem
  ;; if you need to recompile all the Lisp files using interpreted code.
  (nconc (list 'custom-declare-variable
               (list 'quote symbol)
               (list 'quote value)
               :initialize (list 'quote 'custom-initialize-hook)
               :type (list 'quote 'hook))

;; Test using `defcustom':

(add-hook 'foo-hook 'early)

(defcustom foo-hook '(bar baz)
  :initialize 'custom-initialize-hook
  :type 'hook)

;; foo-hook's value is (baz bar early)

;; Test using `defhook':

(add-hook 'bar-hook 'early)

(defhook bar-hook '(foo baz)
  "Testing 2")

;; bar-hook's value is (baz foo early)

reply via email to

[Prev in Thread] Current Thread [Next in Thread]