-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariables.texi
2446 lines (2195 loc) · 115 KB
/
variables.texi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
@c -*-texinfo-*-
@c This is part of the GNU Emacs Lisp Reference Manual.
@c Copyright (C) 1990, 1991, 1992, 1993, 1994, 1995, 1998 Free Software Foundation, Inc.
@c See the file elisp.texi for copying conditions.
@setfilename ../info/variables
@node Variables, Functions, Control Structures, Top
@c @chapter Variables
@chapter 変数
@c @cindex variable
@cindex 変数
@c A @dfn{variable} is a name used in a program to stand for a value.
@c Nearly all programming languages have variables of some sort. In the
@c text of a Lisp program, variables are written using the syntax for
@c symbols.
@dfn{変数}(variable)は、プログラムにおいて値を表すために使う名前です。
ほとんどすべてのプログラム言語には、ある種の変数があります。
Lispプログラムのテキストでは、シンボルの構文を使って変数を書きます。
@c In Lisp, unlike most programming languages, programs are represented
@c primarily as Lisp objects and only secondarily as text. The Lisp
@c objects used for variables are symbols: the symbol name is the variable
@c name, and the variable's value is stored in the value cell of the
@c symbol. The use of a symbol as a variable is independent of its use as
@c a function name. @xref{Symbol Components}.
ほとんどのプログラム言語と違って、Lispでは、
プログラムはLispオブジェクトで表現し、テキスト表現は副次的なものです。
変数として使うLispオブジェクトはシンボルです。
シンボル名が変数名であり、変数の値はシンボルの値セルに格納されています。
変数としてのシンボルの使い方は、関数名としての使い方とは独立しています。
@xref{Symbol Components}。
@c The Lisp objects that constitute a Lisp program determine the textual
@c form of the program---it is simply the read syntax for those Lisp
@c objects. This is why, for example, a variable in a textual Lisp program
@c is written using the read syntax for the symbol that represents the
@c variable.
Lispプログラムを構成するLispオブジェクト群は、
プログラムのテキスト表現を決定します。
つまり、Lispオブジェクト群に対する単なる入力構文です。
これは、たとえば、Lispプログラムのテキスト表現では、
変数を表現するシンボルの入力構文で変数を書く理由です。
@menu
* Global Variables:: Variable values that exist permanently, everywhere.
* Constant Variables:: Certain "variables" have values that never change.
* Local Variables:: Variable values that exist only temporarily.
* Void Variables:: Symbols that lack values.
* Defining Variables:: A definition says a symbol is used as a variable.
* Tips for Defining:: How to avoid bad results from quitting
within the code to initialize a variable.
* Accessing Variables:: Examining values of variables whose names
are known only at run time.
* Setting Variables:: Storing new values in variables.
* Variable Scoping:: How Lisp chooses among local and global values.
* Buffer-Local Variables:: Variable values in effect only in one buffer.
* Frame-Local Variables:: Variable values in effect only in one frame.
* Future Local Variables:: New kinds of local values we might add some day.
@end menu
@node Global Variables
@c @section Global Variables
@section グローバル変数
@c @cindex global variable
@cindex グローバル変数
@cindex 変数、グローバル
@c The simplest way to use a variable is @dfn{globally}. This means that
@c the variable has just one value at a time, and this value is in effect
@c (at least for the moment) throughout the Lisp system. The value remains
@c in effect until you specify a new one. When a new value replaces the
@c old one, no trace of the old value remains in the variable.
変数を使うもっとも簡単な方法は、
@dfn{グローバルに}(globally、大局的に)使うことです。
つまり、どんなときにも変数にはたった1つの値だけがあり、
(少なくともここでは)Lispシステム全体にその値が有効になります。
新たな値を設定するまで、その値が有効であり続けます。
新たな値で古い値を置き換えると、変数には古い値の痕跡はなにも残りません。
@c You specify a value for a symbol with @code{setq}. For example,
シンボルの値は@code{setq}で指定します。
たとえば、
@example
(setq x '(a b))
@end example
@noindent
@c gives the variable @code{x} the value @code{(a b)}. Note that
@c @code{setq} does not evaluate its first argument, the name of the
@c variable, but it does evaluate the second argument, the new value.
は、変数@code{x}に値@code{(a b)}を与えます。
@code{setq}は、最初の引数、つまり、変数の名前を評価せず、
新しい値である第2引数を評価することに注意してください。
@c Once the variable has a value, you can refer to it by using the symbol
@c by itself as an expression. Thus,
変数にいったん値を与えれば、
式としてシンボルそのものを使うことによりその値を参照できます。
つまり、つぎのとおりです。
@example
@group
x @result{} (a b)
@end group
@end example
@noindent
@c assuming the @code{setq} form shown above has already been executed.
ただし、上に示したフォーム@code{setq}を実行してあると仮定します。
@c If you do set the same variable again, the new value replaces the old
@c one:
同じ変数に値を設定し直すと、
新しい値で古い値を置き換えます。
@example
@group
x
@result{} (a b)
@end group
@group
(setq x 4)
@result{} 4
@end group
@group
x
@result{} 4
@end group
@end example
@node Constant Variables
@c @section Variables That Never Change
@section 変更不可能な変数
@vindex nil
@vindex t
@kindex setting-constant
@c In Emacs Lisp, certain symbols normally evaluate to themselves. These
@c include @code{nil} and @code{t}, as well as any symbol whose name starts
@c with @samp{:}. These symbols cannot be rebound, nor can their values be
@c changed. Any attempt to set or bind @code{nil} or @code{t} signals a
@c @code{setting-constant} error. The same is true for a symbol whose name
@c starts with @samp{:}, except that you are allowed to set such a symbol to
@c itself.
Emacs Lispには、通常それ自身に評価されるある種のシンボルがあります。
@samp{:}で始まる名前の任意の変数、および、@code{nil}と@code{t}です。
これらのシンボルを再束縛することはできず、
それらの値を変更することもできません。
@code{nil}や@code{t}を設定しようとしたり束縛しようとすると、
エラー@code{setting-constant}を通知します。
@samp{:}で始まる名前のシンボルに関してもそうですが、
そのようなシンボルにそれ自身を設定することはできます。
@example
@group
nil @equiv{} 'nil
@result{} nil
@end group
@group
(setq nil 500)
@error{} Attempt to set constant symbol: nil
@end group
@end example
@defvar keyword-symbols-constant-flag
@tindex keyword-symbols-constant-flag
@c If this variable is @code{nil}, you are allowed to set and bind symbols
@c whose names start with @samp{:} as you wish. This is to make it
@c possible to run old Lisp programs which do that.
この変数が@code{nil}であると、
@samp{:}で始まる名前の変数を望みの値に設定したり束縛したりできる。
これは、そのようなことを行う古いLispプログラムの実行を可能にするためである。
@end defvar
@node Local Variables
@c @section Local Variables
@section ローカル変数
@c @cindex binding local variables
@c @cindex local variables
@c @cindex local binding
@c @cindex global binding
@cindex ローカル変数束縛
@cindex ローカル変数
@cindex 変数、ローカル
@cindex ローカル束縛
@cindex グローバル束縛
@c Global variables have values that last until explicitly superseded
@c with new values. Sometimes it is useful to create variable values that
@c exist temporarily---only until a certain part of the program finishes.
@c These values are called @dfn{local}, and the variables so used are
@c called @dfn{local variables}.
グローバル変数は、
明示的に新しい値で置き換えない限り存続する値を持ちます。
一時的にしか存在しない変数値、
つまり、プログラムのある部分を完了するまでのみ存在する変数値を
作れると便利なことがあります。
このような値を@dfn{ローカル}(local、局所的)と呼び、
そのように使われる変数を@dfn{ローカル変数}(local variables)と呼びます。
@c For example, when a function is called, its argument variables receive
@c new local values that last until the function exits. The @code{let}
@c special form explicitly establishes new local values for specified
@c variables; these last until exit from the @code{let} form.
たとえば、関数を呼び出したとき、その引数変数は、
関数を抜けるまで存続する新たなローカルな値を受け取ります。
スペシャルフォーム@code{let}は、指定した変数の新たなローカル値を
明示的に確立します。
これらはフォーム@code{let}を抜けるまで存続します。
@c @cindex shadowing of variables
@cindex 変数を隠す
@c Establishing a local value saves away the previous value (or lack of
@c one) of the variable. When the life span of the local value is over,
@c the previous value is restored. In the mean time, we say that the
@c previous value is @dfn{shadowed} and @dfn{not visible}. Both global and
@c local values may be shadowed (@pxref{Scope}).
ローカル値を確立すると、
変数の以前の値(あるいは値がないこと)を保存します。
ローカル値の存続期間が終了すると、以前の値を復元します。
この期間は、以前の値を@dfn{隠して}(shadowed)いて
以前の値は@dfn{見えません}。
グローバル値でもローカル値でも隠せます(@pxref{Scope})。
@c If you set a variable (such as with @code{setq}) while it is local,
@c this replaces the local value; it does not alter the global value, or
@c previous local values, that are shadowed. To model this behavior, we
@c speak of a @dfn{local binding} of the variable as well as a local value.
変数がローカルなときに(@code{setq}などで)その変数を設定すると、
ローカル値を置き換えます。
隠されているグローバル値や以前のローカル値を変更しません。
このふるまいをモデル化するために、
変数のローカル値に加えて変数の@dfn{ローカル束縛}(local binding)を
考えます。
@c The local binding is a conceptual place that holds a local value.
@c Entry to a function, or a special form such as @code{let}, creates the
@c local binding; exit from the function or from the @code{let} removes the
@c local binding. As long as the local binding lasts, the variable's value
@c is stored within it. Use of @code{setq} or @code{set} while there is a
@c local binding stores a different value into the local binding; it does
@c not create a new binding.
ローカル束縛とは、ローカル値を保持する概念的な場所です。
関数や@code{let}などのスペシャルフォームに入るたびに
ローカル束縛を作成します。
関数やフォーム@code{let}から抜けるとローカル束縛を削除します。
ローカル束縛が存続する限り、変数の値はそこに保持されています。
ローカル束縛が存在するときに@code{setq}や@code{set}を使うと、
ローカル束縛の中に別の値を格納します。
新たな束縛を作るのではありません。
@c We also speak of the @dfn{global binding}, which is where
@c (conceptually) the global value is kept.
グローバル値を保持する概念的な場所を
@dfn{グローバル束縛}(global binding)ともいいます。
@c @cindex current binding
@cindex 現在の束縛
@c A variable can have more than one local binding at a time (for
@c example, if there are nested @code{let} forms that bind it). In such a
@c case, the most recently created local binding that still exists is the
@c @dfn{current binding} of the variable. (This rule is called
@c @dfn{dynamic scoping}; see @ref{Variable Scoping}.) If there are no
@c local bindings, the variable's global binding is its current binding.
@c We sometimes call the current binding the @dfn{most-local existing
@c binding}, for emphasis. Ordinary evaluation of a symbol always returns
@c the value of its current binding.
変数には一度に複数のローカル束縛がありえます
(たとえば、同じ変数を束縛する入れ子になったフォーム@code{let}があるとき)。
そのような場合、既存のもっとも最近に作成されたローカル束縛が、
変数の@dfn{現在の束縛}(current binding)です。
(この規則を@dfn{動的スコープ}(dynamic scoping)と呼びます。
@pxref{Variable Scoping})
ローカル束縛がまったくなければ、変数のグローバル束縛が現在の束縛です。
現在の束縛のことを強調して@dfn{既存の最ローカル束縛}と呼ぶこともあります。
シンボルの通常の評価では、その現在の束縛の値を返します。
@c The special forms @code{let} and @code{let*} exist to create
@c local bindings.
スペシャルフォーム@code{let}や@code{let*}は、
ローカル束縛を作るためにあります。
@defspec let (bindings@dots{}) forms@dots{}
@c This special form binds variables according to @var{bindings} and then
@c evaluates all of the @var{forms} in textual order. The @code{let}-form
@c returns the value of the last form in @var{forms}.
このスペシャルフォームは、@var{bindings}に従って変数を束縛し、
@var{forms}のすべてをテキスト上の順に評価する。
@code{let}フォームは、@var{forms}の最後のフォームの値を返す。
@c Each of the @var{bindings} is either @w{(i) a} symbol, in which case
@c that symbol is bound to @code{nil}; or @w{(ii) a} list of the form
@c @code{(@var{symbol} @var{value-form})}, in which case @var{symbol} is
@c bound to the result of evaluating @var{value-form}. If @var{value-form}
@c is omitted, @code{nil} is used.
@var{bindings}のおのおのは、@w{(i)}シンボルであるか、
@w{(ii)}フォーム@code{(@var{symbol} @var{value-form})}のリストである。
前者は、シンボルに@code{nil}を束縛する。
後者は、@var{symbol}に@var{value-form}の評価結果を束縛する。
@var{value-form}を省略すると@code{nil}を使う。
@c All of the @var{value-form}s in @var{bindings} are evaluated in the
@c order they appear and @emph{before} binding any of the symbols to them.
@c Here is an example of this: @code{Z} is bound to the old value of
@c @code{Y}, which is 2, not the new value of @code{Y}, which is 1.
@var{bindings}の@var{value-form}群すべてを現れる順に評価して@emph{から}、
シンボルにそれらの値を束縛する。
例をつぎに示す。
@code{Z}は、@code{Y}の古い値2に束縛され、@code{Y}の新しい値1ではない。
@example
@group
(setq Y 2)
@result{} 2
@end group
@group
(let ((Y 1)
(Z Y))
(list Y Z))
@result{} (1 2)
@end group
@end example
@end defspec
@defspec let* (bindings@dots{}) forms@dots{}
@c This special form is like @code{let}, but it binds each variable right
@c after computing its local value, before computing the local value for
@c the next variable. Therefore, an expression in @var{bindings} can
@c reasonably refer to the preceding symbols bound in this @code{let*}
@c form. Compare the following example with the example above for
@c @code{let}.
このスペシャルフォームは@code{let}に似ているが、
変数のローカル値を計算し終えた直後にその変数を束縛し、
つぎの変数のローカル値の計算に進む。
したがって、@var{bindings}内の式では、この@code{let*}フォーム内で
まえにあるシンボルを参照できる。
つぎの例を上の@code{let}の例と比較してほしい。
@example
@group
(setq Y 2)
@result{} 2
@end group
@group
(let* ((Y 1)
@c (Z Y)) ; @r{Use the just-established value of @code{Y}.}
(Z Y)) ; @r{設定し終えたばかりの@code{Y}の値を使う}
(list Y Z))
@result{} (1 1)
@end group
@end example
@end defspec
@c Here is a complete list of the other facilities that create local
@c bindings:
以下にローカル束縛を作成するその他の機能の完全な一覧をあげておきます。
@itemize @bullet
@item
@c Function calls (@pxref{Functions}).
関数呼び出し(@pxref{Functions})。
@item
@c Macro calls (@pxref{Macros}).
マクロ呼び出し(@pxref{Macros})。
@item
@c @code{condition-case} (@pxref{Errors}).
@code{condition-case}(@pxref{Errors})。
@end itemize
@c Variables can also have buffer-local bindings (@pxref{Buffer-Local
@c Variables}) and frame-local bindings (@pxref{Frame-Local Variables}); a
@c few variables have terminal-local bindings (@pxref{Multiple Displays}).
@c These kinds of bindings work somewhat like ordinary local bindings, but
@c they are localized depending on ``where'' you are in Emacs, rather than
@c localized in time.
変数は、バッファローカルな束縛(@pxref{Buffer-Local Variables}や
フレームローカルな束縛(@pxref{Frame-Local Variables})を持つことができます。
少数の変数は、端末にローカルな束縛(@pxref{Multiple Displays})
を持つこともできます。
この種の束縛は普通のローカル束縛と同じように働きますが、
これらはEmacsの『どの部分』にいるかに依存したローカル化であり、
時間的なローカル化ではありません。
@defvar max-specpdl-size
@c @cindex variable limit error
@c @cindex evaluation error
@c @cindex infinite recursion
@cindex 変数制限エラー
@cindex 評価エラー
@cindex 無限再帰
@c This variable defines the limit on the total number of local variable
@c bindings and @code{unwind-protect} cleanups (@pxref{Nonlocal Exits})
@c that are allowed before signaling an error (with data @code{"Variable
@c binding depth exceeds max-specpdl-size"}).
この変数は、(@code{"Variable binding depth exceeds max-specpdl-size"}を
伴った)エラーを通知するまでに許される、
ローカル変数束縛と
@code{unwind-protect}による後始末(@pxref{Nonlocal Exits})の
全体の個数の制限を定義する。
@c This limit, with the associated error when it is exceeded, is one way
@c that Lisp avoids infinite recursion on an ill-defined function.
@c @code{max-lisp-eval-depth} provides another limit on depth of nesting.
@c @xref{Eval}.
この制限、および、これを超えたときのエラーは、
不正に定義された関数によってLispが無限に再帰することを防止する
1つの方法である。
@c The default value is 600. Entry to the Lisp debugger increases the
@c value, if there is little room left, to make sure the debugger itself
@c has room to execute.
デフォルト値は600である。
Lispデバッガに入ったとき、
制限に近い場合にはデバッガ自身が実行できることを保証するために値を増やす。
@end defvar
@node Void Variables
@c @section When a Variable is ``Void''
@section 変数が『空』であるとき
@kindex void-variable
@c @cindex void variable
@cindex 空の変数
@c If you have never given a symbol any value as a global variable, we
@c say that that symbol's global value is @dfn{void}. In other words, the
@c symbol's value cell does not have any Lisp object in it. If you try to
@c evaluate the symbol, you get a @code{void-variable} error rather than
@c a value.
シンボルにグローバル変数としての値を一度も与えていないとき、
そのシンボルのグローバル値は@dfn{空}(void)であるといいます。
いいかえれば、シンボルの値セルにはどんなLispオブジェクトも入っていません。
シンボルを評価しようとすると、値ではなくエラー@code{void-variable}を得ます。
@c Note that a value of @code{nil} is not the same as void. The symbol
@c @code{nil} is a Lisp object and can be the value of a variable just as any
@c other object can be; but it is @emph{a value}. A void variable does not
@c have any value.
@code{nil}という値は空とは異なることに注意してください。
シンボル@code{nil}はLispオブジェクトであり、他のオブジェクトと同様に
変数の値になりえます。
それは@emph{値}なのです。
空な変数はいかなる値も持たないのです。
@c After you have given a variable a value, you can make it void once more
@c using @code{makunbound}.
変数に値を与えたあとでは、@code{makunbound}を使って
再度その変数を空にできます。
@defun makunbound symbol
@c This function makes the current variable binding of @var{symbol} void.
@c Subsequent attempts to use this symbol's value as a variable will signal
@c the error @code{void-variable}, unless and until you set it again.
この関数は、@var{symbol}の現在の変数束縛を空にする。
これ以降に変数としてこのシンボルの値を使おうとすると、
再度設定していない限り、エラー@code{void-variable}を通知する。
@c @code{makunbound} returns @var{symbol}.
@code{makunbound}は@var{symbol}を返す。
@example
@group
@c (makunbound 'x) ; @r{Make the global value of @code{x} void.}
(makunbound 'x) ; @r{変数@code{x}のグローバル値を空にする}
@result{} x
@end group
@group
x
@error{} Symbol's value as variable is void: x
@end group
@end example
@c If @var{symbol} is locally bound, @code{makunbound} affects the most
@c local existing binding. This is the only way a symbol can have a void
@c local binding, since all the constructs that create local bindings
@c create them with values. In this case, the voidness lasts at most as
@c long as the binding does; when the binding is removed due to exit from
@c the construct that made it, the previous local or global binding is
@c reexposed as usual, and the variable is no longer void unless the newly
@c reexposed binding was void all along.
@var{symbol}がローカルに束縛されていると、
@code{makunbound}は既存の最ローカル束縛に作用する。
ローカル束縛を作成するすべての構文は変数に値を与えるため、
これはシンボルのローカル束縛を空にする唯一の方法である。
この場面では、空の状態は、束縛が存在する限り存続する。
束縛を作成した構造から抜け出して束縛が削除されると、
通常どおりそれ以前のローカル束縛かグローバル束縛が有効になり、
その束縛が空でなければ変数は空ではない。
@smallexample
@group
@c (setq x 1) ; @r{Put a value in the global binding.}
(setq x 1) ; @r{グローバル束縛に値を入れる}
@result{} 1
@c (let ((x 2)) ; @r{Locally bind it.}
@c (makunbound 'x) ; @r{Void the local binding.}
(let ((x 2)) ; @r{ローカルに束縛する}
(makunbound 'x) ; @r{ローカル束縛を空にする}
x)
@error{} Symbol's value as variable is void: x
@end group
@group
@c x ; @r{The global binding is unchanged.}
x ; @r{グローバル束縛は変更されていない}
@result{} 1
@c (let ((x 2)) ; @r{Locally bind it.}
@c (let ((x 3)) ; @r{And again.}
@c (makunbound 'x) ; @r{Void the innermost-local binding.}
@c x)) ; @r{And refer: it's void.}
(let ((x 2)) ; @r{ローカルに束縛する}
(let ((x 3)) ; @r{もう一度}
(makunbound 'x) ; @r{もっとも内側のローカル束縛を空にする}
x)) ; @r{参照するが、それは空}
@error{} Symbol's value as variable is void: x
@end group
@group
(let ((x 2))
(let ((x 3))
@c (makunbound 'x)) ; @r{Void inner binding, then remove it.}
@c x) ; @r{Now outer @code{let} binding is visible.}
(makunbound 'x)) ; @r{内側の束縛を空にし、それを削除する}
x) ; @r{外側の@code{let}の束縛が見える}
@result{} 2
@end group
@end smallexample
@end defun
@c A variable that has been made void with @code{makunbound} is
@c indistinguishable from one that has never received a value and has
@c always been void.
@code{makunbound}で空にした変数は、
一度も値を受け取ったことがなく、そのために空である変数と区別できません。
@c You can use the function @code{boundp} to test whether a variable is
@c currently void.
変数が現在、空であるかどうかは関数@code{boundp}を使って調べられます。
@defun boundp variable
@c @code{boundp} returns @code{t} if @var{variable} (a symbol) is not void;
@c more precisely, if its current binding is not void. It returns
@c @code{nil} otherwise.
@code{boundp}は、(シンボル)@var{variable}が空でなければ、
より正確にいえば、現在の束縛が空でなければ@code{t}を返す。
さもなければ@code{nil}を返す。
@smallexample
@group
@c (boundp 'abracadabra) ; @r{Starts out void.}
(boundp 'abracadabra) ; @r{空で始める}
@result{} nil
@end group
@group
@c (let ((abracadabra 5)) ; @r{Locally bind it.}
(let ((abracadabra 5)) ; @r{ローカルに束縛する}
(boundp 'abracadabra))
@result{} t
@end group
@group
@c (boundp 'abracadabra) ; @r{Still globally void.}
(boundp 'abracadabra) ; @r{グローバルにはまだ空である}
@result{} nil
@end group
@group
@c (setq abracadabra 5) ; @r{Make it globally nonvoid.}
(setq abracadabra 5) ; @r{グローバルに空でなくする}
@result{} 5
@end group
@group
(boundp 'abracadabra)
@result{} t
@end group
@end smallexample
@end defun
@node Defining Variables
@c @section Defining Global Variables
@section グローバル変数を定義する
@c @cindex variable definition
@cindex 変数定義
@c You may announce your intention to use a symbol as a global variable
@c with a @dfn{variable definition}: a special form, either @code{defconst}
@c or @code{defvar}.
スペシャルフォーム@code{defconst}や@code{defvar}の@dfn{変数定義}を使って、
シンボルをグローバル変数として使う意図を表明できます。
@c In Emacs Lisp, definitions serve three purposes. First, they inform
@c people who read the code that certain symbols are @emph{intended} to be
@c used a certain way (as variables). Second, they inform the Lisp system
@c of these things, supplying a value and documentation. Third, they
@c provide information to utilities such as @code{etags} and
@c @code{make-docfile}, which create data bases of the functions and
@c variables in a program.
Emacs Lispでは、定義には3つの目的があります。
まず、コードを読む人向けに、特定のシンボルを(変数として)特定目的に
使う@emph{意図}があることを知らせます。
第2に、Lispシステムに対しては、値と説明文字列を提供して
これらのことを伝えます。
第3に、プログラム内の関数や変数のデータベースを作成する
@code{etags}や@code{make-docfile}などのユーティリティに情報を提供します。
@c The difference between @code{defconst} and @code{defvar} is primarily
@c a matter of intent, serving to inform human readers of whether the value
@c should ever change. Emacs Lisp does not restrict the ways in which a
@c variable can be used based on @code{defconst} or @code{defvar}
@c declarations. However, it does make a difference for initialization:
@c @code{defconst} unconditionally initializes the variable, while
@c @code{defvar} initializes it only if it is void.
@code{defconst}と@code{defvar}の違いは、主に好みの問題であり、
値が変更されるかどうかを人に伝えます。
Emacs Lispは、@code{defconst}や@code{defvar}の宣言に基づいて
変数の使い方を制限することはしません。
しかしながら、初期化に関しては違いがあります。
@code{defconst}は無条件に変数を初期化しますが、
@code{defvar}は変数が空である場合にのみ初期化します。
@ignore
One would expect user option variables to be defined with
@code{defconst}, since programs do not change them. Unfortunately, this
has bad results if the definition is in a library that is not preloaded:
@code{defconst} would override any prior value when the library is
loaded. Users would like to be able to set user options in their init
files, and override the default values given in the definitions. For
this reason, user options must be defined with @code{defvar}.
@end ignore
@defspec defvar symbol [value [doc-string]]
@c This special form defines @var{symbol} as a variable and can also
@c initialize and document it. The definition informs a person reading
@c your code that @var{symbol} is used as a variable that might be set or
@c changed. Note that @var{symbol} is not evaluated; the symbol to be
@c defined must appear explicitly in the @code{defvar}.
このスペシャルフォームは、@var{symbol}を変数として定義し、
初期値や説明文字列を設定する。
この定義は、コードを読む人向けに、
値を設定したり変更する変数として@var{symbol}を使うことを伝える。
@var{symbol}は評価されないことに注意。
定義するシンボルは、@code{defvar}に明示的に現れる必要がある。
@c If @var{symbol} is void and @var{value} is specified, @code{defvar}
@c evaluates it and sets @var{symbol} to the result. But if @var{symbol}
@c already has a value (i.e., it is not void), @var{value} is not even
@c evaluated, and @var{symbol}'s value remains unchanged. If @var{value}
@c is omitted, the value of @var{symbol} is not changed in any case.
@var{symbol}の値が空であり@var{value}を指定してあると、
@code{defvar}は@var{value}を評価し、その結果を@var{symbol}に設定する。
しかし、@var{symbol}にすでに値があれば(つまり、空でなければ)、
@var{value}をまったく評価せず、@var{symbol}の値も変更しない。
@var{value}を省略した場合、@var{symbol}の値をいっさい変更しない。
@c If @var{symbol} has a buffer-local binding in the current buffer,
@c @code{defvar} operates on the default value, which is buffer-independent,
@c not the current (buffer-local) binding. It sets the default value if
@c the default value is void. @xref{Buffer-Local Variables}.
@var{symbol}にカレントバッファでバッファローカルな束縛がある場合には、
@code{defvar}はデフォルト値に作用する。
それは、バッファには独立であり、現在の(バッファローカルな)束縛ではない。
@code{defvar}は、デフォルト値が空の場合にデフォルト値を設定する。
@pxref{Buffer-Local Variables}。
@c When you evaluate a top-level @code{defvar} form with @kbd{C-M-x} in
@c Emacs Lisp mode (@code{eval-defun}), a special feature of
@c @code{eval-defun} arranges to set the variable unconditionally, without
@c testing whether its value is void.
emacs-lispモードにおいて@kbd{C-M-x}(@code{eval-defun})でトップレベルの
フォーム@code{defvar}を評価すると、
@code{eval-defun}の特別な機能により、
変数の値が空かどうかを調べずに無条件に変数に設定する。
@c If the @var{doc-string} argument appears, it specifies the documentation
@c for the variable. (This opportunity to specify documentation is one of
@c the main benefits of defining the variable.) The documentation is
@c stored in the symbol's @code{variable-documentation} property. The
@c Emacs help functions (@pxref{Documentation}) look for this property.
@var{doc-string}があれば、それは変数の説明文を指定する。
(説明文を指定できるのは、変数定義の主な利点の1つである。)
説明文はシンボルの属性@code{variable-documentation}に格納する。
Emacsのヘルプ関数(@pxref{Documentation})は、この属性を調べる。
@c If the first character of @var{doc-string} is @samp{*}, it means that
@c this variable is considered a user option. This lets users set the
@c variable conveniently using the commands @code{set-variable} and
@c @code{edit-options}. However, it is better to use @code{defcustom}
@c instead of @code{defvar} for user option variables, so you can specify
@c customization information. @xref{Customization}.
@var{doc-string}の最初の文字が@samp{*}であると、
この変数をユーザーオプションと考えることを意味する。
これにより、ユーザーはコマンド@code{set-variable}や@code{edit-options}を
使って簡単に変数を設定できる。
しかしながら、ユーザーオプションの変数には、
@code{defvar}ではなく@code{defcustom}を使ったほうがよく、
そうすればカスタマイズ情報を指定できる。
@pxref{Customization}。
@c Here are some examples. This form defines @code{foo} but does not
@c initialize it:
いくつか例をあげる。
つぎのフォームは@code{foo}を定義するが初期化はしない。
@example
@group
(defvar foo)
@result{} foo
@end group
@end example
@c This example initializes the value of @code{bar} to @code{23}, and gives
@c it a documentation string:
つぎの例は、@code{bar}の値を@code{23}に初期化し、説明文字列を与える。
@example
@group
(defvar bar 23
"The normal weight of a bar.")
@result{} bar
@end group
@end example
@c The following form changes the documentation string for @code{bar},
@c making it a user option, but does not change the value, since @code{bar}
@c already has a value. (The addition @code{(1+ nil)} would get an error
@c if it were evaluated, but since it is not evaluated, there is no error.)
つぎの例は、@code{bar}の説明文字列を変更し、
この変数をユーザーオプションにする。
しかし、@code{bar}にはすでに値が設定してあるので、
その値は変更しない。
(さらに@code{(1+ nil)}は評価するとエラーになるが、
評価されないのでエラーはない。)
@example
@group
(defvar bar (1+ nil)
"*The normal weight of a bar.")
@result{} bar
@end group
@group
bar
@result{} 23
@end group
@end example
@c Here is an equivalent expression for the @code{defvar} special form:
つぎの例は、スペシャルフォーム@code{defvar}に等価な式である。
@example
@group
(defvar @var{symbol} @var{value} @var{doc-string})
@equiv{}
(progn
(if (not (boundp '@var{symbol}))
(setq @var{symbol} @var{value}))
(if '@var{doc-string}
(put '@var{symbol} 'variable-documentation '@var{doc-string}))
'@var{symbol})
@end group
@end example
@c The @code{defvar} form returns @var{symbol}, but it is normally used
@c at top level in a file where its value does not matter.
フォーム@code{defvar}は@var{symbol}を返すが、
通常このフォームはファイルのトップレベルで使われ、そこでは値は関係ない。
@end defspec
@defspec defconst symbol [value [doc-string]]
@c This special form defines @var{symbol} as a value and initializes it.
@c It informs a person reading your code that @var{symbol} has a standard
@c global value, established here, that should not be changed by the user
@c or by other programs. Note that @var{symbol} is not evaluated; the
@c symbol to be defined must appear explicitly in the @code{defconst}.
このスペシャルフォームは、@var{symbol}を変数として定義し初期化する。
この定義は、コードを読む人向けに、
@var{symbol}はこれ以降標準のグローバル値を持ち、
ユーザーや他のプログラムが変更すべきでないことを伝える。
@var{symbol}は評価されないことに注意。
定義するシンボルは、@code{defconst}に明示的に現れる必要がある。
@c @code{defconst} always evaluates @var{value}, and sets the value of
@c @var{symbol} to the result if @var{value} is given. If @var{symbol}
@c does have a buffer-local binding in the current buffer, @code{defconst}
@c sets the default value, not the buffer-local value. (But you should not
@c be making buffer-local bindings for a symbol that is defined with
@c @code{defconst}.)
@code{defconst}は、@var{value}があればつねに@var{value}を評価し、
その結果を@var{symbol}に設定する。
@var{symbol}にカレントバッファのバッファローカルな束縛がある場合には、
@code{defconst}はデフォルト値を設定し、
バッファローカルな値にではない。
(しかし、@code{defconst}で定義するシンボルには、
バッファローカルな束縛を作るべきではない。)
@c Here, @code{pi} is a constant that presumably ought not to be changed
@c by anyone (attempts by the Indiana State Legislature notwithstanding).
@c As the second form illustrates, however, this is only advisory.
つぎの例では、@code{pi}は、(インディアナ州立法府はいうにおよばず)
だれも変更すべきではないと考えられる定数である。
しかし、2番目のフォームからわかるように、これは単に助言でしかない。
@example
@group
(defconst pi 3.1415 "Pi to five places.")
@result{} pi
@end group
@group
(setq pi 3)
@result{} pi
@end group
@group
pi
@result{} 3
@end group
@end example
@end defspec
@defun user-variable-p variable
@c @cindex user option
@cindex ユーザーオプション
@c This function returns @code{t} if @var{variable} is a user option---a
@c variable intended to be set by the user for customization---and
@c @code{nil} otherwise. (Variables other than user options exist for the
@c internal purposes of Lisp programs, and users need not know about them.)
この関数は、@var{variable}がユーザーオプション、つまり、
カスタマイズのためにユーザーが設定することを意図した変数であると、
@code{t}を返し、さもなければ@code{nil}を返す。
(ユーザーオプション向け以外の変数は、Lispプログラムの内部目的用にあり、
それらについてユーザーが知る必要はない。)
@c User option variables are distinguished from other variables by the
@c first character of the @code{variable-documentation} property. If the
@c property exists and is a string, and its first character is @samp{*},
@c then the variable is a user option.
ユーザーオプション変数は、
属性@code{variable-documentation}の最初の文字で他の変数と区別される。
その属性が存在して文字列であり、最初の文字が@samp{*}であれば、
その変数はユーザーオプションである。
@end defun
@kindex variable-interactive
@c If a user option variable has a @code{variable-interactive} property,
@c the @code{set-variable} command uses that value to control reading the
@c new value for the variable. The property's value is used as if it were
@c to @code{interactive} (@pxref{Using Interactive}). However, this feature
@c is largely obsoleted by @code{defcustom} (@pxref{Customization}).
ユーザーオプション変数に属性@code{variable-interactive}があると、
コマンド@code{set-variable}はその属性値を使って、
変数の新しい値の読み取りを制御します。
この属性値は、@code{interactive}の引数(@pxref{Using Interactive})
のように使われます。
しかしながら、この機能は@code{defcustom}(@pxref{Customization})により
ほとんど廃れています。
@c @strong{Warning:} If the @code{defconst} and @code{defvar} special
@c forms are used while the variable has a local binding, they set the
@c local binding's value; the global binding is not changed. This is not
@c what we really want. To prevent it, use these special forms at top
@c level in a file, where normally no local binding is in effect, and make
@c sure to load the file before making a local binding for the variable.
@strong{警告:}@code{ }
変数にローカル束縛があるときに
スペシャルフォーム@code{defconst}や@code{defvar}を使うと、
ローカル束縛の値を変更し、グローバル束縛は変更しない。
これは望む効果ではない。
これを防ぐには、これらのスペシャルフォームはファイルのトップレベルで使う。
そうすれば、普通は有効なローカル束縛はない。
さらに、変数のローカル束縛を作るまえに、
確実にファイルをロードしておく。
@node Tips for Defining
@c @section Tips for Defining Variables Robustly
@section 変数を堅牢に定義するためのヒント
@c When defining and initializing a variable that holds a complicated
@c value (such as a keymap with bindings in it), it's best to put the
@c entire computation of the value into the @code{defvar}, like this:
(内部に束縛を含むようなキーマップなどの)複雑な値を保持する変数を
定義し初期化するときには、つぎのように、
値の計算全体を@code{defvar}の内部に入れておくのが最良です。
@example
(defvar my-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "\C-c\C-a" 'my-command)
@dots{}
map)
@var{docstring})
@end example
@noindent
@c This method has several benefits. First, if the user quits while
@c loading the file, the variable is either still uninitialized or
@c initialized properly, never in-between. If it is still uninitialized,
@c reloading the file will initialize it properly. Second, reloading the
@c file once the variable is initialized will not alter it; that is
@c important if the user has run hooks to alter part of the contents (such
@c as, to rebind keys). Third, evaluating the @code{defvar} form with
@c @kbd{C-M-x} @emph{will} reinitialize the map completely.
この方法には、いくつかの利点があります。
まず、ファイルのロード中にユーザーが中断した場合、
変数は初期化されないか正しく初期化されるかのいずれかであり、
その中間状態ということはありません。
第2に、変数をすでに初期化したあとにファイルをロードし直しても、
変数を変更しません。
(キーをバインドし直すなどの)内容の一部を変更するために
ユーザーがフックを実行した場合などには、これは重要です。
第3に、@kbd{C-M-x}でフォーム@code{defvar}を評価すると、
マップを完全に初期化し@emph{直せます}。
@c Putting so much code in the @code{defvar} form has one disadvantage:
@c it puts the documentation string far away from the line which names the
@c variable. Here's a safe way to avoid that:
フォーム@code{defvar}の内側に多くのコードを置くことには、欠点が1つあります。
変数の名前を指定した行から説明文字列が離れすぎてしまうことです。
つぎのようにしてこれを安全に防げます。
@example
(defvar my-mode-map nil
@var{docstring})
(if my-mode-map
nil
(let ((map (make-sparse-keymap)))
(define-key my-mode-map "\C-c\C-a" 'my-command)
@dots{}
(setq my-mode-map map)))
@end example
@noindent
@c This has all the same advantages as putting the initialization inside
@c the @code{defvar}, except that you must type @kbd{C-M-x} twice, once on
@c each form, if you do want to reinitialize the variable.
これには、@code{defvar}の内側に初期化を入れたときと同じ利点がありますが、
変数を再初期化するには、各フォームそれぞれについて
@kbd{C-M-x}を打つ必要があります。
@c But be careful not to write the code like this:
しかし、つぎのようなコードは書かないでください。
@example
(defvar my-mode-map nil
@var{docstring})
(if my-mode-map
nil
(setq my-mode-map (make-sparse-keymap))
(define-key my-mode-map "\C-c\C-a" 'my-command)
@dots{})
@end example
@noindent
@c This code sets the variable, then alters it, but it does so in more than
@c one step. If the user quits just after the @code{setq}, that leaves the
@c variable neither correctly initialized nor void nor @code{nil}. Once
@c that happens, reloading the file will not initialize the variable; it
@c will remain incomplete.
このコードでは、変数を設定してから変更しますが、
それを複数の手順で行います。
@code{setq}の直後にユーザーが中断すると、
変数は正しく初期化されておらず、空でも@code{nil}でもありません。
こうなったときにファイルを再ロードしても変数を初期化できません。
変数は不完全な状態のままです。
@node Accessing Variables
@c @section Accessing Variable Values
@section 変数値の参照
@c The usual way to reference a variable is to write the symbol which
@c names it (@pxref{Symbol Forms}). This requires you to specify the
@c variable name when you write the program. Usually that is exactly what
@c you want to do. Occasionally you need to choose at run time which
@c variable to reference; then you can use @code{symbol-value}.
変数を参照する普通の方法は、
変数を指名するシンボルを書くことです(@pxref{Symbol Forms})。
これには、プログラムを書くときに変数名を指定する必要があります。
読者は、普通このようにするでしょう。
場合によっては、実行時にどの変数を参照するか選ぶ必要があり、
そのときには@code{symbol-value}を使います。
@defun symbol-value symbol
@c This function returns the value of @var{symbol}. This is the value in
@c the innermost local binding of the symbol, or its global value if it
@c has no local bindings.
この関数は@var{symbol}の値を返す。
これは、シンボルのもっとも内側のローカル束縛の値、あるいは、
ローカル束縛がなければグローバル値である。
@example
@group
(setq abracadabra 5)
@result{} 5
@end group
@group
(setq foo 9)
@result{} 9
@end group
@group
@c ;; @r{Here the symbol @code{abracadabra}}
@c ;; @r{is the symbol whose value is examined.}
;; @r{ここで、@code{abracadabra}は、}