]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/content/abbreviations.js
Imported Upstream version 1.1+hg7904
[dactyl.git] / common / content / abbreviations.js
index 08294970d684e4e6541513620430f99c9129e4d7..7ceb5e9afe57e8a994e96982f8c48610085a6552 100644 (file)
@@ -1,10 +1,10 @@
 // Copyright (c) 2006-2009 by Martin Stubenschrott <stubenschrott@vimperator.org>
 // Copyright (c) 2010 by anekos <anekos@snca.net>
-// Copyright (c) 2010-2011 by Kris Maglione <maglione.k at Gmail>
+// Copyright (c) 2010-2014 Kris Maglione <maglione.k at Gmail>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
-/* use strict */
+"use strict";
 
 /** @scope modules */
 
@@ -64,7 +64,7 @@ var Abbreviation = Class("Abbreviation", {
      * @param {Mode} mode The mode to test.
      * @returns {boolean} The result of the comparison.
      */
-    inMode: function (mode) this.modes.some(function (_mode) _mode == mode),
+    inMode: function (mode) this.modes.some(m => m == mode),
 
     /**
      * Returns true if this abbreviation is defined in any of *modes*.
@@ -72,7 +72,7 @@ var Abbreviation = Class("Abbreviation", {
      * @param {[Modes]} modes The modes to test.
      * @returns {boolean} The result of the comparison.
      */
-    inModes: function (modes) modes.some(function (mode) this.inMode(mode), this),
+    inModes: function (modes) modes.some(mode => this.inMode(mode)),
 
     /**
      * Remove *mode* from the list of supported modes for this abbreviation.
@@ -80,7 +80,8 @@ var Abbreviation = Class("Abbreviation", {
      * @param {Mode} mode The mode to remove.
      */
     removeMode: function (mode) {
-        this.modes = this.modes.filter(function (m) m != mode).sort();
+        this.modes = this.modes.filter(m => m != mode)
+                               .sort();
     },
 
     /**
@@ -90,7 +91,7 @@ var Abbreviation = Class("Abbreviation", {
     get modeChar() Abbreviation.modeChar(this.modes)
 }, {
     modeChar: function (_modes) {
-        let result = array.uniq(_modes.map(function (m) m.char)).join("");
+        let result = array.uniq(_modes.map(m => m.char)).join("");
         if (result == "ci")
             result = "!";
         return result;
@@ -104,7 +105,7 @@ var AbbrevHive = Class("AbbrevHive", Contexts.Hive, {
     },
 
     /** @property {boolean} True if there are no abbreviations. */
-    get empty() !values(this._store).nth(util.identity, 0),
+    get empty() !values(this._store).find(util.identity),
 
     /**
      * Adds a new abbreviation.
@@ -131,7 +132,8 @@ var AbbrevHive = Class("AbbrevHive", Contexts.Hive, {
      */
     get: function (mode, lhs) {
         let abbrevs = this._store[mode];
-        return abbrevs && Set.has(abbrevs, lhs) ? abbrevs[lhs] : null;
+        return abbrevs && hasOwnProperty(abbrevs, lhs) ? abbrevs[lhs]
+                                                       : null;
     },
 
     /**
@@ -142,7 +144,7 @@ var AbbrevHive = Class("AbbrevHive", Contexts.Hive, {
         // Wth? --Kris;
         let map = values(this._store).map(Iterator).map(iter.toArray)
                                      .flatten().toObject();
-        return Object.keys(map).sort().map(function (k) map[k]);
+        return Object.keys(map).sort().map(k => map[k]);
     },
 
     /**
@@ -213,24 +215,28 @@ var Abbreviations = Module("abbreviations", {
             nonkeyword: /[   "']/
         };
 
-        this._match = util.regexp(<><![CDATA[
+        this._match = util.regexp(literal(/*
             (^ | \s | <nonkeyword>) (<keyword>+             )$ | // full-id
             (^ | \s | <keyword>   ) (<nonkeyword>+ <keyword>)$ | // end-id
             (^ | \s               ) (\S* <nonkeyword>       )$   // non-id
-        ]]></>, "x", params);
-        this._check = util.regexp(<><![CDATA[
+        */), "x", params);
+        this._check = util.regexp(literal(/*
             ^ (?:
               <keyword>+              | // full-id
               <nonkeyword>+ <keyword> | // end-id
               \S* <nonkeyword>          // non-id
             ) $
-        ]]></>, "x", params);
+        */), "x", params);
     },
 
