1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 04:57:58 +01:00

Update :au to use args parser, and perform certain replacements (more to come?).

This commit is contained in:
Kris Maglione
2008-10-11 01:29:03 +00:00
parent 29cc805622
commit e92a35a197
6 changed files with 92 additions and 67 deletions

View File

@@ -44,9 +44,9 @@ liberator.Bookmarks = function () //{{{
.getService(Components.interfaces.nsIFaviconService);
const storage = liberator.storage;
const properties = { url: 0, title: 1, keyword: 2, tags: 3, id: 4, icon: 5 };
function Cache(name, store, serial)
{
const properties = { uri: 0, title: 1, keyword: 2, tags: 3, id: 4, icon: 5 };
const rootFolders = [bookmarksService.toolbarFolder, bookmarksService.bookmarksMenuFolder, bookmarksService.unfiledBookmarksFolder];
var bookmarks = [];
@@ -67,7 +67,7 @@ liberator.Bookmarks = function () //{{{
let keyword = bookmarksService.getKeywordForBookmark(node.itemId);
let tags = taggingService.getTagsForURI(uri, {}) || [];
let icon = faviconService.getFaviconImageForPage(uri).spec;
bookmarks.push([node.uri, node.title, keyword, tags, node.itemId, icon]);
return bookmarks.push([node.uri, node.title, keyword, tags, node.itemId, icon]);
}
function readBookmark(id)
@@ -144,8 +144,8 @@ liberator.Bookmarks = function () //{{{
{
if (rootFolders.indexOf(findRoot(itemId)) >= 0)
{
loadBookmark(readBookmark(itemId));
storage.fireEvent(name, "add", itemId);
let bmark = loadBookmark(readBookmark(itemId));
storage.fireEvent(name, "add", bmark);
}
}
},
@@ -164,7 +164,7 @@ liberator.Bookmarks = function () //{{{
if (bookmark)
{
if (property == "tags")
value = taggingService.getTagsForURI(ioService.newURI(bookmark[properties.uri], null, null), {});
value = taggingService.getTagsForURI(ioService.newURI(bookmark[properties.url], null, null), {});
if (property in properties)
bookmark[properties[property]] = value;
storage.fireEvent(name, "change", itemId);
@@ -184,9 +184,15 @@ liberator.Bookmarks = function () //{{{
let bookmarkObserver = function (key, event, arg)
{
if (event == "add")
liberator.autocommands.trigger("BookmarkAdd", "");
{
let args = {};
for (let [k, v] in Iterator(properties))
args[k] = arg[v];
liberator.autocommands.trigger("BookmarkAdd", args);
}
liberator.statusline.updateUrl();
}
var cache = liberator.storage.newObject("bookmark-cache", Cache, false);
liberator.storage.addObserver("bookmark-cache", bookmarkObserver);
liberator.registerObserver("shutdown", function () {

View File

@@ -72,61 +72,49 @@ liberator.AutoCommands = function () //{{{
"Execute commands automatically on events",
function (args, special)
{
if (!args)
let [event, regex] = args.arguments;
let cmd = args.literalArg;
let events = null;
if (event)
{
if (special)
liberator.autocommands.remove(null, null); // remove all
else
liberator.autocommands.list(null, null); // list all
}
else
{
// TODO: assume the pattern doesn't contain spaces until we have whitespace escaping
let [, event, regex, cmd] = args.match(/^(\S+)?(?:\s+)?(\S+)?(?:\s+)?(.+)?$/);
// NOTE: event can only be a comma separated list for |:au {event} {pat} {cmd}|
let events = event.split(",");
let validEvents = liberator.config.autocommands.map(function (event) event[0]);
validEvents.push("*");
events = event.split(",");
if (!events.every(function (event) validEvents.indexOf(event) >= 0))
{
liberator.echoerr("E216: No such group or event: " + args);
liberator.echoerr("E216: No such group or event: " + event);
return;
}
}
if (cmd) // add new command, possibly removing all others with the same event/pattern
{
if (special)
liberator.autocommands.remove(event, regex);
liberator.autocommands.add(events, regex, cmd);
}
else if (regex)
{
if (special)
liberator.autocommands.remove(event == "*" ? null : event, regex);
else
liberator.autocommands.list(event == "*" ? null : event, regex);
}
else if (event)
{
if (event == "*")
event = null;
if (special)
{
// TODO: "*" only appears to work in Vim when there is a {group} specified
if (event != "*")
liberator.autocommands.remove(event, null);
if (args.arguments[0] != "*" || regex)
liberator.autocommands.remove(event, regex); // remove all
}
else
{
liberator.autocommands.list(event == "*" ? null : event, null);
}
liberator.autocommands.list(event, regex); // list all
}
}
},
{
argCount: 2,
bang: true,
literal: true,
completer: function (filter) liberator.completion.event(filter)
});
@@ -166,10 +154,11 @@ liberator.AutoCommands = function () //{{{
else
{
// TODO: perhaps trigger could return the number of autocmds triggered
// TODO: Perhaps this should take -args to pass to the command?
if (!liberator.autocommands.get(event).some(function (c) c.pattern.test(url)))
liberator.echo("No matching autocommands");
else
liberator.autocommands.trigger(event, url);
liberator.autocommands.trigger(event, {url: url});
}
},
{
@@ -249,7 +238,7 @@ liberator.AutoCommands = function () //{{{
liberator.commandline.echo(list, liberator.commandline.HL_NORMAL, liberator.commandline.FORCE_MULTILINE);
},
trigger: function (event, url)
trigger: function (event, args)
{
let events = liberator.options["eventignore"].split(",");
@@ -262,6 +251,7 @@ liberator.AutoCommands = function () //{{{
let lastPattern = null;
let url = args.url || "";
for (let [,autoCmd] in Iterator(autoCmds))
{
if (autoCmd.pattern.test(url))
@@ -272,7 +262,7 @@ liberator.AutoCommands = function () //{{{
lastPattern = autoCmd.pattern;
liberator.echomsg("autocommand " + autoCmd.command, 9);
liberator.execute(autoCmd.command);
liberator.execute(liberator.commands.replaceTokens(autoCmd.command, args));
}
}
}
@@ -475,10 +465,22 @@ liberator.Events = function () //{{{
return false;
}
function triggerLoadAutocmd(name, doc)
{
let args = {
url: doc.location.href,
title: doc.title
}
if (liberator.has("tabs"))
args.tab = liberator.tabs.getContentIndex(doc) + 1;
liberator.autocommands.trigger(name, args);
}
function onDOMContentLoaded(event)
{
if (event.originalTarget instanceof HTMLDocument)
liberator.autocommands.trigger("DOMLoad", event.originalTarget.location.href);
triggerLoadAutocmd("DOMLoad", event.originalTarget);
}
// TODO: see what can be moved to onDOMContentLoaded()
@@ -486,7 +488,7 @@ liberator.Events = function () //{{{
{
if (event.originalTarget instanceof HTMLDocument)
{
var doc = event.originalTarget;
let doc = event.originalTarget;
// document is part of a frameset
if (doc.defaultView.frameElement)
{
@@ -499,8 +501,8 @@ liberator.Events = function () //{{{
// code which should happen for all (also background) newly loaded tabs goes here:
var url = doc.location.href;
var title = doc.title;
let url = doc.location.href;
let title = doc.title;
// update history
if (url && liberator.history)
@@ -530,7 +532,7 @@ liberator.Events = function () //{{{
liberator.echomsg("Background tab loaded: " + title || url, 1);
}
liberator.autocommands.trigger("PageLoad", url);
triggerLoadAutocmd("PageLoad", doc);
}
}
@@ -1474,7 +1476,7 @@ liberator.Events = function () //{{{
liberator.buffer.loaded = 0;
liberator.statusline.updateProgress(0);
liberator.autocommands.trigger("PageLoadPre", liberator.buffer.URL);
liberator.autocommands.trigger("PageLoadPre", {url: liberator.buffer.URL});
// don't reset mode if a frame of the frameset gets reloaded which
// is not the focused frame
@@ -1516,7 +1518,7 @@ liberator.Events = function () //{{{
liberator.statusline.updateUrl();
liberator.statusline.updateProgress();
liberator.autocommands.trigger("LocationChange", liberator.buffer.URL);
liberator.autocommands.trigger("LocationChange", {url: liberator.buffer.URL});
// if this is not delayed we get the position of the old buffer
setTimeout(function () { liberator.statusline.updateBufferPosition(); }, 100);

View File

@@ -354,7 +354,7 @@ liberator.IO = function () //{{{
liberator.echo(command + liberator.util.escapeHTML(output));
liberator.autocommands.trigger("ShellCmdPost", "");
liberator.autocommands.trigger("ShellCmdPost", {});
},
{ bang: true });

View File

@@ -1155,7 +1155,7 @@ const liberator = (function () //{{{
}
liberator.triggerObserver("enter", null);
liberator.autocommands.trigger(liberator.config.name + "Enter", "");
liberator.autocommands.trigger(liberator.config.name + "Enter", {});
}, 0);
liberator.statusline.update();
@@ -1165,7 +1165,7 @@ const liberator = (function () //{{{
shutdown: function ()
{
liberator.autocommands.trigger(liberator.config.name + "LeavePre", "");
liberator.autocommands.trigger(liberator.config.name + "LeavePre", {});
liberator.storage.saveAll();
@@ -1173,7 +1173,7 @@ const liberator = (function () //{{{
liberator.dump("All liberator modules destroyed\n");
liberator.autocommands.trigger(liberator.config.name + "Leave", "");
liberator.autocommands.trigger(liberator.config.name + "Leave", {});
},
sleep: function (ms)

View File

@@ -54,7 +54,7 @@ liberator.Mail = function () //{{{
if (folder)
{
var msgFolder = folder.QueryInterface(Components.interfaces.nsIMsgFolder);
liberator.autocommands.trigger("FolderLoaded", msgFolder);
liberator.autocommands.trigger("FolderLoaded", {url: msgFolder});
// Jump to a message when requested
var indices = [];

View File

@@ -634,6 +634,13 @@ liberator.Tabs = function () //{{{
get alternate() alternates[1],
get browsers() function ()
{
let browsers = getBrowser.browsers;
for (let i = 0; i < browsers.length; i++)
yield [i, browsers[i]];
},
get count() getBrowser().mTabs.length,
// used for :setlocal
@@ -676,17 +683,27 @@ liberator.Tabs = function () //{{{
get: function (filter)
{
var buffers = [];
var browsers = getBrowser().browsers;
for (let i = 0; i < browsers.length; i++)
for (let [i, browser] in this.browsers)
{
var title = browsers[i].contentTitle || "(Untitled)";
var uri = browsers[i].currentURI.spec;
var title = browser.contentTitle || "(Untitled)";
var uri = browser.currentURI.spec;
var number = i + 1;
buffers.push([number, title, uri]);
}
return buffers;
},
getContentIndex: function (content)
{
for (let [i, browser] in this.browsers)
{
if (browser.contentWindow == content)
return i;
if (browser.contentDocument == content)
return i;
}
},
getTab: function (index)
{
if (index)