mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-23 00:57:59 +01:00
use the singleton construction idiom for all top level modules and convert all
identifiers to camel case
This commit is contained in:
@@ -26,14 +26,18 @@ the provisions above, a recipient may use your version of this file under
|
||||
the terms of any one of the MPL, the GPL or the LGPL.
|
||||
}}} ***** END LICENSE BLOCK *****/
|
||||
|
||||
vimperator.Completion = function () // {{{
|
||||
vimperator.Completion = function () //{{{
|
||||
{
|
||||
// The completion substrings, used for showing the longest common match
|
||||
var g_substrings = [];
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
////////////////////// PRIVATE SECTION /////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////{{{
|
||||
|
||||
// the completion substrings, used for showing the longest common match
|
||||
var substrings = [];
|
||||
|
||||
// function uses smartcase
|
||||
// list = [ [['com1', 'com2'], 'text'], [['com3', 'com4'], 'text'] ]
|
||||
function build_longest_common_substring(list, filter) //{{{
|
||||
function buildLongestCommonSubstring(list, filter)
|
||||
{
|
||||
var filtered = [];
|
||||
var ignorecase = false;
|
||||
@@ -51,19 +55,19 @@ vimperator.Completion = function () // {{{
|
||||
if (item.indexOf(filter) == -1)
|
||||
continue;
|
||||
|
||||
if (g_substrings.length == 0)
|
||||
if (substrings.length == 0)
|
||||
{
|
||||
var last_index = item.lastIndexOf(filter);
|
||||
var lastIndex = item.lastIndexOf(filter);
|
||||
var length = item.length;
|
||||
for (var k = item.indexOf(filter); k != -1 && k <= last_index; k = item.indexOf(filter, k + 1))
|
||||
for (var k = item.indexOf(filter); k != -1 && k <= lastIndex; k = item.indexOf(filter, k + 1))
|
||||
{
|
||||
for (var l = k + filter.length; l <= length; l++)
|
||||
g_substrings.push(list[i][0][j].substring(k, l));
|
||||
substrings.push(list[i][0][j].substring(k, l));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
g_substrings = g_substrings.filter(function ($_) {
|
||||
substrings = substrings.filter(function ($_) {
|
||||
return list[i][0][j].indexOf($_) >= 0;
|
||||
});
|
||||
}
|
||||
@@ -72,10 +76,10 @@ vimperator.Completion = function () // {{{
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
} //}}}
|
||||
}
|
||||
|
||||
/* this function is case sensitive and should be documented about input and output ;) */
|
||||
function build_longest_starting_substring(list, filter) //{{{
|
||||
// this function is case sensitive and should be documented about input and output ;)
|
||||
function buildLongestStartingSubstring(list, filter)
|
||||
{
|
||||
var filtered = [];
|
||||
for (var i = 0; i < list.length; i++)
|
||||
@@ -85,15 +89,15 @@ vimperator.Completion = function () // {{{
|
||||
if (list[i][0][j].indexOf(filter) != 0)
|
||||
continue;
|
||||
|
||||
if (g_substrings.length == 0)
|
||||
if (substrings.length == 0)
|
||||
{
|
||||
var length = list[i][0][j].length;
|
||||
for (var k = filter.length; k <= length; k++)
|
||||
g_substrings.push(list[i][0][j].substring(0, k));
|
||||
substrings.push(list[i][0][j].substring(0, k));
|
||||
}
|
||||
else
|
||||
{
|
||||
g_substrings = g_substrings.filter(function ($_) {
|
||||
substrings = substrings.filter(function ($_) {
|
||||
return list[i][0][j].indexOf($_) == 0;
|
||||
});
|
||||
}
|
||||
@@ -102,18 +106,18 @@ vimperator.Completion = function () // {{{
|
||||
}
|
||||
}
|
||||
return filtered;
|
||||
} //}}}
|
||||
}
|
||||
|
||||
/* discard all entries in the 'urls' array, which don't match 'filter */
|
||||
function filter_url_array(urls, filter) //{{{
|
||||
function filterUrlArray(urls, filter)
|
||||
{
|
||||
var filtered = [];
|
||||
// completions which don't match the url but just the description
|
||||
// list them add the end of the array
|
||||
var additional_completions = [];
|
||||
var additionalCompletions = [];
|
||||
|
||||
if (!filter) return urls.map(function ($_) {
|
||||
return [$_[0], $_[1]]
|
||||
return [$_[0], $_[1]];
|
||||
});
|
||||
|
||||
var ignorecase = false;
|
||||
@@ -122,7 +126,7 @@ vimperator.Completion = function () // {{{
|
||||
|
||||
/*
|
||||
* Longest Common Subsequence
|
||||
* This shouldn't use build_longest_common_substring
|
||||
* This shouldn't use buildLongestCommonSubstring
|
||||
* for performance reasons, so as not to cycle through the urls twice
|
||||
*/
|
||||
for (var i = 0; i < urls.length; i++)
|
||||
@@ -138,49 +142,54 @@ vimperator.Completion = function () // {{{
|
||||
if (url.indexOf(filter) == -1)
|
||||
{
|
||||
if (title.indexOf(filter) != -1)
|
||||
additional_completions.push([ urls[i][0], urls[i][1] ]);
|
||||
additionalCompletions.push([ urls[i][0], urls[i][1] ]);
|
||||
continue;
|
||||
}
|
||||
if (g_substrings.length == 0) // Build the substrings
|
||||
if (substrings.length == 0) // Build the substrings
|
||||
{
|
||||
var last_index = url.lastIndexOf(filter);
|
||||
var url_length = url.length;
|
||||
for (var k = url.indexOf(filter); k != -1 && k <= last_index; k = url.indexOf(filter, k + 1))
|
||||
var lastIndex = url.lastIndexOf(filter);
|
||||
var urlLength = url.length;
|
||||
for (var k = url.indexOf(filter); k != -1 && k <= lastIndex; k = url.indexOf(filter, k + 1))
|
||||
{
|
||||
for (var l = k + filter.length; l <= url_length; l++)
|
||||
g_substrings.push(url.substring(k, l));
|
||||
for (var l = k + filter.length; l <= urlLength; l++)
|
||||
substrings.push(url.substring(k, l));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
g_substrings = g_substrings.filter(function ($_) {
|
||||
substrings = substrings.filter(function ($_) {
|
||||
return url.indexOf($_) >= 0;
|
||||
});
|
||||
}
|
||||
filtered.push([urls[i][0], urls[i][1]]);
|
||||
}
|
||||
|
||||
return filtered.concat(additional_completions);
|
||||
} //}}}
|
||||
return filtered.concat(additionalCompletions);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////}}}
|
||||
////////////////////// PUBLIC SECTION //////////////////////////////////////////
|
||||
/////////////////////////////////////////////////////////////////////////////{{{
|
||||
|
||||
return {
|
||||
|
||||
/*
|
||||
* returns the longest common substring
|
||||
* used for the 'longest' setting for wildmode
|
||||
*/
|
||||
get_longest_substring: function () //{{{
|
||||
getLongestSubstring: function ()
|
||||
{
|
||||
if (g_substrings.length == 0)
|
||||
if (substrings.length == 0)
|
||||
return "";
|
||||
|
||||
var longest = g_substrings[0];
|
||||
for (var i = 1; i < g_substrings.length; i++)
|
||||
var longest = substrings[0];
|
||||
for (var i = 1; i < substrings.length; i++)
|
||||
{
|
||||
if (g_substrings[i].length > longest.length)
|
||||
longest = g_substrings[i];
|
||||
if (substrings[i].length > longest.length)
|
||||
longest = substrings[i];
|
||||
}
|
||||
return longest;
|
||||
}, //}}}
|
||||
},
|
||||
|
||||
/*
|
||||
* filter a list of urls
|
||||
@@ -189,29 +198,29 @@ vimperator.Completion = function () // {{{
|
||||
* depending on the 'complete' option
|
||||
* if the 'complete' argument is passed like "h", it temporarily overrides the complete option
|
||||
*/
|
||||
get_url_completions: function (filter, complete) //{{{
|
||||
url: function (filter, complete)
|
||||
{
|
||||
var completions = [];
|
||||
g_substrings = [];
|
||||
substrings = [];
|
||||
|
||||
var cpt = complete || vimperator.options["complete"];
|
||||
// join all completion arrays together
|
||||
for (var i = 0; i < cpt.length; i++)
|
||||
{
|
||||
if (cpt[i] == "s")
|
||||
completions = completions.concat(this.get_search_completions(filter));
|
||||
completions = completions.concat(this.search(filter));
|
||||
else if (cpt[i] == "b")
|
||||
completions = completions.concat(this.get_bookmark_completions(filter));
|
||||
completions = completions.concat(this.bookmark(filter));
|
||||
else if (cpt[i] == "h")
|
||||
completions = completions.concat(this.get_history_completions(filter));
|
||||
completions = completions.concat(this.history(filter));
|
||||
else if (cpt[i] == "f")
|
||||
completions = completions.concat(this.get_file_completions(filter, true));
|
||||
completions = completions.concat(this.file(filter, true));
|
||||
}
|
||||
|
||||
return completions;
|
||||
}, //}}}
|
||||
},
|
||||
|
||||
get_search_completions: function (filter) //{{{
|
||||
search: function (filter)
|
||||
{
|
||||
var engines = vimperator.bookmarks.getSearchEngines().concat(vimperator.bookmarks.getKeywords());
|
||||
|
||||
@@ -221,28 +230,28 @@ vimperator.Completion = function () // {{{
|
||||
var mapped = engines.map(function (engine) {
|
||||
return [[engine[0]], engine[1]];
|
||||
});
|
||||
return build_longest_common_substring(mapped, filter);
|
||||
}, //}}}
|
||||
return buildLongestCommonSubstring(mapped, filter);
|
||||
},
|
||||
|
||||
get_history_completions: function (filter) //{{{
|
||||
history: function (filter)
|
||||
{
|
||||
var items = vimperator.history.get();
|
||||
return filter_url_array(items, filter);
|
||||
}, //}}}
|
||||
return filterUrlArray(items, filter);
|
||||
},
|
||||
|
||||
get_bookmark_completions: function (filter) //{{{
|
||||
bookmark: function (filter)
|
||||
{
|
||||
var bookmarks = vimperator.bookmarks.get();
|
||||
return filter_url_array(bookmarks, filter);
|
||||
}, //}}}
|
||||
return filterUrlArray(bookmarks, filter);
|
||||
},
|
||||
|
||||
// TODO: support file:// and \ or / path separators on both platforms
|
||||
get_file_completions: function (filter)
|
||||
file: function (filter)
|
||||
{
|
||||
// this is now also used as part of the url completion, so the
|
||||
// substrings shouldn't be cleared for that case
|
||||
if (!arguments[1])
|
||||
g_substrings = [];
|
||||
substrings = [];
|
||||
|
||||
var matches = filter.match(/^(.*[\/\\])(.*?)$/);
|
||||
var dir;
|
||||
@@ -266,56 +275,56 @@ vimperator.Completion = function () // {{{
|
||||
}
|
||||
|
||||
|
||||
return build_longest_starting_substring(mapped, filter);
|
||||
return buildLongestStartingSubstring(mapped, filter);
|
||||
},
|
||||
|
||||
get_help_completions: function (filter) //{{{
|
||||
help: function (filter)
|
||||
{
|
||||
var help_array = [[["introduction"], "Introductory text"],
|
||||
[["initialization"], "Initialization and startup"],
|
||||
[["mappings"], "Normal mode commands"],
|
||||
[["commands"], "Ex commands"],
|
||||
[["options"], "Configuration options"]]; // TODO: hardcoded until we have proper 'pages'
|
||||
g_substrings = [];
|
||||
var helpArray = [[["introduction"], "Introductory text"],
|
||||
[["initialization"], "Initialization and startup"],
|
||||
[["mappings"], "Normal mode commands"],
|
||||
[["commands"], "Ex commands"],
|
||||
[["options"], "Configuration options"]]; // TODO: hardcoded until we have proper 'pages'
|
||||
substrings = [];
|
||||
for (var command in vimperator.commands)
|
||||
help_array.push([command.long_names.map(function ($_) { return ":" + $_; }), command.short_help]);
|
||||
options = this.get_options_completions(filter, true);
|
||||
help_array = help_array.concat(options.map(function ($_) {
|
||||
helpArray.push([command.longNames.map(function ($_) { return ":" + $_; }), command.shortHelp]);
|
||||
options = this.option(filter, true);
|
||||
helpArray = helpArray.concat(options.map(function ($_) {
|
||||
return [
|
||||
$_[0].map(function ($_) { return "'" + $_ + "'"; }),
|
||||
$_[1]
|
||||
];
|
||||
}));
|
||||
for (var map in vimperator.mappings)
|
||||
help_array.push([map.names, map.short_help]);
|
||||
helpArray.push([map.names, map.shortHelp]);
|
||||
|
||||
if (!filter) return help_array.map(function ($_) {
|
||||
if (!filter) return helpArray.map(function ($_) {
|
||||
return [$_[0][0], $_[1]]; // unfiltered, use the first command
|
||||
});
|
||||
|
||||
return build_longest_common_substring(help_array, filter);
|
||||
}, //}}}
|
||||
return buildLongestCommonSubstring(helpArray, filter);
|
||||
},
|
||||
|
||||
get_command_completions: function (filter) //{{{
|
||||
command: function (filter)
|
||||
{
|
||||
g_substrings = [];
|
||||
substrings = [];
|
||||
var completions = [];
|
||||
if (!filter)
|
||||
{
|
||||
for (var command in vimperator.commands)
|
||||
completions.push([command.name, command.short_help]);
|
||||
completions.push([command.name, command.shortHelp]);
|
||||
return completions;
|
||||
}
|
||||
|
||||
for (var command in vimperator.commands)
|
||||
completions.push([command.long_names, command.short_help]);
|
||||
return build_longest_starting_substring(completions, filter);
|
||||
}, //}}}
|
||||
completions.push([command.longNames, command.shortHelp]);
|
||||
return buildLongestStartingSubstring(completions, filter);
|
||||
},
|
||||
|
||||
get_options_completions: function (filter, unfiltered) //{{{
|
||||
option: function (filter, unfiltered)
|
||||
{
|
||||
g_substrings = [];
|
||||
var options_completions = [];
|
||||
substrings = [];
|
||||
var optionsCompletions = [];
|
||||
var prefix = filter.match(/^no|inv/) || "";
|
||||
|
||||
if (prefix)
|
||||
@@ -328,7 +337,7 @@ vimperator.Completion = function () // {{{
|
||||
{
|
||||
if (prefix && option.type != "boolean")
|
||||
continue;
|
||||
options.push([option.names, option.short_help]);
|
||||
options.push([option.names, option.shortHelp]);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
@@ -340,7 +349,7 @@ vimperator.Completion = function () // {{{
|
||||
{
|
||||
if (prefix && option.type != "boolean")
|
||||
continue;
|
||||
options.push([prefix + option.name, option.short_help]);
|
||||
options.push([prefix + option.name, option.shortHelp]);
|
||||
}
|
||||
return options;
|
||||
}
|
||||
@@ -352,15 +361,15 @@ vimperator.Completion = function () // {{{
|
||||
{
|
||||
if (option.hasName(filter))
|
||||
{
|
||||
options_completions.push([filter + "=" + option.value, ""]);
|
||||
return options_completions;
|
||||
optionsCompletions.push([filter + "=" + option.value, ""]);
|
||||
return optionsCompletions;
|
||||
}
|
||||
}
|
||||
return options_completions;
|
||||
return optionsCompletions;
|
||||
}
|
||||
|
||||
// can't use b_l_s_s, since this has special requirements (the prefix)
|
||||
var filter_length = filter.length;
|
||||
var filterLength = filter.length;
|
||||
for (var option in vimperator.options)
|
||||
{
|
||||
if (prefix && option.type != "boolean")
|
||||
@@ -371,29 +380,29 @@ vimperator.Completion = function () // {{{
|
||||
if (option.names[j].indexOf(filter) != 0)
|
||||
continue;
|
||||
|
||||
if (g_substrings.length == 0)
|
||||
if (substrings.length == 0)
|
||||
{
|
||||
var length = option.names[j].length;
|
||||
for (var k = filter_length; k <= length; k++)
|
||||
g_substrings.push(prefix + option.names[j].substring(0, k));
|
||||
for (var k = filterLength; k <= length; k++)
|
||||
substrings.push(prefix + option.names[j].substring(0, k));
|
||||
}
|
||||
else
|
||||
{
|
||||
g_substrings = g_substrings.filter(function ($_) {
|
||||
substrings = substrings.filter(function ($_) {
|
||||
return option.names[j].indexOf($_) == 0;
|
||||
});
|
||||
}
|
||||
options_completions.push([prefix + option.names[j], option.short_help]);
|
||||
optionsCompletions.push([prefix + option.names[j], option.shortHelp]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return options_completions;
|
||||
}, //}}}
|
||||
return optionsCompletions;
|
||||
},
|
||||
|
||||
get_buffer_completions: function (filter) //{{{
|
||||
buffer: function (filter)
|
||||
{
|
||||
g_substrings = [];
|
||||
substrings = [];
|
||||
var items = [];
|
||||
var num = getBrowser().browsers.length;
|
||||
var title, url;
|
||||
@@ -424,13 +433,13 @@ vimperator.Completion = function () // {{{
|
||||
if (!filter) return items.map(function ($_) {
|
||||
return [$_[0][0], $_[1]];
|
||||
});
|
||||
return build_longest_common_substring(items, filter);
|
||||
}, //}}}
|
||||
return buildLongestCommonSubstring(items, filter);
|
||||
},
|
||||
|
||||
get_sidebar_completions: function (filter) //{{{
|
||||
sidebar: function (filter)
|
||||
{
|
||||
g_substrings = [];
|
||||
var menu = document.getElementById("viewSidebarMenu")
|
||||
substrings = [];
|
||||
var menu = document.getElementById("viewSidebarMenu");
|
||||
var nodes = [];
|
||||
|
||||
for (var i = 0; i < menu.childNodes.length; i++)
|
||||
@@ -443,12 +452,12 @@ vimperator.Completion = function () // {{{
|
||||
return [[node[0]], node[1]];
|
||||
});
|
||||
|
||||
return build_longest_common_substring(mapped, filter);
|
||||
}, //}}}
|
||||
return buildLongestCommonSubstring(mapped, filter);
|
||||
},
|
||||
|
||||
javascript: function (str) // {{{
|
||||
{
|
||||
g_substrings = [];
|
||||
substrings = [];
|
||||
var matches = str.match(/^(.*?)(\s*\.\s*)?(\w*)$/);
|
||||
var object = "window";
|
||||
var filter = matches[3] || "";
|
||||
@@ -514,18 +523,18 @@ vimperator.Completion = function () // {{{
|
||||
completions = [];
|
||||
}
|
||||
|
||||
return build_longest_starting_substring(completions, filter);
|
||||
return buildLongestStartingSubstring(completions, filter);
|
||||
}, // }}}
|
||||
|
||||
// helper function which checks if the given arguments pass "filter"
|
||||
// items must be an array of strings
|
||||
// if case_sensitive == true, be sure to pass filter already in lowercased version
|
||||
match: function (filter, items, case_sensitive)
|
||||
// if caseSensitive == true, be sure to pass filter already in lowercased version
|
||||
match: function (filter, items, caseSensitive)
|
||||
{
|
||||
if (typeof(filter) != "string" || !items)
|
||||
if (typeof filter != "string" || !items)
|
||||
return false;
|
||||
|
||||
if (case_sensitive)
|
||||
if (caseSensitive)
|
||||
{
|
||||
for (var i = 0; i < items.length; i++)
|
||||
{
|
||||
@@ -544,7 +553,7 @@ vimperator.Completion = function () // {{{
|
||||
return false;
|
||||
},
|
||||
|
||||
exTabCompletion: function (str) //{{{
|
||||
exTabCompletion: function (str)
|
||||
{
|
||||
var [count, cmd, special, args] = vimperator.commands.parseCommand(str);
|
||||
var completions = [];
|
||||
@@ -555,7 +564,7 @@ vimperator.Completion = function () // {{{
|
||||
var matches = str.match(/^(:*\d*)\w*$/);
|
||||
if (matches)
|
||||
{
|
||||
completions = this.get_command_completions(cmd);
|
||||
completions = this.command(cmd);
|
||||
start = matches[1].length;
|
||||
}
|
||||
else // dynamically get completions as specified with the command's completer function
|
||||
@@ -587,9 +596,10 @@ vimperator.Completion = function () // {{{
|
||||
}
|
||||
}
|
||||
return [start, completions];
|
||||
} //}}}
|
||||
}
|
||||
|
||||
}
|
||||
} // }}}
|
||||
};
|
||||
//}}}
|
||||
}; //}}}
|
||||
|
||||
// vim: set fdm=marker sw=4 ts=4 et:
|
||||
|
||||
Reference in New Issue
Block a user