diff --git a/doc/pattern.jax b/doc/pattern.jax index e7c11641d..55e42c72a 100644 --- a/doc/pattern.jax +++ b/doc/pattern.jax @@ -1,4 +1,4 @@ -*pattern.txt* For Vim バージョン 9.1. Last change: 2025 Aug 12 +*pattern.txt* For Vim バージョン 9.1. Last change: 2025 Aug 21 VIMリファレンスマニュアル by Bram Moolenaar @@ -1477,39 +1477,52 @@ Vim では、'^' と '$' は常に中間の改行にもマッチします。最 ============================================================================== 11. ファジーマッチ *fuzzy-matching* -ファジーマッチとは、不正確な検索文字列を使って文字列を検索することを言います。 -ファジーマッチで、ある文字列がマッチするのは、検索文字列の全ての文字がその文字 -列のどこかに同じ順序で存在する場合です。大文字小文字は無視されます。マッチした -文字列では、検索文字列内で隣り合う2文字の間に他の文字が存在するかもしれません。 -検索文字列が複数の単語を持つ場合、各単語は別々にマッチします。そのため検索文字 -列内の複数の単語は{訳注: マッチした}文字列内でどのような順序でも存在し得ます。 +ファジーマッチは、パターン文字が必ずしも連続して出現するとは限らない場合に、文 +字列がパターンにどの程度マッチするかをスコア付けする。 -Vim は fzy プロジェクトと同じ改良されたアルゴリズムを使用します: +例: > + パターン: "vim" + 候補: "vim" -> 完璧 + "vimeo" -> 良い (v i m) + "voice mail" -> 弱い (v _ i _ _ _ m) + "vintage" -> マッチしない (no "m") +< +検索文字列に複数の単語が含まれている場合、各単語は個別にマッチし、候補リスト内 +で任意の順序で出現する可能性がある。例えば、"get pat" は "GetPattern"、 +"PatternGet"、"getPattern"、"patGetter"、"getSomePattern"、"MatchpatternGet" +などとマッチする。 + +'ignorecase' および 'smartcase' オプションは適用されず、パターンがすべて小文字 +の場合、大文字/小文字は無視される。 + +Vim の実装は、fzy プロジェクトのアルゴリズムに基づいている: https://github.com/jhawthorn/fzy -ファジーマッチではマッチした文字列ごとに以下の基準に基づいてスコアが割り当てら -れます: - - 連続してマッチした文字の個数。 - - 検索文字列内で隣り合う2文字の間の文字の個数 (距離)。 - - 単語の先頭でのマッチ - - キャメルケース文字でのマッチ (例えば CamelCase の Case) - - パスの区切り文字またはハイフンの後ろでのマッチ - - {訳注: マッチした}文字列内のマッチしなかった文字の個数。 - - 完全/正確なマッチが推奨される。 -マッチした文字列でスコアが最も高かったものが最初に返されます。 - -例えば "get pat" という文字列をファジーマッチで検索すると、"GetPattern"、 -"PatternGet"、"getPattern"、"patGetter"、"getSomePattern"、"MatchpatternGet" -などの文字列がマッチします。 +与えられたパターンと候補に対して、動的計画法を用いて最適なスコアを計算する。 + +アルゴリズムは 2 段階で動作する: + +1. 前方パス + 候補を左から右にスキャンし、各パターンの位置における最高スコアを追跡する。 + マッチは、候補の先頭、単語の先頭 (スペース、アンダースコア、ダッシュ、キャ + メルケース)、または直前のマッチの直後に出現すると、より高いスコアになる。 + +2. 後方パス + 最高得点の終了位置から開始し、一歩下がってマッチする位置を見つけ、配置が最 + 適であることを確認する。 + +Vim はオリジナルのアルゴリズムを拡張し、マルチバイトコードポイントをサポートす +ることで、UTF-8 やその他のエンコーディングでの正確なマッチングを可能にした。 + +計算時間は O (パターン * 候補) である。メモリ使用量もこれに比例する。 -関数 |matchfuzzy()| や |matchfuzzypos()| で文字列のリストに含まれる文字列をファ -ジー検索できます。関数 matchfuzzy() はマッチした文字列のリストを返します。関数 -matchfuzzypos() はマッチのリスト、マッチの位置およびファジーマッチのスコアを返 -します。 +|matchfuzzy()| 関数と |matchfuzzypos()| 関数は、文字列のリスト内でファジー検索 +を実行する。|matchfuzzy()| はマッチする文字列を返し、|matchfuzzypos()| はマッ +チする文字列をその位置とスコアとともに返す。 -`:vimgrep` の フラグ "f" はファジーマッチを有効にします。 +`:vimgrep` の フラグ "f" はファジーマッチを有効にする。 |ins-completion| のファジーマッチを有効にするには、'completeopt' オプションに -"fuzzy" 値を追加します。 +"fuzzy" 値を追加する。 vim:tw=78:ts=8:noet:ft=help:norl: diff --git a/en/pattern.txt b/en/pattern.txt index 02668a047..51280317a 100644 --- a/en/pattern.txt +++ b/en/pattern.txt @@ -1,4 +1,4 @@ -*pattern.txt* For Vim version 9.1. Last change: 2025 Aug 12 +*pattern.txt* For Vim version 9.1. Last change: 2025 Aug 21 VIM REFERENCE MANUAL by Bram Moolenaar @@ -1501,37 +1501,51 @@ Finally, these constructs are unique to Perl: ============================================================================== 11. Fuzzy matching *fuzzy-matching* -Fuzzy matching refers to matching strings using a non-exact search string. -Fuzzy matching will match a string, if all the characters in the search string -are present anywhere in the string in the same order. Case is ignored. In a -matched string, other characters can be present between two consecutive -characters in the search string. If the search string has multiple words, then -each word is matched separately. So the words in the search string can be -present in any order in a string. +Fuzzy matching scores how well a string matches a pattern when the pattern +characters appear in order but not necessarily contiguously. -Vim uses the same improved algorithm as the fzy project: +Example: > + Pattern: "vim" + Candidates: "vim" -> perfect + "vimeo" -> good (v i m) + "voice mail" -> weaker (v _ i _ _ _ m) + "vintage" -> no match (no "m") +< +If the search string has multiple words, each word is matched separately and +may appear in any order in the candidate. For example "get pat" matches +"GetPattern", "PatternGet", "getPattern", "patGetter", "getSomePattern", +"MatchpatternGet", etc. + +The 'ignorecase' and 'smartcase' options do not apply, case is ignored if the +pattern is all lower case. + +Vim's implementation is based on the algorithm from the fzy project: https://github.com/jhawthorn/fzy -Fuzzy matching assigns a score for each matched string based on the following -criteria: - - The number of sequentially matching characters. - - The number of characters (distance) between two consecutive matching - characters. - - Matches at the beginning of a word - - Matches at a camel case character (e.g. Case in CamelCase) - - Matches after a path separator or a hyphen. - - The number of unmatched characters in a string. - - A full/exact match is preferred. -The matching string with the highest score is returned first. - -For example, when you search for the "get pat" string using fuzzy matching, it -will match the strings "GetPattern", "PatternGet", "getPattern", "patGetter", -"getSomePattern", "MatchpatternGet" etc. - -The functions |matchfuzzy()| and |matchfuzzypos()| can be used to fuzzy search -a string in a List of strings. The matchfuzzy() function returns a List of -matching strings. The matchfuzzypos() functions returns the List of matches, -the matching positions and the fuzzy match scores. +It uses dynamic programming to compute an optimal score for a given pattern +and candidate. + +The algorithm works in two stages: + +1. Forward pass + Scan the candidate left to right, tracking the best score for each + pattern position. Matches score higher when they occur at the start + of the candidate, the start of a word (space, underscore, dash, + camelCase), or directly after the previous match. + +2. Backward pass + Start from the best-scoring end position and step back to find match + positions, ensuring the alignment is optimal. + +Vim extends the original algorithm to support multibyte codepoints, allowing +correct matching for UTF-8 and other encodings. + +Time complexity is O(pattern * candidate). Memory usage is proportional +to the same. + +The |matchfuzzy()| and |matchfuzzypos()| functions perform fuzzy searching in +a List of strings. |matchfuzzy()| returns the matching strings, while +|matchfuzzypos()| returns the matches along with their positions and scores. The "f" flag of `:vimgrep` enables fuzzy matching.