GNU Emacs 32.0.50 (9008bf64c3) のマニュアルを PLaMo 翻訳で機械翻訳したものです。誤訳を含むことがあります。原文: https://www.gnu.org/software/emacs/manual/html_node/elisp/Variable-Aliases.html ・ emacs-doc.conao3.com

12.15 変数エイリアス

2つの変数を同義語として定義しておくと、両方の変数が常に同じ値を持ち、一方を変更するともう一方も変更されるため、有用な場合があります。変数名を変更する際――旧名が適切ではなかったと気づいた場合や、意味が一部変わった場合など――、互換性を保つために旧名を新名のエイリアスとして残しておくと便利です。これはdefvaraliasで実現できます。

関数: defvaralias new-alias base-variable &optional docstring

この関数は、シンボル new-alias をシンボル base-variable の変数エイリアスとして定義します。つまり、new-alias の値を取得すると base-variable の値が返され、new-alias の値を変更すると base-variable の値が変更されます。エイリアスされた2つの変数名は、常に同じ値と同一のバインディングを共有します。

If the docstring argument is non-nil, it specifies the documentation for new-alias; otherwise, the alias gets the same documentation as base-variable has, if any, unless base-variable is itself an alias, in which case new-alias gets the documentation of the variable at the end of the chain of aliases.

この関数は base-variable を返します。

結果として得られる変数定義の連鎖が循環する場合、Emacsはcyclic-variable-indirectionエラーを通知します。

変数エイリアスは、変数の古い名前を新しい名前に置き換える際に便利です。make-obsolete-variable は、古い名前が廃止されたこと、したがって将来的にいずれかの段階で削除される可能性があることを宣言します。

関数: make-obsolete-variable obsolete-name current-name when &optional access-type

この関数は、変数 obsolete-name が廃止されたことをバイトコンパイラに警告させます。current-name がシンボルである場合、それは変数の新しい名称となり、警告メッセージには obsolete-name の代わりに current-name を使用するよう記載されます。current-name が文字列である場合、(substitute-command-keys を経由した後の)メッセージはこの内容となり、置換用の変数は存在しません。when は、変数が最初に廃止された時点を示す文字列(通常はバージョン番号の文字列)である必要があります。

オプション引数 access-type は、nil 以外の場合、廃止警告をトリガーするアクセスの種類を指定する必要があり、get または set のいずれかとなります。

マクロ define-obsolete-variable-alias を使用すると、2つの変数を同義語にし、同時に一方を廃止済みとして宣言できます。

マクロ: define-obsolete-variable-alias obsolete-name current-name when &optional docstring

このマクロは、変数 obsolete-name を廃止済みとしてマークし、同時に変数 current-name のエイリアスとしても機能します。これは以下と同等です。

(defvaralias obsolete-name current-name docstring)
(make-obsolete-variable obsolete-name current-name when)

このマクロはすべてのパラメータを評価します。また、obsolete-name と current-name はいずれもシンボルである必要があるため、典型的な使い方は以下のようになります。

(define-obsolete-variable-alias 'foo-thing 'bar-thing "27.1")
関数: indirect-variable variable

この関数は、variable のエイリアスチェーンの末尾にある変数を返します。variable がシンボルでない場合、または variable がエイリアスとして定義されていない場合は、variable を返します。

(defvaralias 'foo 'bar)
(indirect-variable 'foo)
     ⇒ bar
(indirect-variable 'bar)
     ⇒ bar
(setq bar 2)
bar
     ⇒ 2
foo
     ⇒ 2
(setq foo 0)
bar
     ⇒ 0
foo
     ⇒ 0