このサブセクションではlaxマッチに関する置換コマンドの振る舞いと、それをカスタマイズする方法を説明します。一般的には、ほとんどの置換は、それと同等な検索コマンドに比べて、デフォルトではより厳密なマッチを行ないます。
インクリメンタル検索とは異なり、置換コマンドはデフォルトではlax space matching(緩いスペースマッチング)を行いません(lax space matchingを参照してください)。置換でlax space
matchingを有効にするには、変数replace-lax-whitespace
を非nil
に変更してください(これはEmacsが置換文字列ではなく、置換するテキストを検索する方法だけに影響を与えます)。
query-replace-regexp
がパターンを検索するときlax whitespace
matchingを使うかどうかを制御するのは、それに対応する変数replace-regexp-lax-whitespace
です。
置換コマンドの最初の引数がすべて小文字の場合、置換のための検索において大文字小文字の違いを無視します。これはcase-fold-search
とsearch-upper-case
がいずれも非nil
の場合です。search-upper-case
(search-upper-caseを参照)がnil
なら、検索が大文字小文字を無視するかどうかは、コマンドの1つ目の引数が小文字かどうかと無関係にcase-fold-search
単独で決定されます。case-fold-search
をnil
にセットすると、すべての検索において常に大文字小文字の違いが有効になります。
さらに置換コマンドの2つ目の引数のすべて、または一部が小文字の場合には、置換コマンドは大文字小文字が出現するパターンを維持しようと試みます。したがって以下のコマンド、
M-x replace-string RET foo RET bar RET
replaces a lower case ‘foo’ with a lower case ‘bar’, an all-caps
‘FOO’ with ‘BAR’, and a capitalized ‘Foo’ with ‘Bar’.
(These three alternatives—lower case, all caps, and capitalized, are the
only ones that replace-string
can distinguish.) Note that Emacs
decides whether to up-case or capitalize the replacement text by analyzing
each word in the text being replaced, and will preserve the letter-case of
the replaced text only if all of its words use the same letter-case.
Thus, the command
M-x replace-string RET foo bar RET baz quux RET
replaces ‘Foo Bar’ with ‘Baz Quux’ because both words in ‘Foo Bar’ are capitalized. By contrast, the same command replaces ‘Foo bar’ with ‘baz quux’, i.e. it leaves the letter-case of the replacement text unchanged, since the two words in ‘Foo bar’ use different capitalization. What exactly is considered a “word” depends on the syntax tables that are in effect in the current buffer (see Syntax Tables in The Emacs Lisp Reference Manual); thus, ‘Foo_Bar’ is two words in Text mode, but could be a single word in some major mode that supports a programming language.
置換文字列に大文字が使われている場合、テキストが挿入されるときは、常に大文字のままとなります。大文字が最初の引数で使用されている場合、大文字小文字の変換なしで、2番目の引数に与えられたとおりに置換されます。同様にcase-replace
とcase-fold-search
の両方がnil
にセットされている場合、大文字小文字の変換なしで置換されます。
デフォルトでは置換コマンドは、置換するテキストを探すとき、character folding(character
foldingを参照してください)を使用しません。query-replace
とreplace-string
でのマッチングでcharacter
foldingを有効にするには、変数replace-char-fold
に非nil
値をセットします(このセッティングは、Emacsが置換するテキストを探す方法だけに影響し、置換するテキストには影響を与えません。また、replace-regexp
にも影響を与えません)。