-    get: deprecated("group.abbrevs.get", { get: function get() this.user.closure.get }),
-    set: deprecated("group.abbrevs.set", { get: function set() this.user.closure.set }),
-    remove: deprecated("group.abbrevs.remove", { get: function remove() this.user.closure.remove }),
-    removeAll: deprecated("group.abbrevs.clear", { get: function removeAll() this.user.closure.clear }),
+    get allHives() contexts.allGroups.abbrevs,
+
+    get userHives() this.allHives.filter(h => h !== this.builtin),
+
+    get: deprecated("group.abbrevs.get", { get: function get() this.user.bound.get }),
+    set: deprecated("group.abbrevs.set", { get: function set() this.user.bound.set }),
+    remove: deprecated("group.abbrevs.remove", { get: function remove() this.user.bound.remove }),
+    removeAll: deprecated("group.abbrevs.clear", { get: function removeAll() this.user.bound.clear }),
 
     /**
      * Returns the abbreviation for the given *mode* if *text* matches the
@@ -244,7 +250,8 @@ var Abbreviations = Module("abbreviations", {
     match: function (mode, text) {
         let match = this._match.exec(text);
         if (match)
-            return this.hives.map(function (h) h.get(mode, match[2] || match[4] || match[6])).nth(util.identity, 0);
+            return this.hives.map(h => h.get(mode, match[2] || match[4] || match[6]))
+                       .find(util.identity);
         return null;
     },
 
@@ -257,38 +264,34 @@ var Abbreviations = Module("abbreviations", {
      * @optional
      */
     list: function (modes, lhs, hives) {
-        let hives = hives || contexts.allGroups.abbrevs.filter(function (h) !h.empty);
+        let hives = (hives || this.userHives).filter(h => !h.empty);
 
         function abbrevs(hive)
-            hive.merged.filter(function (abbr) (abbr.inModes(modes) && abbr.lhs.indexOf(lhs) == 0));
-
-        let list = <table>
-                <tr highlight="Title">
-                    <td/>
-                    <td style="padding-right: 1em;">{_("title.Mode")}</td>
-                    <td style="padding-right: 1em;">{_("title.Abbrev")}</td>
-                    <td style="padding-right: 1em;">{_("title.Replacement")}</td>
-                </tr>
-                <col style="min-width: 6em; padding-right: 1em;"/>
-                {
-                    template.map(hives, function (hive) let (i = 0)
-                        <tr style="height: .5ex;"/> +
-                        template.map(abbrevs(hive), function (abbrev)
-                            <tr>
-                                <td highlight="Title">{!i++ ? hive.name : ""}</td>
-                                <td>{abbrev.modeChar}</td>
-                                <td>{abbrev.lhs}</td>
-                                <td>{abbrev.rhs}</td>
-                            </tr>) +
-                        <tr style="height: .5ex;"/>)
-                }
-                </table>;
-
-        // TODO: Move this to an ItemList to show this automatically
-        if (list.*.length() === list.text().length() + 2)
-            dactyl.echomsg(_("abbreviation.none"));
-        else
-            commandline.commandOutput(list);
+            hive.merged.filter(ab => (ab.inModes(modes) && ab.lhs.startsWith(lhs)));
+
+        let list = ["table", {},
+                ["tr", { highlight: "Title" },
+                    ["td"],
+                    ["td", { style: "padding-right: 1em;" }, _("title.Mode")],
+                    ["td", { style: "padding-right: 1em;" }, _("title.Abbrev")],
+                    ["td", { style: "padding-right: 1em;" }, _("title.Replacement")]],
+                ["col", { style: "min-width: 6em; padding-right: 1em;" }],
+                hives.map(hive => let (i = 0) [
+                    ["tr", { style: "height: .5ex;" }],
+                    abbrevs(hive).map(abbrev =>
+                        ["tr", {},
+                            ["td", { highlight: "Title" }, !i++ ? String(hive.name) : ""],
+                            ["td", {}, abbrev.modeChar],
+                            ["td", {}, abbrev.lhs],
+                            ["td", {}, abbrev.rhs]]),
+                    ["tr", { style: "height: .5ex;" }]])];
+
+        // FIXME?
+        // // TODO: Move this to an ItemList to show this automatically
+        // if (list.*.length() === list.text().length() + 2)
+        //     dactyl.echomsg(_("abbreviation.none"));
+        // else
+        commandline.commandOutput(list);
     }
 
 }, {
@@ -299,15 +302,16 @@ var Abbreviations = Module("abbreviations", {
             user: contexts.hives.abbrevs.user
         });
     },
-    completion: function () {
+    completion: function initCompletion() {
         completion.abbreviation = function abbreviation(context, modes, group) {
             group = group || abbreviations.user;
-            let fn = modes ? function (abbr) abbr.inModes(modes) : util.identity;
+            let fn = modes ? abbr => abbr.inModes(modes)
+                           : abbr => abbr;
             context.keys = { text: "lhs" , description: "rhs" };
             context.completions = group.merged.filter(fn);
         };
     },
-    commands: function () {
+    commands: function initCommands() {
         function addAbbreviationCommands(modes, ch, modeDescription) {
             modes.sort();
             modeDescription = modeDescription ? " in " + modeDescription + " mode" : "";
@@ -345,18 +349,22 @@ var Abbreviations = Module("abbreviations", {
                             description: "Expand this abbreviation by evaluating its right-hand-side as JavaScript"
                         }
                     ],
-                    serialize: function () [
-                        {
-                            command: this.name,
-                            arguments: [abbr.lhs],
-                            literalArg: abbr.rhs,
-                            options: {
-                                "-javascript": abbr.rhs ? null : undefined
+                    serialize: function () array(abbreviations.userHives)
+                        .filter(h => h.persist)
+                        .map(hive => [
+                            {
+                                command: this.name,
+                                arguments: [abbr.lhs],
+                                literalArg: abbr.rhs,
+                                options: {
+                                    "-group": hive.name == "user" ? undefined : hive.name,
+                                    "-javascript": callable(abbr.rhs) ? null : undefined
+                                }
                             }
-                        }
-                        for ([, abbr] in Iterator(abbreviations.user.merged))
-                        if (abbr.modesEqual(modes))
-                    ]
+                            for ([, abbr] in Iterator(hive.merged))
+                            if (abbr.modesEqual(modes))
+                        ]).
+                        flatten().array
                 });
 
             commands.add([ch + "una[bbreviate]"],
@@ -384,4 +392,4 @@ var Abbreviations = Module("abbreviations", {
     }
 });
 
-// vim: set fdm=marker sw=4 ts=4 et:
+// vim: set fdm=marker sw=4 sts=4 ts=8 et: