aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard M. Stallman1995-08-08 06:15:53 +0000
committerRichard M. Stallman1995-08-08 06:15:53 +0000
commitaa9b77f67e8e6d6b11abe7510134f5fc92c114df (patch)
tree7d6a5ce9d4c41115bd48ff1b0ae591663b597faa
parent3e1fb00f0b2b1f85a4b87f5695103938d636020f (diff)
downloademacs-aa9b77f67e8e6d6b11abe7510134f5fc92c114df.tar.gz
emacs-aa9b77f67e8e6d6b11abe7510134f5fc92c114df.zip
New node, Tips for Defining.
-rw-r--r--lispref/variables.texi67
1 files changed, 67 insertions, 0 deletions
diff --git a/lispref/variables.texi b/lispref/variables.texi
index 34cd07b6769..942afd7bd92 100644
--- a/lispref/variables.texi
+++ b/lispref/variables.texi
@@ -31,6 +31,8 @@ variable.
31* Local Variables:: Variable values that exist only temporarily. 31* Local Variables:: Variable values that exist only temporarily.
32* Void Variables:: Symbols that lack values. 32* Void Variables:: Symbols that lack values.
33* Defining Variables:: A definition says a symbol is used as a variable. 33* Defining Variables:: A definition says a symbol is used as a variable.
34* Tips for Defining:: How to avoid bad results from quitting
35 within the code to initialize a variable.
34* Accessing Variables:: Examining values of variables whose names 36* Accessing Variables:: Examining values of variables whose names
35 are known only at run time. 37 are known only at run time.
36* Setting Variables:: Storing new values in variables. 38* Setting Variables:: Storing new values in variables.
@@ -555,6 +557,71 @@ what we really want. To prevent it, use these special forms at top
555level in a file, where normally no local binding is in effect, and make 557level in a file, where normally no local binding is in effect, and make
556sure to load the file before making a local binding for the variable. 558sure to load the file before making a local binding for the variable.
557 559
560@node Tips for Defining
561@section Tips for Defining Variables Robustly
562
563 When defining and initializing a variable that holds a complicated
564value (such as a keymap with bindings in it), it's best to put the
565entire computation of the value into the @code{defvar}, like this:
566
567@example
568(defvar my-mode-map
569 (let ((map (make-sparse-keymap)))
570 (define-key my-mode-map "\C-c\C-a" 'my-command)
571 @dots{}
572 map)
573 @var{docstring})
574@end example
575
576@noindent
577This method has several benefits. First, if the user quits while
578loading the file, the variable is either still uninitialized or
579initialized properly, never in-between. If it is uninitialized,
580reloading the file will initialize it properly. Second, reloading the
581file once the variable is initialized will not alter it; that is
582important if the user has run hooks to alter part of the contents (such
583as, to rebind keys). Third, evaluating the @code{defvar} form with
584@kbd{C-M-x} @emph{will} reinitialize the map completely.
585
586 Putting so much code in the @code{defvar} form has one disadvantage:
587it puts the documentation string far away from the line which names the
588variable. Here's a safe way to avoid that:
589
590@example
591(defvar my-mode-map nil
592 @var{docstring})
593(if my-mode-map
594 nil
595 (let ((map (make-sparse-keymap)))
596 (define-key my-mode-map "\C-c\C-a" 'my-command)
597 @dots{}
598 (setq my-mode-map map)))
599@end example
600
601@noindent
602This has all the same advantages as putting the initialization inside
603the @code{defvar}, except that you must type @kbd{C-M-x} twice, once on
604each form, if you do want to reinitialize the variable.
605
606 But be careful not to write the code like this:
607
608@example
609(defvar my-mode-map nil
610 @var{docstring})
611(if my-mode-map
612 nil
613 (setq my-mode-map (make-sparse-keymap))
614 (define-key my-mode-map "\C-c\C-a" 'my-command)
615 @dots{})
616@end example
617
618@noindent
619This code sets the variable, then alters it, but only if the variable
620had been @code{ni}. If the user quits just after the @code{setq}, that
621leaves the variable neither correctly initialized nor void nor
622@code{nil}. Once that happens, reloading the file will not initialize
623the variable; it will remain incomplete.
624
558@node Accessing Variables 625@node Accessing Variables
559@section Accessing Variable Values 626@section Accessing Variable Values
560 627