]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/content/browser.js
Import r6923 from upstream hg supporting Firefox up to 22.0a1
[dactyl.git] / common / content / browser.js
index 746b46ec01352d65b0524660bc190d2d73b6da6d..a1b111094fd986cd1bb80763c2d9d862365fe984 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-2011 by Kris Maglione <maglione.k at Gmail>
+// Copyright (c) 2008-2012 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.
@@ -13,8 +13,8 @@
  */
 var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
     init: function init() {
-        this.cleanupProgressListener = util.overlayObject(window.XULBrowserWindow,
-                                                          this.progressListener);
+        this.cleanupProgressListener = overlay.overlayObject(window.XULBrowserWindow,
+                                                             this.progressListener);
         util.addObserver(this);
     },
 
@@ -28,8 +28,11 @@ var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
         "content-document-global-created": function (win, uri) {
             let top = util.topWindow(win);
 
-            if (top == window)
-                this._triggerLoadAutocmd("PageLoadPre", win.document, win.location.href != "null" ? window.location.href : uri);
+            if (uri == "null")
+                uri = null;
+
+            if (top == window && (win.location.href || uri))
+                this._triggerLoadAutocmd("PageLoadPre", win.document, win.location.href || uri);
         }
     },
 
@@ -43,12 +46,18 @@ var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
             title: doc.title
         };
 
