]> git.donarmstrong.com Git - dactyl.git/blobdiff - common/content/modes.js
Imported Upstream version 1.1+hg7904
[dactyl.git] / common / content / modes.js
index 838eab65d1186d67f684389d1e49ab79929fdb3d..d4cd27399cc9f6a7794167ab9391c8746b9f18f9 100644 (file)
@@ -1,6 +1,6 @@
 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
-// Copyright (c) 2008-2013 Kris Maglione <maglione.k@gmail.com>
+// Copyright (c) 2008-2014 Kris Maglione <maglione.k@gmail.com>
 //
 // This work is licensed for reuse under an MIT license. Details are
 // given in the LICENSE.txt file included with this file.
@@ -21,13 +21,7 @@ var Modes = Module("modes", {
         this._recording = false;
         this._replaying = false; // playing a macro
 
-        this._modeStack = update([], {
-            pop: function pop() {
-                if (this.length <= 1)
-                    throw Error("Trying to pop last element in mode stack");
-                return pop.superapply(this, arguments);
-            }
-        });
+        this._modeStack = Modes.ModeStack([]);
 
         this._modes = [];
         this._mainModes = [];
@@ -262,20 +256,21 @@ var Modes = Module("modes", {
 
     // show the current mode string in the command line
     show: function show() {
-        if (!loaded.modes)
+        if (!loaded.has("modes"))
             return;
 
         let msg = this._getModeMessage();
 
-        if (msg || loaded.commandline)
+        if (msg || loaded.has("commandline"))
             commandline.widgets.mode = msg || null;
     },
 
     remove: function remove(mode, covert) {
         if (covert && this.topOfStack.main != mode) {
             util.assert(mode != this.NORMAL);
-            for (let m; m = array.nth(this.modeStack, m => m.main == mode, 0);)
-                this._modeStack.splice(this._modeStack.indexOf(m));
+
+            this._modeStack = Modes.ModeStack(
+                this._modeStack.filter(m => m.main != mode));
         }
         else if (this.stack.some(m => m.main == mode)) {
             this.pop(mode);
@@ -334,10 +329,11 @@ var Modes = Module("modes", {
                                       { push: push }, push);
 
                 for (let [id, { obj, prop, test }] in Iterator(this.boundProperties)) {
-                    if (!obj.get())
+                    obj = obj.get();
+                    if (!obj)
                         delete this.boundProperties[id];
                     else
-                        this.topOfStack.saved[id] = { obj: obj.get(), prop: prop, value: obj.get()[prop], test: test };
+                        this.topOfStack.saved[id] = { obj: obj, prop: prop, value: obj[prop], test: test };
                 }
             }
 
@@ -446,9 +442,11 @@ var Modes = Module("modes", {
             this.allBases.indexOf(obj) >= 0 || callable(obj) && this instanceof obj,
 
         allBases: Class.Memoize(function () {
-            let seen = {}, res = [], queue = [this].concat(this.bases);
+            let seen = RealSet(),
+                res = [],
+                queue = [this].concat(this.bases);
             for (let mode in array.iterValues(queue))
-                if (!Set.add(seen, mode)) {
+                if (!seen.add(mode)) {
                     res.push(mode);
                     queue.push.apply(queue, mode.bases);
                 }
@@ -485,13 +483,21 @@ var Modes = Module("modes", {
     }, {
         _id: 0
     }),
+    ModeStack: function ModeStack(array)
+        update(array, {
+            pop: function pop() {
+                if (this.length <= 1)
+                    throw Error("Trying to pop last element in mode stack");
+                return pop.superapply(this, arguments);
+            }
+        }),
     StackElement: (function () {
         const StackElement = Struct("main", "extended", "params", "saved");
         StackElement.className = "Modes.StackElement";
         StackElement.defaultValue("params", function () this.main.params);
 
         update(StackElement.prototype, {
-            get toStringParams() !loaded.modes ? this.main.name : [
+            get toStringParams() !loaded.has("modes") ? [this.main.name] : [
                 this.main.name,
                 ["(", modes.all.filter(m => this.extended & m)
                                .map(m => m.name)
@@ -502,7 +508,7 @@ var Modes = Module("modes", {
         return StackElement;
     })(),
     cacheId: 0,
-    boundProperty: function BoundProperty(desc = {}) {
+    boundProperty: function BoundProperty(desc={}) {
         let id = this.cacheId++;
         let value;
 
@@ -560,9 +566,10 @@ var Modes = Module("modes", {
             return rec(roots);
         }
 
-        cache.register("modes.dtd", () =>
-            util.makeDTD(iter({ "modes.tree": makeTree() },
-                              config.dtd)));
+        cache.register("modes.dtd",
+            () => util.makeDTD(iter({ "modes.tree": makeTree() },
+                                    config.dtd)),
+            true);
     },
     mappings: function initMappings() {
         mappings.add([modes.BASE, modes.NORMAL],
@@ -601,10 +608,10 @@ var Modes = Module("modes", {
 
             getKey: function getKey(val, default_) {
                 if (isArray(val))
-                    return (array.nth(this.value, v => val.some(m => m.name === v.mode), 0)
+                    return (this.value.find(v => val.some(m => m.name === v.mode))
                                 || { result: default_ }).result;
 
-                return Set.has(this.valueMap, val) ? this.valueMap[val] : default_;
+                return hasOwnProperty(this.valueMap, val) ? this.valueMap[val] : default_;
             },
 
             setter: function (vals) {
@@ -622,9 +629,10 @@ var Modes = Module("modes", {
             },
 
             validator: function validator(vals) vals.map(v => v.replace(/^!/, ""))
-                                                    .every(Set.has(this.values)),
+                                                    .every(k => hasOwnProperty(this.values, k)),
 
-            get values() array.toObject([[m.name.toLowerCase(), m.description] for (m in values(modes._modes)) if (!m.hidden)])
+            get values() array.toObject([[m.name.toLowerCase(), m.description]
+                                         for (m in values(modes._modes)) if (!m.hidden)])
         };
 
         options.add(["passunknown", "pu"],