diff --git a/content/bookmarks.js b/content/bookmarks.js index af85db6b..f4cb31a5 100644 --- a/content/bookmarks.js +++ b/content/bookmarks.js @@ -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 () { diff --git a/content/events.js b/content/events.js index e3724dda..0d2698b9 100644 --- a/content/events.js +++ b/content/events.js @@ -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) + { + // NOTE: event can only be a comma separated list for |:au {event} {pat} {cmd}| + 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: " + event); + return; + } + } + + if (cmd) // add new command, possibly removing all others with the same event/pattern { if (special) - liberator.autocommands.remove(null, null); // remove all - else - liberator.autocommands.list(null, null); // list all + liberator.autocommands.remove(event, regex); + liberator.autocommands.add(events, regex, cmd); } 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("*"); - - if (!events.every(function (event) validEvents.indexOf(event) >= 0)) + if (event == "*") + event = null; + if (special) { - liberator.echoerr("E216: No such group or event: " + args); - return; + // TODO: "*" only appears to work in Vim when there is a {group} specified + if (args.arguments[0] != "*" || regex) + liberator.autocommands.remove(event, regex); // remove all } - - if (cmd) // add new command, possibly removing all others with the same event/pattern + else { - 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 (special) - { - // TODO: "*" only appears to work in Vim when there is a {group} specified - if (event != "*") - liberator.autocommands.remove(event, null); - } - 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); diff --git a/content/io.js b/content/io.js index 2444ad00..3db6db1d 100644 --- a/content/io.js +++ b/content/io.js @@ -354,7 +354,7 @@ liberator.IO = function () //{{{ liberator.echo(command + liberator.util.escapeHTML(output)); - liberator.autocommands.trigger("ShellCmdPost", ""); + liberator.autocommands.trigger("ShellCmdPost", {}); }, { bang: true }); diff --git a/content/liberator.js b/content/liberator.js index 7623ca56..62e60106 100644 --- a/content/liberator.js +++ b/content/liberator.js @@ -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) diff --git a/content/mail.js b/content/mail.js index 8269937d..6526fb5f 100644 --- a/content/mail.js +++ b/content/mail.js @@ -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 = []; diff --git a/content/tabs.js b/content/tabs.js index d7b1730a..487c783b 100644 --- a/content/tabs.js +++ b/content/tabs.js @@ -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)