mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-22 00:47:59 +01:00
Also group :autocmds.
--HG-- branch : groups
This commit is contained in:
@@ -8,16 +8,25 @@
|
|||||||
|
|
||||||
/** @scope modules */
|
/** @scope modules */
|
||||||
|
|
||||||
var AutoCommand = Struct("event", "patterns", "command");
|
var AutoCommand = Struct("event", "filter", "command");
|
||||||
|
update(AutoCommand.prototype, {
|
||||||
|
eventName: Class.memoize(function () this.event.toLowerCase()),
|
||||||
|
|
||||||
/**
|
match: function (event, pattern) {
|
||||||
* @instance autocommands
|
return (!event || this.eventName == event.toLowerCase()) && (!pattern || String(this.filter) === pattern);
|
||||||
*/
|
}
|
||||||
var AutoCommands = Module("autocommands", {
|
});
|
||||||
init: function () {
|
|
||||||
|
var AutoCmdHive = Class("AutoCmdHive", {
|
||||||
|
init: function init(group) {
|
||||||
|
this.group = group;
|
||||||
this._store = [];
|
this._store = [];
|
||||||
},
|
},
|
||||||
|
|
||||||
|
get toStringParams() [this.group.name],
|
||||||
|
|
||||||
|
get builtin() this.group.builtin,
|
||||||
|
|
||||||
__iterator__: function () array.iterValues(this._store),
|
__iterator__: function () array.iterValues(this._store),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -26,24 +35,26 @@ var AutoCommands = Module("autocommands", {
|
|||||||
*
|
*
|
||||||
* @param {Array} events The array of event names for which this
|
* @param {Array} events The array of event names for which this
|
||||||
* autocommand should be executed.
|
* autocommand should be executed.
|
||||||
* @param {string} regexp The URL pattern to match against the buffer URL.
|
* @param {string} pattern The URL pattern to match against the buffer URL.
|
||||||
* @param {string} cmd The Ex command to run.
|
* @param {string} cmd The Ex command to run.
|
||||||
*/
|
*/
|
||||||
add: function (events, regexp, cmd) {
|
add: function (events, pattern, cmd) {
|
||||||
events.forEach(function (event) {
|
if (!callable(pattern))
|
||||||
this._store.push(AutoCommand(event, Option.parse.regexplist(regexp.source || regexp), cmd));
|
pattern = Group.compileFilter(pattern);
|
||||||
}, this);
|
|
||||||
|
for (let event in values(events))
|
||||||
|
this._store.push(AutoCommand(event, pattern, cmd));
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns all autocommands with a matching *event* and *regexp*.
|
* Returns all autocommands with a matching *event* and *regexp*.
|
||||||
*
|
*
|
||||||
* @param {string} event The event name filter.
|
* @param {string} event The event name filter.
|
||||||
* @param {string} regexp The URL pattern filter.
|
* @param {string} pattern The URL pattern filter.
|
||||||
* @returns {AutoCommand[]}
|
* @returns {AutoCommand[]}
|
||||||
*/
|
*/
|
||||||
get: function (event, regexp) {
|
get: function (event, pattern) {
|
||||||
return this._store.filter(function (autoCmd) AutoCommands.matchAutoCmd(autoCmd, event, regexp));
|
return this._store.filter(function (autoCmd) autoCmd.match(event, regexp));
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -53,8 +64,25 @@ var AutoCommands = Module("autocommands", {
|
|||||||
* @param {string} regexp The URL pattern filter.
|
* @param {string} regexp The URL pattern filter.
|
||||||
*/
|
*/
|
||||||
remove: function (event, regexp) {
|
remove: function (event, regexp) {
|
||||||
this._store = this._store.filter(function (autoCmd) !AutoCommands.matchAutoCmd(autoCmd, event, regexp));
|
this._store = this._store.filter(function (autoCmd) !autoCmd.match(event, regexp));
|
||||||
},
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @instance autocommands
|
||||||
|
*/
|
||||||
|
var AutoCommands = Module("autocommands", {
|
||||||
|
init: function () {
|
||||||
|
this.user = contexts.subGroup.autocmd.user;
|
||||||
|
},
|
||||||
|
|
||||||
|
hives: Group.SubGroup("autocmd", AutoCmdHive),
|
||||||
|
|
||||||
|
get activeHives() contexts.activeGroups("autocmd").map(function (h) h.autocmd),
|
||||||
|
|
||||||
|
add: deprecated("autocommand.user.add", { get: function add() autocommands.user.closure.add }),
|
||||||
|
get: deprecated("autocommand.user.get", { get: function get() autocommands.user.closure.get }),
|
||||||
|
remove: deprecated("autocommand.user.remove", { get: function remove() autocommands.user.closure.remove }),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Lists all autocommands with a matching *event* and *regexp*.
|
* Lists all autocommands with a matching *event* and *regexp*.
|
||||||
@@ -63,32 +91,39 @@ var AutoCommands = Module("autocommands", {
|
|||||||
* @param {string} regexp The URL pattern filter.
|
* @param {string} regexp The URL pattern filter.
|
||||||
*/
|
*/
|
||||||
list: function (event, regexp) {
|
list: function (event, regexp) {
|
||||||
let cmds = {};
|
|
||||||
|
|
||||||
// XXX
|
function cmds(hive) {
|
||||||
this._store.forEach(function (autoCmd) {
|
let cmds = {};
|
||||||
if (AutoCommands.matchAutoCmd(autoCmd, event, regexp)) {
|
hive._store.forEach(function (autoCmd) {
|
||||||
cmds[autoCmd.event] = cmds[autoCmd.event] || [];
|
if (autoCmd.match(event, regexp)) {
|
||||||
cmds[autoCmd.event].push(autoCmd);
|
cmds[autoCmd.event] = cmds[autoCmd.event] || [];
|
||||||
}
|
cmds[autoCmd.event].push(autoCmd);
|
||||||
});
|
}
|
||||||
|
});
|
||||||
|
return cmds;
|
||||||
|
}
|
||||||
|
|
||||||
commandline.commandOutput(
|
commandline.commandOutput(
|
||||||
<table>
|
<table>
|
||||||
<tr highlight="Title">
|
<tr highlight="Title">
|
||||||
<td colspan="2">----- Auto Commands -----</td>
|
<td colspan="3">----- Auto Commands -----</td>
|
||||||
</tr>
|
</tr>
|
||||||
{
|
{
|
||||||
template.map(cmds, function ([event, items])
|
template.map(this.activeHives, function (hive)
|
||||||
<tr highlight="Title">
|
<tr highlight="Title">
|
||||||
<td colspan="2">{event}</td>
|
<td colspan="3">{hive.group.name}</td>
|
||||||
</tr>
|
</tr> +
|
||||||
+
|
<tr style="height: .5ex;"/> +
|
||||||
template.map(items, function (item)
|
template.map(cmds(hive), function ([event, items])
|
||||||
<tr>
|
<tr style="height: .5ex;"/> +
|
||||||
<td> {item.patterns}</td>
|
template.map(items, function (item, i)
|
||||||
<td>{item.command}</td>
|
<tr>
|
||||||
</tr>))
|
<td highlight="Title" style="padding-right: 1em;">{i == 0 ? event : ""}</td>
|
||||||
|
<td>{item.filter}</td>
|
||||||
|
<td>{item.command}</td>
|
||||||
|
</tr>) +
|
||||||
|
<tr style="height: .5ex;"/>) +
|
||||||
|
<tr style="height: .5ex;"/>)
|
||||||
}
|
}
|
||||||
</table>);
|
</table>);
|
||||||
},
|
},
|
||||||
@@ -104,29 +139,26 @@ var AutoCommands = Module("autocommands", {
|
|||||||
if (options.get("eventignore").has(event))
|
if (options.get("eventignore").has(event))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
let autoCmds = this._store.filter(function (autoCmd) autoCmd.event == event);
|
|
||||||
|
|
||||||
dactyl.echomsg('Executing ' + event + ' Auto commands for "*"', 8);
|
dactyl.echomsg('Executing ' + event + ' Auto commands for "*"', 8);
|
||||||
|
|
||||||
let lastPattern = null;
|
let lastPattern = null;
|
||||||
let url = args.url || "";
|
let uri = args.url ? util.newURI(args.url) : buffer.uri;
|
||||||
|
|
||||||
for (let [, autoCmd] in Iterator(autoCmds)) {
|
event = event.toLowerCase();
|
||||||
if (autoCmd.patterns.some(function (re) re.test(url) ^ !re.result)) {
|
for (let hive in this.hives.iterValues())
|
||||||
if (!lastPattern || String(lastPattern) != String(autoCmd.patterns))
|
for (let autoCmd in values(hive._store))
|
||||||
dactyl.echomsg("Executing " + event + " Auto commands for " + autoCmd.patterns, 8);
|
if (autoCmd.eventName === event && autoCmd.filter(uri)) {
|
||||||
|
if (!lastPattern || lastPattern !== String(autoCmd.filter))
|
||||||
|
dactyl.echomsg("Executing " + event + " Auto commands for " + autoCmd.filter, 8);
|
||||||
|
|
||||||
lastPattern = autoCmd.patterns;
|
lastPattern = String(autoCmd.filter);
|
||||||
dactyl.echomsg("autocommand " + autoCmd.command, 9);
|
dactyl.echomsg("autocommand " + autoCmd.command, 9);
|
||||||
|
|
||||||
dactyl.trapErrors(autoCmd.command, autoCmd, args);
|
dactyl.trapErrors(autoCmd.command, autoCmd, args);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
matchAutoCmd: function (autoCmd, event, regexp) {
|
|
||||||
return (!event || autoCmd.event == event) && (!regexp || String(autoCmd.patterns) == regexp);
|
|
||||||
}
|
|
||||||
}, {
|
}, {
|
||||||
commands: function () {
|
commands: function () {
|
||||||
commands.add(["au[tocmd]"],
|
commands.add(["au[tocmd]"],
|
||||||
@@ -135,29 +167,21 @@ var AutoCommands = Module("autocommands", {
|
|||||||
let [event, regexp, cmd] = args;
|
let [event, regexp, cmd] = args;
|
||||||
let events = [];
|
let events = [];
|
||||||
|
|
||||||
try {
|
|
||||||
if (args.length > 1)
|
|
||||||
Option.parse.regexplist(regexp);
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
dactyl.assert(false, "E475: Invalid argument: " + regexp);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event) {
|
if (event) {
|
||||||
// NOTE: event can only be a comma separated list for |:au {event} {pat} {cmd}|
|
// NOTE: event can only be a comma separated list for |:au {event} {pat} {cmd}|
|
||||||
let validEvents = Object.keys(config.autocommands);
|
let validEvents = Object.keys(config.autocommands).map(String.toLowerCase);
|
||||||
validEvents.push("*");
|
validEvents.push("*");
|
||||||
|
|
||||||
events = Option.parse.stringlist(event);
|
events = Option.parse.stringlist(event);
|
||||||
dactyl.assert(events.every(function (event) validEvents.indexOf(event) >= 0),
|
dactyl.assert(events.every(function (event) validEvents.indexOf(event.toLowerCase()) >= 0),
|
||||||
"E216: No such group or event: " + event);
|
"E216: No such group or event: " + event);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (args.length > 2) { // add new command, possibly removing all others with the same event/pattern
|
if (args.length > 2) { // add new command, possibly removing all others with the same event/pattern
|
||||||
if (args.bang)
|
if (args.bang)
|
||||||
autocommands.remove(event, regexp);
|
args["-group"].remove(event, regexp);
|
||||||
cmd = contexts.bindMacro(args, "-ex", function (params) params);
|
cmd = contexts.bindMacro(args, "-ex", function (params) params);
|
||||||
autocommands.add(events, regexp, cmd);
|
args["-group"].add(events, regexp, cmd);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (event == "*")
|
if (event == "*")
|
||||||
@@ -166,7 +190,7 @@ var AutoCommands = Module("autocommands", {
|
|||||||
if (args.bang) {
|
if (args.bang) {
|
||||||
// TODO: "*" only appears to work in Vim when there is a {group} specified
|
// TODO: "*" only appears to work in Vim when there is a {group} specified
|
||||||
if (args[0] != "*" || args.length > 1)
|
if (args[0] != "*" || args.length > 1)
|
||||||
autocommands.remove(event, regexp); // remove all
|
args["-group"].remove(event, regexp); // remove all
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
autocommands.list(event, regexp); // list all
|
autocommands.list(event, regexp); // list all
|
||||||
@@ -183,6 +207,7 @@ var AutoCommands = Module("autocommands", {
|
|||||||
keepQuotes: true,
|
keepQuotes: true,
|
||||||
literal: 2,
|
literal: 2,
|
||||||
options: [
|
options: [
|
||||||
|
contexts.GroupFlag("autocmd"),
|
||||||
{
|
{
|
||||||
names: ["-javascript", "-js"],
|
names: ["-javascript", "-js"],
|
||||||
description: "Interpret the action as JavaScript code rather than an Ex command"
|
description: "Interpret the action as JavaScript code rather than an Ex command"
|
||||||
@@ -245,7 +270,7 @@ var AutoCommands = Module("autocommands", {
|
|||||||
};
|
};
|
||||||
},
|
},
|
||||||
javascript: function () {
|
javascript: function () {
|
||||||
JavaScript.setCompleter(autocommands.get, [function () Iterator(config.autocommands)]);
|
JavaScript.setCompleter(autocommands.user.get, [function () Iterator(config.autocommands)]);
|
||||||
},
|
},
|
||||||
options: function () {
|
options: function () {
|
||||||
options.add(["eventignore", "ei"],
|
options.add(["eventignore", "ei"],
|
||||||
|
|||||||
@@ -27,6 +27,26 @@ var Group = Class("Group", {
|
|||||||
subGroups: {}
|
subGroups: {}
|
||||||
|
|
||||||
}, {
|
}, {
|
||||||
|
compileFilter: function (patterns) {
|
||||||
|
|
||||||
|
function siteFilter(uri) siteFilter.filters.every(function (f) f(uri) == f.result);
|
||||||
|
|
||||||
|
patterns = Option.splitList(patterns, true);
|
||||||
|
|
||||||
|
return update(siteFilter, {
|
||||||
|
toString: function () this.filters.join(","),
|
||||||
|
|
||||||
|
filters: patterns.map(function (pattern) {
|
||||||
|
let [, res, filter] = /^(!?)(.*)/.exec(pattern);
|
||||||
|
|
||||||
|
return update(Styles.matchFilter(Option.dequote(filter)), {
|
||||||
|
result: !res,
|
||||||
|
toString: function () pattern
|
||||||
|
});
|
||||||
|
})
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
groupsProto: {},
|
groupsProto: {},
|
||||||
|
|
||||||
subGroupMap: {},
|
subGroupMap: {},
|
||||||
@@ -256,20 +276,7 @@ var Contexts = Module("contexts", {
|
|||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
dactyl.assert(!group || args.bang, "Group exists");
|
dactyl.assert(!group || args.bang, "Group exists");
|
||||||
|
|
||||||
let filter = function siteFilter(uri)
|
let filter = Group.compileFilter(args[1]);
|
||||||
siteFilter.filters.every(function (f) f(uri) == f.result);
|
|
||||||
|
|
||||||
update(filter, {
|
|
||||||
toString: function () this.filters.join(","),
|
|
||||||
filters: Option.splitList(args[1], true).map(function (pattern) {
|
|
||||||
let [, res, filter] = /^(!?)(.*)/.exec(pattern);
|
|
||||||
|
|
||||||
return update(Styles.matchFilter(Option.dequote(filter)), {
|
|
||||||
result: !res,
|
|
||||||
toString: function () pattern
|
|
||||||
});
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
group = contexts.addGroup(name, args["-description"], filter, !args["-nopersist"]);
|
group = contexts.addGroup(name, args["-description"], filter, !args["-nopersist"]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,10 +86,10 @@ var Template = Module("Template", {
|
|||||||
let ret = <></>;
|
let ret = <></>;
|
||||||
let n = 0;
|
let n = 0;
|
||||||
for each (let i in Iterator(iter)) {
|
for each (let i in Iterator(iter)) {
|
||||||
let val = func(i);
|
let val = func(i, n);
|
||||||
if (val == undefined)
|
if (val == undefined)
|
||||||
continue;
|
continue;
|
||||||
if (sep && n++)
|
if (n++ && sep)
|
||||||
ret += sep;
|
ret += sep;
|
||||||
if (interruptable && n % interruptable == 0)
|
if (interruptable && n % interruptable == 0)
|
||||||
util.threadYield(true, true);
|
util.threadYield(true, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user