]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/modules/io.jsm
Import r6976 from upstream hg supporting Firefox up to 25.*
[dactyl.git] / common / modules / io.jsm
index 9e717d74ed51ebd217ea017e821b017e7a7ef7c7..4a61d67313b74d7e1c3a2092a44ef7ea027c1125 100644 (file)
@@ -1,6 +1,6 @@
 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
 // Copyright (c) 2007-2012 by Doug Kearns <dougkearns@gmail.com>
-// Copyright (c) 2008-2012 Kris Maglione <maglione.k@gmail.com>
+// Copyright (c) 2008-2013 Kris Maglione <maglione.k@gmail.com>
 // Some code based on Venkman
 //
 // This work is licensed for reuse under an MIT license. Details are
@@ -74,8 +74,8 @@ var IO = Module("io", {
          */
         getRuntimeDirectories: function getRuntimeDirectories(name) {
             return modules.options.get("runtimepath").files
-                .map(function (dir) dir.child(name))
-                .filter(function (dir) dir.exists() && dir.isDirectory() && dir.isReadable());
+                .map(dir => dir.child(name))
+                .filter(dir => (dir.exists() && dir.isDirectory() && dir.isReadable()));
         },
 
         // FIXME: multiple paths?
@@ -149,7 +149,7 @@ var IO = Module("io", {
                     let sourceJSM = function sourceJSM() {
                         context = contexts.Module(uri);
                         dactyl.triggerObserver("io.source", context, file, file.lastModifiedTime);
-                    }
+                    };
 
                     if (/\.jsm$/.test(filename))
                         sourceJSM();
@@ -241,11 +241,13 @@ var IO = Module("io", {
     /**
      * Sets the current working directory.
      *
-     * @param {string} newDir The new CWD. This may be a relative or
+     * @param {File|string} newDir The new CWD. This may be a relative or
      *     absolute path and is expanded by {@link #expandPath}.
+     *     @optional
+     *     @default = "~"
      */
-    set cwd(newDir) {
-        newDir = newDir && newDir.path || newDir || "~";
+    set cwd(newDir = "~") {
+        newDir = newDir.path || newDir;
 
         if (newDir == "-") {
             util.assert(this._oldcwd != null, _("io.noPrevDir"));
@@ -266,8 +268,8 @@ var IO = Module("io", {
      */
     File: Class.Memoize(function () let (io = this)
         Class("File", File, {
-            init: function init(path, checkCWD)
-                init.supercall(this, path, (arguments.length < 2 || checkCWD) && io.cwd)
+            init: function init(path, checkCWD=true)
+                init.supercall(this, path, checkCWD && io.cwd)
         })),
 
     /**
@@ -307,23 +309,20 @@ var IO = Module("io", {
     },
 
     /**
-     * Creates a temporary file.
+     * Returns a temporary file.
      *
+     * @param {string} ext The filename extension.
+     *     @default "txt"
+     * @param {string} label A metadata string appended to the filename. Useful
+     *     for identifying the file, beyond its extension, to external
+     *     applications.
+     *     @default ""
      * @returns {File}
      */
-    createTempFile: function createTempFile(name, type) {
-        if (name instanceof Ci.nsIFile) {
-            var file = name.clone();
-            if (!type || type == "file")
-                file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, octal(666));
-            else
-                file.createUnique(Ci.nsIFile.DIRECTORY_TYPE, octal(777));
-        }
-        else {
-            file = services.directory.get("TmpD", Ci.nsIFile);
-            file.append(this.config.tempFile + (name ? "." + name : ""));
-            file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, octal(666));
-        }
+    createTempFile: function createTempFile(ext = "txt", label = "") {
+        let file = services.directory.get("TmpD", Ci.nsIFile);
+        file.append(config.name + label + "." + ext);
+        file.createUnique(Ci.nsIFile.NORMAL_FILE_TYPE, octal(666));
 
         services.externalApp.deleteTemporaryFileOnExit(file);
 
@@ -505,7 +504,9 @@ var IO = Module("io", {
 
             function async(status) {
                 let output = stdout.read();
-                [stdin, stdout, cmd].forEach(function (f) f.exists() && f.remove(false));
+                for (let f of [stdin, stdout, cmd])
+                    if (f.exists())
+                        f.remove(false);
                 callback(result(status, output));
             }
 
@@ -543,9 +544,9 @@ var IO = Module("io", {
      * @returns {boolean} false if temp files couldn't be created,
      *     otherwise, the return value of *func*.
      */
-    withTempFiles: function withTempFiles(func, self, checked, ext) {
+    withTempFiles: function withTempFiles(func, self, checked, ext, label) {
         let args = array(util.range(0, func.length))
-                    .map(bind("createTempFile", this, ext)).array;
+                    .map(bind("createTempFile", this, ext, label)).array;
         try {
             if (!args.every(util.identity))
                 return false;
@@ -553,7 +554,7 @@ var IO = Module("io", {
         }
         finally {
             if (!checked || res !== true)
-                args.forEach(function (f) f.remove(false));
+                args.forEach(f => { f.remove(false); });
         }
         return res;
     }
@@ -639,7 +640,7 @@ var IO = Module("io", {
                 lines.push("\n\" vim: set ft=" + config.name + ":");
 
                 try {
-                    file.write(lines.join("\n"));
+                    file.write(lines.join("\n").concat("\n"));
                     dactyl.echomsg(_("io.writing", file.path.quote()), 2);
                 }
                 catch (e) {
@@ -675,13 +676,13 @@ var IO = Module("io", {
                     item.file = file;
                 }
 
-                rtItems.ftdetect.template = // {{{
+                rtItems.ftdetect.template = //{{{
 literal(/*" Vim filetype detection file
 <header>
 
 au BufNewFile,BufRead *<name>rc*,*.<fileext> set filetype=<name>
 */);//}}}
-                rtItems.ftplugin.template = // {{{
+                rtItems.ftplugin.template = //{{{
 literal(/*" Vim filetype plugin file
 <header>
 
@@ -708,7 +709,7 @@ endif
 let &cpo = s:cpo_save
 unlet s:cpo_save
 */);//}}}
-                rtItems.syntax.template = // {{{
+                rtItems.syntax.template = //{{{
 literal(/*" Vim syntax file
 <header>
 
@@ -809,10 +810,12 @@ unlet s:cpo_save
                         lines.last.push(item, sep);
                     }
                     lines.last.pop();
-                    return lines.map(function (l) l.join("")).join("\n").replace(/\s+\n/gm, "\n");
+                    return lines.map(l => l.join(""))
+                                .join("\n")
+                                .replace(/\s+\n/gm, "\n");
                 }//}}}
 
-                let params = { // {{{
+                let params = { //{{{
                     header: ['" Language:    ' + config.appName + ' configuration file',
                              '" Maintainer:  Doug Kearns <dougkearns@gmail.com>',
                              '" Version:     ' + config.version].join("\n"),
@@ -830,7 +833,7 @@ unlet s:cpo_save
                                         array(o.realNames for (o in options) if (o.type == "boolean"))
                                             .flatten().map(String.quote),
                                         ", ") + "]"
-                }; // }}}
+                }; //}}}
 
                 for (let { file, template } in values(rtItems)) {
                     try {
@@ -907,8 +910,8 @@ unlet s:cpo_save
                         _("command.run.noPrevious"));
 
                     arg = arg.replace(/(\\)*!/g,
-                        function (m) /^\\(\\\\)*!$/.test(m) ? m.replace("\\!", "!") : m.replace("!", io._lastRunCommand)
-                    );
+                                      m => (/^\\(\\\\)*!$/.test(m) ? m.replace("\\!", "!")
+                                                                   : m.replace("!", io._lastRunCommand)));
                 }
 
                 io._lastRunCommand = arg;
@@ -945,21 +948,21 @@ unlet s:cpo_save
                     }
                 }
             };
-            context.generate = function () iter(services.charset.getDecoderList());
+            context.generate = () => iter(services.charset.getDecoderList());
         };
 
         completion.directory = function directory(context, full) {
             this.file(context, full);
-            context.filters.push(function (item) item.isdir);
+            context.filters.push(item => item.isdir);
         };
 
         completion.environment = function environment(context) {
             context.title = ["Environment Variable", "Value"];
-            context.generate = function ()
+            context.generate = () =>
                 io.system(config.OS.isWindows ? "set" : "env")
                   .output.split("\n")
-                  .filter(function (line) line.indexOf("=") > 0)
-                  .map(function (line) line.match(/([^=]+)=(.*)/).slice(1));
+                  .filter(line => line.indexOf("=") > 0)
+                  .map(line => line.match(/([^=]+)=(.*)/).slice(1));
         };
 
         completion.file = function file(context, full, dir) {
@@ -986,10 +989,10 @@ unlet s:cpo_save
                 icon: function (f) this.isdir ? "resource://gre/res/html/folder.png"
                                               : "moz-icon://" + f.leafName
             };
-            context.compare = function (a, b) b.isdir - a.isdir || String.localeCompare(a.text, b.text);
+            context.compare = (a, b) => b.isdir - a.isdir || String.localeCompare(a.text, b.text);
 
             if (modules.options["wildignore"])
-                context.filters.push(function (item) !modules.options.get("wildignore").getKey(item.path));
+                context.filters.push(item => !modules.options.get("wildignore").getKey(item.path));
 
             // context.background = true;
             context.key = dir;
@@ -1057,7 +1060,7 @@ unlet s:cpo_save
                 if (!match.path) {
                     context.key = match.proto;
                     context.advance(match.proto.length);
-                    context.generate = function () config.chromePackages.map(function (p) [p, match.proto + p + "/"]);
+                    context.generate = () => config.chromePackages.map(p => [p, match.proto + p + "/"]);
                 }
                 else if (match.scheme === "chrome") {
                     context.key = match.prefix;
@@ -1124,8 +1127,8 @@ unlet s:cpo_save
             "List of directories searched when executing :cd",
             "stringlist", ["."].concat(services.environment.get("CDPATH").split(/[:;]/).filter(util.identity)).join(","),
             {
-                get files() this.value.map(function (path) File(path, modules.io.cwd))
-                                .filter(function (dir) dir.exists()),
+                get files() this.value.map(path => File(path, modules.io.cwd))
+                                .filter(dir => dir.exists()),
                 setter: function (value) File.expandPathList(value)
             });
 
@@ -1133,8 +1136,8 @@ unlet s:cpo_save
             "List of directories searched for runtime files",
             "stringlist", IO.runtimePath,
             {
-                get files() this.value.map(function (path) File(path, modules.io.cwd))
-                                .filter(function (dir) dir.exists())
+                get files() this.value.map(path => File(path, modules.io.cwd))
+                                .filter(dir => dir.exists())
             });
 
         options.add(["shell", "sh"],
@@ -1165,4 +1168,4 @@ endModule();
 
 } catch(e){ if (isString(e)) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
 
-// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
+// vim: set fdm=marker sw=4 sts=4 ts=8 et ft=javascript: