/***** BEGIN LICENSE BLOCK ***** {{{ Version: MPL 1.1/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. (c) 2006-2007: Martin Stubenschrott Alternatively, the contents of this file may be used under the terms of either the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the MPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the MPL, the GPL or the LGPL. }}} ***** END LICENSE BLOCK *****/ const vimperator = (function() //{{{ { //////////////////////////////////////////////////////////////////////////////// ////////////////////// PRIVATE SECTION ///////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ // our services var sound_service = Components.classes['@mozilla.org/sound;1'] .getService(Components.interfaces.nsISound); var console_service = Components.classes['@mozilla.org/consoleservice;1'] .getService(Components.interfaces.nsIConsoleService); var environment_service = Components.classes["@mozilla.org/process/environment;1"] .getService(Components.interfaces.nsIEnvironment); var callbacks = []; /////////////////////////////////////////////////////////////////////////////}}} ////////////////////// PUBLIC SECTION ////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////{{{ return { get mode() { return vimperator.modes.main; }, set mode(value) { vimperator.modes.main = value; }, // Global contants CURRENT_TAB: 1, NEW_TAB: 2, NEW_BACKGROUND_TAB: 3, NEW_WINDOW: 4, // ###VERSION### and ###DATE### are replaced by the Makefile version: "###VERSION### (created: ###DATE###)", input: { buffer: "", // partial command storage pendingMotionMap: null, // e.g. "d{motion}" if we wait for a motion of the "d" command pendingArgMap: null, // pending map storage for commands like m{a-z} count: -1 // parsed count from the input buffer }, // @param type can be: // "submit": when the user pressed enter in the command line // "change" // "cancel" // "complete" // TODO: "zoom": if the zoom value of the current buffer changed registerCallback: function(type, mode, func) { // TODO: check if callback is already registered callbacks.push([type, mode, func]); }, triggerCallback: function(type, mode, data) { // dump("type: " + type + " mode: " + mode + "data: " + data + "\n"); for (var i in callbacks) { var [thistype, thismode, thisfunc] = callbacks[i]; if (mode == thismode && type == thistype) return thisfunc.call(this, data); } return false; }, beep: function() { if (vimperator.options["visualbell"]) { // flash the visual bell var popup = document.getElementById("vimperator-visualbell"); var win = getBrowser().mPanelContainer; var box = document.getBoxObjectFor(win); popup.height = box.height; popup.width = box.width; popup.openPopup(win, "overlap", 0, 0, false, false) setTimeout(function() { popup.hidePopup(); }, 50); } else { sound_service.beep(); } }, copyToClipboard: function(str) { var clipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"] .getService(Components.interfaces.nsIClipboardHelper); clipboardHelper.copyString(str); }, execute: function(str, modifiers) { // skip comments and blank lines if (/^\s*("|$)/.test(str)) return; if (!modifiers) modifiers = {}; var [count, cmd, special, args] = vimperator.commands.parseCommand(str.replace(/^'(.*)'$/, '$1')); var command = vimperator.commands.get(cmd); if (command === null) { vimperator.echoerr("E492: Not an editor command: " + str); vimperator.focusContent(); return; } // TODO: need to perform this test? -- djk if (command.action === null) { vimperator.echoerr("E666: Internal error: command.action === null"); return; } // valid command, call it: command.execute(args, special, count, modifiers); }, // after pressing Escape, put focus on a non-input field of the browser document // if clearFocusedElement, also blur a focused link focusContent: function(clearFocusedElement) { var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]. getService(Components.interfaces.nsIWindowWatcher); if (window == ww.activeWindow && document.commandDispatcher.focusedElement && clearFocusedElement) document.commandDispatcher.focusedElement.blur(); content.focus(); }, // partial sixth level expression evaluation eval: function(string) { string = string.toString().replace(/^\s*/, "").replace(/\s*$/, ""); var match = string.match(/^&(\w+)/); if (match) { var opt = this.options.get(match[1]); if (!opt) { this.echoerr("E113: Unknown option: " + match[1]); return; } var type = opt.type; var value = opt.getter(); if (type != "boolean" && type != "number") value = value.toString(); return value; } // String else if (match = string.match(/^(['"])([^\1]*?[^\\]?)\1/)) { if (match) return match[2].toString(); else { this.echoerr("E115: Missing quote: " + string); return; } } // Number else if (match = string.match(/^(\d+)$/)) { return parseInt(match[1]); } var reference = this.variableReference(string); if (!reference[0]) this.echoerr("E121: Undefined variable: " + string); else return reference[0][reference[1]]; return; }, variableReference: function(string) { if (!string) return [null, null, null]; if (match = string.match(/^([bwtglsv]):(\w+)/)) // Variable { // Other variables should be implemented if (match[1] == "g") { if (match[2] in this.globalVariables) return [this.globalVariables, match[2], match[1]]; else return [null, match[2], match[1]]; } } else // Global variable { if (string in this.globalVariables) return [this.globalVariables, string, "g"]; else return [null, string, "g"]; } }, // TODO: move to vimp.util.? --mst // if color = true it uses HTML markup to color certain items objectToString: function(object, color) { if (object === null) return "null"; if (typeof object != "object") return false; var string = ""; var obj = ""; try { // for window.JSON obj = object.toString(); } catch (e) { obj = "<Object>"; } if (color) string += "" + obj + "::\n"; else string += obj + "::\n"; try // window.content often does not want to be queried with "var i in object" { for (var i in object) { var value; try { value = object[i]; } catch (e) { value = "<no value>"; } if (color) { value = vimperator.util.colorize(value, true); string += "" + i + ": " + value + "\n"; } else string += i + ": " + value + "\n"; } } catch (e) { }; return string; }, // logs a message to the javascript error console // if msg is an object, it is beautified log: function(msg, level) { //if (vimperator.options.getPref("verbose") >= level) // FIXME: hangs vimperator, probably timing issue --mst if (typeof msg == "object") msg = this.objectToString(msg, false); console_service.logStringMessage('vimperator: ' + msg); }, // open one or more URLs // // @param urls: either a string or an array of urls // The array can look like this: // ["url1", "url2", "url3", ...] or: // [["url1", postdata1], ["url2", postdata2], ...] // @param where: if ommited, CURRENT_TAB is assumed // @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) { // convert the string to an array of converted URLs // -> see String.prototype.toURLArray for more details if (typeof urls == "string") urls = urls.toURLArray(); if (urls.length == 0) return false; if (!where) where = vimperator.CURRENT_TAB; var url = typeof urls[0] == "string" ? urls[0] : urls[0][0]; var postdata = typeof urls[0] == "string" ? null : urls[0][1]; var whichwindow = window; // decide where to load the first url switch (where) { case vimperator.CURRENT_TAB: window.loadURI(url, null, postdata); // getBrowser.loadURI() did not work with postdata in my quick experiments --mst break; case vimperator.NEW_TAB: var firsttab = getBrowser().addTab(url, null, null, postdata); getBrowser().selectedTab = firsttab; break; case vimperator.NEW_BACKGROUND_TAB: getBrowser().addTab(url, null, null, postdata); break; case vimperator.NEW_WINDOW: window.open(); var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] .getService(Components.interfaces.nsIWindowMediator); whichwindow = wm.getMostRecentWindow("navigator:browser"); whichwindow.loadURI(url, null, postdata) break; default: vimperator.echoerr("Exxx: Invalid 'where' directive in vimperator.open(...)"); return false; } // all other URLs are always loaded in background for (var i = 1; i < urls.length; i++) { url = typeof urls[i] == "string" ? urls[i] : urls[i][0]; postdata = typeof urls[i] == "string" ? null : urls[i][1]; whichwindow.getBrowser().addTab(url, null, null, postdata); } // TODO: register callbacks return true; }, // quit vimperator, no matter how many tabs/windows are open quit: function(save_session) { if (save_session) vimperator.options.setFirefoxPref("browser.startup.page", 3); // start with saved session else vimperator.options.setFirefoxPref("browser.startup.page", 1); // start with default homepage session goQuitApplication(); }, restart: function() { const nsIAppStartup = Components.interfaces.nsIAppStartup; // notify all windows that an application quit has been requested. var os = Components.classes["@mozilla.org/observer-service;1"] .getService(Components.interfaces.nsIObserverService); var cancelQuit = Components.classes["@mozilla.org/supports-PRBool;1"] .createInstance(Components.interfaces.nsISupportsPRBool); os.notifyObservers(cancelQuit, "quit-application-requested", null); // something aborted the quit process. if (cancelQuit.data) return; // notify all windows that an application quit has been granted. os.notifyObservers(null, "quit-application-granted", null); // enumerate all windows and call shutdown handlers var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"] .getService(Components.interfaces.nsIWindowMediator); var windows = wm.getEnumerator(null); while (windows.hasMoreElements()) { var win = windows.getNext(); if (("tryToClose" in win) && !win.tryToClose()) return; } Components.classes["@mozilla.org/toolkit/app-startup;1"].getService(nsIAppStartup) .quit(nsIAppStartup.eRestart | nsIAppStartup.eAttemptQuit); }, run: function(program, args, blocking) { var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile); const WINDOWS = navigator.platform == "Win32"; if (!args) args = []; if (typeof(blocking) != "boolean") blocking = false; try { file.initWithPath(program); } catch (e) { var dirs = environment_service.get("PATH").split(WINDOWS ? ";" : ":"); for (var i = 0; i < dirs.length; i++) { var path = dirs[i] + (WINDOWS ? "\\" : "/") + program; try { file.initWithPath(path); if (file.exists()) break; } catch (e) { } } } if (!file.exists()) { vimperator.echoerr("command not found: " + program); return -1; } var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess); process.init(file); var ec = process.run(blocking, args, args.length); return ec; }, // when https://bugzilla.mozilla.org/show_bug.cgi?id=68702 is fixed // is fixed, should use that instead of a tmpfile // TODO: add shell/shellcmdflag options to replace "sh" and "-c" system: function (str, input) { const WINDOWS = navigator.platform == "Win32"; // FIXME: duplicated everywhere var fileout = vimperator.io.createTempFile(); if (!fileout) return ""; if (WINDOWS) var command = str + " > " + fileout.path; else var command = str + " > \"" + fileout.path.replace('"', '\\"') + "\""; var filein = null; if (input) { filein = vimperator.io.createTempFile(); vimperator.io.writeFile(filein, input); command += " < \"" + filein.path.replace('"', '\\"') + "\""; } var res; if (WINDOWS) res = this.run("cmd.exe", ["/C", command], true); else res = this.run("sh", ["-c", command], true); var output = vimperator.io.readFile(fileout); fileout.remove(false); if (filein) filein.remove(false); // if there is only one \n at the end, chop it off if (output && output.indexOf("\n") == output.length-1) output = output.substr(0, output.length-1); return output; }, // files which end in .js are sourced as pure javascript files, // no need (actually forbidden) to add: js <