Skip to content

Commit 9b641f8

Browse files
committed
Merge pull request #109 from klaplong/master
Added fuzzy matcher to hints
2 parents 0daf18c + a8735b3 commit 9b641f8

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

common/content/hints.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,38 @@ const Hints = Module("hints", {
833833
};
834834
} //}}}
835835

836+
/**
837+
* Get a hint matcher for hintmatching=fuzzy
838+
*
839+
* The user input can be anything. The matcher looks for occurances of
840+
* the characters in the user input. If all occurances are found in the
841+
* link, in order, the link is relevant.
842+
*
843+
* @param {string} hintString The string typed by the user.
844+
* @returns {function(string):boolean} A function that takes the text
845+
* of a hint and returns true if all characters of the input are
846+
* found in the link.
847+
*/
848+
function fuzzyMatcher(hintString) {
849+
expression = '';
850+
851+
// Build a regex for fuzzy matching with the input.
852+
//
853+
// If the input is 'abc', the regex will be '[^a]*a[^b]*b[^c]c'.
854+
for (var i = 0; i < hintString.length; i ++) {
855+
var char = hintString[i];
856+
expression += '[^' + char + ']*' + char;
857+
}
858+
859+
var re = new RegExp(expression, 'i');
860+
861+
return function(linkText) {
862+
var found = linkText.search(re) != -1;
863+
864+
return found;
865+
};
866+
}
867+
836868
let indexOf = String.indexOf;
837869
if (options.get("hintmatching").has("transliterated"))
838870
indexOf = Hints.indexOf;
@@ -841,6 +873,7 @@ const Hints = Module("hints", {
841873
case "contains" : return containsMatcher(hintString);
842874
case "wordstartswith": return wordStartsWithMatcher(hintString, /*allowWordOverleaping=*/ true);
843875
case "firstletters" : return wordStartsWithMatcher(hintString, /*allowWordOverleaping=*/ false);
876+
case "fuzzy" : return fuzzyMatcher(hintString);
844877
case "custom" : return liberator.plugins.customHintMatcher(hintString);
845878
default : liberator.echoerr("Invalid hintmatching type: " + hintMatching);
846879
}
@@ -1268,6 +1301,7 @@ const Hints = Module("hints", {
12681301
["contains", "The typed characters are split on whitespace. The resulting groups must all appear in the hint."],
12691302
["wordstartswith", "The typed characters are split on whitespace. The resulting groups must all match the beginings of words, in order."],
12701303
["firstletters", "Behaves like wordstartswith, but all groups much match a sequence of words."],
1304+
["fuzzy", "Hints are matched according to the fuzzy search algorithm."],
12711305
["custom", "Delegate to a custom function: liberator.plugins.customHintMatcher(hintString)"],
12721306
["transliterated", "When true, special latin characters are translated to their ascii equivalent (e.g., \u00e9 -> e)"]
12731307
]

common/locale/en-US/options.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@
596596
<dt>contains</dt> <dd>The typed characters are split on whitespace, and these character groups have to match anywhere inside the text of the link.</dd>
597597
<dt>wordstartswith</dt> <dd>The typed characters are matched with the beginning of the first word (see <o>wordseparators</o>) in the link as long as possible. If no matches occur in the current word, then the matching is continued at the beginning of the next word. The words are worked through in the order they appear in the link. If the typed characters contain spaces, then the characters are split on whitespace. These character groups are then matched with the beginning of the words, beginning at the first one and continuing with the following words in the order they appear in the link.</dd>
598598
<dt>firstletters</dt> <dd>Behaves like wordstartswith, but non-matching words aren't overleaped.</dd>
599+
<dt>fuzzy</dt> <dd>Hints are matched according to the fuzzy search algorithm.</dd>
599600
<dt>custom</dt> <dd>Delegate to a custom function: liberator.plugins.customHintMatcher(hintString)</dd>
600601
</dl>
601602
</description>

vimperator/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
201x-xx-xx
2+
* Add support for :set hintmatching=fuzzy as a new, alternative way for hintmatching.
3+
14
2014-11-09
25
* Version 3.8.3
36
* Many compatibility fixes with new Firefox versions (up to Firefox 35)

0 commit comments

Comments
 (0)