Next: , Previous: , Up: 置換コマンド   [Contents][Index]


17.10.3 置換コマンドとLaxマッチ

このサブセクションでは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-searchsearch-upper-caseがいずれも非nilの場合です。search-upper-case (search-upper-caseを参照)がnilなら、検索が大文字小文字を無視するかどうかは、コマンドの1つ目の引数が小文字かどうかと無関係にcase-fold-search単独で決定されます。case-fold-searchnilにセットすると、すべての検索において常に大文字小文字の違いが有効になります。

さらに置換コマンドの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-replacecase-fold-searchの両方がnilにセットされている場合、大文字小文字の変換なしで置換されます。

デフォルトでは置換コマンドは、置換するテキストを探すとき、character folding(character foldingを参照してください)を使用しません。query-replacereplace-stringでのマッチングでcharacter foldingを有効にするには、変数replace-char-foldに非nil値をセットします(このセッティングは、Emacsが置換するテキストを探す方法だけに影響し、置換するテキストには影響を与えません。また、replace-regexpにも影響を与えません)。

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