help-gnu-emacs
[Top][All Lists]
Advanced

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

RE: defcustom: changing from defvar - order of execution


From: Drew Adams
Subject: RE: defcustom: changing from defvar - order of execution
Date: Wed, 4 May 2005 15:49:48 -0700

    You're comparing two different elisp package code and two
    different .emacs code.  Try to separate those two cases;...

    E.g. changing the elisp package to use defcustom, but without the user
    changing her .emacs will not result in any problem.
    The problem comes when the user replaces her "setq at the
    beginning" with a "custom-set-var at the end".

    It's only weakly related to the use of custom since the same behavior
    appears if you move the setq, or if you move the custom-set-var.

Let me try to be clearer.

The only differences are these:

 - the library in setup #1 uses defvar;
   the library in setup #2 uses defcustom

 - the user in setup #1 uses setq at the beginning;
   the user in setup #2 customizes the variable using Customize,
   which adds (custom-set-variables...) at the end of .emacs

The user in setup #2 has never used the library, let's say (and so, does not
have the setq in .emacs). The user knows nothing about setq and Lisp; he
just copies the text "(load-library "foo")(foo-fn)", per the library
comments, to his .emacs. He fires up Emacs, and then uses Customize to set
and save foovar. This causes (custom-set-variables...) to be added to the
end of his .emacs - after the load-library and the call to foo-fn.

The user is then surprised to find that the customization had no effect: in
subsequent Emacs sessions, load-library does the defcustom, which sets
foovar to nil; and foo-fn then executes with foovar=nil, returning `t'.

What am I missing?

I'm not trying to suggest something is wrong with Emacs (e.g. Customization)
here, I'm just wondering how to use defcustom so that users can customize
things. Telling them that they need to either use setq or to move the
custom-set-variables form that Customize inserts at the end of .emacs seems
the wrong way to proceed. I assume that I must be doing something wrong wrt
the library definition, that if I do things correctly there should be no
such problem. So, what am I missing here?

Thanks,

  Drew

--- my original message ---

    I'm unclear on the recommended way to change Lisp code from
    using defvar for user options to using defcustom. I guess I'm
    also unclear on just how defcustom and Customize work. I
    couldn't find an explanation of this in Info for Emacs or Emacs Lisp.

    Initial setup - library foo.el has this:

     (defvar foovar nil)
     (defun foo-fn () foovar)

    Initial setup - user has this in .emacs:

     (defvar foovar t)
     (load-library "foo")
     (foo-fn)

    When .emacs is loaded, (foo-fn) is executed, foovar is `t', so
    (foo-fn) returns `t'.


    Second setup - library foo.el has this:

     (defcustom foovar nil)
     (defun foo-fn () foovar)

    Second setup - user has this in .emacs, after customizing
    foovar to `t' with Customize:

     (load-library "foo")
     (foo-fn)
     ...
     (custom-set-variables '(foovar t)...)

    When .emacs is loaded, (foo-fn) is executed, foovar is (has
    default value) `nil', so (foo-fn) returns `nil'.

    That is, because (custom-set-variables...) is executed after
    (foo-fn), it has no effect during foo-fn's execution.

    What am I missing/confusing here? What is the proper way for
    library foo.el to move from using defvar to using defcustom?
    Also, what difference does it make, if any, whether a defcustom
    is executed at the top level or a lower level of a file.








reply via email to

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