1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 14:22:27 +01:00

Add some standard JS function completers for the liberator API

This commit is contained in:
Kris Maglione
2008-11-12 23:55:34 +00:00
parent 5221fb41be
commit d7076c5aa5
6 changed files with 66 additions and 35 deletions

View File

@@ -227,6 +227,11 @@ function Commands() //{{{
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
liberator.registerObserver("load_completion", function ()
{
completion.setFunctionCompleter(commands.get, [function () ([c.name, c.description] for (c in commands))]);
});
var commandManager = { var commandManager = {
// FIXME: remove later, when our option handler is better // FIXME: remove later, when our option handler is better

View File

@@ -27,9 +27,7 @@ the terms of any one of the MPL, the GPL or the LGPL.
}}} ***** END LICENSE BLOCK *****/ }}} ***** END LICENSE BLOCK *****/
// An eval with a cleaner lexical scope. // An eval with a cleaner lexical scope.
// TODO: that shows up in ":echo modules", can we move it "inside" the Completion class? --mst modules._cleanEval = function (__liberator_eval_arg, __liberator_eval_tmp)
const EVAL_TMP = "__liberator_eval_tmp";
function __eval(__liberator_eval_arg, __liberator_eval_tmp)
{ {
return window.eval(__liberator_eval_arg); return window.eval(__liberator_eval_arg);
} }
@@ -47,6 +45,10 @@ function Completion() //{{{
} }
catch (e) {} catch (e) {}
const EVAL_TMP = "__liberator_eval_tmp";
const cleanEval = _cleanEval;
delete modules._cleanEval;
// the completion substrings, used for showing the longest common match // the completion substrings, used for showing the longest common match
var cacheFilter = {} var cacheFilter = {}
var cacheResults = {} var cacheResults = {}
@@ -205,7 +207,7 @@ function Completion() //{{{
try try
{ {
return cache[key] = __eval(arg, tmp); return cache[key] = cleanEval(arg, tmp);
} }
catch (e) catch (e)
{ {
@@ -361,7 +363,7 @@ function Completion() //{{{
} }
catch (e) catch (e)
{ {
liberator.dump(util.escapeString(string) + ": " + e + "\n" + e.stack); // liberator.dump(util.escapeString(string) + ": " + e + "\n" + e.stack);
lastIdx = 0; lastIdx = 0;
return [0, []]; return [0, []];
} }
@@ -480,14 +482,13 @@ function Completion() //{{{
let [offset, obj, func] = getObjKey(-3); let [offset, obj, func] = getObjKey(-3);
let key = str.substring(get(-2, 0, STATEMENTS), top[OFFSET]) + "''"; let key = str.substring(get(-2, 0, STATEMENTS), top[OFFSET]) + "''";
let completer = this.completers[func];
try try
{ {
if (!completer) var completer = obj[func].liberatorCompleter;
completer = obj[func].liberatorCompleter;
} }
catch (e) {} catch (e) {}
liberator.dump({ call: completer, func: func, obj: obj, string: string, args: "args" }); if (!completer)
completer = this.completers[func];
if (!completer) if (!completer)
return [0, []]; return [0, []];
@@ -502,6 +503,8 @@ function Completion() //{{{
args.push(key); args.push(key);
let compl = completer.call(this, func, obj, string, args); let compl = completer.call(this, func, obj, string, args);
if (!(compl instanceof Array))
compl = [v for (v in compl)];
key = this.eval(key); key = this.eval(key);
return [top[OFFSET], this.filter(compl, key + string, last, key.length)]; return [top[OFFSET], this.filter(compl, key + string, last, key.length)];
} }
@@ -636,6 +639,16 @@ function Completion() //{{{
return { return {
setFunctionCompleter: function (func, completers)
{
func.liberatorCompleter = function (func, obj, string, args) {
let completer = completers[args.length - 1];
if (!completer)
return [];
return completer.call(this, this.eval(obj), this.eval(args.pop()) + string, args);
};
},
// returns the longest common substring // returns the longest common substring
// used for the 'longest' setting for wildmode // used for the 'longest' setting for wildmode
getLongestSubstring: function getLongestSubstring() getLongestSubstring: function getLongestSubstring()
@@ -982,17 +995,10 @@ function Completion() //{{{
return mapped; return mapped;
}; };
try if (tail)
{ return [dir.length, this.cached("file-" + dir, compl, generate, "filter", [true])];
if (tail) else
return [dir.length, this.cached("file-" + dir, compl, generate, "filter", [true])]; return [0, this.cached("file-" + dir, filter, generate, "filter", [true])];
else
return [0, this.cached("file-" + dir, filter, generate, "filter", [true])];
}
catch (e)
{
liberator.dump(e);
}
}, },
help: function help(filter) help: function help(filter)
@@ -1028,15 +1034,7 @@ function Completion() //{{{
javascript: function _javascript(str) javascript: function _javascript(str)
{ {
try return javascript.complete(str);
{
return javascript.complete(str);
}
catch (e)
{
liberator.dump(e);
return [0, []];
}
}, },
macro: function macro(filter) macro: function macro(filter)

