1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-22 19:07:58 +01:00

new :set and :prefs commands, tweaked :set output to use colors

This commit is contained in:
Doug Kearns
2007-10-10 02:54:44 +00:00
parent 0e576502d9
commit 21703d5b57
4 changed files with 220 additions and 163 deletions

View File

@@ -1042,11 +1042,35 @@ function Commands() //{{{
} }
)); ));
addDefaultCommand(new Command(["pref[erences]", "prefs"], addDefaultCommand(new Command(["pref[erences]", "prefs"],
openPreferences, function(args, special, count, modifiers)
{ {
if (!args)
{
// TODO: copy these snippets to more function which should work with :tab xxx
if (modifiers && modifiers.inTab)
{
vimperator.open(special ? "about:config" :
"chrome://browser/content/preferences/preferences.xul", vimperator.NEW_TAB);
}
else
{
if (special) // open firefox settings gui dialog
vimperator.open("about:config", vimperator.CURRENT_TAB);
else
openPreferences();
}
}
else
{
vimperator.echoerr("E488: Trailing characters");
}
},
{
usage: [":pref[erences][!]"],
short_help: "Show Browser Preferences", short_help: "Show Browser Preferences",
help: "You can change the browser preferences from this dialog.<br/>Be aware that not all Firefox preferences work, because Vimperator overrides some keybindings and changes Firefox's GUI.<br/>" + help: "You can change the browser preferences from this dialog. " +
"Works like <code class=\"command\">:set!</code>, but opens the dialog in a new window instead of a new tab. Use this, if you experience problems/crashes when using <code class=\"command\">:set!</code>" "Be aware that not all Firefox preferences work, because Vimperator overrides some keybindings and changes Firefox's GUI.<br/>" +
"<code class=\"command\">:prefs!</code> opens about:config in the current tab where you can change advanced Firefox preferences."
} }
)); ));
addDefaultCommand(new Command(["qma[rk]"], addDefaultCommand(new Command(["qma[rk]"],
@@ -1154,158 +1178,156 @@ function Commands() //{{{
addDefaultCommand(new Command(["se[t]"], addDefaultCommand(new Command(["se[t]"],
function(args, special, count, modifiers) function(args, special, count, modifiers)
{ {
if (special)
{
vimperator.echo("This WILL show all non-default about:config options");
return;
}
var only_non_default = false; //used for :set to print non-default options
if (!args) if (!args)
{ {
// TODO: copy these snippets to more function which should work with :tab xxx args = "all?"
var where = vimperator.CURRENT_TAB; only_non_default = true;
if (arguments[3] && arguments[3].inTab)
where = vimperator.NEW_TAB;
if (special) // open firefox settings gui dialog
vimperator.open("chrome://browser/content/preferences/preferences.xul", where);
else
vimperator.open("about:config", where);
} }
// 1 2 3 4 5 6
var matches = args.match(/^\s*(no|inv)?([a-z]+)([?&!])?(([+-])?=(.*))?/);
if (!matches)
{
vimperator.echoerr("E518: Unknown option: " + args);
return;
}
var no = false;
if (matches[1] == "no")
no = true;
var opt = matches[2];
var all = false;
if (opt == "all")
all = true;
var option = vimperator.options.get(opt);
if (!option && !all)
{
vimperator.echoerr("E518: Unknown option: " + opt);
return;
}
var get = false;
if (all || matches[3] == "?" || (option.type != "boolean" && matches[4] === undefined))
get = true;
var reset = false;
if (matches[3] == "&")
reset = true;
var invert = false;
if (matches[1] == "inv" || matches[3] == "!")
invert = true;
var oper = matches[5];
var val = matches[6];
if (val === undefined)
val = "";
// reset a variable to its default value
// TODO: remove the value from about:config instead of setting it to the current default value
if (reset)
{
if (all)
{
for (let opt in vimperator.options)
opt.value = opt.default_value;
}
else
{
option.value = option.default_value;
}
}
// read access
else if (get)
{
if (all)
{
vimperator.options.list(only_non_default);
}
else
{
if (option.type == "boolean")
vimperator.echo((option.value ? " " : "no") + option.name);
else
vimperator.echo(" " + option.name + "=" + option.value);
}
}
// write access
else else
{ {
// 1 2 3 4 5 6 var type = option.type;
var matches = args.match(/^\s*(no|inv)?([a-z_]+)([?&!])?(([+-])?=(.*))?/); if (type == "boolean")
if (!matches)
{ {
vimperator.echoerr("E518: Unknown option: " + args); if (matches[4])
return; vimperator.echoerr("E474: Invalid argument: " + option.name + "=" + val);
} else if (invert)
option.value = !option.value;
var no = false;
if (matches[1] == "no")
no = true;
var opt = matches[2];
var all = false;
if (opt == "all")
all = true;
var option = vimperator.options.get(opt);
if (!option && !all)
{
vimperator.echoerr("E518: Unknown option: " + opt);
return;
}
var get = false;
if (all || matches[3] == "?" || (option.type != "boolean" && matches[4] === undefined))
get = true;
var reset = false;
if (matches[3] == "&")
reset = true;
var invert = false;
if (matches[1] == "inv" || matches[3] == "!")
invert = true;
var oper = matches[5];
var val = matches[6];
if (val === undefined)
val = "";
// reset a variable to its default value
// TODO: remove the value from about:config instead of setting it to the current default value
if (reset)
{
if (all)
{
for (let opt in vimperator.options)
opt.value = opt.default_value;
}
else else
{ option.value = !no;
option.value = option.default_value;
}
} }
// read access else if (type == "number")
else if (get)
{ {
if (all) var num = parseInt(val, 10);
{ if (isNaN(num))
vimperator.options.list(); vimperator.echoerr("E521: Number required after =: " + option.name + "=" + val);
}
else else
{
if (option.type == "boolean")
vimperator.echo((option.value ? " " : "no") + option.name);
else
vimperator.echo(" " + option.name + "=" + option.value);
}
}
// write access
else
{
var type = option.type;
if (type == "boolean")
{
if (matches[4])
vimperator.echoerr("E474: Invalid argument: " + option.name + "=" + val);
else if (invert)
option.value = !option.value;
else
option.value = !no;
}
else if (type == "number")
{
var num = parseInt(val, 10);
if (isNaN(num))
vimperator.echoerr("E521: Number required after =: " + option.name + "=" + val);
else
{
var cur_val = option.value;
if (oper == '+') num = cur_val + num;
if (oper == '-') num = cur_val - num;
// FIXME
if (option.validator != null && option.validator.call(this, num) == false)
vimperator.echoerr("E474: Invalid argument: " + option.name + "=" + val); // FIXME: need to be able to specify unique parse error messages
else // all checks passed, execute option handler
option.value = num;
}
}
else if (type == "charlist" || type == "stringlist" || type == "string")
{ {
var cur_val = option.value; var cur_val = option.value;
if (type == "charlist" || type == "string") if (oper == '+') num = cur_val + num;
{ if (oper == '-') num = cur_val - num;
if (oper == '+' && !cur_val.match(val))
val = cur_val + val;
if (oper == '-') val = cur_val.replace(val, '');
}
else
{
if (oper == '+' && !cur_val.match(val) && cur_val.length > 0)
val = cur_val + ',' + val;
if (oper == '-')
{
val = cur_val.replace(new RegExp(',?' + val), '');
val = val.replace(/^,?/, '');
}
}
// FIXME // FIXME
if (option.validator != null && option.validator.call(this, val) == false) if (option.validator != null && option.validator.call(this, num) == false)
vimperator.echoerr("E474: Invalid argument: " + option.name + "=" + val); vimperator.echoerr("E474: Invalid argument: " + option.name + "=" + val); // FIXME: need to be able to specify unique parse error messages
else // all checks passed, execute option handler else // all checks passed, execute option handler
option.value = val; option.value = num;
}
}
else if (type == "charlist" || type == "stringlist" || type == "string")
{
var cur_val = option.value;
if (type == "charlist" || type == "string")
{
if (oper == '+' && !cur_val.match(val))
val = cur_val + val;
if (oper == '-') val = cur_val.replace(val, '');
} }
else else
vimperator.echoerr("E685: Internal error: option format `" + type + "' not supported"); {
if (oper == '+' && !cur_val.match(val) && cur_val.length > 0)
val = cur_val + ',' + val;
if (oper == '-')
{
val = cur_val.replace(new RegExp(',?' + val), '');
val = val.replace(/^,?/, '');
}
}
// FIXME
if (option.validator != null && option.validator.call(this, val) == false)
vimperator.echoerr("E474: Invalid argument: " + option.name + "=" + val);
else // all checks passed, execute option handler
option.value = val;
} }
else
vimperator.echoerr("E685: Internal error: option format `" + type + "' not supported");
} }
}, },
{ {
usage: ["se[t][!]", "se[t] {option}?", "se[t] [no]{option}", "se[t] {option}[+-]={value}", "se[t] {option}! | inv{option}", "se[t] {option}&"], usage: ["se[t][!]", "se[t] {option}?", "se[t] [no]{option}", "se[t] {option}[+-]={value}", "se[t] {option}! | inv{option}", "se[t] {option}&"],
short_help: "Set an option", short_help: "Set an option",
help: "Permanently change an option. In contrast to Vim options are stored throughout sessions.<br/>" + help: "Permanently change an option. In contrast to Vim options are currently stored throughout sessions.<br/>" +
"<code class=\"command\">:set</code> without an argument opens <code>about:config</code> in a new tab to change advanced Firefox options.<br/>" + "<code class=\"command\">:set</code> without an argument shows all Vimperator options which differ from their default values.<br/>" +
"<code class=\"command\">:set!</code> opens the GUI preference panel from Firefox in a new tab.<br/>" + "<code class=\"command\">:set!</code> without an argument shows all about:config preferences which differ from their default values.<br/>" +
"There are three types of options: boolean, number and string. " + "There are three types of options: boolean, number and string. " +
"Boolean options must be set with <code class=\"command\">:set option</code> and <code class=\"command\">:set nooption</code>. " + "Boolean options must be set with <code class=\"command\">:set option</code> and <code class=\"command\">:set nooption</code>. " +
"Number and string option types must be set with <code class=\"command\">:set option={value}</code>.<br/>" + "Number and string option types must be set with <code class=\"command\">:set option={value}</code>.<br/>" +
@@ -1407,8 +1429,9 @@ function Commands() //{{{
{ {
usage: ["tab {cmd}"], usage: ["tab {cmd}"],
short_help: "Execute {cmd} and tell it to output in a new tab", short_help: "Execute {cmd} and tell it to output in a new tab",
help: "Works only for commands that support it.<br/>" + help: "Works only for commands that support it, currently:" +
"Example: <code class=\"command\">:tab help tab</code> opens the help in a new tab.", "<ul><li>:tab help</li>" +
"<li>:tab prefs[!]</li></ul>",
completer: function(filter) { return vimperator.completion.get_command_completions(filter); } completer: function(filter) { return vimperator.completion.get_command_completions(filter); }
} }
)); ));

View File

@@ -301,25 +301,39 @@ function Options() //{{{
storePreference('dom.popup_allowed_events', popup_allowed_events); storePreference('dom.popup_allowed_events', popup_allowed_events);
} }
this.list = function() this.list = function(only_non_default)
{ {
// TODO: columns like Vim? // TODO: columns like Vim?
var list = ":" + vimperator.util.escapeHTML(vimperator.commandline.getCommand()) + "<br/>" + var list = ":" + vimperator.util.escapeHTML(vimperator.commandline.getCommand()) + "<br/>" +
"<table><tr align=\"left\" class=\"hl-Title\"><th>--- Options ---</th></tr>"; "<table><tr align=\"left\" class=\"hl-Title\"><th>--- Options ---</th></tr>";
var name, value; var name, value, def;
for (var i = 0; i < options.length; i++) for (var i = 0; i < options.length; i++)
{ {
name = options[i].name; name = options[i].name;
value = options[i].value; value = options[i].value;
def = options[i].default_value;
if (only_non_default && value == def)
continue;
if (options[i].type == "boolean") if (options[i].type == "boolean")
{ {
name = value ? " " + name : "no" + name; name = value ? " " + name : "no" + name;
if (value != def)
name = "<span style=\"font-weight: bold\">" + name + "</span><span style=\"color: gray\"> (default: " + (def ? "" : "no") + options[i].name + ")</span>";
list += "<tr><td>" + name + "</td></tr>"; list += "<tr><td>" + name + "</td></tr>";
} }
else else
{ {
if (value != def)
{
name = "<span style=\"font-weight: bold\">" + name + "</span>";
value = vimperator.util.colorize(value, false) + "<span style=\"color: gray\"> (default: " + def + ")</span>";
}
else
value = vimperator.util.colorize(value, false);
list += "<tr><td>" + " " + name + "=" + value + "</td></tr>"; list += "<tr><td>" + " " + name + "=" + value + "</td></tr>";
} }
} }

View File

@@ -32,6 +32,49 @@ vimperator.util = {
var e = window.content.document.createElement("div"); var e = window.content.document.createElement("div");
e.appendChild(window.content.document.createTextNode(str)); e.appendChild(window.content.document.createTextNode(str));
return e.innerHTML; return e.innerHTML;
},
// TODO: use :highlight color groups
// if "process_strings" is true, any passed strings will be surrounded by " and
// any line breaks are displayed as \n
colorize: function(arg, process_strings)
{
var type = typeof(arg);
if (type == "number")
{
return "<span style=\"color: red;\">" + arg + "</span>";
}
else if (type == "string")
{
if (process_strings)
arg = '"' + vimperator.util.escapeHTML(arg.replace(/\n/, "\\n")) + '"';
return "<span style=\"color: green;\">" + arg + "</span>";
}
else if (type == "boolean")
{
return "<span style=\"color: blue;\">" + arg + "</span>";
}
else if (arg == null || arg == "undefined")
{
return "<span style=\"color: blue;\">" + arg + "</span>";
}
else if (type == "object" || type == "function")
{
// for java packages value.toString() would crash so badly
// that we cannot even try/catch it
if (/^\[JavaPackage.*\]$/.test(arg))
return "[JavaPackage]";
var str = arg.toString();
if (typeof str == "string") // can be "undefined"
return vimperator.util.escapeHTML(str);
else
return "undefined";
}
return arg;
} }
} }

View File

@@ -434,11 +434,13 @@ const vimperator = (function() //{{{
} }
}, },
// TODO: move to vimp.util.? --mst
// if color = true it uses HTML markup to color certain items // if color = true it uses HTML markup to color certain items
objectToString: function(object, color) objectToString: function(object, color)
{ {
if (object === null) if (object === null)
return "null"; return "null";
if (typeof object != "object") if (typeof object != "object")
return false; return false;
@@ -472,32 +474,7 @@ const vimperator = (function() //{{{
if (color) if (color)
{ {
// syntax highlighting for special items value = vimperator.util.colorize(value, true);
if (typeof value === "number")
value = "<span style=\"color: red;\">" + value + "</span>";
else if (typeof value === "string")
{
value = value.replace(/\n/, "\\n").replace(/</, "&lt;");
value = "<span style=\"color: green;\">\"" + value + "\"</span>";
}
else if (typeof value === "boolean")
value = "<span style=\"color: blue;\">" + value + "</span>";
else if (value == null || value == "undefined")
value = "<span style=\"color: blue;\">" + value + "</span>";
else if (typeof value === "object" || typeof value === "function")
{
// for java packages value.toString() would crash so badly
// that we cannot even try/catch it
if (/^\[JavaPackage.*\]$/.test(value))
value = "[JavaPackage]";
else
{
var str = value.toString();
if (typeof str == "string") // can be "undefined"
value = vimperator.util.escapeHTML(str);
}
}
string += "<span style=\"font-weight: bold;\">" + i + "</span>: " + value + "\n"; string += "<span style=\"font-weight: bold;\">" + i + "</span>: " + value + "\n";
} }
else else