Next: , Previous: , Up: 変数   [Contents][Index]


12.6 堅牢な変数定義のためのヒント

値が関数(または関数のリスト)であるような変数を定義するときには、変数の名前の最後に‘-function’(または‘-functions’)を使用します。

他にも変数名に関する慣習があります。以下はその完全なリストです:

…-hook

変数はノーマルフック(フックを参照)。

…-function

値は関数。

…-functions

値は関数のリスト。

…-form

値はフォーム(式)。

…-forms

値はフォーム(式)のリスト。

…-predicate

値は述語(predicate) — 1つの引数をとる関数 — であり成功なら非nil、失敗ならnilをリターンする。

…-flag

nilか否かだけが意味をもつような値。結局そのような変数は、やがては多くの値をもつことが多いので、この慣習を強く推奨はしない。

…-program

値はプログラム名。

…-command

値は完全なシェルコマンド。

…-switches

値はコマンドにたいして指定するオプション。

prefix--…

これは内部的な使用を意図した変数でありファイルprefix.el内で定義される(他の規約にしたがうかもしれない2018年以前に貢献されたEmacsコードは段階的に廃止される)。

…-internal

これは内部的な使用を意図した変数でありファイルCコード内で定義される(他の規約にしたがうかもしれない2018年以前に貢献されたEmacsコードは段階的に廃止される)。

変数を定義するときは、その変数を安全(safe)とマークすべきか、それとも危険(risky)とマークすべきかを常に考慮してください。ファイルローカル変数を参照してください。

When defining and initializing a variable that holds a complicated value (such as a syntax table for a major mode), it’s best to put the entire computation of the value into the defvar, like this:

(defvar my-major-mode-syntax-table
  (let ((table (make-syntax-table)))
    (modify-syntax-entry ?# "<" table)
    …
    table)
  docstring)

This method has several benefits. First, if the user quits while loading the file, the variable is either still uninitialized or initialized properly, never in-between. If it is still uninitialized, reloading the file will initialize it properly. Second, reloading the file once the variable is initialized will not alter it; that is important if the user has changed its value. Third, evaluating the defvar form with C-M-x will reinitialize the variable completely.

This page has generated for branch:master, commit:762705fb24fd90db318f2e51c1e762452d26f7e2 to check Japanese translation.