View File

@@ -171,6 +171,12 @@ function AutoCommands() //{{{
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
liberator.registerObserver("load_completion", function ()
{
completion.setFunctionCompleter(autocommands.get, [function () config.autocommands]);
});
return { return {
__iterator__: function () util.Array.iterator(store), __iterator__: function () util.Array.iterator(store),

View File

@@ -258,6 +258,23 @@ function Mappings() //{{{
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
liberator.registerObserver("load_completion", function ()
{
completion.setFunctionCompleter(mappings.get,
[
null,
function (obj, filter, args)
{
let mode = this.eval(args[0]);
return util.Array.flatten(
[
[[name, map.description] for ([i, name] in Iterator(map.names))]
for ([i, map] in Iterator(user[mode].concat(main[mode])))
])
}
]);
});
// FIXME: // FIXME:
Mappings.flags = { Mappings.flags = {
ALLOW_EVENT_ROUTING: 1 << 0, // if set, return true inside the map command to pass the event further to firefox ALLOW_EVENT_ROUTING: 1 << 0, // if set, return true inside the map command to pass the event further to firefox

View File

@@ -49,10 +49,6 @@ const modes = (function () //{{{
else if (passAllKeys && !passNextKey) else if (passAllKeys && !passNextKey)
return "-- PASS THROUGH --"; return "-- PASS THROUGH --";
var ext = "";
if (extended & modes.MENU) // TODO: desirable?
ext += " (menu)";
// when recording a macro // when recording a macro
var macromode = ""; var macromode = "";
if (modes.isRecording) if (modes.isRecording)
@@ -60,6 +56,9 @@ const modes = (function () //{{{
else if (modes.isReplaying) else if (modes.isReplaying)
macromode = "replaying"; macromode = "replaying";
var ext = "";
if (extended & modes.MENU) // TODO: desirable?
ext += " (menu)";
ext += " --" + macromode; ext += " --" + macromode;
switch (main) switch (main)
@@ -67,9 +66,9 @@ const modes = (function () //{{{
case modes.INSERT: case modes.INSERT:
return "-- INSERT" + ext; return "-- INSERT" + ext;
case modes.VISUAL: case modes.VISUAL:
return (extended & modes.LINE) ? "-- VISUAL LINE" + ext : "-- VISUAL" + ext; return "-- VISUAL" + (extended & modes.LINE ? " LINE" : "") + ext;
case modes.COMMAND_LINE: case modes.COMMAND_LINE:
// under modes.COMMAND_LINE, this block will never be reached // under modes.COMMAND_LINE, this function will never be called
return macromode; return macromode;
case modes.CARET: case modes.CARET:
return "-- CARET" + ext; return "-- CARET" + ext;

View File

@@ -847,6 +847,12 @@ function Options() //{{{
////////////////////// PUBLIC SECTION ////////////////////////////////////////// ////////////////////// PUBLIC SECTION //////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////{{{ /////////////////////////////////////////////////////////////////////////////{{{
// TODO: Does this belong elsewhere?
liberator.registerObserver("load_completion", function ()
{
completion.setFunctionCompleter(options.get, [function () ([o.name, o.description] for (o in options))]);
});
return { return {
OPTION_SCOPE_GLOBAL: 1, OPTION_SCOPE_GLOBAL: 1,