diff --git a/common/content/editor.js b/common/content/editor.js index af210936..3681a66d 100644 --- a/common/content/editor.js +++ b/common/content/editor.js @@ -252,7 +252,7 @@ const Editor = Module("editor", { }, // TODO: clean up with 2 functions for textboxes and currentEditor? - editFieldExternally: function (forceEditing) { + editFieldExternally: function editFieldExternally(forceEditing) { if (!options["editor"]) return; @@ -280,72 +280,72 @@ const Editor = Module("editor", { dactyl.assert(editor); text = Array.map(editor.rootElement.childNodes, function (e) util.domToString(e, true)).join(""); } - let oldBg, tmpBg; - try { - let res = io.withTempFiles(function (tmpfile) { - if (textBox) { - textBox.setAttribute("readonly", "true"); - oldBg = textBox.style.backgroundColor; - tmpBg = "yellow"; - textBox.style.backgroundColor = "#bbbbbb"; - } + function cleanup(error) { + if (timer) + timer.cancel(); - if (!tmpfile.write(text)) - throw Error("Input contains characters not valid in the current " + - "file encoding"); + if (error) { + dactyl.reportError(error, true); + tmpBg = "red"; + } + else + dactyl.trapErrors(update, null, true); - let lastUpdate = Date.now(); - function update(force) { - if (force !== true && tmpfile.lastModifiedTime <= lastUpdate) - return; - lastUpdate = Date.now(); + if (tmpfile && tmpfile.exists()) + tmpfile.remove(false); - let val = tmpfile.read(); - if (textBox) - textBox.value = val; - else { - while (editor.rootElement.firstChild) - editor.rootElement.removeChild(editor.rootElement.firstChild); - editor.rootElement.innerHTML = val; - } - } - - let timer = services.Timer(update, 100, services.Timer.TYPE_REPEATING_SLACK); - - try { - this.editFileExternally({ file: tmpfile.path, line: line, column: column }, true); - } - finally { - timer.cancel(); - } - - update(true); - - }, this); - if (res == false) - throw Error("Couldn't create temporary file"); - } - catch (e) { - // Errors are unlikely, and our error messages won't - // likely be any more helpful than that given in the - // exception. - dactyl.reportError(e, true); - tmpBg = "red"; - } - finally { if (textBox) textBox.removeAttribute("readonly"); + + // blink the textbox after returning + if (textBox) { + let colors = [tmpBg, oldBg, tmpBg, oldBg]; + (function next() { + textBox.style.backgroundColor = colors.shift(); + if (colors.length > 0) + util.timeout(next, 100); + })(); + } } - // blink the textbox after returning - if (textBox) { - let colors = [tmpBg, oldBg, tmpBg, oldBg]; - (function next() { - textBox.style.backgroundColor = colors.shift(); - if (colors.length > 0) - util.timeout(next, 100); - })(); + try { + var tmpfile = io.createTempFile(); + if (!tmpfile) + throw Error("Couldn't create temporary file"); + + if (textBox) { + textBox.setAttribute("readonly", "true"); + oldBg = textBox.style.backgroundColor; + tmpBg = "yellow"; + textBox.style.backgroundColor = "#bbbbbb"; + } + + if (!tmpfile.write(text)) + throw Error("Input contains characters not valid in the current " + + "file encoding"); + + let lastUpdate = Date.now(); + function update(force) { + if (force !== true && tmpfile.lastModifiedTime <= lastUpdate) + return; + lastUpdate = Date.now(); + + let val = tmpfile.read(); + if (textBox) + textBox.value = val; + else { + while (editor.rootElement.firstChild) + editor.rootElement.removeChild(editor.rootElement.firstChild); + editor.rootElement.innerHTML = val; + } + } + + var timer = services.Timer(update, 100, services.Timer.TYPE_REPEATING_SLACK); + this.editFileExternally({ file: tmpfile.path, line: line, column: column }, cleanup); + } + catch (e) { + cleanup(e); } }, diff --git a/common/content/io.js b/common/content/io.js index 46464800..55a302a9 100644 --- a/common/content/io.js +++ b/common/content/io.js @@ -200,9 +200,9 @@ const IO = Module("io", { */ createTempFile: function () { let file = services.directory.get("TmpD", Ci.nsIFile); - file.append(config.tempFile); file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, octal(600)); + Cc["@mozilla.org/uriloader/external-helper-app-service;1"] .getService(Ci.nsPIExternalAppLauncher).deleteTemporaryFileOnExit(file); @@ -279,7 +279,7 @@ lookup: function () { if (!process.isRunning) { timer.cancel(); - callback(); + dactyl.trapErrors(blocking); } }, 100, services.Timer.TYPE_REPEATING_SLACK); diff --git a/common/modules/util.jsm b/common/modules/util.jsm index e34b6fba..cd7348db 100644 --- a/common/modules/util.jsm +++ b/common/modules/util.jsm @@ -1230,6 +1230,16 @@ const Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) while (flush === true && mainThread.hasPendingEvents()); }, + yieldable: function yieldable(func) + function magic() { + let gen = func.apply(this, arguments); + (function next() { + try { + util.timeout(next, gen.next()); + } catch(e if e instanceof StopIteration) {}; + })(); + }, + /** * Traps errors in the called function, possibly reporting them. *