]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/content/mow.js
Import 1.0rc1 supporting Firefox up to 11.*
[dactyl.git] / common / content / mow.js
index 5c7a28c698e9c1a09cc859f194261cde1382f06d..770929dd5fb580df96979c5d50810aa2b7bfb297 100644 (file)
@@ -4,7 +4,7 @@
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
-"use strict";
+/* use strict */
 
 var MOW = Module("mow", {
     init: function init() {
@@ -21,11 +21,9 @@ var MOW = Module("mow", {
             if (modes.have(modes.OUTPUT_MULTILINE)) {
                 this.resize(true);
 
-                if (options["more"] && this.isScrollable(1)) {
+                if (options["more"] && this.canScroll(1))
                     // start the last executed command's output at the top of the screen
-                    let elements = this.document.getElementsByClassName("ex-command-output");
-                    elements[elements.length - 1].scrollIntoView(true);
-                }
+                    DOM(this.document.body.lastElementChild).scrollIntoView(true);
                 else
                     this.body.scrollTop = this.body.scrollHeight;
 
@@ -37,14 +35,14 @@ var MOW = Module("mow", {
         events.listen(window, this, "windowEvents");
 
         modules.mow = this;
-        let fontSize = util.computedStyle(document.documentElement).fontSize;
+        let fontSize = DOM(document.documentElement).style.fontSize;
         styles.system.add("font-size", "dactyl://content/buffer.xhtml",
                           "body { font-size: " + fontSize + "; } \
                            html|html > xul|scrollbar { visibility: collapse !important; }",
                           true);
 
         XML.ignoreWhitespace = true;
-        util.overlayWindow(window, {
+        overlay.overlayWindow(window, {
             objects: {
                 eventTarget: this
             },
@@ -67,7 +65,7 @@ var MOW = Module("mow", {
                         </menupopup>
                     </popupset>
                 </window>
-                <vbox id={config.commandContainer}>
+                <vbox id={config.ids.commandContainer}>
                     <vbox class="dactyl-container" id="dactyl-multiline-output-container" hidden="false" collapsed="true">
                         <iframe id="dactyl-multiline-output" src="dactyl://content/buffer.xhtml"
                                 flex="1" hidden="false" collapsed="false" contextmenu="dactyl-contextmenu"
@@ -81,9 +79,9 @@ var MOW = Module("mow", {
     __noSuchMethod__: function (meth, args) Buffer[meth].apply(Buffer, [this.body].concat(args)),
 
     get widget() this.widgets.multilineOutput,
-    widgets: Class.memoize(function widgets() commandline.widgets),
+    widgets: Class.Memoize(function widgets() commandline.widgets),
 
-    body: Class.memoize(function body() this.widget.contentDocument.documentElement),
+    body: Class.Memoize(function body() this.widget.contentDocument.documentElement),
     get document() this.widget.contentDocument,
     get window() this.widget.contentWindow,
 
@@ -94,7 +92,7 @@ var MOW = Module("mow", {
      * @param {string} highlightGroup
      */
     echo: function echo(data, highlightGroup, silent) {
-        let body = this.document.body;
+        let body = DOM(this.document.body);
 
         this.widgets.message = null;
         if (!commandline.commandVisible)
@@ -103,12 +101,15 @@ var MOW = Module("mow", {
         if (modes.main != modes.OUTPUT_MULTILINE) {
             modes.push(modes.OUTPUT_MULTILINE, null, {
                 onKeyPress: this.closure.onKeyPress,
+
                 leave: this.closure(function leave(stack) {
                     if (stack.pop)
                         for (let message in values(this.messages))
                             if (message.leave)
                                 message.leave(stack);
-                })
+                }),
+
+                window: this.window
             });
             this.messages = [];
         }
@@ -119,14 +120,16 @@ var MOW = Module("mow", {
         // after interpolated data.
         XML.ignoreWhitespace = XML.prettyPrinting = false;
 
+        highlightGroup = "CommandOutput " + (highlightGroup || "");
+
         if (isObject(data) && !isinstance(data, _)) {
             this.lastOutput = null;
 
-            var output = util.xmlToDom(<div class="ex-command-output" style="white-space: nowrap" highlight={highlightGroup}/>,
-                                       this.document);
+            var output = DOM(<div style="white-space: nowrap" highlight={highlightGroup}/>,
+                             this.document);
             data.document = this.document;
             try {
-                output.appendChild(data.message);
+                output.append(data.message);
             }
             catch (e) {
                 util.reportError(e);
@@ -135,24 +138,24 @@ var MOW = Module("mow", {
             this.messages.push(data);
         }
         else {
-            let style = isString(data) ? "pre" : "nowrap";
-            this.lastOutput = <div class="ex-command-output" style={"white-space: " + style} highlight={highlightGroup}>{data}</div>;
+            let style = isString(data) ? "pre-wrap" : "nowrap";
+            this.lastOutput = <div style={"white-space: " + style} highlight={highlightGroup}>{data}</div>;
 
-            var output = util.xmlToDom(this.lastOutput, this.document);
+            var output = DOM(this.lastOutput, this.document);
         }
 
         // FIXME: need to make sure an open MOW is closed when commands
         //        that don't generate output are executed
         if (!this.visible) {
             this.body.scrollTop = 0;
-            body.textContent = "";
+            body.empty();
         }
 
-        body.appendChild(output);
+        body.append(output);
 
         let str = typeof data !== "xml" && data.message || data;
         if (!silent)
-            dactyl.triggerObserver("echoMultiline", data, highlightGroup, output);
+            dactyl.triggerObserver("echoMultiline", data, highlightGroup, output[0]);
 
         this._timer.tell();
         if (!this.visible)
@@ -170,7 +173,7 @@ var MOW = Module("mow", {
             };
 
             if (event.target instanceof HTMLAnchorElement)
-                switch (events.toString(event)) {
+                switch (DOM.Event.stringify(event)) {
                 case "<LeftMouse>":
                     openLink(dactyl.CURRENT_TAB);
                     break;
@@ -219,7 +222,7 @@ var MOW = Module("mow", {
     onKeyPress: function onKeyPress(eventList) {
         const KILL = false, PASS = true;
 
-        if (options["more"] && mow.isScrollable(1))
+        if (options["more"] && mow.canScroll(1))
             this.updateMorePrompt(false, true);
         else {
             modes.pop();
@@ -241,16 +244,21 @@ var MOW = Module("mow", {
 
         let doc = this.widget.contentDocument;
 
-        let availableHeight = config.outputHeight;
+        let trim = this.spaceNeeded;
+        let availableHeight = config.outputHeight - trim;
         if (this.visible)
             availableHeight += parseFloat(this.widgets.mowContainer.height || 0);
         availableHeight -= extra || 0;
 
         doc.body.style.minWidth = this.widgets.commandbar.commandline.scrollWidth + "px";
-        this.widgets.mowContainer.height = Math.min(doc.body.clientHeight, availableHeight) + "px";
-        this.timeout(function ()
-            this.widgets.mowContainer.height = Math.min(doc.body.clientHeight, availableHeight) + "px",
-            0);
+
+        function adjust() {
+            let wantedHeight = doc.body.clientHeight;
+            this.widgets.mowContainer.height = Math.min(wantedHeight, availableHeight) + "px",
+            this.wantedHeight = Math.max(0, wantedHeight - availableHeight);
+        }
+        adjust.call(this);
+        this.timeout(adjust);
 
         doc.body.style.minWidth = "";
 
@@ -258,9 +266,10 @@ var MOW = Module("mow", {
     },
 
     get spaceNeeded() {
-        let rect = this.widgets.commandbar.commandline.getBoundingClientRect();
-        let offset = rect.bottom - window.innerHeight;
-        return Math.max(0, offset);
+        if (DOM("#dactyl-bell", document).isVisible)
+            return 0;
+        return Math.max(0, DOM("#" + config.ids.commandContainer, document).rect.bottom
+                            - window.innerHeight);
     },
 
     /**
@@ -279,7 +288,7 @@ var MOW = Module("mow", {
 
         if (showHelp)
             this.widgets.message = ["MoreMsg", _("mow.moreHelp")];
-        else if (force || (options["more"] && Buffer.isScrollable(elem, 1)))
+        else if (force || (options["more"] && Buffer.canScroll(elem, 1)))
             this.widgets.message = ["MoreMsg", _("mow.more")];
         else
             this.widgets.message = ["Question", _("mow.continue")];
@@ -294,7 +303,7 @@ var MOW = Module("mow", {
             if (!value && elem && elem.contentWindow == document.commandDispatcher.focusedWindow) {
 
                 let focused = content.document.activeElement;
-                if (Events.isInputElement(focused))
+                if (focused && Events.isInputElement(focused))
                     focused.blur();
 
                 document.commandDispatcher.focusedWindow = content;
@@ -303,6 +312,12 @@ var MOW = Module("mow", {
     })
 }, {
 }, {
+    modes: function initModes() {
+        modes.addMode("OUTPUT_MULTILINE", {
+            description: "Active when the multi-line output buffer is open",
+            bases: [modes.NORMAL]
+        });
+    },
     mappings: function initMappings() {
         const PASS = true;
         const DROP = false;
@@ -315,6 +330,11 @@ var MOW = Module("mow", {
                 mow.echo(mow.lastOutput, "Normal");
             });
 
+        mappings.add([modes.OUTPUT_MULTILINE],
+            ["<Esc>", "<C-[>"],
+            "Return to the previous mode",
+            function () { modes.pop(null, { fromEscape: true }); });
+
         let bind = function bind(keys, description, action, test, default_) {
             mappings.add([modes.OUTPUT_MULTILINE],
                 keys, description,
@@ -341,36 +361,36 @@ var MOW = Module("mow", {
 
         bind(["j", "<C-e>", "<Down>"], "Scroll down one line",
              function ({ count }) { mow.scrollVertical("lines", 1 * (count || 1)); },
-             function () mow.isScrollable(1), BEEP);
+             function () mow.canScroll(1), BEEP);
 
         bind(["k", "<C-y>", "<Up>"], "Scroll up one line",
              function ({ count }) { mow.scrollVertical("lines", -1 * (count || 1)); },
-             function () mow.isScrollable(-1), BEEP);
+             function () mow.canScroll(-1), BEEP);
 
         bind(["<C-j>", "<C-m>", "<Return>"], "Scroll down one line, exit on last line",
              function ({ count }) { mow.scrollVertical("lines", 1 * (count || 1)); },
-             function () mow.isScrollable(1), DROP);
+             function () mow.canScroll(1), DROP);
 
         // half page down
         bind(["<C-d>"], "Scroll down half a page",
              function ({ count }) { mow.scrollVertical("pages", .5 * (count || 1)); },
-             function () mow.isScrollable(1), BEEP);
+             function () mow.canScroll(1), BEEP);
 
         bind(["<C-f>", "<PageDown>"], "Scroll down one page",
              function ({ count }) { mow.scrollVertical("pages", 1 * (count || 1)); },
-             function () mow.isScrollable(1), BEEP);
+             function () mow.canScroll(1), BEEP);
 
         bind(["<Space>"], "Scroll down one page",
              function ({ count }) { mow.scrollVertical("pages", 1 * (count || 1)); },
-             function () mow.isScrollable(1), DROP);
+             function () mow.canScroll(1), DROP);
 
         bind(["<C-u>"], "Scroll up half a page",
              function ({ count }) { mow.scrollVertical("pages", -.5 * (count || 1)); },
-             function () mow.isScrollable(-1), BEEP);
+             function () mow.canScroll(-1), BEEP);
 
         bind(["<C-b>", "<PageUp>"], "Scroll up half a page",
              function ({ count }) { mow.scrollVertical("pages", -1 * (count || 1)); },
-             function () mow.isScrollable(-1), BEEP);
+             function () mow.canScroll(-1), BEEP);
 
         bind(["gg"], "Scroll to the beginning of output",
              function () { mow.scrollToPercent(null, 0); });