mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2025-12-21 13:07:59 +01:00
Decruftify crufty old gunk, promisify stuff.
This commit is contained in:
@@ -373,7 +373,7 @@ var Editor = Module("editor", XPCOM(Ci.nsIEditActionListener, ModuleBase), {
|
|||||||
|
|
||||||
dactyl.assert(args.length >= 1, _("option.notSet", "editor"));
|
dactyl.assert(args.length >= 1, _("option.notSet", "editor"));
|
||||||
|
|
||||||
io.run(args.shift(), args, blocking);
|
return io.run(args.shift(), args, blocking);
|
||||||
},
|
},
|
||||||
|
|
||||||
// TODO: clean up with 2 functions for textboxes and currentEditor?
|
// TODO: clean up with 2 functions for textboxes and currentEditor?
|
||||||
|
|||||||
@@ -807,8 +807,7 @@ var Buffer = Module("Buffer", {
|
|||||||
/**
|
/**
|
||||||
* Saves the contents of a URI to disk.
|
* Saves the contents of a URI to disk.
|
||||||
*
|
*
|
||||||
* @param {nsIURI} uri The URI to save
|
* @returns {Promise}
|
||||||
* @param {nsIFile} file The file into which to write the result.
|
|
||||||
*/
|
*/
|
||||||
saveURI: function saveURI(params) {
|
saveURI: function saveURI(params) {
|
||||||
if (params instanceof Ci.nsIURI)
|
if (params instanceof Ci.nsIURI)
|
||||||
@@ -816,42 +815,34 @@ var Buffer = Module("Buffer", {
|
|||||||
params = { uri: arguments[0], file: arguments[1],
|
params = { uri: arguments[0], file: arguments[1],
|
||||||
callback: arguments[2], self: arguments[3] };
|
callback: arguments[2], self: arguments[3] };
|
||||||
|
|
||||||
var persist = services.Persist();
|
let promise = new CancelablePromise(promises.task(function* (resolve, reject, canceled) {
|
||||||
persist.persistFlags = persist.PERSIST_FLAGS_FROM_CACHE
|
if (params.isPrivate === undefined) {
|
||||||
| persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
|
let privacy = sanitizer.getContext(params.context || this.win);
|
||||||
|
params.isPrivate = privacy.usePrivateBrowsing;
|
||||||
|
}
|
||||||
|
|
||||||
let window = this.topWindow;
|
let download = Downloads.createDownload({
|
||||||
let privacy = sanitizer.getContext(params.context || this.win);
|
source: {
|
||||||
let file = File(params.file);
|
url: params.uri.spec,
|
||||||
if (!file.exists())
|
isPrivate: params.isPrivate,
|
||||||
file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o666);
|
referrer: params.referrer,
|
||||||
|
},
|
||||||
let downloadListener = new window.DownloadListener(window,
|
target: params.file,
|
||||||
services.Transfer(params.uri, file.URI, "", null, null, null,
|
|
||||||
persist, privacy && privacy.usePrivateBrowsing));
|
|
||||||
|
|
||||||
var { callback, self } = params;
|
|
||||||
if (callback)
|
|
||||||
persist.progressListener = update(Object.create(downloadListener), {
|
|
||||||
onStateChange: util.wrapCallback(function onStateChange(progress, request, flags, status) {
|
|
||||||
if (callback && (flags & Ci.nsIWebProgressListener.STATE_STOP) && status == 0)
|
|
||||||
util.trapErrors(callback, self, params.uri, file.file,
|
|
||||||
progress, request, flags, status);
|
|
||||||
|
|
||||||
return onStateChange.superapply(this, arguments);
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
else
|
canceled.then(download.cancel);
|
||||||
persist.progressListener = downloadListener;
|
|
||||||
|
|
||||||
if (persist.saveURI.length <= 7)
|
let list = yield Downloads.getList(Downloads.ALL);
|
||||||
persist.saveURI(params.uri, null, null, null, null,
|
list.add(download);
|
||||||
file.file, privacy);
|
|
||||||
else
|
return download.start();
|
||||||
// Let's add an extra null to the middle of the arguments
|
}));
|
||||||
// list, because why not.
|
|
||||||
persist.saveURI(params.uri, null, null, null, null, null,
|
// Deprecated.
|
||||||
file.file, privacy);
|
if (params.callback)
|
||||||
|
promise.then(() => {
|
||||||
|
params.callback.call(params.self, params.uri, params.file);
|
||||||
|
});
|
||||||
|
return promise;
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1235,25 +1226,22 @@ var Buffer = Module("Buffer", {
|
|||||||
* immediately.
|
* immediately.
|
||||||
*
|
*
|
||||||
* @param {Document} doc The document to view.
|
* @param {Document} doc The document to view.
|
||||||
* @param {function|object} callback If a function, the callback to be
|
* @returns {Promise}
|
||||||
* called with two arguments: the nsIFile of the file, and temp, a
|
|
||||||
* boolean which is true if the file is temporary. Otherwise, an object
|
|
||||||
* with line and column properties used to determine where to open the
|
|
||||||
* source.
|
|
||||||
* @optional
|
|
||||||
*/
|
*/
|
||||||
viewSourceExternally: Class("viewSourceExternally",
|
viewSourceExternally: function viewSourceExternally(doc, loc) {
|
||||||
XPCOM([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference]), {
|
return this.fetchSourceExternally(doc).then(([file, isTemp]) => {
|
||||||
init: function init(doc, callback) {
|
let { editor } = overlay.activeModules;
|
||||||
this.callback = callable(callback) ? callback :
|
|
||||||
function (file, temp) {
|
|
||||||
let { editor } = overlay.activeModules;
|
|
||||||
|
|
||||||
editor.editFileExternally(update({ file: file.path }, callback || {}),
|
editor.editFileExternally(update({ file: file.path }, loc))
|
||||||
function () { temp && file.remove(false); });
|
.then(() => {
|
||||||
return true;
|
if (isTemp)
|
||||||
};
|
file.remove(false);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
fetchSourceExternally: function fetchSourceExternally(doc) {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
if (isString(doc)) {
|
if (isString(doc)) {
|
||||||
var privacyContext = null;
|
var privacyContext = null;
|
||||||
var uri = util.newURI(doc);
|
var uri = util.newURI(doc);
|
||||||
@@ -1270,41 +1258,28 @@ var Buffer = Module("Buffer", {
|
|||||||
}
|
}
|
||||||
catch (e) {}
|
catch (e) {}
|
||||||
|
|
||||||
if (!isString(doc))
|
if (!isString(doc)) {
|
||||||
return io.withTempFiles(function (temp) {
|
let file = io.createTempFile();
|
||||||
let encoder = services.HtmlEncoder();
|
let encoder = services.HtmlEncoder();
|
||||||
encoder.init(doc, "text/unicode", encoder.OutputRaw|encoder.OutputPreformatted);
|
encoder.init(doc, "text/unicode", encoder.OutputRaw|encoder.OutputPreformatted);
|
||||||
temp.write(encoder.encodeToString(), ">");
|
|
||||||
return this.callback(temp, true);
|
|
||||||
}, this, true, ext);
|
|
||||||
|
|
||||||
let file = util.getFile(uri);
|
OS.File.writeAtomic(file.path, encoder.encodeToString())
|
||||||
if (file)
|
.then(() => { resolve([file, true]) })
|
||||||
this.callback(file, false);
|
.catch(reject);
|
||||||
|
}
|
||||||
else {
|
else {
|
||||||
this.file = io.createTempFile();
|
let file = util.getFile(uri);
|
||||||
var persist = services.Persist();
|
if (file)
|
||||||
persist.persistFlags = persist.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
|
resolve([file, false]);
|
||||||
persist.progressListener = this;
|
else {
|
||||||
persist.saveURI(uri, null, null, null, null, this.file,
|
let file = io.createTempFile();
|
||||||
privacyContext);
|
Downloads.fetch(uri, file.file).then(() => {
|
||||||
}
|
resolve([file, true]);
|
||||||
return null;
|
}).catch(reject);
|
||||||
},
|
|
||||||
|
|
||||||
onStateChange: function onStateChange(progress, request, flags, status) {
|
|
||||||
if ((flags & this.STATE_STOP) && status == 0) {
|
|
||||||
try {
|
|
||||||
var ok = this.callback(this.file, true);
|
|
||||||
}
|
|
||||||
finally {
|
|
||||||
if (ok !== true)
|
|
||||||
this.file.remove(false);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
});
|
||||||
}
|
},
|
||||||
}),
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Increases the zoom level of the current buffer.
|
* Increases the zoom level of the current buffer.
|
||||||
@@ -1956,27 +1931,32 @@ var Buffer = Module("Buffer", {
|
|||||||
let command = commandline.command;
|
let command = commandline.command;
|
||||||
if (filename) {
|
if (filename) {
|
||||||
if (filename[0] == "!")
|
if (filename[0] == "!")
|
||||||
return buffer.viewSourceExternally(buffer.focusedFrame.document,
|
return buffer.fetchSourceExternally(buffer.focusedFrame.document)
|
||||||
function (file) {
|
.then(([file, isTemp]) => {
|
||||||
let output = io.system(filename.substr(1), file);
|
let output = io.system(filename.substr(1), file);
|
||||||
commandline.command = command;
|
if (isTemp)
|
||||||
commandline.commandOutput(["span", { highlight: "CmdOutput" }, output]);
|
file.remove(false);
|
||||||
});
|
|
||||||
|
commandline.command = command;
|
||||||
|
commandline.commandOutput(["span", { highlight: "CmdOutput" }, output]);
|
||||||
|
});
|
||||||
|
|
||||||
if (/^>>/.test(filename)) {
|
if (/^>>/.test(filename)) {
|
||||||
let file = io.File(filename.replace(/^>>\s*/, ""));
|
let file = io.File(filename.replace(/^>>\s*/, ""));
|
||||||
dactyl.assert(args.bang || file.exists() && file.isWritable(),
|
dactyl.assert(args.bang || file.exists() && file.isWritable(),
|
||||||
_("io.notWriteable", JSON.stringify(file.path)));
|
_("io.notWriteable", JSON.stringify(file.path)));
|
||||||
|
|
||||||
return buffer.viewSourceExternally(buffer.focusedFrame.document,
|
return buffer.fetchSourceExternally(buffer.focusedFrame.document)
|
||||||
function (tmpFile) {
|
.then(([tmpFile, isTemp]) => {
|
||||||
try {
|
try {
|
||||||
file.write(tmpFile, ">>");
|
file.write(tmpFile, ">>");
|
||||||
}
|
}
|
||||||
catch (e) {
|
catch (e) {
|
||||||
dactyl.echoerr(_("io.notWriteable", JSON.stringify(file.path)));
|
dactyl.echoerr(_("io.notWriteable", JSON.stringify(file.path)));
|
||||||
}
|
}
|
||||||
});
|
if (isTemp)
|
||||||
|
tmpFile.remove();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let file = io.File(filename);
|
let file = io.File(filename);
|
||||||
|
|||||||
Reference in New Issue
Block a user