From b4dfb2540b4e279315a43f70ddc03cd9e29de1a7 Mon Sep 17 00:00:00 2001 From: Martin Stubenschrott Date: Sun, 29 Jul 2007 03:48:29 +0000 Subject: [PATCH] inputMultiline method finished (some display bug may remain) :js < 2007-07-02: * version 0.5 + * :javascript <) @@ -36,7 +34,6 @@ FEATURES: 6 macros (qq) 6 gf = view source? 6 make a real one-tab-mode, divert _all_ other targets, possible by setting a firefox option (set popup=0-3) -6 Shift-Insert in textboxes pastes selection contents 6 page info support (ctrl-g, g) 5 Use arrow keys in preview window, and ctrl-w+j/k to switch to from preview window 5 Sort :open completion by date? (use 'wildsort') @@ -46,6 +43,7 @@ FEATURES: 3 Splitting Windows with [:sp :vsp ctrl-w,s ctrl-w,v] and closing with [ctrl-w,q], moving with [ctrl-w,w or tab] have a look into the split browser extension 3 :set should also set about:config options (with autocomplete) +- many other ideas are listed in the wiki RANDOM IDEAS: * numbered tabs diff --git a/chrome/content/vimperator/commands.js b/chrome/content/vimperator/commands.js index a008ea98..74d6b6bd 100644 --- a/chrome/content/vimperator/commands.js +++ b/chrome/content/vimperator/commands.js @@ -488,14 +488,29 @@ function Commands() //{{{ if (special) // open javascript console vimperator.open("chrome://global/content/console.xul", vimperator.NEW_TAB); else - try + { + // check for a heredoc + var matches = args.match(/(.*)<<\s*([^\s]+)$/); + if (matches && matches[2]) { - eval(args); + vimperator.commandline.inputMultiline(new RegExp("^" + matches[2] + "$", "m"), + function(code) { + try { eval(matches[1] + "\n" + code); } + catch (e) { vimperator.echoerr(e.name + ": " + e.message); } + }); } - catch (e) + else // single line javascript code { - vimperator.echoerr(e.name + ": " + e.message); + try + { + eval(args); + } + catch (e) + { + vimperator.echoerr(e.name + ": " + e.message); + } } + } }, { usage: ["javas[cript] {cmd}", "javascript <<{endpattern}\\n{script}\\n{endpattern}", "javascript[!]"], // \\n is changed to
in the help.js code diff --git a/chrome/content/vimperator/ui.js b/chrome/content/vimperator/ui.js index a97dca87..e24d164c 100644 --- a/chrome/content/vimperator/ui.js +++ b/chrome/content/vimperator/ui.js @@ -102,9 +102,11 @@ function CommandLine() //{{{ var prompt_widget = document.getElementById('vimperator-commandline-prompt'); // The command bar which contains the current command var command_widget = document.getElementById('vimperator-commandline-command'); - // The widget used for multiline in-/output - var multiline_widget = document.getElementById("vimperator-multiline"); - multiline_widget.contentDocument.body.setAttribute("style", "margin: 0px; font-family: -moz-fixed;"); // get rid of the default border + // The widget used for multiline output + var multiline_output_widget = document.getElementById("vimperator-multiline-output"); + multiline_output_widget.contentDocument.body.setAttribute("style", "margin: 0px; font-family: -moz-fixed;"); // get rid of the default border + // The widget used for multiline output + var multiline_input_widget = document.getElementById("vimperator-multiline-input"); // we need to save the mode which were in before opening the command line // this is then used if we focus the command line again without the "official" @@ -119,6 +121,10 @@ function CommandLine() //{{{ // and before the blur() event gets fired var echo_allowed = false; + // save the arguments for the inputMultiline method which are needed in the event handler + var multiline_regexp = null; + var multiline_callback = null; + // load the commandline history var hist = Options.getPref("commandline_history", ""); history = hist.split("\n"); @@ -167,20 +173,22 @@ function CommandLine() //{{{ function setMultiline(cmd) { // TODO: we should retain any previous command output like Vim - if (!multiline_widget.collapsed) - multiline_widget.collapsed = true; + if (!multiline_output_widget.collapsed) + multiline_output_widget.collapsed = true; + + multiline_input_widget.collapsed = true; cmd = cmd.replace(/\n|\\n/g, "
") + "
Press ENTER or type command to continue"; - multiline_widget.contentDocument.body.innerHTML = cmd; + multiline_output_widget.contentDocument.body.innerHTML = cmd; // TODO: resize upon a window resize var available_height = getBrowser().mPanelContainer.boxObject.height; - var content_height = multiline_widget.contentDocument.height; + var content_height = multiline_output_widget.contentDocument.height; var height = content_height < available_height ? content_height : available_height; - multiline_widget.style.height = height + "px"; - multiline_widget.collapsed = false; - multiline_widget.contentWindow.scrollTo(0, content_height); // scroll to the end when 'nomore' is set + multiline_output_widget.style.height = height + "px"; + multiline_output_widget.collapsed = false; + multiline_output_widget.contentWindow.scrollTo(0, content_height); // scroll to the end when 'nomore' is set } function addToHistory(str) @@ -234,6 +242,9 @@ function CommandLine() //{{{ if (!echo_allowed && focused && focused == command_widget.inputField) return false; + if (typeof str != "string") + str = ""; + setNormalStyle(); if (flags || str.indexOf("\n") > -1 || str.indexOf("\\n") > -1 || str.indexOf("
") > -1 || str.indexOf("
") > -1) { @@ -241,7 +252,8 @@ function CommandLine() //{{{ } else { - multiline_widget.collapsed = true; + multiline_output_widget.collapsed = true; + multiline_input_widget.collapsed = true; setPrompt(""); setCommand(str); } @@ -275,17 +287,23 @@ function CommandLine() //{{{ }; // reads a multi line input and returns the string once the last line matches - // param until_regex - this.readMultiline = function(until_regex, callback_func) + // @param until_regexp + this.inputMultiline = function(until_regexp, callback_func) { // save the mode, because we need to restore it on blur() // [old_mode, old_extended_mode] = vimperator.getMode(); // vimperator.setMode(vimperator.modes.COMMAND_LINE, vimperator.modes.READ_MULTLINE, true); - multiline_widget.collapsed = false; - multiline_widget.contentDocument.body.innerHTML = ""; - multiline_widget.contentDocument.designMode = "on"; - multiline_widget.contentWindow.focus(); // FIXME: does not work + // save the arguments, they are needed in the event handler onEvent + multiline_regexp = until_regexp; + multiline_callback = callback_func; + + multiline_input_widget.collapsed = false; + multiline_input_widget.value = ""; + setTimeout(function() { + multiline_input_widget.focus(); + }, 10); + }; this.clear = function() @@ -355,20 +373,6 @@ function CommandLine() //{{{ /* user pressed ENTER to carry out a command */ if (key == "" || key == "" || key == "") { - // FIXME: move to execute() in commands.js - // var end = false; - // try { - // [prev_match, heredoc, end] = multiliner(command, prev_match, heredoc); - // } catch(e) { - // logObject(e); - // echoerr(e.name + ": " + e.message); - // prev_match = new Array(5); - // heredoc = ''; - // return; - // } - // if (!end) - // command_line.value = ""; - echo_allowed = true; addToHistory(command); var res = vimperator.triggerCallback("submit", command); @@ -376,6 +380,7 @@ function CommandLine() //{{{ echo_allowed = false; return res; } + /* user pressed ESCAPE to cancel this prompt */ else if (key == "" || key == "" || key == "") { @@ -567,6 +572,24 @@ function CommandLine() //{{{ } } + this.onMultilineEvent = function(event) + { + // for now we just receive keypress events + + var key = event.toString(); + if (key == "" || key == "" || key == "") + { + //var lines = multiline_input_widget.value.substr(0, multiline_input_widget.selectionStart).split(/\n/); + var text = multiline_input_widget.value.substr(0, multiline_input_widget.selectionStart); + if (text.match(multiline_regexp)) + { + text = text.replace(multiline_regexp, ""); + multiline_callback.call(this, text); + multiline_input_widget.collapsed = true; + } + } + } + // it would be better if we had a destructor in javascript ... this.destroy = function() { diff --git a/chrome/content/vimperator/vimperator.js b/chrome/content/vimperator/vimperator.js index c4877f73..bbdf02c7 100644 --- a/chrome/content/vimperator/vimperator.js +++ b/chrome/content/vimperator/vimperator.js @@ -126,7 +126,7 @@ const vimperator = (function() //{{{ count: -1 // parsed count from the input buffer }, - /** + /** TODO: for now, these callbacks are mostly for the command line, move there? * @param type Can be: * "submit": when the user pressed enter in the command line * "change" @@ -215,7 +215,7 @@ const vimperator = (function() //{{{ if (window == ww.activeWindow && document.commandDispatcher.focusedElement) document.commandDispatcher.focusedElement.blur(); - content.focus(); + content.focus(); // FIXME: shouldn't be window.document.content? }, /** @@ -253,7 +253,7 @@ const vimperator = (function() //{{{ // // @param urls: either a string or an array of urls // @param where: if ommited, CURRENT_TAB is assumed - // @param callback: not implmented, will be allowed to specifiy a callback function + // @param callback: not implemented, will be allowed to specify a callback function // which is called, when the page finished loading // @returns true when load was initiated, or false on error open: function(urls, where, callback) @@ -309,7 +309,6 @@ const vimperator = (function() //{{{ restart: function() { - // if (!arguments[1]) return; const nsIAppStartup = Components.interfaces.nsIAppStartup; // Notify all windows that an application quit has been requested. @@ -466,6 +465,7 @@ const vimperator = (function() //{{{ var s = fd.read(); fd.close(); + // TODO: simplify and get rid of multiliner (look at "javascript" in commands.js) var prev_match = new Array(5); var heredoc = ''; var end = false; @@ -521,11 +521,13 @@ const vimperator = (function() //{{{ vimperator.echo = vimperator.commandline.echo; vimperator.echoerr = vimperator.commandline.echoErr; - // XXX: move elsewhere + // TODO: move elsewhere vimperator.registerCallback("submit", vimperator.modes.EX, function(command) { /*vimperator.*/execute(command); } ); vimperator.registerCallback("complete", vimperator.modes.EX, function(str) { return exTabCompletion(str); } ); - // work around firefox popup blocker + //TODO: move most of the following code to Options constructor + + // work around firefox popup blocker popup_allowed_events = Options.getFirefoxPref('dom.popup_allowed_events', 'change click dblclick mouseup reset submit'); if (!popup_allowed_events.match("keypress")) Options.setFirefoxPref('dom.popup_allowed_events', popup_allowed_events + " keypress"); diff --git a/chrome/content/vimperator/vimperator.xul b/chrome/content/vimperator/vimperator.xul index 7ed11cc7..d0424e57 100644 --- a/chrome/content/vimperator/vimperator.xul +++ b/chrome/content/vimperator/vimperator.xul @@ -99,7 +99,11 @@ the terms of any one of the MPL, the GPL or the LGPL. onfocus="vimperator.commandline.onEvent(event);" onblur="vimperator.commandline.onEvent(event);"/> -