-        if (dactyl.has("tabs")) {
+        if (!dactyl.has("tabs"))
+            update(args, { doc: doc, win: doc.defaultView });
+        else {
             args.tab = tabs.getContentIndex(doc) + 1;
             args.doc = {
                 valueOf: function () doc,
                 toString: function () "tabs.getTab(" + (args.tab - 1) + ").linkedBrowser.contentDocument"
             };
+            args.win = {
+                valueOf: function () doc.defaultView,
+                toString: function () "tabs.getTab(" + (args.tab - 1) + ").linkedBrowser.contentWindow"
+            };
         }
 
         autocommands.trigger(name, args);
@@ -95,26 +104,29 @@ var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
     progressListener: {
         // XXX: function may later be needed to detect a canceled synchronous openURL()
         onStateChange: util.wrapCallback(function onStateChange(webProgress, request, flags, status) {
-            onStateChange.superapply(this, arguments);
-            // STATE_IS_DOCUMENT | STATE_IS_WINDOW is important, because we also
-            // receive statechange events for loading images and other parts of the web page
-            if (flags & (Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | Ci.nsIWebProgressListener.STATE_IS_WINDOW)) {
+            const L = Ci.nsIWebProgressListener;
+
+            if (request)
                 dactyl.applyTriggerObserver("browser.stateChange", arguments);
+
+            if (flags & (L.STATE_IS_DOCUMENT | L.STATE_IS_WINDOW)) {
                 // This fires when the load event is initiated
                 // only thrown for the current tab, not when another tab changes
-                if (flags & Ci.nsIWebProgressListener.STATE_START) {
+                if (flags & L.STATE_START) {
                     while (document.commandDispatcher.focusedWindow == webProgress.DOMWindow
                            && modes.have(modes.INPUT))
                         modes.pop();
 
                 }
-                else if (flags & Ci.nsIWebProgressListener.STATE_STOP) {
+                else if (flags & L.STATE_STOP) {
                     // Workaround for bugs 591425 and 606877, dactyl bug #81
                     config.browser.mCurrentBrowser.collapsed = false;
                     if (!dactyl.focusedElement || dactyl.focusedElement === document.documentElement)
                         dactyl.focusContent();
                 }
             }
+
+            onStateChange.superapply(this, arguments);
         }),
         onSecurityChange: util.wrapCallback(function onSecurityChange(webProgress, request, state) {
             onSecurityChange.superapply(this, arguments);
@@ -136,13 +148,16 @@ var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
 
             let win = webProgress.DOMWindow;
             if (win && uri) {
-                let oldURI = win.document.dactylURI;
-                if (win.document.dactylLoadIdx === webProgress.loadedTransIndex
+                Buffer(win).updateZoom();
+
+                let oldURI = overlay.getData(win.document)["uri"];
+                if (overlay.getData(win.document)["load-idx"] === webProgress.loadedTransIndex
                     || !oldURI || uri.spec.replace(/#.*/, "") !== oldURI.replace(/#.*/, ""))
                     for (let frame in values(buffer.allFrames(win)))
-                        frame.document.dactylFocusAllowed = false;
-                win.document.dactylURI = uri.spec;
-                win.document.dactylLoadIdx = webProgress.loadedTransIndex;
+                        overlay.setData(frame.document, "focus-allowed", false);
+
+                overlay.setData(win.document, "uri", uri.spec);
+                overlay.setData(win.document, "load-idx", webProgress.loadedTransIndex);
             }
 
             // Workaround for bugs 591425 and 606877, dactyl bug #81
@@ -165,7 +180,7 @@ var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
         setOverLink: util.wrapCallback(function setOverLink(link, b) {
             setOverLink.superapply(this, arguments);
             dactyl.triggerObserver("browser.overLink", link);
-        }),
+        })
     }
 }, {
 }, {
@@ -187,22 +202,48 @@ var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
         commands.add(["redr[aw]"],
             "Redraw the screen",
             function () {
-                window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
-                      .redraw();
+                statusline.overLink = null;
                 statusline.updateStatus();
                 commandline.clear();
+                window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
+                      .redraw();
             },
             { argCount: "0" });
     },
     mappings: function initMappings(dactyl, modules, window) {
-        // opening websites
+        let openModes = array.toObject([
+            [dactyl.CURRENT_TAB, ""],
+            [dactyl.NEW_TAB, "tab"],
+            [dactyl.NEW_BACKGROUND_TAB, "background tab"],
+            [dactyl.NEW_WINDOW, "win"]
+        ]);
+
+        function open(mode, args) {
+            if (dactyl.forceTarget in openModes)
+                mode = openModes[dactyl.forceTarget];
+
+            CommandExMode().open(mode + "open " + (args || ""))
+        }
+
+        function decode(uri) util.losslessDecodeURI(uri)
+                                 .replace(/%20(?!(?:%20)*$)/g, " ")
+                                 .replace(RegExp(options["urlseparator"], "g"), encodeURIComponent);
+
         mappings.add([modes.NORMAL],
             ["o"], "Open one or more URLs",
-            function () { CommandExMode().open("open "); });
+            function () { open(""); });
 
         mappings.add([modes.NORMAL], ["O"],
             "Open one or more URLs, based on current location",
-            function () { CommandExMode().open("open " + buffer.uri.spec); });
+            function () { open("", decode(buffer.uri.spec)); });
+
+        mappings.add([modes.NORMAL], ["s"],
+            "Open a search prompt",
+            function () { open("", options["defsearch"] + " "); });
+
+        mappings.add([modes.NORMAL], ["S"],
+            "Open a search prompt for a new tab",
+            function () { open("tab", options["defsearch"] + " "); });
 
         mappings.add([modes.NORMAL], ["t"],
             "Open one or more URLs in a new tab",
@@ -210,32 +251,32 @@ var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase),
 
         mappings.add([modes.NORMAL], ["T"],
             "Open one or more URLs in a new tab, based on current location",
-            function () { CommandExMode().open("tabopen " + buffer.uri.spec); });
+            function () { open("tab", decode(buffer.uri.spec)); });
 
         mappings.add([modes.NORMAL], ["w"],
             "Open one or more URLs in a new window",
-            function () { CommandExMode().open("winopen "); });
+            function () { open("win"); });
 
         mappings.add([modes.NORMAL], ["W"],
             "Open one or more URLs in a new window, based on current location",
-            function () { CommandExMode().open("winopen " + buffer.uri.spec); });
+            function () { open("win", decode(buffer.uri.spec)); });
 
-        mappings.add([modes.NORMAL], ["~"],
+        mappings.add([modes.NORMAL], ["<open-home-directory>", "~"],
             "Open home directory",
             function () { dactyl.open("~"); });
 
-        mappings.add([modes.NORMAL], ["gh"],
+        mappings.add([modes.NORMAL], ["<open-homepage>", "gh"],
             "Open homepage",
-            function () { BrowserHome(); });
+            function () { window.BrowserHome(); });
 
-        mappings.add([modes.NORMAL], ["gH"],
+        mappings.add([modes.NORMAL], ["<tab-open-homepage>", "gH"],
             "Open homepage in a new tab",
             function () {
-                let homepages = gHomeButton.getHomePage();
+                let homepages = window.gHomeButton.getHomePage();
                 dactyl.open(homepages, { from: "homepage", where: dactyl.NEW_TAB });
             });
 
-        mappings.add([modes.MAIN], ["<C-l>"],
+        mappings.add([modes.MAIN], ["<redraw-screen>", "<C-l>"],
             "Redraw the screen",
             function () { ex.redraw(); });
     }