]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/content/tabs.js
Import r6976 from upstream hg supporting Firefox up to 25.*
[dactyl.git] / common / content / tabs.js
index 016355fa9630d8fa0980498c6f36bccd337b0f84..a7f038e3603b6c4004ed26b3d22aeabb2fa37f05 100644 (file)
@@ -1,6 +1,6 @@
 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
-// Copyright (c) 2008-2012 Kris Maglione <maglione.k at Gmail>
+// Copyright (c) 2008-2013 Kris Maglione <maglione.k at Gmail>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
@@ -37,7 +37,7 @@ var Tabs = Module("tabs", {
 
         this.tabBinding = styles.system.add("tab-binding", "chrome://browser/content/browser.xul", literal(/*
                 xul|tab { -moz-binding: url(chrome://dactyl/content/bindings.xml#tab) !important; }
-            */).replace(/tab-./g, function (m) config.OS.isMacOSX ? "tab-mac" : m),
+            */).replace(/tab-./g, m => config.OS.isMacOSX ? "tab-mac" : m),
             false, true);
 
         this.timeout(function () {
@@ -57,7 +57,7 @@ var Tabs = Module("tabs", {
         }
     },
 
-    _alternates: Class.Memoize(function () [config.tabbrowser.mCurrentTab, null]),
+    _alternates: Class.Memoize(() => [config.tabbrowser.mCurrentTab, null]),
 
     cleanup: function cleanup() {
         for (let [i, tab] in Iterator(this.allTabs)) {
@@ -139,7 +139,7 @@ var Tabs = Module("tabs", {
      */
     get options() this.localStore.options,
 
-    get visibleTabs() config.tabbrowser.visibleTabs || this.allTabs.filter(function (tab) !tab.hidden),
+    get visibleTabs() config.tabbrowser.visibleTabs || this.allTabs.filter(tab => !tab.hidden),
 
     /**
      * Returns the local state store for the tab at the specified *tabIndex*.
@@ -244,7 +244,7 @@ var Tabs = Module("tabs", {
 
         this._groups = iframe ? iframe.contentWindow : null;
         if (this._groups && !func)
-            util.waitFor(function () this._groups.TabItems, this);
+            util.waitFor(() => this._groups.TabItems);
         return this._groups;
     },
 
@@ -408,8 +408,7 @@ var Tabs = Module("tabs", {
      * @param {number} count How many tabs to remove.
      * @param {boolean} focusLeftTab Focus the tab to the left of the removed tab.
      */
-    remove: function remove(tab, count, focusLeftTab) {
-        count = count || 1;
+    remove: function remove(tab, count = 1, focusLeftTab = false) {
         let res = this.count > count;
 
         let tabs = this.visibleTabs;
@@ -426,9 +425,13 @@ var Tabs = Module("tabs", {
         }
 
         if (focusLeftTab)
-            tabs.slice(Math.max(0, index + 1 - count), index + 1).forEach(config.closure.removeTab);
+            tabs.slice(Math.max(0, index + 1 - count),
+                       index + 1)
+                .forEach(config.closure.removeTab);
         else
-            tabs.slice(index, index + count).forEach(config.closure.removeTab);
+            tabs.slice(index,
+                       index + count)
+                .forEach(config.closure.removeTab);
         return res;
     },
 
@@ -548,11 +551,11 @@ var Tabs = Module("tabs", {
         if (matches)
             return tabs.select(this.allTabs[parseInt(matches[1], 10) - 1], false);
 
-        matches = array.nth(tabs.allTabs, function (t) (t.linkedBrowser.lastURI || {}).spec === buffer, 0);
+        matches = array.nth(tabs.allTabs, t => (t.linkedBrowser.lastURI || {}).spec === buffer, 0);
         if (matches)
             return tabs.select(matches, false);
 
-        matches = completion.runCompleter("buffer", buffer).map(function (obj) obj.tab);
+        matches = completion.runCompleter("buffer", buffer).map(obj => obj.tab);
 
         if (matches.length == 0)
             dactyl.echoerr(_("buffer.noMatching", buffer));
@@ -650,7 +653,7 @@ var Tabs = Module("tabs", {
                 count: true,
                 completer: function (context, args) {
                     if (!args.bang)
-                        context.filters.push(function ({ item }) !item.tab.pinned);
+                        context.filters.push(({ item }) => !item.tab.pinned);
                     completion.buffer(context);
                 }
             });
@@ -665,7 +668,7 @@ var Tabs = Module("tabs", {
                 argCount: "?",
                 count: true,
                 completer: function (context, args) {
-                    context.filters.push(function ({ item }) item.tab.pinned);
+                    context.filters.push(({ item }) => item.tab.pinned);
                     completion.buffer(context);
                 }
             });
@@ -730,7 +733,7 @@ var Tabs = Module("tabs", {
 
         commands.add(["tabl[ast]", "bl[ast]"],
             "Switch to the last tab",
-            function () tabs.select("$", false),
+            function () { tabs.select("$", false); },
             { argCount: "0" });
 
         // TODO: "Zero count" if 0 specified as arg
@@ -794,7 +797,12 @@ var Tabs = Module("tabs", {
         if (config.has("tabbrowser")) {
             commands.add(["b[uffer]"],
                 "Switch to a buffer",
-                function (args) { tabs.switchTo(args[0], args.bang, args.count); }, {
+                function (args) {
+                    if (args.length)
+                        tabs.switchTo(args[0], args.bang, args.count);
+                    else if (args.count)
+                        tabs.switchTo(String(args.count));
+                }, {
                     argCount: "?",
                     bang: true,
                     count: true,
@@ -893,10 +901,10 @@ var Tabs = Module("tabs", {
             commands.add(["taba[ttach]"],
                 "Attach the current tab to another window",
                 function (args) {
-                    dactyl.assert(args.length <= 2 && !args.some(function (i) !/^\d+(?:$|:)/.test(i)),
+                    dactyl.assert(args.length <= 2 && !args.some(i => !/^\d+(?:$|:)/.test(i)),
                                   _("error.trailingCharacters"));
 
-                    let [winIndex, tabIndex] = args.map(function (arg) parseInt(arg));
+                    let [winIndex, tabIndex] = args.map(arg => parseInt(arg));
                     if (args["-group"]) {
                         util.assert(args.length == 1);
                         window.TabView.moveTabTo(tabs.getTab(), winIndex);
@@ -912,12 +920,6 @@ var Tabs = Module("tabs", {
                     let modules     = win.dactyl.modules;
                     let { browser } = modules.config;
 
-                    if (args[1]) {
-                        let tabList = modules.tabs.visibleTabs;
-                        let target  = dactyl.assert(tabList[tabIndex]);
-                        tabIndex = Array.indexOf(tabs.allTabs, target) - 1;
-                    }
-
                     let newTab = browser.addTab("about:blank");
                     browser.stop();
                     // XXX: the implementation of DnD in tabbrowser.xml suggests
@@ -925,10 +927,13 @@ var Tabs = Module("tabs", {
                     // without this reference?
                     browser.docShell;
 
-                    let last = modules.tabs.allTabs.length - 1;
+                    if (args[1]) {
+                        let { visibleTabs, allTabs } = modules.tabs;
+                        tabIndex = Math.constrain(tabIndex, 1, visibleTabs.length);
+                        let target = visibleTabs[tabIndex - 1];
+                        browser.moveTabTo(newTab, Array.indexOf(allTabs, target));
+                    }
 
-                    if (args[1])
-                        browser.moveTabTo(newTab, tabIndex);
                     browser.selectedTab = newTab; // required
                     browser.swapBrowsersAndCloseOther(newTab, sourceTab);
                 }, {
@@ -940,7 +945,7 @@ var Tabs = Module("tabs", {
                             if (args["-group"])
                                 completion.tabGroup(context);
                             else {
-                                context.filters.push(function ({ item }) item != window);
+                                context.filters.push(({ item }) => item != window);
                                 completion.window(context);
                             }
                             break;
@@ -992,7 +997,9 @@ var Tabs = Module("tabs", {
                         context.anchored = false;
                         context.compare = CompletionContext.Sort.unsorted;
                         context.filters = [CompletionContext.Filter.textDescription];
-                        context.keys = { text: function ([i, { state: s }]) (i + 1) + ": " + s.entries[s.index - 1].url, description: "[1].title", icon: "[1].image" };
+                        context.keys = { text: function ([i, { state: s }]) (i + 1) + ": " + s.entries[s.index - 1].url,
+                                         description: "[1].title",
+                                         icon: "[1].image" };
                         context.completions = Iterator(tabs.closedTabs);
                     },
                     count: true,
@@ -1061,7 +1068,7 @@ var Tabs = Module("tabs", {
             for (let [id, vals] in Iterator(tabGroups))
                 context.fork(id, 0, this, function (context, [name, browsers]) {
                     context.title = [name || "Buffers"];
-                    context.generate = function ()
+                    context.generate = () =>
                         Array.map(browsers, function ([i, browser]) {
                             let indicator = " ";
                             if (i == tabs.index())
@@ -1089,7 +1096,7 @@ var Tabs = Module("tabs", {
             context.keys = {
                 text: "id",
                 description: function (group) group.getTitle() ||
-                    group.getChildren().map(function (t) t.tab.label).join(", ")
+                    group.getChildren().map(t => t.tab.label).join(", ")
             };
             context.generate = function () {
                 context.incomplete = true;
@@ -1166,12 +1173,12 @@ var Tabs = Module("tabs", {
 
             mappings.add([modes.NORMAL], ["d"],
                 "Delete current buffer",
-                function ({ count }) { tabs.remove(tabs.getTab(), count, false); },
+                function ({ count }) { tabs.remove(tabs.getTab(), count || 1, false); },
                 { count: true });
 
             mappings.add([modes.NORMAL], ["D"],
                 "Delete current buffer, focus tab to the left",
-                function ({ count }) { tabs.remove(tabs.getTab(), count, true); },
+                function ({ count }) { tabs.remove(tabs.getTab(), count || 1, true); },
                 { count: true });
 
             mappings.add([modes.NORMAL], ["gb"],